LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.6 % 9068 8489
Test Date: 2025-04-19 15:48:17 Functions: 97.8 % 226 221
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Perform type resolution on the various structures.
       2                 :             :    Copyright (C) 2001-2025 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                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "options.h"
      25                 :             : #include "bitmap.h"
      26                 :             : #include "gfortran.h"
      27                 :             : #include "arith.h"  /* For gfc_compare_expr().  */
      28                 :             : #include "dependency.h"
      29                 :             : #include "data.h"
      30                 :             : #include "target-memory.h" /* for gfc_simplify_transfer */
      31                 :             : #include "constructor.h"
      32                 :             : 
      33                 :             : /* Types used in equivalence statements.  */
      34                 :             : 
      35                 :             : enum seq_type
      36                 :             : {
      37                 :             :   SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
      38                 :             : };
      39                 :             : 
      40                 :             : /* Stack to keep track of the nesting of blocks as we move through the
      41                 :             :    code.  See resolve_branch() and gfc_resolve_code().  */
      42                 :             : 
      43                 :             : typedef struct code_stack
      44                 :             : {
      45                 :             :   struct gfc_code *head, *current;
      46                 :             :   struct code_stack *prev;
      47                 :             : 
      48                 :             :   /* This bitmap keeps track of the targets valid for a branch from
      49                 :             :      inside this block except for END {IF|SELECT}s of enclosing
      50                 :             :      blocks.  */
      51                 :             :   bitmap reachable_labels;
      52                 :             : }
      53                 :             : code_stack;
      54                 :             : 
      55                 :             : static code_stack *cs_base = NULL;
      56                 :             : 
      57                 :             : struct check_default_none_data
      58                 :             : {
      59                 :             :   gfc_code *code;
      60                 :             :   hash_set<gfc_symbol *> *sym_hash;
      61                 :             :   gfc_namespace *ns;
      62                 :             :   bool default_none;
      63                 :             : };
      64                 :             : 
      65                 :             : /* Nonzero if we're inside a FORALL or DO CONCURRENT block.  */
      66                 :             : 
      67                 :             : static int forall_flag;
      68                 :             : int gfc_do_concurrent_flag;
      69                 :             : 
      70                 :             : /* True when we are resolving an expression that is an actual argument to
      71                 :             :    a procedure.  */
      72                 :             : static bool actual_arg = false;
      73                 :             : /* True when we are resolving an expression that is the first actual argument
      74                 :             :    to a procedure.  */
      75                 :             : static bool first_actual_arg = false;
      76                 :             : 
      77                 :             : 
      78                 :             : /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block.  */
      79                 :             : 
      80                 :             : static int omp_workshare_flag;
      81                 :             : 
      82                 :             : 
      83                 :             : /* True if we are resolving a specification expression.  */
      84                 :             : static bool specification_expr = false;
      85                 :             : 
      86                 :             : /* The id of the last entry seen.  */
      87                 :             : static int current_entry_id;
      88                 :             : 
      89                 :             : /* We use bitmaps to determine if a branch target is valid.  */
      90                 :             : static bitmap_obstack labels_obstack;
      91                 :             : 
      92                 :             : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function.  */
      93                 :             : static bool inquiry_argument = false;
      94                 :             : 
      95                 :             : 
      96                 :             : /* Is the symbol host associated?  */
      97                 :             : static bool
      98                 :       48429 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
      99                 :             : {
     100                 :       52833 :   for (ns = ns->parent; ns; ns = ns->parent)
     101                 :             :     {
     102                 :        4655 :       if (sym->ns == ns)
     103                 :             :         return true;
     104                 :             :     }
     105                 :             : 
     106                 :             :   return false;
     107                 :             : }
     108                 :             : 
     109                 :             : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
     110                 :             :    an ABSTRACT derived-type.  If where is not NULL, an error message with that
     111                 :             :    locus is printed, optionally using name.  */
     112                 :             : 
     113                 :             : static bool
     114                 :     1440167 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     115                 :             : {
     116                 :     1440167 :   if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
     117                 :             :     {
     118                 :           5 :       if (where)
     119                 :             :         {
     120                 :           5 :           if (name)
     121                 :           4 :             gfc_error ("%qs at %L is of the ABSTRACT type %qs",
     122                 :             :                        name, where, ts->u.derived->name);
     123                 :             :           else
     124                 :           1 :             gfc_error ("ABSTRACT type %qs used at %L",
     125                 :             :                        ts->u.derived->name, where);
     126                 :             :         }
     127                 :             : 
     128                 :           5 :       return false;
     129                 :             :     }
     130                 :             : 
     131                 :             :   return true;
     132                 :             : }
     133                 :             : 
     134                 :             : 
     135                 :             : static bool
     136                 :        4944 : check_proc_interface (gfc_symbol *ifc, locus *where)
     137                 :             : {
     138                 :             :   /* Several checks for F08:C1216.  */
     139                 :        4944 :   if (ifc->attr.procedure)
     140                 :             :     {
     141                 :           2 :       gfc_error ("Interface %qs at %L is declared "
     142                 :             :                  "in a later PROCEDURE statement", ifc->name, where);
     143                 :           2 :       return false;
     144                 :             :     }
     145                 :        4942 :   if (ifc->generic)
     146                 :             :     {
     147                 :             :       /* For generic interfaces, check if there is
     148                 :             :          a specific procedure with the same name.  */
     149                 :             :       gfc_interface *gen = ifc->generic;
     150                 :          12 :       while (gen && strcmp (gen->sym->name, ifc->name) != 0)
     151                 :           5 :         gen = gen->next;
     152                 :           7 :       if (!gen)
     153                 :             :         {
     154                 :           4 :           gfc_error ("Interface %qs at %L may not be generic",
     155                 :             :                      ifc->name, where);
     156                 :           4 :           return false;
     157                 :             :         }
     158                 :             :     }
     159                 :        4938 :   if (ifc->attr.proc == PROC_ST_FUNCTION)
     160                 :             :     {
     161                 :           4 :       gfc_error ("Interface %qs at %L may not be a statement function",
     162                 :             :                  ifc->name, where);
     163                 :           4 :       return false;
     164                 :             :     }
     165                 :        4934 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     166                 :        4934 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     167                 :          17 :     ifc->attr.intrinsic = 1;
     168                 :        4934 :   if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
     169                 :             :     {
     170                 :           3 :       gfc_error ("Intrinsic procedure %qs not allowed in "
     171                 :             :                  "PROCEDURE statement at %L", ifc->name, where);
     172                 :           3 :       return false;
     173                 :             :     }
     174                 :        4931 :   if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
     175                 :             :     {
     176                 :           7 :       gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
     177                 :           7 :       return false;
     178                 :             :     }
     179                 :             :   return true;
     180                 :             : }
     181                 :             : 
     182                 :             : 
     183                 :             : static void resolve_symbol (gfc_symbol *sym);
     184                 :             : 
     185                 :             : 
     186                 :             : /* Resolve the interface for a PROCEDURE declaration or procedure pointer.  */
     187                 :             : 
     188                 :             : static bool
     189                 :        1939 : resolve_procedure_interface (gfc_symbol *sym)
     190                 :             : {
     191                 :        1939 :   gfc_symbol *ifc = sym->ts.interface;
     192                 :             : 
     193                 :        1939 :   if (!ifc)
     194                 :             :     return true;
     195                 :             : 
     196                 :        1783 :   if (ifc == sym)
     197                 :             :     {
     198                 :           2 :       gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
     199                 :             :                  sym->name, &sym->declared_at);
     200                 :           2 :       return false;
     201                 :             :     }
     202                 :        1781 :   if (!check_proc_interface (ifc, &sym->declared_at))
     203                 :             :     return false;
     204                 :             : 
     205                 :        1772 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     206                 :             :     {
     207                 :             :       /* Resolve interface and copy attributes.  */
     208                 :        1493 :       resolve_symbol (ifc);
     209                 :        1493 :       if (ifc->attr.intrinsic)
     210                 :          14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     211                 :             : 
     212                 :        1493 :       if (ifc->result)
     213                 :             :         {
     214                 :         632 :           sym->ts = ifc->result->ts;
     215                 :         632 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     216                 :         632 :           sym->attr.pointer = ifc->result->attr.pointer;
     217                 :         632 :           sym->attr.dimension = ifc->result->attr.dimension;
     218                 :         632 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     219                 :         632 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     220                 :         632 :           sym->result = sym;
     221                 :             :         }
     222                 :             :       else
     223                 :             :         {
     224                 :         861 :           sym->ts = ifc->ts;
     225                 :         861 :           sym->attr.allocatable = ifc->attr.allocatable;
     226                 :         861 :           sym->attr.pointer = ifc->attr.pointer;
     227                 :         861 :           sym->attr.dimension = ifc->attr.dimension;
     228                 :         861 :           sym->attr.class_ok = ifc->attr.class_ok;
     229                 :         861 :           sym->as = gfc_copy_array_spec (ifc->as);
     230                 :             :         }
     231                 :        1493 :       sym->ts.interface = ifc;
     232                 :        1493 :       sym->attr.function = ifc->attr.function;
     233                 :        1493 :       sym->attr.subroutine = ifc->attr.subroutine;
     234                 :             : 
     235                 :        1493 :       sym->attr.pure = ifc->attr.pure;
     236                 :        1493 :       sym->attr.elemental = ifc->attr.elemental;
     237                 :        1493 :       sym->attr.contiguous = ifc->attr.contiguous;
     238                 :        1493 :       sym->attr.recursive = ifc->attr.recursive;
     239                 :        1493 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     240                 :        1493 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     241                 :        1493 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     242                 :             :       /* Copy char length.  */
     243                 :        1493 :       if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
     244                 :             :         {
     245                 :          45 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
     246                 :          45 :           if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
     247                 :          53 :               && !gfc_resolve_expr (sym->ts.u.cl->length))
     248                 :             :             return false;
     249                 :             :         }
     250                 :             :     }
     251                 :             : 
     252                 :             :   return true;
     253                 :             : }
     254                 :             : 
     255                 :             : 
     256                 :             : /* Resolve types of formal argument lists.  These have to be done early so that
     257                 :             :    the formal argument lists of module procedures can be copied to the
     258                 :             :    containing module before the individual procedures are resolved
     259                 :             :    individually.  We also resolve argument lists of procedures in interface
     260                 :             :    blocks because they are self-contained scoping units.
     261                 :             : 
     262                 :             :    Since a dummy argument cannot be a non-dummy procedure, the only
     263                 :             :    resort left for untyped names are the IMPLICIT types.  */
     264                 :             : 
     265                 :             : void
     266                 :      491827 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     267                 :             : {
     268                 :      491827 :   gfc_formal_arglist *f;
     269                 :      491827 :   gfc_symbol *sym;
     270                 :      491827 :   bool saved_specification_expr;
     271                 :      491827 :   int i;
     272                 :             : 
     273                 :      491827 :   if (proc->result != NULL)
     274                 :      312615 :     sym = proc->result;
     275                 :             :   else
     276                 :             :     sym = proc;
     277                 :             : 
     278                 :      491827 :   if (gfc_elemental (proc)
     279                 :      330525 :       || sym->attr.pointer || sym->attr.allocatable
     280                 :      810798 :       || (sym->as && sym->as->rank != 0))
     281                 :             :     {
     282                 :      175115 :       proc->attr.always_explicit = 1;
     283                 :      175115 :       sym->attr.always_explicit = 1;
     284                 :             :     }
     285                 :             : 
     286                 :      491827 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     287                 :      491827 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     288                 :             : 
     289                 :     1259114 :   for (f = proc->formal; f; f = f->next)
     290                 :             :     {
     291                 :      767289 :       gfc_array_spec *as;
     292                 :             : 
     293                 :      767289 :       sym = f->sym;
     294                 :             : 
     295                 :      767289 :       if (sym == NULL)
     296                 :             :         {
     297                 :             :           /* Alternate return placeholder.  */
     298                 :         171 :           if (gfc_elemental (proc))
     299                 :           1 :             gfc_error ("Alternate return specifier in elemental subroutine "
     300                 :             :                        "%qs at %L is not allowed", proc->name,
     301                 :             :                        &proc->declared_at);
     302                 :         171 :           if (proc->attr.function)
     303                 :           1 :             gfc_error ("Alternate return specifier in function "
     304                 :             :                        "%qs at %L is not allowed", proc->name,
     305                 :             :                        &proc->declared_at);
     306                 :         171 :           continue;
     307                 :             :         }
     308                 :             : 
     309                 :         536 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     310                 :      767654 :                && !resolve_procedure_interface (sym))
     311                 :             :         break;
     312                 :             : 
     313                 :      767118 :       if (strcmp (proc->name, sym->name) == 0)
     314                 :             :         {
     315                 :           2 :           gfc_error ("Self-referential argument "
     316                 :             :                      "%qs at %L is not allowed", sym->name,
     317                 :             :                      &proc->declared_at);
     318                 :           2 :           break;
     319                 :             :         }
     320                 :             : 
     321                 :      767116 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     322                 :         798 :         gfc_resolve_formal_arglist (sym);
     323                 :             : 
     324                 :      767116 :       if (sym->attr.subroutine || sym->attr.external)
     325                 :             :         {
     326                 :         799 :           if (sym->attr.flavor == FL_UNKNOWN)
     327                 :           9 :             gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
     328                 :             :         }
     329                 :             :       else
     330                 :             :         {
     331                 :      766317 :           if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
     332                 :        3659 :               && (!sym->attr.function || sym->result == sym))
     333                 :        3622 :             gfc_set_default_type (sym, 1, sym->ns);
     334                 :             :         }
     335                 :             : 
     336                 :       12951 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     337                 :      780067 :            ? CLASS_DATA (sym)->as : sym->as;
     338                 :             : 
     339                 :      767116 :       saved_specification_expr = specification_expr;
     340                 :      767116 :       specification_expr = true;
     341                 :      767116 :       gfc_resolve_array_spec (as, 0);
     342                 :      767116 :       specification_expr = saved_specification_expr;
     343                 :             : 
     344                 :             :       /* We can't tell if an array with dimension (:) is assumed or deferred
     345                 :             :          shape until we know if it has the pointer or allocatable attributes.
     346                 :             :       */
     347                 :      767116 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     348                 :       11901 :           && ((sym->ts.type != BT_CLASS
     349                 :       10838 :                && !(sym->attr.pointer || sym->attr.allocatable))
     350                 :        5184 :               || (sym->ts.type == BT_CLASS
     351                 :        1063 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     352                 :             :                        || CLASS_DATA (sym)->attr.allocatable)))
     353                 :        7172 :           && sym->attr.flavor != FL_PROCEDURE)
     354                 :             :         {
     355                 :        7171 :           as->type = AS_ASSUMED_SHAPE;
     356                 :       16671 :           for (i = 0; i < as->rank; i++)
     357                 :        9500 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     358                 :             :         }
     359                 :             : 
     360                 :      115115 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     361                 :      101984 :           || (as && as->type == AS_ASSUMED_RANK)
     362                 :      722488 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     363                 :      712559 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     364                 :       10951 :               && (CLASS_DATA (sym)->attr.class_pointer
     365                 :             :                   || CLASS_DATA (sym)->attr.allocatable
     366                 :       10951 :                   || CLASS_DATA (sym)->attr.target))
     367                 :      711217 :           || sym->attr.optional)
     368                 :             :         {
     369                 :       69168 :           proc->attr.always_explicit = 1;
     370                 :       69168 :           if (proc->result)
     371                 :       33671 :             proc->result->attr.always_explicit = 1;
     372                 :             :         }
     373                 :             : 
     374                 :             :       /* If the flavor is unknown at this point, it has to be a variable.
     375                 :             :          A procedure specification would have already set the type.  */
     376                 :             : 
     377                 :      767116 :       if (sym->attr.flavor == FL_UNKNOWN)
     378                 :       46972 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     379                 :             : 
     380                 :      767116 :       if (gfc_pure (proc))
     381                 :             :         {
     382                 :      325281 :           if (sym->attr.flavor == FL_PROCEDURE)
     383                 :             :             {
     384                 :             :               /* F08:C1279.  */
     385                 :          29 :               if (!gfc_pure (sym))
     386                 :             :                 {
     387                 :           1 :                   gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
     388                 :             :                             "also be PURE", sym->name, &sym->declared_at);
     389                 :           1 :                   continue;
     390                 :             :                 }
     391                 :             :             }
     392                 :      325252 :           else if (!sym->attr.pointer)
     393                 :             :             {
     394                 :      325244 :               if (proc->attr.function && sym->attr.intent != INTENT_IN)
     395                 :             :                 {
     396                 :         109 :                   if (sym->attr.value)
     397                 :         108 :                     gfc_notify_std (GFC_STD_F2008, "Argument %qs"
     398                 :             :                                     " of pure function %qs at %L with VALUE "
     399                 :             :                                     "attribute but without INTENT(IN)",
     400                 :             :                                     sym->name, proc->name, &sym->declared_at);
     401                 :             :                   else
     402                 :           1 :                     gfc_error ("Argument %qs of pure function %qs at %L must "
     403                 :             :                                "be INTENT(IN) or VALUE", sym->name, proc->name,
     404                 :             :                                &sym->declared_at);
     405                 :             :                 }
     406                 :             : 
     407                 :      325244 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
     408                 :             :                 {
     409                 :         159 :                   if (sym->attr.value)
     410                 :         159 :                     gfc_notify_std (GFC_STD_F2008, "Argument %qs"
     411                 :             :                                     " of pure subroutine %qs at %L with VALUE "
     412                 :             :                                     "attribute but without INTENT", sym->name,
     413                 :             :                                     proc->name, &sym->declared_at);
     414                 :             :                   else
     415                 :           0 :                     gfc_error ("Argument %qs of pure subroutine %qs at %L "
     416                 :             :                                "must have its INTENT specified or have the "
     417                 :             :                                "VALUE attribute", sym->name, proc->name,
     418                 :             :                                &sym->declared_at);
     419                 :             :                 }
     420                 :             :             }
     421                 :             : 
     422                 :             :           /* F08:C1278a.  */
     423                 :      325280 :           if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
     424                 :             :             {
     425                 :           1 :               gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
     426                 :             :                          " may not be polymorphic", sym->name, proc->name,
     427                 :             :                          &sym->declared_at);
     428                 :           1 :               continue;
     429                 :             :             }
     430                 :             :         }
     431                 :             : 
     432                 :      767114 :       if (proc->attr.implicit_pure)
     433                 :             :         {
     434                 :       23580 :           if (sym->attr.flavor == FL_PROCEDURE)
     435                 :             :             {
     436                 :         285 :               if (!gfc_pure (sym))
     437                 :         267 :                 proc->attr.implicit_pure = 0;
     438                 :             :             }
     439                 :       23295 :           else if (!sym->attr.pointer)
     440                 :             :             {
     441                 :       22514 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     442                 :        2605 :                   && !sym->value)
     443                 :        2605 :                 proc->attr.implicit_pure = 0;
     444                 :             : 
     445                 :       22514 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     446                 :        4054 :                   && !sym->value)
     447                 :        4054 :                 proc->attr.implicit_pure = 0;
     448                 :             :             }
     449                 :             :         }
     450                 :             : 
     451                 :      767114 :       if (gfc_elemental (proc))
     452                 :             :         {
     453                 :             :           /* F08:C1289.  */
     454                 :      299995 :           if (sym->attr.codimension
     455                 :      299994 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     456                 :         889 :                   && CLASS_DATA (sym)->attr.codimension))
     457                 :             :             {
     458                 :           3 :               gfc_error ("Coarray dummy argument %qs at %L to elemental "
     459                 :             :                          "procedure", sym->name, &sym->declared_at);
     460                 :           3 :               continue;
     461                 :             :             }
     462                 :             : 
     463                 :      299992 :           if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     464                 :         887 :                           && CLASS_DATA (sym)->as))
     465                 :             :             {
     466                 :           2 :               gfc_error ("Argument %qs of elemental procedure at %L must "
     467                 :             :                          "be scalar", sym->name, &sym->declared_at);
     468                 :           2 :               continue;
     469                 :             :             }
     470                 :             : 
     471                 :      299990 :           if (sym->attr.allocatable
     472                 :      299989 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     473                 :         886 :                   && CLASS_DATA (sym)->attr.allocatable))
     474                 :             :             {
     475                 :           2 :               gfc_error ("Argument %qs of elemental procedure at %L cannot "
     476                 :             :                          "have the ALLOCATABLE attribute", sym->name,
     477                 :             :                          &sym->declared_at);
     478                 :           2 :               continue;
     479                 :             :             }
     480                 :             : 
     481                 :      299988 :           if (sym->attr.pointer
     482                 :      299987 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     483                 :         885 :                   && CLASS_DATA (sym)->attr.class_pointer))
     484                 :             :             {
     485                 :           2 :               gfc_error ("Argument %qs of elemental procedure at %L cannot "
     486                 :             :                          "have the POINTER attribute", sym->name,
     487                 :             :                          &sym->declared_at);
     488                 :           2 :               continue;
     489                 :             :             }
     490                 :             : 
     491                 :      299986 :           if (sym->attr.flavor == FL_PROCEDURE)
     492                 :             :             {
     493                 :           2 :               gfc_error ("Dummy procedure %qs not allowed in elemental "
     494                 :             :                          "procedure %qs at %L", sym->name, proc->name,
     495                 :             :                          &sym->declared_at);
     496                 :           2 :               continue;
     497                 :             :             }
     498                 :             : 
     499                 :             :           /* Fortran 2008 Corrigendum 1, C1290a.  */
     500                 :      299984 :           if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
     501                 :             :             {
     502                 :           2 :               gfc_error ("Argument %qs of elemental procedure %qs at %L must "
     503                 :             :                          "have its INTENT specified or have the VALUE "
     504                 :             :                          "attribute", sym->name, proc->name,
     505                 :             :                          &sym->declared_at);
     506                 :           2 :               continue;
     507                 :             :             }
     508                 :             :         }
     509                 :             : 
     510                 :             :       /* Each dummy shall be specified to be scalar.  */
     511                 :      767101 :       if (proc->attr.proc == PROC_ST_FUNCTION)
     512                 :             :         {
     513                 :         305 :           if (sym->as != NULL)
     514                 :             :             {
     515                 :             :               /* F03:C1263 (R1238) The function-name and each dummy-arg-name
     516                 :             :                  shall be specified, explicitly or implicitly, to be scalar.  */
     517                 :           1 :               gfc_error ("Argument %qs of statement function %qs at %L "
     518                 :             :                          "must be scalar", sym->name, proc->name,
     519                 :             :                          &proc->declared_at);
     520                 :           1 :               continue;
     521                 :             :             }
     522                 :             : 
     523                 :         304 :           if (sym->ts.type == BT_CHARACTER)
     524                 :             :             {
     525                 :          48 :               gfc_charlen *cl = sym->ts.u.cl;
     526                 :          48 :               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
     527                 :             :                 {
     528                 :           0 :                   gfc_error ("Character-valued argument %qs of statement "
     529                 :             :                              "function at %L must have constant length",
     530                 :             :                              sym->name, &sym->declared_at);
     531                 :           0 :                   continue;
     532                 :             :                 }
     533                 :             :             }
     534                 :             :         }
     535                 :             :     }
     536                 :             : 
     537                 :      491827 :   gfc_current_ns = orig_current_ns;
     538                 :      491827 : }
     539                 :             : 
     540                 :             : 
     541                 :             : /* Work function called when searching for symbols that have argument lists
     542                 :             :    associated with them.  */
     543                 :             : 
     544                 :             : static void
     545                 :     1720289 : find_arglists (gfc_symbol *sym)
     546                 :             : {
     547                 :     1720289 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     548                 :      312143 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     549                 :             :     return;
     550                 :             : 
     551                 :      310528 :   gfc_resolve_formal_arglist (sym);
     552                 :             : }
     553                 :             : 
     554                 :             : 
     555                 :             : /* Given a namespace, resolve all formal argument lists within the namespace.
     556                 :             :  */
     557                 :             : 
     558                 :             : static void
     559                 :      326171 : resolve_formal_arglists (gfc_namespace *ns)
     560                 :             : {
     561                 :           0 :   if (ns == NULL)
     562                 :             :     return;
     563                 :             : 
     564                 :      326171 :   gfc_traverse_ns (ns, find_arglists);
     565                 :             : }
     566                 :             : 
     567                 :             : 
     568                 :             : static void
     569                 :       35521 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     570                 :             : {
     571                 :       35521 :   bool t;
     572                 :             : 
     573                 :       35521 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     574                 :       35521 :       && sym->ns->parent
     575                 :        1062 :       && sym->ns->parent->proc_name
     576                 :        1062 :       && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
     577                 :           1 :       && !strcmp (sym->name, sym->ns->parent->proc_name->name))
     578                 :           0 :     gfc_error ("Contained procedure %qs at %L has the same name as its "
     579                 :             :                "encompassing procedure", sym->name, &sym->declared_at);
     580                 :             : 
     581                 :             :   /* If this namespace is not a function or an entry master function,
     582                 :             :      ignore it.  */
     583                 :       35521 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     584                 :       10311 :       || sym->attr.entry_master)
     585                 :       25398 :     return;
     586                 :             : 
     587                 :       10123 :   if (!sym->result)
     588                 :             :     return;
     589                 :             : 
     590                 :             :   /* Try to find out of what the return type is.  */
     591                 :       10123 :   if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
     592                 :             :     {
     593                 :          55 :       t = gfc_set_default_type (sym->result, 0, ns);
     594                 :             : 
     595                 :          55 :       if (!t && !sym->result->attr.untyped)
     596                 :             :         {
     597                 :          19 :           if (sym->result == sym)
     598                 :           1 :             gfc_error ("Contained function %qs at %L has no IMPLICIT type",
     599                 :             :                        sym->name, &sym->declared_at);
     600                 :          18 :           else if (!sym->result->attr.proc_pointer)
     601                 :           0 :             gfc_error ("Result %qs of contained function %qs at %L has "
     602                 :             :                        "no IMPLICIT type", sym->result->name, sym->name,
     603                 :             :                        &sym->result->declared_at);
     604                 :          19 :           sym->result->attr.untyped = 1;
     605                 :             :         }
     606                 :             :     }
     607                 :             : 
     608                 :             :   /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
     609                 :             :      type, lists the only ways a character length value of * can be used:
     610                 :             :      dummy arguments of procedures, named constants, function results and
     611                 :             :      in allocate statements if the allocate_object is an assumed length dummy
     612                 :             :      in external functions.  Internal function results and results of module
     613                 :             :      procedures are not on this list, ergo, not permitted.  */
     614                 :             : 
     615                 :       10123 :   if (sym->result->ts.type == BT_CHARACTER)
     616                 :             :     {
     617                 :        1151 :       gfc_charlen *cl = sym->result->ts.u.cl;
     618                 :        1151 :       if ((!cl || !cl->length) && !sym->result->ts.deferred)
     619                 :             :         {
     620                 :             :           /* See if this is a module-procedure and adapt error message
     621                 :             :              accordingly.  */
     622                 :           4 :           bool module_proc;
     623                 :           4 :           gcc_assert (ns->parent && ns->parent->proc_name);
     624                 :           4 :           module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
     625                 :             : 
     626                 :           7 :           gfc_error (module_proc
     627                 :             :                      ? G_("Character-valued module procedure %qs at %L"
     628                 :             :                           " must not be assumed length")
     629                 :             :                      : G_("Character-valued internal function %qs at %L"
     630                 :             :                           " must not be assumed length"),
     631                 :             :                      sym->name, &sym->declared_at);
     632                 :             :         }
     633                 :             :     }
     634                 :             : }
     635                 :             : 
     636                 :             : 
     637                 :             : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
     638                 :             :    introduce duplicates.  */
     639                 :             : 
     640                 :             : static void
     641                 :        1420 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     642                 :             : {
     643                 :        1420 :   gfc_formal_arglist *f, *new_arglist;
     644                 :        1420 :   gfc_symbol *new_sym;
     645                 :             : 
     646                 :        2561 :   for (; new_args != NULL; new_args = new_args->next)
     647                 :             :     {
     648                 :        1141 :       new_sym = new_args->sym;
     649                 :             :       /* See if this arg is already in the formal argument list.  */
     650                 :        2165 :       for (f = proc->formal; f; f = f->next)
     651                 :             :         {
     652                 :        1470 :           if (new_sym == f->sym)
     653                 :             :             break;
     654                 :             :         }
     655                 :             : 
     656                 :        1141 :       if (f)
     657                 :         446 :         continue;
     658                 :             : 
     659                 :             :       /* Add a new argument.  Argument order is not important.  */
     660                 :         695 :       new_arglist = gfc_get_formal_arglist ();
     661                 :         695 :       new_arglist->sym = new_sym;
     662                 :         695 :       new_arglist->next = proc->formal;
     663                 :         695 :       proc->formal  = new_arglist;
     664                 :             :     }
     665                 :        1420 : }
     666                 :             : 
     667                 :             : 
     668                 :             : /* Flag the arguments that are not present in all entries.  */
     669                 :             : 
     670                 :             : static void
     671                 :        1420 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     672                 :             : {
     673                 :        1420 :   gfc_formal_arglist *f, *head;
     674                 :        1420 :   head = new_args;
     675                 :             : 
     676                 :        2994 :   for (f = proc->formal; f; f = f->next)
     677                 :             :     {
     678                 :        1574 :       if (f->sym == NULL)
     679                 :          36 :         continue;
     680                 :             : 
     681                 :        2704 :       for (new_args = head; new_args; new_args = new_args->next)
     682                 :             :         {
     683                 :        2262 :           if (new_args->sym == f->sym)
     684                 :             :             break;
     685                 :             :         }
     686                 :             : 
     687                 :        1538 :       if (new_args)
     688                 :        1096 :         continue;
     689                 :             : 
     690                 :         442 :       f->sym->attr.not_always_present = 1;
     691                 :             :     }
     692                 :        1420 : }
     693                 :             : 
     694                 :             : 
     695                 :             : /* Resolve alternate entry points.  If a symbol has multiple entry points we
     696                 :             :    create a new master symbol for the main routine, and turn the existing
     697                 :             :    symbol into an entry point.  */
     698                 :             : 
     699                 :             : static void
     700                 :      361187 : resolve_entries (gfc_namespace *ns)
     701                 :             : {
     702                 :      361187 :   gfc_namespace *old_ns;
     703                 :      361187 :   gfc_code *c;
     704                 :      361187 :   gfc_symbol *proc;
     705                 :      361187 :   gfc_entry_list *el;
     706                 :             :   /* Provide sufficient space to hold "master.%d.%s".  */
     707                 :      361187 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     708                 :      361187 :   static int master_count = 0;
     709                 :             : 
     710                 :      361187 :   if (ns->proc_name == NULL)
     711                 :      360519 :     return;
     712                 :             : 
     713                 :             :   /* No need to do anything if this procedure doesn't have alternate entry
     714                 :             :      points.  */
     715                 :      361139 :   if (!ns->entries)
     716                 :             :     return;
     717                 :             : 
     718                 :             :   /* We may already have resolved alternate entry points.  */
     719                 :         918 :   if (ns->proc_name->attr.entry_master)
     720                 :             :     return;
     721                 :             : 
     722                 :             :   /* If this isn't a procedure something has gone horribly wrong.  */
     723                 :         668 :   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
     724                 :             : 
     725                 :             :   /* Remember the current namespace.  */
     726                 :         668 :   old_ns = gfc_current_ns;
     727                 :             : 
     728                 :         668 :   gfc_current_ns = ns;
     729                 :             : 
     730                 :             :   /* Add the main entry point to the list of entry points.  */
     731                 :         668 :   el = gfc_get_entry_list ();
     732                 :         668 :   el->sym = ns->proc_name;
     733                 :         668 :   el->id = 0;
     734                 :         668 :   el->next = ns->entries;
     735                 :         668 :   ns->entries = el;
     736                 :         668 :   ns->proc_name->attr.entry = 1;
     737                 :             : 
     738                 :             :   /* If it is a module function, it needs to be in the right namespace
     739                 :             :      so that gfc_get_fake_result_decl can gather up the results. The
     740                 :             :      need for this arose in get_proc_name, where these beasts were
     741                 :             :      left in their own namespace, to keep prior references linked to
     742                 :             :      the entry declaration.*/
     743                 :         668 :   if (ns->proc_name->attr.function
     744                 :         564 :       && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
     745                 :         188 :     el->sym->ns = ns;
     746                 :             : 
     747                 :             :   /* Do the same for entries where the master is not a module
     748                 :             :      procedure.  These are retained in the module namespace because
     749                 :             :      of the module procedure declaration.  */
     750                 :        1420 :   for (el = el->next; el; el = el->next)
     751                 :         752 :     if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
     752                 :           0 :           && el->sym->attr.mod_proc)
     753                 :           0 :       el->sym->ns = ns;
     754                 :         668 :   el = ns->entries;
     755                 :             : 
     756                 :             :   /* Add an entry statement for it.  */
     757                 :         668 :   c = gfc_get_code (EXEC_ENTRY);
     758                 :         668 :   c->ext.entry = el;
     759                 :         668 :   c->next = ns->code;
     760                 :         668 :   ns->code = c;
     761                 :             : 
     762                 :             :   /* Create a new symbol for the master function.  */
     763                 :             :   /* Give the internal function a unique name (within this file).
     764                 :             :      Also include the function name so the user has some hope of figuring
     765                 :             :      out what is going on.  */
     766                 :         668 :   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
     767                 :         668 :             master_count++, ns->proc_name->name);
     768                 :         668 :   gfc_get_ha_symbol (name, &proc);
     769                 :         668 :   gcc_assert (proc != NULL);
     770                 :             : 
     771                 :         668 :   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
     772                 :         668 :   if (ns->proc_name->attr.subroutine)
     773                 :         104 :     gfc_add_subroutine (&proc->attr, proc->name, NULL);
     774                 :             :   else
     775                 :             :     {
     776                 :         564 :       gfc_symbol *sym;
     777                 :         564 :       gfc_typespec *ts, *fts;
     778                 :         564 :       gfc_array_spec *as, *fas;
     779                 :         564 :       gfc_add_function (&proc->attr, proc->name, NULL);
     780                 :         564 :       proc->result = proc;
     781                 :         564 :       fas = ns->entries->sym->as;
     782                 :         564 :       fas = fas ? fas : ns->entries->sym->result->as;
     783                 :         564 :       fts = &ns->entries->sym->result->ts;
     784                 :         564 :       if (fts->type == BT_UNKNOWN)
     785                 :          51 :         fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
     786                 :        1058 :       for (el = ns->entries->next; el; el = el->next)
     787                 :             :         {
     788                 :         603 :           ts = &el->sym->result->ts;
     789                 :         603 :           as = el->sym->as;
     790                 :         603 :           as = as ? as : el->sym->result->as;
     791                 :         603 :           if (ts->type == BT_UNKNOWN)
     792                 :          61 :             ts = gfc_get_default_type (el->sym->result->name, NULL);
     793                 :             : 
     794                 :         603 :           if (! gfc_compare_types (ts, fts)
     795                 :         497 :               || (el->sym->result->attr.dimension
     796                 :         497 :                   != ns->entries->sym->result->attr.dimension)
     797                 :         603 :               || (el->sym->result->attr.pointer
     798                 :         497 :                   != ns->entries->sym->result->attr.pointer))
     799                 :             :             break;
     800                 :          65 :           else if (as && fas && ns->entries->sym->result != el->sym->result
     801                 :         559 :                       && gfc_compare_array_spec (as, fas) == 0)
     802                 :           5 :             gfc_error ("Function %s at %L has entries with mismatched "
     803                 :             :                        "array specifications", ns->entries->sym->name,
     804                 :           5 :                        &ns->entries->sym->declared_at);
     805                 :             :           /* The characteristics need to match and thus both need to have
     806                 :             :              the same string length, i.e. both len=*, or both len=4.
     807                 :             :              Having both len=<variable> is also possible, but difficult to
     808                 :             :              check at compile time.  */
     809                 :         492 :           else if (ts->type == BT_CHARACTER
     810                 :          89 :                    && (el->sym->result->attr.allocatable
     811                 :          89 :                        != ns->entries->sym->result->attr.allocatable))
     812                 :             :             {
     813                 :           3 :               gfc_error ("Function %s at %L has entry %s with mismatched "
     814                 :             :                          "characteristics", ns->entries->sym->name,
     815                 :             :                          &ns->entries->sym->declared_at, el->sym->name);
     816                 :           3 :               goto cleanup;
     817                 :             :             }
     818                 :         489 :           else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
     819                 :          86 :                    && (((ts->u.cl->length && !fts->u.cl->length)
     820                 :          85 :                         ||(!ts->u.cl->length && fts->u.cl->length))
     821                 :          66 :                        || (ts->u.cl->length
     822                 :          29 :                            && ts->u.cl->length->expr_type
     823                 :          29 :                               != fts->u.cl->length->expr_type)
     824                 :          66 :                        || (ts->u.cl->length
     825                 :          29 :                            && ts->u.cl->length->expr_type == EXPR_CONSTANT
     826                 :          28 :                            && mpz_cmp (ts->u.cl->length->value.integer,
     827                 :          28 :                                        fts->u.cl->length->value.integer) != 0)))
     828                 :          21 :             gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
     829                 :             :                             "entries returning variables of different "
     830                 :             :                             "string lengths", ns->entries->sym->name,
     831                 :          21 :                             &ns->entries->sym->declared_at);
     832                 :         468 :           else if (el->sym->result->attr.allocatable
     833                 :         468 :                    != ns->entries->sym->result->attr.allocatable)
     834                 :             :             break;
     835                 :             :         }
     836                 :             : 
     837                 :         561 :       if (el == NULL)
     838                 :             :         {
     839                 :         455 :           sym = ns->entries->sym->result;
     840                 :             :           /* All result types the same.  */
     841                 :         455 :           proc->ts = *fts;
     842                 :         455 :           if (sym->attr.dimension)
     843                 :          63 :             gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
     844                 :         455 :           if (sym->attr.pointer)
     845                 :          78 :             gfc_add_pointer (&proc->attr, NULL);
     846                 :         455 :           if (sym->attr.allocatable)
     847                 :          24 :             gfc_add_allocatable (&proc->attr, NULL);
     848                 :             :         }
     849                 :             :       else
     850                 :             :         {
     851                 :             :           /* Otherwise the result will be passed through a union by
     852                 :             :              reference.  */
     853                 :         106 :           proc->attr.mixed_entry_master = 1;
     854                 :         340 :           for (el = ns->entries; el; el = el->next)
     855                 :             :             {
     856                 :         234 :               sym = el->sym->result;
     857                 :         234 :               if (sym->attr.dimension)
     858                 :             :                 {
     859                 :           1 :                   if (el == ns->entries)
     860                 :           0 :                     gfc_error ("FUNCTION result %s cannot be an array in "
     861                 :             :                                "FUNCTION %s at %L", sym->name,
     862                 :           0 :                                ns->entries->sym->name, &sym->declared_at);
     863                 :             :                   else
     864                 :           1 :                     gfc_error ("ENTRY result %s cannot be an array in "
     865                 :             :                                "FUNCTION %s at %L", sym->name,
     866                 :           1 :                                ns->entries->sym->name, &sym->declared_at);
     867                 :             :                 }
     868                 :         233 :               else if (sym->attr.pointer)
     869                 :             :                 {
     870                 :           1 :                   if (el == ns->entries)
     871                 :           1 :                     gfc_error ("FUNCTION result %s cannot be a POINTER in "
     872                 :             :                                "FUNCTION %s at %L", sym->name,
     873                 :           1 :                                ns->entries->sym->name, &sym->declared_at);
     874                 :             :                   else
     875                 :           0 :                     gfc_error ("ENTRY result %s cannot be a POINTER in "
     876                 :             :                                "FUNCTION %s at %L", sym->name,
     877                 :           0 :                                ns->entries->sym->name, &sym->declared_at);
     878                 :             :                 }
     879                 :         232 :               else if (sym->attr.allocatable)
     880                 :             :                 {
     881                 :           0 :                   if (el == ns->entries)
     882                 :           0 :                     gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
     883                 :             :                                "FUNCTION %s at %L", sym->name,
     884                 :           0 :                                ns->entries->sym->name, &sym->declared_at);
     885                 :             :                   else
     886                 :           0 :                     gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
     887                 :             :                                "FUNCTION %s at %L", sym->name,
     888                 :           0 :                                ns->entries->sym->name, &sym->declared_at);
     889                 :             :                 }
     890                 :             :               else
     891                 :             :                 {
     892                 :         232 :                   ts = &sym->ts;
     893                 :         232 :                   if (ts->type == BT_UNKNOWN)
     894                 :           9 :                     ts = gfc_get_default_type (sym->name, NULL);
     895                 :         232 :                   switch (ts->type)
     896                 :             :                     {
     897                 :          84 :                     case BT_INTEGER:
     898                 :          84 :                       if (ts->kind == gfc_default_integer_kind)
     899                 :             :                         sym = NULL;
     900                 :             :                       break;
     901                 :          99 :                     case BT_REAL:
     902                 :          99 :                       if (ts->kind == gfc_default_real_kind
     903                 :          18 :                           || ts->kind == gfc_default_double_kind)
     904                 :             :                         sym = NULL;
     905                 :             :                       break;
     906                 :          19 :                     case BT_COMPLEX:
     907                 :          19 :                       if (ts->kind == gfc_default_complex_kind)
     908                 :             :                         sym = NULL;
     909                 :             :                       break;
     910                 :          27 :                     case BT_LOGICAL:
     911                 :          27 :                       if (ts->kind == gfc_default_logical_kind)
     912                 :             :                         sym = NULL;
     913                 :             :                       break;
     914                 :             :                     case BT_UNKNOWN:
     915                 :             :                       /* We will issue error elsewhere.  */
     916                 :             :                       sym = NULL;
     917                 :             :                       break;
     918                 :             :                     default:
     919                 :             :                       break;
     920                 :             :                     }
     921                 :           3 :                   if (sym)
     922                 :             :                     {
     923                 :           3 :                       if (el == ns->entries)
     924                 :           1 :                         gfc_error ("FUNCTION result %s cannot be of type %s "
     925                 :             :                                    "in FUNCTION %s at %L", sym->name,
     926                 :           1 :                                    gfc_typename (ts), ns->entries->sym->name,
     927                 :             :                                    &sym->declared_at);
     928                 :             :                       else
     929                 :           2 :                         gfc_error ("ENTRY result %s cannot be of type %s "
     930                 :             :                                    "in FUNCTION %s at %L", sym->name,
     931                 :           2 :                                    gfc_typename (ts), ns->entries->sym->name,
     932                 :             :                                    &sym->declared_at);
     933                 :             :                     }
     934                 :             :                 }
     935                 :             :             }
     936                 :             :         }
     937                 :             :     }
     938                 :             : 
     939                 :         106 : cleanup:
     940                 :         668 :   proc->attr.access = ACCESS_PRIVATE;
     941                 :         668 :   proc->attr.entry_master = 1;
     942                 :             : 
     943                 :             :   /* Merge all the entry point arguments.  */
     944                 :        2088 :   for (el = ns->entries; el; el = el->next)
     945                 :        1420 :     merge_argument_lists (proc, el->sym->formal);
     946                 :             : 
     947                 :             :   /* Check the master formal arguments for any that are not
     948                 :             :      present in all entry points.  */
     949                 :        2088 :   for (el = ns->entries; el; el = el->next)
     950                 :        1420 :     check_argument_lists (proc, el->sym->formal);
     951                 :             : 
     952                 :             :   /* Use the master function for the function body.  */
     953                 :         668 :   ns->proc_name = proc;
     954                 :             : 
     955                 :             :   /* Finalize the new symbols.  */
     956                 :         668 :   gfc_commit_symbols ();
     957                 :             : 
     958                 :             :   /* Restore the original namespace.  */
     959                 :         668 :   gfc_current_ns = old_ns;
     960                 :             : }
     961                 :             : 
     962                 :             : 
     963                 :             : /* Forward declaration.  */
     964                 :             : static bool is_non_constant_shape_array (gfc_symbol *sym);
     965                 :             : 
     966                 :             : 
     967                 :             : /* Resolve common variables.  */
     968                 :             : static void
     969                 :      328066 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     970                 :             : {
     971                 :      328066 :   gfc_symbol *csym = common_block->head;
     972                 :      328066 :   gfc_gsymbol *gsym;
     973                 :             : 
     974                 :      334035 :   for (; csym; csym = csym->common_next)
     975                 :             :     {
     976                 :        5969 :       gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
     977                 :        5969 :       if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
     978                 :             :         {
     979                 :           3 :           if (csym->common_block)
     980                 :           2 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
     981                 :             :                            "COMMON block at %L", gsym->name,
     982                 :             :                            &gsym->where, &csym->common_block->where);
     983                 :             :           else
     984                 :           1 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
     985                 :             :                            "COMMON block", gsym->name, &gsym->where);
     986                 :             :         }
     987                 :             : 
     988                 :             :       /* gfc_add_in_common may have been called before, but the reported errors
     989                 :             :          have been ignored to continue parsing.
     990                 :             :          We do the checks again here, unless the symbol is USE associated.  */
     991                 :        5969 :       if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
     992                 :             :         {
     993                 :        5696 :           gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
     994                 :        5696 :           gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
     995                 :             :                           &common_block->where);
     996                 :             :         }
     997                 :             : 
     998                 :        5969 :       if (csym->value || csym->attr.data)
     999                 :             :         {
    1000                 :         131 :           if (!csym->ns->is_block_data)
    1001                 :          32 :             gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
    1002                 :             :                             "but only in BLOCK DATA initialization is "
    1003                 :             :                             "allowed", csym->name, &csym->declared_at);
    1004                 :          99 :           else if (!named_common)
    1005                 :           8 :             gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
    1006                 :             :                             "in a blank COMMON but initialization is only "
    1007                 :             :                             "allowed in named common blocks", csym->name,
    1008                 :             :                             &csym->declared_at);
    1009                 :             :         }
    1010                 :             : 
    1011                 :        5969 :       if (UNLIMITED_POLY (csym))
    1012                 :           1 :         gfc_error_now ("%qs at %L cannot appear in COMMON "
    1013                 :             :                        "[F2008:C5100]", csym->name, &csym->declared_at);
    1014                 :             : 
    1015                 :        5969 :       if (csym->attr.dimension && is_non_constant_shape_array (csym))
    1016                 :             :         {
    1017                 :           1 :           gfc_error_now ("Automatic object %qs at %L cannot appear in "
    1018                 :             :                          "COMMON at %L", csym->name, &csym->declared_at,
    1019                 :             :                          &common_block->where);
    1020                 :             :           /* Avoid confusing follow-on error.  */
    1021                 :           1 :           csym->error = 1;
    1022                 :             :         }
    1023                 :             : 
    1024                 :        5969 :       if (csym->ts.type != BT_DERIVED)
    1025                 :        5922 :         continue;
    1026                 :             : 
    1027                 :          47 :       if (!(csym->ts.u.derived->attr.sequence
    1028                 :             :             || csym->ts.u.derived->attr.is_bind_c))
    1029                 :           2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1030                 :             :                        "has neither the SEQUENCE nor the BIND(C) "
    1031                 :             :                        "attribute", csym->name, &csym->declared_at);
    1032                 :          47 :       if (csym->ts.u.derived->attr.alloc_comp)
    1033                 :           3 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1034                 :             :                        "has an ultimate component that is "
    1035                 :             :                        "allocatable", csym->name, &csym->declared_at);
    1036                 :          47 :       if (gfc_has_default_initializer (csym->ts.u.derived))
    1037                 :           2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1038                 :             :                        "may not have default initializer", csym->name,
    1039                 :             :                        &csym->declared_at);
    1040                 :             : 
    1041                 :          47 :       if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
    1042                 :          16 :         gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
    1043                 :             :     }
    1044                 :      328066 : }
    1045                 :             : 
    1046                 :             : /* Resolve common blocks.  */
    1047                 :             : static void
    1048                 :      326636 : resolve_common_blocks (gfc_symtree *common_root)
    1049                 :             : {
    1050                 :      326636 :   gfc_symbol *sym = NULL;
    1051                 :      326636 :   gfc_gsymbol * gsym;
    1052                 :             : 
    1053                 :      326636 :   if (common_root == NULL)
    1054                 :      326514 :     return;
    1055                 :             : 
    1056                 :        1895 :   if (common_root->left)
    1057                 :         211 :     resolve_common_blocks (common_root->left);
    1058                 :        1895 :   if (common_root->right)
    1059                 :         254 :     resolve_common_blocks (common_root->right);
    1060                 :             : 
    1061                 :        1895 :   resolve_common_vars (common_root->n.common, true);
    1062                 :             : 
    1063                 :             :   /* The common name is a global name - in Fortran 2003 also if it has a
    1064                 :             :      C binding name, since Fortran 2008 only the C binding name is a global
    1065                 :             :      identifier.  */
    1066                 :        1895 :   if (!common_root->n.common->binding_label
    1067                 :        1895 :       || gfc_notification_std (GFC_STD_F2008))
    1068                 :             :     {
    1069                 :        3646 :       gsym = gfc_find_gsymbol (gfc_gsym_root,
    1070                 :        1823 :                                common_root->n.common->name);
    1071                 :             : 
    1072                 :         820 :       if (gsym && gfc_notification_std (GFC_STD_F2008)
    1073                 :          14 :           && gsym->type == GSYM_COMMON
    1074                 :        1836 :           && ((common_root->n.common->binding_label
    1075                 :           6 :                && (!gsym->binding_label
    1076                 :           0 :                    || strcmp (common_root->n.common->binding_label,
    1077                 :             :                               gsym->binding_label) != 0))
    1078                 :           7 :               || (!common_root->n.common->binding_label
    1079                 :           7 :                   && gsym->binding_label)))
    1080                 :             :         {
    1081                 :           6 :           gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
    1082                 :             :                      "identifier and must thus have the same binding name "
    1083                 :             :                      "as the same-named COMMON block at %L: %s vs %s",
    1084                 :           6 :                      common_root->n.common->name, &common_root->n.common->where,
    1085                 :             :                      &gsym->where,
    1086                 :             :                      common_root->n.common->binding_label
    1087                 :             :                      ? common_root->n.common->binding_label : "(blank)",
    1088                 :           6 :                      gsym->binding_label ? gsym->binding_label : "(blank)");
    1089                 :           6 :           return;
    1090                 :             :         }
    1091                 :             : 
    1092                 :        1817 :       if (gsym && gsym->type != GSYM_COMMON
    1093                 :           1 :           && !common_root->n.common->binding_label)
    1094                 :             :         {
    1095                 :           0 :           gfc_error ("COMMON block %qs at %L uses the same global identifier "
    1096                 :             :                      "as entity at %L",
    1097                 :           0 :                      common_root->n.common->name, &common_root->n.common->where,
    1098                 :             :                      &gsym->where);
    1099                 :           0 :           return;
    1100                 :             :         }
    1101                 :         814 :       if (gsym && gsym->type != GSYM_COMMON)
    1102                 :             :         {
    1103                 :           1 :           gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
    1104                 :             :                      "%L sharing the identifier with global non-COMMON-block "
    1105                 :           1 :                      "entity at %L", common_root->n.common->name,
    1106                 :           1 :                      &common_root->n.common->where, &gsym->where);
    1107                 :           1 :           return;
    1108                 :             :         }
    1109                 :        1003 :       if (!gsym)
    1110                 :             :         {
    1111                 :        1003 :           gsym = gfc_get_gsymbol (common_root->n.common->name, false);
    1112                 :        1003 :           gsym->type = GSYM_COMMON;
    1113                 :        1003 :           gsym->where = common_root->n.common->where;
    1114                 :        1003 :           gsym->defined = 1;
    1115                 :             :         }
    1116                 :        1816 :       gsym->used = 1;
    1117                 :             :     }
    1118                 :             : 
    1119                 :        1888 :   if (common_root->n.common->binding_label)
    1120                 :             :     {
    1121                 :          76 :       gsym = gfc_find_gsymbol (gfc_gsym_root,
    1122                 :             :                                common_root->n.common->binding_label);
    1123                 :          76 :       if (gsym && gsym->type != GSYM_COMMON)
    1124                 :             :         {
    1125                 :           1 :           gfc_error ("COMMON block at %L with binding label %qs uses the same "
    1126                 :             :                      "global identifier as entity at %L",
    1127                 :             :                      &common_root->n.common->where,
    1128                 :           1 :                      common_root->n.common->binding_label, &gsym->where);
    1129                 :           1 :           return;
    1130                 :             :         }
    1131                 :          57 :       if (!gsym)
    1132                 :             :         {
    1133                 :          57 :           gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
    1134                 :          57 :           gsym->type = GSYM_COMMON;
    1135                 :          57 :           gsym->where = common_root->n.common->where;
    1136                 :          57 :           gsym->defined = 1;
    1137                 :             :         }
    1138                 :          75 :       gsym->used = 1;
    1139                 :             :     }
    1140                 :             : 
    1141                 :        1887 :   gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
    1142                 :        1887 :   if (sym == NULL)
    1143                 :             :     return;
    1144                 :             : 
    1145                 :         122 :   if (sym->attr.flavor == FL_PARAMETER)
    1146                 :           2 :     gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
    1147                 :           2 :                sym->name, &common_root->n.common->where, &sym->declared_at);
    1148                 :             : 
    1149                 :         122 :   if (sym->attr.external)
    1150                 :           1 :     gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
    1151                 :           1 :                sym->name, &common_root->n.common->where);
    1152                 :             : 
    1153                 :         122 :   if (sym->attr.intrinsic)
    1154                 :           2 :     gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
    1155                 :           2 :                sym->name, &common_root->n.common->where);
    1156                 :         120 :   else if (sym->attr.result
    1157                 :         120 :            || gfc_is_function_return_value (sym, gfc_current_ns))
    1158                 :           1 :     gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
    1159                 :             :                     "that is also a function result", sym->name,
    1160                 :           1 :                     &common_root->n.common->where);
    1161                 :         119 :   else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
    1162                 :           5 :            && sym->attr.proc != PROC_ST_FUNCTION)
    1163                 :           3 :     gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
    1164                 :             :                     "that is also a global procedure", sym->name,
    1165                 :           3 :                     &common_root->n.common->where);
    1166                 :             : }
    1167                 :             : 
    1168                 :             : 
    1169                 :             : /* Resolve contained function types.  Because contained functions can call one
    1170                 :             :    another, they have to be worked out before any of the contained procedures
    1171                 :             :    can be resolved.
    1172                 :             : 
    1173                 :             :    The good news is that if a function doesn't already have a type, the only
    1174                 :             :    way it can get one is through an IMPLICIT type or a RESULT variable, because
    1175                 :             :    by definition contained functions are contained namespace they're contained
    1176                 :             :    in, not in a sibling or parent namespace.  */
    1177                 :             : 
    1178                 :             : static void
    1179                 :      326171 : resolve_contained_functions (gfc_namespace *ns)
    1180                 :             : {
    1181                 :      326171 :   gfc_namespace *child;
    1182                 :      326171 :   gfc_entry_list *el;
    1183                 :             : 
    1184                 :      326171 :   resolve_formal_arglists (ns);
    1185                 :             : 
    1186                 :      361187 :   for (child = ns->contained; child; child = child->sibling)
    1187                 :             :     {
    1188                 :             :       /* Resolve alternate entry points first.  */
    1189                 :       35016 :       resolve_entries (child);
    1190                 :             : 
    1191                 :             :       /* Then check function return types.  */
    1192                 :       35016 :       resolve_contained_fntype (child->proc_name, child);
    1193                 :       35521 :       for (el = child->entries; el; el = el->next)
    1194                 :         505 :         resolve_contained_fntype (el->sym, child);
    1195                 :             :     }
    1196                 :      326171 : }
    1197                 :             : 
    1198                 :             : 
    1199                 :             : 
    1200                 :             : /* A Parameterized Derived Type constructor must contain values for
    1201                 :             :    the PDT KIND parameters or they must have a default initializer.
    1202                 :             :    Go through the constructor picking out the KIND expressions,
    1203                 :             :    storing them in 'param_list' and then call gfc_get_pdt_instance
    1204                 :             :    to obtain the PDT instance.  */
    1205                 :             : 
    1206                 :             : static gfc_actual_arglist *param_list, *param_tail, *param;
    1207                 :             : 
    1208                 :             : static bool
    1209                 :          24 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
    1210                 :             : {
    1211                 :          24 :   param = gfc_get_actual_arglist ();
    1212                 :          24 :   if (!param_list)
    1213                 :          18 :     param_list = param_tail = param;
    1214                 :             :   else
    1215                 :             :     {
    1216                 :           6 :       param_tail->next = param;
    1217                 :           6 :       param_tail = param_tail->next;
    1218                 :             :     }
    1219                 :             : 
    1220                 :          24 :   param_tail->name = c->name;
    1221                 :          24 :   if (expr)
    1222                 :          24 :     param_tail->expr = gfc_copy_expr (expr);
    1223                 :           0 :   else if (c->initializer)
    1224                 :           0 :     param_tail->expr = gfc_copy_expr (c->initializer);
    1225                 :             :   else
    1226                 :             :     {
    1227                 :           0 :       param_tail->spec_type = SPEC_ASSUMED;
    1228                 :           0 :       if (c->attr.pdt_kind)
    1229                 :             :         {
    1230                 :           0 :           gfc_error ("The KIND parameter %qs in the PDT constructor "
    1231                 :             :                      "at %C has no value", param->name);
    1232                 :           0 :           return false;
    1233                 :             :         }
    1234                 :             :     }
    1235                 :             : 
    1236                 :             :   return true;
    1237                 :             : }
    1238                 :             : 
    1239                 :             : static bool
    1240                 :          18 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
    1241                 :             :                      gfc_symbol *derived)
    1242                 :             : {
    1243                 :          18 :   gfc_constructor *cons = NULL;
    1244                 :          18 :   gfc_component *comp;
    1245                 :          18 :   bool t = true;
    1246                 :             : 
    1247                 :          18 :   if (expr && expr->expr_type == EXPR_STRUCTURE)
    1248                 :          18 :     cons = gfc_constructor_first (expr->value.constructor);
    1249                 :           0 :   else if (constr)
    1250                 :           0 :     cons = *constr;
    1251                 :          18 :   gcc_assert (cons);
    1252                 :             : 
    1253                 :          18 :   comp = derived->components;
    1254                 :             : 
    1255                 :          66 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1256                 :             :     {
    1257                 :          48 :       if (cons->expr
    1258                 :          48 :           && cons->expr->expr_type == EXPR_STRUCTURE
    1259                 :           0 :           && comp->ts.type == BT_DERIVED)
    1260                 :             :         {
    1261                 :           0 :           t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
    1262                 :           0 :           if (!t)
    1263                 :             :             return t;
    1264                 :             :         }
    1265                 :          48 :       else if (comp->ts.type == BT_DERIVED)
    1266                 :             :         {
    1267                 :           0 :           t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
    1268                 :           0 :           if (!t)
    1269                 :             :             return t;
    1270                 :             :         }
    1271                 :          48 :      else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
    1272                 :          24 :                && derived->attr.pdt_template)
    1273                 :             :         {
    1274                 :          24 :           t = get_pdt_spec_expr (comp, cons->expr);
    1275                 :          24 :           if (!t)
    1276                 :             :             return t;
    1277                 :             :         }
    1278                 :             :     }
    1279                 :             :   return t;
    1280                 :             : }
    1281                 :             : 
    1282                 :             : 
    1283                 :             : static bool resolve_fl_derived0 (gfc_symbol *sym);
    1284                 :             : static bool resolve_fl_struct (gfc_symbol *sym);
    1285                 :             : 
    1286                 :             : 
    1287                 :             : /* Resolve all of the elements of a structure constructor and make sure that
    1288                 :             :    the types are correct. The 'init' flag indicates that the given
    1289                 :             :    constructor is an initializer.  */
    1290                 :             : 
    1291                 :             : static bool
    1292                 :       58658 : resolve_structure_cons (gfc_expr *expr, int init)
    1293                 :             : {
    1294                 :       58658 :   gfc_constructor *cons;
    1295                 :       58658 :   gfc_component *comp;
    1296                 :       58658 :   bool t;
    1297                 :       58658 :   symbol_attribute a;
    1298                 :             : 
    1299                 :       58658 :   t = true;
    1300                 :             : 
    1301                 :       58658 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1302                 :             :     {
    1303                 :       55910 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1304                 :       55760 :         resolve_fl_derived0 (expr->ts.u.derived);
    1305                 :             :       else
    1306                 :         150 :         resolve_fl_struct (expr->ts.u.derived);
    1307                 :             : 
    1308                 :             :       /* If this is a Parameterized Derived Type template, find the
    1309                 :             :          instance corresponding to the PDT kind parameters.  */
    1310                 :       55910 :       if (expr->ts.u.derived->attr.pdt_template)
    1311                 :             :         {
    1312                 :          18 :           param_list = NULL;
    1313                 :          18 :           t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
    1314                 :          18 :           if (!t)
    1315                 :             :             return t;
    1316                 :          18 :           gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
    1317                 :             : 
    1318                 :          18 :           expr->param_list = gfc_copy_actual_arglist (param_list);
    1319                 :             : 
    1320                 :          18 :           if (param_list)
    1321                 :          18 :             gfc_free_actual_arglist (param_list);
    1322                 :             : 
    1323                 :          18 :           if (!expr->ts.u.derived->attr.pdt_type)
    1324                 :             :             return false;
    1325                 :             :         }
    1326                 :             :     }
    1327                 :             : 
    1328                 :             :   /* A constructor may have references if it is the result of substituting a
    1329                 :             :      parameter variable.  In this case we just pull out the component we
    1330                 :             :      want.  */
    1331                 :       58658 :   if (expr->ref)
    1332                 :         144 :     comp = expr->ref->u.c.sym->components;
    1333                 :       58514 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1334                 :             :             || expr->ts.type == BT_UNION)
    1335                 :       58512 :            && expr->ts.u.derived)
    1336                 :       58512 :     comp = expr->ts.u.derived->components;
    1337                 :             :   else
    1338                 :             :     return false;
    1339                 :             : 
    1340                 :       58656 :   cons = gfc_constructor_first (expr->value.constructor);
    1341                 :             : 
    1342                 :      193528 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1343                 :             :     {
    1344                 :      134874 :       int rank;
    1345                 :             : 
    1346                 :      134874 :       if (!cons->expr)
    1347                 :        8891 :         continue;
    1348                 :             : 
    1349                 :             :       /* Unions use an EXPR_NULL contrived expression to tell the translation
    1350                 :             :          phase to generate an initializer of the appropriate length.
    1351                 :             :          Ignore it here.  */
    1352                 :      125983 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1353                 :          15 :         continue;
    1354                 :             : 
    1355                 :      125968 :       if (!gfc_resolve_expr (cons->expr))
    1356                 :             :         {
    1357                 :           0 :           t = false;
    1358                 :           0 :           continue;
    1359                 :             :         }
    1360                 :             : 
    1361                 :      125968 :       rank = comp->as ? comp->as->rank : 0;
    1362                 :      125968 :       if (comp->ts.type == BT_CLASS
    1363                 :        1639 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1364                 :        1638 :           && CLASS_DATA (comp)->as)
    1365                 :         473 :         rank = CLASS_DATA (comp)->as->rank;
    1366                 :             : 
    1367                 :      125968 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1368                 :         191 :           gfc_find_vtab (&cons->expr->ts);
    1369                 :             : 
    1370                 :      125968 :       if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
    1371                 :         444 :           && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
    1372                 :             :         {
    1373                 :           4 :           gfc_error ("The rank of the element in the structure "
    1374                 :             :                      "constructor at %L does not match that of the "
    1375                 :             :                      "component (%d/%d)", &cons->expr->where,
    1376                 :             :                      cons->expr->rank, rank);
    1377                 :           4 :           t = false;
    1378                 :             :         }
    1379                 :             : 
    1380                 :             :       /* If we don't have the right type, try to convert it.  */
    1381                 :             : 
    1382                 :      220417 :       if (!comp->attr.proc_pointer &&
    1383                 :       94449 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1384                 :             :         {
    1385                 :       11466 :           if (strcmp (comp->name, "_extends") == 0)
    1386                 :             :             {
    1387                 :             :               /* Can afford to be brutal with the _extends initializer.
    1388                 :             :                  The derived type can get lost because it is PRIVATE
    1389                 :             :                  but it is not usage constrained by the standard.  */
    1390                 :        8354 :               cons->expr->ts = comp->ts;
    1391                 :             :             }
    1392                 :        3112 :           else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
    1393                 :             :             {
    1394                 :           2 :               gfc_error ("The element in the structure constructor at %L, "
    1395                 :             :                          "for pointer component %qs, is %s but should be %s",
    1396                 :           2 :                          &cons->expr->where, comp->name,
    1397                 :           2 :                          gfc_basic_typename (cons->expr->ts.type),
    1398                 :             :                          gfc_basic_typename (comp->ts.type));
    1399                 :           2 :               t = false;
    1400                 :             :             }
    1401                 :        3110 :           else if (!UNLIMITED_POLY (comp))
    1402                 :             :             {
    1403                 :        3072 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1404                 :        3072 :               if (t)
    1405                 :      125968 :                 t = t2;
    1406                 :             :             }
    1407                 :             :         }
    1408                 :             : 
    1409                 :             :       /* For strings, the length of the constructor should be the same as
    1410                 :             :          the one of the structure, ensure this if the lengths are known at
    1411                 :             :          compile time and when we are dealing with PARAMETER or structure
    1412                 :             :          constructors.  */
    1413                 :      125968 :       if (cons->expr->ts.type == BT_CHARACTER
    1414                 :        3712 :           && comp->ts.type == BT_CHARACTER
    1415                 :        3693 :           && comp->ts.u.cl && comp->ts.u.cl->length
    1416                 :        2406 :           && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1417                 :        2377 :           && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
    1418                 :         861 :           && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1419                 :         861 :           && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
    1420                 :         861 :           && comp->ts.u.cl->length->ts.type == BT_INTEGER
    1421                 :         861 :           && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
    1422                 :         861 :                       comp->ts.u.cl->length->value.integer) != 0)
    1423                 :             :         {
    1424                 :          11 :           if (comp->attr.pointer)
    1425                 :             :             {
    1426                 :           3 :               HOST_WIDE_INT la, lb;
    1427                 :           3 :               la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
    1428                 :           3 :               lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
    1429                 :           3 :               gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
    1430                 :             :                          "component %qs in constructor at %L",
    1431                 :           3 :                          la, lb, comp->name, &cons->expr->where);
    1432                 :           3 :               t = false;
    1433                 :             :             }
    1434                 :             : 
    1435                 :          11 :           if (cons->expr->expr_type == EXPR_VARIABLE
    1436                 :           4 :               && cons->expr->rank != 0
    1437                 :           2 :               && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
    1438                 :             :             {
    1439                 :             :               /* Wrap the parameter in an array constructor (EXPR_ARRAY)
    1440                 :             :                  to make use of the gfc_resolve_character_array_constructor
    1441                 :             :                  machinery.  The expression is later simplified away to
    1442                 :             :                  an array of string literals.  */
    1443                 :           1 :               gfc_expr *para = cons->expr;
    1444                 :           1 :               cons->expr = gfc_get_expr ();
    1445                 :           1 :               cons->expr->ts = para->ts;
    1446                 :           1 :               cons->expr->where = para->where;
    1447                 :           1 :               cons->expr->expr_type = EXPR_ARRAY;
    1448                 :           1 :               cons->expr->rank = para->rank;
    1449                 :           1 :               cons->expr->corank = para->corank;
    1450                 :           1 :               cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
    1451                 :           1 :               gfc_constructor_append_expr (&cons->expr->value.constructor,
    1452                 :           1 :                                            para, &cons->expr->where);
    1453                 :             :             }
    1454                 :             : 
    1455                 :          11 :           if (cons->expr->expr_type == EXPR_ARRAY)
    1456                 :             :             {
    1457                 :             :               /* Rely on the cleanup of the namespace to deal correctly with
    1458                 :             :                  the old charlen.  (There was a block here that attempted to
    1459                 :             :                  remove the charlen but broke the chain in so doing.)  */
    1460                 :           5 :               cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    1461                 :           5 :               cons->expr->ts.u.cl->length_from_typespec = true;
    1462                 :           5 :               cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
    1463                 :           5 :               gfc_resolve_character_array_constructor (cons->expr);
    1464                 :             :             }
    1465                 :             :         }
    1466                 :             : 
    1467                 :      125968 :       if (cons->expr->expr_type == EXPR_NULL
    1468                 :       38217 :           && !(comp->attr.pointer || comp->attr.allocatable
    1469                 :       37192 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1470                 :        1025 :                || (comp->ts.type == BT_CLASS
    1471                 :        1023 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1472                 :        1023 :                        || CLASS_DATA (comp)->attr.allocatable))))
    1473                 :             :         {
    1474                 :           2 :           t = false;
    1475                 :           2 :           gfc_error ("The NULL in the structure constructor at %L is "
    1476                 :             :                      "being applied to component %qs, which is neither "
    1477                 :             :                      "a POINTER nor ALLOCATABLE", &cons->expr->where,
    1478                 :             :                      comp->name);
    1479                 :             :         }
    1480                 :             : 
    1481                 :      125968 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1482                 :             :         {
    1483                 :             :           /* Check procedure pointer interface.  */
    1484                 :       13770 :           gfc_symbol *s2 = NULL;
    1485                 :       13770 :           gfc_component *c2;
    1486                 :       13770 :           const char *name;
    1487                 :       13770 :           char err[200];
    1488                 :             : 
    1489                 :       13770 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1490                 :       13770 :           if (c2)
    1491                 :             :             {
    1492                 :          12 :               s2 = c2->ts.interface;
    1493                 :          12 :               name = c2->name;
    1494                 :             :             }
    1495                 :       13758 :           else if (cons->expr->expr_type == EXPR_FUNCTION)
    1496                 :             :             {
    1497                 :           0 :               s2 = cons->expr->symtree->n.sym->result;
    1498                 :           0 :               name = cons->expr->symtree->n.sym->result->name;
    1499                 :             :             }
    1500                 :       13758 :           else if (cons->expr->expr_type != EXPR_NULL)
    1501                 :             :             {
    1502                 :       13389 :               s2 = cons->expr->symtree->n.sym;
    1503                 :       13389 :               name = cons->expr->symtree->n.sym->name;
    1504                 :             :             }
    1505                 :             : 
    1506                 :       13401 :           if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
    1507                 :             :                                              err, sizeof (err), NULL, NULL))
    1508                 :             :             {
    1509                 :           2 :               gfc_error_opt (0, "Interface mismatch for procedure-pointer "
    1510                 :             :                              "component %qs in structure constructor at %L:"
    1511                 :           2 :                              " %s", comp->name, &cons->expr->where, err);
    1512                 :           2 :               return false;
    1513                 :             :             }
    1514                 :             :         }
    1515                 :             : 
    1516                 :             :       /* Validate shape, except for dynamic or PDT arrays.  */
    1517                 :      125966 :       if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
    1518                 :        1922 :           && comp->as && !comp->attr.allocatable && !comp->attr.pointer
    1519                 :        1234 :           && !comp->attr.pdt_array)
    1520                 :             :         {
    1521                 :        1174 :           mpz_t len;
    1522                 :        1174 :           mpz_init (len);
    1523                 :        2445 :           for (int n = 0; n < rank; n++)
    1524                 :             :             {
    1525                 :        1272 :               if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
    1526                 :        1271 :                   || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
    1527                 :             :                 {
    1528                 :           1 :                   gfc_error ("Bad array spec of component %qs referenced in "
    1529                 :             :                              "structure constructor at %L",
    1530                 :           1 :                              comp->name, &cons->expr->where);
    1531                 :           1 :                   t = false;
    1532                 :           1 :                   break;
    1533                 :        1271 :                 };
    1534                 :        1271 :               if (cons->expr->shape == NULL)
    1535                 :          12 :                 continue;
    1536                 :        1259 :               mpz_set_ui (len, 1);
    1537                 :        1259 :               mpz_add (len, len, comp->as->upper[n]->value.integer);
    1538                 :        1259 :               mpz_sub (len, len, comp->as->lower[n]->value.integer);
    1539                 :        1259 :               if (mpz_cmp (cons->expr->shape[n], len) != 0)
    1540                 :             :                 {
    1541                 :           9 :                   gfc_error ("The shape of component %qs in the structure "
    1542                 :             :                              "constructor at %L differs from the shape of the "
    1543                 :             :                              "declared component for dimension %d (%ld/%ld)",
    1544                 :             :                              comp->name, &cons->expr->where, n+1,
    1545                 :             :                              mpz_get_si (cons->expr->shape[n]),
    1546                 :             :                              mpz_get_si (len));
    1547                 :           9 :                   t = false;
    1548                 :             :                 }
    1549                 :             :             }
    1550                 :        1174 :           mpz_clear (len);
    1551                 :             :         }
    1552                 :             : 
    1553                 :      125966 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1554                 :       20249 :           || cons->expr->expr_type == EXPR_NULL)
    1555                 :      116652 :         continue;
    1556                 :             : 
    1557                 :        9314 :       a = gfc_expr_attr (cons->expr);
    1558                 :             : 
    1559                 :        9314 :       if (!a.pointer && !a.target)
    1560                 :             :         {
    1561                 :           1 :           t = false;
    1562                 :           1 :           gfc_error ("The element in the structure constructor at %L, "
    1563                 :             :                      "for pointer component %qs should be a POINTER or "
    1564                 :           1 :                      "a TARGET", &cons->expr->where, comp->name);
    1565                 :             :         }
    1566                 :             : 
    1567                 :        9314 :       if (init)
    1568                 :             :         {
    1569                 :             :           /* F08:C461. Additional checks for pointer initialization.  */
    1570                 :        9246 :           if (a.allocatable)
    1571                 :             :             {
    1572                 :           0 :               t = false;
    1573                 :           0 :               gfc_error ("Pointer initialization target at %L "
    1574                 :           0 :                          "must not be ALLOCATABLE", &cons->expr->where);
    1575                 :             :             }
    1576                 :        9246 :           if (!a.save)
    1577                 :             :             {
    1578                 :           0 :               t = false;
    1579                 :           0 :               gfc_error ("Pointer initialization target at %L "
    1580                 :           0 :                          "must have the SAVE attribute", &cons->expr->where);
    1581                 :             :             }
    1582                 :             :         }
    1583                 :             : 
    1584                 :             :       /* F2023:C770: A designator that is an initial-data-target shall ...
    1585                 :             :          not have a vector subscript.  */
    1586                 :        9314 :       if (comp->attr.pointer && (a.pointer || a.target)
    1587                 :       18627 :           && gfc_has_vector_index (cons->expr))
    1588                 :             :         {
    1589                 :           1 :           gfc_error ("Pointer assignment target at %L has a vector subscript",
    1590                 :           1 :                      &cons->expr->where);
    1591                 :           1 :           t = false;
    1592                 :             :         }
    1593                 :             : 
    1594                 :             :       /* F2003, C1272 (3).  */
    1595                 :        9314 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1596                 :        9314 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1597                 :        9278 :                         || gfc_is_coindexed (cons->expr));
    1598                 :          33 :       if (impure && gfc_pure (NULL))
    1599                 :             :         {
    1600                 :           1 :           t = false;
    1601                 :           1 :           gfc_error ("Invalid expression in the structure constructor for "
    1602                 :             :                      "pointer component %qs at %L in PURE procedure",
    1603                 :           1 :                      comp->name, &cons->expr->where);
    1604                 :             :         }
    1605                 :             : 
    1606                 :        9314 :       if (impure)
    1607                 :          33 :         gfc_unset_implicit_pure (NULL);
    1608                 :             :     }
    1609                 :             : 
    1610                 :             :   return t;
    1611                 :             : }
    1612                 :             : 
    1613                 :             : 
    1614                 :             : /****************** Expression name resolution ******************/
    1615                 :             : 
    1616                 :             : /* Returns 0 if a symbol was not declared with a type or
    1617                 :             :    attribute declaration statement, nonzero otherwise.  */
    1618                 :             : 
    1619                 :             : static bool
    1620                 :      707974 : was_declared (gfc_symbol *sym)
    1621                 :             : {
    1622                 :      707974 :   symbol_attribute a;
    1623                 :             : 
    1624                 :      707974 :   a = sym->attr;
    1625                 :             : 
    1626                 :      707974 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1627                 :             :     return 1;
    1628                 :             : 
    1629                 :      598165 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1630                 :             :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1631                 :      598165 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1632                 :      589928 :       || a.asynchronous || a.codimension)
    1633                 :        8237 :     return 1;
    1634                 :             : 
    1635                 :             :   return 0;
    1636                 :             : }
    1637                 :             : 
    1638                 :             : 
    1639                 :             : /* Determine if a symbol is generic or not.  */
    1640                 :             : 
    1641                 :             : static int
    1642                 :      393919 : generic_sym (gfc_symbol *sym)
    1643                 :             : {
    1644                 :      393919 :   gfc_symbol *s;
    1645                 :             : 
    1646                 :      393919 :   if (sym->attr.generic ||
    1647                 :      366093 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1648                 :       28889 :     return 1;
    1649                 :             : 
    1650                 :      365030 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1651                 :             :     return 0;
    1652                 :             : 
    1653                 :       77025 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1654                 :             : 
    1655                 :       77025 :   if (s != NULL)
    1656                 :             :     {
    1657                 :        1264 :       if (s == sym)
    1658                 :             :         return 0;
    1659                 :             :       else
    1660                 :        1253 :         return generic_sym (s);
    1661                 :             :     }
    1662                 :             : 
    1663                 :             :   return 0;
    1664                 :             : }
    1665                 :             : 
    1666                 :             : 
    1667                 :             : /* Determine if a symbol is specific or not.  */
    1668                 :             : 
    1669                 :             : static int
    1670                 :      363835 : specific_sym (gfc_symbol *sym)
    1671                 :             : {
    1672                 :      363835 :   gfc_symbol *s;
    1673                 :             : 
    1674                 :      363835 :   if (sym->attr.if_source == IFSRC_IFBODY
    1675                 :      353000 :       || sym->attr.proc == PROC_MODULE
    1676                 :             :       || sym->attr.proc == PROC_INTERNAL
    1677                 :             :       || sym->attr.proc == PROC_ST_FUNCTION
    1678                 :      278840 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1679                 :      641944 :       || sym->attr.external)
    1680                 :       88107 :     return 1;
    1681                 :             : 
    1682                 :      275728 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1683                 :             :     return 0;
    1684                 :             : 
    1685                 :       75482 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1686                 :             : 
    1687                 :       75482 :   return (s == NULL) ? 0 : specific_sym (s);
    1688                 :             : }
    1689                 :             : 
    1690                 :             : 
    1691                 :             : /* Figure out if the procedure is specific, generic or unknown.  */
    1692                 :             : 
    1693                 :             : enum proc_type
    1694                 :             : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
    1695                 :             : 
    1696                 :             : static proc_type
    1697                 :      392520 : procedure_kind (gfc_symbol *sym)
    1698                 :             : {
    1699                 :      392520 :   if (generic_sym (sym))
    1700                 :             :     return PTYPE_GENERIC;
    1701                 :             : 
    1702                 :      363774 :   if (specific_sym (sym))
    1703                 :       88107 :     return PTYPE_SPECIFIC;
    1704                 :             : 
    1705                 :             :   return PTYPE_UNKNOWN;
    1706                 :             : }
    1707                 :             : 
    1708                 :             : /* Check references to assumed size arrays.  The flag need_full_assumed_size
    1709                 :             :    is nonzero when matching actual arguments.  */
    1710                 :             : 
    1711                 :             : static int need_full_assumed_size = 0;
    1712                 :             : 
    1713                 :             : static bool
    1714                 :     1363350 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1715                 :             : {
    1716                 :     1363350 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1717                 :             :       return false;
    1718                 :             : 
    1719                 :             :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1720                 :             :      What should it be?  */
    1721                 :        3782 :   if (e->ref
    1722                 :        3780 :       && e->ref->u.ar.as
    1723                 :        3779 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1724                 :        3284 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1725                 :        3284 :       && (e->ref->u.ar.type == AR_FULL))
    1726                 :             :     {
    1727                 :          25 :       gfc_error ("The upper bound in the last dimension must "
    1728                 :             :                  "appear in the reference to the assumed size "
    1729                 :             :                  "array %qs at %L", sym->name, &e->where);
    1730                 :          25 :       return true;
    1731                 :             :     }
    1732                 :             :   return false;
    1733                 :             : }
    1734                 :             : 
    1735                 :             : 
    1736                 :             : /* Look for bad assumed size array references in argument expressions
    1737                 :             :   of elemental and array valued intrinsic procedures.  Since this is
    1738                 :             :   called from procedure resolution functions, it only recurses at
    1739                 :             :   operators.  */
    1740                 :             : 
    1741                 :             : static bool
    1742                 :      210435 : resolve_assumed_size_actual (gfc_expr *e)
    1743                 :             : {
    1744                 :      210435 :   if (e == NULL)
    1745                 :             :    return false;
    1746                 :             : 
    1747                 :      209960 :   switch (e->expr_type)
    1748                 :             :     {
    1749                 :      102978 :     case EXPR_VARIABLE:
    1750                 :      102978 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1751                 :             :         return true;
    1752                 :             :       break;
    1753                 :             : 
    1754                 :       44288 :     case EXPR_OP:
    1755                 :       44288 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1756                 :       44288 :           || resolve_assumed_size_actual (e->value.op.op2))
    1757                 :           0 :         return true;
    1758                 :             :       break;
    1759                 :             : 
    1760                 :             :     default:
    1761                 :             :       break;
    1762                 :             :     }
    1763                 :             :   return false;
    1764                 :             : }
    1765                 :             : 
    1766                 :             : 
    1767                 :             : /* Check a generic procedure, passed as an actual argument, to see if
    1768                 :             :    there is a matching specific name.  If none, it is an error, and if
    1769                 :             :    more than one, the reference is ambiguous.  */
    1770                 :             : static int
    1771                 :           8 : count_specific_procs (gfc_expr *e)
    1772                 :             : {
    1773                 :           8 :   int n;
    1774                 :           8 :   gfc_interface *p;
    1775                 :           8 :   gfc_symbol *sym;
    1776                 :             : 
    1777                 :           8 :   n = 0;
    1778                 :           8 :   sym = e->symtree->n.sym;
    1779                 :             : 
    1780                 :          22 :   for (p = sym->generic; p; p = p->next)
    1781                 :          14 :     if (strcmp (sym->name, p->sym->name) == 0)
    1782                 :             :       {
    1783                 :           8 :         e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
    1784                 :             :                                        sym->name);
    1785                 :           8 :         n++;
    1786                 :             :       }
    1787                 :             : 
    1788                 :           8 :   if (n > 1)
    1789                 :           1 :     gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
    1790                 :             :                &e->where);
    1791                 :             : 
    1792                 :           8 :   if (n == 0)
    1793                 :           1 :     gfc_error ("GENERIC procedure %qs is not allowed as an actual "
    1794                 :             :                "argument at %L", sym->name, &e->where);
    1795                 :             : 
    1796                 :           8 :   return n;
    1797                 :             : }
    1798                 :             : 
    1799                 :             : 
    1800                 :             : /* See if a call to sym could possibly be a not allowed RECURSION because of
    1801                 :             :    a missing RECURSIVE declaration.  This means that either sym is the current
    1802                 :             :    context itself, or sym is the parent of a contained procedure calling its
    1803                 :             :    non-RECURSIVE containing procedure.
    1804                 :             :    This also works if sym is an ENTRY.  */
    1805                 :             : 
    1806                 :             : static bool
    1807                 :      144080 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1808                 :             : {
    1809                 :      144080 :   gfc_symbol* proc_sym;
    1810                 :      144080 :   gfc_symbol* context_proc;
    1811                 :      144080 :   gfc_namespace* real_context;
    1812                 :             : 
    1813                 :      144080 :   if (sym->attr.flavor == FL_PROGRAM
    1814                 :             :       || gfc_fl_struct (sym->attr.flavor))
    1815                 :             :     return false;
    1816                 :             : 
    1817                 :             :   /* If we've got an ENTRY, find real procedure.  */
    1818                 :      144079 :   if (sym->attr.entry && sym->ns->entries)
    1819                 :          45 :     proc_sym = sym->ns->entries->sym;
    1820                 :             :   else
    1821                 :             :     proc_sym = sym;
    1822                 :             : 
    1823                 :             :   /* If sym is RECURSIVE, all is well of course.  */
    1824                 :      144079 :   if (proc_sym->attr.recursive || flag_recursive)
    1825                 :             :     return false;
    1826                 :             : 
    1827                 :             :   /* Find the context procedure's "real" symbol if it has entries.
    1828                 :             :      We look for a procedure symbol, so recurse on the parents if we don't
    1829                 :             :      find one (like in case of a BLOCK construct).  */
    1830                 :        1644 :   for (real_context = context; ; real_context = real_context->parent)
    1831                 :             :     {
    1832                 :             :       /* We should find something, eventually!  */
    1833                 :      123634 :       gcc_assert (real_context);
    1834                 :             : 
    1835                 :      123634 :       context_proc = (real_context->entries ? real_context->entries->sym
    1836                 :             :                                             : real_context->proc_name);
    1837                 :             : 
    1838                 :             :       /* In some special cases, there may not be a proc_name, like for this
    1839                 :             :          invalid code:
    1840                 :             :          real(bad_kind()) function foo () ...
    1841                 :             :          when checking the call to bad_kind ().
    1842                 :             :          In these cases, we simply return here and assume that the
    1843                 :             :          call is ok.  */
    1844                 :      123634 :       if (!context_proc)
    1845                 :             :         return false;
    1846                 :             : 
    1847                 :      123370 :       if (context_proc->attr.flavor != FL_LABEL)
    1848                 :             :         break;
    1849                 :             :     }
    1850                 :             : 
    1851                 :             :   /* A call from sym's body to itself is recursion, of course.  */
    1852                 :      121726 :   if (context_proc == proc_sym)
    1853                 :             :     return true;
    1854                 :             : 
    1855                 :             :   /* The same is true if context is a contained procedure and sym the
    1856                 :             :      containing one.  */
    1857                 :      121712 :   if (context_proc->attr.contained)
    1858                 :             :     {
    1859                 :       20300 :       gfc_symbol* parent_proc;
    1860                 :             : 
    1861                 :       20300 :       gcc_assert (context->parent);
    1862                 :       20300 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1863                 :             :                                               : context->parent->proc_name);
    1864                 :             : 
    1865                 :       20300 :       if (parent_proc == proc_sym)
    1866                 :           9 :         return true;
    1867                 :             :     }
    1868                 :             : 
    1869                 :             :   return false;
    1870                 :             : }
    1871                 :             : 
    1872                 :             : 
    1873                 :             : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
    1874                 :             :    its typespec and formal argument list.  */
    1875                 :             : 
    1876                 :             : bool
    1877                 :       40699 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1878                 :             : {
    1879                 :       40699 :   gfc_intrinsic_sym* isym = NULL;
    1880                 :       40699 :   const char* symstd;
    1881                 :             : 
    1882                 :       40699 :   if (sym->resolve_symbol_called >= 2)
    1883                 :             :     return true;
    1884                 :             : 
    1885                 :       31171 :   sym->resolve_symbol_called = 2;
    1886                 :             : 
    1887                 :             :   /* Already resolved.  */
    1888                 :       31171 :   if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
    1889                 :             :     return true;
    1890                 :             : 
    1891                 :             :   /* We already know this one is an intrinsic, so we don't call
    1892                 :             :      gfc_is_intrinsic for full checking but rather use gfc_find_function and
    1893                 :             :      gfc_find_subroutine directly to check whether it is a function or
    1894                 :             :      subroutine.  */
    1895                 :             : 
    1896                 :       23466 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1897                 :             :     {
    1898                 :        8458 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1899                 :        8458 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1900                 :        8458 :     }
    1901                 :       15008 :   else if (sym->intmod_sym_id)
    1902                 :             :     {
    1903                 :       11682 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1904                 :       11682 :       isym = gfc_intrinsic_function_by_id (id);
    1905                 :             :     }
    1906                 :        3326 :   else if (!sym->attr.subroutine)
    1907                 :        3254 :     isym = gfc_find_function (sym->name);
    1908                 :             : 
    1909                 :       23394 :   if (isym && !sym->attr.subroutine)
    1910                 :             :     {
    1911                 :       14897 :       if (sym->ts.type != BT_UNKNOWN && warn_surprising
    1912                 :          24 :           && !sym->attr.implicit_type)
    1913                 :          10 :         gfc_warning (OPT_Wsurprising,
    1914                 :             :                      "Type specified for intrinsic function %qs at %L is"
    1915                 :             :                       " ignored", sym->name, &sym->declared_at);
    1916                 :             : 
    1917                 :       18385 :       if (!sym->attr.function &&
    1918                 :        3488 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1919                 :             :         return false;
    1920                 :             : 
    1921                 :       14897 :       sym->ts = isym->ts;
    1922                 :             :     }
    1923                 :        8569 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1924                 :             :     {
    1925                 :        8566 :       if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
    1926                 :             :         {
    1927                 :           1 :           gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
    1928                 :             :                       " specifier", sym->name, &sym->declared_at);
    1929                 :           1 :           return false;
    1930                 :             :         }
    1931                 :             : 
    1932                 :        8600 :       if (!sym->attr.subroutine &&
    1933                 :          35 :           !gfc_add_subroutine(&sym->attr, sym->name, loc))
    1934                 :             :         return false;
    1935                 :             :     }
    1936                 :             :   else
    1937                 :             :     {
    1938                 :           3 :       gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
    1939                 :             :                  &sym->declared_at);
    1940                 :           3 :       return false;
    1941                 :             :     }
    1942                 :             : 
    1943                 :       23461 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1944                 :             : 
    1945                 :       23461 :   sym->attr.pure = isym->pure;
    1946                 :       23461 :   sym->attr.elemental = isym->elemental;
    1947                 :             : 
    1948                 :             :   /* Check it is actually available in the standard settings.  */
    1949                 :       23461 :   if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
    1950                 :             :     {
    1951                 :          24 :       gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
    1952                 :             :                  "available in the current standard settings but %s. Use "
    1953                 :             :                  "an appropriate %<-std=*%> option or enable "
    1954                 :             :                  "%<-fall-intrinsics%> in order to use it.",
    1955                 :             :                  sym->name, &sym->declared_at, symstd);
    1956                 :          24 :       return false;
    1957                 :             :     }
    1958                 :             : 
    1959                 :             :   return true;
    1960                 :             : }
    1961                 :             : 
    1962                 :             : 
    1963                 :             : /* Resolve a procedure expression, like passing it to a called procedure or as
    1964                 :             :    RHS for a procedure pointer assignment.  */
    1965                 :             : 
    1966                 :             : static bool
    1967                 :     1272586 : resolve_procedure_expression (gfc_expr* expr)
    1968                 :             : {
    1969                 :     1272586 :   gfc_symbol* sym;
    1970                 :             : 
    1971                 :     1272586 :   if (expr->expr_type != EXPR_VARIABLE)
    1972                 :             :     return true;
    1973                 :     1272581 :   gcc_assert (expr->symtree);
    1974                 :             : 
    1975                 :     1272581 :   sym = expr->symtree->n.sym;
    1976                 :             : 
    1977                 :     1272581 :   if (sym->attr.intrinsic)
    1978                 :        1346 :     gfc_resolve_intrinsic (sym, &expr->where);
    1979                 :             : 
    1980                 :     1272581 :   if (sym->attr.flavor != FL_PROCEDURE
    1981                 :       29134 :       || (sym->attr.function && sym->result == sym))
    1982                 :             :     return true;
    1983                 :             : 
    1984                 :             :    /* A non-RECURSIVE procedure that is used as procedure expression within its
    1985                 :             :      own body is in danger of being called recursively.  */
    1986                 :       13807 :   if (is_illegal_recursion (sym, gfc_current_ns))
    1987                 :             :     {
    1988                 :          10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    1989                 :           0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    1990                 :             :                      " possibly calling itself recursively in procedure %qs. "
    1991                 :             :                      " Declare it RECURSIVE or use %<-frecursive%>",
    1992                 :           0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    1993                 :             :       else
    1994                 :          10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    1995                 :             :                      " itself recursively.  Declare it RECURSIVE or use"
    1996                 :             :                      " %<-frecursive%>", sym->name, &expr->where);
    1997                 :             :     }
    1998                 :             : 
    1999                 :             :   return true;
    2000                 :             : }
    2001                 :             : 
    2002                 :             : 
    2003                 :             : /* Check that name is not a derived type.  */
    2004                 :             : 
    2005                 :             : static bool
    2006                 :        3087 : is_dt_name (const char *name)
    2007                 :             : {
    2008                 :        3087 :   gfc_symbol *dt_list, *dt_first;
    2009                 :             : 
    2010                 :        3087 :   dt_list = dt_first = gfc_derived_types;
    2011                 :        5463 :   for (; dt_list; dt_list = dt_list->dt_next)
    2012                 :             :     {
    2013                 :        3379 :       if (strcmp(dt_list->name, name) == 0)
    2014                 :             :         return true;
    2015                 :        3376 :       if (dt_first == dt_list->dt_next)
    2016                 :             :         break;
    2017                 :             :     }
    2018                 :             :   return false;
    2019                 :             : }
    2020                 :             : 
    2021                 :             : 
    2022                 :             : /* Resolve an actual argument list.  Most of the time, this is just
    2023                 :             :    resolving the expressions in the list.
    2024                 :             :    The exception is that we sometimes have to decide whether arguments
    2025                 :             :    that look like procedure arguments are really simple variable
    2026                 :             :    references.  */
    2027                 :             : 
    2028                 :             : static bool
    2029                 :      405922 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2030                 :             :                         bool no_formal_args)
    2031                 :             : {
    2032                 :      405922 :   gfc_symbol *sym;
    2033                 :      405922 :   gfc_symtree *parent_st;
    2034                 :      405922 :   gfc_expr *e;
    2035                 :      405922 :   gfc_component *comp;
    2036                 :      405922 :   int save_need_full_assumed_size;
    2037                 :      405922 :   bool return_value = false;
    2038                 :      405922 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2039                 :             : 
    2040                 :      405922 :   actual_arg = true;
    2041                 :      405922 :   first_actual_arg = true;
    2042                 :             : 
    2043                 :     1039420 :   for (; arg; arg = arg->next)
    2044                 :             :     {
    2045                 :      633597 :       e = arg->expr;
    2046                 :      633597 :       if (e == NULL)
    2047                 :             :         {
    2048                 :             :           /* Check the label is a valid branching target.  */
    2049                 :        2313 :           if (arg->label)
    2050                 :             :             {
    2051                 :         236 :               if (arg->label->defined == ST_LABEL_UNKNOWN)
    2052                 :             :                 {
    2053                 :           0 :                   gfc_error ("Label %d referenced at %L is never defined",
    2054                 :             :                              arg->label->value, &arg->label->where);
    2055                 :           0 :                   goto cleanup;
    2056                 :             :                 }
    2057                 :             :             }
    2058                 :        2313 :           first_actual_arg = false;
    2059                 :        2313 :           continue;
    2060                 :             :         }
    2061                 :             : 
    2062                 :      631284 :       if (e->expr_type == EXPR_VARIABLE
    2063                 :      279662 :             && e->symtree->n.sym->attr.generic
    2064                 :           8 :             && no_formal_args
    2065                 :      631289 :             && count_specific_procs (e) != 1)
    2066                 :           2 :         goto cleanup;
    2067                 :             : 
    2068                 :      631282 :       if (e->ts.type != BT_PROCEDURE)
    2069                 :             :         {
    2070                 :      560979 :           save_need_full_assumed_size = need_full_assumed_size;
    2071                 :      560979 :           if (e->expr_type != EXPR_VARIABLE)
    2072                 :      351622 :             need_full_assumed_size = 0;
    2073                 :      560979 :           if (!gfc_resolve_expr (e))
    2074                 :          60 :             goto cleanup;
    2075                 :      560919 :           need_full_assumed_size = save_need_full_assumed_size;
    2076                 :      560919 :           goto argument_list;
    2077                 :             :         }
    2078                 :             : 
    2079                 :             :       /* See if the expression node should really be a variable reference.  */
    2080                 :             : 
    2081                 :       70303 :       sym = e->symtree->n.sym;
    2082                 :             : 
    2083                 :       70303 :       if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
    2084                 :             :         {
    2085                 :           3 :           gfc_error ("Derived type %qs is used as an actual "
    2086                 :             :                      "argument at %L", sym->name, &e->where);
    2087                 :           3 :           goto cleanup;
    2088                 :             :         }
    2089                 :             : 
    2090                 :       70300 :       if (sym->attr.flavor == FL_PROCEDURE
    2091                 :             :           || sym->attr.intrinsic
    2092                 :       67216 :           || sym->attr.external)
    2093                 :             :         {
    2094                 :        3084 :           int actual_ok;
    2095                 :             : 
    2096                 :             :           /* If a procedure is not already determined to be something else
    2097                 :             :              check if it is intrinsic.  */
    2098                 :        3084 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2099                 :        1254 :             sym->attr.intrinsic = 1;
    2100                 :             : 
    2101                 :        3084 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2102                 :             :             {
    2103                 :           2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2104                 :             :                          "actual argument", sym->name, &e->where);
    2105                 :             :             }
    2106                 :             : 
    2107                 :        6168 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2108                 :        3084 :                                                sym->attr.subroutine);
    2109                 :        3084 :           if (sym->attr.intrinsic && actual_ok == 0)
    2110                 :             :             {
    2111                 :           0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2112                 :             :                          "actual argument", sym->name, &e->where);
    2113                 :             :             }
    2114                 :             : 
    2115                 :        3084 :           if (sym->attr.contained && !sym->attr.use_assoc
    2116                 :         404 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2117                 :             :             {
    2118                 :         216 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2119                 :             :                                    " used as actual argument at %L",
    2120                 :             :                                    sym->name, &e->where))
    2121                 :           3 :                 goto cleanup;
    2122                 :             :             }
    2123                 :             : 
    2124                 :        3081 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2125                 :             :             {
    2126                 :           2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2127                 :             :                          "allowed as an actual argument at %L", sym->name,
    2128                 :             :                          &e->where);
    2129                 :             :             }
    2130                 :             : 
    2131                 :             :           /* Check if a generic interface has a specific procedure
    2132                 :             :             with the same name before emitting an error.  */
    2133                 :        3081 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2134                 :           0 :             goto cleanup;
    2135                 :             : 
    2136                 :             :           /* Just in case a specific was found for the expression.  */
    2137                 :        3081 :           sym = e->symtree->n.sym;
    2138                 :             : 
    2139                 :             :           /* If the symbol is the function that names the current (or
    2140                 :             :              parent) scope, then we really have a variable reference.  */
    2141                 :             : 
    2142                 :        3081 :           if (gfc_is_function_return_value (sym, sym->ns))
    2143                 :           0 :             goto got_variable;
    2144                 :             : 
    2145                 :             :           /* If all else fails, see if we have a specific intrinsic.  */
    2146                 :        3081 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2147                 :             :             {
    2148                 :           0 :               gfc_intrinsic_sym *isym;
    2149                 :             : 
    2150                 :           0 :               isym = gfc_find_function (sym->name);
    2151                 :           0 :               if (isym == NULL || !isym->specific)
    2152                 :             :                 {
    2153                 :           0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2154                 :             :                              "for the reference %qs at %L", sym->name,
    2155                 :             :                              &e->where);
    2156                 :           0 :                   goto cleanup;
    2157                 :             :                 }
    2158                 :           0 :               sym->ts = isym->ts;
    2159                 :           0 :               sym->attr.intrinsic = 1;
    2160                 :           0 :               sym->attr.function = 1;
    2161                 :             :             }
    2162                 :             : 
    2163                 :        3081 :           if (!gfc_resolve_expr (e))
    2164                 :           0 :             goto cleanup;
    2165                 :        3081 :           goto argument_list;
    2166                 :             :         }
    2167                 :             : 
    2168                 :             :       /* See if the name is a module procedure in a parent unit.  */
    2169                 :             : 
    2170                 :       67216 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2171                 :       67123 :         goto got_variable;
    2172                 :             : 
    2173                 :          93 :       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
    2174                 :             :         {
    2175                 :           0 :           gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
    2176                 :           0 :           goto cleanup;
    2177                 :             :         }
    2178                 :             : 
    2179                 :          93 :       if (parent_st == NULL)
    2180                 :          93 :         goto got_variable;
    2181                 :             : 
    2182                 :           0 :       sym = parent_st->n.sym;
    2183                 :           0 :       e->symtree = parent_st;                /* Point to the right thing.  */
    2184                 :             : 
    2185                 :           0 :       if (sym->attr.flavor == FL_PROCEDURE
    2186                 :             :           || sym->attr.intrinsic
    2187                 :           0 :           || sym->attr.external)
    2188                 :             :         {
    2189                 :           0 :           if (!gfc_resolve_expr (e))
    2190                 :           0 :             goto cleanup;
    2191                 :           0 :           goto argument_list;
    2192                 :             :         }
    2193                 :             : 
    2194                 :           0 :     got_variable:
    2195                 :       67216 :       e->expr_type = EXPR_VARIABLE;
    2196                 :       67216 :       e->ts = sym->ts;
    2197                 :       67216 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2198                 :       34843 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2199                 :        3756 :               && CLASS_DATA (sym)->as))
    2200                 :             :         {
    2201                 :       70156 :           gfc_array_spec *as
    2202                 :       35078 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2203                 :       35078 :           e->rank = as->rank;
    2204                 :       35078 :           e->corank = as->corank;
    2205                 :       35078 :           e->ref = gfc_get_ref ();
    2206                 :       35078 :           e->ref->type = REF_ARRAY;
    2207                 :       35078 :           e->ref->u.ar.type = AR_FULL;
    2208                 :       35078 :           e->ref->u.ar.as = as;
    2209                 :             :         }
    2210                 :             : 
    2211                 :             :       /* These symbols are set untyped by calls to gfc_set_default_type
    2212                 :             :          with 'error_flag' = false.  Reset the untyped attribute so that
    2213                 :             :          the error will be generated in gfc_resolve_expr.  */
    2214                 :       67216 :       if (e->expr_type == EXPR_VARIABLE
    2215                 :       67216 :           && sym->ts.type == BT_UNKNOWN
    2216                 :          36 :           && sym->attr.untyped)
    2217                 :           5 :         sym->attr.untyped = 0;
    2218                 :             : 
    2219                 :             :       /* Expressions are assigned a default ts.type of BT_PROCEDURE in
    2220                 :             :          primary.cc (match_actual_arg). If above code determines that it
    2221                 :             :          is a  variable instead, it needs to be resolved as it was not
    2222                 :             :          done at the beginning of this function.  */
    2223                 :       67216 :       save_need_full_assumed_size = need_full_assumed_size;
    2224                 :       67216 :       if (e->expr_type != EXPR_VARIABLE)
    2225                 :           0 :         need_full_assumed_size = 0;
    2226                 :       67216 :       if (!gfc_resolve_expr (e))
    2227                 :          22 :         goto cleanup;
    2228                 :       67194 :       need_full_assumed_size = save_need_full_assumed_size;
    2229                 :             : 
    2230                 :      631194 :     argument_list:
    2231                 :             :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2232                 :             :          nothing to do for %REF.  */
    2233                 :      631194 :       if (arg->name && arg->name[0] == '%')
    2234                 :             :         {
    2235                 :          42 :           if (strcmp ("%VAL", arg->name) == 0)
    2236                 :             :             {
    2237                 :          28 :               if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
    2238                 :             :                 {
    2239                 :           2 :                   gfc_error ("By-value argument at %L is not of numeric "
    2240                 :             :                              "type", &e->where);
    2241                 :           2 :                   goto cleanup;
    2242                 :             :                 }
    2243                 :             : 
    2244                 :          26 :               if (e->rank)
    2245                 :             :                 {
    2246                 :           1 :                   gfc_error ("By-value argument at %L cannot be an array or "
    2247                 :             :                              "an array section", &e->where);
    2248                 :           1 :                   goto cleanup;
    2249                 :             :                 }
    2250                 :             : 
    2251                 :             :               /* Intrinsics are still PROC_UNKNOWN here.  However,
    2252                 :             :                  since same file external procedures are not resolvable
    2253                 :             :                  in gfortran, it is a good deal easier to leave them to
    2254                 :             :                  intrinsic.cc.  */
    2255                 :          25 :               if (ptype != PROC_UNKNOWN
    2256                 :          25 :                   && ptype != PROC_DUMMY
    2257                 :           9 :                   && ptype != PROC_EXTERNAL
    2258                 :           9 :                   && ptype != PROC_MODULE)
    2259                 :             :                 {
    2260                 :           3 :                   gfc_error ("By-value argument at %L is not allowed "
    2261                 :             :                              "in this context", &e->where);
    2262                 :           3 :                   goto cleanup;
    2263                 :             :                 }
    2264                 :             :             }
    2265                 :             : 
    2266                 :             :           /* Statement functions have already been excluded above.  */
    2267                 :          14 :           else if (strcmp ("%LOC", arg->name) == 0
    2268                 :           8 :                    && e->ts.type == BT_PROCEDURE)
    2269                 :             :             {
    2270                 :           0 :               if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
    2271                 :             :                 {
    2272                 :           0 :                   gfc_error ("Passing internal procedure at %L by location "
    2273                 :             :                              "not allowed", &e->where);
    2274                 :           0 :                   goto cleanup;
    2275                 :             :                 }
    2276                 :             :             }
    2277                 :             :         }
    2278                 :             : 
    2279                 :      631188 :       comp = gfc_get_proc_ptr_comp(e);
    2280                 :      631188 :       if (e->expr_type == EXPR_VARIABLE
    2281                 :      278284 :           && comp && comp->attr.elemental)
    2282                 :             :         {
    2283                 :           1 :             gfc_error ("ELEMENTAL procedure pointer component %qs is not "
    2284                 :             :                        "allowed as an actual argument at %L", comp->name,
    2285                 :             :                        &e->where);
    2286                 :             :         }
    2287                 :             : 
    2288                 :             :       /* Fortran 2008, C1237.  */
    2289                 :      278284 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2290                 :      631494 :           && gfc_has_ultimate_pointer (e))
    2291                 :             :         {
    2292                 :           3 :           gfc_error ("Coindexed actual argument at %L with ultimate pointer "
    2293                 :             :                      "component", &e->where);
    2294                 :           3 :           goto cleanup;
    2295                 :             :         }
    2296                 :             : 
    2297                 :      631185 :       first_actual_arg = false;
    2298                 :             :     }
    2299                 :             : 
    2300                 :             :   return_value = true;
    2301                 :             : 
    2302                 :      405922 : cleanup:
    2303                 :      405922 :   actual_arg = actual_arg_sav;
    2304                 :      405922 :   first_actual_arg = first_actual_arg_sav;
    2305                 :             : 
    2306                 :      405922 :   return return_value;
    2307                 :             : }
    2308                 :             : 
    2309                 :             : 
    2310                 :             : /* Do the checks of the actual argument list that are specific to elemental
    2311                 :             :    procedures.  If called with c == NULL, we have a function, otherwise if
    2312                 :             :    expr == NULL, we have a subroutine.  */
    2313                 :             : 
    2314                 :             : static bool
    2315                 :      308067 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2316                 :             : {
    2317                 :      308067 :   gfc_actual_arglist *arg0;
    2318                 :      308067 :   gfc_actual_arglist *arg;
    2319                 :      308067 :   gfc_symbol *esym = NULL;
    2320                 :      308067 :   gfc_intrinsic_sym *isym = NULL;
    2321                 :      308067 :   gfc_expr *e = NULL;
    2322                 :      308067 :   gfc_intrinsic_arg *iformal = NULL;
    2323                 :      308067 :   gfc_formal_arglist *eformal = NULL;
    2324                 :      308067 :   bool formal_optional = false;
    2325                 :      308067 :   bool set_by_optional = false;
    2326                 :      308067 :   int i;
    2327                 :      308067 :   int rank = 0;
    2328                 :             : 
    2329                 :             :   /* Is this an elemental procedure?  */
    2330                 :      308067 :   if (expr && expr->value.function.actual != NULL)
    2331                 :             :     {
    2332                 :      221787 :       if (expr->value.function.esym != NULL
    2333                 :       42840 :           && expr->value.function.esym->attr.elemental)
    2334                 :             :         {
    2335                 :             :           arg0 = expr->value.function.actual;
    2336                 :             :           esym = expr->value.function.esym;
    2337                 :             :         }
    2338                 :      205515 :       else if (expr->value.function.isym != NULL
    2339                 :      177974 :                && expr->value.function.isym->elemental)
    2340                 :             :         {
    2341                 :             :           arg0 = expr->value.function.actual;
    2342                 :             :           isym = expr->value.function.isym;
    2343                 :             :         }
    2344                 :             :       else
    2345                 :             :         return true;
    2346                 :             :     }
    2347                 :       86280 :   else if (c && c->ext.actual != NULL)
    2348                 :             :     {
    2349                 :       68453 :       arg0 = c->ext.actual;
    2350                 :             : 
    2351                 :       68453 :       if (c->resolved_sym)
    2352                 :             :         esym = c->resolved_sym;
    2353                 :             :       else
    2354                 :         298 :         esym = c->symtree->n.sym;
    2355                 :       68453 :       gcc_assert (esym);
    2356                 :             : 
    2357                 :       68453 :       if (!esym->attr.elemental)
    2358                 :             :         return true;
    2359                 :             :     }
    2360                 :             :   else
    2361                 :             :     return true;
    2362                 :             : 
    2363                 :             :   /* The rank of an elemental is the rank of its array argument(s).  */
    2364                 :      169053 :   for (arg = arg0; arg; arg = arg->next)
    2365                 :             :     {
    2366                 :      109562 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2367                 :             :         {
    2368                 :        9883 :           rank = arg->expr->rank;
    2369                 :        9883 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2370                 :        5204 :               && arg->expr->symtree->n.sym->attr.optional)
    2371                 :        9883 :             set_by_optional = true;
    2372                 :             : 
    2373                 :             :           /* Function specific; set the result rank and shape.  */
    2374                 :        9883 :           if (expr)
    2375                 :             :             {
    2376                 :        7721 :               expr->rank = rank;
    2377                 :        7721 :               expr->corank = arg->expr->corank;
    2378                 :        7721 :               if (!expr->shape && arg->expr->shape)
    2379                 :             :                 {
    2380                 :        3899 :                   expr->shape = gfc_get_shape (rank);
    2381                 :        8590 :                   for (i = 0; i < rank; i++)
    2382                 :        4691 :                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
    2383                 :             :                 }
    2384                 :             :             }
    2385                 :             :           break;
    2386                 :             :         }
    2387                 :             :     }
    2388                 :             : 
    2389                 :             :   /* If it is an array, it shall not be supplied as an actual argument
    2390                 :             :      to an elemental procedure unless an array of the same rank is supplied
    2391                 :             :      as an actual argument corresponding to a nonoptional dummy argument of
    2392                 :             :      that elemental procedure(12.4.1.5).  */
    2393                 :       69374 :   formal_optional = false;
    2394                 :       69374 :   if (isym)
    2395                 :       47141 :     iformal = isym->formal;
    2396                 :             :   else
    2397                 :       22233 :     eformal = esym->formal;
    2398                 :             : 
    2399                 :      184197 :   for (arg = arg0; arg; arg = arg->next)
    2400                 :             :     {
    2401                 :      114823 :       if (eformal)
    2402                 :             :         {
    2403                 :       39879 :           if (eformal->sym && eformal->sym->attr.optional)
    2404                 :       39879 :             formal_optional = true;
    2405                 :       39879 :           eformal = eformal->next;
    2406                 :             :         }
    2407                 :       74944 :       else if (isym && iformal)
    2408                 :             :         {
    2409                 :       65787 :           if (iformal->optional)
    2410                 :       13169 :             formal_optional = true;
    2411                 :       65787 :           iformal = iformal->next;
    2412                 :             :         }
    2413                 :        9157 :       else if (isym)
    2414                 :        9149 :         formal_optional = true;
    2415                 :             : 
    2416                 :      114823 :       if (pedantic && arg->expr != NULL
    2417                 :       69857 :           && arg->expr->expr_type == EXPR_VARIABLE
    2418                 :       34348 :           && arg->expr->symtree->n.sym->attr.optional
    2419                 :         572 :           && formal_optional
    2420                 :         479 :           && arg->expr->rank
    2421                 :         153 :           && (set_by_optional || arg->expr->rank != rank)
    2422                 :          42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2423                 :             :         {
    2424                 :         114 :           bool t = false;
    2425                 :             :           gfc_actual_arglist *a;
    2426                 :             : 
    2427                 :             :           /* Scan the argument list for a non-optional argument with the
    2428                 :             :              same rank as arg.  */
    2429                 :         114 :           for (a = arg0; a; a = a->next)
    2430                 :          87 :             if (a != arg
    2431                 :          45 :                 && a->expr->rank == arg->expr->rank
    2432                 :          39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2433                 :          37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2434                 :          37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2435                 :             :               {
    2436                 :             :                 t = true;
    2437                 :             :                 break;
    2438                 :             :               }
    2439                 :             : 
    2440                 :          42 :           if (!t)
    2441                 :          27 :             gfc_warning (OPT_Wpedantic,
    2442                 :             :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2443                 :             :                          "present, then it cannot be the actual argument of "
    2444                 :             :                          "an ELEMENTAL procedure unless there is a non-optional"
    2445                 :             :                          " argument with the same rank "
    2446                 :             :                          "(Fortran 2018, 15.5.2.12)",
    2447                 :             :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2448                 :             :         }
    2449                 :             :     }
    2450                 :             : 
    2451                 :      184186 :   for (arg = arg0; arg; arg = arg->next)
    2452                 :             :     {
    2453                 :      114821 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2454                 :      102296 :         continue;
    2455                 :             : 
    2456                 :             :       /* Being elemental, the last upper bound of an assumed size array
    2457                 :             :          argument must be present.  */
    2458                 :       12525 :       if (resolve_assumed_size_actual (arg->expr))
    2459                 :             :         return false;
    2460                 :             : 
    2461                 :             :       /* Elemental procedure's array actual arguments must conform.  */
    2462                 :       12522 :       if (e != NULL)
    2463                 :             :         {
    2464                 :        2642 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2465                 :             :             return false;
    2466                 :             :         }
    2467                 :             :       else
    2468                 :        9880 :         e = arg->expr;
    2469                 :             :     }
    2470                 :             : 
    2471                 :             :   /* INTENT(OUT) is only allowed for subroutines; if any actual argument
    2472                 :             :      is an array, the intent inout/out variable needs to be also an array.  */
    2473                 :       69365 :   if (rank > 0 && esym && expr == NULL)
    2474                 :        6613 :     for (eformal = esym->formal, arg = arg0; arg && eformal;
    2475                 :        4457 :          arg = arg->next, eformal = eformal->next)
    2476                 :        4459 :       if (eformal->sym
    2477                 :        4458 :           && (eformal->sym->attr.intent == INTENT_OUT
    2478                 :        3382 :               || eformal->sym->attr.intent == INTENT_INOUT)
    2479                 :        1470 :           && arg->expr && arg->expr->rank == 0)
    2480                 :             :         {
    2481                 :           2 :           gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
    2482                 :             :                      "ELEMENTAL subroutine %qs is a scalar, but another "
    2483                 :             :                      "actual argument is an array", &arg->expr->where,
    2484                 :             :                      (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
    2485                 :             :                      : "INOUT", eformal->sym->name, esym->name);
    2486                 :           2 :           return false;
    2487                 :             :         }
    2488                 :             :   return true;
    2489                 :             : }
    2490                 :             : 
    2491                 :             : 
    2492                 :             : /* This function does the checking of references to global procedures
    2493                 :             :    as defined in sections 18.1 and 14.1, respectively, of the Fortran
    2494                 :             :    77 and 95 standards.  It checks for a gsymbol for the name, making
    2495                 :             :    one if it does not already exist.  If it already exists, then the
    2496                 :             :    reference being resolved must correspond to the type of gsymbol.
    2497                 :             :    Otherwise, the new symbol is equipped with the attributes of the
    2498                 :             :    reference.  The corresponding code that is called in creating
    2499                 :             :    global entities is parse.cc.
    2500                 :             : 
    2501                 :             :    In addition, for all but -std=legacy, the gsymbols are used to
    2502                 :             :    check the interfaces of external procedures from the same file.
    2503                 :             :    The namespace of the gsymbol is resolved and then, once this is
    2504                 :             :    done the interface is checked.  */
    2505                 :             : 
    2506                 :             : 
    2507                 :             : static bool
    2508                 :       14684 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2509                 :             : {
    2510                 :       14684 :   if (!gsym_ns->proc_name->attr.recursive)
    2511                 :             :     return true;
    2512                 :             : 
    2513                 :         151 :   if (sym->ns == gsym_ns)
    2514                 :             :     return false;
    2515                 :             : 
    2516                 :         151 :   if (sym->ns->parent && sym->ns->parent == gsym_ns)
    2517                 :           0 :     return false;
    2518                 :             : 
    2519                 :             :   return true;
    2520                 :             : }
    2521                 :             : 
    2522                 :             : static bool
    2523                 :       14684 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2524                 :             : {
    2525                 :       14684 :   if (gsym_ns->entries)
    2526                 :             :     {
    2527                 :             :       gfc_entry_list *entry = gsym_ns->entries;
    2528                 :             : 
    2529                 :        3234 :       for (; entry; entry = entry->next)
    2530                 :             :         {
    2531                 :        2281 :           if (strcmp (sym->name, entry->sym->name) == 0)
    2532                 :             :             {
    2533                 :         946 :               if (strcmp (gsym_ns->proc_name->name,
    2534                 :         946 :                           sym->ns->proc_name->name) == 0)
    2535                 :             :                 return false;
    2536                 :             : 
    2537                 :         946 :               if (sym->ns->parent
    2538                 :           0 :                   && strcmp (gsym_ns->proc_name->name,
    2539                 :           0 :                              sym->ns->parent->proc_name->name) == 0)
    2540                 :             :                 return false;
    2541                 :             :             }
    2542                 :             :         }
    2543                 :             :     }
    2544                 :             :   return true;
    2545                 :             : }
    2546                 :             : 
    2547                 :             : 
    2548                 :             : /* Check for the requirement of an explicit interface. F08:12.4.2.2.  */
    2549                 :             : 
    2550                 :             : bool
    2551                 :       15461 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2552                 :             : {
    2553                 :       15461 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2554                 :             : 
    2555                 :       58276 :   for ( ; arg; arg = arg->next)
    2556                 :             :     {
    2557                 :       27700 :       if (!arg->sym)
    2558                 :         157 :         continue;
    2559                 :             : 
    2560                 :       27543 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2561                 :             :         {
    2562                 :           0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2563                 :           0 :           return true;
    2564                 :             :         }
    2565                 :       27543 :       else if (arg->sym->attr.asynchronous)
    2566                 :             :         {
    2567                 :           0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2568                 :           0 :           return true;
    2569                 :             :         }
    2570                 :       27543 :       else if (arg->sym->attr.optional)
    2571                 :             :         {
    2572                 :          75 :           strncpy (errmsg, _("optional argument"), err_len);
    2573                 :          75 :           return true;
    2574                 :             :         }
    2575                 :       27468 :       else if (arg->sym->attr.pointer)
    2576                 :             :         {
    2577                 :          12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2578                 :          12 :           return true;
    2579                 :             :         }
    2580                 :       27456 :       else if (arg->sym->attr.target)
    2581                 :             :         {
    2582                 :          48 :           strncpy (errmsg, _("target argument"), err_len);
    2583                 :          48 :           return true;
    2584                 :             :         }
    2585                 :       27408 :       else if (arg->sym->attr.value)
    2586                 :             :         {
    2587                 :           0 :           strncpy (errmsg, _("value argument"), err_len);
    2588                 :           0 :           return true;
    2589                 :             :         }
    2590                 :       27408 :       else if (arg->sym->attr.volatile_)
    2591                 :             :         {
    2592                 :           1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2593                 :           1 :           return true;
    2594                 :             :         }
    2595                 :       27407 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2596                 :             :         {
    2597                 :          45 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2598                 :          45 :           return true;
    2599                 :             :         }
    2600                 :       27362 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK)  /* TS 29113, 6.2.  */
    2601                 :             :         {
    2602                 :           1 :           strncpy (errmsg, _("assumed-rank argument"), err_len);
    2603                 :           1 :           return true;
    2604                 :             :         }
    2605                 :       27361 :       else if (arg->sym->attr.codimension)  /* (2c)  */
    2606                 :             :         {
    2607                 :           1 :           strncpy (errmsg, _("coarray argument"), err_len);
    2608                 :           1 :           return true;
    2609                 :             :         }
    2610                 :       27360 :       else if (false)  /* (2d) TODO: parametrized derived type  */
    2611                 :             :         {
    2612                 :             :           strncpy (errmsg, _("parametrized derived type argument"), err_len);
    2613                 :             :           return true;
    2614                 :             :         }
    2615                 :       27360 :       else if (arg->sym->ts.type == BT_CLASS)  /* (2e)  */
    2616                 :             :         {
    2617                 :         162 :           strncpy (errmsg, _("polymorphic argument"), err_len);
    2618                 :         162 :           return true;
    2619                 :             :         }
    2620                 :       27198 :       else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2621                 :             :         {
    2622                 :           0 :           strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
    2623                 :           0 :           return true;
    2624                 :             :         }
    2625                 :       27198 :       else if (arg->sym->ts.type == BT_ASSUMED)
    2626                 :             :         {
    2627                 :             :           /* As assumed-type is unlimited polymorphic (cf. above).
    2628                 :             :              See also TS 29113, Note 6.1.  */
    2629                 :           1 :           strncpy (errmsg, _("assumed-type argument"), err_len);
    2630                 :           1 :           return true;
    2631                 :             :         }
    2632                 :             :     }
    2633                 :             : 
    2634                 :       15115 :   if (sym->attr.function)
    2635                 :             :     {
    2636                 :        3431 :       gfc_symbol *res = sym->result ? sym->result : sym;
    2637                 :             : 
    2638                 :        3431 :       if (res->attr.dimension)  /* (3a)  */
    2639                 :             :         {
    2640                 :          93 :           strncpy (errmsg, _("array result"), err_len);
    2641                 :          93 :           return true;
    2642                 :             :         }
    2643                 :        3338 :       else if (res->attr.pointer || res->attr.allocatable)  /* (3b)  */
    2644                 :             :         {
    2645                 :          38 :           strncpy (errmsg, _("pointer or allocatable result"), err_len);
    2646                 :          38 :           return true;
    2647                 :             :         }
    2648                 :        3300 :       else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
    2649                 :         347 :                && res->ts.u.cl->length
    2650                 :         166 :                && res->ts.u.cl->length->expr_type != EXPR_CONSTANT)  /* (3c)  */
    2651                 :             :         {
    2652                 :          12 :           strncpy (errmsg, _("result with non-constant character length"), err_len);
    2653                 :          12 :           return true;
    2654                 :             :         }
    2655                 :             :     }
    2656                 :             : 
    2657                 :       14972 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2658                 :             :     {
    2659                 :           7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2660                 :           7 :       return true;
    2661                 :             :     }
    2662                 :       14965 :   else if (sym->attr.is_bind_c)  /* (5)  */
    2663                 :             :     {
    2664                 :           0 :       strncpy (errmsg, _("bind(c) procedure"), err_len);
    2665                 :           0 :       return true;
    2666                 :             :     }
    2667                 :             : 
    2668                 :             :   return false;
    2669                 :             : }
    2670                 :             : 
    2671                 :             : 
    2672                 :             : static void
    2673                 :       28631 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2674                 :             : {
    2675                 :       28631 :   gfc_gsymbol * gsym;
    2676                 :       28631 :   gfc_namespace *ns;
    2677                 :       28631 :   enum gfc_symbol_type type;
    2678                 :       28631 :   char reason[200];
    2679                 :             : 
    2680                 :       28631 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2681                 :             : 
    2682                 :       28631 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2683                 :       28631 :                           sym->binding_label != NULL);
    2684                 :             : 
    2685                 :       28631 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2686                 :          10 :     gfc_global_used (gsym, where);
    2687                 :             : 
    2688                 :       28631 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2689                 :        8740 :        || sym->attr.if_source == IFSRC_IFBODY)
    2690                 :       24658 :       && gsym->type != GSYM_UNKNOWN
    2691                 :       22522 :       && !gsym->binding_label
    2692                 :       20263 :       && gsym->ns
    2693                 :       14684 :       && gsym->ns->proc_name
    2694                 :       14684 :       && not_in_recursive (sym, gsym->ns)
    2695                 :       43315 :       && not_entry_self_reference (sym, gsym->ns))
    2696                 :             :     {
    2697                 :       14684 :       gfc_symbol *def_sym;
    2698                 :       14684 :       def_sym = gsym->ns->proc_name;
    2699                 :             : 
    2700                 :       14684 :       if (gsym->ns->resolved != -1)
    2701                 :             :         {
    2702                 :             : 
    2703                 :             :           /* Resolve the gsymbol namespace if needed.  */
    2704                 :       14663 :           if (!gsym->ns->resolved)
    2705                 :             :             {
    2706                 :        2740 :               gfc_symbol *old_dt_list;
    2707                 :             : 
    2708                 :             :               /* Stash away derived types so that the backend_decls
    2709                 :             :                  do not get mixed up.  */
    2710                 :        2740 :               old_dt_list = gfc_derived_types;
    2711                 :        2740 :               gfc_derived_types = NULL;
    2712                 :             : 
    2713                 :        2740 :               gfc_resolve (gsym->ns);
    2714                 :             : 
    2715                 :             :               /* Store the new derived types with the global namespace.  */
    2716                 :        2740 :               if (gfc_derived_types)
    2717                 :         293 :                 gsym->ns->derived_types = gfc_derived_types;
    2718                 :             : 
    2719                 :             :               /* Restore the derived types of this namespace.  */
    2720                 :        2740 :               gfc_derived_types = old_dt_list;
    2721                 :             :             }
    2722                 :             : 
    2723                 :             :           /* Make sure that translation for the gsymbol occurs before
    2724                 :             :              the procedure currently being resolved.  */
    2725                 :       14663 :           ns = gfc_global_ns_list;
    2726                 :       24708 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2727                 :             :             {
    2728                 :       16372 :               if (ns->sibling == gsym->ns)
    2729                 :             :                 {
    2730                 :        6327 :                   ns->sibling = gsym->ns->sibling;
    2731                 :        6327 :                   gsym->ns->sibling = gfc_global_ns_list;
    2732                 :        6327 :                   gfc_global_ns_list = gsym->ns;
    2733                 :        6327 :                   break;
    2734                 :             :                 }
    2735                 :             :             }
    2736                 :             : 
    2737                 :             :           /* This can happen if a binding name has been specified.  */
    2738                 :       14663 :           if (gsym->binding_label && gsym->sym_name != def_sym->name)
    2739                 :           0 :             gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
    2740                 :             : 
    2741                 :       14663 :           if (def_sym->attr.entry_master || def_sym->attr.entry)
    2742                 :             :             {
    2743                 :         953 :               gfc_entry_list *entry;
    2744                 :        1659 :               for (entry = gsym->ns->entries; entry; entry = entry->next)
    2745                 :        1659 :                 if (strcmp (entry->sym->name, sym->name) == 0)
    2746                 :             :                   {
    2747                 :         953 :                     def_sym = entry->sym;
    2748                 :         953 :                     break;
    2749                 :             :                   }
    2750                 :             :             }
    2751                 :             :         }
    2752                 :             : 
    2753                 :       14684 :       if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
    2754                 :             :         {
    2755                 :           6 :           gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
    2756                 :             :                      sym->name, &sym->declared_at, gfc_typename (&sym->ts),
    2757                 :           6 :                      gfc_typename (&def_sym->ts));
    2758                 :          29 :           goto done;
    2759                 :             :         }
    2760                 :             : 
    2761                 :       14678 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2762                 :       14678 :           && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
    2763                 :             :         {
    2764                 :           8 :           gfc_error ("Explicit interface required for %qs at %L: %s",
    2765                 :             :                      sym->name, &sym->declared_at, reason);
    2766                 :           8 :           goto done;
    2767                 :             :         }
    2768                 :             : 
    2769                 :       14670 :       bool bad_result_characteristics;
    2770                 :       14670 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2771                 :             :                                    reason, sizeof(reason), NULL, NULL,
    2772                 :             :                                    &bad_result_characteristics))
    2773                 :             :         {
    2774                 :             :           /* Turn erros into warnings with -std=gnu and -std=legacy,
    2775                 :             :              unless a function returns a wrong type, which can lead
    2776                 :             :              to all kinds of ICEs and wrong code.  */
    2777                 :             : 
    2778                 :          15 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2779                 :           2 :               && !bad_result_characteristics)
    2780                 :           2 :             gfc_errors_to_warnings (true);
    2781                 :             : 
    2782                 :          15 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2783                 :             :                      sym->name, &sym->declared_at, reason);
    2784                 :          15 :           sym->error = 1;
    2785                 :          15 :           gfc_errors_to_warnings (false);
    2786                 :          15 :           goto done;
    2787                 :             :         }
    2788                 :             :     }
    2789                 :             : 
    2790                 :       28631 : done:
    2791                 :             : 
    2792                 :       28631 :   if (gsym->type == GSYM_UNKNOWN)
    2793                 :             :     {
    2794                 :        3786 :       gsym->type = type;
    2795                 :        3786 :       gsym->where = *where;
    2796                 :             :     }
    2797                 :             : 
    2798                 :       28631 :   gsym->used = 1;
    2799                 :       28631 : }
    2800                 :             : 
    2801                 :             : 
    2802                 :             : /************* Function resolution *************/
    2803                 :             : 
    2804                 :             : /* Resolve a function call known to be generic.
    2805                 :             :    Section 14.1.2.4.1.  */
    2806                 :             : 
    2807                 :             : static match
    2808                 :       26716 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2809                 :             : {
    2810                 :       26716 :   gfc_symbol *s;
    2811                 :             : 
    2812                 :       26716 :   if (sym->attr.generic)
    2813                 :             :     {
    2814                 :       25611 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2815                 :       25611 :       if (s != NULL)
    2816                 :             :         {
    2817                 :       19638 :           expr->value.function.name = s->name;
    2818                 :       19638 :           expr->value.function.esym = s;
    2819                 :             : 
    2820                 :       19638 :           if (s->ts.type != BT_UNKNOWN)
    2821                 :       19621 :             expr->ts = s->ts;
    2822                 :          17 :           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
    2823                 :          15 :             expr->ts = s->result->ts;
    2824                 :             : 
    2825                 :       19638 :           if (s->as != NULL)
    2826                 :             :             {
    2827                 :          54 :               expr->rank = s->as->rank;
    2828                 :          54 :               expr->corank = s->as->corank;
    2829                 :             :             }
    2830                 :       19584 :           else if (s->result != NULL && s->result->as != NULL)
    2831                 :             :             {
    2832                 :           0 :               expr->rank = s->result->as->rank;
    2833                 :           0 :               expr->corank = s->result->as->corank;
    2834                 :             :             }
    2835                 :             : 
    2836                 :       19638 :           gfc_set_sym_referenced (expr->value.function.esym);
    2837                 :             : 
    2838                 :       19638 :           return MATCH_YES;
    2839                 :             :         }
    2840                 :             : 
    2841                 :             :       /* TODO: Need to search for elemental references in generic
    2842                 :             :          interface.  */
    2843                 :             :     }
    2844                 :             : 
    2845                 :        7078 :   if (sym->attr.intrinsic)
    2846                 :        1062 :     return gfc_intrinsic_func_interface (expr, 0);
    2847                 :             : 
    2848                 :             :   return MATCH_NO;
    2849                 :             : }
    2850                 :             : 
    2851                 :             : 
    2852                 :             : static bool
    2853                 :       26575 : resolve_generic_f (gfc_expr *expr)
    2854                 :             : {
    2855                 :       26575 :   gfc_symbol *sym;
    2856                 :       26575 :   match m;
    2857                 :       26575 :   gfc_interface *intr = NULL;
    2858                 :             : 
    2859                 :       26575 :   sym = expr->symtree->n.sym;
    2860                 :             : 
    2861                 :       26716 :   for (;;)
    2862                 :             :     {
    2863                 :       26716 :       m = resolve_generic_f0 (expr, sym);
    2864                 :       26716 :       if (m == MATCH_YES)
    2865                 :             :         return true;
    2866                 :        6018 :       else if (m == MATCH_ERROR)
    2867                 :             :         return false;
    2868                 :             : 
    2869                 :        6018 : generic:
    2870                 :        6021 :       if (!intr)
    2871                 :        5975 :         for (intr = sym->generic; intr; intr = intr->next)
    2872                 :        5892 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2873                 :             :             break;
    2874                 :             : 
    2875                 :        6021 :       if (sym->ns->parent == NULL)
    2876                 :             :         break;
    2877                 :         258 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2878                 :             : 
    2879                 :         258 :       if (sym == NULL)
    2880                 :             :         break;
    2881                 :         144 :       if (!generic_sym (sym))
    2882                 :           3 :         goto generic;
    2883                 :             :     }
    2884                 :             : 
    2885                 :             :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2886                 :             :      that possesses a matching interface.  14.1.2.4  */
    2887                 :        5877 :   if (sym  && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
    2888                 :             :     {
    2889                 :           4 :       if (gfc_init_expr_flag)
    2890                 :           1 :         gfc_error ("Function %qs in initialization expression at %L "
    2891                 :             :                    "must be an intrinsic function",
    2892                 :           1 :                    expr->symtree->n.sym->name, &expr->where);
    2893                 :             :       else
    2894                 :           3 :         gfc_error ("There is no specific function for the generic %qs "
    2895                 :           3 :                    "at %L", expr->symtree->n.sym->name, &expr->where);
    2896                 :           4 :       return false;
    2897                 :             :     }
    2898                 :             : 
    2899                 :        5873 :   if (intr)
    2900                 :             :     {
    2901                 :        5838 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2902                 :             :                                                  NULL, false))
    2903                 :             :         return false;
    2904                 :        5818 :       if (!gfc_use_derived (expr->ts.u.derived))
    2905                 :             :         return false;
    2906                 :        5817 :       return resolve_structure_cons (expr, 0);
    2907                 :             :     }
    2908                 :             : 
    2909                 :          35 :   m = gfc_intrinsic_func_interface (expr, 0);
    2910                 :          35 :   if (m == MATCH_YES)
    2911                 :             :     return true;
    2912                 :             : 
    2913                 :           3 :   if (m == MATCH_NO)
    2914                 :           3 :     gfc_error ("Generic function %qs at %L is not consistent with a "
    2915                 :           3 :                "specific intrinsic interface", expr->symtree->n.sym->name,
    2916                 :             :                &expr->where);
    2917                 :             : 
    2918                 :             :   return false;
    2919                 :             : }
    2920                 :             : 
    2921                 :             : 
    2922                 :             : /* Resolve a function call known to be specific.  */
    2923                 :             : 
    2924                 :             : static match
    2925                 :       26968 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2926                 :             : {
    2927                 :       26968 :   match m;
    2928                 :             : 
    2929                 :       26968 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2930                 :             :     {
    2931                 :        7762 :       if (sym->attr.dummy)
    2932                 :             :         {
    2933                 :         267 :           sym->attr.proc = PROC_DUMMY;
    2934                 :         267 :           goto found;
    2935                 :             :         }
    2936                 :             : 
    2937                 :        7495 :       sym->attr.proc = PROC_EXTERNAL;
    2938                 :        7495 :       goto found;
    2939                 :             :     }
    2940                 :             : 
    2941                 :       19206 :   if (sym->attr.proc == PROC_MODULE
    2942                 :             :       || sym->attr.proc == PROC_ST_FUNCTION
    2943                 :             :       || sym->attr.proc == PROC_INTERNAL)
    2944                 :       18468 :     goto found;
    2945                 :             : 
    2946                 :         738 :   if (sym->attr.intrinsic)
    2947                 :             :     {
    2948                 :         731 :       m = gfc_intrinsic_func_interface (expr, 1);
    2949                 :         731 :       if (m == MATCH_YES)
    2950                 :             :         return MATCH_YES;
    2951                 :           0 :       if (m == MATCH_NO)
    2952                 :           0 :         gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
    2953                 :             :                    "with an intrinsic", sym->name, &expr->where);
    2954                 :             : 
    2955                 :           0 :       return MATCH_ERROR;
    2956                 :             :     }
    2957                 :             : 
    2958                 :             :   return MATCH_NO;
    2959                 :             : 
    2960                 :       26230 : found:
    2961                 :       26230 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    2962                 :             : 
    2963                 :       26230 :   if (sym->result)
    2964                 :       26230 :     expr->ts = sym->result->ts;
    2965                 :             :   else
    2966                 :           0 :     expr->ts = sym->ts;
    2967                 :       26230 :   expr->value.function.name = sym->name;
    2968                 :       26230 :   expr->value.function.esym = sym;
    2969                 :             :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    2970                 :             :      error(s).  */
    2971                 :       26230 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    2972                 :             :     return MATCH_ERROR;
    2973                 :       26229 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
    2974                 :             :     {
    2975                 :         310 :       expr->rank = CLASS_DATA (sym)->as->rank;
    2976                 :         310 :       expr->corank = CLASS_DATA (sym)->as->corank;
    2977                 :             :     }
    2978                 :       25919 :   else if (sym->as != NULL)
    2979                 :             :     {
    2980                 :        2241 :       expr->rank = sym->as->rank;
    2981                 :        2241 :       expr->corank = sym->as->corank;
    2982                 :             :     }
    2983                 :             : 
    2984                 :             :   return MATCH_YES;
    2985                 :             : }
    2986                 :             : 
    2987                 :             : 
    2988                 :             : static bool
    2989                 :       26961 : resolve_specific_f (gfc_expr *expr)
    2990                 :             : {
    2991                 :       26961 :   gfc_symbol *sym;
    2992                 :       26961 :   match m;
    2993                 :             : 
    2994                 :       26961 :   sym = expr->symtree->n.sym;
    2995                 :             : 
    2996                 :       26968 :   for (;;)
    2997                 :             :     {
    2998                 :       26968 :       m = resolve_specific_f0 (sym, expr);
    2999                 :       26968 :       if (m == MATCH_YES)
    3000                 :             :         return true;
    3001                 :           8 :       if (m == MATCH_ERROR)
    3002                 :             :         return false;
    3003                 :             : 
    3004                 :           7 :       if (sym->ns->parent == NULL)
    3005                 :             :         break;
    3006                 :             : 
    3007                 :           7 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3008                 :             : 
    3009                 :           7 :       if (sym == NULL)
    3010                 :             :         break;
    3011                 :             :     }
    3012                 :             : 
    3013                 :           0 :   gfc_error ("Unable to resolve the specific function %qs at %L",
    3014                 :           0 :              expr->symtree->n.sym->name, &expr->where);
    3015                 :             : 
    3016                 :           0 :   return true;
    3017                 :             : }
    3018                 :             : 
    3019                 :             : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
    3020                 :             :    candidates in CANDIDATES_LEN.  */
    3021                 :             : 
    3022                 :             : static void
    3023                 :         209 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
    3024                 :             :                                        char **&candidates,
    3025                 :             :                                        size_t &candidates_len)
    3026                 :             : {
    3027                 :         370 :   gfc_symtree *p;
    3028                 :             : 
    3029                 :         370 :   if (sym == NULL)
    3030                 :             :     return;
    3031                 :         370 :   if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
    3032                 :         120 :       && sym->n.sym->attr.flavor == FL_PROCEDURE)
    3033                 :          47 :     vec_push (candidates, candidates_len, sym->name);
    3034                 :             : 
    3035                 :         370 :   p = sym->left;
    3036                 :         370 :   if (p)
    3037                 :         153 :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3038                 :             : 
    3039                 :         370 :   p = sym->right;
    3040                 :         370 :   if (p)
    3041                 :             :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3042                 :             : }
    3043                 :             : 
    3044                 :             : 
    3045                 :             : /* Lookup function FN fuzzily, taking names in SYMROOT into account.  */
    3046                 :             : 
    3047                 :             : const char*
    3048                 :          56 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
    3049                 :             : {
    3050                 :          56 :   char **candidates = NULL;
    3051                 :          56 :   size_t candidates_len = 0;
    3052                 :          56 :   lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
    3053                 :          56 :   return gfc_closest_fuzzy_match (fn, candidates);
    3054                 :             : }
    3055                 :             : 
    3056                 :             : 
    3057                 :             : /* Resolve a procedure call not known to be generic nor specific.  */
    3058                 :             : 
    3059                 :             : static bool
    3060                 :      260492 : resolve_unknown_f (gfc_expr *expr)
    3061                 :             : {
    3062                 :      260492 :   gfc_symbol *sym;
    3063                 :      260492 :   gfc_typespec *ts;
    3064                 :             : 
    3065                 :      260492 :   sym = expr->symtree->n.sym;
    3066                 :             : 
    3067                 :      260492 :   if (sym->attr.dummy)
    3068                 :             :     {
    3069                 :         288 :       sym->attr.proc = PROC_DUMMY;
    3070                 :         288 :       expr->value.function.name = sym->name;
    3071                 :         288 :       goto set_type;
    3072                 :             :     }
    3073                 :             : 
    3074                 :             :   /* See if we have an intrinsic function reference.  */
    3075                 :             : 
    3076                 :      260204 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3077                 :             :     {
    3078                 :      257945 :       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
    3079                 :             :         return true;
    3080                 :             :       return false;
    3081                 :             :     }
    3082                 :             : 
    3083                 :             :   /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
    3084                 :             :   /* Intrinsics were handled above, only non-intrinsics left here.  */
    3085                 :        2259 :   if (sym->attr.flavor == FL_PROCEDURE
    3086                 :        2256 :       && sym->attr.implicit_type
    3087                 :         375 :       && sym->ns
    3088                 :         375 :       && sym->ns->has_implicit_none_export)
    3089                 :             :     {
    3090                 :           3 :           gfc_error ("Missing explicit declaration with EXTERNAL attribute "
    3091                 :             :               "for symbol %qs at %L", sym->name, &sym->declared_at);
    3092                 :           3 :           sym->error = 1;
    3093                 :           3 :           return false;
    3094                 :             :     }
    3095                 :             : 
    3096                 :             :   /* The reference is to an external name.  */
    3097                 :             : 
    3098                 :        2256 :   sym->attr.proc = PROC_EXTERNAL;
    3099                 :        2256 :   expr->value.function.name = sym->name;
    3100                 :        2256 :   expr->value.function.esym = expr->symtree->n.sym;
    3101                 :             : 
    3102                 :        2256 :   if (sym->as != NULL)
    3103                 :             :     {
    3104                 :           1 :       expr->rank = sym->as->rank;
    3105                 :           1 :       expr->corank = sym->as->corank;
    3106                 :             :     }
    3107                 :             : 
    3108                 :             :   /* Type of the expression is either the type of the symbol or the
    3109                 :             :      default type of the symbol.  */
    3110                 :             : 
    3111                 :        2255 : set_type:
    3112                 :        2544 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3113                 :             : 
    3114                 :        2544 :   if (sym->ts.type != BT_UNKNOWN)
    3115                 :        2493 :     expr->ts = sym->ts;
    3116                 :             :   else
    3117                 :             :     {
    3118                 :          51 :       ts = gfc_get_default_type (sym->name, sym->ns);
    3119                 :             : 
    3120                 :          51 :       if (ts->type == BT_UNKNOWN)
    3121                 :             :         {
    3122                 :          41 :           const char *guessed
    3123                 :          41 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    3124                 :          41 :           if (guessed)
    3125                 :           3 :             gfc_error ("Function %qs at %L has no IMPLICIT type"
    3126                 :             :                        "; did you mean %qs?",
    3127                 :             :                        sym->name, &expr->where, guessed);
    3128                 :             :           else
    3129                 :          38 :             gfc_error ("Function %qs at %L has no IMPLICIT type",
    3130                 :             :                        sym->name, &expr->where);
    3131                 :          41 :           return false;
    3132                 :             :         }
    3133                 :             :       else
    3134                 :          10 :         expr->ts = *ts;
    3135                 :             :     }
    3136                 :             : 
    3137                 :             :   return true;
    3138                 :             : }
    3139                 :             : 
    3140                 :             : 
    3141                 :             : /* Return true, if the symbol is an external procedure.  */
    3142                 :             : static bool
    3143                 :      809758 : is_external_proc (gfc_symbol *sym)
    3144                 :             : {
    3145                 :      808092 :   if (!sym->attr.dummy && !sym->attr.contained
    3146                 :      702928 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3147                 :      155844 :         && sym->attr.proc != PROC_ST_FUNCTION
    3148                 :             :         && !sym->attr.proc_pointer
    3149                 :      155249 :         && !sym->attr.use_assoc
    3150                 :      867044 :         && sym->name)
    3151                 :             :     return true;
    3152                 :             : 
    3153                 :             :   return false;
    3154                 :             : }
    3155                 :             : 
    3156                 :             : 
    3157                 :             : /* Figure out if a function reference is pure or not.  Also set the name
    3158                 :             :    of the function for a potential error message.  Return nonzero if the
    3159                 :             :    function is PURE, zero if not.  */
    3160                 :             : static bool
    3161                 :             : pure_stmt_function (gfc_expr *, gfc_symbol *);
    3162                 :             : 
    3163                 :             : bool
    3164                 :      241523 : gfc_pure_function (gfc_expr *e, const char **name)
    3165                 :             : {
    3166                 :      241523 :   bool pure;
    3167                 :      241523 :   gfc_component *comp;
    3168                 :             : 
    3169                 :      241523 :   *name = NULL;
    3170                 :             : 
    3171                 :      241523 :   if (e->symtree != NULL
    3172                 :      241207 :         && e->symtree->n.sym != NULL
    3173                 :      241207 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3174                 :         305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3175                 :             : 
    3176                 :      241218 :   comp = gfc_get_proc_ptr_comp (e);
    3177                 :      241218 :   if (comp)
    3178                 :             :     {
    3179                 :         290 :       pure = gfc_pure (comp->ts.interface);
    3180                 :         290 :       *name = comp->name;
    3181                 :             :     }
    3182                 :      240928 :   else if (e->value.function.esym)
    3183                 :             :     {
    3184                 :       51368 :       pure = gfc_pure (e->value.function.esym);
    3185                 :       51368 :       *name = e->value.function.esym->name;
    3186                 :             :     }
    3187                 :      189560 :   else if (e->value.function.isym)
    3188                 :             :     {
    3189                 :      188573 :       pure = e->value.function.isym->pure
    3190                 :      188573 :              || e->value.function.isym->elemental;
    3191                 :      188573 :       *name = e->value.function.isym->name;
    3192                 :             :     }
    3193                 :         987 :   else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
    3194                 :             :     {
    3195                 :             :       /* The function has been resolved, but esym is not yet set.
    3196                 :             :          This can happen with functions as dummy argument.  */
    3197                 :         286 :       pure = e->symtree->n.sym->attr.pure;
    3198                 :         286 :       *name = e->symtree->n.sym->name;
    3199                 :             :     }
    3200                 :             :   else
    3201                 :             :     {
    3202                 :             :       /* Implicit functions are not pure.  */
    3203                 :         701 :       pure = 0;
    3204                 :         701 :       *name = e->value.function.name;
    3205                 :             :     }
    3206                 :             : 
    3207                 :             :   return pure;
    3208                 :             : }
    3209                 :             : 
    3210                 :             : 
    3211                 :             : /* Check if the expression is a reference to an implicitly pure function.  */
    3212                 :             : 
    3213                 :             : bool
    3214                 :       36682 : gfc_implicit_pure_function (gfc_expr *e)
    3215                 :             : {
    3216                 :       36682 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3217                 :       36682 :   if (comp)
    3218                 :         274 :     return gfc_implicit_pure (comp->ts.interface);
    3219                 :       36408 :   else if (e->value.function.esym)
    3220                 :       31089 :     return gfc_implicit_pure (e->value.function.esym);
    3221                 :             :   else
    3222                 :             :     return 0;
    3223                 :             : }
    3224                 :             : 
    3225                 :             : 
    3226                 :             : static bool
    3227                 :         981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
    3228                 :             :                  int *f ATTRIBUTE_UNUSED)
    3229                 :             : {
    3230                 :         981 :   const char *name;
    3231                 :             : 
    3232                 :             :   /* Don't bother recursing into other statement functions
    3233                 :             :      since they will be checked individually for purity.  */
    3234                 :         981 :   if (e->expr_type != EXPR_FUNCTION
    3235                 :         343 :         || !e->symtree
    3236                 :         343 :         || e->symtree->n.sym == sym
    3237                 :          20 :         || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3238                 :             :     return false;
    3239                 :             : 
    3240                 :          19 :   return gfc_pure_function (e, &name) ? false : true;
    3241                 :             : }
    3242                 :             : 
    3243                 :             : 
    3244                 :             : static bool
    3245                 :         305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
    3246                 :             : {
    3247                 :         305 :   return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
    3248                 :             : }
    3249                 :             : 
    3250                 :             : 
    3251                 :             : /* Check if an impure function is allowed in the current context. */
    3252                 :             : 
    3253                 :      229793 : static bool check_pure_function (gfc_expr *e)
    3254                 :             : {
    3255                 :      229793 :   const char *name = NULL;
    3256                 :      229793 :   code_stack *stack;
    3257                 :      229793 :   bool saw_block = false;
    3258                 :             : 
    3259                 :             :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3260                 :             :      gfc_do_concurrent_flag = 0 when the check for an impure function
    3261                 :             :      occurs.  Check the stack to see if the source code has a nested
    3262                 :             :      BLOCK construct.  */
    3263                 :             : 
    3264                 :      533314 :   for (stack = cs_base; stack; stack = stack->prev)
    3265                 :             :     {
    3266                 :      303523 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3267                 :             :         {
    3268                 :        6582 :           saw_block = true;
    3269                 :        6582 :           continue;
    3270                 :             :         }
    3271                 :             : 
    3272                 :        5036 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3273                 :             :         {
    3274                 :           9 :           bool is_pure;
    3275                 :      303521 :           is_pure = (e->value.function.isym
    3276                 :           8 :                      && (e->value.function.isym->pure
    3277                 :           8 :                          || e->value.function.isym->elemental))
    3278                 :          10 :                     || (e->value.function.esym
    3279                 :           1 :                         && (e->value.function.esym->attr.pure
    3280                 :           1 :                             || e->value.function.esym->attr.elemental));
    3281                 :           2 :           if (!is_pure)
    3282                 :             :             {
    3283                 :           2 :               gfc_error ("Reference to impure function at %L inside a "
    3284                 :             :                          "DO CONCURRENT", &e->where);
    3285                 :           2 :               return false;
    3286                 :             :             }
    3287                 :             :         }
    3288                 :             :     }
    3289                 :             : 
    3290                 :      229791 :   if (!gfc_pure_function (e, &name) && name)
    3291                 :             :     {
    3292                 :       35416 :       if (forall_flag)
    3293                 :             :         {
    3294                 :           4 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3295                 :             :                      "FORALL %s", name, &e->where,
    3296                 :             :                      forall_flag == 2 ? "mask" : "block");
    3297                 :           4 :           return false;
    3298                 :             :         }
    3299                 :       35412 :       else if (gfc_do_concurrent_flag)
    3300                 :             :         {
    3301                 :           2 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3302                 :             :                      "DO CONCURRENT %s", name, &e->where,
    3303                 :             :                      gfc_do_concurrent_flag == 2 ? "mask" : "block");
    3304                 :           2 :           return false;
    3305                 :             :         }
    3306                 :       35410 :       else if (gfc_pure (NULL))
    3307                 :             :         {
    3308                 :           5 :           gfc_error ("Reference to impure function %qs at %L "
    3309                 :             :                      "within a PURE procedure", name, &e->where);
    3310                 :           5 :           return false;
    3311                 :             :         }
    3312                 :       35405 :       if (!gfc_implicit_pure_function (e))
    3313                 :       29489 :         gfc_unset_implicit_pure (NULL);
    3314                 :             :     }
    3315                 :             :   return true;
    3316                 :             : }
    3317                 :             : 
    3318                 :             : 
    3319                 :             : /* Update current procedure's array_outer_dependency flag, considering
    3320                 :             :    a call to procedure SYM.  */
    3321                 :             : 
    3322                 :             : static void
    3323                 :      128299 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
    3324                 :             : {
    3325                 :             :   /* Check to see if this is a sibling function that has not yet
    3326                 :             :      been resolved.  */
    3327                 :      128299 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3328                 :      242420 :   for (; sibling; sibling = sibling->sibling)
    3329                 :             :     {
    3330                 :      120830 :       if (sibling->proc_name == sym)
    3331                 :             :         {
    3332                 :        6709 :           gfc_resolve (sibling);
    3333                 :        6709 :           break;
    3334                 :             :         }
    3335                 :             :     }
    3336                 :             : 
    3337                 :             :   /* If SYM has references to outer arrays, so has the procedure calling
    3338                 :             :      SYM.  If SYM is a procedure pointer, we can assume the worst.  */
    3339                 :      128299 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3340                 :       66164 :       && gfc_current_ns->proc_name)
    3341                 :       66120 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3342                 :      128299 : }
    3343                 :             : 
    3344                 :             : 
    3345                 :             : /* Resolve a function call, which means resolving the arguments, then figuring
    3346                 :             :    out which entity the name refers to.  */
    3347                 :             : 
    3348                 :             : static bool
    3349                 :      326793 : resolve_function (gfc_expr *expr)
    3350                 :             : {
    3351                 :      326793 :   gfc_actual_arglist *arg;
    3352                 :      326793 :   gfc_symbol *sym;
    3353                 :      326793 :   bool t;
    3354                 :      326793 :   int temp;
    3355                 :      326793 :   procedure_type p = PROC_INTRINSIC;
    3356                 :      326793 :   bool no_formal_args;
    3357                 :             : 
    3358                 :      326793 :   sym = NULL;
    3359                 :      326793 :   if (expr->symtree)
    3360                 :      326477 :     sym = expr->symtree->n.sym;
    3361                 :             : 
    3362                 :             :   /* If this is a procedure pointer component, it has already been resolved.  */
    3363                 :      326793 :   if (gfc_is_proc_ptr_comp (expr))
    3364                 :             :     return true;
    3365                 :             : 
    3366                 :             :   /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
    3367                 :             :      another caf_get.  */
    3368                 :      326396 :   if (sym && sym->attr.intrinsic
    3369                 :        8255 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3370                 :        8255 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3371                 :             :     return true;
    3372                 :             : 
    3373                 :      326396 :   if (expr->ref)
    3374                 :             :     {
    3375                 :           1 :       gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
    3376                 :             :                  &expr->where);
    3377                 :           1 :       return false;
    3378                 :             :     }
    3379                 :             : 
    3380                 :      326079 :   if (sym && sym->attr.intrinsic
    3381                 :      334650 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3382                 :             :     return false;
    3383                 :             : 
    3384                 :      326395 :   if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
    3385                 :             :     {
    3386                 :           4 :       gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
    3387                 :           4 :       return false;
    3388                 :             :     }
    3389                 :             : 
    3390                 :             :   /* If this is a deferred TBP with an abstract interface (which may
    3391                 :             :      of course be referenced), expr->value.function.esym will be set.  */
    3392                 :      326075 :   if (sym && sym->attr.abstract && !expr->value.function.esym)
    3393                 :             :     {
    3394                 :           1 :       gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3395                 :             :                  sym->name, &expr->where);
    3396                 :           1 :       return false;
    3397                 :             :     }
    3398                 :             : 
    3399                 :             :   /* If this is a deferred TBP with an abstract interface, its result
    3400                 :             :      cannot be an assumed length character (F2003: C418).  */
    3401                 :      326074 :   if (sym && sym->attr.abstract && sym->attr.function
    3402                 :         190 :       && sym->result->ts.u.cl
    3403                 :         157 :       && sym->result->ts.u.cl->length == NULL
    3404                 :           2 :       && !sym->result->ts.deferred)
    3405                 :             :     {
    3406                 :           1 :       gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
    3407                 :             :                  "character length result (F2008: C418)", sym->name,
    3408                 :             :                  &sym->declared_at);
    3409                 :           1 :       return false;
    3410                 :             :     }
    3411                 :             : 
    3412                 :             :   /* Switch off assumed size checking and do this again for certain kinds
    3413                 :             :      of procedure, once the procedure itself is resolved.  */
    3414                 :      326389 :   need_full_assumed_size++;
    3415                 :             : 
    3416                 :      326389 :   if (expr->symtree && expr->symtree->n.sym)
    3417                 :      326073 :     p = expr->symtree->n.sym->attr.proc;
    3418                 :             : 
    3419                 :      326389 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3420                 :        1071 :     inquiry_argument = true;
    3421                 :      326073 :   no_formal_args = sym && is_external_proc (sym)
    3422                 :      339656 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3423                 :             : 
    3424                 :      326389 :   if (!resolve_actual_arglist (expr->value.function.actual,
    3425                 :             :                                p, no_formal_args))
    3426                 :             :     {
    3427                 :          66 :       inquiry_argument = false;
    3428                 :          66 :       return false;
    3429                 :             :     }
    3430                 :             : 
    3431                 :      326323 :   inquiry_argument = false;
    3432                 :             : 
    3433                 :             :   /* Resume assumed_size checking.  */
    3434                 :      326323 :   need_full_assumed_size--;
    3435                 :             : 
    3436                 :             :   /* If the procedure is external, check for usage.  */
    3437                 :      326323 :   if (sym && is_external_proc (sym))
    3438                 :       13248 :     resolve_global_procedure (sym, &expr->where, 0);
    3439                 :             : 
    3440                 :      326323 :   if (sym && sym->ts.type == BT_CHARACTER
    3441                 :        3155 :       && sym->ts.u.cl
    3442                 :        3095 :       && sym->ts.u.cl->length == NULL
    3443                 :         583 :       && !sym->attr.dummy
    3444                 :         578 :       && !sym->ts.deferred
    3445                 :           2 :       && expr->value.function.esym == NULL
    3446                 :           2 :       && !sym->attr.contained)
    3447                 :             :     {
    3448                 :             :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3449                 :           1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3450                 :             :                  "be used at %L since it is not a dummy argument",
    3451                 :             :                  sym->name, &expr->where);
    3452                 :           1 :       return false;
    3453                 :             :     }
    3454                 :             : 
    3455                 :             :   /* Add and check formal interface when -fc-prototypes-external is in
    3456                 :             :      force, see comment in resolve_call().  */
    3457                 :             : 
    3458                 :      326322 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3459                 :        2559 :       && sym->attr.external)
    3460                 :             :     {
    3461                 :          12 :       if (sym->formal)
    3462                 :             :         {
    3463                 :           6 :           bool conflict;
    3464                 :           6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3465                 :             :                                                  sym->formal, 0, 0, 0, NULL);
    3466                 :           6 :           if (conflict)
    3467                 :             :             {
    3468                 :           6 :               sym->ext_dummy_arglist_mismatch = 1;
    3469                 :           6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3470                 :             :                            "Different argument lists in external dummy "
    3471                 :             :                            "function %s at %L and %L", sym->name,
    3472                 :             :                            &expr->where, &sym->formal_at);
    3473                 :             :             }
    3474                 :             :         }
    3475                 :             :       else
    3476                 :             :         {
    3477                 :           6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3478                 :           6 :           sym->formal_at = expr->where;
    3479                 :             :         }
    3480                 :             :     }
    3481                 :             :   /* See if function is already resolved.  */
    3482                 :             : 
    3483                 :      326322 :   if (expr->value.function.name != NULL
    3484                 :      314814 :       || expr->value.function.isym != NULL)
    3485                 :             :     {
    3486                 :       12294 :       if (expr->ts.type == BT_UNKNOWN)
    3487                 :           3 :         expr->ts = sym->ts;
    3488                 :             :       t = true;
    3489                 :             :     }
    3490                 :             :   else
    3491                 :             :     {
    3492                 :             :       /* Apply the rules of section 14.1.2.  */
    3493                 :             : 
    3494                 :      314028 :       switch (procedure_kind (sym))
    3495                 :             :         {
    3496                 :       26575 :         case PTYPE_GENERIC:
    3497                 :       26575 :           t = resolve_generic_f (expr);
    3498                 :       26575 :           break;
    3499                 :             : 
    3500                 :       26961 :         case PTYPE_SPECIFIC:
    3501                 :       26961 :           t = resolve_specific_f (expr);
    3502                 :       26961 :           break;
    3503                 :             : 
    3504                 :      260492 :         case PTYPE_UNKNOWN:
    3505                 :      260492 :           t = resolve_unknown_f (expr);
    3506                 :      260492 :           break;
    3507                 :             : 
    3508                 :             :         default:
    3509                 :             :           gfc_internal_error ("resolve_function(): bad function type");
    3510                 :             :         }
    3511                 :             :     }
    3512                 :             : 
    3513                 :             :   /* If the expression is still a function (it might have simplified),
    3514                 :             :      then we check to see if we are calling an elemental function.  */
    3515                 :             : 
    3516                 :      326322 :   if (expr->expr_type != EXPR_FUNCTION)
    3517                 :             :     return t;
    3518                 :             : 
    3519                 :             :   /* Walk the argument list looking for invalid BOZ.  */
    3520                 :      690391 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3521                 :      460866 :     if (arg->expr && arg->expr->ts.type == BT_BOZ)
    3522                 :             :       {
    3523                 :           5 :         gfc_error ("A BOZ literal constant at %L cannot appear as an "
    3524                 :             :                    "actual argument in a function reference",
    3525                 :             :                    &arg->expr->where);
    3526                 :           5 :         return false;
    3527                 :             :       }
    3528                 :             : 
    3529                 :      229525 :   temp = need_full_assumed_size;
    3530                 :      229525 :   need_full_assumed_size = 0;
    3531                 :             : 
    3532                 :      229525 :   if (!resolve_elemental_actual (expr, NULL))
    3533                 :             :     return false;
    3534                 :             : 
    3535                 :      229522 :   if (omp_workshare_flag
    3536                 :          32 :       && expr->value.function.esym
    3537                 :      229527 :       && ! gfc_elemental (expr->value.function.esym))
    3538                 :             :     {
    3539                 :           4 :       gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
    3540                 :           4 :                  "in WORKSHARE construct", expr->value.function.esym->name,
    3541                 :             :                  &expr->where);
    3542                 :           4 :       t = false;
    3543                 :             :     }
    3544                 :             : 
    3545                 :             : #define GENERIC_ID expr->value.function.isym->id
    3546                 :      229518 :   else if (expr->value.function.actual != NULL
    3547                 :      221784 :            && expr->value.function.isym != NULL
    3548                 :      177973 :            && GENERIC_ID != GFC_ISYM_LBOUND
    3549                 :             :            && GENERIC_ID != GFC_ISYM_LCOBOUND
    3550                 :             :            && GENERIC_ID != GFC_ISYM_UCOBOUND
    3551                 :             :            && GENERIC_ID != GFC_ISYM_LEN
    3552                 :             :            && GENERIC_ID != GFC_ISYM_LOC
    3553                 :             :            && GENERIC_ID != GFC_ISYM_C_LOC
    3554                 :             :            && GENERIC_ID != GFC_ISYM_PRESENT)
    3555                 :             :     {
    3556                 :             :       /* Array intrinsics must also have the last upper bound of an
    3557                 :             :          assumed size array argument.  UBOUND and SIZE have to be
    3558                 :             :          excluded from the check if the second argument is anything
    3559                 :             :          than a constant.  */
    3560                 :             : 
    3561                 :      492949 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3562                 :             :         {
    3563                 :      340182 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3564                 :       44554 :               && arg == expr->value.function.actual
    3565                 :       16454 :               && arg->next != NULL && arg->next->expr)
    3566                 :             :             {
    3567                 :        8187 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3568                 :             :                 break;
    3569                 :             : 
    3570                 :        7963 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3571                 :             :                 break;
    3572                 :             : 
    3573                 :        7963 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3574                 :        7963 :                         < arg->expr->rank)
    3575                 :             :                 break;
    3576                 :             :             }
    3577                 :             : 
    3578                 :      337780 :           if (arg->expr != NULL
    3579                 :      225115 :               && arg->expr->rank > 0
    3580                 :      447114 :               && resolve_assumed_size_actual (arg->expr))
    3581                 :             :             return false;
    3582                 :             :         }
    3583                 :             :     }
    3584                 :             : #undef GENERIC_ID
    3585                 :             : 
    3586                 :      229519 :   need_full_assumed_size = temp;
    3587                 :             : 
    3588                 :      229519 :   if (!check_pure_function(expr))
    3589                 :          12 :     t = false;
    3590                 :             : 
    3591                 :             :   /* Functions without the RECURSIVE attribution are not allowed to
    3592                 :             :    * call themselves.  */
    3593                 :      229519 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3594                 :             :     {
    3595                 :       50163 :       gfc_symbol *esym;
    3596                 :       50163 :       esym = expr->value.function.esym;
    3597                 :             : 
    3598                 :       50163 :       if (is_illegal_recursion (esym, gfc_current_ns))
    3599                 :             :       {
    3600                 :           5 :         if (esym->attr.entry && esym->ns->entries)
    3601                 :           3 :           gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
    3602                 :             :                      " function %qs is not RECURSIVE",
    3603                 :           3 :                      esym->name, &expr->where, esym->ns->entries->sym->name);
    3604                 :             :         else
    3605                 :           2 :           gfc_error ("Function %qs at %L cannot be called recursively, as it"
    3606                 :             :                      " is not RECURSIVE", esym->name, &expr->where);
    3607                 :             : 
    3608                 :             :         t = false;
    3609                 :             :       }
    3610                 :             :     }
    3611                 :             : 
    3612                 :             :   /* Character lengths of use associated functions may contains references to
    3613                 :             :      symbols not referenced from the current program unit otherwise.  Make sure
    3614                 :             :      those symbols are marked as referenced.  */
    3615                 :             : 
    3616                 :      229519 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3617                 :        3293 :       && expr->value.function.esym->attr.use_assoc)
    3618                 :             :     {
    3619                 :        1236 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3620                 :             :     }
    3621                 :             : 
    3622                 :             :   /* Make sure that the expression has a typespec that works.  */
    3623                 :      229519 :   if (expr->ts.type == BT_UNKNOWN)
    3624                 :             :     {
    3625                 :         834 :       if (expr->symtree->n.sym->result
    3626                 :         826 :             && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
    3627                 :         523 :             && !expr->symtree->n.sym->result->attr.proc_pointer)
    3628                 :         523 :         expr->ts = expr->symtree->n.sym->result->ts;
    3629                 :             :     }
    3630                 :             : 
    3631                 :             :   /* These derived types with an incomplete namespace, arising from use
    3632                 :             :      association, cause gfc_get_derived_vtab to segfault. If the function
    3633                 :             :      namespace does not suffice, something is badly wrong.  */
    3634                 :      229519 :   if (expr->ts.type == BT_DERIVED
    3635                 :        8773 :       && !expr->ts.u.derived->ns->proc_name)
    3636                 :             :     {
    3637                 :           3 :       gfc_symbol *der;
    3638                 :           3 :       gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
    3639                 :           3 :       if (der)
    3640                 :             :         {
    3641                 :           3 :           expr->ts.u.derived->refs--;
    3642                 :           3 :           expr->ts.u.derived = der;
    3643                 :           3 :           der->refs++;
    3644                 :             :         }
    3645                 :             :       else
    3646                 :           0 :         expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
    3647                 :             :     }
    3648                 :             : 
    3649                 :      229519 :   if (!expr->ref && !expr->value.function.isym)
    3650                 :             :     {
    3651                 :       51425 :       if (expr->value.function.esym)
    3652                 :       50438 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3653                 :             :       else
    3654                 :         987 :         update_current_proc_array_outer_dependency (sym);
    3655                 :             :     }
    3656                 :      178094 :   else if (expr->ref)
    3657                 :             :     /* typebound procedure: Assume the worst.  */
    3658                 :           0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3659                 :             : 
    3660                 :      229519 :   if (expr->value.function.esym
    3661                 :       50438 :       && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    3662                 :           2 :     gfc_warning (OPT_Wdeprecated_declarations,
    3663                 :             :                  "Using function %qs at %L is deprecated",
    3664                 :             :                  sym->name, &expr->where);
    3665                 :             :   return t;
    3666                 :             : }
    3667                 :             : 
    3668                 :             : 
    3669                 :             : /************* Subroutine resolution *************/
    3670                 :             : 
    3671                 :             : static bool
    3672                 :       74795 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3673                 :             : {
    3674                 :       74795 :   code_stack *stack;
    3675                 :       74795 :   bool saw_block = false;
    3676                 :             : 
    3677                 :       74795 :   if (gfc_pure (sym))
    3678                 :             :     return true;
    3679                 :             : 
    3680                 :             :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3681                 :             :      gfc_do_concurrent_flag = 0 when the check for an impure subroutine
    3682                 :             :      occurs.  Walk up the stack to see if the source code has a nested
    3683                 :             :      construct.  */
    3684                 :             : 
    3685                 :      154819 :   for (stack = cs_base; stack; stack = stack->prev)
    3686                 :             :     {
    3687                 :       85422 :       if (stack->current->op == EXEC_BLOCK)
    3688                 :             :         {
    3689                 :        1874 :           saw_block = true;
    3690                 :        1874 :           continue;
    3691                 :             :         }
    3692                 :             : 
    3693                 :       83548 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3694                 :             :         {
    3695                 :             : 
    3696                 :           2 :           bool is_pure = true;
    3697                 :           2 :           is_pure = sym->attr.pure || sym->attr.elemental;
    3698                 :             : 
    3699                 :           2 :           if (!is_pure)
    3700                 :             :             {
    3701                 :           2 :               gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
    3702                 :             :                          "is not PURE", loc);
    3703                 :           2 :               return false;
    3704                 :             :             }
    3705                 :             :         }
    3706                 :             :     }
    3707                 :             : 
    3708                 :       69397 :   if (forall_flag)
    3709                 :             :     {
    3710                 :           0 :       gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
    3711                 :             :                  name, loc);
    3712                 :           0 :       return false;
    3713                 :             :     }
    3714                 :       69397 :   else if (gfc_do_concurrent_flag)
    3715                 :             :     {
    3716                 :           6 :       gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
    3717                 :             :                  "PURE", name, loc);
    3718                 :           6 :       return false;
    3719                 :             :     }
    3720                 :       69391 :   else if (gfc_pure (NULL))
    3721                 :             :     {
    3722                 :           4 :       gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
    3723                 :           4 :       return false;
    3724                 :             :     }
    3725                 :             : 
    3726                 :       69387 :   gfc_unset_implicit_pure (NULL);
    3727                 :       69387 :   return true;
    3728                 :             : }
    3729                 :             : 
    3730                 :             : 
    3731                 :             : static match
    3732                 :        2173 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3733                 :             : {
    3734                 :        2173 :   gfc_symbol *s;
    3735                 :             : 
    3736                 :        2173 :   if (sym->attr.generic)
    3737                 :             :     {
    3738                 :        2172 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3739                 :        2172 :       if (s != NULL)
    3740                 :             :         {
    3741                 :        2163 :           c->resolved_sym = s;
    3742                 :        2163 :           if (!pure_subroutine (s, s->name, &c->loc))
    3743                 :             :             return MATCH_ERROR;
    3744                 :        2163 :           return MATCH_YES;
    3745                 :             :         }
    3746                 :             : 
    3747                 :             :       /* TODO: Need to search for elemental references in generic interface.  */
    3748                 :             :     }
    3749                 :             : 
    3750                 :          10 :   if (sym->attr.intrinsic)
    3751                 :           1 :     return gfc_intrinsic_sub_interface (c, 0);
    3752                 :             : 
    3753                 :             :   return MATCH_NO;
    3754                 :             : }
    3755                 :             : 
    3756                 :             : 
    3757                 :             : static bool
    3758                 :        2171 : resolve_generic_s (gfc_code *c)
    3759                 :             : {
    3760                 :        2171 :   gfc_symbol *sym;
    3761                 :        2171 :   match m;
    3762                 :             : 
    3763                 :        2171 :   sym = c->symtree->n.sym;
    3764                 :             : 
    3765                 :        2173 :   for (;;)
    3766                 :             :     {
    3767                 :        2173 :       m = resolve_generic_s0 (c, sym);
    3768                 :        2173 :       if (m == MATCH_YES)
    3769                 :             :         return true;
    3770                 :           9 :       else if (m == MATCH_ERROR)
    3771                 :             :         return false;
    3772                 :             : 
    3773                 :           9 : generic:
    3774                 :           9 :       if (sym->ns->parent == NULL)
    3775                 :             :         break;
    3776                 :           3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3777                 :             : 
    3778                 :           3 :       if (sym == NULL)
    3779                 :             :         break;
    3780                 :           2 :       if (!generic_sym (sym))
    3781                 :           0 :         goto generic;
    3782                 :             :     }
    3783                 :             : 
    3784                 :             :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3785                 :             :      that possesses a matching interface.  14.1.2.4  */
    3786                 :           7 :   sym = c->symtree->n.sym;
    3787                 :             : 
    3788                 :           7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3789                 :             :     {
    3790                 :           4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3791                 :             :                  sym->name, &c->loc);
    3792                 :           4 :       return false;
    3793                 :             :     }
    3794                 :             : 
    3795                 :           3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3796                 :           3 :   if (m == MATCH_YES)
    3797                 :             :     return true;
    3798                 :           1 :   if (m == MATCH_NO)
    3799                 :           1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3800                 :             :                "intrinsic subroutine interface", sym->name, &c->loc);
    3801                 :             : 
    3802                 :             :   return false;
    3803                 :             : }
    3804                 :             : 
    3805                 :             : 
    3806                 :             : /* Resolve a subroutine call known to be specific.  */
    3807                 :             : 
    3808                 :             : static match
    3809                 :       61146 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3810                 :             : {
    3811                 :       61146 :   match m;
    3812                 :             : 
    3813                 :       61146 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3814                 :             :     {
    3815                 :        5528 :       if (sym->attr.dummy)
    3816                 :             :         {
    3817                 :         255 :           sym->attr.proc = PROC_DUMMY;
    3818                 :         255 :           goto found;
    3819                 :             :         }
    3820                 :             : 
    3821                 :        5273 :       sym->attr.proc = PROC_EXTERNAL;
    3822                 :        5273 :       goto found;
    3823                 :             :     }
    3824                 :             : 
    3825                 :       55618 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3826                 :       55618 :     goto found;
    3827                 :             : 
    3828                 :           0 :   if (sym->attr.intrinsic)
    3829                 :             :     {
    3830                 :           0 :       m = gfc_intrinsic_sub_interface (c, 1);
    3831                 :           0 :       if (m == MATCH_YES)
    3832                 :             :         return MATCH_YES;
    3833                 :           0 :       if (m == MATCH_NO)
    3834                 :           0 :         gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
    3835                 :             :                    "with an intrinsic", sym->name, &c->loc);
    3836                 :             : 
    3837                 :           0 :       return MATCH_ERROR;
    3838                 :             :     }
    3839                 :             : 
    3840                 :             :   return MATCH_NO;
    3841                 :             : 
    3842                 :       61146 : found:
    3843                 :       61146 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3844                 :             : 
    3845                 :       61146 :   c->resolved_sym = sym;
    3846                 :       61146 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3847                 :             :     return MATCH_ERROR;
    3848                 :             : 
    3849                 :             :   return MATCH_YES;
    3850                 :             : }
    3851                 :             : 
    3852                 :             : 
    3853                 :             : static bool
    3854                 :       61146 : resolve_specific_s (gfc_code *c)
    3855                 :             : {
    3856                 :       61146 :   gfc_symbol *sym;
    3857                 :       61146 :   match m;
    3858                 :             : 
    3859                 :       61146 :   sym = c->symtree->n.sym;
    3860                 :             : 
    3861                 :       61146 :   for (;;)
    3862                 :             :     {
    3863                 :       61146 :       m = resolve_specific_s0 (c, sym);
    3864                 :       61146 :       if (m == MATCH_YES)
    3865                 :             :         return true;
    3866                 :           7 :       if (m == MATCH_ERROR)
    3867                 :             :         return false;
    3868                 :             : 
    3869                 :           0 :       if (sym->ns->parent == NULL)
    3870                 :             :         break;
    3871                 :             : 
    3872                 :           0 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3873                 :             : 
    3874                 :           0 :       if (sym == NULL)
    3875                 :             :         break;
    3876                 :             :     }
    3877                 :             : 
    3878                 :           0 :   sym = c->symtree->n.sym;
    3879                 :           0 :   gfc_error ("Unable to resolve the specific subroutine %qs at %L",
    3880                 :             :              sym->name, &c->loc);
    3881                 :             : 
    3882                 :           0 :   return false;
    3883                 :             : }
    3884                 :             : 
    3885                 :             : 
    3886                 :             : /* Resolve a subroutine call not known to be generic nor specific.  */
    3887                 :             : 
    3888                 :             : static bool
    3889                 :       15175 : resolve_unknown_s (gfc_code *c)
    3890                 :             : {
    3891                 :       15175 :   gfc_symbol *sym;
    3892                 :             : 
    3893                 :       15175 :   sym = c->symtree->n.sym;
    3894                 :             : 
    3895                 :       15175 :   if (sym->attr.dummy)
    3896                 :             :     {
    3897                 :          20 :       sym->attr.proc = PROC_DUMMY;
    3898                 :          20 :       goto found;
    3899                 :             :     }
    3900                 :             : 
    3901                 :             :   /* See if we have an intrinsic function reference.  */
    3902                 :             : 
    3903                 :       15155 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3904                 :             :     {
    3905                 :        3811 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3906                 :             :         return true;
    3907                 :         294 :       return false;
    3908                 :             :     }
    3909                 :             : 
    3910                 :             :   /* The reference is to an external name.  */
    3911                 :             : 
    3912                 :       11344 : found:
    3913                 :       11364 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3914                 :             : 
    3915                 :       11364 :   c->resolved_sym = sym;
    3916                 :             : 
    3917                 :       11364 :   return pure_subroutine (sym, sym->name, &c->loc);
    3918                 :             : }
    3919                 :             : 
    3920                 :             : 
    3921                 :             : /* Resolve a subroutine call.  Although it was tempting to use the same code
    3922                 :             :    for functions, subroutines and functions are stored differently and this
    3923                 :             :    makes things awkward.  */
    3924                 :             : 
    3925                 :             : static bool
    3926                 :       78581 : resolve_call (gfc_code *c)
    3927                 :             : {
    3928                 :       78581 :   bool t;
    3929                 :       78581 :   procedure_type ptype = PROC_INTRINSIC;
    3930                 :       78581 :   gfc_symbol *csym, *sym;
    3931                 :       78581 :   bool no_formal_args;
    3932                 :             : 
    3933                 :       78581 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    3934                 :             : 
    3935                 :       78581 :   if (csym && csym->ts.type != BT_UNKNOWN)
    3936                 :             :     {
    3937                 :           4 :       gfc_error ("%qs at %L has a type, which is not consistent with "
    3938                 :             :                  "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
    3939                 :           4 :       return false;
    3940                 :             :     }
    3941                 :             : 
    3942                 :       78577 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    3943                 :             :     {
    3944                 :       16685 :       gfc_symtree *st;
    3945                 :       16685 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    3946                 :       16685 :       sym = st ? st->n.sym : NULL;
    3947                 :       16685 :       if (sym && csym != sym
    3948                 :           3 :               && sym->ns == gfc_current_ns
    3949                 :             :               && sym->attr.flavor == FL_PROCEDURE
    3950                 :           3 :               && sym->attr.contained)
    3951                 :             :         {
    3952                 :           3 :           sym->refs++;
    3953                 :           3 :           if (csym->attr.generic)
    3954                 :           2 :             c->symtree->n.sym = sym;
    3955                 :             :           else
    3956                 :           1 :             c->symtree = st;
    3957                 :           3 :           csym = c->symtree->n.sym;
    3958                 :             :         }
    3959                 :             :     }
    3960                 :             : 
    3961                 :             :   /* If this ia a deferred TBP, c->expr1 will be set.  */
    3962                 :       78577 :   if (!c->expr1 && csym)
    3963                 :             :     {
    3964                 :       76917 :       if (csym->attr.abstract)
    3965                 :             :         {
    3966                 :           1 :           gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3967                 :             :                     csym->name, &c->loc);
    3968                 :           1 :           return false;
    3969                 :             :         }
    3970                 :             : 
    3971                 :             :       /* Subroutines without the RECURSIVE attribution are not allowed to
    3972                 :             :          call themselves.  */
    3973                 :       76916 :       if (is_illegal_recursion (csym, gfc_current_ns))
    3974                 :             :         {
    3975                 :           4 :           if (csym->attr.entry && csym->ns->entries)
    3976                 :           2 :             gfc_error ("ENTRY %qs at %L cannot be called recursively, "
    3977                 :             :                        "as subroutine %qs is not RECURSIVE",
    3978                 :           2 :                        csym->name, &c->loc, csym->ns->entries->sym->name);
    3979                 :             :           else
    3980                 :           2 :             gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
    3981                 :             :                        "as it is not RECURSIVE", csym->name, &c->loc);
    3982                 :             : 
    3983                 :       78576 :           t = false;
    3984                 :             :         }
    3985                 :             :     }
    3986                 :             : 
    3987                 :             :   /* Switch off assumed size checking and do this again for certain kinds
    3988                 :             :      of procedure, once the procedure itself is resolved.  */
    3989                 :       78576 :   need_full_assumed_size++;
    3990                 :             : 
    3991                 :       78576 :   if (csym)
    3992                 :       78576 :     ptype = csym->attr.proc;
    3993                 :             : 
    3994                 :       78576 :   no_formal_args = csym && is_external_proc (csym)
    3995                 :       15388 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    3996                 :       78576 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    3997                 :             :     return false;
    3998                 :             : 
    3999                 :             :   /* Resume assumed_size checking.  */
    4000                 :       78543 :   need_full_assumed_size--;
    4001                 :             : 
    4002                 :             :   /* If external, check for usage.  */
    4003                 :       78543 :   if (csym && is_external_proc (csym))
    4004                 :       15383 :     resolve_global_procedure (csym, &c->loc, 1);
    4005                 :             : 
    4006                 :             :   /* If we have an external dummy argument, we want to write out its arguments
    4007                 :             :      with -fc-prototypes-external.  Code like
    4008                 :             : 
    4009                 :             :      subroutine foo(a,n)
    4010                 :             :        external a
    4011                 :             :        if (n == 1) call a(1)
    4012                 :             :        if (n == 2) call a(2,3)
    4013                 :             :      end subroutine foo
    4014                 :             : 
    4015                 :             :      is actually legal Fortran, but it is not possible to generate a C23-
    4016                 :             :      compliant prototype for this, so we just record the fact here and
    4017                 :             :      handle that during -fc-prototypes-external processing.  */
    4018                 :             : 
    4019                 :       78543 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4020                 :         499 :       && csym->attr.external)
    4021                 :             :     {
    4022                 :          13 :       if (csym->formal)
    4023                 :             :         {
    4024                 :           6 :           bool conflict;
    4025                 :           6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4026                 :             :                                                  0, 0, 0, NULL);
    4027                 :           6 :           if (conflict)
    4028                 :             :             {
    4029                 :           6 :               csym->ext_dummy_arglist_mismatch = 1;
    4030                 :           6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4031                 :             :                            "Different argument lists in external dummy "
    4032                 :             :                            "subroutine %s at %L and %L", csym->name,
    4033                 :             :                            &c->loc, &csym->formal_at);
    4034                 :             :             }
    4035                 :             :         }
    4036                 :             :       else
    4037                 :             :         {
    4038                 :           7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4039                 :           7 :           csym->formal_at = c->loc;
    4040                 :             :         }
    4041                 :             :     }
    4042                 :             : 
    4043                 :       78543 :   t = true;
    4044                 :       78543 :   if (c->resolved_sym == NULL)
    4045                 :             :     {
    4046                 :       78492 :       c->resolved_isym = NULL;
    4047                 :       78492 :       switch (procedure_kind (csym))
    4048                 :             :         {
    4049                 :        2171 :         case PTYPE_GENERIC:
    4050                 :        2171 :           t = resolve_generic_s (c);
    4051                 :        2171 :           break;
    4052                 :             : 
    4053                 :       61146 :         case PTYPE_SPECIFIC:
    4054                 :       61146 :           t = resolve_specific_s (c);
    4055                 :       61146 :           break;
    4056                 :             : 
    4057                 :       15175 :         case PTYPE_UNKNOWN:
    4058                 :       15175 :           t = resolve_unknown_s (c);
    4059                 :       15175 :           break;
    4060                 :             : 
    4061                 :             :         default:
    4062                 :             :           gfc_internal_error ("resolve_subroutine(): bad function type");
    4063                 :             :         }
    4064                 :             :     }
    4065                 :             : 
    4066                 :             :   /* Some checks of elemental subroutine actual arguments.  */
    4067                 :       78542 :   if (!resolve_elemental_actual (NULL, c))
    4068                 :             :     return false;
    4069                 :             : 
    4070                 :       78534 :   if (!c->expr1)
    4071                 :       76874 :     update_current_proc_array_outer_dependency (csym);
    4072                 :             :   else
    4073                 :             :     /* Typebound procedure: Assume the worst.  */
    4074                 :        1660 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4075                 :             : 
    4076                 :       78534 :   if (c->resolved_sym
    4077                 :       78236 :       && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    4078                 :           2 :     gfc_warning (OPT_Wdeprecated_declarations,
    4079                 :             :                  "Using subroutine %qs at %L is deprecated",
    4080                 :             :                  c->resolved_sym->name, &c->loc);
    4081                 :             : 
    4082                 :             :   return t;
    4083                 :             : }
    4084                 :             : 
    4085                 :             : 
    4086                 :             : /* Compare the shapes of two arrays that have non-NULL shapes.  If both
    4087                 :             :    op1->shape and op2->shape are non-NULL return true if their shapes
    4088                 :             :    match.  If both op1->shape and op2->shape are non-NULL return false
    4089                 :             :    if their shapes do not match.  If either op1->shape or op2->shape is
    4090                 :             :    NULL, return true.  */
    4091                 :             : 
    4092                 :             : static bool
    4093                 :       31092 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4094                 :             : {
    4095                 :       31092 :   bool t;
    4096                 :       31092 :   int i;
    4097                 :             : 
    4098                 :       31092 :   t = true;
    4099                 :             : 
    4100                 :       31092 :   if (op1->shape != NULL && op2->shape != NULL)
    4101                 :             :     {
    4102                 :       41986 :       for (i = 0; i < op1->rank; i++)
    4103                 :             :         {
    4104                 :       22417 :           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
    4105                 :             :            {
    4106                 :           3 :              gfc_error ("Shapes for operands at %L and %L are not conformable",
    4107                 :             :                         &op1->where, &op2->where);
    4108                 :           3 :              t = false;
    4109                 :           3 :              break;
    4110                 :             :            }
    4111                 :             :         }
    4112                 :             :     }
    4113                 :             : 
    4114                 :       31092 :   return t;
    4115                 :             : }
    4116                 :             : 
    4117                 :             : /* Convert a logical operator to the corresponding bitwise intrinsic call.
    4118                 :             :    For example A .AND. B becomes IAND(A, B).  */
    4119                 :             : static gfc_expr *
    4120                 :         668 : logical_to_bitwise (gfc_expr *e)
    4121                 :             : {
    4122                 :         668 :   gfc_expr *tmp, *op1, *op2;
    4123                 :         668 :   gfc_isym_id isym;
    4124                 :         668 :   gfc_actual_arglist *args = NULL;
    4125                 :             : 
    4126                 :         668 :   gcc_assert (e->expr_type == EXPR_OP);
    4127                 :             : 
    4128                 :         668 :   isym = GFC_ISYM_NONE;
    4129                 :         668 :   op1 = e->value.op.op1;
    4130                 :         668 :   op2 = e->value.op.op2;
    4131                 :             : 
    4132                 :         668 :   switch (e->value.op.op)
    4133                 :             :     {
    4134                 :             :     case INTRINSIC_NOT:
    4135                 :             :       isym = GFC_ISYM_NOT;
    4136                 :             :       break;
    4137                 :         126 :     case INTRINSIC_AND:
    4138                 :         126 :       isym = GFC_ISYM_IAND;
    4139                 :         126 :       break;
    4140                 :         127 :     case INTRINSIC_OR:
    4141                 :         127 :       isym = GFC_ISYM_IOR;
    4142                 :         127 :       break;
    4143                 :         270 :     case INTRINSIC_NEQV:
    4144                 :         270 :       isym = GFC_ISYM_IEOR;
    4145                 :         270 :       break;
    4146                 :         126 :     case INTRINSIC_EQV:
    4147                 :             :       /* "Bitwise eqv" is just the complement of NEQV === IEOR.
    4148                 :             :          Change the old expression to NEQV, which will get replaced by IEOR,
    4149                 :             :          and wrap it in NOT.  */
    4150                 :         126 :       tmp = gfc_copy_expr (e);
    4151                 :         126 :       tmp->value.op.op = INTRINSIC_NEQV;
    4152                 :         126 :       tmp = logical_to_bitwise (tmp);
    4153                 :         126 :       isym = GFC_ISYM_NOT;
    4154                 :         126 :       op1 = tmp;
    4155                 :         126 :       op2 = NULL;
    4156                 :         126 :       break;
    4157                 :           0 :     default:
    4158                 :           0 :       gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
    4159                 :             :     }
    4160                 :             : 
    4161                 :             :   /* Inherit the original operation's operands as arguments.  */
    4162                 :         668 :   args = gfc_get_actual_arglist ();
    4163                 :         668 :   args->expr = op1;
    4164                 :         668 :   if (op2)
    4165                 :             :     {
    4166                 :         523 :       args->next = gfc_get_actual_arglist ();
    4167                 :         523 :       args->next->expr = op2;
    4168                 :             :     }
    4169                 :             : 
    4170                 :             :   /* Convert the expression to a function call.  */
    4171                 :         668 :   e->expr_type = EXPR_FUNCTION;
    4172                 :         668 :   e->value.function.actual = args;
    4173                 :         668 :   e->value.function.isym = gfc_intrinsic_function_by_id (isym);
    4174                 :         668 :   e->value.function.name = e->value.function.isym->name;
    4175                 :         668 :   e->value.function.esym = NULL;
    4176                 :             : 
    4177                 :             :   /* Make up a pre-resolved function call symtree if we need to.  */
    4178                 :         668 :   if (!e->symtree || !e->symtree->n.sym)
    4179                 :             :     {
    4180                 :         668 :       gfc_symbol *sym;
    4181                 :         668 :       gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
    4182                 :         668 :       sym = e->symtree->n.sym;
    4183                 :         668 :       sym->result = sym;
    4184                 :         668 :       sym->attr.flavor = FL_PROCEDURE;
    4185                 :         668 :       sym->attr.function = 1;
    4186                 :         668 :       sym->attr.elemental = 1;
    4187                 :         668 :       sym->attr.pure = 1;
    4188                 :         668 :       sym->attr.referenced = 1;
    4189                 :         668 :       gfc_intrinsic_symbol (sym);
    4190                 :         668 :       gfc_commit_symbol (sym);
    4191                 :             :     }
    4192                 :             : 
    4193                 :         668 :   args->name = e->value.function.isym->formal->name;
    4194                 :         668 :   if (e->value.function.isym->formal->next)
    4195                 :         523 :     args->next->name = e->value.function.isym->formal->next->name;
    4196                 :             : 
    4197                 :         668 :   return e;
    4198                 :             : }
    4199                 :             : 
    4200                 :             : /* Recursively append candidate UOP to CANDIDATES.  Store the number of
    4201                 :             :    candidates in CANDIDATES_LEN.  */
    4202                 :             : static void
    4203                 :          43 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4204                 :             :                                   char **&candidates,
    4205                 :             :                                   size_t &candidates_len)
    4206                 :             : {
    4207                 :          45 :   gfc_symtree *p;
    4208                 :             : 
    4209                 :          45 :   if (uop == NULL)
    4210                 :             :     return;
    4211                 :             : 
    4212                 :             :   /* Not sure how to properly filter here.  Use all for a start.
    4213                 :             :      n.uop.op is NULL for empty interface operators (is that legal?) disregard
    4214                 :             :      these as i suppose they don't make terribly sense.  */
    4215                 :             : 
    4216                 :          45 :   if (uop->n.uop->op != NULL)
    4217                 :           2 :     vec_push (candidates, candidates_len, uop->name);
    4218                 :             : 
    4219                 :          45 :   p = uop->left;
    4220                 :          45 :   if (p)
    4221                 :           0 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4222                 :             : 
    4223                 :          45 :   p = uop->right;
    4224                 :          45 :   if (p)
    4225                 :             :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4226                 :             : }
    4227                 :             : 
    4228                 :             : /* Lookup user-operator OP fuzzily, taking names in UOP into account.  */
    4229                 :             : 
    4230                 :             : static const char*
    4231                 :          43 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4232                 :             : {
    4233                 :          43 :   char **candidates = NULL;
    4234                 :          43 :   size_t candidates_len = 0;
    4235                 :          43 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4236                 :          43 :   return gfc_closest_fuzzy_match (op, candidates);
    4237                 :             : }
    4238                 :             : 
    4239                 :             : 
    4240                 :             : /* Callback finding an impure function as an operand to an .and. or
    4241                 :             :    .or.  expression.  Remember the last function warned about to
    4242                 :             :    avoid double warnings when recursing.  */
    4243                 :             : 
    4244                 :             : static int
    4245                 :      190833 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4246                 :             :                           void *data)
    4247                 :             : {
    4248                 :      190833 :   gfc_expr *f = *e;
    4249                 :      190833 :   const char *name;
    4250                 :      190833 :   static gfc_expr *last = NULL;
    4251                 :      190833 :   bool *found = (bool *) data;
    4252                 :             : 
    4253                 :      190833 :   if (f->expr_type == EXPR_FUNCTION)
    4254                 :             :     {
    4255                 :       11707 :       *found = 1;
    4256                 :       11707 :       if (f != last && !gfc_pure_function (f, &name)
    4257                 :       12982 :           && !gfc_implicit_pure_function (f))
    4258                 :             :         {
    4259                 :        1136 :           if (name)
    4260                 :        1136 :             gfc_warning (OPT_Wfunction_elimination,
    4261                 :             :                          "Impure function %qs at %L might not be evaluated",
    4262                 :             :                          name, &f->where);
    4263                 :             :           else
    4264                 :           0 :             gfc_warning (OPT_Wfunction_elimination,
    4265                 :             :                          "Impure function at %L might not be evaluated",
    4266                 :             :                          &f->where);
    4267                 :             :         }
    4268                 :       11707 :       last = f;
    4269                 :             :     }
    4270                 :             : 
    4271                 :      190833 :   return 0;
    4272                 :             : }
    4273                 :             : 
    4274                 :             : /* Return true if TYPE is character based, false otherwise.  */
    4275                 :             : 
    4276                 :             : static int
    4277                 :        1373 : is_character_based (bt type)
    4278                 :             : {
    4279                 :        1373 :   return type == BT_CHARACTER || type == BT_HOLLERITH;
    4280                 :             : }
    4281                 :             : 
    4282                 :             : 
    4283                 :             : /* If expression is a hollerith, convert it to character and issue a warning
    4284                 :             :    for the conversion.  */
    4285                 :             : 
    4286                 :             : static void
    4287                 :         408 : convert_hollerith_to_character (gfc_expr *e)
    4288                 :             : {
    4289                 :         408 :   if (e->ts.type == BT_HOLLERITH)
    4290                 :             :     {
    4291                 :         108 :       gfc_typespec t;
    4292                 :         108 :       gfc_clear_ts (&t);
    4293                 :         108 :       t.type = BT_CHARACTER;
    4294                 :         108 :       t.kind = e->ts.kind;
    4295                 :         108 :       gfc_convert_type_warn (e, &t, 2, 1);
    4296                 :             :     }
    4297                 :         408 : }
    4298                 :             : 
    4299                 :             : /* Convert to numeric and issue a warning for the conversion.  */
    4300                 :             : 
    4301                 :             : static void
    4302                 :         240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
    4303                 :             : {
    4304                 :         240 :   gfc_typespec t;
    4305                 :         240 :   gfc_clear_ts (&t);
    4306                 :         240 :   t.type = b->ts.type;
    4307                 :         240 :   t.kind = b->ts.kind;
    4308                 :         240 :   gfc_convert_type_warn (a, &t, 2, 1);
    4309                 :         240 : }
    4310                 :             : 
    4311                 :             : /* Resolve an operator expression node.  This can involve replacing the
    4312                 :             :    operation with a user defined function call.  CHECK_INTERFACES is a
    4313                 :             :    helper macro.  */
    4314                 :             : 
    4315                 :             : #define CHECK_INTERFACES \
    4316                 :             :   { \
    4317                 :             :     match m = gfc_extend_expr (e); \
    4318                 :             :     if (m == MATCH_YES) \
    4319                 :             :       return true; \
    4320                 :             :     if (m == MATCH_ERROR) \
    4321                 :             :       return false; \
    4322                 :             :   }
    4323                 :             : 
    4324                 :             : static bool
    4325                 :      511582 : resolve_operator (gfc_expr *e)
    4326                 :             : {
    4327                 :      511582 :   gfc_expr *op1, *op2;
    4328                 :             :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4329                 :      511582 :   bool t = true;
    4330                 :             : 
    4331                 :             :   /* Reduce stacked parentheses to single pair  */
    4332                 :      511582 :   while (e->expr_type == EXPR_OP
    4333                 :      511692 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4334                 :       20447 :          && e->value.op.op1->expr_type == EXPR_OP
    4335                 :      527890 :          && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
    4336                 :             :     {
    4337                 :         110 :       gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
    4338                 :         110 :       gfc_replace_expr (e, tmp);
    4339                 :             :     }
    4340                 :             : 
    4341                 :             :   /* Resolve all subnodes-- give them types.  */
    4342                 :             : 
    4343                 :      511582 :   switch (e->value.op.op)
    4344                 :             :     {
    4345                 :      463250 :     default:
    4346                 :      463250 :       if (!gfc_resolve_expr (e->value.op.op2))
    4347                 :      511582 :         t = false;
    4348                 :             : 
    4349                 :             :     /* Fall through.  */
    4350                 :             : 
    4351                 :      511582 :     case INTRINSIC_NOT:
    4352                 :      511582 :     case INTRINSIC_UPLUS:
    4353                 :      511582 :     case INTRINSIC_UMINUS:
    4354                 :      511582 :     case INTRINSIC_PARENTHESES:
    4355                 :      511582 :       if (!gfc_resolve_expr (e->value.op.op1))
    4356                 :             :         return false;
    4357                 :      511415 :       if (e->value.op.op1
    4358                 :      511406 :           && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
    4359                 :             :         {
    4360                 :           0 :           gfc_error ("BOZ literal constant at %L cannot be an operand of "
    4361                 :           0 :                      "unary operator %qs", &e->value.op.op1->where,
    4362                 :             :                      gfc_op2string (e->value.op.op));
    4363                 :           0 :           return false;
    4364                 :             :         }
    4365                 :      511415 :       if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
    4366                 :           6 :           && e->value.op.op == INTRINSIC_UMINUS)
    4367                 :             :         {
    4368                 :           2 :           gfc_error ("Negation of unsigned expression at %L not permitted ",
    4369                 :             :                      &e->value.op.op1->where);
    4370                 :           2 :           return false;
    4371                 :             :         }
    4372                 :      511413 :       break;
    4373                 :             :     }
    4374                 :             : 
    4375                 :             :   /* Typecheck the new node.  */
    4376                 :             : 
    4377                 :      511413 :   op1 = e->value.op.op1;
    4378                 :      511413 :   op2 = e->value.op.op2;
    4379                 :      511413 :   if (op1 == NULL && op2 == NULL)
    4380                 :             :     return false;
    4381                 :             :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4382                 :      511404 :   if (t == false)
    4383                 :             :     return false;
    4384                 :             : 
    4385                 :             :   /* op1 and op2 cannot both be BOZ.  */
    4386                 :      511342 :   if (op1 && op1->ts.type == BT_BOZ
    4387                 :           0 :       && op2 && op2->ts.type == BT_BOZ)
    4388                 :             :     {
    4389                 :           0 :       gfc_error ("Operands at %L and %L cannot appear as operands of "
    4390                 :           0 :                  "binary operator %qs", &op1->where, &op2->where,
    4391                 :             :                  gfc_op2string (e->value.op.op));
    4392                 :           0 :       return false;
    4393                 :             :     }
    4394                 :             : 
    4395                 :      511342 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4396                 :      511340 :       || (op2 && op2->expr_type == EXPR_NULL))
    4397                 :             :     {
    4398                 :           3 :       CHECK_INTERFACES
    4399                 :           3 :       gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
    4400                 :           3 :       return false;
    4401                 :             :     }
    4402                 :             : 
    4403                 :      511339 :   switch (e->value.op.op)
    4404                 :             :     {
    4405                 :        7922 :     case INTRINSIC_UPLUS:
    4406                 :        7922 :     case INTRINSIC_UMINUS:
    4407                 :        7922 :       if (op1->ts.type == BT_INTEGER
    4408                 :             :           || op1->ts.type == BT_REAL
    4409                 :             :           || op1->ts.type == BT_COMPLEX
    4410                 :             :           || op1->ts.type == BT_UNSIGNED)
    4411                 :             :         {
    4412                 :        7853 :           e->ts = op1->ts;
    4413                 :        7853 :           break;
    4414                 :             :         }
    4415                 :             : 
    4416                 :          69 :       CHECK_INTERFACES
    4417                 :          43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4418                 :             :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4419                 :          43 :       return false;
    4420                 :             : 
    4421                 :      150139 :     case INTRINSIC_POWER:
    4422                 :      150139 :     case INTRINSIC_PLUS:
    4423                 :      150139 :     case INTRINSIC_MINUS:
    4424                 :      150139 :     case INTRINSIC_TIMES:
    4425                 :      150139 :     case INTRINSIC_DIVIDE:
    4426                 :             : 
    4427                 :             :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4428                 :             :              conversion.  */
    4429                 :      150139 :       if (flag_unsigned &&  gfc_invalid_unsigned_ops (op1, op2))
    4430                 :             :         {
    4431                 :           3 :           CHECK_INTERFACES
    4432                 :           3 :           gfc_error ("Operands of binary numeric operator %qs at %L are "
    4433                 :             :                      "%s/%s", gfc_op2string (e->value.op.op), &e->where,
    4434                 :             :                      gfc_typename (op1), gfc_typename (op2));
    4435                 :           3 :           return false;
    4436                 :             :         }
    4437                 :             : 
    4438                 :      150136 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4439                 :             :         {
    4440                 :             :           /* Do not perform conversions if operands are not conformable as
    4441                 :             :              required for the binary intrinsic operators (F2018:10.1.5).
    4442                 :             :              Defer to a possibly overloading user-defined operator.  */
    4443                 :      149692 :           if (!gfc_op_rank_conformable (op1, op2))
    4444                 :             :             {
    4445                 :          36 :               CHECK_INTERFACES
    4446                 :           0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4447                 :           0 :                          &op1->where, &op2->where);
    4448                 :           0 :               return false;
    4449                 :             :             }
    4450                 :             : 
    4451                 :      149656 :           gfc_type_convert_binary (e, 1);
    4452                 :      149656 :           break;
    4453                 :             :         }
    4454                 :             : 
    4455                 :         444 :       if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
    4456                 :             :         {
    4457                 :         215 :           CHECK_INTERFACES
    4458                 :           2 :           gfc_error ("Unexpected derived-type entities in binary intrinsic "
    4459                 :             :                      "numeric operator %qs at %L",
    4460                 :             :                      gfc_op2string (e->value.op.op), &e->where);
    4461                 :           2 :           return false;
    4462                 :             :         }
    4463                 :             :       else
    4464                 :             :         {
    4465                 :         229 :           CHECK_INTERFACES
    4466                 :           3 :           gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
    4467                 :             :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4468                 :             :                      gfc_typename (op2));
    4469                 :           3 :           return false;
    4470                 :             :         }
    4471                 :             : 
    4472                 :        2112 :     case INTRINSIC_CONCAT:
    4473                 :        2112 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4474                 :        2087 :           && op1->ts.kind == op2->ts.kind)
    4475                 :             :         {
    4476                 :        2078 :           e->ts.type = BT_CHARACTER;
    4477                 :        2078 :           e->ts.kind = op1->ts.kind;
    4478                 :        2078 :           break;
    4479                 :             :         }
    4480                 :             : 
    4481                 :          34 :       CHECK_INTERFACES
    4482                 :          10 :       gfc_error ("Operands of string concatenation operator at %L are %s/%s",
    4483                 :             :                  &e->where, gfc_typename (op1), gfc_typename (op2));
    4484                 :          10 :       return false;
    4485                 :             : 
    4486                 :       68992 :     case INTRINSIC_AND:
    4487                 :       68992 :     case INTRINSIC_OR:
    4488                 :       68992 :     case INTRINSIC_EQV:
    4489                 :       68992 :     case INTRINSIC_NEQV:
    4490                 :       68992 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4491                 :             :         {
    4492                 :       68441 :           e->ts.type = BT_LOGICAL;
    4493                 :       68441 :           e->ts.kind = gfc_kind_max (op1, op2);
    4494                 :       68441 :           if (op1->ts.kind < e->ts.kind)
    4495                 :         138 :             gfc_convert_type (op1, &e->ts, 2);
    4496                 :       68303 :           else if (op2->ts.kind < e->ts.kind)
    4497                 :         117 :             gfc_convert_type (op2, &e->ts, 2);
    4498                 :             : 
    4499                 :       68441 :           if (flag_frontend_optimize &&
    4500                 :       57430 :             (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
    4501                 :             :             {
    4502                 :             :               /* Warn about short-circuiting
    4503                 :             :                  with impure function as second operand.  */
    4504                 :       51464 :               bool op2_f = false;
    4505                 :       51464 :               gfc_expr_walker (&op2, impure_function_callback, &op2_f);
    4506                 :             :             }
    4507                 :             :           break;
    4508                 :             :         }
    4509                 :             : 
    4510                 :             :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4511                 :         551 :       else if (flag_dec
    4512                 :         523 :                && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
    4513                 :             :         {
    4514                 :         523 :           e->ts.type = BT_INTEGER;
    4515                 :         523 :           e->ts.kind = gfc_kind_max (op1, op2);
    4516                 :         523 :           if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
    4517                 :         289 :             gfc_convert_type (op1, &e->ts, 1);
    4518                 :         523 :           if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
    4519                 :         144 :             gfc_convert_type (op2, &e->ts, 1);
    4520                 :         523 :           e = logical_to_bitwise (e);
    4521                 :         523 :           goto simplify_op;
    4522                 :             :         }
    4523                 :             : 
    4524                 :          28 :       CHECK_INTERFACES
    4525                 :          16 :       gfc_error ("Operands of logical operator %qs at %L are %s/%s",
    4526                 :             :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4527                 :             :                  gfc_typename (op2));
    4528                 :          16 :       return false;
    4529                 :             : 
    4530                 :       20057 :     case INTRINSIC_NOT:
    4531                 :             :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4532                 :       20057 :       if (flag_dec && op1->ts.type == BT_INTEGER)
    4533                 :             :         {
    4534                 :          19 :           e->ts.type = BT_INTEGER;
    4535                 :          19 :           e->ts.kind = op1->ts.kind;
    4536                 :          19 :           e = logical_to_bitwise (e);
    4537                 :          19 :           goto simplify_op;
    4538                 :             :         }
    4539                 :             : 
    4540                 :       20038 :       if (op1->ts.type == BT_LOGICAL)
    4541                 :             :         {
    4542                 :       20032 :           e->ts.type = BT_LOGICAL;
    4543                 :       20032 :           e->ts.kind = op1->ts.kind;
    4544                 :       20032 :           break;
    4545                 :             :         }
    4546                 :             : 
    4547                 :           6 :       CHECK_INTERFACES
    4548                 :           3 :       gfc_error ("Operand of .not. operator at %L is %s", &e->where,
    4549                 :             :                  gfc_typename (op1));
    4550                 :           3 :       return false;
    4551                 :             : 
    4552                 :       20823 :     case INTRINSIC_GT:
    4553                 :       20823 :     case INTRINSIC_GT_OS:
    4554                 :       20823 :     case INTRINSIC_GE:
    4555                 :       20823 :     case INTRINSIC_GE_OS:
    4556                 :       20823 :     case INTRINSIC_LT:
    4557                 :       20823 :     case INTRINSIC_LT_OS:
    4558                 :       20823 :     case INTRINSIC_LE:
    4559                 :       20823 :     case INTRINSIC_LE_OS:
    4560                 :       20823 :       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
    4561                 :             :         {
    4562                 :          18 :           CHECK_INTERFACES
    4563                 :           0 :           gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
    4564                 :           0 :           return false;
    4565                 :             :         }
    4566                 :             : 
    4567                 :             :       /* Fall through.  */
    4568                 :             : 
    4569                 :      241576 :     case INTRINSIC_EQ:
    4570                 :      241576 :     case INTRINSIC_EQ_OS:
    4571                 :      241576 :     case INTRINSIC_NE:
    4572                 :      241576 :     case INTRINSIC_NE_OS:
    4573                 :             : 
    4574                 :      241576 :       if (flag_dec
    4575                 :        1038 :           && is_character_based (op1->ts.type)
    4576                 :      241911 :           && is_character_based (op2->ts.type))
    4577                 :             :         {
    4578                 :         204 :           convert_hollerith_to_character (op1);
    4579                 :         204 :           convert_hollerith_to_character (op2);
    4580                 :             :         }
    4581                 :             : 
    4582                 :      241576 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4583                 :       36322 :           && op1->ts.kind == op2->ts.kind)
    4584                 :             :         {
    4585                 :       36285 :           e->ts.type = BT_LOGICAL;
    4586                 :       36285 :           e->ts.kind = gfc_default_logical_kind;
    4587                 :       36285 :           break;
    4588                 :             :         }
    4589                 :             : 
    4590                 :             :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4591                 :      205291 :       if (op1->ts.type == BT_BOZ)
    4592                 :             :         {
    4593                 :           0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
    4594                 :             :                                "as an operand of a relational operator"),
    4595                 :             :                                &op1->where))
    4596                 :             :             return false;
    4597                 :             : 
    4598                 :           0 :           if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
    4599                 :             :             return false;
    4600                 :             : 
    4601                 :           0 :           if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
    4602                 :             :             return false;
    4603                 :             :         }
    4604                 :             : 
    4605                 :             :       /* If op2 is BOZ, then op1 is not!.  Try to convert to type of op2. */
    4606                 :      205291 :       if (op2->ts.type == BT_BOZ)
    4607                 :             :         {
    4608                 :           0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
    4609                 :             :                                " as an operand of a relational operator"),
    4610                 :             :                                 &op2->where))
    4611                 :             :             return false;
    4612                 :             : 
    4613                 :           0 :           if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
    4614                 :             :             return false;
    4615                 :             : 
    4616                 :           0 :           if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
    4617                 :             :             return false;
    4618                 :             :         }
    4619                 :      205291 :       if (flag_dec
    4620                 :      205291 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4621                 :         120 :         convert_to_numeric (op1, op2);
    4622                 :             : 
    4623                 :      205291 :       if (flag_dec
    4624                 :      205291 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4625                 :         120 :         convert_to_numeric (op2, op1);
    4626                 :             : 
    4627                 :      205291 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4628                 :             :         {
    4629                 :             :           /* Do not perform conversions if operands are not conformable as
    4630                 :             :              required for the binary intrinsic operators (F2018:10.1.5).
    4631                 :             :              Defer to a possibly overloading user-defined operator.  */
    4632                 :      204166 :           if (!gfc_op_rank_conformable (op1, op2))
    4633                 :             :             {
    4634                 :          70 :               CHECK_INTERFACES
    4635                 :           0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4636                 :           0 :                          &op1->where, &op2->where);
    4637                 :           0 :               return false;
    4638                 :             :             }
    4639                 :             : 
    4640                 :      204096 :           if (flag_unsigned  && gfc_invalid_unsigned_ops (op1, op2))
    4641                 :             :             {
    4642                 :           1 :               CHECK_INTERFACES
    4643                 :           1 :               gfc_error ("Inconsistent types for operator at %L and %L: "
    4644                 :           1 :                          "%s and %s", &op1->where, &op2->where,
    4645                 :             :                          gfc_typename (op1), gfc_typename (op2));
    4646                 :           1 :               return false;
    4647                 :             :             }
    4648                 :             : 
    4649                 :      204095 :           gfc_type_convert_binary (e, 1);
    4650                 :             : 
    4651                 :      204095 :           e->ts.type = BT_LOGICAL;
    4652                 :      204095 :           e->ts.kind = gfc_default_logical_kind;
    4653                 :             : 
    4654                 :      204095 :           if (warn_compare_reals)
    4655                 :             :             {
    4656                 :          69 :               gfc_intrinsic_op op = e->value.op.op;
    4657                 :             : 
    4658                 :             :               /* Type conversion has made sure that the types of op1 and op2
    4659                 :             :                  agree, so it is only necessary to check the first one.   */
    4660                 :          69 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4661                 :          13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4662                 :           6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4663                 :             :                 {
    4664                 :          13 :                   const char *msg;
    4665                 :             : 
    4666                 :          13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4667                 :             :                     msg = G_("Equality comparison for %s at %L");
    4668                 :             :                   else
    4669                 :           6 :                     msg = G_("Inequality comparison for %s at %L");
    4670                 :             : 
    4671                 :          13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4672                 :             :                                gfc_typename (op1), &op1->where);
    4673                 :             :                 }
    4674                 :             :             }
    4675                 :             : 
    4676                 :             :           break;
    4677                 :             :         }
    4678                 :             : 
    4679                 :        1125 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4680                 :             :         {
    4681                 :           2 :           CHECK_INTERFACES
    4682                 :           4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4683                 :             :                      &e->where,
    4684                 :           2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4685                 :             :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4686                 :           2 :         }
    4687                 :             :       else
    4688                 :             :         {
    4689                 :        1123 :           CHECK_INTERFACES
    4690                 :         113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4691                 :             :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4692                 :             :                      gfc_typename (op2));
    4693                 :             :         }
    4694                 :             : 
    4695                 :             :       return false;
    4696                 :             : 
    4697                 :         232 :     case INTRINSIC_USER:
    4698                 :         232 :       if (e->value.op.uop->op == NULL)
    4699                 :             :         {
    4700                 :          43 :           const char *name = e->value.op.uop->name;
    4701                 :          43 :           const char *guessed;
    4702                 :          43 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4703                 :          43 :           CHECK_INTERFACES
    4704                 :           4 :           if (guessed)
    4705                 :           1 :             gfc_error ("Unknown operator %qs at %L; did you mean "
    4706                 :             :                         "%qs?", name, &e->where, guessed);
    4707                 :             :           else
    4708                 :           3 :             gfc_error ("Unknown operator %qs at %L", name, &e->where);
    4709                 :             :         }
    4710                 :         189 :       else if (op2 == NULL)
    4711                 :             :         {
    4712                 :          48 :           CHECK_INTERFACES
    4713                 :           0 :           gfc_error ("Operand of user operator %qs at %L is %s",
    4714                 :           0 :                   e->value.op.uop->name, &e->where, gfc_typename (op1));
    4715                 :             :         }
    4716                 :             :       else
    4717                 :             :         {
    4718                 :         141 :           e->value.op.uop->op->sym->attr.referenced = 1;
    4719                 :         141 :           CHECK_INTERFACES
    4720                 :           5 :           gfc_error ("Operands of user operator %qs at %L are %s/%s",
    4721                 :           5 :                     e->value.op.uop->name, &e->where, gfc_typename (op1),
    4722                 :             :                     gfc_typename (op2));
    4723                 :             :         }
    4724                 :             : 
    4725                 :             :       return false;
    4726                 :             : 
    4727                 :       20291 :     case INTRINSIC_PARENTHESES:
    4728                 :       20291 :       e->ts = op1->ts;
    4729                 :       20291 :       if (e->ts.type == BT_CHARACTER)
    4730                 :         297 :         e->ts.u.cl = op1->ts.u.cl;
    4731                 :             :       break;
    4732                 :             : 
    4733                 :           0 :     default:
    4734                 :           0 :       gfc_internal_error ("resolve_operator(): Bad intrinsic");
    4735                 :             :     }
    4736                 :             : 
    4737                 :             :   /* Deal with arrayness of an operand through an operator.  */
    4738                 :             : 
    4739                 :      508731 :   switch (e->value.op.op)
    4740                 :             :     {
    4741                 :      460555 :     case INTRINSIC_PLUS:
    4742                 :      460555 :     case INTRINSIC_MINUS:
    4743                 :      460555 :     case INTRINSIC_TIMES:
    4744                 :      460555 :     case INTRINSIC_DIVIDE:
    4745                 :      460555 :     case INTRINSIC_POWER:
    4746                 :      460555 :     case INTRINSIC_CONCAT:
    4747                 :      460555 :     case INTRINSIC_AND:
    4748                 :      460555 :     case INTRINSIC_OR:
    4749                 :      460555 :     case INTRINSIC_EQV:
    4750                 :      460555 :     case INTRINSIC_NEQV:
    4751                 :      460555 :     case INTRINSIC_EQ:
    4752                 :      460555 :     case INTRINSIC_EQ_OS:
    4753                 :      460555 :     case INTRINSIC_NE:
    4754                 :      460555 :     case INTRINSIC_NE_OS:
    4755                 :      460555 :     case INTRINSIC_GT:
    4756                 :      460555 :     case INTRINSIC_GT_OS:
    4757                 :      460555 :     case INTRINSIC_GE:
    4758                 :      460555 :     case INTRINSIC_GE_OS:
    4759                 :      460555 :     case INTRINSIC_LT:
    4760                 :      460555 :     case INTRINSIC_LT_OS:
    4761                 :      460555 :     case INTRINSIC_LE:
    4762                 :      460555 :     case INTRINSIC_LE_OS:
    4763                 :             : 
    4764                 :      460555 :       if (op1->rank == 0 && op2->rank == 0)
    4765                 :      412955 :         e->rank = 0;
    4766                 :             : 
    4767                 :      460555 :       if (op1->rank == 0 && op2->rank != 0)
    4768                 :             :         {
    4769                 :        2396 :           e->rank = op2->rank;
    4770                 :             : 
    4771                 :        2396 :           if (e->shape == NULL)
    4772                 :        2366 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    4773                 :             :         }
    4774                 :             : 
    4775                 :      460555 :       if (op1->rank != 0 && op2->rank == 0)
    4776                 :             :         {
    4777                 :       14051 :           e->rank = op1->rank;
    4778                 :             : 
    4779                 :       14051 :           if (e->shape == NULL)
    4780                 :       14033 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    4781                 :             :         }
    4782                 :             : 
    4783                 :      460555 :       if (op1->rank != 0 && op2->rank != 0)
    4784                 :             :         {
    4785                 :       31153 :           if (op1->rank == op2->rank)
    4786                 :             :             {
    4787                 :       31153 :               e->rank = op1->rank;
    4788                 :       31153 :               if (e->shape == NULL)
    4789                 :             :                 {
    4790                 :       31092 :                   t = compare_shapes (op1, op2);
    4791                 :       31092 :                   if (!t)
    4792                 :           3 :                     e->shape = NULL;
    4793                 :             :                   else
    4794                 :       31089 :                     e->shape = gfc_copy_shape (op1->shape, op1->rank);
    4795                 :             :                 }
    4796                 :             :             }
    4797                 :             :           else
    4798                 :             :             {
    4799                 :             :               /* Allow higher level expressions to work.  */
    4800                 :           0 :               e->rank = 0;
    4801                 :             : 
    4802                 :             :               /* Try user-defined operators, and otherwise throw an error.  */
    4803                 :           0 :               CHECK_INTERFACES
    4804                 :           0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4805                 :           0 :                          &op1->where, &op2->where);
    4806                 :           0 :               return false;
    4807                 :             :             }
    4808                 :             :         }
    4809                 :             : 
    4810                 :             :       /* coranks have to be equal or one has to be zero to be combinable.  */
    4811                 :      460555 :       if (op1->corank == op2->corank || (op1->corank != 0 && op2->corank == 0))
    4812                 :             :         {
    4813                 :      460444 :           e->corank = op1->corank;
    4814                 :             :           /* Only do this, when regular array has not set a shape yet.  */
    4815                 :      460444 :           if (e->shape == NULL)
    4816                 :             :             {
    4817                 :      431940 :               if (op1->corank != 0)
    4818                 :             :                 {
    4819                 :        1398 :                   e->shape = gfc_copy_shape (op1->shape, op1->corank);
    4820                 :             :                 }
    4821                 :             :             }
    4822                 :             :         }
    4823                 :         111 :       else if (op1->corank == 0 && op2->corank != 0)
    4824                 :             :         {
    4825                 :         111 :           e->corank = op2->corank;
    4826                 :             :           /* Only do this, when regular array has not set a shape yet.  */
    4827                 :         111 :           if (e->shape == NULL)
    4828                 :          83 :             e->shape = gfc_copy_shape (op2->shape, op2->corank);
    4829                 :             :         }
    4830                 :             :       else
    4831                 :             :         {
    4832                 :           0 :           gfc_error ("Inconsistent coranks for operator at %L and %L",
    4833                 :             :                      &op1->where, &op2->where);
    4834                 :           0 :           return false;
    4835                 :             :         }
    4836                 :             : 
    4837                 :             :       break;
    4838                 :             : 
    4839                 :       48176 :     case INTRINSIC_PARENTHESES:
    4840                 :       48176 :     case INTRINSIC_NOT:
    4841                 :       48176 :     case INTRINSIC_UPLUS:
    4842                 :       48176 :     case INTRINSIC_UMINUS:
    4843                 :             :       /* Simply copy arrayness attribute */
    4844                 :       48176 :       e->rank = op1->rank;
    4845                 :       48176 :       e->corank = op1->corank;
    4846                 :             : 
    4847                 :       48176 :       if (e->shape == NULL)
    4848                 :       48170 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    4849                 :             : 
    4850                 :             :       break;
    4851                 :             : 
    4852                 :             :     default:
    4853                 :             :       break;
    4854                 :             :     }
    4855                 :             : 
    4856                 :      509273 : simplify_op:
    4857                 :             : 
    4858                 :             :   /* Attempt to simplify the expression.  */
    4859                 :      509273 :   if (t)
    4860                 :             :     {
    4861                 :      509270 :       t = gfc_simplify_expr (e, 0);
    4862                 :             :       /* Some calls do not succeed in simplification and return false
    4863                 :             :          even though there is no error; e.g. variable references to
    4864                 :             :          PARAMETER arrays.  */
    4865                 :      509270 :       if (!gfc_is_constant_expr (e))
    4866                 :      466738 :         t = true;
    4867                 :             :     }
    4868                 :             :   return t;
    4869                 :             : }
    4870                 :             : 
    4871                 :             : 
    4872                 :             : /************** Array resolution subroutines **************/
    4873                 :             : 
    4874                 :             : enum compare_result
    4875                 :             : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
    4876                 :             : 
    4877                 :             : /* Compare two integer expressions.  */
    4878                 :             : 
    4879                 :             : static compare_result
    4880                 :      441148 : compare_bound (gfc_expr *a, gfc_expr *b)
    4881                 :             : {
    4882                 :      441148 :   int i;
    4883                 :             : 
    4884                 :      441148 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    4885                 :      288565 :       || b == NULL || b->expr_type != EXPR_CONSTANT)
    4886                 :             :     return CMP_UNKNOWN;
    4887                 :             : 
    4888                 :             :   /* If either of the types isn't INTEGER, we must have
    4889                 :             :      raised an error earlier.  */
    4890                 :             : 
    4891                 :      202520 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    4892                 :             :     return CMP_UNKNOWN;
    4893                 :             : 
    4894                 :      202516 :   i = mpz_cmp (a->value.integer, b->value.integer);
    4895                 :             : 
    4896                 :      202516 :   if (i < 0)
    4897                 :             :     return CMP_LT;
    4898                 :       95363 :   if (i > 0)
    4899                 :       37653 :     return CMP_GT;
    4900                 :             :   return CMP_EQ;
    4901                 :             : }
    4902                 :             : 
    4903                 :             : 
    4904                 :             : /* Compare an integer expression with an integer.  */
    4905                 :             : 
    4906                 :             : static compare_result
    4907                 :       70839 : compare_bound_int (gfc_expr *a, int b)
    4908                 :             : {
    4909                 :       70839 :   int i;
    4910                 :             : 
    4911                 :       70839 :   if (a == NULL
    4912                 :       29888 :       || a->expr_type != EXPR_CONSTANT
    4913                 :       27127 :       || a->ts.type != BT_INTEGER)
    4914                 :             :     return CMP_UNKNOWN;
    4915                 :             : 
    4916                 :       27127 :   i = mpz_cmp_si (a->value.integer, b);
    4917                 :             : 
    4918                 :       27127 :   if (i < 0)
    4919                 :             :     return CMP_LT;
    4920                 :       23916 :   if (i > 0)
    4921                 :       20860 :     return CMP_GT;
    4922                 :             :   return CMP_EQ;
    4923                 :             : }
    4924                 :             : 
    4925                 :             : 
    4926                 :             : /* Compare an integer expression with a mpz_t.  */
    4927                 :             : 
    4928                 :             : static compare_result
    4929                 :       66002 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    4930                 :             : {
    4931                 :       66002 :   int i;
    4932                 :             : 
    4933                 :       66002 :   if (a == NULL
    4934                 :       54178 :       || a->expr_type != EXPR_CONSTANT
    4935                 :       52095 :       || a->ts.type != BT_INTEGER)
    4936                 :             :     return CMP_UNKNOWN;
    4937                 :             : 
    4938                 :       52092 :   i = mpz_cmp (a->value.integer, b);
    4939                 :             : 
    4940                 :       52092 :   if (i < 0)
    4941                 :             :     return CMP_LT;
    4942                 :       23655 :   if (i > 0)
    4943                 :       10230 :     return CMP_GT;
    4944                 :             :   return CMP_EQ;
    4945                 :             : }
    4946                 :             : 
    4947                 :             : 
    4948                 :             : /* Compute the last value of a sequence given by a triplet.
    4949                 :             :    Return 0 if it wasn't able to compute the last value, or if the
    4950                 :             :    sequence if empty, and 1 otherwise.  */
    4951                 :             : 
    4952                 :             : static int
    4953                 :       49506 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    4954                 :             :                                 gfc_expr *stride, mpz_t last)
    4955                 :             : {
    4956                 :       49506 :   mpz_t rem;
    4957                 :             : 
    4958                 :       49506 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    4959                 :       35061 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    4960                 :       30685 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    4961                 :             :     return 0;
    4962                 :             : 
    4963                 :       30366 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    4964                 :       30365 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    4965                 :             :     return 0;
    4966                 :             : 
    4967                 :        6116 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    4968                 :             :     {
    4969                 :       24375 :       if (compare_bound (start, end) == CMP_GT)
    4970                 :             :         return 0;
    4971                 :       22986 :       mpz_set (last, end->value.integer);
    4972                 :       22986 :       return 1;
    4973                 :             :     }
    4974                 :             : 
    4975                 :        5990 :   if (compare_bound_int (stride, 0) == CMP_GT)
    4976                 :             :     {
    4977                 :             :       /* Stride is positive */
    4978                 :        5046 :       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
    4979                 :             :         return 0;
    4980                 :             :     }
    4981                 :             :   else
    4982                 :             :     {
    4983                 :             :       /* Stride is negative */
    4984                 :         944 :       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
    4985                 :             :         return 0;
    4986                 :             :     }
    4987                 :             : 
    4988                 :        5970 :   mpz_init (rem);
    4989                 :        5970 :   mpz_sub (rem, end->value.integer, start->value.integer);
    4990                 :        5970 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    4991                 :        5970 :   mpz_sub (last, end->value.integer, rem);
    4992                 :        5970 :   mpz_clear (rem);
    4993                 :             : 
    4994                 :        5970 :   return 1;
    4995                 :             : }
    4996                 :             : 
    4997                 :             : 
    4998                 :             : /* Compare a single dimension of an array reference to the array
    4999                 :             :    specification.  */
    5000                 :             : 
    5001                 :             : static bool
    5002                 :      202682 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5003                 :             : {
    5004                 :      202682 :   mpz_t last_value;
    5005                 :             : 
    5006                 :      202682 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5007                 :             :     {
    5008                 :         395 :       gcc_assert (ar->stride[i] == NULL);
    5009                 :             :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5010                 :         395 :       if (ar->start[i] == NULL)
    5011                 :             :         {
    5012                 :         321 :           gcc_assert (ar->end[i] == NULL);
    5013                 :             :           return true;
    5014                 :             :         }
    5015                 :             :     }
    5016                 :             : 
    5017                 :             : /* Given start, end and stride values, calculate the minimum and
    5018                 :             :    maximum referenced indexes.  */
    5019                 :             : 
    5020                 :      202361 :   switch (ar->dimen_type[i])
    5021                 :             :     {
    5022                 :             :     case DIMEN_VECTOR:
    5023                 :             :     case DIMEN_THIS_IMAGE:
    5024                 :             :       break;
    5025                 :             : 
    5026                 :      146663 :     case DIMEN_STAR:
    5027                 :      146663 :     case DIMEN_ELEMENT:
    5028                 :      146663 :       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
    5029                 :             :         {
    5030                 :           2 :           if (i < as->rank)
    5031                 :           2 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5032                 :             :                          "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5033                 :           2 :                          mpz_get_si (ar->start[i]->value.integer),
    5034                 :           2 :                          mpz_get_si (as->lower[i]->value.integer), i+1);
    5035                 :             :           else
    5036                 :           0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5037                 :             :                          "(%ld < %ld) in codimension %d", &ar->c_where[i],
    5038                 :           0 :                          mpz_get_si (ar->start[i]->value.integer),
    5039                 :           0 :                          mpz_get_si (as->lower[i]->value.integer),
    5040                 :           0 :                          i + 1 - as->rank);
    5041                 :           2 :           return true;
    5042                 :             :         }
    5043                 :      146661 :       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
    5044                 :             :         {
    5045                 :          39 :           if (i < as->rank)
    5046                 :          39 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5047                 :             :                          "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5048                 :          39 :                          mpz_get_si (ar->start[i]->value.integer),
    5049                 :          39 :                          mpz_get_si (as->upper[i]->value.integer), i+1);
    5050                 :             :           else
    5051                 :           0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5052                 :             :                          "(%ld > %ld) in codimension %d", &ar->c_where[i],
    5053                 :           0 :                          mpz_get_si (ar->start[i]->value.integer),
    5054                 :           0 :                          mpz_get_si (as->upper[i]->value.integer),
    5055                 :           0 :                          i + 1 - as->rank);
    5056                 :          39 :           return true;
    5057                 :             :         }
    5058                 :             : 
    5059                 :             :       break;
    5060                 :             : 
    5061                 :       49551 :     case DIMEN_RANGE:
    5062                 :       49551 :       {
    5063                 :             : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
    5064                 :             : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
    5065                 :             : 
    5066                 :       49551 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5067                 :       49551 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5068                 :             : 
    5069                 :             :         /* Check for zero stride, which is not allowed.  */
    5070                 :       49551 :         if (comp_stride_zero == CMP_EQ)
    5071                 :             :           {
    5072                 :           1 :             gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
    5073                 :           1 :             return false;
    5074                 :             :           }
    5075                 :             : 
    5076                 :             :         /* if start == end || (stride > 0 && start < end)
    5077                 :             :                            || (stride < 0 && start > end),
    5078                 :             :            then the array section contains at least one element.  In this
    5079                 :             :            case, there is an out-of-bounds access if
    5080                 :             :            (start < lower || start > upper).  */
    5081                 :       49550 :         if (comp_start_end == CMP_EQ
    5082                 :       48865 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5083                 :       46644 :                 && comp_start_end == CMP_LT)
    5084                 :       21472 :             || (comp_stride_zero == CMP_LT
    5085                 :       21472 :                 && comp_start_end == CMP_GT))
    5086                 :             :           {
    5087                 :       29002 :             if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
    5088                 :             :               {
    5089                 :          27 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5090                 :             :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5091                 :          27 :                        mpz_get_si (AR_START->value.integer),
    5092                 :          27 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5093                 :          27 :                 return true;
    5094                 :             :               }
    5095                 :       28975 :             if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
    5096                 :             :               {
    5097                 :          17 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5098                 :             :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5099                 :          17 :                        mpz_get_si (AR_START->value.integer),
    5100                 :          17 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5101                 :          17 :                 return true;
    5102                 :             :               }
    5103                 :             :           }
    5104                 :             : 
    5105                 :             :         /* If we can compute the highest index of the array section,
    5106                 :             :            then it also has to be between lower and upper.  */
    5107                 :       49506 :         mpz_init (last_value);
    5108                 :       49506 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5109                 :             :                                             last_value))
    5110                 :             :           {
    5111                 :       28956 :             if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
    5112                 :             :               {
    5113                 :           3 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5114                 :             :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5115                 :             :                        mpz_get_si (last_value),
    5116                 :           3 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5117                 :           3 :                 mpz_clear (last_value);
    5118                 :           3 :                 return true;
    5119                 :             :               }
    5120                 :       28953 :             if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
    5121                 :             :               {
    5122                 :           7 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5123                 :             :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5124                 :             :                        mpz_get_si (last_value),
    5125                 :           7 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5126                 :           7 :                 mpz_clear (last_value);
    5127                 :           7 :                 return true;
    5128                 :             :               }
    5129                 :             :           }
    5130                 :       49496 :         mpz_clear (last_value);
    5131                 :             : 
    5132                 :             : #undef AR_START
    5133                 :             : #undef AR_END
    5134                 :             :       }
    5135                 :       49496 :       break;
    5136                 :             : 
    5137                 :           0 :     default:
    5138                 :           0 :       gfc_internal_error ("check_dimension(): Bad array reference");
    5139                 :             :     }
    5140                 :             : 
    5141                 :             :   return true;
    5142                 :             : }
    5143                 :             : 
    5144                 :             : 
    5145                 :             : /* Compare an array reference with an array specification.  */
    5146                 :             : 
    5147                 :             : static bool
    5148                 :      399035 : compare_spec_to_ref (gfc_array_ref *ar)
    5149                 :             : {
    5150                 :      399035 :   gfc_array_spec *as;
    5151                 :      399035 :   int i;
    5152                 :             : 
    5153                 :      399035 :   as = ar->as;
    5154                 :      399035 :   i = as->rank - 1;
    5155                 :             :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5156                 :      399035 :   if (as->type == AS_ASSUMED_SIZE
    5157                 :        5741 :       && (/*ar->type == AR_FULL
    5158                 :        5741 :           ||*/ (ar->type == AR_SECTION
    5159                 :         514 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5160                 :             :     {
    5161                 :           5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5162                 :             :                  "not specified at %L", &ar->where);
    5163                 :           5 :       return false;
    5164                 :             :     }
    5165                 :             : 
    5166                 :      399030 :   if (ar->type == AR_FULL)
    5167                 :             :     return true;
    5168                 :             : 
    5169                 :      152915 :   if (as->rank != ar->dimen)
    5170                 :             :     {
    5171                 :          28 :       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
    5172                 :             :                  &ar->where, ar->dimen, as->rank);
    5173                 :          28 :       return false;
    5174                 :             :     }
    5175                 :             : 
    5176                 :             :   /* ar->codimen == 0 is a local array.  */
    5177                 :      152887 :   if (as->corank != ar->codimen && ar->codimen != 0)
    5178                 :             :     {
    5179                 :           0 :       gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
    5180                 :             :                  &ar->where, ar->codimen, as->corank);
    5181                 :           0 :       return false;
    5182                 :             :     }
    5183                 :             : 
    5184                 :      348047 :   for (i = 0; i < as->rank; i++)
    5185                 :      195161 :     if (!check_dimension (i, ar, as))
    5186                 :             :       return false;
    5187                 :             : 
    5188                 :             :   /* Local access has no coarray spec.  */
    5189                 :      152886 :   if (ar->codimen != 0)
    5190                 :       14387 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5191                 :             :       {
    5192                 :        7523 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5193                 :        5157 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5194                 :             :           {
    5195                 :           2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5196                 :           2 :                        i + 1 - as->rank, &ar->where);
    5197                 :           2 :             return false;
    5198                 :             :           }
    5199                 :        7521 :         if (!check_dimension (i, ar, as))
    5200                 :             :           return false;
    5201                 :             :       }
    5202                 :             : 
    5203                 :             :   return true;
    5204                 :             : }
    5205                 :             : 
    5206                 :             : 
    5207                 :             : /* Resolve one part of an array index.  */
    5208                 :             : 
    5209                 :             : static bool
    5210                 :      687340 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5211                 :             :                      int force_index_integer_kind)
    5212                 :             : {
    5213                 :      687340 :   gfc_typespec ts;
    5214                 :             : 
    5215                 :      687340 :   if (index == NULL)
    5216                 :             :     return true;
    5217                 :             : 
    5218                 :      204653 :   if (!gfc_resolve_expr (index))
    5219                 :             :     return false;
    5220                 :             : 
    5221                 :      204630 :   if (check_scalar && index->rank != 0)
    5222                 :             :     {
    5223                 :           2 :       gfc_error ("Array index at %L must be scalar", &index->where);
    5224                 :           2 :       return false;
    5225                 :             :     }
    5226                 :             : 
    5227                 :      204628 :   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
    5228                 :             :     {
    5229                 :           3 :       gfc_error ("Array index at %L must be of INTEGER type, found %s",
    5230                 :             :                  &index->where, gfc_basic_typename (index->ts.type));
    5231                 :           3 :       return false;
    5232                 :             :     }
    5233                 :             : 
    5234                 :      204625 :   if (index->ts.type == BT_REAL)
    5235                 :         336 :     if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
    5236                 :             :                          &index->where))
    5237                 :             :       return false;
    5238                 :             : 
    5239                 :      204625 :   if ((index->ts.kind != gfc_index_integer_kind
    5240                 :      200270 :        && force_index_integer_kind)
    5241                 :      175073 :       || (index->ts.type != BT_INTEGER
    5242                 :             :           && index->ts.type != BT_UNKNOWN))
    5243                 :             :     {
    5244                 :       29887 :       gfc_clear_ts (&ts);
    5245                 :       29887 :       ts.type = BT_INTEGER;
    5246                 :       29887 :       ts.kind = gfc_index_integer_kind;
    5247                 :             : 
    5248                 :       29887 :       gfc_convert_type_warn (index, &ts, 2, 0);
    5249                 :             :     }
    5250                 :             : 
    5251                 :             :   return true;
    5252                 :             : }
    5253                 :             : 
    5254                 :             : /* Resolve one part of an array index.  */
    5255                 :             : 
    5256                 :             : bool
    5257                 :      458445 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5258                 :             : {
    5259                 :      458445 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5260                 :             : }
    5261                 :             : 
    5262                 :             : /* Resolve a dim argument to an intrinsic function.  */
    5263                 :             : 
    5264                 :             : bool
    5265                 :       17607 : gfc_resolve_dim_arg (gfc_expr *dim)
    5266                 :             : {
    5267                 :       17607 :   if (dim == NULL)
    5268                 :             :     return true;
    5269                 :             : 
    5270                 :       17607 :   if (!gfc_resolve_expr (dim))
    5271                 :             :     return false;
    5272                 :             : 
    5273                 :       17607 :   if (dim->rank != 0)
    5274                 :             :     {
    5275                 :           0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5276                 :           0 :       return false;
    5277                 :             : 
    5278                 :             :     }
    5279                 :             : 
    5280                 :       17607 :   if (dim->ts.type != BT_INTEGER)
    5281                 :             :     {
    5282                 :           0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5283                 :           0 :       return false;
    5284                 :             :     }
    5285                 :             : 
    5286                 :       17607 :   if (dim->ts.kind != gfc_index_integer_kind)
    5287                 :             :     {
    5288                 :       12023 :       gfc_typespec ts;
    5289                 :             : 
    5290                 :       12023 :       gfc_clear_ts (&ts);
    5291                 :       12023 :       ts.type = BT_INTEGER;
    5292                 :       12023 :       ts.kind = gfc_index_integer_kind;
    5293                 :             : 
    5294                 :       12023 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5295                 :             :     }
    5296                 :             : 
    5297                 :             :   return true;
    5298                 :             : }
    5299                 :             : 
    5300                 :             : /* Given an expression that contains array references, update those array
    5301                 :             :    references to point to the right array specifications.  While this is
    5302                 :             :    filled in during matching, this information is difficult to save and load
    5303                 :             :    in a module, so we take care of it here.
    5304                 :             : 
    5305                 :             :    The idea here is that the original array reference comes from the
    5306                 :             :    base symbol.  We traverse the list of reference structures, setting
    5307                 :             :    the stored reference to references.  Component references can
    5308                 :             :    provide an additional array specification.  */
    5309                 :             : static void
    5310                 :             : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5311                 :             : 
    5312                 :             : static bool
    5313                 :         897 : find_array_spec (gfc_expr *e)
    5314                 :             : {
    5315                 :         897 :   gfc_array_spec *as;
    5316                 :         897 :   gfc_component *c;
    5317                 :         897 :   gfc_ref *ref;
    5318                 :         897 :   bool class_as = false;
    5319                 :             : 
    5320                 :         897 :   if (e->symtree->n.sym->assoc)
    5321                 :             :     {
    5322                 :         217 :       if (e->symtree->n.sym->assoc->target)
    5323                 :         217 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5324                 :         217 :       resolve_assoc_var (e->symtree->n.sym, false);
    5325                 :             :     }
    5326                 :             : 
    5327                 :         897 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5328                 :             :     {
    5329                 :         112 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5330                 :         112 :       class_as = true;
    5331                 :             :     }
    5332                 :             :   else
    5333                 :         785 :     as = e->symtree->n.sym->as;
    5334                 :             : 
    5335                 :        2034 :   for (ref = e->ref; ref; ref = ref->next)
    5336                 :        1144 :     switch (ref->type)
    5337                 :             :       {
    5338                 :         899 :       case REF_ARRAY:
    5339                 :         899 :         if (as == NULL)
    5340                 :             :           {
    5341                 :           7 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5342                 :          14 :                          ? ref->u.ar.where : e->where);
    5343                 :           7 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5344                 :             :                        &loc);
    5345                 :           7 :             return false;
    5346                 :             :           }
    5347                 :             : 
    5348                 :         892 :         ref->u.ar.as = as;
    5349                 :         892 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5350                 :             :         as = NULL;
    5351                 :             :         break;
    5352                 :             : 
    5353                 :         221 :       case REF_COMPONENT:
    5354                 :         221 :         c = ref->u.c.component;
    5355                 :         221 :         if (c->attr.dimension)
    5356                 :             :           {
    5357                 :          90 :             if (as != NULL && !(class_as && as == c->as))
    5358                 :           0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5359                 :          90 :             as = c->as;
    5360                 :             :           }
    5361                 :             : 
    5362                 :             :         break;
    5363                 :             : 
    5364                 :             :       case REF_SUBSTRING:
    5365                 :             :       case REF_INQUIRY:
    5366                 :             :         break;
    5367                 :             :       }
    5368                 :             : 
    5369                 :         890 :   if (as != NULL)
    5370                 :           0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5371                 :             : 
    5372                 :             :   return true;
    5373                 :             : }
    5374                 :             : 
    5375                 :             : 
    5376                 :             : /* Resolve an array reference.  */
    5377                 :             : 
    5378                 :             : static bool
    5379                 :      399760 : resolve_array_ref (gfc_array_ref *ar)
    5380                 :             : {
    5381                 :      399760 :   int i, check_scalar;
    5382                 :      399760 :   gfc_expr *e;
    5383                 :             : 
    5384                 :      628627 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5385                 :             :     {
    5386                 :      228895 :       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
    5387                 :             : 
    5388                 :             :       /* Do not force gfc_index_integer_kind for the start.  We can
    5389                 :             :          do fine with any integer kind.  This avoids temporary arrays
    5390                 :             :          created for indexing with a vector.  */
    5391                 :      228895 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5392                 :             :         return false;
    5393                 :      228869 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5394                 :             :         return false;
    5395                 :      228867 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5396                 :             :         return false;
    5397                 :             : 
    5398                 :      228867 :       e = ar->start[i];
    5399                 :             : 
    5400                 :      228867 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5401                 :      139245 :         switch (e->rank)
    5402                 :             :           {
    5403                 :      138401 :           case 0:
    5404                 :      138401 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5405                 :      138401 :             break;
    5406                 :             : 
    5407                 :         844 :           case 1:
    5408                 :         844 :             ar->dimen_type[i] = DIMEN_VECTOR;
    5409                 :         844 :             if (e->expr_type == EXPR_VARIABLE
    5410                 :         446 :                 && e->symtree->n.sym->ts.type == BT_DERIVED)
    5411                 :          13 :               ar->start[i] = gfc_get_parentheses (e);
    5412                 :             :             break;
    5413                 :             : 
    5414                 :           0 :           default:
    5415                 :           0 :             gfc_error ("Array index at %L is an array of rank %d",
    5416                 :             :                        &ar->c_where[i], e->rank);
    5417                 :           0 :             return false;
    5418                 :             :           }
    5419                 :             : 
    5420                 :             :       /* Fill in the upper bound, which may be lower than the
    5421                 :             :          specified one for something like a(2:10:5), which is
    5422                 :             :          identical to a(2:7:5).  Only relevant for strides not equal
    5423                 :             :          to one.  Don't try a division by zero.  */
    5424                 :      228867 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5425                 :       68491 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5426                 :        7653 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5427                 :        7506 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5428                 :             :         {
    5429                 :        7505 :           mpz_t size, end;
    5430                 :             : 
    5431                 :        7505 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5432                 :             :             {
    5433                 :        6000 :               if (ar->end[i] == NULL)
    5434                 :             :                 {
    5435                 :        7824 :                   ar->end[i] =
    5436                 :        3912 :                     gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
    5437                 :             :                                            &ar->where);
    5438                 :        3912 :                   mpz_set (ar->end[i]->value.integer, end);
    5439                 :             :                 }
    5440                 :        2088 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5441                 :        2088 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5442                 :             :                 {
    5443                 :        2088 :                   mpz_set (ar->end[i]->value.integer, end);
    5444                 :             :                 }
    5445                 :             :               else
    5446                 :           0 :                 gcc_unreachable ();
    5447                 :             : 
    5448                 :        6000 :               mpz_clear (size);
    5449                 :        6000 :               mpz_clear (end);
    5450                 :             :             }
    5451                 :             :         }
    5452                 :             :     }
    5453                 :             : 
    5454                 :      399732 :   if (ar->type == AR_FULL)
    5455                 :             :     {
    5456                 :      248978 :       if (ar->as->rank == 0)
    5457                 :        2829 :         ar->type = AR_ELEMENT;
    5458                 :             : 
    5459                 :             :       /* Make sure array is the same as array(:,:), this way
    5460                 :             :          we don't need to special case all the time.  */
    5461                 :      248978 :       ar->dimen = ar->as->rank;
    5462                 :      592556 :       for (i = 0; i < ar->dimen; i++)
    5463                 :             :         {
    5464                 :      343578 :           ar->dimen_type[i] = DIMEN_RANGE;
    5465                 :             : 
    5466                 :      343578 :           gcc_assert (ar->start[i] == NULL);
    5467                 :      343578 :           gcc_assert (ar->end[i] == NULL);
    5468                 :      343578 :           gcc_assert (ar->stride[i] == NULL);
    5469                 :             :         }
    5470                 :             :     }
    5471                 :             : 
    5472                 :             :   /* If the reference type is unknown, figure out what kind it is.  */
    5473                 :             : 
    5474                 :      399732 :   if (ar->type == AR_UNKNOWN)
    5475                 :             :     {
    5476                 :      140947 :       ar->type = AR_ELEMENT;
    5477                 :      274288 :       for (i = 0; i < ar->dimen; i++)
    5478                 :      169584 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5479                 :      169584 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5480                 :             :           {
    5481                 :       36243 :             ar->type = AR_SECTION;
    5482                 :       36243 :             break;
    5483                 :             :           }
    5484                 :             :     }
    5485                 :             : 
    5486                 :      399732 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5487                 :             :     return false;
    5488                 :             : 
    5489                 :      399696 :   if (ar->as->corank && ar->codimen == 0)
    5490                 :             :     {
    5491                 :        1319 :       int n;
    5492                 :        1319 :       ar->codimen = ar->as->corank;
    5493                 :        3812 :       for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
    5494                 :        2493 :         ar->dimen_type[n] = DIMEN_THIS_IMAGE;
    5495                 :             :     }
    5496                 :             : 
    5497                 :      399696 :   if (ar->codimen)
    5498                 :             :     {
    5499                 :       10108 :       if (ar->team_type == TEAM_NUMBER)
    5500                 :             :         {
    5501                 :          12 :           if (!gfc_resolve_expr (ar->team))
    5502                 :             :             return false;
    5503                 :             : 
    5504                 :          12 :           if (ar->team->rank != 0)
    5505                 :             :             {
    5506                 :           0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5507                 :             :                          &ar->team->where);
    5508                 :           0 :               return false;
    5509                 :             :             }
    5510                 :             : 
    5511                 :          12 :           if (ar->team->ts.type != BT_INTEGER)
    5512                 :             :             {
    5513                 :           4 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5514                 :             :                          "type, found %s",
    5515                 :           4 :                          &ar->team->where,
    5516                 :             :                          gfc_basic_typename (ar->team->ts.type));
    5517                 :           4 :               return false;
    5518                 :             :             }
    5519                 :             :         }
    5520                 :       10096 :       else if (ar->team_type == TEAM_TEAM)
    5521                 :             :         {
    5522                 :          12 :           if (!gfc_resolve_expr (ar->team))
    5523                 :             :             return false;
    5524                 :             : 
    5525                 :          12 :           if (ar->team->rank != 0)
    5526                 :             :             {
    5527                 :           2 :               gfc_error ("TEAM argument at %L must be scalar",
    5528                 :             :                          &ar->team->where);
    5529                 :           2 :               return false;
    5530                 :             :             }
    5531                 :             : 
    5532                 :          10 :           if (ar->team->ts.type != BT_DERIVED
    5533                 :           8 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5534                 :           8 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5535                 :             :             {
    5536                 :           2 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5537                 :             :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5538                 :           2 :                          &ar->team->where,
    5539                 :             :                          gfc_basic_typename (ar->team->ts.type));
    5540                 :           2 :               return false;
    5541                 :             :             }
    5542                 :             :         }
    5543                 :       10100 :       if (ar->stat)
    5544                 :             :         {
    5545                 :          29 :           if (!gfc_resolve_expr (ar->stat))
    5546                 :             :             return false;
    5547                 :             : 
    5548                 :          29 :           if (ar->stat->rank != 0)
    5549                 :             :             {
    5550                 :           2 :               gfc_error ("STAT argument at %L must be scalar",
    5551                 :             :                          &ar->stat->where);
    5552                 :           2 :               return false;
    5553                 :             :             }
    5554                 :             : 
    5555                 :          27 :           if (ar->stat->ts.type != BT_INTEGER)
    5556                 :             :             {
    5557                 :           2 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5558                 :             :                          "type, found %s",
    5559                 :           2 :                          &ar->stat->where,
    5560                 :             :                          gfc_basic_typename (ar->stat->ts.type));
    5561                 :           2 :               return false;
    5562                 :             :             }
    5563                 :             : 
    5564                 :          25 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5565                 :             :             {
    5566                 :           0 :               gfc_error ("STAT's expression at %L must be a variable",
    5567                 :             :                          &ar->stat->where);
    5568                 :           0 :               return false;
    5569                 :             :             }
    5570                 :             :         }
    5571                 :             :     }
    5572                 :             :   return true;
    5573                 :             : }
    5574                 :             : 
    5575                 :             : 
    5576                 :             : bool
    5577                 :        8269 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5578                 :             : {
    5579                 :        8269 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5580                 :             : 
    5581                 :        8269 :   if (ref->u.ss.start != NULL)
    5582                 :             :     {
    5583                 :        8269 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5584                 :             :         return false;
    5585                 :             : 
    5586                 :        8269 :       if (ref->u.ss.start->ts.type != BT_INTEGER)
    5587                 :             :         {
    5588                 :           1 :           gfc_error ("Substring start index at %L must be of type INTEGER",
    5589                 :             :                      &ref->u.ss.start->where);
    5590                 :           1 :           return false;
    5591                 :             :         }
    5592                 :             : 
    5593                 :        8268 :       if (ref->u.ss.start->rank != 0)
    5594                 :             :         {
    5595                 :           0 :           gfc_error ("Substring start index at %L must be scalar",
    5596                 :             :                      &ref->u.ss.start->where);
    5597                 :           0 :           return false;
    5598                 :             :         }
    5599                 :             : 
    5600                 :        8268 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5601                 :        8268 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5602                 :          37 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5603                 :             :         {
    5604                 :           1 :           gfc_error ("Substring start index at %L is less than one",
    5605                 :             :                      &ref->u.ss.start->where);
    5606                 :           1 :           return false;
    5607                 :             :         }
    5608                 :             :     }
    5609                 :             : 
    5610                 :        8267 :   if (ref->u.ss.end != NULL)
    5611                 :             :     {
    5612                 :        8098 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5613                 :             :         return false;
    5614                 :             : 
    5615                 :        8098 :       if (ref->u.ss.end->ts.type != BT_INTEGER)
    5616                 :             :         {
    5617                 :           1 :           gfc_error ("Substring end index at %L must be of type INTEGER",
    5618                 :             :                      &ref->u.ss.end->where);
    5619                 :           1 :           return false;
    5620                 :             :         }
    5621                 :             : 
    5622                 :        8097 :       if (ref->u.ss.end->rank != 0)
    5623                 :             :         {
    5624                 :           0 :           gfc_error ("Substring end index at %L must be scalar",
    5625                 :             :                      &ref->u.ss.end->where);
    5626                 :           0 :           return false;
    5627                 :             :         }
    5628                 :             : 
    5629                 :        8097 :       if (ref->u.ss.length != NULL
    5630                 :        7766 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5631                 :        8109 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5632                 :          12 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5633                 :             :         {
    5634                 :           4 :           gfc_error ("Substring end index at %L exceeds the string length",
    5635                 :             :                      &ref->u.ss.start->where);
    5636                 :           4 :           return false;
    5637                 :             :         }
    5638                 :             : 
    5639                 :        8093 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5640                 :        8093 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5641                 :        8093 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5642                 :           7 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5643                 :             :         {
    5644                 :           4 :           gfc_error ("Substring end index at %L is too large",
    5645                 :             :                      &ref->u.ss.end->where);
    5646                 :           4 :           return false;
    5647                 :             :         }
    5648                 :             :       /*  If the substring has the same length as the original
    5649                 :             :           variable, the reference itself can be deleted.  */
    5650                 :             : 
    5651                 :        8089 :       if (ref->u.ss.length != NULL
    5652                 :        7758 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5653                 :        9003 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5654                 :         228 :         *equal_length = true;
    5655                 :             :     }
    5656                 :             : 
    5657                 :             :   return true;
    5658                 :             : }
    5659                 :             : 
    5660                 :             : 
    5661                 :             : /* This function supplies missing substring charlens.  */
    5662                 :             : 
    5663                 :             : void
    5664                 :        4456 : gfc_resolve_substring_charlen (gfc_expr *e)
    5665                 :             : {
    5666                 :        4456 :   gfc_ref *char_ref;
    5667                 :        4456 :   gfc_expr *start, *end;
    5668                 :        4456 :   gfc_typespec *ts = NULL;
    5669                 :        4456 :   mpz_t diff;
    5670                 :             : 
    5671                 :        8558 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    5672                 :             :     {
    5673                 :        6724 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    5674                 :             :         break;
    5675                 :        4102 :       if (char_ref->type == REF_COMPONENT)
    5676                 :         242 :         ts = &char_ref->u.c.component->ts;
    5677                 :             :     }
    5678                 :             : 
    5679                 :        4456 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    5680                 :        1896 :     return;
    5681                 :             : 
    5682                 :        2622 :   gcc_assert (char_ref->next == NULL);
    5683                 :             : 
    5684                 :        2622 :   if (e->ts.u.cl)
    5685                 :             :     {
    5686                 :         120 :       if (e->ts.u.cl->length)
    5687                 :         108 :         gfc_free_expr (e->ts.u.cl->length);
    5688                 :          12 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    5689                 :             :         return;
    5690                 :             :     }
    5691                 :             : 
    5692                 :        2610 :   if (!e->ts.u.cl)
    5693                 :        2502 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    5694                 :             : 
    5695                 :        2610 :   if (char_ref->u.ss.start)
    5696                 :        2610 :     start = gfc_copy_expr (char_ref->u.ss.start);
    5697                 :             :   else
    5698                 :           0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    5699                 :             : 
    5700                 :        2610 :   if (char_ref->u.ss.end)
    5701                 :        2560 :     end = gfc_copy_expr (char_ref->u.ss.end);
    5702                 :          50 :   else if (e->expr_type == EXPR_VARIABLE)
    5703                 :             :     {
    5704                 :          50 :       if (!ts)
    5705                 :          32 :         ts = &e->symtree->n.sym->ts;
    5706                 :          50 :       end = gfc_copy_expr (ts->u.cl->length);
    5707                 :             :     }
    5708                 :             :   else
    5709                 :             :     end = NULL;
    5710                 :             : 
    5711                 :        2610 :   if (!start || !end)
    5712                 :             :     {
    5713                 :          50 :       gfc_free_expr (start);
    5714                 :          50 :       gfc_free_expr (end);
    5715                 :          50 :       return;
    5716                 :             :     }
    5717                 :             : 
    5718                 :             :   /* Length = (end - start + 1).
    5719                 :             :      Check first whether it has a constant length.  */
    5720                 :        2560 :   if (gfc_dep_difference (end, start, &diff))
    5721                 :             :     {
    5722                 :        2446 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    5723                 :             :                                              &e->where);
    5724                 :             : 
    5725                 :        2446 :       mpz_add_ui (len->value.integer, diff, 1);
    5726                 :        2446 :       mpz_clear (diff);
    5727                 :        2446 :       e->ts.u.cl->length = len;
    5728                 :             :       /* The check for length < 0 is handled below */
    5729                 :             :     }
    5730                 :             :   else
    5731                 :             :     {
    5732                 :         114 :       e->ts.u.cl->length = gfc_subtract (end, start);
    5733                 :         114 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    5734                 :             :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    5735                 :             :                                                       NULL, 1));
    5736                 :             :     }
    5737                 :             : 
    5738                 :             :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    5739                 :             :      be within the range 1, 2, ..., n unless the starting point exceeds
    5740                 :             :      the ending point, in which case the substring has length zero.  */
    5741                 :             : 
    5742                 :        2560 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    5743                 :          15 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    5744                 :             : 
    5745                 :        2560 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    5746                 :        2560 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    5747                 :             : 
    5748                 :             :   /* Make sure that the length is simplified.  */
    5749                 :        2560 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    5750                 :        2560 :   gfc_resolve_expr (e->ts.u.cl->length);
    5751                 :             : }
    5752                 :             : 
    5753                 :             : 
    5754                 :             : /* Resolve subtype references.  */
    5755                 :             : 
    5756                 :             : bool
    5757                 :      509459 : gfc_resolve_ref (gfc_expr *expr)
    5758                 :             : {
    5759                 :      509459 :   int current_part_dimension, n_components, seen_part_dimension, dim;
    5760                 :      509459 :   gfc_ref *ref, **prev, *array_ref;
    5761                 :      509459 :   bool equal_length;
    5762                 :             : 
    5763                 :      995506 :   for (ref = expr->ref; ref; ref = ref->next)
    5764                 :      486944 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    5765                 :             :       {
    5766                 :         897 :         if (!find_array_spec (expr))
    5767                 :             :           return false;
    5768                 :             :         break;
    5769                 :             :       }
    5770                 :             : 
    5771                 :      996457 :   for (prev = &expr->ref; *prev != NULL;
    5772                 :      487005 :        prev = *prev == NULL ? prev : &(*prev)->next)
    5773                 :      487089 :     switch ((*prev)->type)
    5774                 :             :       {
    5775                 :      399760 :       case REF_ARRAY:
    5776                 :      399760 :         if (!resolve_array_ref (&(*prev)->u.ar))
    5777                 :             :             return false;
    5778                 :             :         break;
    5779                 :             : 
    5780                 :             :       case REF_COMPONENT:
    5781                 :             :       case REF_INQUIRY:
    5782                 :             :         break;
    5783                 :             : 
    5784                 :        7992 :       case REF_SUBSTRING:
    5785                 :        7992 :         equal_length = false;
    5786                 :        7992 :         if (!gfc_resolve_substring (*prev, &equal_length))
    5787                 :             :             return false;
    5788                 :             : 
    5789                 :        7984 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    5790                 :             :           {
    5791                 :             :             /* Remove the reference and move the charlen, if any.  */
    5792                 :         203 :             ref = *prev;
    5793                 :         203 :             *prev = ref->next;
    5794                 :         203 :             ref->next = NULL;
    5795                 :         203 :             expr->ts.u.cl = ref->u.ss.length;
    5796                 :         203 :             ref->u.ss.length = NULL;
    5797                 :         203 :             gfc_free_ref_list (ref);
    5798                 :             :           }
    5799                 :             :         break;
    5800                 :             :       }
    5801                 :             : 
    5802                 :             :   /* Check constraints on part references.  */
    5803                 :             : 
    5804                 :      509368 :   current_part_dimension = 0;
    5805                 :      509368 :   seen_part_dimension = 0;
    5806                 :      509368 :   n_components = 0;
    5807                 :      509368 :   array_ref = NULL;
    5808                 :             : 
    5809                 :      996145 :   for (ref = expr->ref; ref; ref = ref->next)
    5810                 :             :     {
    5811                 :      486788 :       switch (ref->type)
    5812                 :             :         {
    5813                 :      399677 :         case REF_ARRAY:
    5814                 :      399677 :           array_ref = ref;
    5815                 :      399677 :           switch (ref->u.ar.type)
    5816                 :             :             {
    5817                 :      246147 :             case AR_FULL:
    5818                 :             :               /* Coarray scalar.  */
    5819                 :      246147 :               if (ref->u.ar.as->rank == 0)
    5820                 :             :                 {
    5821                 :             :                   current_part_dimension = 0;
    5822                 :             :                   break;
    5823                 :             :                 }
    5824                 :             :               /* Fall through.  */
    5825                 :      284690 :             case AR_SECTION:
    5826                 :      284690 :               current_part_dimension = 1;
    5827                 :      284690 :               break;
    5828                 :             : 
    5829                 :      114987 :             case AR_ELEMENT:
    5830                 :      114987 :               array_ref = NULL;
    5831                 :      114987 :               current_part_dimension = 0;
    5832                 :      114987 :               break;
    5833                 :             : 
    5834                 :           0 :             case AR_UNKNOWN:
    5835                 :           0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    5836                 :             :             }
    5837                 :             : 
    5838                 :             :           break;
    5839                 :             : 
    5840                 :       78681 :         case REF_COMPONENT:
    5841                 :       78681 :           if (current_part_dimension || seen_part_dimension)
    5842                 :             :             {
    5843                 :             :               /* F03:C614.  */
    5844                 :        5854 :               if (ref->u.c.component->attr.pointer
    5845                 :        5854 :                   || ref->u.c.component->attr.proc_pointer
    5846                 :        5850 :                   || (ref->u.c.component->ts.type == BT_CLASS
    5847                 :           1 :                         && CLASS_DATA (ref->u.c.component)->attr.pointer))
    5848                 :             :                 {
    5849                 :           4 :                   gfc_error ("Component to the right of a part reference "
    5850                 :             :                              "with nonzero rank must not have the POINTER "
    5851                 :             :                              "attribute at %L", &expr->where);
    5852                 :           4 :                   return false;
    5853                 :             :                 }
    5854                 :        5850 :               else if (ref->u.c.component->attr.allocatable
    5855                 :        5844 :                         || (ref->u.c.component->ts.type == BT_CLASS
    5856                 :           1 :                             && CLASS_DATA (ref->u.c.component)->attr.allocatable))
    5857                 :             : 
    5858                 :             :                 {
    5859                 :           7 :                   gfc_error ("Component to the right of a part reference "
    5860                 :             :                              "with nonzero rank must not have the ALLOCATABLE "
    5861                 :             :                              "attribute at %L", &expr->where);
    5862                 :           7 :                   return false;
    5863                 :             :                 }
    5864                 :             :             }
    5865                 :             : 
    5866                 :       78670 :           n_components++;
    5867                 :       78670 :           break;
    5868                 :             : 
    5869                 :             :         case REF_SUBSTRING:
    5870                 :             :           break;
    5871                 :             : 
    5872                 :         649 :         case REF_INQUIRY:
    5873                 :             :           /* Implement requirement in note 9.7 of F2018 that the result of the
    5874                 :             :              LEN inquiry be a scalar.  */
    5875                 :         649 :           if (ref->u.i == INQUIRY_LEN && array_ref
    5876                 :          40 :               && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
    5877                 :          40 :                   || expr->ts.type == BT_INTEGER))
    5878                 :             :             {
    5879                 :          14 :               array_ref->u.ar.type = AR_ELEMENT;
    5880                 :          14 :               expr->rank = 0;
    5881                 :             :               /* INQUIRY_LEN is not evaluated from the rest of the expr
    5882                 :             :                  but directly from the string length. This means that setting
    5883                 :             :                  the array indices to one does not matter but might trigger
    5884                 :             :                  a runtime bounds error. Suppress the check.  */
    5885                 :          14 :               expr->no_bounds_check = 1;
    5886                 :          28 :               for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    5887                 :             :                 {
    5888                 :          14 :                   array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    5889                 :          14 :                   if (array_ref->u.ar.start[dim])
    5890                 :           0 :                     gfc_free_expr (array_ref->u.ar.start[dim]);
    5891                 :          14 :                   array_ref->u.ar.start[dim]
    5892                 :          14 :                         = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    5893                 :          14 :                   if (array_ref->u.ar.end[dim])
    5894                 :           0 :                     gfc_free_expr (array_ref->u.ar.end[dim]);
    5895                 :          14 :                   if (array_ref->u.ar.stride[dim])
    5896                 :           0 :                     gfc_free_expr (array_ref->u.ar.stride[dim]);
    5897                 :             :                 }
    5898                 :             :             }
    5899                 :             :           break;
    5900                 :             :         }
    5901                 :             : 
    5902                 :      486777 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    5903                 :      476171 :            || ref->next == NULL)
    5904                 :             :           && current_part_dimension
    5905                 :      430961 :           && seen_part_dimension)
    5906                 :             :         {
    5907                 :           0 :           gfc_error ("Two or more part references with nonzero rank must "
    5908                 :             :                      "not be specified at %L", &expr->where);
    5909                 :           0 :           return false;
    5910                 :             :         }
    5911                 :             : 
    5912                 :      486777 :       if (ref->type == REF_COMPONENT)
    5913                 :             :         {
    5914                 :       78670 :           if (current_part_dimension)
    5915                 :        5662 :             seen_part_dimension = 1;
    5916                 :             : 
    5917                 :             :           /* reset to make sure */
    5918                 :             :           current_part_dimension = 0;
    5919                 :             :         }
    5920                 :             :     }
    5921                 :             : 
    5922                 :             :   return true;
    5923                 :             : }
    5924                 :             : 
    5925                 :             : 
    5926                 :             : /* Given an expression, determine its shape.  This is easier than it sounds.
    5927                 :             :    Leaves the shape array NULL if it is not possible to determine the shape.  */
    5928                 :             : 
    5929                 :             : static void
    5930                 :     2514651 : expression_shape (gfc_expr *e)
    5931                 :             : {
    5932                 :     2514651 :   mpz_t array[GFC_MAX_DIMENSIONS];
    5933                 :     2514651 :   int i;
    5934                 :             : 
    5935                 :     2514651 :   if (e->rank <= 0 || e->shape != NULL)
    5936                 :     2348515 :     return;
    5937                 :             : 
    5938                 :      657933 :   for (i = 0; i < e->rank; i++)
    5939                 :      444277 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    5940                 :      166136 :       goto fail;
    5941                 :             : 
    5942                 :      213656 :   e->shape = gfc_get_shape (e->rank);
    5943                 :             : 
    5944                 :      213656 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    5945                 :             : 
    5946                 :      213656 :   return;
    5947                 :             : 
    5948                 :      166136 : fail:
    5949                 :      167807 :   for (i--; i >= 0; i--)
    5950                 :        1671 :     mpz_clear (array[i]);
    5951                 :             : }
    5952                 :             : 
    5953                 :             : 
    5954                 :             : /* Given a variable expression node, compute the rank of the expression by
    5955                 :             :    examining the base symbol and any reference structures it may have.  */
    5956                 :             : 
    5957                 :             : void
    5958                 :     2514651 : gfc_expression_rank (gfc_expr *e)
    5959                 :             : {
    5960                 :     2514651 :   gfc_ref *ref, *last_arr_ref = nullptr;
    5961                 :     2514651 :   int i, rank, corank;
    5962                 :             : 
    5963                 :             :   /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
    5964                 :             :      could lead to serious confusion...  */
    5965                 :     2514651 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    5966                 :             : 
    5967                 :     2514651 :   if (e->ref == NULL)
    5968                 :             :     {
    5969                 :     1867163 :       if (e->expr_type == EXPR_ARRAY)
    5970                 :       66485 :         goto done;
    5971                 :             :       /* Constructors can have a rank different from one via RESHAPE().  */
    5972                 :             : 
    5973                 :     1800678 :       if (e->symtree != NULL)
    5974                 :             :         {
    5975                 :             :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    5976                 :     3601332 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    5977                 :       13504 :                                 && e->symtree->n.sym->ts.u.derived
    5978                 :       13499 :                                 && CLASS_DATA (e->symtree->n.sym))
    5979                 :     1814170 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    5980                 :             :                                  : e->symtree->n.sym->as;
    5981                 :     1800666 :           if (as)
    5982                 :             :             {
    5983                 :         580 :               e->rank = as->rank;
    5984                 :         580 :               e->corank = as->corank;
    5985                 :         580 :               goto done;
    5986                 :             :             }
    5987                 :             :         }
    5988                 :     1800098 :       e->rank = 0;
    5989                 :     1800098 :       e->corank = 0;
    5990                 :     1800098 :       goto done;
    5991                 :             :     }
    5992                 :             : 
    5993                 :             :   rank = 0;
    5994                 :             :   corank = 0;
    5995                 :             : 
    5996                 :     1016254 :   for (ref = e->ref; ref; ref = ref->next)
    5997                 :             :     {
    5998                 :      739136 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    5999                 :      135007 :           && ref->u.c.component->attr.function && !ref->next)
    6000                 :             :         {
    6001                 :         344 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6002                 :         344 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6003                 :             :         }
    6004                 :             : 
    6005                 :      739136 :       if (ref->type != REF_ARRAY)
    6006                 :      141745 :         continue;
    6007                 :             : 
    6008                 :      597391 :       last_arr_ref = ref;
    6009                 :      597391 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6010                 :             :         {
    6011                 :      327373 :           rank = ref->u.ar.as->rank;
    6012                 :      327373 :           break;
    6013                 :             :         }
    6014                 :             : 
    6015                 :      270018 :       if (ref->u.ar.type == AR_SECTION)
    6016                 :             :         {
    6017                 :             :           /* Figure out the rank of the section.  */
    6018                 :       42997 :           if (rank != 0)
    6019                 :           0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6020                 :             : 
    6021                 :      107488 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6022                 :       64491 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6023                 :       64491 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6024                 :       56335 :               rank++;
    6025                 :             : 
    6026                 :             :           break;
    6027                 :             :         }
    6028                 :             :     }
    6029                 :      647488 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6030                 :      581296 :       && last_arr_ref->u.ar.as->rank != -1)
    6031                 :             :     {
    6032                 :       14051 :       for (i = last_arr_ref->u.ar.as->rank;
    6033                 :      587267 :            i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
    6034                 :             :         {
    6035                 :             :           /* For unknown dimen in non-resolved as assume full corank.  */
    6036                 :       14742 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6037                 :       14293 :               || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
    6038                 :         242 :                   && !last_arr_ref->u.ar.as->resolved))
    6039                 :             :             {
    6040                 :             :               corank = last_arr_ref->u.ar.as->corank;
    6041                 :             :               break;
    6042                 :             :             }
    6043                 :       14051 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6044                 :       14051 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6045                 :       13968 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6046                 :       11915 :             corank++;
    6047                 :        2136 :           else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
    6048                 :           0 :             gfc_internal_error ("Illegal coarray index");
    6049                 :             :         }
    6050                 :             :     }
    6051                 :             : 
    6052                 :      647488 :   e->rank = rank;
    6053                 :      647488 :   e->corank = corank;
    6054                 :             : 
    6055                 :     2514651 : done:
    6056                 :     2514651 :   expression_shape (e);
    6057                 :     2514651 : }
    6058                 :             : 
    6059                 :             : 
    6060                 :             : /* Given two expressions, check that their rank is conformable, i.e. either
    6061                 :             :    both have the same rank or at least one is a scalar.  */
    6062                 :             : 
    6063                 :             : bool
    6064                 :    12167701 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6065                 :             : {
    6066                 :    12167701 :   if (op1->expr_type == EXPR_VARIABLE)
    6067                 :      717134 :     gfc_expression_rank (op1);
    6068                 :    12167701 :   if (op2->expr_type == EXPR_VARIABLE)
    6069                 :      441292 :     gfc_expression_rank (op2);
    6070                 :             : 
    6071                 :       70990 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6072                 :    12238365 :          && (op1->corank == 0 || op2->corank == 0
    6073                 :         146 :              || op1->corank == op2->corank);
    6074                 :             : }
    6075                 :             : 
    6076                 :             : /* Resolve a variable expression.  */
    6077                 :             : 
    6078                 :             : static bool
    6079                 :     1273433 : resolve_variable (gfc_expr *e)
    6080                 :             : {
    6081                 :     1273433 :   gfc_symbol *sym;
    6082                 :     1273433 :   bool t;
    6083                 :             : 
    6084                 :     1273433 :   t = true;
    6085                 :             : 
    6086                 :     1273433 :   if (e->symtree == NULL)
    6087                 :             :     return false;
    6088                 :     1273014 :   sym = e->symtree->n.sym;
    6089                 :             : 
    6090                 :             :   /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
    6091                 :             :      as ts.type is set to BT_ASSUMED in resolve_symbol.  */
    6092                 :     1273014 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    6093                 :             :     {
    6094                 :         167 :       if (!actual_arg || inquiry_argument)
    6095                 :             :         {
    6096                 :           2 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
    6097                 :             :                      "be used as actual argument", sym->name, &e->where);
    6098                 :           2 :           return false;
    6099                 :             :         }
    6100                 :             :     }
    6101                 :             :   /* TS 29113, 407b.  */
    6102                 :     1272847 :   else if (e->ts.type == BT_ASSUMED)
    6103                 :             :     {
    6104                 :         555 :       if (!actual_arg)
    6105                 :             :         {
    6106                 :          20 :           gfc_error ("Assumed-type variable %s at %L may only be used "
    6107                 :             :                      "as actual argument", sym->name, &e->where);
    6108                 :          20 :           return false;
    6109                 :             :         }
    6110                 :         535 :       else if (inquiry_argument && !first_actual_arg)
    6111                 :             :         {
    6112                 :             :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6113                 :             :              for all inquiry functions in resolve_function; the reason is
    6114                 :             :              that the function-name resolution happens too late in that
    6115                 :             :              function.  */
    6116                 :           0 :           gfc_error ("Assumed-type variable %s at %L as actual argument to "
    6117                 :             :                      "an inquiry function shall be the first argument",
    6118                 :             :                      sym->name, &e->where);
    6119                 :           0 :           return false;
    6120                 :             :         }
    6121                 :             :     }
    6122                 :             :   /* TS 29113, C535b.  */
    6123                 :     1272292 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6124                 :       35573 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6125                 :       35568 :              && CLASS_DATA (sym)->as
    6126                 :       13669 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6127                 :     1271382 :             || (sym->ts.type != BT_CLASS && sym->as
    6128                 :      345066 :                 && sym->as->type == AS_ASSUMED_RANK))
    6129                 :        7888 :            && !sym->attr.select_rank_temporary
    6130                 :        7888 :            && !(sym->assoc && sym->assoc->ar))
    6131                 :             :     {
    6132                 :        7888 :       if (!actual_arg
    6133                 :        1247 :           && !(cs_base && cs_base->current
    6134                 :        1246 :                && (cs_base->current->op == EXEC_SELECT_RANK
    6135                 :         188 :                    || sym->attr.target)))
    6136                 :             :         {
    6137                 :         144 :           gfc_error ("Assumed-rank variable %s at %L may only be used as "
    6138                 :             :                      "actual argument", sym->name, &e->where);
    6139                 :         144 :           return false;
    6140                 :             :         }
    6141                 :        7744 :       else if (inquiry_argument && !first_actual_arg)
    6142                 :             :         {
    6143                 :             :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6144                 :             :              for all inquiry functions in resolve_function; the reason is
    6145                 :             :              that the function-name resolution happens too late in that
    6146                 :             :              function.  */
    6147                 :           0 :           gfc_error ("Assumed-rank variable %s at %L as actual argument "
    6148                 :             :                      "to an inquiry function shall be the first argument",
    6149                 :             :                      sym->name, &e->where);
    6150                 :           0 :           return false;
    6151                 :             :         }
    6152                 :             :     }
    6153                 :             : 
    6154                 :     1272848 :   if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
    6155                 :         165 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6156                 :         164 :            && e->ref->next == NULL))
    6157                 :             :     {
    6158                 :           1 :       gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
    6159                 :             :                  "a subobject reference", sym->name, &e->ref->u.ar.where);
    6160                 :           1 :       return false;
    6161                 :             :     }
    6162                 :             :   /* TS 29113, 407b.  */
    6163                 :     1272847 :   else if (e->ts.type == BT_ASSUMED && e->ref
    6164                 :         655 :            && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6165                 :         648 :                 && e->ref->next == NULL))
    6166                 :             :     {
    6167                 :           7 :       gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
    6168                 :             :                  "reference", sym->name, &e->ref->u.ar.where);
    6169                 :           7 :       return false;
    6170                 :             :     }
    6171                 :             : 
    6172                 :             :   /* TS 29113, C535b.  */
    6173                 :     1272840 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6174                 :       35573 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6175                 :       35568 :         && CLASS_DATA (sym)->as
    6176                 :       13669 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6177                 :     1271930 :        || (sym->ts.type != BT_CLASS && sym->as
    6178                 :      345570 :            && sym->as->type == AS_ASSUMED_RANK))
    6179                 :        8012 :       && !(sym->assoc && sym->assoc->ar)
    6180                 :        8012 :       && e->ref
    6181                 :        8012 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6182                 :        8008 :            && e->ref->next == NULL))
    6183                 :             :     {
    6184                 :           4 :       gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
    6185                 :             :                  "reference", sym->name, &e->ref->u.ar.where);
    6186                 :           4 :       return false;
    6187                 :             :     }
    6188                 :             : 
    6189                 :             :   /* Guessed type variables are associate_names whose selector had not been
    6190                 :             :      parsed at the time that the construct was parsed. Now the namespace is
    6191                 :             :      being resolved, the TKR of the selector will be available for fixup of
    6192                 :             :      the associate_name.  */
    6193                 :     1272836 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6194                 :             :     {
    6195                 :         372 :       gfc_fixup_inferred_type_refs (e);
    6196                 :             :       /* KIND inquiry ref returns the kind of the target.  */
    6197                 :         372 :       if (e->expr_type == EXPR_CONSTANT)
    6198                 :             :         return true;
    6199                 :             :     }
    6200                 :     1272464 :   else if (sym->attr.select_type_temporary
    6201                 :        8777 :            && sym->ns->assoc_name_inferred)
    6202                 :          92 :     gfc_fixup_inferred_type_refs (e);
    6203                 :             : 
    6204                 :             :   /* For variables that are used in an associate (target => object) where
    6205                 :             :      the object's basetype is array valued while the target is scalar,
    6206                 :             :      the ts' type of the component refs is still array valued, which
    6207                 :             :      can't be translated that way.  */
    6208                 :     1272824 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6209                 :         561 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6210                 :         561 :       && sym->assoc->target->ts.u.derived
    6211                 :         561 :       && CLASS_DATA (sym->assoc->target)
    6212                 :         561 :       && CLASS_DATA (sym->assoc->target)->as)
    6213                 :             :     {
    6214                 :             :       gfc_ref *ref = e->ref;
    6215                 :         657 :       while (ref)
    6216                 :             :         {
    6217                 :         499 :           switch (ref->type)
    6218                 :             :             {
    6219                 :         216 :             case REF_COMPONENT:
    6220                 :         216 :               ref->u.c.sym = sym->ts.u.derived;
    6221                 :             :               /* Stop the loop.  */
    6222                 :         216 :               ref = NULL;
    6223                 :         216 :               break;
    6224                 :         283 :             default:
    6225                 :         283 :               ref = ref->next;
    6226                 :         283 :               break;
    6227                 :             :             }
    6228                 :             :         }
    6229                 :             :     }
    6230                 :             : 
    6231                 :             :   /* If this is an associate-name, it may be parsed with an array reference
    6232                 :             :      in error even though the target is scalar.  Fail directly in this case.
    6233                 :             :      TODO Understand why class scalar expressions must be excluded.  */
    6234                 :     1272824 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6235                 :             :     {
    6236                 :       10752 :       if (sym->ts.type == BT_CLASS)
    6237                 :         242 :         gfc_fix_class_refs (e);
    6238                 :       10752 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6239                 :        2009 :           && e->ref->type == REF_ARRAY)
    6240                 :             :         {
    6241                 :             :           /* Unambiguously scalar!  */
    6242                 :           3 :           if (sym->assoc->target
    6243                 :           3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6244                 :           1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6245                 :           2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6246                 :             :                        sym->name, &e->where);
    6247                 :           3 :           return false;
    6248                 :             :         }
    6249                 :       10749 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6250                 :        6751 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6251                 :             :         {
    6252                 :             :           /* This can happen because the parser did not detect that the
    6253                 :             :              associate name is an array and the expression had no array
    6254                 :             :              part_ref.  */
    6255                 :         140 :           gfc_ref *ref = gfc_get_ref ();
    6256                 :         140 :           ref->type = REF_ARRAY;
    6257                 :         140 :           ref->u.ar.type = AR_FULL;
    6258                 :         140 :           if (sym->as)
    6259                 :             :             {
    6260                 :         139 :               ref->u.ar.as = sym->as;
    6261                 :         139 :               ref->u.ar.dimen = sym->as->rank;
    6262                 :             :             }
    6263                 :         140 :           ref->next = e->ref;
    6264                 :         140 :           e->ref = ref;
    6265                 :             :         }
    6266                 :             :     }
    6267                 :             : 
    6268                 :     1272821 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6269                 :           0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6270                 :             : 
    6271                 :             :   /* On the other hand, the parser may not have known this is an array;
    6272                 :             :      in this case, we have to add a FULL reference.  */
    6273                 :     1272821 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6274                 :             :     {
    6275                 :           0 :       e->ref = gfc_get_ref ();
    6276                 :           0 :       e->ref->type = REF_ARRAY;
    6277                 :           0 :       e->ref->u.ar.type = AR_FULL;
    6278                 :           0 :       e->ref->u.ar.dimen = 0;
    6279                 :             :     }
    6280                 :             : 
    6281                 :             :   /* Like above, but for class types, where the checking whether an array
    6282                 :             :      ref is present is more complicated.  Furthermore make sure not to add
    6283                 :             :      the full array ref to _vptr or _len refs.  */
    6284                 :     1272821 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6285                 :         966 :       && CLASS_DATA (sym)
    6286                 :         966 :       && (CLASS_DATA (sym)->attr.dimension
    6287                 :         966 :           || CLASS_DATA (sym)->attr.codimension)
    6288                 :         555 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6289                 :             :     {
    6290                 :         531 :       gfc_ref *ref, *newref;
    6291                 :             : 
    6292                 :         531 :       newref = gfc_get_ref ();
    6293                 :         531 :       newref->type = REF_ARRAY;
    6294                 :         531 :       newref->u.ar.type = AR_FULL;
    6295                 :         531 :       newref->u.ar.dimen = 0;
    6296                 :             : 
    6297                 :             :       /* Because this is an associate var and the first ref either is a ref to
    6298                 :             :          the _data component or not, no traversal of the ref chain is
    6299                 :             :          needed.  The array ref needs to be inserted after the _data ref,
    6300                 :             :          or when that is not present, which may happened for polymorphic
    6301                 :             :          types, then at the first position.  */
    6302                 :         531 :       ref = e->ref;
    6303                 :         531 :       if (!ref)
    6304                 :          18 :         e->ref = newref;
    6305                 :         513 :       else if (ref->type == REF_COMPONENT
    6306                 :         230 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6307                 :             :         {
    6308                 :         230 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6309                 :             :             {
    6310                 :          12 :               newref->next = ref->next;
    6311                 :          12 :               ref->next = newref;
    6312                 :             :             }
    6313                 :             :           else
    6314                 :             :             /* Array ref present already.  */
    6315                 :         218 :             gfc_free_ref_list (newref);
    6316                 :             :         }
    6317                 :         283 :       else if (ref->type == REF_ARRAY)
    6318                 :             :         /* Array ref present already.  */
    6319                 :         283 :         gfc_free_ref_list (newref);
    6320                 :             :       else
    6321                 :             :         {
    6322                 :           0 :           newref->next = ref;
    6323                 :           0 :           e->ref = newref;
    6324                 :             :         }
    6325                 :             :     }
    6326                 :     1272290 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6327                 :             :     {
    6328                 :         485 :       gfc_ref *ref;
    6329                 :         908 :       for (ref = e->ref; ref; ref = ref->next)
    6330                 :         453 :         if (ref->type == REF_SUBSTRING)
    6331                 :             :           break;
    6332                 :         485 :       if (ref == NULL)
    6333                 :         455 :         e->ts = sym->ts;
    6334                 :             :     }
    6335                 :             : 
    6336                 :     1272821 :   if (e->ref && !gfc_resolve_ref (e))
    6337                 :             :     return false;
    6338                 :             : 
    6339                 :     1272735 :   if (sym->attr.flavor == FL_PROCEDURE
    6340                 :       29140 :       && (!sym->attr.function
    6341                 :       16902 :           || (sym->attr.function && sym->result
    6342                 :             :               && sym->result->attr.proc_pointer
    6343                 :       16456 :               && !sym->result->attr.function)))
    6344                 :             :     {
    6345                 :       12238 :       e->ts.type = BT_PROCEDURE;
    6346                 :       12238 :       goto resolve_procedure;
    6347                 :             :     }
    6348                 :             : 
    6349                 :     1260497 :   if (sym->ts.type != BT_UNKNOWN)
    6350                 :     1259858 :     gfc_variable_attr (e, &e->ts);
    6351                 :         639 :   else if (sym->attr.flavor == FL_PROCEDURE
    6352                 :          12 :            && sym->attr.function && sym->result
    6353                 :          12 :            && sym->result->ts.type != BT_UNKNOWN
    6354                 :          10 :            && sym->result->attr.proc_pointer)
    6355                 :          10 :     e->ts = sym->result->ts;
    6356                 :             :   else
    6357                 :             :     {
    6358                 :             :       /* Must be a simple variable reference.  */
    6359                 :         629 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6360                 :             :         return false;
    6361                 :         504 :       e->ts = sym->ts;
    6362                 :             :     }
    6363                 :             : 
    6364                 :     1260372 :   if (check_assumed_size_reference (sym, e))
    6365                 :             :     return false;
    6366                 :             : 
    6367                 :             :   /* Deal with forward references to entries during gfc_resolve_code, to
    6368                 :             :      satisfy, at least partially, 12.5.2.5.  */
    6369                 :     1260353 :   if (gfc_current_ns->entries
    6370                 :        3060 :       && current_entry_id == sym->entry_id
    6371                 :        1000 :       && cs_base
    6372                 :         914 :       && cs_base->current
    6373                 :         914 :       && cs_base->current->op != EXEC_ENTRY)
    6374                 :             :     {
    6375                 :         914 :       gfc_entry_list *entry;
    6376                 :         914 :       gfc_formal_arglist *formal;
    6377                 :         914 :       int n;
    6378                 :         914 :       bool seen, saved_specification_expr;
    6379                 :             : 
    6380                 :             :       /* If the symbol is a dummy...  */
    6381                 :         914 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6382                 :             :         {
    6383                 :             :           entry = gfc_current_ns->entries;
    6384                 :             :           seen = false;
    6385                 :             : 
    6386                 :             :           /* ...test if the symbol is a parameter of previous entries.  */
    6387                 :        1033 :           for (; entry && entry->id <= current_entry_id; entry = entry->next)
    6388                 :        1006 :             for (formal = entry->sym->formal; formal; formal = formal->next)
    6389                 :             :               {
    6390                 :         997 :                 if (formal->sym && sym->name == formal->sym->name)
    6391                 :             :                   {
    6392                 :             :                     seen = true;
    6393                 :             :                     break;
    6394                 :             :                   }
    6395                 :             :               }
    6396                 :             : 
    6397                 :             :           /*  If it has not been seen as a dummy, this is an error.  */
    6398                 :         453 :           if (!seen)
    6399                 :             :             {
    6400                 :           3 :               if (specification_expr)
    6401                 :           2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6402                 :             :                            ", is referenced at %L before the ENTRY statement "
    6403                 :             :                            "in which it is a parameter",
    6404                 :             :                            sym->name, &cs_base->current->loc);
    6405                 :             :               else
    6406                 :           1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6407                 :             :                            "statement in which it is a parameter",
    6408                 :             :                            sym->name, &cs_base->current->loc);
    6409                 :             :               t = false;
    6410                 :             :             }
    6411                 :             :         }
    6412                 :             : 
    6413                 :             :       /* Now do the same check on the specification expressions.  */
    6414                 :         914 :       saved_specification_expr = specification_expr;
    6415                 :         914 :       specification_expr = true;
    6416                 :         914 :       if (sym->ts.type == BT_CHARACTER
    6417                 :         914 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6418                 :             :         t = false;
    6419                 :             : 
    6420                 :         914 :       if (sym->as)
    6421                 :             :         {
    6422                 :         271 :           for (n = 0; n < sym->as->rank; n++)
    6423                 :             :             {
    6424                 :         159 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6425                 :           0 :                 t = false;
    6426                 :         159 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6427                 :           1 :                 t = false;
    6428                 :             :             }
    6429                 :             :         }
    6430                 :         914 :       specification_expr = saved_specification_expr;
    6431                 :             : 
    6432                 :         914 :       if (t)
    6433                 :             :         /* Update the symbol's entry level.  */
    6434                 :         909 :         sym->entry_id = current_entry_id + 1;
    6435                 :             :     }
    6436                 :             : 
    6437                 :             :   /* If a symbol has been host_associated mark it.  This is used latter,
    6438                 :             :      to identify if aliasing is possible via host association.  */
    6439                 :     1260353 :   if (sym->attr.flavor == FL_VARIABLE
    6440                 :     1226069 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6441                 :        5267 :           || !sym->ns->code->ext.block.assoc)
    6442                 :     1224620 :       && gfc_current_ns->parent
    6443                 :      590716 :       && (gfc_current_ns->parent == sym->ns
    6444                 :      554330 :           || (gfc_current_ns->parent->parent
    6445                 :       10594 :               && gfc_current_ns->parent->parent == sym->ns)))
    6446                 :       42726 :     sym->attr.host_assoc = 1;
    6447                 :             : 
    6448                 :     1260353 :   if (gfc_current_ns->proc_name
    6449                 :     1256984 :       && sym->attr.dimension
    6450                 :      340324 :       && (sym->ns != gfc_current_ns
    6451                 :             :           || sym->attr.use_assoc
    6452                 :      317192 :           || sym->attr.in_common))
    6453                 :       31869 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6454                 :             : 
    6455                 :     1272591 : resolve_procedure:
    6456                 :     1272591 :   if (t && !resolve_procedure_expression (e))
    6457                 :             :     t = false;
    6458                 :             : 
    6459                 :             :   /* F2008, C617 and C1229.  */
    6460                 :     1271564 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6461                 :     1363704 :       && gfc_is_coindexed (e))
    6462                 :             :     {
    6463                 :         304 :       gfc_ref *ref, *ref2 = NULL;
    6464                 :             : 
    6465                 :         383 :       for (ref = e->ref; ref; ref = ref->next)
    6466                 :             :         {
    6467                 :         383 :           if (ref->type == REF_COMPONENT)
    6468                 :          79 :             ref2 = ref;
    6469                 :         383 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6470                 :             :             break;
    6471                 :             :         }
    6472                 :             : 
    6473                 :         608 :       for ( ; ref; ref = ref->next)
    6474                 :         316 :         if (ref->type == REF_COMPONENT)
    6475                 :             :           break;
    6476                 :             : 
    6477                 :             :       /* Expression itself is not coindexed object.  */
    6478                 :         304 :       if (ref && e->ts.type == BT_CLASS)
    6479                 :             :         {
    6480                 :           3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6481                 :             :                      &e->where);
    6482                 :           3 :           t = false;
    6483                 :             :         }
    6484                 :             : 
    6485                 :             :       /* Expression itself is coindexed object.  */
    6486                 :         292 :       if (ref == NULL)
    6487                 :             :         {
    6488                 :         292 :           gfc_component *c;
    6489                 :         292 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6490                 :         408 :           for ( ; c; c = c->next)
    6491                 :         116 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6492                 :             :               {
    6493                 :           0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6494                 :             :                          "subcomponent at %L", &e->where);
    6495                 :           0 :                 t = false;
    6496                 :           0 :                 break;
    6497                 :             :               }
    6498                 :             :         }
    6499                 :             :     }
    6500                 :             : 
    6501                 :     1272591 :   if (t)
    6502                 :     1272583 :     gfc_expression_rank (e);
    6503                 :             : 
    6504                 :     1272591 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6505                 :           3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6506                 :             :                  "Using variable %qs at %L is deprecated",
    6507                 :             :                  sym->name, &e->where);
    6508                 :             :   /* Simplify cases where access to a parameter array results in a
    6509                 :             :      single constant.  Suppress errors since those will have been
    6510                 :             :      issued before, as warnings.  */
    6511                 :     1272591 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    6512                 :             :     {
    6513                 :        2529 :       gfc_push_suppress_errors ();
    6514                 :        2529 :       gfc_simplify_expr (e, 1);
    6515                 :        2529 :       gfc_pop_suppress_errors ();
    6516                 :             :     }
    6517                 :             : 
    6518                 :             :   return t;
    6519                 :             : }
    6520                 :             : 
    6521                 :             : 
    6522                 :             : /* 'sym' was initially guessed to be derived type but has been corrected
    6523                 :             :    in resolve_assoc_var to be a class entity or the derived type correcting.
    6524                 :             :    If a class entity it will certainly need the _data reference or the
    6525                 :             :    reference derived type symbol correcting in the first component ref if
    6526                 :             :    a derived type.  */
    6527                 :             : 
    6528                 :             : void
    6529                 :         844 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6530                 :             : {
    6531                 :         844 :   gfc_ref *ref, *new_ref;
    6532                 :         844 :   gfc_symbol *sym, *derived;
    6533                 :         844 :   gfc_expr *target;
    6534                 :         844 :   sym = e->symtree->n.sym;
    6535                 :             : 
    6536                 :             :   /* An associate_name whose selector is (i) a component ref of a selector
    6537                 :             :      that is a inferred type associate_name; or (ii) an intrinsic type that
    6538                 :             :      has been inferred from an inquiry ref.  */
    6539                 :         844 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    6540                 :             :     {
    6541                 :         282 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    6542                 :         282 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    6543                 :         282 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    6544                 :             :         {
    6545                 :          60 :           ref = e->ref;
    6546                 :             :           /* A substring misidentified as an array section.  */
    6547                 :          60 :           if (sym->ts.type == BT_CHARACTER
    6548                 :          30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    6549                 :           6 :               && !ref->u.ar.stride[0])
    6550                 :             :             {
    6551                 :           6 :               new_ref = gfc_get_ref ();
    6552                 :           6 :               new_ref->type = REF_SUBSTRING;
    6553                 :           6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    6554                 :           6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    6555                 :           6 :               new_ref->u.ss.length = sym->ts.u.cl;
    6556                 :           6 :               *ref = *new_ref;
    6557                 :           6 :               free (new_ref);
    6558                 :             :             }
    6559                 :             :           else
    6560                 :             :             {
    6561                 :          54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    6562                 :          24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    6563                 :          54 :               e->ref = ref->next;
    6564                 :          54 :               free (ref);
    6565                 :             :             }
    6566                 :             :         }
    6567                 :             : 
    6568                 :             :       /* It is possible for an inquiry reference to be mistaken for a
    6569                 :             :          component reference. Correct this now.  */
    6570                 :         282 :       ref = e->ref;
    6571                 :         282 :       if (ref && ref->type == REF_ARRAY)
    6572                 :         138 :         ref = ref->next;
    6573                 :         150 :       if (ref && ref->type == REF_COMPONENT
    6574                 :         150 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    6575                 :             :         {
    6576                 :          12 :           e->symtree->n.sym = sym;
    6577                 :          12 :           *ref = *new_ref;
    6578                 :          12 :           gfc_free_ref_list (new_ref);
    6579                 :             :         }
    6580                 :             : 
    6581                 :             :       /* The kind of the associate name is best evaluated directly from the
    6582                 :             :          selector because of the guesses made in primary.cc, when the type
    6583                 :             :          is still unknown.  */
    6584                 :         282 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    6585                 :             :         {
    6586                 :          24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    6587                 :          12 :                                            sym->assoc->target->ts.kind);
    6588                 :          12 :           gfc_replace_expr (e, ne);
    6589                 :             :         }
    6590                 :             : 
    6591                 :             :       /* Now that the references are all sorted out, set the expression rank
    6592                 :             :          and return.  */
    6593                 :         282 :       gfc_expression_rank (e);
    6594                 :         282 :       return;
    6595                 :             :     }
    6596                 :             : 
    6597                 :         562 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    6598                 :             :                                      : sym->ts.u.derived;
    6599                 :             : 
    6600                 :             :   /* Ensure that class symbols have an array spec and ensure that there
    6601                 :             :      is a _data field reference following class type references.  */
    6602                 :         562 :   if (sym->ts.type == BT_CLASS
    6603                 :         196 :       && sym->assoc->target->ts.type == BT_CLASS)
    6604                 :             :     {
    6605                 :         196 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    6606                 :         196 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    6607                 :         196 :       sym->attr.dimension = 0;
    6608                 :         196 :       sym->attr.codimension = 0;
    6609                 :         196 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    6610                 :         196 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    6611                 :         196 :       if (e->ref && (e->ref->type != REF_COMPONENT
    6612                 :         160 :                      || e->ref->u.c.component->name[0] != '_'))
    6613                 :             :         {
    6614                 :          82 :           ref = gfc_get_ref ();
    6615                 :          82 :           ref->type = REF_COMPONENT;
    6616                 :          82 :           ref->next = e->ref;
    6617                 :          82 :           e->ref = ref;
    6618                 :          82 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    6619                 :             :                                                    true, true, NULL);
    6620                 :          82 :           ref->u.c.sym = sym->ts.u.derived;
    6621                 :             :         }
    6622                 :             :     }
    6623                 :             : 
    6624                 :             :   /* Proceed as far as the first component reference and ensure that the
    6625                 :             :      correct derived type is being used.  */
    6626                 :         825 :   for (ref = e->ref; ref; ref = ref->next)
    6627                 :         789 :     if (ref->type == REF_COMPONENT)
    6628                 :             :       {
    6629                 :         526 :         if (ref->u.c.component->name[0] != '_')
    6630                 :         330 :           ref->u.c.sym = derived;
    6631                 :             :         else
    6632                 :         196 :           ref->u.c.sym = sym->ts.u.derived;
    6633                 :             :         break;
    6634                 :             :       }
    6635                 :             : 
    6636                 :             :   /* Verify that the type inferrence mechanism has not introduced a spurious
    6637                 :             :      array reference.  This can happen with an associate name, whose selector
    6638                 :             :      is an element of another inferred type.  */
    6639                 :         562 :   target = e->symtree->n.sym->assoc->target;
    6640                 :         562 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    6641                 :         150 :       && e != target && !target->rank)
    6642                 :             :     {
    6643                 :             :       /* First case: array ref after the scalar class or derived
    6644                 :             :          associate_name.  */
    6645                 :         150 :       if (e->ref && e->ref->type == REF_ARRAY
    6646                 :           7 :           && e->ref->u.ar.type != AR_ELEMENT)
    6647                 :             :         {
    6648                 :           7 :           ref = e->ref;
    6649                 :           7 :           if (ref->u.ar.type == AR_UNKNOWN)
    6650                 :           1 :             gfc_error ("Invalid array reference at %L", &e->where);
    6651                 :           7 :           e->ref = ref->next;
    6652                 :           7 :           free (ref);
    6653                 :             : 
    6654                 :             :           /* If it hasn't a ref to the '_data' field supply one.  */
    6655                 :           7 :           if (sym->ts.type == BT_CLASS
    6656                 :           0 :               && !(e->ref->type == REF_COMPONENT
    6657                 :           0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    6658                 :             :             {
    6659                 :           0 :               gfc_ref *new_ref;
    6660                 :           0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    6661                 :             :                                   "_data", true, true, &new_ref);
    6662                 :           0 :               new_ref->next = e->ref;
    6663                 :           0 :               e->ref = new_ref;
    6664                 :             :             }
    6665                 :             :         }
    6666                 :             :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    6667                 :         143 :       else if (e->ref && e->ref->type == REF_COMPONENT
    6668                 :         143 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    6669                 :          64 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    6670                 :           0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    6671                 :             :         {
    6672                 :           0 :           ref = e->ref->next;
    6673                 :           0 :           if (ref->u.ar.type == AR_UNKNOWN)
    6674                 :           0 :             gfc_error ("Invalid array reference at %L", &e->where);
    6675                 :           0 :           e->ref->next = e->ref->next->next;
    6676                 :           0 :           free (ref);
    6677                 :             :         }
    6678                 :             :     }
    6679                 :             : 
    6680                 :             :   /* Now that all the references are OK, get the expression rank.  */
    6681                 :         562 :   gfc_expression_rank (e);
    6682                 :             : }
    6683                 :             : 
    6684                 :             : 
    6685                 :             : /* Checks to see that the correct symbol has been host associated.
    6686                 :             :    The only situations where this arises are:
    6687                 :             :         (i)  That in which a twice contained function is parsed after
    6688                 :             :              the host association is made. On detecting this, change
    6689                 :             :              the symbol in the expression and convert the array reference
    6690                 :             :              into an actual arglist if the old symbol is a variable; or
    6691                 :             :         (ii) That in which an external function is typed but not declared
    6692                 :             :              explicitly to be external. Here, the old symbol is changed
    6693                 :             :              from a variable to an external function.  */
    6694                 :             : static bool
    6695                 :     1600226 : check_host_association (gfc_expr *e)
    6696                 :             : {
    6697                 :     1600226 :   gfc_symbol *sym, *old_sym;
    6698                 :     1600226 :   gfc_symtree *st;
    6699                 :     1600226 :   int n;
    6700                 :     1600226 :   gfc_ref *ref;
    6701                 :     1600226 :   gfc_actual_arglist *arg, *tail = NULL;
    6702                 :     1600226 :   bool retval = e->expr_type == EXPR_FUNCTION;
    6703                 :             : 
    6704                 :             :   /*  If the expression is the result of substitution in
    6705                 :             :       interface.cc(gfc_extend_expr) because there is no way in
    6706                 :             :       which the host association can be wrong.  */
    6707                 :     1600226 :   if (e->symtree == NULL
    6708                 :     1599491 :         || e->symtree->n.sym == NULL
    6709                 :     1599491 :         || e->user_operator)
    6710                 :             :     return retval;
    6711                 :             : 
    6712                 :     1597781 :   old_sym = e->symtree->n.sym;
    6713                 :             : 
    6714                 :     1597781 :   if (gfc_current_ns->parent
    6715                 :      710324 :         && old_sym->ns != gfc_current_ns)
    6716                 :             :     {
    6717                 :             :       /* Use the 'USE' name so that renamed module symbols are
    6718                 :             :          correctly handled.  */
    6719                 :       85937 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    6720                 :             : 
    6721                 :       85937 :       if (sym && old_sym != sym
    6722                 :             :               && sym->attr.flavor == FL_PROCEDURE
    6723                 :         384 :               && sym->attr.contained)
    6724                 :             :         {
    6725                 :             :           /* Clear the shape, since it might not be valid.  */
    6726                 :          71 :           gfc_free_shape (&e->shape, e->rank);
    6727                 :             : 
    6728                 :             :           /* Give the expression the right symtree!  */
    6729                 :          71 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    6730                 :          71 :           gcc_assert (st != NULL);
    6731                 :             : 
    6732                 :          71 :           if (old_sym->attr.flavor == FL_PROCEDURE
    6733                 :          47 :                 || e->expr_type == EXPR_FUNCTION)
    6734                 :             :             {
    6735                 :             :               /* Original was function so point to the new symbol, since
    6736                 :             :                  the actual argument list is already attached to the
    6737                 :             :                  expression.  */
    6738                 :          30 :               e->value.function.esym = NULL;
    6739                 :          30 :               e->symtree = st;
    6740                 :             :             }
    6741                 :             :           else
    6742                 :             :             {
    6743                 :             :               /* Original was variable so convert array references into
    6744                 :             :                  an actual arglist. This does not need any checking now
    6745                 :             :                  since resolve_function will take care of it.  */
    6746                 :          41 :               e->value.function.actual = NULL;
    6747                 :          41 :               e->expr_type = EXPR_FUNCTION;
    6748                 :          41 :               e->symtree = st;
    6749                 :             : 
    6750                 :             :               /* Ambiguity will not arise if the array reference is not
    6751                 :             :                  the last reference.  */
    6752                 :          43 :               for (ref = e->ref; ref; ref = ref->next)
    6753                 :          38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    6754                 :             :                   break;
    6755                 :             : 
    6756                 :          41 :               if ((ref == NULL || ref->type != REF_ARRAY)
    6757                 :           5 :                   && sym->attr.proc == PROC_INTERNAL)
    6758                 :             :                 {
    6759                 :           4 :                   gfc_error ("%qs at %L is host associated at %L into "
    6760                 :             :                              "a contained procedure with an internal "
    6761                 :             :                              "procedure of the same name", sym->name,
    6762                 :             :                               &old_sym->declared_at, &e->where);
    6763                 :           4 :                   return false;
    6764                 :             :                 }
    6765                 :             : 
    6766                 :           1 :               if (ref == NULL)
    6767                 :             :                 return false;
    6768                 :             : 
    6769                 :          36 :               gcc_assert (ref->type == REF_ARRAY);
    6770                 :             : 
    6771                 :             :               /* Grab the start expressions from the array ref and
    6772                 :             :                  copy them into actual arguments.  */
    6773                 :          84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    6774                 :             :                 {
    6775                 :          48 :                   arg = gfc_get_actual_arglist ();
    6776                 :          48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    6777                 :          48 :                   if (e->value.function.actual == NULL)
    6778                 :          36 :                     tail = e->value.function.actual = arg;
    6779                 :             :                   else
    6780                 :             :                     {
    6781                 :          12 :                       tail->next = arg;
    6782                 :          12 :                       tail = arg;
    6783                 :             :                     }
    6784                 :             :                 }
    6785                 :             : 
    6786                 :             :               /* Dump the reference list and set the rank.  */
    6787                 :          36 :               gfc_free_ref_list (e->ref);
    6788                 :          36 :               e->ref = NULL;
    6789                 :          36 :               e->rank = sym->as ? sym->as->rank : 0;
    6790                 :          36 :               e->corank = sym->as ? sym->as->corank : 0;
    6791                 :             :             }
    6792                 :             : 
    6793                 :          66 :           gfc_resolve_expr (e);
    6794                 :          66 :           sym->refs++;
    6795                 :             :         }
    6796                 :             :       /* This case corresponds to a call, from a block or a contained
    6797                 :             :          procedure, to an external function, which has not been declared
    6798                 :             :          as being external in the main program but has been typed.  */
    6799                 :       85866 :       else if (sym && old_sym != sym
    6800                 :         313 :                && !e->ref
    6801                 :         213 :                && sym->ts.type == BT_UNKNOWN
    6802                 :          21 :                && old_sym->ts.type != BT_UNKNOWN
    6803                 :          19 :                && sym->attr.flavor == FL_PROCEDURE
    6804                 :          19 :                && old_sym->attr.flavor == FL_VARIABLE
    6805                 :           7 :                && sym->ns->parent == old_sym->ns
    6806                 :           7 :                && sym->ns->proc_name
    6807                 :           7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    6808                 :           6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    6809                 :           6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    6810                 :             :         {
    6811                 :           6 :           old_sym->attr.flavor = FL_PROCEDURE;
    6812                 :           6 :           old_sym->attr.external = 1;
    6813                 :           6 :           old_sym->attr.function = 1;
    6814                 :           6 :           old_sym->result = old_sym;
    6815                 :           6 :           gfc_resolve_expr (e);
    6816                 :             :         }
    6817                 :             :     }
    6818                 :             :   /* This might have changed!  */
    6819                 :     1597776 :   return e->expr_type == EXPR_FUNCTION;
    6820                 :             : }
    6821                 :             : 
    6822                 :             : 
    6823                 :             : static void
    6824                 :        1322 : gfc_resolve_character_operator (gfc_expr *e)
    6825                 :             : {
    6826                 :        1322 :   gfc_expr *op1 = e->value.op.op1;
    6827                 :        1322 :   gfc_expr *op2 = e->value.op.op2;
    6828                 :        1322 :   gfc_expr *e1 = NULL;
    6829                 :        1322 :   gfc_expr *e2 = NULL;
    6830                 :             : 
    6831                 :        1322 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    6832                 :             : 
    6833                 :        1322 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    6834                 :         653 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    6835                 :         669 :   else if (op1->expr_type == EXPR_CONSTANT)
    6836                 :         262 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    6837                 :             :                            op1->value.character.length);
    6838                 :             : 
    6839                 :        1322 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    6840                 :         635 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    6841                 :         687 :   else if (op2->expr_type == EXPR_CONSTANT)
    6842                 :         457 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    6843                 :             :                            op2->value.character.length);
    6844                 :             : 
    6845                 :        1322 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6846                 :             : 
    6847                 :        1322 :   if (!e1 || !e2)
    6848                 :             :     {
    6849                 :         535 :       gfc_free_expr (e1);
    6850                 :         535 :       gfc_free_expr (e2);
    6851                 :             : 
    6852                 :         535 :       return;
    6853                 :             :     }
    6854                 :             : 
    6855                 :         787 :   e->ts.u.cl->length = gfc_add (e1, e2);
    6856                 :         787 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6857                 :         787 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6858                 :         787 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    6859                 :         787 :   gfc_resolve_expr (e->ts.u.cl->length);
    6860                 :             : 
    6861                 :         787 :   return;
    6862                 :             : }
    6863                 :             : 
    6864                 :             : 
    6865                 :             : /*  Ensure that an character expression has a charlen and, if possible, a
    6866                 :             :     length expression.  */
    6867                 :             : 
    6868                 :             : static void
    6869                 :      175067 : fixup_charlen (gfc_expr *e)
    6870                 :             : {
    6871                 :             :   /* The cases fall through so that changes in expression type and the need
    6872                 :             :      for multiple fixes are picked up.  In all circumstances, a charlen should
    6873                 :             :      be available for the middle end to hang a backend_decl on.  */
    6874                 :      175067 :   switch (e->expr_type)
    6875                 :             :     {
    6876                 :        1322 :     case EXPR_OP:
    6877                 :        1322 :       gfc_resolve_character_operator (e);
    6878                 :             :       /* FALLTHRU */
    6879                 :             : 
    6880                 :        1377 :     case EXPR_ARRAY:
    6881                 :        1377 :       if (e->expr_type == EXPR_ARRAY)
    6882                 :          55 :         gfc_resolve_character_array_constructor (e);
    6883                 :             :       /* FALLTHRU */
    6884                 :             : 
    6885                 :        1833 :     case EXPR_SUBSTRING:
    6886                 :        1833 :       if (!e->ts.u.cl && e->ref)
    6887                 :         452 :         gfc_resolve_substring_charlen (e);
    6888                 :             :       /* FALLTHRU */
    6889                 :             : 
    6890                 :      175067 :     default:
    6891                 :      175067 :       if (!e->ts.u.cl)
    6892                 :      173238 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6893                 :             : 
    6894                 :      175067 :       break;
    6895                 :             :     }
    6896                 :      175067 : }
    6897                 :             : 
    6898                 :             : 
    6899                 :             : /* Update an actual argument to include the passed-object for type-bound
    6900                 :             :    procedures at the right position.  */
    6901                 :             : 
    6902                 :             : static gfc_actual_arglist*
    6903                 :        2793 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    6904                 :             :                      const char *name)
    6905                 :             : {
    6906                 :        2817 :   gcc_assert (argpos > 0);
    6907                 :             : 
    6908                 :        2817 :   if (argpos == 1)
    6909                 :             :     {
    6910                 :        2680 :       gfc_actual_arglist* result;
    6911                 :             : 
    6912                 :        2680 :       result = gfc_get_actual_arglist ();
    6913                 :        2680 :       result->expr = po;
    6914                 :        2680 :       result->next = lst;
    6915                 :        2680 :       if (name)
    6916                 :         502 :         result->name = name;
    6917                 :             : 
    6918                 :        2680 :       return result;
    6919                 :             :     }
    6920                 :             : 
    6921                 :         137 :   if (lst)
    6922                 :         113 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    6923                 :             :   else
    6924                 :          24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    6925                 :             :   return lst;
    6926                 :             : }
    6927                 :             : 
    6928                 :             : 
    6929                 :             : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    6930                 :             : 
    6931                 :             : static gfc_expr*
    6932                 :        6875 : extract_compcall_passed_object (gfc_expr* e)
    6933                 :             : {
    6934                 :        6875 :   gfc_expr* po;
    6935                 :             : 
    6936                 :        6875 :   if (e->expr_type == EXPR_UNKNOWN)
    6937                 :             :     {
    6938                 :           0 :       gfc_error ("Error in typebound call at %L",
    6939                 :             :                  &e->where);
    6940                 :           0 :       return NULL;
    6941                 :             :     }
    6942                 :             : 
    6943                 :        6875 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    6944                 :             : 
    6945                 :        6875 :   if (e->value.compcall.base_object)
    6946                 :        1494 :     po = gfc_copy_expr (e->value.compcall.base_object);
    6947                 :             :   else
    6948                 :             :     {
    6949                 :        5381 :       po = gfc_get_expr ();
    6950                 :        5381 :       po->expr_type = EXPR_VARIABLE;
    6951                 :        5381 :       po->symtree = e->symtree;
    6952                 :        5381 :       po->ref = gfc_copy_ref (e->ref);
    6953                 :        5381 :       po->where = e->where;
    6954                 :             :     }
    6955                 :             : 
    6956                 :        6875 :   if (!gfc_resolve_expr (po))
    6957                 :             :     return NULL;
    6958                 :             : 
    6959                 :             :   return po;
    6960                 :             : }
    6961                 :             : 
    6962                 :             : 
    6963                 :             : /* Update the arglist of an EXPR_COMPCALL expression to include the
    6964                 :             :    passed-object.  */
    6965                 :             : 
    6966                 :             : static bool
    6967                 :        3196 : update_compcall_arglist (gfc_expr* e)
    6968                 :             : {
    6969                 :        3196 :   gfc_expr* po;
    6970                 :        3196 :   gfc_typebound_proc* tbp;
    6971                 :             : 
    6972                 :        3196 :   tbp = e->value.compcall.tbp;
    6973                 :             : 
    6974                 :        3196 :   if (tbp->error)
    6975                 :             :     return false;
    6976                 :             : 
    6977                 :        3195 :   po = extract_compcall_passed_object (e);
    6978                 :        3195 :   if (!po)
    6979                 :             :     return false;
    6980                 :             : 
    6981                 :        3195 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    6982                 :             :     {
    6983                 :        1071 :       gfc_free_expr (po);
    6984                 :        1071 :       return true;
    6985                 :             :     }
    6986                 :             : 
    6987                 :        2124 :   if (tbp->pass_arg_num <= 0)
    6988                 :             :     return false;
    6989                 :             : 
    6990                 :        2123 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    6991                 :             :                                                   tbp->pass_arg_num,
    6992                 :             :                                                   tbp->pass_arg);
    6993                 :             : 
    6994                 :        2123 :   return true;
    6995                 :             : }
    6996                 :             : 
    6997                 :             : 
    6998                 :             : /* Extract the passed object from a PPC call (a copy of it).  */
    6999                 :             : 
    7000                 :             : static gfc_expr*
    7001                 :          85 : extract_ppc_passed_object (gfc_expr *e)
    7002                 :             : {
    7003                 :          85 :   gfc_expr *po;
    7004                 :          85 :   gfc_ref **ref;
    7005                 :             : 
    7006                 :          85 :   po = gfc_get_expr ();
    7007                 :          85 :   po->expr_type = EXPR_VARIABLE;
    7008                 :          85 :   po->symtree = e->symtree;
    7009                 :          85 :   po->ref = gfc_copy_ref (e->ref);
    7010                 :          85 :   po->where = e->where;
    7011                 :             : 
    7012                 :             :   /* Remove PPC reference.  */
    7013                 :          85 :   ref = &po->ref;
    7014                 :          91 :   while ((*ref)->next)
    7015                 :           6 :     ref = &(*ref)->next;
    7016                 :          85 :   gfc_free_ref_list (*ref);
    7017                 :          85 :   *ref = NULL;
    7018                 :             : 
    7019                 :          85 :   if (!gfc_resolve_expr (po))
    7020                 :           0 :     return NULL;
    7021                 :             : 
    7022                 :             :   return po;
    7023                 :             : }
    7024                 :             : 
    7025                 :             : 
    7026                 :             : /* Update the actual arglist of a procedure pointer component to include the
    7027                 :             :    passed-object.  */
    7028                 :             : 
    7029                 :             : static bool
    7030                 :         399 : update_ppc_arglist (gfc_expr* e)
    7031                 :             : {
    7032                 :         399 :   gfc_expr* po;
    7033                 :         399 :   gfc_component *ppc;
    7034                 :         399 :   gfc_typebound_proc* tb;
    7035                 :             : 
    7036                 :         399 :   ppc = gfc_get_proc_ptr_comp (e);
    7037                 :         399 :   if (!ppc)
    7038                 :             :     return false;
    7039                 :             : 
    7040                 :         399 :   tb = ppc->tb;
    7041                 :             : 
    7042                 :         399 :   if (tb->error)
    7043                 :             :     return false;
    7044                 :         397 :   else if (tb->nopass)
    7045                 :             :     return true;
    7046                 :             : 
    7047                 :          85 :   po = extract_ppc_passed_object (e);
    7048                 :          85 :   if (!po)
    7049                 :             :     return false;
    7050                 :             : 
    7051                 :             :   /* F08:R739.  */
    7052                 :          85 :   if (po->rank != 0)
    7053                 :             :     {
    7054                 :           0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7055                 :           0 :       return false;
    7056                 :             :     }
    7057                 :             : 
    7058                 :             :   /* F08:C611.  */
    7059                 :          85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7060                 :             :     {
    7061                 :           1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7062                 :             :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7063                 :           1 :       return false;
    7064                 :             :     }
    7065                 :             : 
    7066                 :          84 :   gcc_assert (tb->pass_arg_num > 0);
    7067                 :          84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7068                 :             :                                                   tb->pass_arg_num,
    7069                 :             :                                                   tb->pass_arg);
    7070                 :             : 
    7071                 :          84 :   return true;
    7072                 :             : }
    7073                 :             : 
    7074                 :             : 
    7075                 :             : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7076                 :             :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7077                 :             : 
    7078                 :             : static bool
    7079                 :        3207 : check_typebound_baseobject (gfc_expr* e)
    7080                 :             : {
    7081                 :        3207 :   gfc_expr* base;
    7082                 :        3207 :   bool return_value = false;
    7083                 :             : 
    7084                 :        3207 :   base = extract_compcall_passed_object (e);
    7085                 :        3207 :   if (!base)
    7086                 :             :     return false;
    7087                 :             : 
    7088                 :        3204 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7089                 :             :     {
    7090                 :           1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7091                 :           1 :       goto cleanup;
    7092                 :             :     }
    7093                 :             : 
    7094                 :        3203 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7095                 :           1 :     return false;
    7096                 :             : 
    7097                 :             :   /* F08:C611.  */
    7098                 :        3202 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7099                 :             :     {
    7100                 :           3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7101                 :             :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7102                 :           3 :       goto cleanup;
    7103                 :             :     }
    7104                 :             : 
    7105                 :             :   /* F08:C1230. If the procedure called is NOPASS,
    7106                 :             :      the base object must be scalar.  */
    7107                 :        3199 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7108                 :             :     {
    7109                 :           1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7110                 :             :                  " be scalar", &e->where);
    7111                 :           1 :       goto cleanup;
    7112                 :             :     }
    7113                 :             : 
    7114                 :             :   return_value = true;
    7115                 :             : 
    7116                 :        3203 : cleanup:
    7117                 :        3203 :   gfc_free_expr (base);
    7118                 :        3203 :   return return_value;
    7119                 :             : }
    7120                 :             : 
    7121                 :             : 
    7122                 :             : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7123                 :             :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7124                 :             :    arglist and the target-procedure symtree are returned.  */
    7125                 :             : 
    7126                 :             : static bool
    7127                 :        3196 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7128                 :             :                           gfc_actual_arglist** actual)
    7129                 :             : {
    7130                 :        3196 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7131                 :        3196 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7132                 :             : 
    7133                 :             :   /* Update the actual arglist for PASS.  */
    7134                 :        3196 :   if (!update_compcall_arglist (e))
    7135                 :             :     return false;
    7136                 :             : 
    7137                 :        3194 :   *actual = e->value.compcall.actual;
    7138                 :        3194 :   *target = e->value.compcall.tbp->u.specific;
    7139                 :             : 
    7140                 :        3194 :   gfc_free_ref_list (e->ref);
    7141                 :        3194 :   e->ref = NULL;
    7142                 :        3194 :   e->value.compcall.actual = NULL;
    7143                 :             : 
    7144                 :             :   /* If we find a deferred typebound procedure, check for derived types
    7145                 :             :      that an overriding typebound procedure has not been missed.  */
    7146                 :        3194 :   if (e->value.compcall.name
    7147                 :        3194 :       && !e->value.compcall.tbp->non_overridable
    7148                 :        3178 :       && e->value.compcall.base_object
    7149                 :         747 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7150                 :             :     {
    7151                 :         460 :       gfc_symtree *st;
    7152                 :         460 :       gfc_symbol *derived;
    7153                 :             : 
    7154                 :             :       /* Use the derived type of the base_object.  */
    7155                 :         460 :       derived = e->value.compcall.base_object->ts.u.derived;
    7156                 :         460 :       st = NULL;
    7157                 :             : 
    7158                 :             :       /* If necessary, go through the inheritance chain.  */
    7159                 :        1381 :       while (!st && derived)
    7160                 :             :         {
    7161                 :             :           /* Look for the typebound procedure 'name'.  */
    7162                 :         461 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7163                 :         460 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7164                 :             :                                    e->value.compcall.name);
    7165                 :         461 :           if (!st)
    7166                 :           1 :             derived = gfc_get_derived_super_type (derived);
    7167                 :             :         }
    7168                 :             : 
    7169                 :             :       /* Now find the specific name in the derived type namespace.  */
    7170                 :         460 :       if (st && st->n.tb && st->n.tb->u.specific)
    7171                 :         460 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7172                 :             :                            derived->ns, 1, &st);
    7173                 :         460 :       if (st)
    7174                 :         460 :         *target = st;
    7175                 :             :     }
    7176                 :             : 
    7177                 :        3194 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7178                 :        3194 :       && !e->value.compcall.tbp->deferred)
    7179                 :           1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7180                 :             :                  " itself recursively.  Declare it RECURSIVE or use"
    7181                 :             :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7182                 :             : 
    7183                 :             :   return true;
    7184                 :             : }
    7185                 :             : 
    7186                 :             : 
    7187                 :             : /* Get the ultimate declared type from an expression.  In addition,
    7188                 :             :    return the last class/derived type reference and the copy of the
    7189                 :             :    reference list.  If check_types is set true, derived types are
    7190                 :             :    identified as well as class references.  */
    7191                 :             : static gfc_symbol*
    7192                 :        3142 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7193                 :             :                         gfc_expr *e, bool check_types)
    7194                 :             : {
    7195                 :        3142 :   gfc_symbol *declared;
    7196                 :        3142 :   gfc_ref *ref;
    7197                 :             : 
    7198                 :        3142 :   declared = NULL;
    7199                 :        3142 :   if (class_ref)
    7200                 :        2771 :     *class_ref = NULL;
    7201                 :        3142 :   if (new_ref)
    7202                 :        2484 :     *new_ref = gfc_copy_ref (e->ref);
    7203                 :             : 
    7204                 :        3911 :   for (ref = e->ref; ref; ref = ref->next)
    7205                 :             :     {
    7206                 :         769 :       if (ref->type != REF_COMPONENT)
    7207                 :         283 :         continue;
    7208                 :             : 
    7209                 :         486 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7210                 :         240 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7211                 :         411 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7212                 :             :         {
    7213                 :         337 :           declared = ref->u.c.component->ts.u.derived;
    7214                 :         337 :           if (class_ref)
    7215                 :         319 :             *class_ref = ref;
    7216                 :             :         }
    7217                 :             :     }
    7218                 :             : 
    7219                 :        3142 :   if (declared == NULL)
    7220                 :        2831 :     declared = e->symtree->n.sym->ts.u.derived;
    7221                 :             : 
    7222                 :        3142 :   return declared;
    7223                 :             : }
    7224                 :             : 
    7225                 :             : 
    7226                 :             : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7227                 :             :    which of the specific bindings (if any) matches the arglist and transform
    7228                 :             :    the expression into a call of that binding.  */
    7229                 :             : 
    7230                 :             : static bool
    7231                 :        3198 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7232                 :             : {
    7233                 :        3198 :   gfc_typebound_proc* genproc;
    7234                 :        3198 :   const char* genname;
    7235                 :        3198 :   gfc_symtree *st;
    7236                 :        3198 :   gfc_symbol *derived;
    7237                 :             : 
    7238                 :        3198 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7239                 :        3198 :   genname = e->value.compcall.name;
    7240                 :        3198 :   genproc = e->value.compcall.tbp;
    7241                 :             : 
    7242                 :        3198 :   if (!genproc->is_generic)
    7243                 :             :     return true;
    7244                 :             : 
    7245                 :             :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7246                 :         383 :   for (; genproc; genproc = genproc->overridden)
    7247                 :             :     {
    7248                 :         381 :       gfc_tbp_generic* g;
    7249                 :             : 
    7250                 :         381 :       gcc_assert (genproc->is_generic);
    7251                 :         569 :       for (g = genproc->u.generic; g; g = g->next)
    7252                 :             :         {
    7253                 :         559 :           gfc_symbol* target;
    7254                 :         559 :           gfc_actual_arglist* args;
    7255                 :         559 :           bool matches;
    7256                 :             : 
    7257                 :         559 :           gcc_assert (g->specific);
    7258                 :             : 
    7259                 :         559 :           if (g->specific->error)
    7260                 :           0 :             continue;
    7261                 :             : 
    7262                 :         559 :           target = g->specific->u.specific->n.sym;
    7263                 :             : 
    7264                 :             :           /* Get the right arglist by handling PASS/NOPASS.  */
    7265                 :         559 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7266                 :         559 :           if (!g->specific->nopass)
    7267                 :             :             {
    7268                 :         473 :               gfc_expr* po;
    7269                 :         473 :               po = extract_compcall_passed_object (e);
    7270                 :         473 :               if (!po)
    7271                 :             :                 {
    7272                 :           0 :                   gfc_free_actual_arglist (args);
    7273                 :           0 :                   return false;
    7274                 :             :                 }
    7275                 :             : 
    7276                 :         473 :               gcc_assert (g->specific->pass_arg_num > 0);
    7277                 :         473 :               gcc_assert (!g->specific->error);
    7278                 :         473 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7279                 :             :                                           g->specific->pass_arg);
    7280                 :             :             }
    7281                 :         559 :           resolve_actual_arglist (args, target->attr.proc,
    7282                 :         559 :                                   is_external_proc (target)
    7283                 :         559 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7284                 :             : 
    7285                 :             :           /* Check if this arglist matches the formal.  */
    7286                 :         559 :           matches = gfc_arglist_matches_symbol (&args, target);
    7287                 :             : 
    7288                 :             :           /* Clean up and break out of the loop if we've found it.  */
    7289                 :         559 :           gfc_free_actual_arglist (args);
    7290                 :         559 :           if (matches)
    7291                 :             :             {
    7292                 :         371 :               e->value.compcall.tbp = g->specific;
    7293                 :         371 :               genname = g->specific_st->name;
    7294                 :             :               /* Pass along the name for CLASS methods, where the vtab
    7295                 :             :                  procedure pointer component has to be referenced.  */
    7296                 :         371 :               if (name)
    7297                 :         159 :                 *name = genname;
    7298                 :         371 :               goto success;
    7299                 :             :             }
    7300                 :             :         }
    7301                 :             :     }
    7302                 :             : 
    7303                 :             :   /* Nothing matching found!  */
    7304                 :           2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7305                 :             :              " %qs at %L", genname, &e->where);
    7306                 :           2 :   return false;
    7307                 :             : 
    7308                 :         371 : success:
    7309                 :             :   /* Make sure that we have the right specific instance for the name.  */
    7310                 :         371 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7311                 :             : 
    7312                 :         371 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7313                 :         371 :   if (st)
    7314                 :         371 :     e->value.compcall.tbp = st->n.tb;
    7315                 :             : 
    7316                 :             :   return true;
    7317                 :             : }
    7318                 :             : 
    7319                 :             : 
    7320                 :             : /* Resolve a call to a type-bound subroutine.  */
    7321                 :             : 
    7322                 :             : static bool
    7323                 :        1675 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7324                 :             : {
    7325                 :        1675 :   gfc_actual_arglist* newactual;
    7326                 :        1675 :   gfc_symtree* target;
    7327                 :             : 
    7328                 :             :   /* Check that's really a SUBROUTINE.  */
    7329                 :        1675 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7330                 :             :     {
    7331                 :          17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7332                 :          15 :           && c->expr1->value.compcall.tbp->u.specific
    7333                 :          15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7334                 :          15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7335                 :          12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7336                 :             :       else
    7337                 :             :         {
    7338                 :           5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7339                 :             :                      c->expr1->value.compcall.name, &c->loc);
    7340                 :           5 :           return false;
    7341                 :             :         }
    7342                 :             :     }
    7343                 :             : 
    7344                 :        1670 :   if (!check_typebound_baseobject (c->expr1))
    7345                 :             :     return false;
    7346                 :             : 
    7347                 :             :   /* Pass along the name for CLASS methods, where the vtab
    7348                 :             :      procedure pointer component has to be referenced.  */
    7349                 :        1663 :   if (name)
    7350                 :         473 :     *name = c->expr1->value.compcall.name;
    7351                 :             : 
    7352                 :        1663 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7353                 :             :     return false;
    7354                 :             : 
    7355                 :             :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7356                 :        1662 :   if (overridable)
    7357                 :         370 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7358                 :             : 
    7359                 :             :   /* Transform into an ordinary EXEC_CALL for now.  */
    7360                 :             : 
    7361                 :        1662 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7362                 :             :     return false;
    7363                 :             : 
    7364                 :        1660 :   c->ext.actual = newactual;
    7365                 :        1660 :   c->symtree = target;
    7366                 :        1660 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7367                 :             : 
    7368                 :        1660 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7369                 :             : 
    7370                 :        1660 :   gfc_free_expr (c->expr1);
    7371                 :        1660 :   c->expr1 = gfc_get_expr ();
    7372                 :        1660 :   c->expr1->expr_type = EXPR_FUNCTION;
    7373                 :        1660 :   c->expr1->symtree = target;
    7374                 :        1660 :   c->expr1->where = c->loc;
    7375                 :             : 
    7376                 :        1660 :   return resolve_call (c);
    7377                 :             : }
    7378                 :             : 
    7379                 :             : 
    7380                 :             : /* Resolve a component-call expression.  */
    7381                 :             : static bool
    7382                 :        1556 : resolve_compcall (gfc_expr* e, const char **name)
    7383                 :             : {
    7384                 :        1556 :   gfc_actual_arglist* newactual;
    7385                 :        1556 :   gfc_symtree* target;
    7386                 :             : 
    7387                 :             :   /* Check that's really a FUNCTION.  */
    7388                 :        1556 :   if (!e->value.compcall.tbp->function)
    7389                 :             :     {
    7390                 :          19 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    7391                 :           5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    7392                 :             :                    &e->where);
    7393                 :          19 :       return false;
    7394                 :             :     }
    7395                 :             : 
    7396                 :             : 
    7397                 :             :   /* These must not be assign-calls!  */
    7398                 :        1537 :   gcc_assert (!e->value.compcall.assign);
    7399                 :             : 
    7400                 :        1537 :   if (!check_typebound_baseobject (e))
    7401                 :             :     return false;
    7402                 :             : 
    7403                 :             :   /* Pass along the name for CLASS methods, where the vtab
    7404                 :             :      procedure pointer component has to be referenced.  */
    7405                 :        1535 :   if (name)
    7406                 :         857 :     *name = e->value.compcall.name;
    7407                 :             : 
    7408                 :        1535 :   if (!resolve_typebound_generic_call (e, name))
    7409                 :             :     return false;
    7410                 :        1534 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7411                 :             : 
    7412                 :             :   /* Take the rank from the function's symbol.  */
    7413                 :        1534 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    7414                 :             :     {
    7415                 :         149 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    7416                 :         149 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    7417                 :             :     }
    7418                 :             : 
    7419                 :             :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    7420                 :             :      arglist to the TBP's binding target.  */
    7421                 :             : 
    7422                 :        1534 :   if (!resolve_typebound_static (e, &target, &newactual))
    7423                 :             :     return false;
    7424                 :             : 
    7425                 :        1534 :   e->value.function.actual = newactual;
    7426                 :        1534 :   e->value.function.name = NULL;
    7427                 :        1534 :   e->value.function.esym = target->n.sym;
    7428                 :        1534 :   e->value.function.isym = NULL;
    7429                 :        1534 :   e->symtree = target;
    7430                 :        1534 :   e->ts = target->n.sym->ts;
    7431                 :        1534 :   e->expr_type = EXPR_FUNCTION;
    7432                 :             : 
    7433                 :             :   /* Resolution is not necessary if this is a class subroutine; this
    7434                 :             :      function only has to identify the specific proc. Resolution of
    7435                 :             :      the call will be done next in resolve_typebound_call.  */
    7436                 :        1534 :   return gfc_resolve_expr (e);
    7437                 :             : }
    7438                 :             : 
    7439                 :             : 
    7440                 :             : static bool resolve_fl_derived (gfc_symbol *sym);
    7441                 :             : 
    7442                 :             : 
    7443                 :             : /* Resolve a typebound function, or 'method'. First separate all
    7444                 :             :    the non-CLASS references by calling resolve_compcall directly.  */
    7445                 :             : 
    7446                 :             : static bool
    7447                 :        1556 : resolve_typebound_function (gfc_expr* e)
    7448                 :             : {
    7449                 :        1556 :   gfc_symbol *declared;
    7450                 :        1556 :   gfc_component *c;
    7451                 :        1556 :   gfc_ref *new_ref;
    7452                 :        1556 :   gfc_ref *class_ref;
    7453                 :        1556 :   gfc_symtree *st;
    7454                 :        1556 :   const char *name;
    7455                 :        1556 :   gfc_typespec ts;
    7456                 :        1556 :   gfc_expr *expr;
    7457                 :        1556 :   bool overridable;
    7458                 :             : 
    7459                 :        1556 :   st = e->symtree;
    7460                 :             : 
    7461                 :             :   /* Deal with typebound operators for CLASS objects.  */
    7462                 :        1556 :   expr = e->value.compcall.base_object;
    7463                 :        1556 :   overridable = !e->value.compcall.tbp->non_overridable;
    7464                 :        1556 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    7465                 :             :     {
    7466                 :             :       /* Since the typebound operators are generic, we have to ensure
    7467                 :             :          that any delays in resolution are corrected and that the vtab
    7468                 :             :          is present.  */
    7469                 :         184 :       ts = expr->ts;
    7470                 :         184 :       declared = ts.u.derived;
    7471                 :         184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7472                 :         184 :       if (c->ts.u.derived == NULL)
    7473                 :           0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7474                 :             : 
    7475                 :         184 :       if (!resolve_compcall (e, &name))
    7476                 :             :         return false;
    7477                 :             : 
    7478                 :             :       /* Use the generic name if it is there.  */
    7479                 :         184 :       name = name ? name : e->value.function.esym->name;
    7480                 :         184 :       e->symtree = expr->symtree;
    7481                 :         184 :       e->ref = gfc_copy_ref (expr->ref);
    7482                 :         184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    7483                 :             : 
    7484                 :             :       /* Trim away the extraneous references that emerge from nested
    7485                 :             :          use of interface.cc (extend_expr).  */
    7486                 :         184 :       if (class_ref && class_ref->next)
    7487                 :             :         {
    7488                 :           0 :           gfc_free_ref_list (class_ref->next);
    7489                 :           0 :           class_ref->next = NULL;
    7490                 :             :         }
    7491                 :         184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    7492                 :             :         {
    7493                 :           0 :           gfc_free_ref_list (e->ref);
    7494                 :           0 :           e->ref = NULL;
    7495                 :             :         }
    7496                 :             : 
    7497                 :         184 :       gfc_add_vptr_component (e);
    7498                 :         184 :       gfc_add_component_ref (e, name);
    7499                 :         184 :       e->value.function.esym = NULL;
    7500                 :         184 :       if (expr->expr_type != EXPR_VARIABLE)
    7501                 :          80 :         e->base_expr = expr;
    7502                 :         184 :       return true;
    7503                 :             :     }
    7504                 :             : 
    7505                 :        1372 :   if (st == NULL)
    7506                 :         147 :     return resolve_compcall (e, NULL);
    7507                 :             : 
    7508                 :        1225 :   if (!gfc_resolve_ref (e))
    7509                 :             :     return false;
    7510                 :             : 
    7511                 :             :   /* Get the CLASS declared type.  */
    7512                 :        1225 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7513                 :             : 
    7514                 :        1225 :   if (!resolve_fl_derived (declared))
    7515                 :             :     return false;
    7516                 :             : 
    7517                 :             :   /* Weed out cases of the ultimate component being a derived type.  */
    7518                 :        1225 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7519                 :        1141 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7520                 :             :     {
    7521                 :         538 :       gfc_free_ref_list (new_ref);
    7522                 :         538 :       return resolve_compcall (e, NULL);
    7523                 :             :     }
    7524                 :             : 
    7525                 :         687 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    7526                 :             : 
    7527                 :             :   /* Treat the call as if it is a typebound procedure, in order to roll
    7528                 :             :      out the correct name for the specific function.  */
    7529                 :         687 :   if (!resolve_compcall (e, &name))
    7530                 :             :     {
    7531                 :          15 :       gfc_free_ref_list (new_ref);
    7532                 :          15 :       return false;
    7533                 :             :     }
    7534                 :         672 :   ts = e->ts;
    7535                 :             : 
    7536                 :         672 :   if (overridable)
    7537                 :             :     {
    7538                 :             :       /* Convert the expression to a procedure pointer component call.  */
    7539                 :         670 :       e->value.function.esym = NULL;
    7540                 :         670 :       e->symtree = st;
    7541                 :             : 
    7542                 :         670 :       if (new_ref)
    7543                 :         124 :         e->ref = new_ref;
    7544                 :             : 
    7545                 :             :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7546                 :         670 :       gfc_add_vptr_component (e);
    7547                 :         670 :       gfc_add_component_ref (e, name);
    7548                 :             : 
    7549                 :             :       /* Recover the typespec for the expression.  This is really only
    7550                 :             :         necessary for generic procedures, where the additional call
    7551                 :             :         to gfc_add_component_ref seems to throw the collection of the
    7552                 :             :         correct typespec.  */
    7553                 :         670 :       e->ts = ts;
    7554                 :             :     }
    7555                 :           2 :   else if (new_ref)
    7556                 :           0 :     gfc_free_ref_list (new_ref);
    7557                 :             : 
    7558                 :             :   return true;
    7559                 :             : }
    7560                 :             : 
    7561                 :             : /* Resolve a typebound subroutine, or 'method'. First separate all
    7562                 :             :    the non-CLASS references by calling resolve_typebound_call
    7563                 :             :    directly.  */
    7564                 :             : 
    7565                 :             : static bool
    7566                 :        1675 : resolve_typebound_subroutine (gfc_code *code)
    7567                 :             : {
    7568                 :        1675 :   gfc_symbol *declared;
    7569                 :        1675 :   gfc_component *c;
    7570                 :        1675 :   gfc_ref *new_ref;
    7571                 :        1675 :   gfc_ref *class_ref;
    7572                 :        1675 :   gfc_symtree *st;
    7573                 :        1675 :   const char *name;
    7574                 :        1675 :   gfc_typespec ts;
    7575                 :        1675 :   gfc_expr *expr;
    7576                 :        1675 :   bool overridable;
    7577                 :             : 
    7578                 :        1675 :   st = code->expr1->symtree;
    7579                 :             : 
    7580                 :             :   /* Deal with typebound operators for CLASS objects.  */
    7581                 :        1675 :   expr = code->expr1->value.compcall.base_object;
    7582                 :        1675 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7583                 :        1675 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    7584                 :             :     {
    7585                 :             :       /* If the base_object is not a variable, the corresponding actual
    7586                 :             :          argument expression must be stored in e->base_expression so
    7587                 :             :          that the corresponding tree temporary can be used as the base
    7588                 :             :          object in gfc_conv_procedure_call.  */
    7589                 :         103 :       if (expr->expr_type != EXPR_VARIABLE)
    7590                 :             :         {
    7591                 :             :           gfc_actual_arglist *args;
    7592                 :             : 
    7593                 :             :           args= code->expr1->value.function.actual;
    7594                 :             :           for (; args; args = args->next)
    7595                 :             :             if (expr == args->expr)
    7596                 :             :               expr = args->expr;
    7597                 :             :         }
    7598                 :             : 
    7599                 :             :       /* Since the typebound operators are generic, we have to ensure
    7600                 :             :          that any delays in resolution are corrected and that the vtab
    7601                 :             :          is present.  */
    7602                 :         103 :       declared = expr->ts.u.derived;
    7603                 :         103 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7604                 :         103 :       if (c->ts.u.derived == NULL)
    7605                 :           0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7606                 :             : 
    7607                 :         103 :       if (!resolve_typebound_call (code, &name, NULL))
    7608                 :             :         return false;
    7609                 :             : 
    7610                 :             :       /* Use the generic name if it is there.  */
    7611                 :         103 :       name = name ? name : code->expr1->value.function.esym->name;
    7612                 :         103 :       code->expr1->symtree = expr->symtree;
    7613                 :         103 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    7614                 :             : 
    7615                 :             :       /* Trim away the extraneous references that emerge from nested
    7616                 :             :          use of interface.cc (extend_expr).  */
    7617                 :         103 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    7618                 :         103 :       if (class_ref && class_ref->next)
    7619                 :             :         {
    7620                 :           0 :           gfc_free_ref_list (class_ref->next);
    7621                 :           0 :           class_ref->next = NULL;
    7622                 :             :         }
    7623                 :         103 :       else if (code->expr1->ref && !class_ref)
    7624                 :             :         {
    7625                 :          12 :           gfc_free_ref_list (code->expr1->ref);
    7626                 :          12 :           code->expr1->ref = NULL;
    7627                 :             :         }
    7628                 :             : 
    7629                 :             :       /* Now use the procedure in the vtable.  */
    7630                 :         103 :       gfc_add_vptr_component (code->expr1);
    7631                 :         103 :       gfc_add_component_ref (code->expr1, name);
    7632                 :         103 :       code->expr1->value.function.esym = NULL;
    7633                 :         103 :       if (expr->expr_type != EXPR_VARIABLE)
    7634                 :           0 :         code->expr1->base_expr = expr;
    7635                 :         103 :       return true;
    7636                 :             :     }
    7637                 :             : 
    7638                 :        1572 :   if (st == NULL)
    7639                 :         313 :     return resolve_typebound_call (code, NULL, NULL);
    7640                 :             : 
    7641                 :        1259 :   if (!gfc_resolve_ref (code->expr1))
    7642                 :             :     return false;
    7643                 :             : 
    7644                 :             :   /* Get the CLASS declared type.  */
    7645                 :        1259 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    7646                 :             : 
    7647                 :             :   /* Weed out cases of the ultimate component being a derived type.  */
    7648                 :        1259 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7649                 :        1196 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7650                 :             :     {
    7651                 :         884 :       gfc_free_ref_list (new_ref);
    7652                 :         884 :       return resolve_typebound_call (code, NULL, NULL);
    7653                 :             :     }
    7654                 :             : 
    7655                 :         375 :   if (!resolve_typebound_call (code, &name, &overridable))
    7656                 :             :     {
    7657                 :           5 :       gfc_free_ref_list (new_ref);
    7658                 :           5 :       return false;
    7659                 :             :     }
    7660                 :         370 :   ts = code->expr1->ts;
    7661                 :             : 
    7662                 :         370 :   if (overridable)
    7663                 :             :     {
    7664                 :             :       /* Convert the expression to a procedure pointer component call.  */
    7665                 :         368 :       code->expr1->value.function.esym = NULL;
    7666                 :         368 :       code->expr1->symtree = st;
    7667                 :             : 
    7668                 :         368 :       if (new_ref)
    7669                 :          92 :         code->expr1->ref = new_ref;
    7670                 :             : 
    7671                 :             :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7672                 :         368 :       gfc_add_vptr_component (code->expr1);
    7673                 :         368 :       gfc_add_component_ref (code->expr1, name);
    7674                 :             : 
    7675                 :             :       /* Recover the typespec for the expression.  This is really only
    7676                 :             :         necessary for generic procedures, where the additional call
    7677                 :             :         to gfc_add_component_ref seems to throw the collection of the
    7678                 :             :         correct typespec.  */
    7679                 :         368 :       code->expr1->ts = ts;
    7680                 :             :     }
    7681                 :           2 :   else if (new_ref)
    7682                 :           0 :     gfc_free_ref_list (new_ref);
    7683                 :             : 
    7684                 :             :   return true;
    7685                 :             : }
    7686                 :             : 
    7687                 :             : 
    7688                 :             : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    7689                 :             : 
    7690                 :             : static bool
    7691                 :         123 : resolve_ppc_call (gfc_code* c)
    7692                 :             : {
    7693                 :         123 :   gfc_component *comp;
    7694                 :             : 
    7695                 :         123 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    7696                 :         123 :   gcc_assert (comp != NULL);
    7697                 :             : 
    7698                 :         123 :   c->resolved_sym = c->expr1->symtree->n.sym;
    7699                 :         123 :   c->expr1->expr_type = EXPR_VARIABLE;
    7700                 :             : 
    7701                 :         123 :   if (!comp->attr.subroutine)
    7702                 :           1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    7703                 :             : 
    7704                 :         123 :   if (!gfc_resolve_ref (c->expr1))
    7705                 :             :     return false;
    7706                 :             : 
    7707                 :         123 :   if (!update_ppc_arglist (c->expr1))
    7708                 :             :     return false;
    7709                 :             : 
    7710                 :         122 :   c->ext.actual = c->expr1->value.compcall.actual;
    7711                 :             : 
    7712                 :         122 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    7713                 :         122 :                                !(comp->ts.interface
    7714                 :          93 :                                  && comp->ts.interface->formal)))
    7715                 :             :     return false;
    7716                 :             : 
    7717                 :         122 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    7718                 :             :     return false;
    7719                 :             : 
    7720                 :         121 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    7721                 :             : 
    7722                 :         121 :   return true;
    7723                 :             : }
    7724                 :             : 
    7725                 :             : 
    7726                 :             : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    7727                 :             : 
    7728                 :             : static bool
    7729                 :         276 : resolve_expr_ppc (gfc_expr* e)
    7730                 :             : {
    7731                 :         276 :   gfc_component *comp;
    7732                 :             : 
    7733                 :         276 :   comp = gfc_get_proc_ptr_comp (e);
    7734                 :         276 :   gcc_assert (comp != NULL);
    7735                 :             : 
    7736                 :             :   /* Convert to EXPR_FUNCTION.  */
    7737                 :         276 :   e->expr_type = EXPR_FUNCTION;
    7738                 :         276 :   e->value.function.isym = NULL;
    7739                 :         276 :   e->value.function.actual = e->value.compcall.actual;
    7740                 :         276 :   e->ts = comp->ts;
    7741                 :         276 :   if (comp->as != NULL)
    7742                 :             :     {
    7743                 :          28 :       e->rank = comp->as->rank;
    7744                 :          28 :       e->corank = comp->as->corank;
    7745                 :             :     }
    7746                 :             : 
    7747                 :         276 :   if (!comp->attr.function)
    7748                 :           3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    7749                 :             : 
    7750                 :         276 :   if (!gfc_resolve_ref (e))
    7751                 :             :     return false;
    7752                 :             : 
    7753                 :         276 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    7754                 :         276 :                                !(comp->ts.interface
    7755                 :         275 :                                  && comp->ts.interface->formal)))
    7756                 :             :     return false;
    7757                 :             : 
    7758                 :         276 :   if (!update_ppc_arglist (e))
    7759                 :             :     return false;
    7760                 :             : 
    7761                 :         274 :   if (!check_pure_function(e))
    7762                 :             :     return false;
    7763                 :             : 
    7764                 :         273 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    7765                 :             : 
    7766                 :         273 :   return true;
    7767                 :             : }
    7768                 :             : 
    7769                 :             : 
    7770                 :             : static bool
    7771                 :       11575 : gfc_is_expandable_expr (gfc_expr *e)
    7772                 :             : {
    7773                 :       11575 :   gfc_constructor *con;
    7774                 :             : 
    7775                 :       11575 :   if (e->expr_type == EXPR_ARRAY)
    7776                 :             :     {
    7777                 :             :       /* Traverse the constructor looking for variables that are flavor
    7778                 :             :          parameter.  Parameters must be expanded since they are fully used at
    7779                 :             :          compile time.  */
    7780                 :       11575 :       con = gfc_constructor_first (e->value.constructor);
    7781                 :       30834 :       for (; con; con = gfc_constructor_next (con))
    7782                 :             :         {
    7783                 :       13255 :           if (con->expr->expr_type == EXPR_VARIABLE
    7784                 :        4904 :               && con->expr->symtree
    7785                 :        4904 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    7786                 :        4849 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    7787                 :             :             return true;
    7788                 :        8351 :           if (con->expr->expr_type == EXPR_ARRAY
    7789                 :        8351 :               && gfc_is_expandable_expr (con->expr))
    7790                 :             :             return true;
    7791                 :             :         }
    7792                 :             :     }
    7793                 :             : 
    7794                 :             :   return false;
    7795                 :             : }
    7796                 :             : 
    7797                 :             : 
    7798                 :             : /* Sometimes variables in specification expressions of the result
    7799                 :             :    of module procedures in submodules wind up not being the 'real'
    7800                 :             :    dummy.  Find this, if possible, in the namespace of the first
    7801                 :             :    formal argument.  */
    7802                 :             : 
    7803                 :             : static void
    7804                 :        3340 : fixup_unique_dummy (gfc_expr *e)
    7805                 :             : {
    7806                 :        3340 :   gfc_symtree *st = NULL;
    7807                 :        3340 :   gfc_symbol *s = NULL;
    7808                 :             : 
    7809                 :        3340 :   if (e->symtree->n.sym->ns->proc_name
    7810                 :        3340 :       && e->symtree->n.sym->ns->proc_name->formal)
    7811                 :        3340 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    7812                 :             : 
    7813                 :        3340 :   if (s != NULL)
    7814                 :        3340 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    7815                 :             : 
    7816                 :        3340 :   if (st != NULL
    7817                 :          14 :       && st->n.sym != NULL
    7818                 :          14 :       && st->n.sym->attr.dummy)
    7819                 :          14 :     e->symtree = st;
    7820                 :        3340 : }
    7821                 :             : 
    7822                 :             : /* Resolve an expression.  That is, make sure that types of operands agree
    7823                 :             :    with their operators, intrinsic operators are converted to function calls
    7824                 :             :    for overloaded types and unresolved function references are resolved.  */
    7825                 :             : 
    7826                 :             : bool
    7827                 :     6823458 : gfc_resolve_expr (gfc_expr *e)
    7828                 :             : {
    7829                 :     6823458 :   bool t;
    7830                 :     6823458 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    7831                 :             : 
    7832                 :     6823458 :   if (e == NULL || e->do_not_resolve_again)
    7833                 :             :     return true;
    7834                 :             : 
    7835                 :             :   /* inquiry_argument only applies to variables.  */
    7836                 :     4986448 :   inquiry_save = inquiry_argument;
    7837                 :     4986448 :   actual_arg_save = actual_arg;
    7838                 :     4986448 :   first_actual_arg_save = first_actual_arg;
    7839                 :             : 
    7840                 :     4986448 :   if (e->expr_type != EXPR_VARIABLE)
    7841                 :             :     {
    7842                 :     3712979 :       inquiry_argument = false;
    7843                 :     3712979 :       actual_arg = false;
    7844                 :     3712979 :       first_actual_arg = false;
    7845                 :             :     }
    7846                 :     1273469 :   else if (e->symtree != NULL
    7847                 :     1273050 :            && *e->symtree->name == '@'
    7848                 :        4047 :            && e->symtree->n.sym->attr.dummy)
    7849                 :             :     {
    7850                 :             :       /* Deal with submodule specification expressions that are not
    7851                 :             :          found to be referenced in module.cc(read_cleanup).  */
    7852                 :        3340 :       fixup_unique_dummy (e);
    7853                 :             :     }
    7854                 :             : 
    7855                 :     4986448 :   switch (e->expr_type)
    7856                 :             :     {
    7857                 :      511582 :     case EXPR_OP:
    7858                 :      511582 :       t = resolve_operator (e);
    7859                 :      511582 :       break;
    7860                 :             : 
    7861                 :     1600226 :     case EXPR_FUNCTION:
    7862                 :     1600226 :     case EXPR_VARIABLE:
    7863                 :             : 
    7864                 :     1600226 :       if (check_host_association (e))
    7865                 :      326793 :         t = resolve_function (e);
    7866                 :             :       else
    7867                 :     1273433 :         t = resolve_variable (e);
    7868                 :             : 
    7869                 :     1600226 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    7870                 :        6756 :           && e->ref->type != REF_SUBSTRING)
    7871                 :        2056 :         gfc_resolve_substring_charlen (e);
    7872                 :             : 
    7873                 :             :       break;
    7874                 :             : 
    7875                 :        1556 :     case EXPR_COMPCALL:
    7876                 :        1556 :       t = resolve_typebound_function (e);
    7877                 :        1556 :       break;
    7878                 :             : 
    7879                 :         507 :     case EXPR_SUBSTRING:
    7880                 :         507 :       t = gfc_resolve_ref (e);
    7881                 :         507 :       break;
    7882                 :             : 
    7883                 :             :     case EXPR_CONSTANT:
    7884                 :             :     case EXPR_NULL:
    7885                 :             :       t = true;
    7886                 :             :       break;
    7887                 :             : 
    7888                 :         276 :     case EXPR_PPC:
    7889                 :         276 :       t = resolve_expr_ppc (e);
    7890                 :         276 :       break;
    7891                 :             : 
    7892                 :       66656 :     case EXPR_ARRAY:
    7893                 :       66656 :       t = false;
    7894                 :       66656 :       if (!gfc_resolve_ref (e))
    7895                 :             :         break;
    7896                 :             : 
    7897                 :       66656 :       t = gfc_resolve_array_constructor (e);
    7898                 :             :       /* Also try to expand a constructor.  */
    7899                 :       66656 :       if (t)
    7900                 :             :         {
    7901                 :       66562 :           gfc_expression_rank (e);
    7902                 :       66562 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    7903                 :       61629 :             gfc_expand_constructor (e, false);
    7904                 :             :         }
    7905                 :             : 
    7906                 :             :       /* This provides the opportunity for the length of constructors with
    7907                 :             :          character valued function elements to propagate the string length
    7908                 :             :          to the expression.  */
    7909                 :       66562 :       if (t && e->ts.type == BT_CHARACTER)
    7910                 :             :         {
    7911                 :             :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    7912                 :             :              here rather then add a duplicate test for it above.  */
    7913                 :        9828 :           gfc_expand_constructor (e, false);
    7914                 :        9828 :           t = gfc_resolve_character_array_constructor (e);
    7915                 :             :         }
    7916                 :             : 
    7917                 :             :       break;
    7918                 :             : 
    7919                 :       15237 :     case EXPR_STRUCTURE:
    7920                 :       15237 :       t = gfc_resolve_ref (e);
    7921                 :       15237 :       if (!t)
    7922                 :             :         break;
    7923                 :             : 
    7924                 :       15237 :       t = resolve_structure_cons (e, 0);
    7925                 :       15237 :       if (!t)
    7926                 :             :         break;
    7927                 :             : 
    7928                 :       15225 :       t = gfc_simplify_expr (e, 0);
    7929                 :       15225 :       break;
    7930                 :             : 
    7931                 :           0 :     default:
    7932                 :           0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    7933                 :             :     }
    7934                 :             : 
    7935                 :     4986448 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    7936                 :      175067 :     fixup_charlen (e);
    7937                 :             : 
    7938                 :     4986448 :   inquiry_argument = inquiry_save;
    7939                 :     4986448 :   actual_arg = actual_arg_save;
    7940                 :     4986448 :   first_actual_arg = first_actual_arg_save;
    7941                 :             : 
    7942                 :             :   /* For some reason, resolving these expressions a second time mangles
    7943                 :             :      the typespec of the expression itself.  */
    7944                 :     4986448 :   if (t && e->expr_type == EXPR_VARIABLE
    7945                 :     1270624 :       && e->symtree->n.sym->attr.select_rank_temporary
    7946                 :        3422 :       && UNLIMITED_POLY (e->symtree->n.sym))
    7947                 :          83 :     e->do_not_resolve_again = 1;
    7948                 :             : 
    7949                 :             :   return t;
    7950                 :             : }
    7951                 :             : 
    7952                 :             : 
    7953                 :             : /* Resolve an expression from an iterator.  They must be scalar and have
    7954                 :             :    INTEGER or (optionally) REAL type.  */
    7955                 :             : 
    7956                 :             : static bool
    7957                 :      148309 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    7958                 :             :                            const char *name_msgid)
    7959                 :             : {
    7960                 :      148309 :   if (!gfc_resolve_expr (expr))
    7961                 :             :     return false;
    7962                 :             : 
    7963                 :      148304 :   if (expr->rank != 0)
    7964                 :             :     {
    7965                 :           0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    7966                 :           0 :       return false;
    7967                 :             :     }
    7968                 :             : 
    7969                 :      148304 :   if (expr->ts.type != BT_INTEGER)
    7970                 :             :     {
    7971                 :         274 :       if (expr->ts.type == BT_REAL)
    7972                 :             :         {
    7973                 :         274 :           if (real_ok)
    7974                 :         271 :             return gfc_notify_std (GFC_STD_F95_DEL,
    7975                 :             :                                    "%s at %L must be integer",
    7976                 :         271 :                                    _(name_msgid), &expr->where);
    7977                 :             :           else
    7978                 :             :             {
    7979                 :           3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    7980                 :             :                          &expr->where);
    7981                 :           3 :               return false;
    7982                 :             :             }
    7983                 :             :         }
    7984                 :             :       else
    7985                 :             :         {
    7986                 :           0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    7987                 :           0 :           return false;
    7988                 :             :         }
    7989                 :             :     }
    7990                 :             :   return true;
    7991                 :             : }
    7992                 :             : 
    7993                 :             : 
    7994                 :             : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    7995                 :             :    false allow only INTEGER type iterators, otherwise allow REAL types.
    7996                 :             :    Set own_scope to true for ac-implied-do and data-implied-do as those
    7997                 :             :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    7998                 :             : 
    7999                 :             : bool
    8000                 :       37086 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8001                 :             : {
    8002                 :       37086 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8003                 :             :     return false;
    8004                 :             : 
    8005                 :       37082 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8006                 :       37082 :                                  _("iterator variable")))
    8007                 :             :     return false;
    8008                 :             : 
    8009                 :       37076 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8010                 :             :                                   "Start expression in DO loop"))
    8011                 :             :     return false;
    8012                 :             : 
    8013                 :       37075 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8014                 :             :                                   "End expression in DO loop"))
    8015                 :             :     return false;
    8016                 :             : 
    8017                 :       37072 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8018                 :             :                                   "Step expression in DO loop"))
    8019                 :             :     return false;
    8020                 :             : 
    8021                 :             :   /* Convert start, end, and step to the same type as var.  */
    8022                 :       37071 :   if (iter->start->ts.kind != iter->var->ts.kind
    8023                 :       37071 :       || iter->start->ts.type != iter->var->ts.type)
    8024                 :         315 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8025                 :             : 
    8026                 :       37071 :   if (iter->end->ts.kind != iter->var->ts.kind
    8027                 :       37071 :       || iter->end->ts.type != iter->var->ts.type)
    8028                 :         278 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8029                 :             : 
    8030                 :       37071 :   if (iter->step->ts.kind != iter->var->ts.kind
    8031                 :       37071 :       || iter->step->ts.type != iter->var->ts.type)
    8032                 :         280 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8033                 :             : 
    8034                 :       37071 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8035                 :             :     {
    8036                 :       35949 :       if ((iter->step->ts.type == BT_INTEGER
    8037                 :       35866 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8038                 :       71813 :           || (iter->step->ts.type == BT_REAL
    8039                 :          83 :               && mpfr_sgn (iter->step->value.real) == 0))
    8040                 :             :         {
    8041                 :           3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8042                 :           3 :                      &iter->step->where);
    8043                 :           3 :           return false;
    8044                 :             :         }
    8045                 :             :     }
    8046                 :             : 
    8047                 :       37068 :   if (iter->start->expr_type == EXPR_CONSTANT
    8048                 :       34023 :       && iter->end->expr_type == EXPR_CONSTANT
    8049                 :       26672 :       && iter->step->expr_type == EXPR_CONSTANT)
    8050                 :             :     {
    8051                 :       26405 :       int sgn, cmp;
    8052                 :       26405 :       if (iter->start->ts.type == BT_INTEGER)
    8053                 :             :         {
    8054                 :       26351 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8055                 :       26351 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8056                 :             :         }
    8057                 :             :       else
    8058                 :             :         {
    8059                 :          54 :           sgn = mpfr_sgn (iter->step->value.real);
    8060                 :          54 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8061                 :             :         }
    8062                 :       26405 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8063                 :         146 :         gfc_warning (OPT_Wzerotrip,
    8064                 :             :                      "DO loop at %L will be executed zero times",
    8065                 :         146 :                      &iter->step->where);
    8066                 :             :     }
    8067                 :             : 
    8068                 :       37068 :   if (iter->end->expr_type == EXPR_CONSTANT
    8069                 :       27037 :       && iter->end->ts.type == BT_INTEGER
    8070                 :       26983 :       && iter->step->expr_type == EXPR_CONSTANT
    8071                 :       26673 :       && iter->step->ts.type == BT_INTEGER
    8072                 :       26673 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8073                 :       26305 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8074                 :             :     {
    8075                 :       25559 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8076                 :       25559 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8077                 :             : 
    8078                 :       25559 :       if (is_step_positive
    8079                 :       25191 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8080                 :           7 :         gfc_warning (OPT_Wundefined_do_loop,
    8081                 :             :                      "DO loop at %L is undefined as it overflows",
    8082                 :           7 :                      &iter->step->where);
    8083                 :             :       else if (!is_step_positive
    8084                 :         368 :                && mpz_cmp (iter->end->value.integer,
    8085                 :         368 :                            gfc_integer_kinds[k].min_int) == 0)
    8086                 :           7 :         gfc_warning (OPT_Wundefined_do_loop,
    8087                 :             :                      "DO loop at %L is undefined as it underflows",
    8088                 :           7 :                      &iter->step->where);
    8089                 :             :     }
    8090                 :             : 
    8091                 :             :   return true;
    8092                 :             : }
    8093                 :             : 
    8094                 :             : 
    8095                 :             : /* Traversal function for find_forall_index.  f == 2 signals that
    8096                 :             :    that variable itself is not to be checked - only the references.  */
    8097                 :             : 
    8098                 :             : static bool
    8099                 :       41673 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8100                 :             : {
    8101                 :       41673 :   if (expr->expr_type != EXPR_VARIABLE)
    8102                 :             :     return false;
    8103                 :             : 
    8104                 :             :   /* A scalar assignment  */
    8105                 :       17614 :   if (!expr->ref || *f == 1)
    8106                 :             :     {
    8107                 :       11914 :       if (expr->symtree->n.sym == sym)
    8108                 :             :         return true;
    8109                 :             :       else
    8110                 :             :         return false;
    8111                 :             :     }
    8112                 :             : 
    8113                 :        5700 :   if (*f == 2)
    8114                 :        1730 :     *f = 1;
    8115                 :             :   return false;
    8116                 :             : }
    8117                 :             : 
    8118                 :             : 
    8119                 :             : /* Check whether the FORALL index appears in the expression or not.
    8120                 :             :    Returns true if SYM is found in EXPR.  */
    8121                 :             : 
    8122                 :             : bool
    8123                 :       26375 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8124                 :             : {
    8125                 :       26375 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8126                 :             :     return true;
    8127                 :             :   else
    8128                 :             :     return false;
    8129                 :             : }
    8130                 :             : 
    8131                 :             : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8132                 :             :    This constraint specifies rules for variables in locality-specs.  */
    8133                 :             : 
    8134                 :             : static int
    8135                 :         582 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8136                 :             : {
    8137                 :         582 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8138                 :             : 
    8139                 :         582 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8140                 :             :     {
    8141                 :          18 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8142                 :          18 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8143                 :          20 :            list; list = list->next)
    8144                 :             :         {
    8145                 :           2 :           if (list->expr->symtree->n.sym == sym)
    8146                 :             :             {
    8147                 :           0 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8148                 :             :                          "must not appear in LOCAL locality-spec at %L",
    8149                 :             :                          sym->name, &(*expr)->where, &list->expr->where);
    8150                 :           0 :               *walk_subtrees = 0;
    8151                 :           0 :               return 1;
    8152                 :             :             }
    8153                 :             :         }
    8154                 :             :     }
    8155                 :             : 
    8156                 :         582 :     *walk_subtrees = 1;
    8157                 :         582 :     return 0;
    8158                 :             : }
    8159                 :             : 
    8160                 :             : static int
    8161                 :        3773 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8162                 :             : {
    8163                 :        3773 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8164                 :             : 
    8165                 :        3773 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8166                 :             :     {
    8167                 :        1697 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8168                 :             : 
    8169                 :        1697 :       if (d->sym_hash->contains (sym))
    8170                 :        1262 :         sym->mark = 1;
    8171                 :             : 
    8172                 :         435 :       else if (d->default_none)
    8173                 :             :         {
    8174                 :           3 :           gfc_namespace *ns2 = d->ns;
    8175                 :           5 :           while (ns2)
    8176                 :             :             {
    8177                 :           3 :               if (ns2 == sym->ns)
    8178                 :             :                 break;
    8179                 :           2 :               ns2 = ns2->parent;
    8180                 :             :             }
    8181                 :           3 :           if (ns2 != NULL)
    8182                 :             :             {
    8183                 :           1 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8184                 :             :                         "of DO CONCURRENT at %L but required due to "
    8185                 :             :                         "DEFAULT (NONE)",
    8186                 :           1 :                         sym->name, &(*e)->where, &d->code->loc);
    8187                 :           1 :               d->sym_hash->add (sym);
    8188                 :             :             }
    8189                 :             :         }
    8190                 :             :     }
    8191                 :        3773 :   return 0;
    8192                 :             : }
    8193                 :             : 
    8194                 :             : static void
    8195                 :         174 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8196                 :             : {
    8197                 :         174 :   struct check_default_none_data data;
    8198                 :         174 :   data.code = code;
    8199                 :         174 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8200                 :         174 :   data.ns = ns;
    8201                 :         174 :   data.default_none = code->ext.concur.default_none;
    8202                 :             : 
    8203                 :         870 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8204                 :             :     {
    8205                 :         696 :       const char *name;
    8206                 :         696 :       switch (locality)
    8207                 :             :         {
    8208                 :             :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8209                 :         174 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8210                 :         174 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8211                 :         174 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8212                 :             :           default: gcc_unreachable ();
    8213                 :             :         }
    8214                 :             : 
    8215                 :        1079 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8216                 :         383 :            list = list->next)
    8217                 :             :         {
    8218                 :         383 :           gfc_expr *expr = list->expr;
    8219                 :             : 
    8220                 :         383 :           if (locality == LOCALITY_REDUCE
    8221                 :          72 :               && (expr->expr_type == EXPR_FUNCTION
    8222                 :          48 :                   || expr->expr_type == EXPR_OP))
    8223                 :          35 :             continue;
    8224                 :             : 
    8225                 :         359 :           if (!gfc_resolve_expr (expr))
    8226                 :           3 :             continue;
    8227                 :             : 
    8228                 :         356 :           if (expr->expr_type != EXPR_VARIABLE
    8229                 :         356 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8230                 :         356 :               || (expr->ref
    8231                 :         147 :                   && (expr->ref->type != REF_ARRAY
    8232                 :         147 :                       || expr->ref->u.ar.type != AR_FULL
    8233                 :         143 :                       || expr->ref->next)))
    8234                 :             :             {
    8235                 :           4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8236                 :             :                          name, &expr->where);
    8237                 :           4 :                 continue;
    8238                 :             :             }
    8239                 :             : 
    8240                 :         352 :           gfc_symbol *sym = expr->symtree->n.sym;
    8241                 :             : 
    8242                 :         352 :           if (data.sym_hash->contains (sym))
    8243                 :             :             {
    8244                 :           4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8245                 :             :                          "locality-spec", sym->name, &expr->where);
    8246                 :           4 :               continue;
    8247                 :             :             }
    8248                 :             : 
    8249                 :         348 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8250                 :         696 :                iter; iter = iter->next)
    8251                 :             :             {
    8252                 :         348 :               if (iter->var->symtree->n.sym == sym)
    8253                 :             :                 {
    8254                 :           0 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8255                 :             :                              "locality-spec", sym->name, &expr->where);
    8256                 :           0 :                   continue;
    8257                 :             :                 }
    8258                 :             : 
    8259                 :         348 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8260                 :             :             }
    8261                 :             : 
    8262                 :         348 :           if (locality == LOCALITY_LOCAL
    8263                 :         348 :               || locality == LOCALITY_LOCAL_INIT
    8264                 :         348 :               || locality == LOCALITY_REDUCE)
    8265                 :             :             {
    8266                 :         194 :               if (sym->attr.optional)
    8267                 :           3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8268                 :             :                            "locality-spec at %L",
    8269                 :             :                            sym->name, name, &expr->where);
    8270                 :             : 
    8271                 :         194 :               if (sym->attr.dimension
    8272                 :          66 :                   && sym->as
    8273                 :          66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8274                 :           0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8275                 :             :                            "locality-spec at %L",
    8276                 :             :                            sym->name, name, &expr->where);
    8277                 :             : 
    8278                 :         194 :               gfc_check_vardef_context (expr, false, false, false, name);
    8279                 :             :             }
    8280                 :             : 
    8281                 :         194 :           if (locality == LOCALITY_LOCAL
    8282                 :             :               || locality == LOCALITY_LOCAL_INIT)
    8283                 :             :             {
    8284                 :         177 :               symbol_attribute attr = gfc_expr_attr (expr);
    8285                 :             : 
    8286                 :         177 :               if (attr.allocatable)
    8287                 :           2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8288                 :             :                            "locality-spec at %L",
    8289                 :             :                            sym->name, name, &expr->where);
    8290                 :             : 
    8291                 :         175 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8292                 :           2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8293                 :             :                            " for %qs in %s locality-spec at %L",
    8294                 :             :                            sym->name, name, &expr->where);
    8295                 :             : 
    8296                 :         173 :               else if (attr.codimension)
    8297                 :           0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8298                 :             :                            "at %L",
    8299                 :             :                            sym->name, name, &expr->where);
    8300                 :             : 
    8301                 :         173 :               else if (expr->ts.type == BT_DERIVED
    8302                 :         173 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8303                 :           0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8304                 :             :                            "locality-spec at %L",
    8305                 :             :                            sym->name, name, &expr->where);
    8306                 :             : 
    8307                 :         173 :               else if (gfc_has_ultimate_allocatable (expr))
    8308                 :           4 :                 gfc_error ("Type with ultimate allocatable component not "
    8309                 :             :                            "permitted for %qs in %s locality-spec at %L",
    8310                 :             :                            sym->name, name, &expr->where);
    8311                 :             :             }
    8312                 :             : 
    8313                 :         171 :           else if (locality == LOCALITY_REDUCE)
    8314                 :             :             {
    8315                 :          17 :               if (sym->attr.asynchronous)
    8316                 :           1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8317                 :             :                            "REDUCE locality-spec at %L",
    8318                 :             :                            sym->name, &expr->where);
    8319                 :          17 :               if (sym->attr.volatile_)
    8320                 :           1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8321                 :             :                            "locality-spec at %L", sym->name, &expr->where);
    8322                 :             :             }
    8323                 :             : 
    8324                 :         348 :           data.sym_hash->add (sym);
    8325                 :             :         }
    8326                 :             : 
    8327                 :         696 :       if (locality == LOCALITY_LOCAL)
    8328                 :             :         {
    8329                 :         174 :           gcc_assert (locality == 0);
    8330                 :             : 
    8331                 :         174 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8332                 :         359 :                iter; iter = iter->next)
    8333                 :             :             {
    8334                 :         185 :               gfc_expr_walker (&iter->start,
    8335                 :             :                                do_concur_locality_specs_f2023,
    8336                 :             :                                &data);
    8337                 :             : 
    8338                 :         185 :               gfc_expr_walker (&iter->end,
    8339                 :             :                                do_concur_locality_specs_f2023,
    8340                 :             :                                &data);
    8341                 :             : 
    8342                 :         185 :               gfc_expr_walker (&iter->stride,
    8343                 :             :                                do_concur_locality_specs_f2023,
    8344                 :             :                                &data);
    8345                 :             :             }
    8346                 :             : 
    8347                 :         174 :           if (code->expr1)
    8348                 :           7 :             gfc_expr_walker (&code->expr1,
    8349                 :             :                              do_concur_locality_specs_f2023,
    8350                 :             :                              &data);
    8351                 :             :         }
    8352                 :             :     }
    8353                 :             : 
    8354                 :         174 :   gfc_expr *reduce_op = NULL;
    8355                 :             : 
    8356                 :         174 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8357                 :         222 :        list; list = list->next)
    8358                 :             :     {
    8359                 :          48 :       gfc_expr *expr = list->expr;
    8360                 :             : 
    8361                 :          48 :       if (expr->expr_type != EXPR_VARIABLE)
    8362                 :             :         {
    8363                 :          24 :           reduce_op = expr;
    8364                 :          24 :           continue;
    8365                 :             :         }
    8366                 :             : 
    8367                 :          24 :       if (reduce_op->expr_type == EXPR_OP)
    8368                 :             :         {
    8369                 :          17 :           switch (reduce_op->value.op.op)
    8370                 :             :             {
    8371                 :          17 :               case INTRINSIC_PLUS:
    8372                 :          17 :               case INTRINSIC_TIMES:
    8373                 :          17 :                 if (!gfc_numeric_ts (&expr->ts))
    8374                 :           3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8375                 :           3 :                              "got %s", expr->symtree->n.sym->name,
    8376                 :             :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8377                 :             :                 break;
    8378                 :           0 :               case INTRINSIC_AND:
    8379                 :           0 :               case INTRINSIC_OR:
    8380                 :           0 :               case INTRINSIC_EQV:
    8381                 :           0 :               case INTRINSIC_NEQV:
    8382                 :           0 :                 if (expr->ts.type != BT_LOGICAL)
    8383                 :           0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8384                 :           0 :                              "got %qs", expr->symtree->n.sym->name,
    8385                 :             :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8386                 :             :                 break;
    8387                 :           0 :               default:
    8388                 :           0 :                 gcc_unreachable ();
    8389                 :             :             }
    8390                 :             :         }
    8391                 :             : 
    8392                 :           7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8393                 :             :         {
    8394                 :           7 :           switch (reduce_op->value.function.isym->id)
    8395                 :             :             {
    8396                 :           6 :               case GFC_ISYM_MIN:
    8397                 :           6 :               case GFC_ISYM_MAX:
    8398                 :           6 :                 if (expr->ts.type != BT_INTEGER
    8399                 :             :                     && expr->ts.type != BT_REAL
    8400                 :             :                     && expr->ts.type != BT_CHARACTER)
    8401                 :           2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8402                 :             :                              "in REDUCE with MIN/MAX at %L, got %s",
    8403                 :           2 :                              expr->symtree->n.sym->name, &expr->where,
    8404                 :             :                              gfc_basic_typename (expr->ts.type));
    8405                 :             :                 break;
    8406                 :           1 :               case GFC_ISYM_IAND:
    8407                 :           1 :               case GFC_ISYM_IOR:
    8408                 :           1 :               case GFC_ISYM_IEOR:
    8409                 :           1 :                 if (expr->ts.type != BT_INTEGER)
    8410                 :           1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8411                 :             :                              "IAND/IOR/IEOR at %L, got %s",
    8412                 :           1 :                              expr->symtree->n.sym->name, &expr->where,
    8413                 :             :                              gfc_basic_typename (expr->ts.type));
    8414                 :             :                 break;
    8415                 :           0 :               default:
    8416                 :           0 :                 gcc_unreachable ();
    8417                 :             :             }
    8418                 :             :         }
    8419                 :             : 
    8420                 :             :       else
    8421                 :           0 :         gcc_unreachable ();
    8422                 :             :     }
    8423                 :             : 
    8424                 :         870 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8425                 :             :     {
    8426                 :        1079 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8427                 :         383 :            list = list->next)
    8428                 :             :         {
    8429                 :         383 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8430                 :         359 :             list->expr->symtree->n.sym->mark = 0;
    8431                 :             :         }
    8432                 :             :     }
    8433                 :             : 
    8434                 :         174 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8435                 :             :                    check_default_none_expr, &data);
    8436                 :             : 
    8437                 :         870 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8438                 :             :     {
    8439                 :         696 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8440                 :        1079 :       while (*plist)
    8441                 :             :         {
    8442                 :         383 :           gfc_expr *expr = (*plist)->expr;
    8443                 :         383 :           if (expr->expr_type == EXPR_VARIABLE)
    8444                 :             :             {
    8445                 :         359 :               gfc_symbol *sym = expr->symtree->n.sym;
    8446                 :         359 :               if (sym->mark == 0)
    8447                 :             :                 {
    8448                 :          67 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8449                 :             :                                "locality-spec at %L is not used",
    8450                 :             :                                sym->name, &expr->where);
    8451                 :          67 :                   gfc_expr_list *tmp = *plist;
    8452                 :          67 :                   *plist = (*plist)->next;
    8453                 :          67 :                   gfc_free_expr (tmp->expr);
    8454                 :          67 :                   free (tmp);
    8455                 :          67 :                   continue;
    8456                 :          67 :                 }
    8457                 :             :             }
    8458                 :         316 :           plist = &((*plist)->next);
    8459                 :             :         }
    8460                 :             :     }
    8461                 :         174 : }
    8462                 :             : 
    8463                 :             : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8464                 :             :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8465                 :             :    INTEGERs, and if stride is a constant it must be nonzero.
    8466                 :             :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8467                 :             :    not contain a reference to any index-name in the
    8468                 :             :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8469                 :             : 
    8470                 :             : static void
    8471                 :        2164 : resolve_forall_iterators (gfc_forall_iterator *it)
    8472                 :             : {
    8473                 :        2164 :   gfc_forall_iterator *iter, *iter2;
    8474                 :             : 
    8475                 :        6237 :   for (iter = it; iter; iter = iter->next)
    8476                 :             :     {
    8477                 :        4073 :       if (gfc_resolve_expr (iter->var)
    8478                 :        4073 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8479                 :           0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8480                 :             :                    &iter->var->where);
    8481                 :             : 
    8482                 :        4073 :       if (gfc_resolve_expr (iter->start)
    8483                 :        4073 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8484                 :           0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8485                 :             :                    &iter->start->where);
    8486                 :        4073 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8487                 :           1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8488                 :             : 
    8489                 :        4073 :       if (gfc_resolve_expr (iter->end)
    8490                 :        4073 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8491                 :           0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8492                 :             :                    &iter->end->where);
    8493                 :        4073 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8494                 :           2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8495                 :             : 
    8496                 :        4073 :       if (gfc_resolve_expr (iter->stride))
    8497                 :             :         {
    8498                 :        4073 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8499                 :           0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8500                 :             :                        &iter->stride->where, "INTEGER");
    8501                 :             : 
    8502                 :        4073 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8503                 :        4070 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8504                 :           1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8505                 :             :                        &iter->stride->where);
    8506                 :             :         }
    8507                 :        4073 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8508                 :           1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8509                 :             :     }
    8510                 :             : 
    8511                 :        6237 :   for (iter = it; iter; iter = iter->next)
    8512                 :       10981 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8513                 :             :       {
    8514                 :        6908 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8515                 :        6906 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8516                 :       13812 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8517                 :           6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8518                 :           6 :                      "specification at %L", iter->var->symtree->name,
    8519                 :           6 :                      &iter2->start->where);
    8520                 :             :       }
    8521                 :        2164 : }
    8522                 :             : 
    8523                 :             : 
    8524                 :             : /* Given a pointer to a symbol that is a derived type, see if it's
    8525                 :             :    inaccessible, i.e. if it's defined in another module and the components are
    8526                 :             :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8527                 :             :    inaccessible components are found, nonzero otherwise.  */
    8528                 :             : 
    8529                 :             : static bool
    8530                 :        1311 : derived_inaccessible (gfc_symbol *sym)
    8531                 :             : {
    8532                 :        1311 :   gfc_component *c;
    8533                 :             : 
    8534                 :        1311 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8535                 :             :     return 1;
    8536                 :             : 
    8537                 :        3895 :   for (c = sym->components; c; c = c->next)
    8538                 :             :     {
    8539                 :             :         /* Prevent an infinite loop through this function.  */
    8540                 :        2597 :         if (c->ts.type == BT_DERIVED
    8541                 :         282 :             && (c->attr.pointer || c->attr.allocatable)
    8542                 :          72 :             && sym == c->ts.u.derived)
    8543                 :          72 :           continue;
    8544                 :             : 
    8545                 :        2525 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    8546                 :             :           return 1;
    8547                 :             :     }
    8548                 :             : 
    8549                 :             :   return 0;
    8550                 :             : }
    8551                 :             : 
    8552                 :             : 
    8553                 :             : /* Resolve the argument of a deallocate expression.  The expression must be
    8554                 :             :    a pointer or a full array.  */
    8555                 :             : 
    8556                 :             : static bool
    8557                 :        7783 : resolve_deallocate_expr (gfc_expr *e)
    8558                 :             : {
    8559                 :        7783 :   symbol_attribute attr;
    8560                 :        7783 :   int allocatable, pointer;
    8561                 :        7783 :   gfc_ref *ref;
    8562                 :        7783 :   gfc_symbol *sym;
    8563                 :        7783 :   gfc_component *c;
    8564                 :        7783 :   bool unlimited;
    8565                 :             : 
    8566                 :        7783 :   if (!gfc_resolve_expr (e))
    8567                 :             :     return false;
    8568                 :             : 
    8569                 :        7783 :   if (e->expr_type != EXPR_VARIABLE)
    8570                 :           0 :     goto bad;
    8571                 :             : 
    8572                 :        7783 :   sym = e->symtree->n.sym;
    8573                 :        7783 :   unlimited = UNLIMITED_POLY(sym);
    8574                 :             : 
    8575                 :        7783 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    8576                 :             :     {
    8577                 :        1486 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    8578                 :        1486 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    8579                 :             :     }
    8580                 :             :   else
    8581                 :             :     {
    8582                 :        6297 :       allocatable = sym->attr.allocatable;
    8583                 :        6297 :       pointer = sym->attr.pointer;
    8584                 :             :     }
    8585                 :       15577 :   for (ref = e->ref; ref; ref = ref->next)
    8586                 :             :     {
    8587                 :        7794 :       switch (ref->type)
    8588                 :             :         {
    8589                 :        5802 :         case REF_ARRAY:
    8590                 :        5802 :           if (ref->u.ar.type != AR_FULL
    8591                 :        5990 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    8592                 :         188 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    8593                 :             :             allocatable = 0;
    8594                 :             :           break;
    8595                 :             : 
    8596                 :        1992 :         case REF_COMPONENT:
    8597                 :        1992 :           c = ref->u.c.component;
    8598                 :        1992 :           if (c->ts.type == BT_CLASS)
    8599                 :             :             {
    8600                 :         272 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    8601                 :         272 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    8602                 :             :             }
    8603                 :             :           else
    8604                 :             :             {
    8605                 :        1720 :               allocatable = c->attr.allocatable;
    8606                 :        1720 :               pointer = c->attr.pointer;
    8607                 :             :             }
    8608                 :             :           break;
    8609                 :             : 
    8610                 :             :         case REF_SUBSTRING:
    8611                 :             :         case REF_INQUIRY:
    8612                 :         471 :           allocatable = 0;
    8613                 :             :           break;
    8614                 :             :         }
    8615                 :             :     }
    8616                 :             : 
    8617                 :        7783 :   attr = gfc_expr_attr (e);
    8618                 :             : 
    8619                 :        7783 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    8620                 :             :     {
    8621                 :           3 :     bad:
    8622                 :           3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    8623                 :             :                  &e->where);
    8624                 :           3 :       return false;
    8625                 :             :     }
    8626                 :             : 
    8627                 :             :   /* F2008, C644.  */
    8628                 :        7780 :   if (gfc_is_coindexed (e))
    8629                 :             :     {
    8630                 :           1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    8631                 :           1 :       return false;
    8632                 :             :     }
    8633                 :             : 
    8634                 :        7779 :   if (pointer
    8635                 :       10043 :       && !gfc_check_vardef_context (e, true, true, false,
    8636                 :        2264 :                                     _("DEALLOCATE object")))
    8637                 :             :     return false;
    8638                 :        7777 :   if (!gfc_check_vardef_context (e, false, true, false,
    8639                 :        7777 :                                  _("DEALLOCATE object")))
    8640                 :             :     return false;
    8641                 :             : 
    8642                 :             :   return true;
    8643                 :             : }
    8644                 :             : 
    8645                 :             : 
    8646                 :             : /* Returns true if the expression e contains a reference to the symbol sym.  */
    8647                 :             : static bool
    8648                 :       46521 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    8649                 :             : {
    8650                 :       46521 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    8651                 :        2081 :     return true;
    8652                 :             : 
    8653                 :             :   return false;
    8654                 :             : }
    8655                 :             : 
    8656                 :             : bool
    8657                 :       20027 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    8658                 :             : {
    8659                 :       20027 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    8660                 :             : }
    8661                 :             : 
    8662                 :             : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    8663                 :             :    of character expressions.  */
    8664                 :             : static bool
    8665                 :       20109 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    8666                 :             : {
    8667                 :           0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    8668                 :             : }
    8669                 :             : 
    8670                 :             : 
    8671                 :             : /* Given the expression node e for an allocatable/pointer of derived type to be
    8672                 :             :    allocated, get the expression node to be initialized afterwards (needed for
    8673                 :             :    derived types with default initializers, and derived types with allocatable
    8674                 :             :    components that need nullification.)  */
    8675                 :             : 
    8676                 :             : gfc_expr *
    8677                 :        5419 : gfc_expr_to_initialize (gfc_expr *e)
    8678                 :             : {
    8679                 :        5419 :   gfc_expr *result;
    8680                 :        5419 :   gfc_ref *ref;
    8681                 :        5419 :   int i;
    8682                 :             : 
    8683                 :        5419 :   result = gfc_copy_expr (e);
    8684                 :             : 
    8685                 :             :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    8686                 :       10706 :   for (ref = result->ref; ref; ref = ref->next)
    8687                 :        8376 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    8688                 :             :       {
    8689                 :        3089 :         if (ref->u.ar.dimen == 0
    8690                 :          64 :             && ref->u.ar.as && ref->u.ar.as->corank)
    8691                 :             :           return result;
    8692                 :             : 
    8693                 :        3025 :         ref->u.ar.type = AR_FULL;
    8694                 :             : 
    8695                 :        6839 :         for (i = 0; i < ref->u.ar.dimen; i++)
    8696                 :        3814 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    8697                 :             : 
    8698                 :             :         break;
    8699                 :             :       }
    8700                 :             : 
    8701                 :        5355 :   gfc_free_shape (&result->shape, result->rank);
    8702                 :             : 
    8703                 :             :   /* Recalculate rank, shape, etc.  */
    8704                 :        5355 :   gfc_resolve_expr (result);
    8705                 :        5355 :   return result;
    8706                 :             : }
    8707                 :             : 
    8708                 :             : 
    8709                 :             : /* If the last ref of an expression is an array ref, return a copy of the
    8710                 :             :    expression with that one removed.  Otherwise, a copy of the original
    8711                 :             :    expression.  This is used for allocate-expressions and pointer assignment
    8712                 :             :    LHS, where there may be an array specification that needs to be stripped
    8713                 :             :    off when using gfc_check_vardef_context.  */
    8714                 :             : 
    8715                 :             : static gfc_expr*
    8716                 :       26579 : remove_last_array_ref (gfc_expr* e)
    8717                 :             : {
    8718                 :       26579 :   gfc_expr* e2;
    8719                 :       26579 :   gfc_ref** r;
    8720                 :             : 
    8721                 :       26579 :   e2 = gfc_copy_expr (e);
    8722                 :       33887 :   for (r = &e2->ref; *r; r = &(*r)->next)
    8723                 :       22904 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    8724                 :             :       {
    8725                 :       15596 :         gfc_free_ref_list (*r);
    8726                 :       15596 :         *r = NULL;
    8727                 :       15596 :         break;
    8728                 :             :       }
    8729                 :             : 
    8730                 :       26579 :   return e2;
    8731                 :             : }
    8732                 :             : 
    8733                 :             : 
    8734                 :             : /* Used in resolve_allocate_expr to check that a allocation-object and
    8735                 :             :    a source-expr are conformable.  This does not catch all possible
    8736                 :             :    cases; in particular a runtime checking is needed.  */
    8737                 :             : 
    8738                 :             : static bool
    8739                 :        1805 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    8740                 :             : {
    8741                 :        1805 :   gfc_ref *tail;
    8742                 :        2510 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    8743                 :             : 
    8744                 :             :   /* First compare rank.  */
    8745                 :        1805 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    8746                 :           2 :       || (!tail && e1->rank != e2->rank))
    8747                 :             :     {
    8748                 :           4 :       gfc_error ("Source-expr at %L must be scalar or have the "
    8749                 :             :                  "same rank as the allocate-object at %L",
    8750                 :             :                  &e1->where, &e2->where);
    8751                 :           4 :       return false;
    8752                 :             :     }
    8753                 :             : 
    8754                 :        1801 :   if (e1->shape)
    8755                 :             :     {
    8756                 :        1306 :       int i;
    8757                 :        1306 :       mpz_t s;
    8758                 :             : 
    8759                 :        1306 :       mpz_init (s);
    8760                 :             : 
    8761                 :        3028 :       for (i = 0; i < e1->rank; i++)
    8762                 :             :         {
    8763                 :        1312 :           if (tail->u.ar.start[i] == NULL)
    8764                 :             :             break;
    8765                 :             : 
    8766                 :         416 :           if (tail->u.ar.end[i])
    8767                 :             :             {
    8768                 :          54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    8769                 :          54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    8770                 :          54 :               mpz_add_ui (s, s, 1);
    8771                 :             :             }
    8772                 :             :           else
    8773                 :             :             {
    8774                 :         362 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    8775                 :             :             }
    8776                 :             : 
    8777                 :         416 :           if (mpz_cmp (e1->shape[i], s) != 0)
    8778                 :             :             {
    8779                 :           0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    8780                 :             :                          "have the same shape", &e1->where, &e2->where);
    8781                 :           0 :               mpz_clear (s);
    8782                 :           0 :               return false;
    8783                 :             :             }
    8784                 :             :         }
    8785                 :             : 
    8786                 :        1306 :       mpz_clear (s);
    8787                 :             :     }
    8788                 :             : 
    8789                 :             :   return true;
    8790                 :             : }
    8791                 :             : 
    8792                 :             : 
    8793                 :             : /* Resolve the expression in an ALLOCATE statement, doing the additional
    8794                 :             :    checks to see whether the expression is OK or not.  The expression must
    8795                 :             :    have a trailing array reference that gives the size of the array.  */
    8796                 :             : 
    8797                 :             : static bool
    8798                 :       16506 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    8799                 :             : {
    8800                 :       16506 :   int i, pointer, allocatable, dimension, is_abstract;
    8801                 :       16506 :   int codimension;
    8802                 :       16506 :   bool coindexed;
    8803                 :       16506 :   bool unlimited;
    8804                 :       16506 :   symbol_attribute attr;
    8805                 :       16506 :   gfc_ref *ref, *ref2;
    8806                 :       16506 :   gfc_expr *e2;
    8807                 :       16506 :   gfc_array_ref *ar;
    8808                 :       16506 :   gfc_symbol *sym = NULL;
    8809                 :       16506 :   gfc_alloc *a;
    8810                 :       16506 :   gfc_component *c;
    8811                 :       16506 :   bool t;
    8812                 :             : 
    8813                 :             :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    8814                 :             :      checking of coarrays.  */
    8815                 :       20658 :   for (ref = e->ref; ref; ref = ref->next)
    8816                 :       16613 :     if (ref->next == NULL)
    8817                 :             :       break;
    8818                 :             : 
    8819                 :       16506 :   if (ref && ref->type == REF_ARRAY)
    8820                 :       11363 :     ref->u.ar.in_allocate = true;
    8821                 :             : 
    8822                 :       16506 :   if (!gfc_resolve_expr (e))
    8823                 :           1 :     goto failure;
    8824                 :             : 
    8825                 :             :   /* Make sure the expression is allocatable or a pointer.  If it is
    8826                 :             :      pointer, the next-to-last reference must be a pointer.  */
    8827                 :             : 
    8828                 :       16505 :   ref2 = NULL;
    8829                 :       16505 :   if (e->symtree)
    8830                 :       16505 :     sym = e->symtree->n.sym;
    8831                 :             : 
    8832                 :             :   /* Check whether ultimate component is abstract and CLASS.  */
    8833                 :       33010 :   is_abstract = 0;
    8834                 :             : 
    8835                 :             :   /* Is the allocate-object unlimited polymorphic?  */
    8836                 :       16505 :   unlimited = UNLIMITED_POLY(e);
    8837                 :             : 
    8838                 :       16505 :   if (e->expr_type != EXPR_VARIABLE)
    8839                 :             :     {
    8840                 :           0 :       allocatable = 0;
    8841                 :           0 :       attr = gfc_expr_attr (e);
    8842                 :           0 :       pointer = attr.pointer;
    8843                 :           0 :       dimension = attr.dimension;
    8844                 :           0 :       codimension = attr.codimension;
    8845                 :             :     }
    8846                 :             :   else
    8847                 :             :     {
    8848                 :       16505 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    8849                 :             :         {
    8850                 :        3274 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    8851                 :        3274 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    8852                 :        3274 :           dimension = CLASS_DATA (sym)->attr.dimension;
    8853                 :        3274 :           codimension = CLASS_DATA (sym)->attr.codimension;
    8854                 :        3274 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    8855                 :             :         }
    8856                 :             :       else
    8857                 :             :         {
    8858                 :       13231 :           allocatable = sym->attr.allocatable;
    8859                 :       13231 :           pointer = sym->attr.pointer;
    8860                 :       13231 :           dimension = sym->attr.dimension;
    8861                 :       13231 :           codimension = sym->attr.codimension;
    8862                 :             :         }
    8863                 :             : 
    8864                 :       16505 :       coindexed = false;
    8865                 :             : 
    8866                 :       33112 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    8867                 :             :         {
    8868                 :       16609 :           switch (ref->type)
    8869                 :             :             {
    8870                 :       12651 :               case REF_ARRAY:
    8871                 :       12651 :                 if (ref->u.ar.codimen > 0)
    8872                 :             :                   {
    8873                 :         641 :                     int n;
    8874                 :         922 :                     for (n = ref->u.ar.dimen;
    8875                 :         922 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    8876                 :         678 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    8877                 :             :                         {
    8878                 :             :                           coindexed = true;
    8879                 :             :                           break;
    8880                 :             :                         }
    8881                 :             :                    }
    8882                 :             : 
    8883                 :       12651 :                 if (ref->next != NULL)
    8884                 :        1290 :                   pointer = 0;
    8885                 :             :                 break;
    8886                 :             : 
    8887                 :        3958 :               case REF_COMPONENT:
    8888                 :             :                 /* F2008, C644.  */
    8889                 :        3958 :                 if (coindexed)
    8890                 :             :                   {
    8891                 :           2 :                     gfc_error ("Coindexed allocatable object at %L",
    8892                 :             :                                &e->where);
    8893                 :           2 :                     goto failure;
    8894                 :             :                   }
    8895                 :             : 
    8896                 :        3956 :                 c = ref->u.c.component;
    8897                 :        3956 :                 if (c->ts.type == BT_CLASS)
    8898                 :             :                   {
    8899                 :         950 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    8900                 :         950 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    8901                 :         950 :                     dimension = CLASS_DATA (c)->attr.dimension;
    8902                 :         950 :                     codimension = CLASS_DATA (c)->attr.codimension;
    8903                 :         950 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    8904                 :             :                   }
    8905                 :             :                 else
    8906                 :             :                   {
    8907                 :        3006 :                     allocatable = c->attr.allocatable;
    8908                 :        3006 :                     pointer = c->attr.pointer;
    8909                 :        3006 :                     dimension = c->attr.dimension;
    8910                 :        3006 :                     codimension = c->attr.codimension;
    8911                 :        3006 :                     is_abstract = c->attr.abstract;
    8912                 :             :                   }
    8913                 :             :                 break;
    8914                 :             : 
    8915                 :           0 :               case REF_SUBSTRING:
    8916                 :           0 :               case REF_INQUIRY:
    8917                 :           0 :                 allocatable = 0;
    8918                 :           0 :                 pointer = 0;
    8919                 :           0 :                 break;
    8920                 :             :             }
    8921                 :             :         }
    8922                 :             :     }
    8923                 :             : 
    8924                 :             :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    8925                 :             :      pointer or an allocatable variable.  */
    8926                 :       16503 :   if (allocatable == 0 && pointer == 0)
    8927                 :             :     {
    8928                 :           4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    8929                 :             :                  &e->where);
    8930                 :           4 :       goto failure;
    8931                 :             :     }
    8932                 :             : 
    8933                 :             :   /* Some checks for the SOURCE tag.  */
    8934                 :       16499 :   if (code->expr3)
    8935                 :             :     {
    8936                 :             :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    8937                 :             :          rank as allocate-object".  This would require the MOLD argument to
    8938                 :             :          NULL() as source-expr for subsequent checking.  However, even the
    8939                 :             :          resulting disassociated pointer or unallocated array has no shape that
    8940                 :             :          could be used for SOURCE= or MOLD=.  */
    8941                 :        3659 :       if (code->expr3->expr_type == EXPR_NULL)
    8942                 :             :         {
    8943                 :           4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    8944                 :             :                      &code->expr3->where);
    8945                 :           4 :           goto failure;
    8946                 :             :         }
    8947                 :             : 
    8948                 :             :       /* Check F03:C631.  */
    8949                 :        3655 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    8950                 :             :         {
    8951                 :          10 :           gfc_error ("Type of entity at %L is type incompatible with "
    8952                 :          10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    8953                 :          10 :           goto failure;
    8954                 :             :         }
    8955                 :             : 
    8956                 :             :       /* Check F03:C632 and restriction following Note 6.18.  */
    8957                 :        3645 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    8958                 :           4 :         goto failure;
    8959                 :             : 
    8960                 :             :       /* Check F03:C633.  */
    8961                 :        3641 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    8962                 :             :         {
    8963                 :           1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    8964                 :             :                      "shall have the same kind type parameter",
    8965                 :             :                      &e->where, &code->expr3->where);
    8966                 :           1 :           goto failure;
    8967                 :             :         }
    8968                 :             : 
    8969                 :             :       /* Check F2008, C642.  */
    8970                 :        3640 :       if (code->expr3->ts.type == BT_DERIVED
    8971                 :        3640 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    8972                 :        1163 :               || (code->expr3->ts.u.derived->from_intmod
    8973                 :             :                      == INTMOD_ISO_FORTRAN_ENV
    8974                 :        1163 :                   && code->expr3->ts.u.derived->intmod_sym_id
    8975                 :             :                      == ISOFORTRAN_LOCK_TYPE)))
    8976                 :             :         {
    8977                 :           0 :           gfc_error ("The source-expr at %L shall neither be of type "
    8978                 :             :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    8979                 :             :                       "allocate-object at %L is a coarray",
    8980                 :           0 :                       &code->expr3->where, &e->where);
    8981                 :           0 :           goto failure;
    8982                 :             :         }
    8983                 :             : 
    8984                 :             :       /* Check F2008:C639: "Corresponding kind type parameters of
    8985                 :             :          allocate-object and source-expr shall have the same values."  */
    8986                 :        3640 :       if (e->ts.type == BT_CHARACTER
    8987                 :         752 :           && !e->ts.deferred
    8988                 :         138 :           && e->ts.u.cl->length
    8989                 :         138 :           && code->expr3->ts.type == BT_CHARACTER
    8990                 :        3778 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    8991                 :             :                                      "SOURCE= or MOLD= specifier"))
    8992                 :          17 :             goto failure;
    8993                 :             : 
    8994                 :             :       /* Check TS18508, C702/C703.  */
    8995                 :        3623 :       if (code->expr3->ts.type == BT_DERIVED
    8996                 :        4786 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    8997                 :        1163 :               || (code->expr3->ts.u.derived->from_intmod
    8998                 :             :                      == INTMOD_ISO_FORTRAN_ENV
    8999                 :        1163 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9000                 :             :                      == ISOFORTRAN_EVENT_TYPE)))
    9001                 :             :         {
    9002                 :           0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9003                 :             :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9004                 :             :                       "allocate-object at %L is a coarray",
    9005                 :           0 :                       &code->expr3->where, &e->where);
    9006                 :           0 :           goto failure;
    9007                 :             :         }
    9008                 :             :     }
    9009                 :             : 
    9010                 :             :   /* Check F08:C629.  */
    9011                 :       16463 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9012                 :         153 :       && !code->expr3)
    9013                 :             :     {
    9014                 :           2 :       gcc_assert (e->ts.type == BT_CLASS);
    9015                 :           2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9016                 :             :                  "type-spec or source-expr", sym->name, &e->where);
    9017                 :           2 :       goto failure;
    9018                 :             :     }
    9019                 :             : 
    9020                 :             :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9021                 :             :      if and only if each allocate-object is a dummy argument for which the
    9022                 :             :      corresponding type parameter is assumed.  */
    9023                 :       16461 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9024                 :         453 :       && code->ext.alloc.ts.u.cl->length != NULL
    9025                 :         438 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9026                 :          23 :       && e->ts.u.cl->length == NULL
    9027                 :           2 :       && e->symtree->n.sym->attr.dummy)
    9028                 :             :     {
    9029                 :           2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9030                 :             :                  "shall be an asterisk as allocate object %qs at %L is a "
    9031                 :             :                  "dummy argument with assumed type parameter",
    9032                 :             :                  sym->name, &e->where);
    9033                 :           2 :       goto failure;
    9034                 :             :     }
    9035                 :             : 
    9036                 :             :   /* Check F08:C632.  */
    9037                 :       16459 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9038                 :          60 :       && !UNLIMITED_POLY (e))
    9039                 :             :     {
    9040                 :          36 :       int cmp;
    9041                 :             : 
    9042                 :          36 :       if (!e->ts.u.cl->length)
    9043                 :          15 :         goto failure;
    9044                 :             : 
    9045                 :          42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9046                 :          21 :                                   code->ext.alloc.ts.u.cl->length);
    9047                 :          21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9048                 :             :         {
    9049                 :           2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9050                 :             :                      "character-length parameter as in the declaration",
    9051                 :             :                      sym->name, &e->where);
    9052                 :           2 :           goto failure;
    9053                 :             :         }
    9054                 :             :     }
    9055                 :             : 
    9056                 :             :   /* In the variable definition context checks, gfc_expr_attr is used
    9057                 :             :      on the expression.  This is fooled by the array specification
    9058                 :             :      present in e, thus we have to eliminate that one temporarily.  */
    9059                 :       16442 :   e2 = remove_last_array_ref (e);
    9060                 :       16442 :   t = true;
    9061                 :       16442 :   if (t && pointer)
    9062                 :        3721 :     t = gfc_check_vardef_context (e2, true, true, false,
    9063                 :        3721 :                                   _("ALLOCATE object"));
    9064                 :        3721 :   if (t)
    9065                 :       16434 :     t = gfc_check_vardef_context (e2, false, true, false,
    9066                 :       16434 :                                   _("ALLOCATE object"));
    9067                 :       16442 :   gfc_free_expr (e2);
    9068                 :       16442 :   if (!t)
    9069                 :          11 :     goto failure;
    9070                 :             : 
    9071                 :       16431 :   code->ext.alloc.expr3_not_explicit = 0;
    9072                 :       16431 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9073                 :        1544 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9074                 :             :     {
    9075                 :             :       /* For class arrays, the initialization with SOURCE is done
    9076                 :             :          using _copy and trans_call. It is convenient to exploit that
    9077                 :             :          when the allocated type is different from the declared type but
    9078                 :             :          no SOURCE exists by setting expr3.  */
    9079                 :         280 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9080                 :         280 :       code->ext.alloc.expr3_not_explicit = 1;
    9081                 :             :     }
    9082                 :       16151 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9083                 :        2364 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9084                 :        2364 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9085                 :             :     {
    9086                 :             :       /* We have to zero initialize the integer variable.  */
    9087                 :           1 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9088                 :           1 :       code->ext.alloc.expr3_not_explicit = 1;
    9089                 :             :     }
    9090                 :             : 
    9091                 :       16431 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9092                 :             :     {
    9093                 :             :       /* Make sure the vtab symbol is present when
    9094                 :             :          the module variables are generated.  */
    9095                 :        2865 :       gfc_typespec ts = e->ts;
    9096                 :        2865 :       if (code->expr3)
    9097                 :        1281 :         ts = code->expr3->ts;
    9098                 :        1584 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9099                 :         687 :         ts = code->ext.alloc.ts;
    9100                 :             : 
    9101                 :             :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9102                 :             :          statement is necessary.  */
    9103                 :        2865 :       gfc_find_derived_vtab (ts.u.derived);
    9104                 :        2865 :     }
    9105                 :       13566 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9106                 :             :     {
    9107                 :             :       /* Again, make sure the vtab symbol is present when
    9108                 :             :          the module variables are generated.  */
    9109                 :         433 :       gfc_typespec *ts = NULL;
    9110                 :         433 :       if (code->expr3)
    9111                 :         347 :         ts = &code->expr3->ts;
    9112                 :             :       else
    9113                 :          86 :         ts = &code->ext.alloc.ts;
    9114                 :             : 
    9115                 :         433 :       gcc_assert (ts);
    9116                 :             : 
    9117                 :             :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9118                 :             :          statement is necessary.  */
    9119                 :         433 :       gfc_find_vtab (ts);
    9120                 :             :     }
    9121                 :             : 
    9122                 :       16431 :   if (dimension == 0 && codimension == 0)
    9123                 :        5096 :     goto success;
    9124                 :             : 
    9125                 :             :   /* Make sure the last reference node is an array specification.  */
    9126                 :             : 
    9127                 :       11335 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9128                 :       10185 :       || (dimension && ref2->u.ar.dimen == 0))
    9129                 :             :     {
    9130                 :             :       /* F08:C633.  */
    9131                 :        1150 :       if (code->expr3)
    9132                 :             :         {
    9133                 :        1149 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9134                 :             :                                "in ALLOCATE statement at %L", &e->where))
    9135                 :           0 :             goto failure;
    9136                 :        1149 :           if (code->expr3->rank != 0)
    9137                 :        1148 :             *array_alloc_wo_spec = true;
    9138                 :             :           else
    9139                 :             :             {
    9140                 :           1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9141                 :             :                          "expression required in ALLOCATE statement at %L",
    9142                 :             :                          &e->where);
    9143                 :           1 :               goto failure;
    9144                 :             :             }
    9145                 :             :         }
    9146                 :             :       else
    9147                 :             :         {
    9148                 :           1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9149                 :             :                      "at %L", &e->where);
    9150                 :           1 :           goto failure;
    9151                 :             :         }
    9152                 :             :     }
    9153                 :             : 
    9154                 :             :   /* Make sure that the array section reference makes sense in the
    9155                 :             :      context of an ALLOCATE specification.  */
    9156                 :             : 
    9157                 :       11333 :   ar = &ref2->u.ar;
    9158                 :             : 
    9159                 :       11333 :   if (codimension)
    9160                 :         942 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9161                 :             :       {
    9162                 :         558 :         switch (ar->dimen_type[i])
    9163                 :             :           {
    9164                 :           2 :           case DIMEN_THIS_IMAGE:
    9165                 :           2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9166                 :             :                        "at %L", &e->where);
    9167                 :           2 :             goto failure;
    9168                 :             : 
    9169                 :          83 :           case  DIMEN_RANGE:
    9170                 :             :             /* F2018:R937:
    9171                 :             :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9172                 :             :              */
    9173                 :          83 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9174                 :             :               {
    9175                 :           8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9176                 :             :                            "at %L", &e->where);
    9177                 :           8 :                 goto failure;
    9178                 :             :               }
    9179                 :          75 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9180                 :             :               {
    9181                 :           2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9182                 :           2 :                            &ar->start[i]->where);
    9183                 :           2 :                 goto failure;
    9184                 :             :               }
    9185                 :             :             break;
    9186                 :             : 
    9187                 :          89 :           case DIMEN_ELEMENT:
    9188                 :          89 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9189                 :             :               {
    9190                 :          81 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9191                 :          81 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9192                 :             :                   {
    9193                 :           1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9194                 :             :                                "of 1 at %L", &ar->start[i]->where);
    9195                 :           1 :                     goto failure;
    9196                 :             :                   }
    9197                 :             :               }
    9198                 :             :             break;
    9199                 :             : 
    9200                 :             :           case DIMEN_STAR:
    9201                 :             :             break;
    9202                 :             : 
    9203                 :           0 :           default:
    9204                 :           0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9205                 :             :                        &e->where);
    9206                 :           0 :             goto failure;
    9207                 :             : 
    9208                 :             :           }
    9209                 :             :       }
    9210                 :       27953 :   for (i = 0; i < ar->dimen; i++)
    9211                 :             :     {
    9212                 :       16637 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9213                 :       13995 :         goto check_symbols;
    9214                 :             : 
    9215                 :        2642 :       switch (ar->dimen_type[i])
    9216                 :             :         {
    9217                 :             :         case DIMEN_ELEMENT:
    9218                 :             :           break;
    9219                 :             : 
    9220                 :        2377 :         case DIMEN_RANGE:
    9221                 :        2377 :           if (ar->start[i] != NULL
    9222                 :        2377 :               && ar->end[i] != NULL
    9223                 :        2376 :               && ar->stride[i] == NULL)
    9224                 :             :             break;
    9225                 :             : 
    9226                 :             :           /* Fall through.  */
    9227                 :             : 
    9228                 :           1 :         case DIMEN_UNKNOWN:
    9229                 :           1 :         case DIMEN_VECTOR:
    9230                 :           1 :         case DIMEN_STAR:
    9231                 :           1 :         case DIMEN_THIS_IMAGE:
    9232                 :           1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9233                 :             :                      &e->where);
    9234                 :           1 :           goto failure;
    9235                 :             :         }
    9236                 :             : 
    9237                 :        2376 : check_symbols:
    9238                 :       43583 :       for (a = code->ext.alloc.list; a; a = a->next)
    9239                 :             :         {
    9240                 :       26950 :           sym = a->expr->symtree->n.sym;
    9241                 :             : 
    9242                 :             :           /* TODO - check derived type components.  */
    9243                 :       26950 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9244                 :        8881 :             continue;
    9245                 :             : 
    9246                 :       18069 :           if ((ar->start[i] != NULL
    9247                 :       17493 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9248                 :       35559 :               || (ar->end[i] != NULL
    9249                 :        2616 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9250                 :             :             {
    9251                 :           3 :               gfc_error ("%qs must not appear in the array specification at "
    9252                 :             :                          "%L in the same ALLOCATE statement where it is "
    9253                 :             :                          "itself allocated", sym->name, &ar->where);
    9254                 :           3 :               goto failure;
    9255                 :             :             }
    9256                 :             :         }
    9257                 :             :     }
    9258                 :             : 
    9259                 :       11476 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9260                 :             :     {
    9261                 :         703 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9262                 :         543 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9263                 :             :         {
    9264                 :         160 :           if (i == (ar->dimen + ar->codimen - 1))
    9265                 :             :             {
    9266                 :           0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9267                 :             :                          "statement at %L", &e->where);
    9268                 :           0 :               goto failure;
    9269                 :             :             }
    9270                 :         160 :           continue;
    9271                 :             :         }
    9272                 :             : 
    9273                 :         383 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9274                 :         383 :           && ar->stride[i] == NULL)
    9275                 :             :         break;
    9276                 :             : 
    9277                 :           0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9278                 :             :                  &e->where);
    9279                 :           0 :       goto failure;
    9280                 :             :     }
    9281                 :             : 
    9282                 :       11316 : success:
    9283                 :             :   return true;
    9284                 :             : 
    9285                 :             : failure:
    9286                 :             :   return false;
    9287                 :             : }
    9288                 :             : 
    9289                 :             : 
    9290                 :             : static void
    9291                 :       19188 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9292                 :             : {
    9293                 :       19188 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9294                 :       19188 :   gfc_alloc *a, *p, *q;
    9295                 :             : 
    9296                 :       19188 :   stat = code->expr1;
    9297                 :       19188 :   errmsg = code->expr2;
    9298                 :             : 
    9299                 :             :   /* Check the stat variable.  */
    9300                 :       19188 :   if (stat)
    9301                 :             :     {
    9302                 :         613 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9303                 :         613 :                                      _("STAT variable")))
    9304                 :           8 :           goto done_stat;
    9305                 :             : 
    9306                 :         605 :       if (stat->ts.type != BT_INTEGER
    9307                 :         596 :           || stat->rank > 0)
    9308                 :          11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9309                 :             :                    "variable", &stat->where);
    9310                 :             : 
    9311                 :         605 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9312                 :           0 :         goto done_stat;
    9313                 :             : 
    9314                 :             :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9315                 :             :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9316                 :             :        */
    9317                 :        1255 :       for (p = code->ext.alloc.list; p; p = p->next)
    9318                 :         657 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9319                 :             :           {
    9320                 :           9 :             gfc_ref *ref1, *ref2;
    9321                 :           9 :             bool found = true;
    9322                 :             : 
    9323                 :          16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9324                 :           7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9325                 :             :               {
    9326                 :           9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9327                 :           5 :                   continue;
    9328                 :           4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9329                 :             :                   {
    9330                 :             :                     found = false;
    9331                 :             :                     break;
    9332                 :             :                   }
    9333                 :             :               }
    9334                 :             : 
    9335                 :           9 :             if (found)
    9336                 :             :               {
    9337                 :           7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9338                 :             :                            "the same %s statement", &stat->where, fcn, fcn);
    9339                 :           7 :                 break;
    9340                 :             :               }
    9341                 :             :           }
    9342                 :             :     }
    9343                 :             : 
    9344                 :       18575 : done_stat:
    9345                 :             : 
    9346                 :             :   /* Check the errmsg variable.  */
    9347                 :       19188 :   if (errmsg)
    9348                 :             :     {
    9349                 :         146 :       if (!stat)
    9350                 :           2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9351                 :             :                      &errmsg->where);
    9352                 :             : 
    9353                 :         146 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9354                 :         146 :                                      _("ERRMSG variable")))
    9355                 :           6 :           goto done_errmsg;
    9356                 :             : 
    9357                 :             :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9358                 :             :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9359                 :             :          F18:R906  default-char-variable is variable
    9360                 :             :          F18:C906  default-char-variable shall be default character.  */
    9361                 :         140 :       if (errmsg->ts.type != BT_CHARACTER
    9362                 :         138 :           || errmsg->rank > 0
    9363                 :         137 :           || errmsg->ts.kind != gfc_default_character_kind)
    9364                 :           4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9365                 :             :                    "variable", &errmsg->where);
    9366                 :             : 
    9367                 :         140 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9368                 :           0 :         goto done_errmsg;
    9369                 :             : 
    9370                 :             :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9371                 :             :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9372                 :             :        */
    9373                 :         278 :       for (p = code->ext.alloc.list; p; p = p->next)
    9374                 :         143 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9375                 :             :           {
    9376                 :           9 :             gfc_ref *ref1, *ref2;
    9377                 :           9 :             bool found = true;
    9378                 :             : 
    9379                 :          16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9380                 :           7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9381                 :             :               {
    9382                 :          11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9383                 :           4 :                   continue;
    9384                 :           7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9385                 :             :                   {
    9386                 :             :                     found = false;
    9387                 :             :                     break;
    9388                 :             :                   }
    9389                 :             :               }
    9390                 :             : 
    9391                 :           9 :             if (found)
    9392                 :             :               {
    9393                 :           5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9394                 :             :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9395                 :           5 :                 break;
    9396                 :             :               }
    9397                 :             :           }
    9398                 :             :     }
    9399                 :             : 
    9400                 :       19042 : done_errmsg:
    9401                 :             : 
    9402                 :             :   /* Check that an allocate-object appears only once in the statement.  */
    9403                 :             : 
    9404                 :       43477 :   for (p = code->ext.alloc.list; p; p = p->next)
    9405                 :             :     {
    9406                 :       24289 :       pe = p->expr;
    9407                 :       33265 :       for (q = p->next; q; q = q->next)
    9408                 :             :         {
    9409                 :        8976 :           qe = q->expr;
    9410                 :        8976 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9411                 :             :             {
    9412                 :             :               /* This is a potential collision.  */
    9413                 :        2091 :               gfc_ref *pr = pe->ref;
    9414                 :        2091 :               gfc_ref *qr = qe->ref;
    9415                 :             : 
    9416                 :             :               /* Follow the references  until
    9417                 :             :                  a) They start to differ, in which case there is no error;
    9418                 :             :                  you can deallocate a%b and a%c in a single statement
    9419                 :             :                  b) Both of them stop, which is an error
    9420                 :             :                  c) One of them stops, which is also an error.  */
    9421                 :        4515 :               while (1)
    9422                 :             :                 {
    9423                 :        3303 :                   if (pr == NULL && qr == NULL)
    9424                 :             :                     {
    9425                 :           7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9426                 :             :                                  &pe->where, &qe->where);
    9427                 :           7 :                       break;
    9428                 :             :                     }
    9429                 :        3296 :                   else if (pr != NULL && qr == NULL)
    9430                 :             :                     {
    9431                 :           2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9432                 :             :                                  " object at %L", &pe->where, &qe->where);
    9433                 :           2 :                       break;
    9434                 :             :                     }
    9435                 :        3294 :                   else if (pr == NULL && qr != NULL)
    9436                 :             :                     {
    9437                 :           2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9438                 :             :                                  " object at %L", &qe->where, &pe->where);
    9439                 :           2 :                       break;
    9440                 :             :                     }
    9441                 :             :                   /* Here, pr != NULL && qr != NULL  */
    9442                 :        3292 :                   gcc_assert(pr->type == qr->type);
    9443                 :        3292 :                   if (pr->type == REF_ARRAY)
    9444                 :             :                     {
    9445                 :             :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9446                 :             :                          which are legal.  */
    9447                 :        1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9448                 :             : 
    9449                 :        1065 :                       if (pr->next && qr->next)
    9450                 :             :                         {
    9451                 :             :                           int i;
    9452                 :             :                           gfc_array_ref *par = &(pr->u.ar);
    9453                 :             :                           gfc_array_ref *qar = &(qr->u.ar);
    9454                 :             : 
    9455                 :        1840 :                           for (i=0; i<par->dimen; i++)
    9456                 :             :                             {
    9457                 :         954 :                               if ((par->start[i] != NULL
    9458                 :           0 :                                    || qar->start[i] != NULL)
    9459                 :         954 :                                   && gfc_dep_compare_expr (par->start[i],
    9460                 :             :                                                            qar->start[i]) != 0)
    9461                 :         168 :                                 goto break_label;
    9462                 :             :                             }
    9463                 :             :                         }
    9464                 :             :                     }
    9465                 :             :                   else
    9466                 :             :                     {
    9467                 :        2227 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9468                 :             :                         break;
    9469                 :             :                     }
    9470                 :             : 
    9471                 :        1212 :                   pr = pr->next;
    9472                 :        1212 :                   qr = qr->next;
    9473                 :        1212 :                 }
    9474                 :        8976 :             break_label:
    9475                 :             :               ;
    9476                 :             :             }
    9477                 :             :         }
    9478                 :             :     }
    9479                 :             : 
    9480                 :       19188 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9481                 :             :     {
    9482                 :       13531 :       bool arr_alloc_wo_spec = false;
    9483                 :             : 
    9484                 :             :       /* Resolving the expr3 in the loop over all objects to allocate would
    9485                 :             :          execute loop invariant code for each loop item.  Therefore do it just
    9486                 :             :          once here.  */
    9487                 :       13531 :       if (code->expr3 && code->expr3->mold
    9488                 :         316 :           && code->expr3->ts.type == BT_DERIVED)
    9489                 :             :         {
    9490                 :             :           /* Default initialization via MOLD (non-polymorphic).  */
    9491                 :          20 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9492                 :          20 :           if (rhs != NULL)
    9493                 :             :             {
    9494                 :           7 :               gfc_resolve_expr (rhs);
    9495                 :           7 :               gfc_free_expr (code->expr3);
    9496                 :           7 :               code->expr3 = rhs;
    9497                 :             :             }
    9498                 :             :         }
    9499                 :       30037 :       for (a = code->ext.alloc.list; a; a = a->next)
    9500                 :       16506 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9501                 :             : 
    9502                 :       13531 :       if (arr_alloc_wo_spec && code->expr3)
    9503                 :             :         {
    9504                 :             :           /* Mark the allocate to have to take the array specification
    9505                 :             :              from the expr3.  */
    9506                 :        1142 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9507                 :             :         }
    9508                 :             :     }
    9509                 :             :   else
    9510                 :             :     {
    9511                 :       13440 :       for (a = code->ext.alloc.list; a; a = a->next)
    9512                 :        7783 :         resolve_deallocate_expr (a->expr);
    9513                 :             :     }
    9514                 :       19188 : }
    9515                 :             : 
    9516                 :             : 
    9517                 :             : /************ SELECT CASE resolution subroutines ************/
    9518                 :             : 
    9519                 :             : /* Callback function for our mergesort variant.  Determines interval
    9520                 :             :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
    9521                 :             :    op1 > op2.  Assumes we're not dealing with the default case.
    9522                 :             :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
    9523                 :             :    There are nine situations to check.  */
    9524                 :             : 
    9525                 :             : static int
    9526                 :        1542 : compare_cases (const gfc_case *op1, const gfc_case *op2)
    9527                 :             : {
    9528                 :        1542 :   int retval;
    9529                 :             : 
    9530                 :        1542 :   if (op1->low == NULL) /* op1 = (:L)  */
    9531                 :             :     {
    9532                 :             :       /* op2 = (:N), so overlap.  */
    9533                 :          52 :       retval = 0;
    9534                 :             :       /* op2 = (M:) or (M:N),  L < M  */
    9535                 :          52 :       if (op2->low != NULL
    9536                 :          52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9537                 :             :         retval = -1;
    9538                 :             :     }
    9539                 :        1490 :   else if (op1->high == NULL) /* op1 = (K:)  */
    9540                 :             :     {
    9541                 :             :       /* op2 = (M:), so overlap.  */
    9542                 :          10 :       retval = 0;
    9543                 :             :       /* op2 = (:N) or (M:N), K > N  */
    9544                 :          10 :       if (op2->high != NULL
    9545                 :          10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9546                 :             :         retval = 1;
    9547                 :             :     }
    9548                 :             :   else /* op1 = (K:L)  */
    9549                 :             :     {
    9550                 :        1480 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
    9551                 :          18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9552                 :          18 :                  ? 1 : 0;
    9553                 :        1462 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
    9554                 :          10 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9555                 :          10 :                  ? -1 : 0;
    9556                 :             :       else                      /* op2 = (M:N)  */
    9557                 :             :         {
    9558                 :        1452 :           retval =  0;
    9559                 :             :           /* L < M  */
    9560                 :        1452 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9561                 :             :             retval =  -1;
    9562                 :             :           /* K > N  */
    9563                 :         412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9564                 :         438 :             retval =  1;
    9565                 :             :         }
    9566                 :             :     }
    9567                 :             : 
    9568                 :        1542 :   return retval;
    9569                 :             : }
    9570                 :             : 
    9571                 :             : 
    9572                 :             : /* Merge-sort a double linked case list, detecting overlap in the
    9573                 :             :    process.  LIST is the head of the double linked case list before it
    9574                 :             :    is sorted.  Returns the head of the sorted list if we don't see any
    9575                 :             :    overlap, or NULL otherwise.  */
    9576                 :             : 
    9577                 :             : static gfc_case *
    9578                 :         642 : check_case_overlap (gfc_case *list)
    9579                 :             : {
    9580                 :         642 :   gfc_case *p, *q, *e, *tail;
    9581                 :         642 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
    9582                 :             : 
    9583                 :             :   /* If the passed list was empty, return immediately.  */
    9584                 :         642 :   if (!list)
    9585                 :             :     return NULL;
    9586                 :             : 
    9587                 :             :   overlap_seen = 0;
    9588                 :             :   insize = 1;
    9589                 :             : 
    9590                 :             :   /* Loop unconditionally.  The only exit from this loop is a return
    9591                 :             :      statement, when we've finished sorting the case list.  */
    9592                 :        1334 :   for (;;)
    9593                 :             :     {
    9594                 :         988 :       p = list;
    9595                 :         988 :       list = NULL;
    9596                 :         988 :       tail = NULL;
    9597                 :             : 
    9598                 :             :       /* Count the number of merges we do in this pass.  */
    9599                 :         988 :       nmerges = 0;
    9600                 :             : 
    9601                 :             :       /* Loop while there exists a merge to be done.  */
    9602                 :        2491 :       while (p)
    9603                 :             :         {
    9604                 :        1503 :           int i;
    9605                 :             : 
    9606                 :             :           /* Count this merge.  */
    9607                 :        1503 :           nmerges++;
    9608                 :             : 
    9609                 :             :           /* Cut the list in two pieces by stepping INSIZE places
    9610                 :             :              forward in the list, starting from P.  */
    9611                 :        1503 :           psize = 0;
    9612                 :        1503 :           q = p;
    9613                 :        3150 :           for (i = 0; i < insize; i++)
    9614                 :             :             {
    9615                 :        2206 :               psize++;
    9616                 :        2206 :               q = q->right;
    9617                 :        2206 :               if (!q)
    9618                 :             :                 break;
    9619                 :             :             }
    9620                 :             :           qsize = insize;
    9621                 :             : 
    9622                 :             :           /* Now we have two lists.  Merge them!  */
    9623                 :        4918 :           while (psize > 0 || (qsize > 0 && q != NULL))
    9624                 :             :             {
    9625                 :             :               /* See from which the next case to merge comes from.  */
    9626                 :         771 :               if (psize == 0)
    9627                 :             :                 {
    9628                 :             :                   /* P is empty so the next case must come from Q.  */
    9629                 :         771 :                   e = q;
    9630                 :         771 :                   q = q->right;
    9631                 :         771 :                   qsize--;
    9632                 :             :                 }
    9633                 :        2644 :               else if (qsize == 0 || q == NULL)
    9634                 :             :                 {
    9635                 :             :                   /* Q is empty.  */
    9636                 :        1102 :                   e = p;
    9637                 :        1102 :                   p = p->right;
    9638                 :        1102 :                   psize--;
    9639                 :             :                 }
    9640                 :             :               else
    9641                 :             :                 {
    9642                 :        1542 :                   cmp = compare_cases (p, q);
    9643                 :        1542 :                   if (cmp < 0)
    9644                 :             :                     {
    9645                 :             :                       /* The whole case range for P is less than the
    9646                 :             :                          one for Q.  */
    9647                 :        1100 :                       e = p;
    9648                 :        1100 :                       p = p->right;
    9649                 :        1100 :                       psize--;
    9650                 :             :                     }
    9651                 :         442 :                   else if (cmp > 0)
    9652                 :             :                     {
    9653                 :             :                       /* The whole case range for Q is greater than
    9654                 :             :                          the case range for P.  */
    9655                 :         438 :                       e = q;
    9656                 :         438 :                       q = q->right;
    9657                 :         438 :                       qsize--;
    9658                 :             :                     }
    9659                 :             :                   else
    9660                 :             :                     {
    9661                 :             :                       /* The cases overlap, or they are the same
    9662                 :             :                          element in the list.  Either way, we must
    9663                 :             :                          issue an error and get the next case from P.  */
    9664                 :             :                       /* FIXME: Sort P and Q by line number.  */
    9665                 :           4 :                       gfc_error ("CASE label at %L overlaps with CASE "
    9666                 :             :                                  "label at %L", &p->where, &q->where);
    9667                 :           4 :                       overlap_seen = 1;
    9668                 :           4 :                       e = p;
    9669                 :           4 :                       p = p->right;
    9670                 :           4 :                       psize--;
    9671                 :             :                     }
    9672                 :             :                 }
    9673                 :             : 
    9674                 :             :                 /* Add the next element to the merged list.  */
    9675                 :        3415 :               if (tail)
    9676                 :        2427 :                 tail->right = e;
    9677                 :             :               else
    9678                 :             :                 list = e;
    9679                 :        3415 :               e->left = tail;
    9680                 :        3415 :               tail = e;
    9681                 :             :             }
    9682                 :             : 
    9683                 :             :           /* P has now stepped INSIZE places along, and so has Q.  So
    9684                 :             :              they're the same.  */
    9685                 :             :           p = q;
    9686                 :             :         }
    9687                 :         988 :       tail->right = NULL;
    9688                 :             : 
    9689                 :             :       /* If we have done only one merge or none at all, we've
    9690                 :             :          finished sorting the cases.  */
    9691                 :         988 :       if (nmerges <= 1)
    9692                 :             :         {
    9693                 :         642 :           if (!overlap_seen)
    9694                 :             :             return list;
    9695                 :             :           else
    9696                 :             :             return NULL;
    9697                 :             :         }
    9698                 :             : 
    9699                 :             :       /* Otherwise repeat, merging lists twice the size.  */
    9700                 :         346 :       insize *= 2;
    9701                 :         346 :     }
    9702                 :             : }
    9703                 :             : 
    9704                 :             : 
    9705                 :             : /* Check to see if an expression is suitable for use in a CASE statement.
    9706                 :             :    Makes sure that all case expressions are scalar constants of the same
    9707                 :             :    type.  Return false if anything is wrong.  */
    9708                 :             : 
    9709                 :             : static bool
    9710                 :        3257 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
    9711                 :             : {
    9712                 :        3257 :   if (e == NULL) return true;
    9713                 :             : 
    9714                 :        3164 :   if (e->ts.type != case_expr->ts.type)
    9715                 :             :     {
    9716                 :           4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
    9717                 :             :                  &e->where, gfc_basic_typename (case_expr->ts.type));
    9718                 :           4 :       return false;
    9719                 :             :     }
    9720                 :             : 
    9721                 :             :   /* C805 (R808) For a given case-construct, each case-value shall be of
    9722                 :             :      the same type as case-expr.  For character type, length differences
    9723                 :             :      are allowed, but the kind type parameters shall be the same.  */
    9724                 :             : 
    9725                 :        3160 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
    9726                 :             :     {
    9727                 :           4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
    9728                 :             :                  &e->where, case_expr->ts.kind);
    9729                 :           4 :       return false;
    9730                 :             :     }
    9731                 :             : 
    9732                 :             :   /* Convert the case value kind to that of case expression kind,
    9733                 :             :      if needed */
    9734                 :             : 
    9735                 :        3156 :   if (e->ts.kind != case_expr->ts.kind)
    9736                 :          14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
    9737                 :             : 
    9738                 :        3156 :   if (e->rank != 0)
    9739                 :             :     {
    9740                 :           0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
    9741                 :             :                  &e->where);
    9742                 :           0 :       return false;
    9743                 :             :     }
    9744                 :             : 
    9745                 :             :   return true;
    9746                 :             : }
    9747                 :             : 
    9748                 :             : 
    9749                 :             : /* Given a completely parsed select statement, we:
    9750                 :             : 
    9751                 :             :      - Validate all expressions and code within the SELECT.
    9752                 :             :      - Make sure that the selection expression is not of the wrong type.
    9753                 :             :      - Make sure that no case ranges overlap.
    9754                 :             :      - Eliminate unreachable cases and unreachable code resulting from
    9755                 :             :        removing case labels.
    9756                 :             : 
    9757                 :             :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
    9758                 :             :    they are a hassle for code generation, and to prevent that, we just
    9759                 :             :    cut them out here.  This is not necessary for overlapping cases
    9760                 :             :    because they are illegal and we never even try to generate code.
    9761                 :             : 
    9762                 :             :    We have the additional caveat that a SELECT construct could have
    9763                 :             :    been a computed GOTO in the source code. Fortunately we can fairly
    9764                 :             :    easily work around that here: The case_expr for a "real" SELECT CASE
    9765                 :             :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
    9766                 :             :    we have to do is make sure that the case_expr is a scalar integer
    9767                 :             :    expression.  */
    9768                 :             : 
    9769                 :             : static void
    9770                 :         683 : resolve_select (gfc_code *code, bool select_type)
    9771                 :             : {
    9772                 :         683 :   gfc_code *body;
    9773                 :         683 :   gfc_expr *case_expr;
    9774                 :         683 :   gfc_case *cp, *default_case, *tail, *head;
    9775                 :         683 :   int seen_unreachable;
    9776                 :         683 :   int seen_logical;
    9777                 :         683 :   int ncases;
    9778                 :         683 :   bt type;
    9779                 :         683 :   bool t;
    9780                 :             : 
    9781                 :         683 :   if (code->expr1 == NULL)
    9782                 :             :     {
    9783                 :             :       /* This was actually a computed GOTO statement.  */
    9784                 :           5 :       case_expr = code->expr2;
    9785                 :           5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
    9786                 :           3 :         gfc_error ("Selection expression in computed GOTO statement "
    9787                 :             :                    "at %L must be a scalar integer expression",
    9788                 :             :                    &case_expr->where);
    9789                 :             : 
    9790                 :             :       /* Further checking is not necessary because this SELECT was built
    9791                 :             :          by the compiler, so it should always be OK.  Just move the
    9792                 :             :          case_expr from expr2 to expr so that we can handle computed
    9793                 :             :          GOTOs as normal SELECTs from here on.  */
    9794                 :           5 :       code->expr1 = code->expr2;
    9795                 :           5 :       code->expr2 = NULL;
    9796                 :           5 :       return;
    9797                 :             :     }
    9798                 :             : 
    9799                 :         678 :   case_expr = code->expr1;
    9800                 :         678 :   type = case_expr->ts.type;
    9801                 :             : 
    9802                 :             :   /* F08:C830.  */
    9803                 :         678 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
    9804                 :           6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
    9805                 :             : 
    9806                 :             :     {
    9807                 :           0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
    9808                 :             :                  &case_expr->where, gfc_typename (case_expr));
    9809                 :             : 
    9810                 :             :       /* Punt. Going on here just produce more garbage error messages.  */
    9811                 :           0 :       return;
    9812                 :             :     }
    9813                 :             : 
    9814                 :             :   /* F08:R842.  */
    9815                 :         678 :   if (!select_type && case_expr->rank != 0)
    9816                 :             :     {
    9817                 :           1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
    9818                 :             :                  "expression", &case_expr->where);
    9819                 :             : 
    9820                 :             :       /* Punt.  */
    9821                 :           1 :       return;
    9822                 :             :     }
    9823                 :             : 
    9824                 :             :   /* Raise a warning if an INTEGER case value exceeds the range of
    9825                 :             :      the case-expr. Later, all expressions will be promoted to the
    9826                 :             :      largest kind of all case-labels.  */
    9827                 :             : 
    9828                 :         677 :   if (type == BT_INTEGER)
    9829                 :        1897 :     for (body = code->block; body; body = body->block)
    9830                 :        2800 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
    9831                 :             :         {
    9832                 :        1436 :           if (cp->low
    9833                 :        1436 :               && gfc_check_integer_range (cp->low->value.integer,
    9834                 :             :                                           case_expr->ts.kind) != ARITH_OK)
    9835                 :           6 :             gfc_warning (0, "Expression in CASE statement at %L is "
    9836                 :           6 :                          "not in the range of %s", &cp->low->where,
    9837                 :             :                          gfc_typename (case_expr));
    9838                 :             : 
    9839                 :        1436 :           if (cp->high
    9840                 :        1153 :               && cp->low != cp->high
    9841                 :        1544 :               && gfc_check_integer_range (cp->high->value.integer,
    9842                 :             :                                           case_expr->ts.kind) != ARITH_OK)
    9843                 :           0 :             gfc_warning (0, "Expression in CASE statement at %L is "
    9844                 :           0 :                          "not in the range of %s", &cp->high->where,
    9845                 :             :                          gfc_typename (case_expr));
    9846                 :             :         }
    9847                 :             : 
    9848                 :             :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
    9849                 :             :      of the SELECT CASE expression and its CASE values.  Walk the lists
    9850                 :             :      of case values, and if we find a mismatch, promote case_expr to
    9851                 :             :      the appropriate kind.  */
    9852                 :             : 
    9853                 :         677 :   if (type == BT_LOGICAL || type == BT_INTEGER)
    9854                 :             :     {
    9855                 :        2083 :       for (body = code->block; body; body = body->block)
    9856                 :             :         {
    9857                 :             :           /* Walk the case label list.  */
    9858                 :        3061 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
    9859                 :             :             {
    9860                 :             :               /* Intercept the DEFAULT case.  It does not have a kind.  */
    9861                 :        1571 :               if (cp->low == NULL && cp->high == NULL)
    9862                 :         291 :                 continue;
    9863                 :             : 
    9864                 :             :               /* Unreachable case ranges are discarded, so ignore.  */
    9865                 :        1235 :               if (cp->low != NULL && cp->high != NULL
    9866                 :        1187 :                   && cp->low != cp->high
    9867                 :        1345 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
    9868                 :          33 :                 continue;
    9869                 :             : 
    9870                 :        1247 :               if (cp->low != NULL
    9871                 :        1247 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
    9872                 :          17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
    9873                 :             : 
    9874                 :        1247 :               if (cp->high != NULL
    9875                 :        1247 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
    9876                 :           4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
    9877                 :             :             }
    9878                 :             :          }
    9879                 :             :     }
    9880                 :             : 
    9881                 :             :   /* Assume there is no DEFAULT case.  */
    9882                 :         677 :   default_case = NULL;
    9883                 :         677 :   head = tail = NULL;
    9884                 :         677 :   ncases = 0;
    9885                 :         677 :   seen_logical = 0;
    9886                 :             : 
    9887                 :        2472 :   for (body = code->block; body; body = body->block)
    9888                 :             :     {
    9889                 :             :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
    9890                 :        1795 :       t = true;
    9891                 :        1795 :       seen_unreachable = 0;
    9892                 :             : 
    9893                 :             :       /* Walk the case label list, making sure that all case labels
    9894                 :             :          are legal.  */
    9895                 :        3777 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
    9896                 :             :         {
    9897                 :             :           /* Count the number of cases in the whole construct.  */
    9898                 :        1993 :           ncases++;
    9899                 :             : 
    9900                 :             :           /* Intercept the DEFAULT case.  */
    9901                 :        1993 :           if (cp->low == NULL && cp->high == NULL)
    9902                 :             :             {
    9903                 :         361 :               if (default_case != NULL)
    9904                 :             :                 {
    9905                 :           0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
    9906                 :             :                              "by a second DEFAULT CASE at %L",
    9907                 :             :                              &default_case->where, &cp->where);
    9908                 :           0 :                   t = false;
    9909                 :           0 :                   break;
    9910                 :             :                 }
    9911                 :             :               else
    9912                 :             :                 {
    9913                 :         361 :                   default_case = cp;
    9914                 :         361 :                   continue;
    9915                 :             :                 }
    9916                 :             :             }
    9917                 :             : 
    9918                 :             :           /* Deal with single value cases and case ranges.  Errors are
    9919                 :             :              issued from the validation function.  */
    9920                 :        1632 :           if (!validate_case_label_expr (cp->low, case_expr)
    9921                 :        1632 :               || !validate_case_label_expr (cp->high, case_expr))
    9922                 :             :             {
    9923                 :             :               t = false;
    9924                 :             :               break;
    9925                 :             :             }
    9926                 :             : 
    9927                 :        1624 :           if (type == BT_LOGICAL
    9928                 :          78 :               && ((cp->low == NULL || cp->high == NULL)
    9929                 :          76 :                   || cp->low != cp->high))
    9930                 :             :             {
    9931                 :           2 :               gfc_error ("Logical range in CASE statement at %L is not "
    9932                 :             :                          "allowed",
    9933                 :           1 :                          cp->low ? &cp->low->where : &cp->high->where);
    9934                 :           2 :               t = false;
    9935                 :           2 :               break;
    9936                 :             :             }
    9937                 :             : 
    9938                 :          76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
    9939                 :             :             {
    9940                 :          76 :               int value;
    9941                 :          76 :               value = cp->low->value.logical == 0 ? 2 : 1;
    9942                 :          76 :               if (value & seen_logical)
    9943                 :             :                 {
    9944                 :           1 :                   gfc_error ("Constant logical value in CASE statement "
    9945                 :             :                              "is repeated at %L",
    9946                 :             :                              &cp->low->where);
    9947                 :           1 :                   t = false;
    9948                 :           1 :                   break;
    9949                 :             :                 }
    9950                 :          75 :               seen_logical |= value;
    9951                 :             :             }
    9952                 :             : 
    9953                 :        1577 :           if (cp->low != NULL && cp->high != NULL
    9954                 :        1530 :               && cp->low != cp->high
    9955                 :        1733 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
    9956                 :             :             {
    9957                 :          35 :               if (warn_surprising)
    9958                 :           1 :                 gfc_warning (OPT_Wsurprising,
    9959                 :             :                              "Range specification at %L can never be matched",
    9960                 :             :                              &cp->where);
    9961                 :             : 
    9962                 :          35 :               cp->unreachable = 1;
    9963                 :          35 :               seen_unreachable = 1;
    9964                 :             :             }
    9965                 :             :           else
    9966                 :             :             {
    9967                 :             :               /* If the case range can be matched, it can also overlap with
    9968                 :             :                  other cases.  To make sure it does not, we put it in a
    9969                 :             :                  double linked list here.  We sort that with a merge sort
    9970                 :             :                  later on to detect any overlapping cases.  */
    9971                 :        1586 :               if (!head)
    9972                 :             :                 {
    9973                 :         642 :                   head = tail = cp;
    9974                 :         642 :                   head->right = head->left = NULL;
    9975                 :             :                 }
    9976                 :             :               else
    9977                 :             :                 {
    9978                 :         944 :                   tail->right = cp;
    9979                 :         944 :                   tail->right->left = tail;
    9980                 :         944 :                   tail = tail->right;
    9981                 :         944 :                   tail->right = NULL;
    9982                 :             :                 }
    9983                 :             :             }
    9984                 :             :         }
    9985                 :             : 
    9986                 :             :       /* It there was a failure in the previous case label, give up
    9987                 :             :          for this case label list.  Continue with the next block.  */
    9988                 :        1795 :       if (!t)
    9989                 :          11 :         continue;
    9990                 :             : 
    9991                 :             :       /* See if any case labels that are unreachable have been seen.
    9992                 :             :          If so, we eliminate them.  This is a bit of a kludge because
    9993                 :             :          the case lists for a single case statement (label) is a
    9994                 :             :          single forward linked lists.  */
    9995                 :        1784 :       if (seen_unreachable)
    9996                 :             :       {
    9997                 :             :         /* Advance until the first case in the list is reachable.  */
    9998                 :          69 :         while (body->ext.block.case_list != NULL
    9999                 :          69 :                && body->ext.block.case_list->unreachable)
   10000                 :             :           {
   10001                 :          34 :             gfc_case *n = body->ext.block.case_list;
   10002                 :          34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10003                 :          34 :             n->next = NULL;
   10004                 :          34 :             gfc_free_case_list (n);
   10005                 :             :           }
   10006                 :             : 
   10007                 :             :         /* Strip all other unreachable cases.  */
   10008                 :          35 :         if (body->ext.block.case_list)
   10009                 :             :           {
   10010                 :           2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10011                 :             :               {
   10012                 :           1 :                 if (cp->next->unreachable)
   10013                 :             :                   {
   10014                 :           1 :                     gfc_case *n = cp->next;
   10015                 :           1 :                     cp->next = cp->next->next;
   10016                 :           1 :                     n->next = NULL;
   10017                 :           1 :                     gfc_free_case_list (n);
   10018                 :             :                   }
   10019                 :             :               }
   10020                 :             :           }
   10021                 :             :       }
   10022                 :             :     }
   10023                 :             : 
   10024                 :             :   /* See if there were overlapping cases.  If the check returns NULL,
   10025                 :             :      there was overlap.  In that case we don't do anything.  If head
   10026                 :             :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10027                 :             :      then used during code generation for SELECT CASE constructs with
   10028                 :             :      a case expression of a CHARACTER type.  */
   10029                 :         677 :   if (head)
   10030                 :             :     {
   10031                 :         642 :       head = check_case_overlap (head);
   10032                 :             : 
   10033                 :             :       /* Prepend the default_case if it is there.  */
   10034                 :         642 :       if (head != NULL && default_case)
   10035                 :             :         {
   10036                 :         344 :           default_case->left = NULL;
   10037                 :         344 :           default_case->right = head;
   10038                 :         344 :           head->left = default_case;
   10039                 :             :         }
   10040                 :             :     }
   10041                 :             : 
   10042                 :             :   /* Eliminate dead blocks that may be the result if we've seen
   10043                 :             :      unreachable case labels for a block.  */
   10044                 :        2438 :   for (body = code; body && body->block; body = body->block)
   10045                 :             :     {
   10046                 :        1761 :       if (body->block->ext.block.case_list == NULL)
   10047                 :             :         {
   10048                 :             :           /* Cut the unreachable block from the code chain.  */
   10049                 :          34 :           gfc_code *c = body->block;
   10050                 :          34 :           body->block = c->block;
   10051                 :             : 
   10052                 :             :           /* Kill the dead block, but not the blocks below it.  */
   10053                 :          34 :           c->block = NULL;
   10054                 :          34 :           gfc_free_statements (c);
   10055                 :             :         }
   10056                 :             :     }
   10057                 :             : 
   10058                 :             :   /* More than two cases is legal but insane for logical selects.
   10059                 :             :      Issue a warning for it.  */
   10060                 :         677 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10061                 :           0 :     gfc_warning (OPT_Wsurprising,
   10062                 :             :                  "Logical SELECT CASE block at %L has more that two cases",
   10063                 :             :                  &code->loc);
   10064                 :             : }
   10065                 :             : 
   10066                 :             : 
   10067                 :             : /* Check if a derived type is extensible.  */
   10068                 :             : 
   10069                 :             : bool
   10070                 :       22755 : gfc_type_is_extensible (gfc_symbol *sym)
   10071                 :             : {
   10072                 :       22755 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10073                 :       22739 :            || (sym->attr.is_class
   10074                 :        2056 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10075                 :             : }
   10076                 :             : 
   10077                 :             : 
   10078                 :             : static void
   10079                 :             : resolve_types (gfc_namespace *ns);
   10080                 :             : 
   10081                 :             : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10082                 :             :    correct as well as possibly the array-spec.  */
   10083                 :             : 
   10084                 :             : static void
   10085                 :       12187 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10086                 :             : {
   10087                 :       12187 :   gfc_expr* target;
   10088                 :       12187 :   bool parentheses = false;
   10089                 :             : 
   10090                 :       12187 :   gcc_assert (sym->assoc);
   10091                 :       12187 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10092                 :             : 
   10093                 :             :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10094                 :             :      case, return.  Resolution will be called later manually again when
   10095                 :             :      this is done.  */
   10096                 :       12187 :   target = sym->assoc->target;
   10097                 :       12187 :   if (!target)
   10098                 :             :     return;
   10099                 :        7212 :   gcc_assert (!sym->assoc->dangling);
   10100                 :             : 
   10101                 :        7212 :   if (target->expr_type == EXPR_OP
   10102                 :         198 :       && target->value.op.op == INTRINSIC_PARENTHESES
   10103                 :          42 :       && target->value.op.op1->expr_type == EXPR_VARIABLE)
   10104                 :             :     {
   10105                 :          23 :       sym->assoc->target = gfc_copy_expr (target->value.op.op1);
   10106                 :          23 :       gfc_free_expr (target);
   10107                 :          23 :       target = sym->assoc->target;
   10108                 :          23 :       parentheses = true;
   10109                 :             :     }
   10110                 :             : 
   10111                 :        7212 :   if (resolve_target && !gfc_resolve_expr (target))
   10112                 :             :     return;
   10113                 :             : 
   10114                 :        7208 :   if (sym->assoc->ar)
   10115                 :             :     {
   10116                 :             :       int dim;
   10117                 :             :       gfc_array_ref *ar = sym->assoc->ar;
   10118                 :          65 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10119                 :             :         {
   10120                 :          39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10121                 :          39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10122                 :          78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10123                 :          39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10124                 :           0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10125                 :             :                        "remapping of associate name %s at %L",
   10126                 :             :                        sym->name, &sym->declared_at);
   10127                 :             :         }
   10128                 :             :     }
   10129                 :             : 
   10130                 :             :   /* For variable targets, we get some attributes from the target.  */
   10131                 :        7208 :   if (target->expr_type == EXPR_VARIABLE)
   10132                 :             :     {
   10133                 :        6363 :       gfc_symbol *tsym, *dsym;
   10134                 :             : 
   10135                 :        6363 :       gcc_assert (target->symtree);
   10136                 :        6363 :       tsym = target->symtree->n.sym;
   10137                 :             : 
   10138                 :        6363 :       if (gfc_expr_attr (target).proc_pointer)
   10139                 :             :         {
   10140                 :           0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10141                 :             :                      tsym->name, &target->where);
   10142                 :           0 :           return;
   10143                 :             :         }
   10144                 :             : 
   10145                 :          74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10146                 :           2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10147                 :        6364 :           && dsym->attr.flavor == FL_DERIVED)
   10148                 :             :         {
   10149                 :           1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10150                 :             :                      tsym->name, &target->where);
   10151                 :           1 :           return;
   10152                 :             :         }
   10153                 :             : 
   10154                 :        6362 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10155                 :             :         {
   10156                 :          73 :           bool is_error = true;
   10157                 :          73 :           if (tsym->attr.function && tsym->result == tsym)
   10158                 :         141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10159                 :         137 :               if (tsym == ns->proc_name)
   10160                 :             :                 {
   10161                 :             :                   is_error = false;
   10162                 :             :                   break;
   10163                 :             :                 }
   10164                 :          64 :           if (is_error)
   10165                 :             :             {
   10166                 :          13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10167                 :             :                          tsym->name, &target->where);
   10168                 :          13 :               return;
   10169                 :             :             }
   10170                 :             :         }
   10171                 :             : 
   10172                 :        6349 :       sym->attr.asynchronous = tsym->attr.asynchronous;
   10173                 :        6349 :       sym->attr.volatile_ = tsym->attr.volatile_;
   10174                 :             : 
   10175                 :       12698 :       sym->attr.target = tsym->attr.target
   10176                 :        6349 :                          || gfc_expr_attr (target).pointer;
   10177                 :        6349 :       if (is_subref_array (target))
   10178                 :         378 :         sym->attr.subref_array_pointer = 1;
   10179                 :             :     }
   10180                 :         845 :   else if (target->ts.type == BT_PROCEDURE)
   10181                 :             :     {
   10182                 :           0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10183                 :             :                  &target->where);
   10184                 :           0 :       return;
   10185                 :             :     }
   10186                 :             : 
   10187                 :        7194 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10188                 :             :     {
   10189                 :             :       /* By now, the type of the target has been fixed up.  */
   10190                 :         287 :       symbol_attribute attr;
   10191                 :             : 
   10192                 :         287 :       if (sym->ts.type == BT_DERIVED
   10193                 :         160 :           && target->ts.type == BT_CLASS
   10194                 :          31 :           && !UNLIMITED_POLY (target))
   10195                 :             :         {
   10196                 :             :           /* Inferred to be derived type but the target has type class.  */
   10197                 :          31 :           sym->ts = CLASS_DATA (target)->ts;
   10198                 :          31 :           if (!sym->as)
   10199                 :          31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10200                 :          31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10201                 :          31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10202                 :          31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10203                 :             :                             target->corank);
   10204                 :          31 :           sym->as = NULL;
   10205                 :             :         }
   10206                 :         256 :       else if (target->ts.type == BT_DERIVED
   10207                 :         129 :                && target->symtree && target->symtree->n.sym
   10208                 :         105 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10209                 :           0 :                && IS_INFERRED_TYPE (target)
   10210                 :           0 :                && target->ref && target->ref->next
   10211                 :           0 :                && target->ref->next->type == REF_ARRAY
   10212                 :           0 :                && !target->ref->next->next)
   10213                 :             :         {
   10214                 :             :           /* A inferred type selector whose symbol has been determined to be
   10215                 :             :              a class array but which only has an array reference. Change the
   10216                 :             :              associate name and the selector to class type.  */
   10217                 :           0 :           sym->ts = target->ts;
   10218                 :           0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10219                 :           0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10220                 :           0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10221                 :             :                             target->corank);
   10222                 :           0 :           sym->as = NULL;
   10223                 :           0 :           target->ts = sym->ts;
   10224                 :             :         }
   10225                 :         256 :       else if ((target->ts.type == BT_DERIVED)
   10226                 :         127 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10227                 :          61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10228                 :             :         /* Confirmed to be either a derived type or misidentified to be a
   10229                 :             :            scalar class object, when the selector is a class array.  */
   10230                 :         135 :         sym->ts = target->ts;
   10231                 :             :     }
   10232                 :             : 
   10233                 :             : 
   10234                 :        7194 :   if (target->expr_type == EXPR_NULL)
   10235                 :             :     {
   10236                 :           1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10237                 :           1 :       return;
   10238                 :             :     }
   10239                 :        7193 :   else if (target->ts.type == BT_UNKNOWN)
   10240                 :             :     {
   10241                 :           2 :       gfc_error ("Selector at %L has no type", &target->where);
   10242                 :           2 :       return;
   10243                 :             :     }
   10244                 :             : 
   10245                 :             :   /* Get type if this was not already set.  Note that it can be
   10246                 :             :      some other type than the target in case this is a SELECT TYPE
   10247                 :             :      selector!  So we must not update when the type is already there.  */
   10248                 :        7191 :   if (sym->ts.type == BT_UNKNOWN)
   10249                 :         208 :     sym->ts = target->ts;
   10250                 :             : 
   10251                 :        7191 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10252                 :             : 
   10253                 :             :   /* See if this is a valid association-to-variable.  */
   10254                 :       14382 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10255                 :        6349 :                            && !parentheses
   10256                 :        6328 :                            && !gfc_has_vector_subscript (target))
   10257                 :        7239 :                           || gfc_is_ptr_fcn (target));
   10258                 :             : 
   10259                 :             :   /* Finally resolve if this is an array or not.  */
   10260                 :        7191 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10261                 :         125 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10262                 :             :     {
   10263                 :          86 :       gfc_expression_rank (target);
   10264                 :          86 :       if (target->ts.type == BT_DERIVED
   10265                 :          39 :           && !sym->as
   10266                 :          39 :           && target->symtree->n.sym->as)
   10267                 :             :         {
   10268                 :           0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10269                 :           0 :           sym->attr.dimension = 1;
   10270                 :             :         }
   10271                 :          86 :       else if (target->ts.type == BT_CLASS
   10272                 :          47 :                && CLASS_DATA (target)->as)
   10273                 :             :         {
   10274                 :           0 :           target->rank = CLASS_DATA (target)->as->rank;
   10275                 :           0 :           target->corank = CLASS_DATA (target)->as->corank;
   10276                 :           0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10277                 :             :             {
   10278                 :           0 :               sym->ts = target->ts;
   10279                 :           0 :               sym->attr.dimension = 0;
   10280                 :             :             }
   10281                 :             :         }
   10282                 :             :     }
   10283                 :             : 
   10284                 :             : 
   10285                 :        7191 :   if (sym->attr.dimension && target->rank == 0)
   10286                 :             :     {
   10287                 :             :       /* primary.cc makes the assumption that a reference to an associate
   10288                 :             :          name followed by a left parenthesis is an array reference.  */
   10289                 :          17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10290                 :             :         {
   10291                 :          12 :           gfc_expression_rank (sym->assoc->target);
   10292                 :          12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10293                 :          12 :           if (!sym->attr.dimension && sym->as)
   10294                 :           0 :             sym->as = NULL;
   10295                 :             :         }
   10296                 :             : 
   10297                 :          17 :       if (sym->attr.dimension && target->rank == 0)
   10298                 :             :         {
   10299                 :           5 :           if (sym->ts.type != BT_CHARACTER)
   10300                 :           5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10301                 :             :                        sym->name, &sym->declared_at);
   10302                 :           5 :           sym->attr.dimension = 0;
   10303                 :           5 :           return;
   10304                 :             :         }
   10305                 :             :     }
   10306                 :             : 
   10307                 :             :   /* We cannot deal with class selectors that need temporaries.  */
   10308                 :        7186 :   if (target->ts.type == BT_CLASS
   10309                 :        7186 :         && gfc_ref_needs_temporary_p (target->ref))
   10310                 :             :     {
   10311                 :           1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10312                 :             :                  "yet implemented", &target->where);
   10313                 :           1 :       return;
   10314                 :             :     }
   10315                 :             : 
   10316                 :        7185 :   if (target->ts.type == BT_CLASS)
   10317                 :        2676 :     gfc_fix_class_refs (target);
   10318                 :             : 
   10319                 :        7185 :   if ((target->rank > 0 || target->corank > 0)
   10320                 :        2621 :       && !sym->attr.select_rank_temporary)
   10321                 :             :     {
   10322                 :        2621 :       gfc_array_spec *as;
   10323                 :             :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10324                 :             :          it is corrected now.  */
   10325                 :        2621 :       if (sym->ts.type != BT_CLASS
   10326                 :        2066 :           && (!sym->as || sym->as->corank != target->corank))
   10327                 :             :         {
   10328                 :         135 :           if (!sym->as)
   10329                 :         128 :             sym->as = gfc_get_array_spec ();
   10330                 :         135 :           as = sym->as;
   10331                 :         135 :           as->rank = target->rank;
   10332                 :         135 :           as->type = AS_DEFERRED;
   10333                 :         135 :           as->corank = target->corank;
   10334                 :         135 :           sym->attr.dimension = 1;
   10335                 :         135 :           if (as->corank != 0)
   10336                 :           7 :             sym->attr.codimension = 1;
   10337                 :             :         }
   10338                 :        2486 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10339                 :         554 :                && (!CLASS_DATA (sym)->as
   10340                 :         554 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10341                 :             :         {
   10342                 :           0 :           if (!CLASS_DATA (sym)->as)
   10343                 :           0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10344                 :           0 :           as = CLASS_DATA (sym)->as;
   10345                 :           0 :           as->rank = target->rank;
   10346                 :           0 :           as->type = AS_DEFERRED;
   10347                 :           0 :           as->corank = target->corank;
   10348                 :           0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10349                 :           0 :           if (as->corank != 0)
   10350                 :           0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10351                 :             :         }
   10352                 :             :     }
   10353                 :        4564 :   else if (!sym->attr.select_rank_temporary)
   10354                 :             :     {
   10355                 :             :       /* target's rank is 0, but the type of the sym is still array valued,
   10356                 :             :          which has to be corrected.  */
   10357                 :        3181 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10358                 :         653 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10359                 :             :         {
   10360                 :          24 :           gfc_array_spec *as;
   10361                 :          24 :           symbol_attribute attr;
   10362                 :             :           /* The associated variable's type is still the array type
   10363                 :             :              correct this now.  */
   10364                 :          24 :           gfc_typespec *ts = &target->ts;
   10365                 :          24 :           gfc_ref *ref;
   10366                 :             :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10367                 :             :            */
   10368                 :          24 :           bool internal_ref = true;
   10369                 :             : 
   10370                 :          72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10371                 :             :             {
   10372                 :          48 :               switch (ref->type)
   10373                 :             :                 {
   10374                 :          24 :                 case REF_COMPONENT:
   10375                 :          24 :                   ts = &ref->u.c.component->ts;
   10376                 :          24 :                   internal_ref
   10377                 :          24 :                     = target->ref == ref && ref->next
   10378                 :          48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10379                 :             :                   break;
   10380                 :          24 :                 case REF_ARRAY:
   10381                 :          24 :                   if (ts->type == BT_CLASS)
   10382                 :           0 :                     ts = &ts->u.derived->components->ts;
   10383                 :          24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10384                 :           0 :                     for (int i = ref->u.ar.dimen;
   10385                 :             :                          internal_ref
   10386                 :           0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10387                 :             :                          ++i)
   10388                 :           0 :                       internal_ref
   10389                 :           0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10390                 :             :                   break;
   10391                 :             :                 default:
   10392                 :             :                   break;
   10393                 :             :                 }
   10394                 :             :             }
   10395                 :             :           /* Only rewrite the type of this symbol, when the refs are not the
   10396                 :             :              internal ones for class and co-array this-image.  */
   10397                 :          24 :           if (!internal_ref)
   10398                 :             :             {
   10399                 :             :               /* Create a scalar instance of the current class type.  Because
   10400                 :             :                  the rank of a class array goes into its name, the type has to
   10401                 :             :                  be rebuilt.  The alternative of (re-)setting just the
   10402                 :             :                  attributes and as in the current type, destroys the type also
   10403                 :             :                  in other places.  */
   10404                 :           0 :               as = NULL;
   10405                 :           0 :               sym->ts = *ts;
   10406                 :           0 :               sym->ts.type = BT_CLASS;
   10407                 :           0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10408                 :           0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10409                 :           0 :               sym->as = NULL;
   10410                 :             :             }
   10411                 :             :         }
   10412                 :             :     }
   10413                 :             : 
   10414                 :             :   /* Mark this as an associate variable.  */
   10415                 :        7185 :   sym->attr.associate_var = 1;
   10416                 :             : 
   10417                 :             :   /* Fix up the type-spec for CHARACTER types.  */
   10418                 :        7185 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10419                 :             :     {
   10420                 :         442 :       gfc_ref *ref;
   10421                 :         709 :       for (ref = target->ref; ref; ref = ref->next)
   10422                 :         293 :         if (ref->type == REF_SUBSTRING
   10423                 :          68 :             && (ref->u.ss.start == NULL
   10424                 :          68 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10425                 :          68 :                 || ref->u.ss.end == NULL
   10426                 :          48 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10427                 :             :           break;
   10428                 :             : 
   10429                 :         442 :       if (!sym->ts.u.cl)
   10430                 :         122 :         sym->ts.u.cl = target->ts.u.cl;
   10431                 :             : 
   10432                 :         442 :       if (sym->ts.deferred
   10433                 :         189 :           && sym->ts.u.cl == target->ts.u.cl)
   10434                 :             :         {
   10435                 :         110 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10436                 :         110 :           sym->ts.deferred = 1;
   10437                 :             :         }
   10438                 :             : 
   10439                 :         442 :       if (!sym->ts.u.cl->length
   10440                 :         326 :           && !sym->ts.deferred
   10441                 :         137 :           && target->expr_type == EXPR_CONSTANT)
   10442                 :             :         {
   10443                 :          30 :           sym->ts.u.cl->length =
   10444                 :          30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10445                 :             :                                   target->value.character.length);
   10446                 :             :         }
   10447                 :         412 :       else if (((!sym->ts.u.cl->length
   10448                 :         116 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10449                 :         302 :                 && target->expr_type != EXPR_VARIABLE)
   10450                 :         290 :                || ref)
   10451                 :             :         {
   10452                 :         148 :           if (!sym->ts.deferred)
   10453                 :             :             {
   10454                 :          44 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10455                 :          44 :               sym->ts.deferred = 1;
   10456                 :             :             }
   10457                 :             : 
   10458                 :             :           /* This is reset in trans-stmt.cc after the assignment
   10459                 :             :              of the target expression to the associate name.  */
   10460                 :         148 :           if (ref && sym->as)
   10461                 :          26 :             sym->attr.pointer = 1;
   10462                 :             :           else
   10463                 :         122 :             sym->attr.allocatable = 1;
   10464                 :             :         }
   10465                 :             :     }
   10466                 :             : 
   10467                 :        7185 :   if (sym->ts.type == BT_CLASS
   10468                 :        1353 :       && IS_INFERRED_TYPE (target)
   10469                 :          13 :       && target->ts.type == BT_DERIVED
   10470                 :           0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10471                 :           0 :       && target->ref && target->ref->next && !target->ref->next->next
   10472                 :           0 :       && target->ref->next->type == REF_ARRAY)
   10473                 :           0 :     target->ts = target->symtree->n.sym->ts;
   10474                 :             : 
   10475                 :             :   /* If the target is a good class object, so is the associate variable.  */
   10476                 :        7185 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10477                 :         666 :     sym->attr.class_ok = 1;
   10478                 :             : }
   10479                 :             : 
   10480                 :             : 
   10481                 :             : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   10482                 :             :    array reference, where necessary.  The symbols are artificial and so
   10483                 :             :    the dimension attribute and arrayspec can also be set.  In addition,
   10484                 :             :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   10485                 :             :    This is corrected here as well.*/
   10486                 :             : 
   10487                 :             : static void
   10488                 :        1644 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   10489                 :             :                  gfc_ref *ref)
   10490                 :             : {
   10491                 :        1644 :   gfc_ref *nref = (*expr1)->ref;
   10492                 :        1644 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   10493                 :        1644 :   gfc_symbol *sym2;
   10494                 :        1644 :   gfc_expr *selector = gfc_copy_expr (expr2);
   10495                 :             : 
   10496                 :        1644 :   (*expr1)->rank = rank;
   10497                 :        1644 :   (*expr1)->corank = corank;
   10498                 :        1644 :   if (selector)
   10499                 :             :     {
   10500                 :         309 :       gfc_resolve_expr (selector);
   10501                 :         309 :       if (selector->expr_type == EXPR_OP
   10502                 :           2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   10503                 :           2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   10504                 :         307 :       else if (selector->expr_type == EXPR_VARIABLE
   10505                 :           7 :                || selector->expr_type == EXPR_FUNCTION)
   10506                 :         307 :         sym2 = selector->symtree->n.sym;
   10507                 :             :       else
   10508                 :           0 :         gcc_unreachable ();
   10509                 :             :     }
   10510                 :             :   else
   10511                 :             :     sym2 = NULL;
   10512                 :             : 
   10513                 :        1644 :   if (sym1->ts.type == BT_CLASS)
   10514                 :             :     {
   10515                 :        1644 :       if ((*expr1)->ts.type != BT_CLASS)
   10516                 :          13 :         (*expr1)->ts = sym1->ts;
   10517                 :             : 
   10518                 :        1644 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   10519                 :        1644 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   10520                 :        1644 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   10521                 :           1 :         CLASS_DATA (sym1)->as
   10522                 :           1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   10523                 :             :     }
   10524                 :             :   else
   10525                 :             :     {
   10526                 :           0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   10527                 :           0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   10528                 :           0 :       if (sym1->as == NULL && sym2)
   10529                 :           0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   10530                 :             :     }
   10531                 :             : 
   10532                 :        2973 :   for (; nref; nref = nref->next)
   10533                 :        2664 :     if (nref->next == NULL)
   10534                 :             :       break;
   10535                 :             : 
   10536                 :        1644 :   if (ref && nref && nref->type != REF_ARRAY)
   10537                 :           6 :     nref->next = gfc_copy_ref (ref);
   10538                 :        1638 :   else if (ref && !nref)
   10539                 :         300 :     (*expr1)->ref = gfc_copy_ref (ref);
   10540                 :        1338 :   else if (ref && nref->u.ar.codimen != corank)
   10541                 :             :     {
   10542                 :         912 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   10543                 :         855 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   10544                 :          57 :       nref->u.ar.codimen = corank;
   10545                 :             :     }
   10546                 :        1644 : }
   10547                 :             : 
   10548                 :             : 
   10549                 :             : static gfc_expr *
   10550                 :        6415 : build_loc_call (gfc_expr *sym_expr)
   10551                 :             : {
   10552                 :        6415 :   gfc_expr *loc_call;
   10553                 :        6415 :   loc_call = gfc_get_expr ();
   10554                 :        6415 :   loc_call->expr_type = EXPR_FUNCTION;
   10555                 :        6415 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   10556                 :        6415 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   10557                 :        6415 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   10558                 :        6415 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   10559                 :        6415 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   10560                 :        6415 :   loc_call->ts.type = BT_INTEGER;
   10561                 :        6415 :   loc_call->ts.kind = gfc_index_integer_kind;
   10562                 :        6415 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   10563                 :        6415 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   10564                 :        6415 :   loc_call->value.function.actual->expr = sym_expr;
   10565                 :        6415 :   loc_call->where = sym_expr->where;
   10566                 :        6415 :   return loc_call;
   10567                 :             : }
   10568                 :             : 
   10569                 :             : /* Resolve a SELECT TYPE statement.  */
   10570                 :             : 
   10571                 :             : static void
   10572                 :        2869 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   10573                 :             : {
   10574                 :        2869 :   gfc_symbol *selector_type;
   10575                 :        2869 :   gfc_code *body, *new_st, *if_st, *tail;
   10576                 :        2869 :   gfc_code *class_is = NULL, *default_case = NULL;
   10577                 :        2869 :   gfc_case *c;
   10578                 :        2869 :   gfc_symtree *st;
   10579                 :        2869 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   10580                 :        2869 :   gfc_namespace *ns;
   10581                 :        2869 :   int error = 0;
   10582                 :        2869 :   int rank = 0, corank = 0;
   10583                 :        2869 :   gfc_ref* ref = NULL;
   10584                 :        2869 :   gfc_expr *selector_expr = NULL;
   10585                 :             : 
   10586                 :        2869 :   ns = code->ext.block.ns;
   10587                 :        2869 :   if (code->expr2)
   10588                 :             :     {
   10589                 :             :       /* Set this, or coarray checks in resolve will fail.  */
   10590                 :         614 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   10591                 :             :     }
   10592                 :        2869 :   gfc_resolve (ns);
   10593                 :             : 
   10594                 :             :   /* Check for F03:C813.  */
   10595                 :        2869 :   if (code->expr1->ts.type != BT_CLASS
   10596                 :          36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   10597                 :             :     {
   10598                 :          13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   10599                 :             :                  "at %L", &code->loc);
   10600                 :          41 :       return;
   10601                 :             :     }
   10602                 :             : 
   10603                 :             :   /* Prevent segfault, when class type is not initialized due to previous
   10604                 :             :      error.  */
   10605                 :        2856 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   10606                 :        2854 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   10607                 :             :     return;
   10608                 :             : 
   10609                 :        2849 :   if (code->expr2)
   10610                 :             :     {
   10611                 :         605 :       gfc_ref *ref2 = NULL;
   10612                 :        1431 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   10613                 :         826 :          if (ref->type == REF_COMPONENT
   10614                 :         425 :              && ref->u.c.component->ts.type == BT_CLASS)
   10615                 :         826 :            ref2 = ref;
   10616                 :             : 
   10617                 :         605 :       if (ref2)
   10618                 :             :         {
   10619                 :         333 :           if (code->expr1->symtree->n.sym->attr.untyped)
   10620                 :           1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   10621                 :         333 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   10622                 :             :         }
   10623                 :             :       else
   10624                 :             :         {
   10625                 :         272 :           if (code->expr1->symtree->n.sym->attr.untyped)
   10626                 :          28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   10627                 :             :           /* Sometimes the selector expression is given the typespec of the
   10628                 :             :              '_data' field, which is logical enough but inappropriate here. */
   10629                 :         272 :           if (code->expr2->ts.type == BT_DERIVED
   10630                 :          80 :               && code->expr2->symtree
   10631                 :          80 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   10632                 :          80 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   10633                 :         272 :           selector_type = CLASS_DATA (code->expr2)
   10634                 :         272 :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   10635                 :             :         }
   10636                 :             : 
   10637                 :         605 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   10638                 :             :         {
   10639                 :         295 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   10640                 :         295 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   10641                 :         295 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   10642                 :             :         }
   10643                 :             : 
   10644                 :             :       /* F2008: C803 The selector expression must not be coindexed.  */
   10645                 :         605 :       if (gfc_is_coindexed (code->expr2))
   10646                 :             :         {
   10647                 :           3 :           gfc_error ("Selector at %L must not be coindexed",
   10648                 :           3 :                      &code->expr2->where);
   10649                 :           3 :           return;
   10650                 :             :         }
   10651                 :             : 
   10652                 :             :     }
   10653                 :             :   else
   10654                 :             :     {
   10655                 :        2244 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   10656                 :             : 
   10657                 :        2244 :       if (gfc_is_coindexed (code->expr1))
   10658                 :             :         {
   10659                 :           0 :           gfc_error ("Selector at %L must not be coindexed",
   10660                 :           0 :                      &code->expr1->where);
   10661                 :           0 :           return;
   10662                 :             :         }
   10663                 :             :     }
   10664                 :             : 
   10665                 :             :   /* Loop over TYPE IS / CLASS IS cases.  */
   10666                 :        7999 :   for (body = code->block; body; body = body->block)
   10667                 :             :     {
   10668                 :        5154 :       c = body->ext.block.case_list;
   10669                 :             : 
   10670                 :        5154 :       if (!error)
   10671                 :             :         {
   10672                 :             :           /* Check for repeated cases.  */
   10673                 :        8020 :           for (tail = code->block; tail; tail = tail->block)
   10674                 :             :             {
   10675                 :        8020 :               gfc_case *d = tail->ext.block.case_list;
   10676                 :        8020 :               if (tail == body)
   10677                 :             :                 break;
   10678                 :             : 
   10679                 :        2875 :               if (c->ts.type == d->ts.type
   10680                 :         513 :                   && ((c->ts.type == BT_DERIVED
   10681                 :         415 :                        && c->ts.u.derived && d->ts.u.derived
   10682                 :         415 :                        && !strcmp (c->ts.u.derived->name,
   10683                 :             :                                    d->ts.u.derived->name))
   10684                 :         512 :                       || c->ts.type == BT_UNKNOWN
   10685                 :         512 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   10686                 :          55 :                           && c->ts.kind == d->ts.kind)))
   10687                 :             :                 {
   10688                 :           1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   10689                 :             :                              &c->where, &d->where);
   10690                 :           1 :                   return;
   10691                 :             :                 }
   10692                 :             :             }
   10693                 :             :         }
   10694                 :             : 
   10695                 :             :       /* Check F03:C815.  */
   10696                 :        3248 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   10697                 :        2215 :           && selector_type
   10698                 :        2215 :           && !selector_type->attr.unlimited_polymorphic
   10699                 :        7073 :           && !gfc_type_is_extensible (c->ts.u.derived))
   10700                 :             :         {
   10701                 :           1 :           gfc_error ("Derived type %qs at %L must be extensible",
   10702                 :           1 :                      c->ts.u.derived->name, &c->where);
   10703                 :           1 :           error++;
   10704                 :           1 :           continue;
   10705                 :             :         }
   10706                 :             : 
   10707                 :             :       /* Check F03:C816.  */
   10708                 :        5158 :       if (c->ts.type != BT_UNKNOWN
   10709                 :        3585 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   10710                 :        7075 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   10711                 :        1919 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   10712                 :             :         {
   10713                 :           6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   10714                 :           2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   10715                 :           2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   10716                 :             :           else
   10717                 :           4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   10718                 :             :                        gfc_basic_typename (c->ts.type), &c->where);
   10719                 :           6 :           error++;
   10720                 :           6 :           continue;
   10721                 :             :         }
   10722                 :             : 
   10723                 :             :       /* Check F03:C814.  */
   10724                 :        5146 :       if (c->ts.type == BT_CHARACTER
   10725                 :         730 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   10726                 :             :         {
   10727                 :           0 :           gfc_error ("The type-spec at %L shall specify that each length "
   10728                 :             :                      "type parameter is assumed", &c->where);
   10729                 :           0 :           error++;
   10730                 :           0 :           continue;
   10731                 :             :         }
   10732                 :             : 
   10733                 :             :       /* Intercept the DEFAULT case.  */
   10734                 :        5146 :       if (c->ts.type == BT_UNKNOWN)
   10735                 :             :         {
   10736                 :             :           /* Check F03:C818.  */
   10737                 :        1567 :           if (default_case)
   10738                 :             :             {
   10739                 :           1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10740                 :             :                          "by a second DEFAULT CASE at %L",
   10741                 :           1 :                          &default_case->ext.block.case_list->where, &c->where);
   10742                 :           1 :               error++;
   10743                 :           1 :               continue;
   10744                 :             :             }
   10745                 :             : 
   10746                 :             :           default_case = body;
   10747                 :             :         }
   10748                 :             :     }
   10749                 :             : 
   10750                 :        2845 :   if (error > 0)
   10751                 :             :     return;
   10752                 :             : 
   10753                 :             :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   10754                 :             :      target if present.  If there are any EXIT statements referring to the
   10755                 :             :      SELECT TYPE construct, this is no problem because the gfc_code
   10756                 :             :      reference stays the same and EXIT is equally possible from the BLOCK
   10757                 :             :      it is changed to.  */
   10758                 :        2842 :   code->op = EXEC_BLOCK;
   10759                 :        2842 :   if (code->expr2)
   10760                 :             :     {
   10761                 :         602 :       gfc_association_list* assoc;
   10762                 :             : 
   10763                 :         602 :       assoc = gfc_get_association_list ();
   10764                 :         602 :       assoc->st = code->expr1->symtree;
   10765                 :         602 :       assoc->target = gfc_copy_expr (code->expr2);
   10766                 :         602 :       assoc->target->where = code->expr2->where;
   10767                 :             :       /* assoc->variable will be set by resolve_assoc_var.  */
   10768                 :             : 
   10769                 :         602 :       code->ext.block.assoc = assoc;
   10770                 :         602 :       code->expr1->symtree->n.sym->assoc = assoc;
   10771                 :             : 
   10772                 :         602 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   10773                 :             :     }
   10774                 :             :   else
   10775                 :        2240 :     code->ext.block.assoc = NULL;
   10776                 :             : 
   10777                 :             :   /* Ensure that the selector rank and arrayspec are available to
   10778                 :             :      correct expressions in which they might be missing.  */
   10779                 :        2842 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   10780                 :             :     {
   10781                 :         309 :       rank = code->expr2->rank;
   10782                 :         309 :       corank = code->expr2->corank;
   10783                 :         582 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   10784                 :         573 :         if (ref->next == NULL)
   10785                 :             :           break;
   10786                 :         309 :       if (ref && ref->type == REF_ARRAY)
   10787                 :         300 :         ref = gfc_copy_ref (ref);
   10788                 :             : 
   10789                 :             :       /* Fixup expr1 if necessary.  */
   10790                 :         309 :       if (rank || corank)
   10791                 :         309 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   10792                 :             :     }
   10793                 :        2533 :   else if (code->expr1->rank || code->expr1->corank)
   10794                 :             :     {
   10795                 :         827 :       rank = code->expr1->rank;
   10796                 :         827 :       corank = code->expr1->corank;
   10797                 :         827 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   10798                 :         827 :         if (ref->next == NULL)
   10799                 :             :           break;
   10800                 :         827 :       if (ref && ref->type == REF_ARRAY)
   10801                 :         827 :         ref = gfc_copy_ref (ref);
   10802                 :             :     }
   10803                 :             : 
   10804                 :             :   /* Add EXEC_SELECT to switch on type.  */
   10805                 :        2842 :   new_st = gfc_get_code (code->op);
   10806                 :        2842 :   new_st->expr1 = code->expr1;
   10807                 :        2842 :   new_st->expr2 = code->expr2;
   10808                 :        2842 :   new_st->block = code->block;
   10809                 :        2842 :   code->expr1 = code->expr2 =  NULL;
   10810                 :        2842 :   code->block = NULL;
   10811                 :        2842 :   if (!ns->code)
   10812                 :        2842 :     ns->code = new_st;
   10813                 :             :   else
   10814                 :           0 :     ns->code->next = new_st;
   10815                 :        2842 :   code = new_st;
   10816                 :        2842 :   code->op = EXEC_SELECT_TYPE;
   10817                 :             : 
   10818                 :             :   /* Use the intrinsic LOC function to generate an integer expression
   10819                 :             :      for the vtable of the selector.  Note that the rank of the selector
   10820                 :             :      expression has to be set to zero.  */
   10821                 :        2842 :   gfc_add_vptr_component (code->expr1);
   10822                 :        2842 :   code->expr1->rank = 0;
   10823                 :        2842 :   code->expr1->corank = 0;
   10824                 :        2842 :   code->expr1 = build_loc_call (code->expr1);
   10825                 :        2842 :   selector_expr = code->expr1->value.function.actual->expr;
   10826                 :             : 
   10827                 :             :   /* Loop over TYPE IS / CLASS IS cases.  */
   10828                 :        7980 :   for (body = code->block; body; body = body->block)
   10829                 :             :     {
   10830                 :        5138 :       gfc_symbol *vtab;
   10831                 :        5138 :       gfc_expr *e;
   10832                 :        5138 :       c = body->ext.block.case_list;
   10833                 :             : 
   10834                 :             :       /* Generate an index integer expression for address of the
   10835                 :             :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   10836                 :             :          is stored in c->high and is used to resolve intrinsic cases.  */
   10837                 :        5138 :       if (c->ts.type != BT_UNKNOWN)
   10838                 :             :         {
   10839                 :        3573 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   10840                 :             :             {
   10841                 :        2206 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   10842                 :        2206 :               gcc_assert (vtab);
   10843                 :        2206 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   10844                 :        2206 :                                           c->ts.u.derived->hash_value);
   10845                 :             :             }
   10846                 :             :           else
   10847                 :             :             {
   10848                 :        1367 :               vtab = gfc_find_vtab (&c->ts);
   10849                 :        1367 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   10850                 :        1367 :               e = CLASS_DATA (vtab)->initializer;
   10851                 :        1367 :               c->high = gfc_copy_expr (e);
   10852                 :        1367 :               if (c->high->ts.kind != gfc_integer_4_kind)
   10853                 :             :                 {
   10854                 :           1 :                   gfc_typespec ts;
   10855                 :           1 :                   ts.kind = gfc_integer_4_kind;
   10856                 :           1 :                   ts.type = BT_INTEGER;
   10857                 :           1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   10858                 :             :                 }
   10859                 :             :             }
   10860                 :             : 
   10861                 :        3573 :           e = gfc_lval_expr_from_sym (vtab);
   10862                 :        3573 :           c->low = build_loc_call (e);
   10863                 :             :         }
   10864                 :             :       else
   10865                 :        1565 :         continue;
   10866                 :             : 
   10867                 :             :       /* Associate temporary to selector.  This should only be done
   10868                 :             :          when this case is actually true, so build a new ASSOCIATE
   10869                 :             :          that does precisely this here (instead of using the
   10870                 :             :          'global' one).  */
   10871                 :             : 
   10872                 :        3573 :       if (c->ts.type == BT_CLASS)
   10873                 :         308 :         sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
   10874                 :        3265 :       else if (c->ts.type == BT_DERIVED)
   10875                 :        1898 :         sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
   10876                 :        1367 :       else if (c->ts.type == BT_CHARACTER)
   10877                 :             :         {
   10878                 :         730 :           HOST_WIDE_INT charlen = 0;
   10879                 :         730 :           if (c->ts.u.cl && c->ts.u.cl->length
   10880                 :           0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   10881                 :           0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   10882                 :         730 :           snprintf (name, sizeof (name),
   10883                 :             :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   10884                 :             :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   10885                 :             :         }
   10886                 :             :       else
   10887                 :         637 :         sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
   10888                 :             :                  c->ts.kind);
   10889                 :             : 
   10890                 :        3573 :       st = gfc_find_symtree (ns->sym_root, name);
   10891                 :        3573 :       gcc_assert (st->n.sym->assoc);
   10892                 :        3573 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   10893                 :        3573 :       st->n.sym->assoc->target->where = selector_expr->where;
   10894                 :        3573 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   10895                 :             :         {
   10896                 :        3265 :           gfc_add_data_component (st->n.sym->assoc->target);
   10897                 :             :           /* Fixup the target expression if necessary.  */
   10898                 :        3265 :           if (rank || corank)
   10899                 :        1335 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   10900                 :             :                              ref);
   10901                 :             :         }
   10902                 :             : 
   10903                 :        3573 :       new_st = gfc_get_code (EXEC_BLOCK);
   10904                 :        3573 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   10905                 :        3573 :       new_st->ext.block.ns->code = body->next;
   10906                 :        3573 :       body->next = new_st;
   10907                 :             : 
   10908                 :             :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   10909                 :             :          there is a CASE label overlap and this is already used.  Just ignore,
   10910                 :             :          the error is diagnosed elsewhere.  */
   10911                 :        3573 :       if (st->n.sym->assoc->dangling)
   10912                 :             :         {
   10913                 :        3572 :           new_st->ext.block.assoc = st->n.sym->assoc;
   10914                 :        3572 :           st->n.sym->assoc->dangling = 0;
   10915                 :             :         }
   10916                 :             : 
   10917                 :        3573 :       resolve_assoc_var (st->n.sym, false);
   10918                 :             :     }
   10919                 :             : 
   10920                 :             :   /* Take out CLASS IS cases for separate treatment.  */
   10921                 :             :   body = code;
   10922                 :        7980 :   while (body && body->block)
   10923                 :             :     {
   10924                 :        5138 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   10925                 :             :         {
   10926                 :             :           /* Add to class_is list.  */
   10927                 :         308 :           if (class_is == NULL)
   10928                 :             :             {
   10929                 :         277 :               class_is = body->block;
   10930                 :         277 :               tail = class_is;
   10931                 :             :             }
   10932                 :             :           else
   10933                 :             :             {
   10934                 :          43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   10935                 :          31 :               tail->block = body->block;
   10936                 :          31 :               tail = tail->block;
   10937                 :             :             }
   10938                 :             :           /* Remove from EXEC_SELECT list.  */
   10939                 :         308 :           body->block = body->block->block;
   10940                 :         308 :           tail->block = NULL;
   10941                 :             :         }
   10942                 :             :       else
   10943                 :             :         body = body->block;
   10944                 :             :     }
   10945                 :             : 
   10946                 :        2842 :   if (class_is)
   10947                 :             :     {
   10948                 :         277 :       gfc_symbol *vtab;
   10949                 :             : 
   10950                 :         277 :       if (!default_case)
   10951                 :             :         {
   10952                 :             :           /* Add a default case to hold the CLASS IS cases.  */
   10953                 :         275 :           for (tail = code; tail->block; tail = tail->block) ;
   10954                 :         167 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   10955                 :         167 :           tail = tail->block;
   10956                 :         167 :           tail->ext.block.case_list = gfc_get_case ();
   10957                 :         167 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   10958                 :         167 :           tail->next = NULL;
   10959                 :         167 :           default_case = tail;
   10960                 :             :         }
   10961                 :             : 
   10962                 :             :       /* More than one CLASS IS block?  */
   10963                 :         277 :       if (class_is->block)
   10964                 :             :         {
   10965                 :          37 :           gfc_code **c1,*c2;
   10966                 :          37 :           bool swapped;
   10967                 :             :           /* Sort CLASS IS blocks by extension level.  */
   10968                 :          36 :           do
   10969                 :             :             {
   10970                 :          37 :               swapped = false;
   10971                 :          97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   10972                 :             :                 {
   10973                 :          61 :                   c2 = (*c1)->block;
   10974                 :             :                   /* F03:C817 (check for doubles).  */
   10975                 :          61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   10976                 :          61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   10977                 :             :                     {
   10978                 :           1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   10979                 :             :                                  "statement at %L",
   10980                 :             :                                  &c2->ext.block.case_list->where);
   10981                 :           1 :                       return;
   10982                 :             :                     }
   10983                 :          60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   10984                 :          60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   10985                 :             :                     {
   10986                 :             :                       /* Swap.  */
   10987                 :          24 :                       (*c1)->block = c2->block;
   10988                 :          24 :                       c2->block = *c1;
   10989                 :          24 :                       *c1 = c2;
   10990                 :          24 :                       swapped = true;
   10991                 :             :                     }
   10992                 :             :                 }
   10993                 :             :             }
   10994                 :             :           while (swapped);
   10995                 :             :         }
   10996                 :             : 
   10997                 :             :       /* Generate IF chain.  */
   10998                 :         276 :       if_st = gfc_get_code (EXEC_IF);
   10999                 :         276 :       new_st = if_st;
   11000                 :         582 :       for (body = class_is; body; body = body->block)
   11001                 :             :         {
   11002                 :         306 :           new_st->block = gfc_get_code (EXEC_IF);
   11003                 :         306 :           new_st = new_st->block;
   11004                 :             :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11005                 :         306 :           new_st->expr1 = gfc_get_expr ();
   11006                 :         306 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11007                 :         306 :           new_st->expr1->ts.type = BT_LOGICAL;
   11008                 :         306 :           new_st->expr1->ts.kind = 4;
   11009                 :         306 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11010                 :         306 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11011                 :         306 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11012                 :             :           /* Set up arguments.  */
   11013                 :         306 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11014                 :         306 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11015                 :         306 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11016                 :         306 :           new_st->expr1->where = code->loc;
   11017                 :         306 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11018                 :         306 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11019                 :         306 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11020                 :         306 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11021                 :         306 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11022                 :         306 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11023                 :             :           /* Set up types in formal arg list.  */
   11024                 :         306 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11025                 :         306 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11026                 :         306 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11027                 :         306 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11028                 :             : 
   11029                 :         306 :           new_st->next = body->next;
   11030                 :             :         }
   11031                 :         276 :         if (default_case->next)
   11032                 :             :           {
   11033                 :         110 :             new_st->block = gfc_get_code (EXEC_IF);
   11034                 :         110 :             new_st = new_st->block;
   11035                 :         110 :             new_st->next = default_case->next;
   11036                 :             :           }
   11037                 :             : 
   11038                 :             :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11039                 :         276 :         default_case->next = if_st;
   11040                 :             :     }
   11041                 :             : 
   11042                 :             :   /* Resolve the internal code.  This cannot be done earlier because
   11043                 :             :      it requires that the sym->assoc of selectors is set already.  */
   11044                 :        2841 :   gfc_current_ns = ns;
   11045                 :        2841 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11046                 :        2841 :   gfc_current_ns = old_ns;
   11047                 :             : 
   11048                 :        2841 :   free (ref);
   11049                 :             : }
   11050                 :             : 
   11051                 :             : 
   11052                 :             : /* Resolve a SELECT RANK statement.  */
   11053                 :             : 
   11054                 :             : static void
   11055                 :        1018 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11056                 :             : {
   11057                 :        1018 :   gfc_namespace *ns;
   11058                 :        1018 :   gfc_code *body, *new_st, *tail;
   11059                 :        1018 :   gfc_case *c;
   11060                 :        1018 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11061                 :        1018 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11062                 :        1018 :   gfc_symtree *st;
   11063                 :        1018 :   gfc_expr *selector_expr = NULL;
   11064                 :        1018 :   int case_value;
   11065                 :        1018 :   HOST_WIDE_INT charlen = 0;
   11066                 :             : 
   11067                 :        1018 :   ns = code->ext.block.ns;
   11068                 :        1018 :   gfc_resolve (ns);
   11069                 :             : 
   11070                 :        1018 :   code->op = EXEC_BLOCK;
   11071                 :        1018 :   if (code->expr2)
   11072                 :             :     {
   11073                 :          42 :       gfc_association_list* assoc;
   11074                 :             : 
   11075                 :          42 :       assoc = gfc_get_association_list ();
   11076                 :          42 :       assoc->st = code->expr1->symtree;
   11077                 :          42 :       assoc->target = gfc_copy_expr (code->expr2);
   11078                 :          42 :       assoc->target->where = code->expr2->where;
   11079                 :             :       /* assoc->variable will be set by resolve_assoc_var.  */
   11080                 :             : 
   11081                 :          42 :       code->ext.block.assoc = assoc;
   11082                 :          42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11083                 :             : 
   11084                 :          42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11085                 :             :     }
   11086                 :             :   else
   11087                 :         976 :     code->ext.block.assoc = NULL;
   11088                 :             : 
   11089                 :             :   /* Loop over RANK cases. Note that returning on the errors causes a
   11090                 :             :      cascade of further errors because the case blocks do not compile
   11091                 :             :      correctly.  */
   11092                 :        3320 :   for (body = code->block; body; body = body->block)
   11093                 :             :     {
   11094                 :        2302 :       c = body->ext.block.case_list;
   11095                 :        2302 :       if (c->low)
   11096                 :        1383 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11097                 :             :       else
   11098                 :             :         case_value = -2;
   11099                 :             : 
   11100                 :             :       /* Check for repeated cases.  */
   11101                 :        5836 :       for (tail = code->block; tail; tail = tail->block)
   11102                 :             :         {
   11103                 :        5836 :           gfc_case *d = tail->ext.block.case_list;
   11104                 :        5836 :           int case_value2;
   11105                 :             : 
   11106                 :        5836 :           if (tail == body)
   11107                 :             :             break;
   11108                 :             : 
   11109                 :             :           /* Check F2018: C1153.  */
   11110                 :        3534 :           if (!c->low && !d->low)
   11111                 :           1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11112                 :             :                        &c->where, &d->where);
   11113                 :             : 
   11114                 :        3534 :           if (!c->low || !d->low)
   11115                 :        1253 :             continue;
   11116                 :             : 
   11117                 :             :           /* Check F2018: C1153.  */
   11118                 :        2281 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11119                 :        2281 :           if ((case_value == case_value2) && case_value == -1)
   11120                 :           1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11121                 :             :                        &c->where, &d->where);
   11122                 :        2280 :           else if (case_value == case_value2)
   11123                 :           1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11124                 :             :                        case_value, &c->where, &d->where);
   11125                 :             :         }
   11126                 :             : 
   11127                 :        2302 :       if (!c->low)
   11128                 :         919 :         continue;
   11129                 :             : 
   11130                 :             :       /* Check F2018: C1155.  */
   11131                 :        1383 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11132                 :        1381 :                                || gfc_expr_attr (code->expr1).pointer))
   11133                 :           3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11134                 :           3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11135                 :             :     }
   11136                 :             : 
   11137                 :             :   /* Add EXEC_SELECT to switch on rank.  */
   11138                 :        1018 :   new_st = gfc_get_code (code->op);
   11139                 :        1018 :   new_st->expr1 = code->expr1;
   11140                 :        1018 :   new_st->expr2 = code->expr2;
   11141                 :        1018 :   new_st->block = code->block;
   11142                 :        1018 :   code->expr1 = code->expr2 =  NULL;
   11143                 :        1018 :   code->block = NULL;
   11144                 :        1018 :   if (!ns->code)
   11145                 :        1018 :     ns->code = new_st;
   11146                 :             :   else
   11147                 :           0 :     ns->code->next = new_st;
   11148                 :        1018 :   code = new_st;
   11149                 :        1018 :   code->op = EXEC_SELECT_RANK;
   11150                 :             : 
   11151                 :        1018 :   selector_expr = code->expr1;
   11152                 :             : 
   11153                 :             :   /* Loop over SELECT RANK cases.  */
   11154                 :        3320 :   for (body = code->block; body; body = body->block)
   11155                 :             :     {
   11156                 :        2302 :       c = body->ext.block.case_list;
   11157                 :        2302 :       int case_value;
   11158                 :             : 
   11159                 :             :       /* Pass on the default case.  */
   11160                 :        2302 :       if (c->low == NULL)
   11161                 :         919 :         continue;
   11162                 :             : 
   11163                 :             :       /* Associate temporary to selector.  This should only be done
   11164                 :             :          when this case is actually true, so build a new ASSOCIATE
   11165                 :             :          that does precisely this here (instead of using the
   11166                 :             :          'global' one).  */
   11167                 :        1383 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11168                 :         265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11169                 :         186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11170                 :             : 
   11171                 :        1383 :       if (c->ts.type == BT_CLASS)
   11172                 :         145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11173                 :        1238 :       else if (c->ts.type == BT_DERIVED)
   11174                 :         110 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11175                 :        1128 :       else if (c->ts.type != BT_CHARACTER)
   11176                 :         569 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11177                 :             :       else
   11178                 :         559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11179                 :             :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11180                 :             : 
   11181                 :        1383 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11182                 :        1383 :       if (case_value >= 0)
   11183                 :        1350 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11184                 :             :       else
   11185                 :          33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11186                 :             : 
   11187                 :        1383 :       st = gfc_find_symtree (ns->sym_root, name);
   11188                 :        1383 :       gcc_assert (st->n.sym->assoc);
   11189                 :             : 
   11190                 :        1383 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11191                 :        1383 :       st->n.sym->assoc->target->where = selector_expr->where;
   11192                 :             : 
   11193                 :        1383 :       new_st = gfc_get_code (EXEC_BLOCK);
   11194                 :        1383 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11195                 :        1383 :       new_st->ext.block.ns->code = body->next;
   11196                 :        1383 :       body->next = new_st;
   11197                 :             : 
   11198                 :             :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11199                 :             :          there is a CASE label overlap and this is already used.  Just ignore,
   11200                 :             :          the error is diagnosed elsewhere.  */
   11201                 :        1383 :       if (st->n.sym->assoc->dangling)
   11202                 :             :         {
   11203                 :        1381 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11204                 :        1381 :           st->n.sym->assoc->dangling = 0;
   11205                 :             :         }
   11206                 :             : 
   11207                 :        1383 :       resolve_assoc_var (st->n.sym, false);
   11208                 :             :     }
   11209                 :             : 
   11210                 :        1018 :   gfc_current_ns = ns;
   11211                 :        1018 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11212                 :        1018 :   gfc_current_ns = old_ns;
   11213                 :        1018 : }
   11214                 :             : 
   11215                 :             : 
   11216                 :             : /* Resolve a transfer statement. This is making sure that:
   11217                 :             :    -- a derived type being transferred has only non-pointer components
   11218                 :             :    -- a derived type being transferred doesn't have private components, unless
   11219                 :             :       it's being transferred from the module where the type was defined
   11220                 :             :    -- we're not trying to transfer a whole assumed size array.  */
   11221                 :             : 
   11222                 :             : static void
   11223                 :       44829 : resolve_transfer (gfc_code *code)
   11224                 :             : {
   11225                 :       44829 :   gfc_symbol *sym, *derived;
   11226                 :       44829 :   gfc_ref *ref;
   11227                 :       44829 :   gfc_expr *exp;
   11228                 :       44829 :   bool write = false;
   11229                 :       44829 :   bool formatted = false;
   11230                 :       44829 :   gfc_dt *dt = code->ext.dt;
   11231                 :       44829 :   gfc_symbol *dtio_sub = NULL;
   11232                 :             : 
   11233                 :       44829 :   exp = code->expr1;
   11234                 :             : 
   11235                 :       89664 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11236                 :       45715 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11237                 :           6 :     exp = exp->value.op.op1;
   11238                 :             : 
   11239                 :       44829 :   if (exp && exp->expr_type == EXPR_NULL
   11240                 :           2 :       && code->ext.dt)
   11241                 :             :     {
   11242                 :           2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11243                 :             :                  &exp->where);
   11244                 :           2 :       return;
   11245                 :             :     }
   11246                 :             : 
   11247                 :             :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11248                 :             :                       && exp->expr_type != EXPR_FUNCTION
   11249                 :             :                       && exp->expr_type != EXPR_ARRAY
   11250                 :             :                       && exp->expr_type != EXPR_STRUCTURE))
   11251                 :             :     return;
   11252                 :             : 
   11253                 :             :   /* If we are reading, the variable will be changed.  Note that
   11254                 :             :      code->ext.dt may be NULL if the TRANSFER is related to
   11255                 :             :      an INQUIRE statement -- but in this case, we are not reading, either.  */
   11256                 :       24331 :   if (dt && dt->dt_io_kind->value.iokind == M_READ
   11257                 :       31697 :       && !gfc_check_vardef_context (exp, false, false, false,
   11258                 :        7218 :                                     _("item in READ")))
   11259                 :             :     return;
   11260                 :             : 
   11261                 :       24475 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11262                 :       24475 :                         || exp->expr_type == EXPR_FUNCTION
   11263                 :       20157 :                         || exp->expr_type == EXPR_ARRAY
   11264                 :       44632 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11265                 :             : 
   11266                 :             :   /* Go to actual component transferred.  */
   11267                 :       31674 :   for (ref = exp->ref; ref; ref = ref->next)
   11268                 :        7199 :     if (ref->type == REF_COMPONENT)
   11269                 :        2124 :       ts = &ref->u.c.component->ts;
   11270                 :             : 
   11271                 :       24475 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11272                 :       24327 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11273                 :             :     {
   11274                 :         686 :       derived = ts->u.derived;
   11275                 :             : 
   11276                 :             :       /* Determine when to use the formatted DTIO procedure.  */
   11277                 :         686 :       if (dt && (dt->format_expr || dt->format_label))
   11278                 :         611 :         formatted = true;
   11279                 :             : 
   11280                 :         686 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11281                 :         686 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11282                 :         686 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11283                 :             : 
   11284                 :         686 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11285                 :             :         {
   11286                 :         425 :           dt->udtio = exp;
   11287                 :         425 :           sym = exp->symtree->n.sym->ns->proc_name;
   11288                 :             :           /* Check to see if this is a nested DTIO call, with the
   11289                 :             :              dummy as the io-list object.  */
   11290                 :         425 :           if (sym && sym == dtio_sub && sym->formal
   11291                 :          30 :               && sym->formal->sym == exp->symtree->n.sym
   11292                 :          30 :               && exp->ref == NULL)
   11293                 :             :             {
   11294                 :           0 :               if (!sym->attr.recursive)
   11295                 :             :                 {
   11296                 :           0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11297                 :             :                              sym->name, &sym->declared_at);
   11298                 :           0 :                   return;
   11299                 :             :                 }
   11300                 :             :             }
   11301                 :             :         }
   11302                 :             :     }
   11303                 :             : 
   11304                 :       24475 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11305                 :             :     {
   11306                 :           3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11307                 :             :                 "it is processed by a defined input/output procedure",
   11308                 :             :                 &code->loc);
   11309                 :           3 :       return;
   11310                 :             :     }
   11311                 :             : 
   11312                 :       24472 :   if (ts->type == BT_DERIVED)
   11313                 :             :     {
   11314                 :             :       /* Check that transferred derived type doesn't contain POINTER
   11315                 :             :          components unless it is processed by a defined input/output
   11316                 :             :          procedure".  */
   11317                 :         654 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11318                 :             :         {
   11319                 :           2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11320                 :             :                      "components unless it is processed by a defined "
   11321                 :             :                      "input/output procedure", &code->loc);
   11322                 :           2 :           return;
   11323                 :             :         }
   11324                 :             : 
   11325                 :             :       /* F08:C935.  */
   11326                 :         652 :       if (ts->u.derived->attr.proc_pointer_comp)
   11327                 :             :         {
   11328                 :           2 :           gfc_error ("Data transfer element at %L cannot have "
   11329                 :             :                      "procedure pointer components", &code->loc);
   11330                 :           2 :           return;
   11331                 :             :         }
   11332                 :             : 
   11333                 :         650 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11334                 :             :         {
   11335                 :           6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11336                 :             :                      "components unless it is processed by a defined "
   11337                 :             :                      "input/output procedure", &code->loc);
   11338                 :           6 :           return;
   11339                 :             :         }
   11340                 :             : 
   11341                 :             :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11342                 :             :          be printed.  However, if -std=gnu and not -pedantic, allow
   11343                 :             :          the component to be printed to help debugging.  */
   11344                 :         644 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11345                 :             :         {
   11346                 :           4 :           gfc_error ("Data transfer element at %L "
   11347                 :             :                      "cannot have PRIVATE components", &code->loc);
   11348                 :           4 :             return;
   11349                 :             :         }
   11350                 :         640 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11351                 :             :         {
   11352                 :           4 :           gfc_error ("Data transfer element at %L cannot have "
   11353                 :             :                      "PRIVATE components unless it is processed by "
   11354                 :             :                      "a defined input/output procedure", &code->loc);
   11355                 :           4 :           return;
   11356                 :             :         }
   11357                 :             :     }
   11358                 :             : 
   11359                 :       24454 :   if (exp->expr_type == EXPR_STRUCTURE)
   11360                 :             :     return;
   11361                 :             : 
   11362                 :       24409 :   if (exp->expr_type == EXPR_ARRAY)
   11363                 :             :     return;
   11364                 :             : 
   11365                 :       24033 :   sym = exp->symtree->n.sym;
   11366                 :             : 
   11367                 :       24033 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11368                 :          75 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11369                 :             :     {
   11370                 :           1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11371                 :             :                  "an assumed-size array", &code->loc);
   11372                 :           1 :       return;
   11373                 :             :     }
   11374                 :             : }
   11375                 :             : 
   11376                 :             : 
   11377                 :             : /*********** Toplevel code resolution subroutines ***********/
   11378                 :             : 
   11379                 :             : /* Find the set of labels that are reachable from this block.  We also
   11380                 :             :    record the last statement in each block.  */
   11381                 :             : 
   11382                 :             : static void
   11383                 :      646028 : find_reachable_labels (gfc_code *block)
   11384                 :             : {
   11385                 :      646028 :   gfc_code *c;
   11386                 :             : 
   11387                 :      646028 :   if (!block)
   11388                 :             :     return;
   11389                 :             : 
   11390                 :      407056 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11391                 :             : 
   11392                 :             :   /* Collect labels in this block.  We don't keep those corresponding
   11393                 :             :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11394                 :             :      up through the code_stack.  */
   11395                 :     1495289 :   for (c = block; c; c = c->next)
   11396                 :             :     {
   11397                 :     1088233 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11398                 :        3588 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11399                 :             :     }
   11400                 :             : 
   11401                 :             :   /* Merge with labels from parent block.  */
   11402                 :      407056 :   if (cs_base->prev)
   11403                 :             :     {
   11404                 :      334038 :       gcc_assert (cs_base->prev->reachable_labels);
   11405                 :      334038 :       bitmap_ior_into (cs_base->reachable_labels,
   11406                 :             :                        cs_base->prev->reachable_labels);
   11407                 :             :     }
   11408                 :             : }
   11409                 :             : 
   11410                 :             : static void
   11411                 :         136 : resolve_lock_unlock_event (gfc_code *code)
   11412                 :             : {
   11413                 :         136 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11414                 :         136 :       && (code->expr1->ts.type != BT_DERIVED
   11415                 :          95 :           || code->expr1->expr_type != EXPR_VARIABLE
   11416                 :          95 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11417                 :          95 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11418                 :          94 :           || code->expr1->rank != 0
   11419                 :         124 :           || (!gfc_is_coarray (code->expr1) &&
   11420                 :          31 :               !gfc_is_coindexed (code->expr1))))
   11421                 :           4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11422                 :           4 :                &code->expr1->where);
   11423                 :         132 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11424                 :          39 :            && (code->expr1->ts.type != BT_DERIVED
   11425                 :          39 :                || code->expr1->expr_type != EXPR_VARIABLE
   11426                 :          39 :                || code->expr1->ts.u.derived->from_intmod
   11427                 :             :                   != INTMOD_ISO_FORTRAN_ENV
   11428                 :          39 :                || code->expr1->ts.u.derived->intmod_sym_id
   11429                 :             :                   != ISOFORTRAN_EVENT_TYPE
   11430                 :          39 :                || code->expr1->rank != 0))
   11431                 :           0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11432                 :             :                &code->expr1->where);
   11433                 :          23 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11434                 :         143 :            && !gfc_is_coindexed (code->expr1))
   11435                 :           0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11436                 :           0 :                &code->expr1->where);
   11437                 :         132 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11438                 :           0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11439                 :           0 :                "coindexed", &code->expr1->where);
   11440                 :             : 
   11441                 :             :   /* Check STAT.  */
   11442                 :         136 :   if (code->expr2
   11443                 :          38 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11444                 :          38 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11445                 :           0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11446                 :             :                &code->expr2->where);
   11447                 :             : 
   11448                 :         136 :   if (code->expr2
   11449                 :         174 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11450                 :          38 :                                     _("STAT variable")))
   11451                 :             :     return;
   11452                 :             : 
   11453                 :             :   /* Check ERRMSG.  */
   11454                 :         136 :   if (code->expr3
   11455                 :           2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   11456                 :           2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   11457                 :           0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11458                 :             :                &code->expr3->where);
   11459                 :             : 
   11460                 :         136 :   if (code->expr3
   11461                 :         138 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   11462                 :           2 :                                     _("ERRMSG variable")))
   11463                 :             :     return;
   11464                 :             : 
   11465                 :             :   /* Check for LOCK the ACQUIRED_LOCK.  */
   11466                 :         136 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11467                 :          16 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   11468                 :          16 :           || code->expr4->expr_type != EXPR_VARIABLE))
   11469                 :           0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   11470                 :             :                "variable", &code->expr4->where);
   11471                 :             : 
   11472                 :         120 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11473                 :         152 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   11474                 :          16 :                                     _("ACQUIRED_LOCK variable")))
   11475                 :             :     return;
   11476                 :             : 
   11477                 :             :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   11478                 :         136 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   11479                 :             :     {
   11480                 :          24 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   11481                 :          24 :           || code->expr4->rank != 0)
   11482                 :           0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   11483                 :           0 :                    "expression", &code->expr4->where);
   11484                 :             :     }
   11485                 :             : }
   11486                 :             : 
   11487                 :             : 
   11488                 :             : static void
   11489                 :          35 : resolve_critical (gfc_code *code)
   11490                 :             : {
   11491                 :          35 :   gfc_symtree *symtree;
   11492                 :          35 :   gfc_symbol *lock_type;
   11493                 :          35 :   char name[GFC_MAX_SYMBOL_LEN];
   11494                 :          35 :   static int serial = 0;
   11495                 :             : 
   11496                 :          35 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   11497                 :          30 :     return;
   11498                 :             : 
   11499                 :           5 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   11500                 :             :                               GFC_PREFIX ("lock_type"));
   11501                 :           5 :   if (symtree)
   11502                 :           2 :     lock_type = symtree->n.sym;
   11503                 :             :   else
   11504                 :             :     {
   11505                 :           3 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   11506                 :             :                             false) != 0)
   11507                 :           0 :         gcc_unreachable ();
   11508                 :           3 :       lock_type = symtree->n.sym;
   11509                 :           3 :       lock_type->attr.flavor = FL_DERIVED;
   11510                 :           3 :       lock_type->attr.zero_comp = 1;
   11511                 :           3 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   11512                 :           3 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   11513                 :             :     }
   11514                 :             : 
   11515                 :           5 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   11516                 :           5 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   11517                 :           0 :     gcc_unreachable ();
   11518                 :             : 
   11519                 :           5 :   code->resolved_sym = symtree->n.sym;
   11520                 :           5 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   11521                 :           5 :   symtree->n.sym->attr.referenced = 1;
   11522                 :           5 :   symtree->n.sym->attr.artificial = 1;
   11523                 :           5 :   symtree->n.sym->attr.codimension = 1;
   11524                 :           5 :   symtree->n.sym->ts.type = BT_DERIVED;
   11525                 :           5 :   symtree->n.sym->ts.u.derived = lock_type;
   11526                 :           5 :   symtree->n.sym->as = gfc_get_array_spec ();
   11527                 :           5 :   symtree->n.sym->as->corank = 1;
   11528                 :           5 :   symtree->n.sym->as->type = AS_EXPLICIT;
   11529                 :           5 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   11530                 :           5 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   11531                 :             :                                                    NULL, 1);
   11532                 :           5 :   gfc_commit_symbols();
   11533                 :             : }
   11534                 :             : 
   11535                 :             : 
   11536                 :             : static void
   11537                 :         747 : resolve_sync (gfc_code *code)
   11538                 :             : {
   11539                 :             :   /* Check imageset. The * case matches expr1 == NULL.  */
   11540                 :         747 :   if (code->expr1)
   11541                 :             :     {
   11542                 :          48 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   11543                 :           1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   11544                 :             :                    "INTEGER expression", &code->expr1->where);
   11545                 :          48 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   11546                 :          23 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   11547                 :           1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   11548                 :             :                    &code->expr1->where);
   11549                 :          47 :       else if (code->expr1->expr_type == EXPR_ARRAY
   11550                 :          47 :                && gfc_simplify_expr (code->expr1, 0))
   11551                 :             :         {
   11552                 :          18 :            gfc_constructor *cons;
   11553                 :          18 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   11554                 :          54 :            for (; cons; cons = gfc_constructor_next (cons))
   11555                 :          18 :              if (cons->expr->expr_type == EXPR_CONSTANT
   11556                 :          18 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   11557                 :           0 :                gfc_error ("Imageset argument at %L must between 1 and "
   11558                 :             :                           "num_images()", &cons->expr->where);
   11559                 :             :         }
   11560                 :             :     }
   11561                 :             : 
   11562                 :             :   /* Check STAT.  */
   11563                 :         747 :   gfc_resolve_expr (code->expr2);
   11564                 :         747 :   if (code->expr2)
   11565                 :             :     {
   11566                 :          84 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   11567                 :           1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11568                 :             :                    &code->expr2->where);
   11569                 :             :       else
   11570                 :          83 :         gfc_check_vardef_context (code->expr2, false, false, false,
   11571                 :          83 :                                   _("STAT variable"));
   11572                 :             :     }
   11573                 :             : 
   11574                 :             :   /* Check ERRMSG.  */
   11575                 :         747 :   gfc_resolve_expr (code->expr3);
   11576                 :         747 :   if (code->expr3)
   11577                 :             :     {
   11578                 :          75 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   11579                 :           4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11580                 :             :                    &code->expr3->where);
   11581                 :             :       else
   11582                 :          71 :         gfc_check_vardef_context (code->expr3, false, false, false,
   11583                 :          71 :                                   _("ERRMSG variable"));
   11584                 :             :     }
   11585                 :         747 : }
   11586                 :             : 
   11587                 :             : 
   11588                 :             : /* Given a branch to a label, see if the branch is conforming.
   11589                 :             :    The code node describes where the branch is located.  */
   11590                 :             : 
   11591                 :             : static void
   11592                 :      105001 : resolve_branch (gfc_st_label *label, gfc_code *code)
   11593                 :             : {
   11594                 :      105001 :   code_stack *stack;
   11595                 :             : 
   11596                 :      105001 :   if (label == NULL)
   11597                 :             :     return;
   11598                 :             : 
   11599                 :             :   /* Step one: is this a valid branching target?  */
   11600                 :             : 
   11601                 :        2458 :   if (label->defined == ST_LABEL_UNKNOWN)
   11602                 :             :     {
   11603                 :           4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   11604                 :             :                  &code->loc);
   11605                 :           4 :       return;
   11606                 :             :     }
   11607                 :             : 
   11608                 :        2454 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   11609                 :             :     {
   11610                 :           4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   11611                 :             :                  "for the branch statement at %L", &label->where, &code->loc);
   11612                 :           4 :       return;
   11613                 :             :     }
   11614                 :             : 
   11615                 :             :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   11616                 :             : 
   11617                 :        2450 :   if (code->here == label)
   11618                 :             :     {
   11619                 :           0 :       gfc_warning (0,
   11620                 :             :                    "Branch at %L may result in an infinite loop", &code->loc);
   11621                 :           0 :       return;
   11622                 :             :     }
   11623                 :             : 
   11624                 :             :   /* Step three:  See if the label is in the same block as the
   11625                 :             :      branching statement.  The hard work has been done by setting up
   11626                 :             :      the bitmap reachable_labels.  */
   11627                 :             : 
   11628                 :        2450 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   11629                 :             :     {
   11630                 :             :       /* Check now whether there is a CRITICAL construct; if so, check
   11631                 :             :          whether the label is still visible outside of the CRITICAL block,
   11632                 :             :          which is invalid.  */
   11633                 :        6261 :       for (stack = cs_base; stack; stack = stack->prev)
   11634                 :             :         {
   11635                 :        3879 :           if (stack->current->op == EXEC_CRITICAL
   11636                 :        3879 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   11637                 :           2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   11638                 :             :                       "label at %L", &code->loc, &label->where);
   11639                 :        3877 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   11640                 :        3877 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   11641                 :           0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   11642                 :             :                       "for label at %L", &code->loc, &label->where);
   11643                 :             :         }
   11644                 :             : 
   11645                 :             :       return;
   11646                 :             :     }
   11647                 :             : 
   11648                 :             :   /* Step four:  If we haven't found the label in the bitmap, it may
   11649                 :             :     still be the label of the END of the enclosing block, in which
   11650                 :             :     case we find it by going up the code_stack.  */
   11651                 :             : 
   11652                 :         167 :   for (stack = cs_base; stack; stack = stack->prev)
   11653                 :             :     {
   11654                 :         131 :       if (stack->current->next && stack->current->next->here == label)
   11655                 :             :         break;
   11656                 :         101 :       if (stack->current->op == EXEC_CRITICAL)
   11657                 :             :         {
   11658                 :             :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   11659                 :             :              construct as END CRITICAL is still part of it.  */
   11660                 :           2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   11661                 :             :                       " at %L", &code->loc, &label->where);
   11662                 :           2 :           return;
   11663                 :             :         }
   11664                 :          99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   11665                 :             :         {
   11666                 :           0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   11667                 :             :                      "label at %L", &code->loc, &label->where);
   11668                 :           0 :           return;
   11669                 :             :         }
   11670                 :             :     }
   11671                 :             : 
   11672                 :          66 :   if (stack)
   11673                 :             :     {
   11674                 :          30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   11675                 :             :       return;
   11676                 :             :     }
   11677                 :             : 
   11678                 :             :   /* The label is not in an enclosing block, so illegal.  This was
   11679                 :             :      allowed in Fortran 66, so we allow it as extension.  No
   11680                 :             :      further checks are necessary in this case.  */
   11681                 :          36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   11682                 :             :                   "as the GOTO statement at %L", &label->where,
   11683                 :             :                   &code->loc);
   11684                 :          36 :   return;
   11685                 :             : }
   11686                 :             : 
   11687                 :             : 
   11688                 :             : /* Check whether EXPR1 has the same shape as EXPR2.  */
   11689                 :             : 
   11690                 :             : static bool
   11691                 :        1461 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   11692                 :             : {
   11693                 :        1461 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   11694                 :        1461 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   11695                 :        1461 :   bool result = false;
   11696                 :        1461 :   int i;
   11697                 :             : 
   11698                 :             :   /* Compare the rank.  */
   11699                 :        1461 :   if (expr1->rank != expr2->rank)
   11700                 :             :     return result;
   11701                 :             : 
   11702                 :             :   /* Compare the size of each dimension.  */
   11703                 :        2795 :   for (i=0; i<expr1->rank; i++)
   11704                 :             :     {
   11705                 :        1484 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   11706                 :         150 :         goto ignore;
   11707                 :             : 
   11708                 :        1334 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   11709                 :           0 :         goto ignore;
   11710                 :             : 
   11711                 :        1334 :       if (mpz_cmp (shape[i], shape2[i]))
   11712                 :           0 :         goto over;
   11713                 :             :     }
   11714                 :             : 
   11715                 :             :   /* When either of the two expression is an assumed size array, we
   11716                 :             :      ignore the comparison of dimension sizes.  */
   11717                 :        1311 : ignore:
   11718                 :             :   result = true;
   11719                 :             : 
   11720                 :        1461 : over:
   11721                 :        1461 :   gfc_clear_shape (shape, i);
   11722                 :        1461 :   gfc_clear_shape (shape2, i);
   11723                 :        1461 :   return result;
   11724                 :             : }
   11725                 :             : 
   11726                 :             : 
   11727                 :             : /* Check whether a WHERE assignment target or a WHERE mask expression
   11728                 :             :    has the same shape as the outmost WHERE mask expression.  */
   11729                 :             : 
   11730                 :             : static void
   11731                 :         506 : resolve_where (gfc_code *code, gfc_expr *mask)
   11732                 :             : {
   11733                 :         506 :   gfc_code *cblock;
   11734                 :         506 :   gfc_code *cnext;
   11735                 :         506 :   gfc_expr *e = NULL;
   11736                 :             : 
   11737                 :         506 :   cblock = code->block;
   11738                 :             : 
   11739                 :             :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   11740                 :             :      In case of nested WHERE, only the outmost one is stored.  */
   11741                 :         506 :   if (mask == NULL) /* outmost WHERE */
   11742                 :         450 :     e = cblock->expr1;
   11743                 :             :   else /* inner WHERE */
   11744                 :         506 :     e = mask;
   11745                 :             : 
   11746                 :        1381 :   while (cblock)
   11747                 :             :     {
   11748                 :         875 :       if (cblock->expr1)
   11749                 :             :         {
   11750                 :             :           /* Check if the mask-expr has a consistent shape with the
   11751                 :             :              outmost WHERE mask-expr.  */
   11752                 :         711 :           if (!resolve_where_shape (cblock->expr1, e))
   11753                 :           0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   11754                 :           0 :                        &cblock->expr1->where);
   11755                 :             :          }
   11756                 :             : 
   11757                 :             :       /* the assignment statement of a WHERE statement, or the first
   11758                 :             :          statement in where-body-construct of a WHERE construct */
   11759                 :         875 :       cnext = cblock->next;
   11760                 :        1727 :       while (cnext)
   11761                 :             :         {
   11762                 :         852 :           switch (cnext->op)
   11763                 :             :             {
   11764                 :             :             /* WHERE assignment statement */
   11765                 :         750 :             case EXEC_ASSIGN:
   11766                 :             : 
   11767                 :             :               /* Check shape consistent for WHERE assignment target.  */
   11768                 :         750 :               if (e && !resolve_where_shape (cnext->expr1, e))
   11769                 :           0 :                gfc_error ("WHERE assignment target at %L has "
   11770                 :           0 :                           "inconsistent shape", &cnext->expr1->where);
   11771                 :             : 
   11772                 :         750 :               if (cnext->op == EXEC_ASSIGN
   11773                 :         750 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   11774                 :           0 :                 cnext->expr1->must_finalize = 1;
   11775                 :             : 
   11776                 :             :               break;
   11777                 :             : 
   11778                 :             : 
   11779                 :          46 :             case EXEC_ASSIGN_CALL:
   11780                 :          46 :               resolve_call (cnext);
   11781                 :          46 :               if (!cnext->resolved_sym->attr.elemental)
   11782                 :           2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   11783                 :           2 :                           &cnext->ext.actual->expr->where);
   11784                 :             :               break;
   11785                 :             : 
   11786                 :             :             /* WHERE or WHERE construct is part of a where-body-construct */
   11787                 :          56 :             case EXEC_WHERE:
   11788                 :          56 :               resolve_where (cnext, e);
   11789                 :          56 :               break;
   11790                 :             : 
   11791                 :           0 :             default:
   11792                 :           0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   11793                 :             :                          &cnext->loc);
   11794                 :             :             }
   11795                 :             :          /* the next statement within the same where-body-construct */
   11796                 :         852 :          cnext = cnext->next;
   11797                 :             :        }
   11798                 :             :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   11799                 :         875 :     cblock = cblock->block;
   11800                 :             :   }
   11801                 :         506 : }
   11802                 :             : 
   11803                 :             : 
   11804                 :             : /* Resolve assignment in FORALL construct.
   11805                 :             :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   11806                 :             :    FORALL index variables.  */
   11807                 :             : 
   11808                 :             : static void
   11809                 :        1944 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   11810                 :             : {
   11811                 :        1944 :   int n;
   11812                 :             : 
   11813                 :        5871 :   for (n = 0; n < nvar; n++)
   11814                 :             :     {
   11815                 :        3927 :       gfc_symbol *forall_index;
   11816                 :             : 
   11817                 :        3927 :       forall_index = var_expr[n]->symtree->n.sym;
   11818                 :             : 
   11819                 :             :       /* Check whether the assignment target is one of the FORALL index
   11820                 :             :          variable.  */
   11821                 :        3927 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   11822                 :        3927 :           && (code->expr1->symtree->n.sym == forall_index))
   11823                 :           0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   11824                 :             :                    &code->expr1->where);
   11825                 :             :       else
   11826                 :             :         {
   11827                 :             :           /* If one of the FORALL index variables doesn't appear in the
   11828                 :             :              assignment variable, then there could be a many-to-one
   11829                 :             :              assignment.  Emit a warning rather than an error because the
   11830                 :             :              mask could be resolving this problem.  */
   11831                 :        3927 :           if (!find_forall_index (code->expr1, forall_index, 0))
   11832                 :           0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   11833                 :             :                          "left side of the assignment at %L and so might "
   11834                 :             :                          "cause multiple assignment to this object",
   11835                 :           0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   11836                 :             :         }
   11837                 :             :     }
   11838                 :        1944 : }
   11839                 :             : 
   11840                 :             : 
   11841                 :             : /* Resolve WHERE statement in FORALL construct.  */
   11842                 :             : 
   11843                 :             : static void
   11844                 :          46 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   11845                 :             :                                   gfc_expr **var_expr)
   11846                 :             : {
   11847                 :          46 :   gfc_code *cblock;
   11848                 :          46 :   gfc_code *cnext;
   11849                 :             : 
   11850                 :          46 :   cblock = code->block;
   11851                 :         111 :   while (cblock)
   11852                 :             :     {
   11853                 :             :       /* the assignment statement of a WHERE statement, or the first
   11854                 :             :          statement in where-body-construct of a WHERE construct */
   11855                 :          65 :       cnext = cblock->next;
   11856                 :         130 :       while (cnext)
   11857                 :             :         {
   11858                 :          65 :           switch (cnext->op)
   11859                 :             :             {
   11860                 :             :             /* WHERE assignment statement */
   11861                 :          65 :             case EXEC_ASSIGN:
   11862                 :          65 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   11863                 :             : 
   11864                 :          65 :               if (cnext->op == EXEC_ASSIGN
   11865                 :          65 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   11866                 :           0 :                 cnext->expr1->must_finalize = 1;
   11867                 :             : 
   11868                 :             :               break;
   11869                 :             : 
   11870                 :             :             /* WHERE operator assignment statement */
   11871                 :           0 :             case EXEC_ASSIGN_CALL:
   11872                 :           0 :               resolve_call (cnext);
   11873                 :           0 :               if (!cnext->resolved_sym->attr.elemental)
   11874                 :           0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   11875                 :           0 :                           &cnext->ext.actual->expr->where);
   11876                 :             :               break;
   11877                 :             : 
   11878                 :             :             /* WHERE or WHERE construct is part of a where-body-construct */
   11879                 :           0 :             case EXEC_WHERE:
   11880                 :           0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   11881                 :           0 :               break;
   11882                 :             : 
   11883                 :           0 :             default:
   11884                 :           0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   11885                 :             :                          &cnext->loc);
   11886                 :             :             }
   11887                 :             :           /* the next statement within the same where-body-construct */
   11888                 :          65 :           cnext = cnext->next;
   11889                 :             :         }
   11890                 :             :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   11891                 :          65 :       cblock = cblock->block;
   11892                 :             :     }
   11893                 :          46 : }
   11894                 :             : 
   11895                 :             : 
   11896                 :             : /* Traverse the FORALL body to check whether the following errors exist:
   11897                 :             :    1. For assignment, check if a many-to-one assignment happens.
   11898                 :             :    2. For WHERE statement, check the WHERE body to see if there is any
   11899                 :             :       many-to-one assignment.  */
   11900                 :             : 
   11901                 :             : static void
   11902                 :        1990 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   11903                 :             : {
   11904                 :        1990 :   gfc_code *c;
   11905                 :             : 
   11906                 :        1990 :   c = code->block->next;
   11907                 :        3993 :   while (c)
   11908                 :             :     {
   11909                 :        2003 :       switch (c->op)
   11910                 :             :         {
   11911                 :        1879 :         case EXEC_ASSIGN:
   11912                 :        1879 :         case EXEC_POINTER_ASSIGN:
   11913                 :        1879 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   11914                 :             : 
   11915                 :        1879 :           if (c->op == EXEC_ASSIGN
   11916                 :        1879 :               && gfc_may_be_finalized (c->expr1->ts))
   11917                 :           0 :             c->expr1->must_finalize = 1;
   11918                 :             : 
   11919                 :             :           break;
   11920                 :             : 
   11921                 :           0 :         case EXEC_ASSIGN_CALL:
   11922                 :           0 :           resolve_call (c);
   11923                 :           0 :           break;
   11924                 :             : 
   11925                 :             :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   11926                 :             :            there is no need to handle it here.  */
   11927                 :             :         case EXEC_FORALL:
   11928                 :             :           break;
   11929                 :          46 :         case EXEC_WHERE:
   11930                 :          46 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   11931                 :          46 :           break;
   11932                 :             :         default:
   11933                 :             :           break;
   11934                 :             :         }
   11935                 :             :       /* The next statement in the FORALL body.  */
   11936                 :        2003 :       c = c->next;
   11937                 :             :     }
   11938                 :        1990 : }
   11939                 :             : 
   11940                 :             : 
   11941                 :             : /* Counts the number of iterators needed inside a forall construct, including
   11942                 :             :    nested forall constructs. This is used to allocate the needed memory
   11943                 :             :    in gfc_resolve_forall.  */
   11944                 :             : 
   11945                 :             : static int
   11946                 :        1990 : gfc_count_forall_iterators (gfc_code *code)
   11947                 :             : {
   11948                 :        1990 :   int max_iters, sub_iters, current_iters;
   11949                 :        1990 :   gfc_forall_iterator *fa;
   11950                 :             : 
   11951                 :        1990 :   gcc_assert(code->op == EXEC_FORALL);
   11952                 :        1990 :   max_iters = 0;
   11953                 :        1990 :   current_iters = 0;
   11954                 :             : 
   11955                 :        5878 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   11956                 :        3888 :     current_iters ++;
   11957                 :             : 
   11958                 :        1990 :   code = code->block->next;
   11959                 :             : 
   11960                 :        3993 :   while (code)
   11961                 :             :     {
   11962                 :        2003 :       if (code->op == EXEC_FORALL)
   11963                 :             :         {
   11964                 :          78 :           sub_iters = gfc_count_forall_iterators (code);
   11965                 :          78 :           if (sub_iters > max_iters)
   11966                 :        2003 :             max_iters = sub_iters;
   11967                 :             :         }
   11968                 :        2003 :       code = code->next;
   11969                 :             :     }
   11970                 :             : 
   11971                 :        1990 :   return current_iters + max_iters;
   11972                 :             : }
   11973                 :             : 
   11974                 :             : 
   11975                 :             : /* Given a FORALL construct, first resolve the FORALL iterator, then call
   11976                 :             :    gfc_resolve_forall_body to resolve the FORALL body.  */
   11977                 :             : 
   11978                 :             : static void
   11979                 :        1990 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   11980                 :             : {
   11981                 :        1990 :   static gfc_expr **var_expr;
   11982                 :        1990 :   static int total_var = 0;
   11983                 :        1990 :   static int nvar = 0;
   11984                 :        1990 :   int i, old_nvar, tmp;
   11985                 :        1990 :   gfc_forall_iterator *fa;
   11986                 :             : 
   11987                 :        1990 :   old_nvar = nvar;
   11988                 :             : 
   11989                 :        1990 :   if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   11990                 :             :     return;
   11991                 :             : 
   11992                 :             :   /* Start to resolve a FORALL construct   */
   11993                 :        1990 :   if (forall_save == 0)
   11994                 :             :     {
   11995                 :             :       /* Count the total number of FORALL indices in the nested FORALL
   11996                 :             :          construct in order to allocate the VAR_EXPR with proper size.  */
   11997                 :        1912 :       total_var = gfc_count_forall_iterators (code);
   11998                 :             : 
   11999                 :             :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12000                 :        1912 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12001                 :             :     }
   12002                 :             : 
   12003                 :             :   /* The information about FORALL iterator, including FORALL indices start, end
   12004                 :             :      and stride.  An outer FORALL indice cannot appear in start, end or stride.  */
   12005                 :        5878 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12006                 :             :     {
   12007                 :             :       /* Fortran 20008: C738 (R753).  */
   12008                 :        3888 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12009                 :             :         {
   12010                 :           2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12011                 :             :                      "of type integer", &fa->var->where);
   12012                 :           2 :           continue;
   12013                 :             :         }
   12014                 :             : 
   12015                 :             :       /* Check if any outer FORALL index name is the same as the current
   12016                 :             :          one.  */
   12017                 :        6859 :       for (i = 0; i < nvar; i++)
   12018                 :             :         {
   12019                 :        2973 :           if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12020                 :           0 :             gfc_error ("An outer FORALL construct already has an index "
   12021                 :             :                         "with this name %L", &fa->var->where);
   12022                 :             :         }
   12023                 :             : 
   12024                 :             :       /* Record the current FORALL index.  */
   12025                 :        3886 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12026                 :             : 
   12027                 :        3886 :       nvar++;
   12028                 :             : 
   12029                 :             :       /* No memory leak.  */
   12030                 :        3886 :       gcc_assert (nvar <= total_var);
   12031                 :             :     }
   12032                 :             : 
   12033                 :             :   /* Resolve the FORALL body.  */
   12034                 :        1990 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12035                 :             : 
   12036                 :             :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12037                 :        1990 :   gfc_resolve_blocks (code->block, ns);
   12038                 :             : 
   12039                 :        1990 :   tmp = nvar;
   12040                 :        1990 :   nvar = old_nvar;
   12041                 :             :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12042                 :        5876 :   for (i = nvar; i < tmp; i++)
   12043                 :        3886 :      gfc_free_expr (var_expr[i]);
   12044                 :             : 
   12045                 :        1990 :   if (nvar == 0)
   12046                 :             :     {
   12047                 :             :       /* We are in the outermost FORALL construct.  */
   12048                 :        1912 :       gcc_assert (forall_save == 0);
   12049                 :             : 
   12050                 :             :       /* VAR_EXPR is not needed any more.  */
   12051                 :        1912 :       free (var_expr);
   12052                 :        1912 :       total_var = 0;
   12053                 :             :     }
   12054                 :             : }
   12055                 :             : 
   12056                 :             : 
   12057                 :             : /* Resolve a BLOCK construct statement.  */
   12058                 :             : 
   12059                 :             : static void
   12060                 :        7497 : resolve_block_construct (gfc_code* code)
   12061                 :             : {
   12062                 :        7497 :   gfc_namespace *ns = code->ext.block.ns;
   12063                 :             : 
   12064                 :             :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12065                 :             :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12066                 :             :      namespace.  */
   12067                 :        7497 :   gfc_resolve (ns);
   12068                 :           0 : }
   12069                 :             : 
   12070                 :             : 
   12071                 :             : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12072                 :             :    DO code nodes.  */
   12073                 :             : 
   12074                 :             : void
   12075                 :      317484 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12076                 :             : {
   12077                 :      317484 :   bool t;
   12078                 :             : 
   12079                 :      646146 :   for (; b; b = b->block)
   12080                 :             :     {
   12081                 :      328662 :       t = gfc_resolve_expr (b->expr1);
   12082                 :      328662 :       if (!gfc_resolve_expr (b->expr2))
   12083                 :           0 :         t = false;
   12084                 :             : 
   12085                 :      328662 :       switch (b->op)
   12086                 :             :         {
   12087                 :      225455 :         case EXEC_IF:
   12088                 :      225455 :           if (t && b->expr1 != NULL
   12089                 :      221339 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   12090                 :           0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   12091                 :             :                        &b->expr1->where);
   12092                 :             :           break;
   12093                 :             : 
   12094                 :         761 :         case EXEC_WHERE:
   12095                 :         761 :           if (t
   12096                 :         761 :               && b->expr1 != NULL
   12097                 :         628 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   12098                 :           0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   12099                 :             :                        &b->expr1->where);
   12100                 :             :           break;
   12101                 :             : 
   12102                 :          76 :         case EXEC_GOTO:
   12103                 :          76 :           resolve_branch (b->label1, b);
   12104                 :          76 :           break;
   12105                 :             : 
   12106                 :           0 :         case EXEC_BLOCK:
   12107                 :           0 :           resolve_block_construct (b);
   12108                 :           0 :           break;
   12109                 :             : 
   12110                 :             :         case EXEC_SELECT:
   12111                 :             :         case EXEC_SELECT_TYPE:
   12112                 :             :         case EXEC_SELECT_RANK:
   12113                 :             :         case EXEC_FORALL:
   12114                 :             :         case EXEC_DO:
   12115                 :             :         case EXEC_DO_WHILE:
   12116                 :             :         case EXEC_DO_CONCURRENT:
   12117                 :             :         case EXEC_CRITICAL:
   12118                 :             :         case EXEC_READ:
   12119                 :             :         case EXEC_WRITE:
   12120                 :             :         case EXEC_IOLENGTH:
   12121                 :             :         case EXEC_WAIT:
   12122                 :             :           break;
   12123                 :             : 
   12124                 :        2697 :         case EXEC_OMP_ATOMIC:
   12125                 :        2697 :         case EXEC_OACC_ATOMIC:
   12126                 :        2697 :           {
   12127                 :             :             /* Verify this before calling gfc_resolve_code, which might
   12128                 :             :                change it.  */
   12129                 :        2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   12130                 :             :                         || (b->next && b->next->op == EXEC_ASSIGN));
   12131                 :             :           }
   12132                 :             :           break;
   12133                 :             : 
   12134                 :             :         case EXEC_OACC_PARALLEL_LOOP:
   12135                 :             :         case EXEC_OACC_PARALLEL:
   12136                 :             :         case EXEC_OACC_KERNELS_LOOP:
   12137                 :             :         case EXEC_OACC_KERNELS:
   12138                 :             :         case EXEC_OACC_SERIAL_LOOP:
   12139                 :             :         case EXEC_OACC_SERIAL:
   12140                 :             :         case EXEC_OACC_DATA:
   12141                 :             :         case EXEC_OACC_HOST_DATA:
   12142                 :             :         case EXEC_OACC_LOOP:
   12143                 :             :         case EXEC_OACC_UPDATE:
   12144                 :             :         case EXEC_OACC_WAIT:
   12145                 :             :         case EXEC_OACC_CACHE:
   12146                 :             :         case EXEC_OACC_ENTER_DATA:
   12147                 :             :         case EXEC_OACC_EXIT_DATA:
   12148                 :             :         case EXEC_OACC_ROUTINE:
   12149                 :             :         case EXEC_OMP_ALLOCATE:
   12150                 :             :         case EXEC_OMP_ALLOCATORS:
   12151                 :             :         case EXEC_OMP_ASSUME:
   12152                 :             :         case EXEC_OMP_CRITICAL:
   12153                 :             :         case EXEC_OMP_DISPATCH:
   12154                 :             :         case EXEC_OMP_DISTRIBUTE:
   12155                 :             :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   12156                 :             :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   12157                 :             :         case EXEC_OMP_DISTRIBUTE_SIMD:
   12158                 :             :         case EXEC_OMP_DO:
   12159                 :             :         case EXEC_OMP_DO_SIMD:
   12160                 :             :         case EXEC_OMP_ERROR:
   12161                 :             :         case EXEC_OMP_LOOP:
   12162                 :             :         case EXEC_OMP_MASKED:
   12163                 :             :         case EXEC_OMP_MASKED_TASKLOOP:
   12164                 :             :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   12165                 :             :         case EXEC_OMP_MASTER:
   12166                 :             :         case EXEC_OMP_MASTER_TASKLOOP:
   12167                 :             :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   12168                 :             :         case EXEC_OMP_ORDERED:
   12169                 :             :         case EXEC_OMP_PARALLEL:
   12170                 :             :         case EXEC_OMP_PARALLEL_DO:
   12171                 :             :         case EXEC_OMP_PARALLEL_DO_SIMD:
   12172                 :             :         case EXEC_OMP_PARALLEL_LOOP:
   12173                 :             :         case EXEC_OMP_PARALLEL_MASKED:
   12174                 :             :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   12175                 :             :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   12176                 :             :         case EXEC_OMP_PARALLEL_MASTER:
   12177                 :             :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   12178                 :             :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   12179                 :             :         case EXEC_OMP_PARALLEL_SECTIONS:
   12180                 :             :         case EXEC_OMP_PARALLEL_WORKSHARE:
   12181                 :             :         case EXEC_OMP_SECTIONS:
   12182                 :             :         case EXEC_OMP_SIMD:
   12183                 :             :         case EXEC_OMP_SCOPE:
   12184                 :             :         case EXEC_OMP_SINGLE:
   12185                 :             :         case EXEC_OMP_TARGET:
   12186                 :             :         case EXEC_OMP_TARGET_DATA:
   12187                 :             :         case EXEC_OMP_TARGET_ENTER_DATA:
   12188                 :             :         case EXEC_OMP_TARGET_EXIT_DATA:
   12189                 :             :         case EXEC_OMP_TARGET_PARALLEL:
   12190                 :             :         case EXEC_OMP_TARGET_PARALLEL_DO:
   12191                 :             :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   12192                 :             :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   12193                 :             :         case EXEC_OMP_TARGET_SIMD:
   12194                 :             :         case EXEC_OMP_TARGET_TEAMS:
   12195                 :             :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   12196                 :             :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   12197                 :             :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   12198                 :             :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   12199                 :             :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   12200                 :             :         case EXEC_OMP_TARGET_UPDATE:
   12201                 :             :         case EXEC_OMP_TASK:
   12202                 :             :         case EXEC_OMP_TASKGROUP:
   12203                 :             :         case EXEC_OMP_TASKLOOP:
   12204                 :             :         case EXEC_OMP_TASKLOOP_SIMD:
   12205                 :             :         case EXEC_OMP_TASKWAIT:
   12206                 :             :         case EXEC_OMP_TASKYIELD:
   12207                 :             :         case EXEC_OMP_TEAMS:
   12208                 :             :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   12209                 :             :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   12210                 :             :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   12211                 :             :         case EXEC_OMP_TEAMS_LOOP:
   12212                 :             :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   12213                 :             :         case EXEC_OMP_TILE:
   12214                 :             :         case EXEC_OMP_UNROLL:
   12215                 :             :         case EXEC_OMP_WORKSHARE:
   12216                 :             :           break;
   12217                 :             : 
   12218                 :           0 :         default:
   12219                 :           0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   12220                 :             :         }
   12221                 :             : 
   12222                 :      328662 :       gfc_resolve_code (b->next, ns);
   12223                 :             :     }
   12224                 :      317484 : }
   12225                 :             : 
   12226                 :             : bool
   12227                 :           0 : caf_possible_reallocate (gfc_expr *e)
   12228                 :             : {
   12229                 :           0 :   symbol_attribute caf_attr;
   12230                 :           0 :   gfc_ref *last_arr_ref = nullptr;
   12231                 :             : 
   12232                 :           0 :   caf_attr = gfc_caf_attr (e);
   12233                 :           0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   12234                 :             :     return false;
   12235                 :             : 
   12236                 :             :   /* Only full array refs can indicate a needed reallocation.  */
   12237                 :           0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   12238                 :           0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   12239                 :           0 :       last_arr_ref = ref;
   12240                 :             : 
   12241                 :           0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   12242                 :             : }
   12243                 :             : 
   12244                 :             : /* Does everything to resolve an ordinary assignment.  Returns true
   12245                 :             :    if this is an interface assignment.  */
   12246                 :             : static bool
   12247                 :      277458 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   12248                 :             : {
   12249                 :      277458 :   bool rval = false;
   12250                 :      277458 :   gfc_expr *lhs;
   12251                 :      277458 :   gfc_expr *rhs;
   12252                 :      277458 :   int n;
   12253                 :      277458 :   gfc_ref *ref;
   12254                 :      277458 :   symbol_attribute attr;
   12255                 :             : 
   12256                 :      277458 :   if (gfc_extend_assign (code, ns))
   12257                 :             :     {
   12258                 :         752 :       gfc_expr** rhsptr;
   12259                 :             : 
   12260                 :         752 :       if (code->op == EXEC_ASSIGN_CALL)
   12261                 :             :         {
   12262                 :         336 :           lhs = code->ext.actual->expr;
   12263                 :         336 :           rhsptr = &code->ext.actual->next->expr;
   12264                 :             :         }
   12265                 :             :       else
   12266                 :             :         {
   12267                 :         416 :           gfc_actual_arglist* args;
   12268                 :         416 :           gfc_typebound_proc* tbp;
   12269                 :             : 
   12270                 :         416 :           gcc_assert (code->op == EXEC_COMPCALL);
   12271                 :             : 
   12272                 :         416 :           args = code->expr1->value.compcall.actual;
   12273                 :         416 :           lhs = args->expr;
   12274                 :         416 :           rhsptr = &args->next->expr;
   12275                 :             : 
   12276                 :         416 :           tbp = code->expr1->value.compcall.tbp;
   12277                 :         416 :           gcc_assert (!tbp->is_generic);
   12278                 :             :         }
   12279                 :             : 
   12280                 :             :       /* Make a temporary rhs when there is a default initializer
   12281                 :             :          and rhs is the same symbol as the lhs.  */
   12282                 :         752 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   12283                 :         380 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   12284                 :         327 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   12285                 :         931 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   12286                 :          24 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   12287                 :             : 
   12288                 :         752 :       return true;
   12289                 :             :     }
   12290                 :             : 
   12291                 :      276706 :   lhs = code->expr1;
   12292                 :      276706 :   rhs = code->expr2;
   12293                 :             : 
   12294                 :      276706 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   12295                 :      258705 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   12296                 :       20355 :       && !lhs->symtree->n.sym->attr.proc_pointer
   12297                 :      297061 :       && gfc_expr_attr (lhs).proc_pointer)
   12298                 :             :     {
   12299                 :           1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   12300                 :             :                  "pointer component",
   12301                 :             :                  &lhs->where);
   12302                 :           1 :       return false;
   12303                 :             :     }
   12304                 :             : 
   12305                 :      324566 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   12306                 :      243925 :       && rhs->ts.type == BT_CHARACTER
   12307                 :      277098 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   12308                 :             :     {
   12309                 :             :       /* Use of -fdec-char-conversions allows assignment of character data
   12310                 :             :          to non-character variables.  This not permitted for nonconstant
   12311                 :             :          strings.  */
   12312                 :          29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   12313                 :             :                  gfc_typename (lhs), &rhs->where);
   12314                 :          29 :       return false;
   12315                 :             :     }
   12316                 :             : 
   12317                 :      276676 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   12318                 :             :     {
   12319                 :           0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   12320                 :             :                    gfc_typename (lhs), &rhs->where);
   12321                 :           0 :       return false;
   12322                 :             :     }
   12323                 :             : 
   12324                 :             :   /* Handle the case of a BOZ literal on the RHS.  */
   12325                 :      276676 :   if (rhs->ts.type == BT_BOZ)
   12326                 :             :     {
   12327                 :           3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   12328                 :             :                            "statement value nor an actual argument of "
   12329                 :             :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   12330                 :             :                            &rhs->where))
   12331                 :             :         return false;
   12332                 :             : 
   12333                 :           1 :       switch (lhs->ts.type)
   12334                 :             :         {
   12335                 :           0 :         case BT_INTEGER:
   12336                 :           0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   12337                 :             :             return false;
   12338                 :             :           break;
   12339                 :           1 :         case BT_REAL:
   12340                 :           1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   12341                 :             :             return false;
   12342                 :             :           break;
   12343                 :           0 :         default:
   12344                 :           0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   12345                 :           0 :           return false;
   12346                 :             :         }
   12347                 :             :     }
   12348                 :             : 
   12349                 :      276674 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   12350                 :             :     {
   12351                 :          64 :       HOST_WIDE_INT llen = 0, rlen = 0;
   12352                 :          64 :       if (lhs->ts.u.cl != NULL
   12353                 :          64 :             && lhs->ts.u.cl->length != NULL
   12354                 :          53 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   12355                 :          53 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   12356                 :             : 
   12357                 :          64 :       if (rhs->expr_type == EXPR_CONSTANT)
   12358                 :          26 :         rlen = rhs->value.character.length;
   12359                 :             : 
   12360                 :          38 :       else if (rhs->ts.u.cl != NULL
   12361                 :          38 :                  && rhs->ts.u.cl->length != NULL
   12362                 :          35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   12363                 :          35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   12364                 :             : 
   12365                 :          64 :       if (rlen && llen && rlen > llen)
   12366                 :          28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   12367                 :             :                          "CHARACTER expression will be truncated "
   12368                 :             :                          "in assignment (%wd/%wd) at %L",
   12369                 :             :                          llen, rlen, &code->loc);
   12370                 :             :     }
   12371                 :             : 
   12372                 :             :   /* Ensure that a vector index expression for the lvalue is evaluated
   12373                 :             :      to a temporary if the lvalue symbol is referenced in it.  */
   12374                 :      276674 :   if (lhs->rank)
   12375                 :             :     {
   12376                 :      104528 :       for (ref = lhs->ref; ref; ref= ref->next)
   12377                 :       55205 :         if (ref->type == REF_ARRAY)
   12378                 :             :           {
   12379                 :      124392 :             for (n = 0; n < ref->u.ar.dimen; n++)
   12380                 :       73722 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   12381                 :       73722 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   12382                 :             :                                            ref->u.ar.start[n]))
   12383                 :          14 :                 ref->u.ar.start[n]
   12384                 :          14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   12385                 :             :           }
   12386                 :             :     }
   12387                 :             : 
   12388                 :      276674 :   if (gfc_pure (NULL))
   12389                 :             :     {
   12390                 :        3232 :       if (lhs->ts.type == BT_DERIVED
   12391                 :          84 :             && lhs->expr_type == EXPR_VARIABLE
   12392                 :          84 :             && lhs->ts.u.derived->attr.pointer_comp
   12393                 :           4 :             && rhs->expr_type == EXPR_VARIABLE
   12394                 :        3235 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   12395                 :           2 :                 || gfc_is_coindexed (rhs)))
   12396                 :             :         {
   12397                 :             :           /* F2008, C1283.  */
   12398                 :           2 :           if (gfc_is_coindexed (rhs))
   12399                 :           1 :             gfc_error ("Coindexed expression at %L is assigned to "
   12400                 :             :                         "a derived type variable with a POINTER "
   12401                 :             :                         "component in a PURE procedure",
   12402                 :             :                         &rhs->where);
   12403                 :             :           else
   12404                 :             :           /* F2008, C1283 (4).  */
   12405                 :           1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   12406                 :             :                         "shall not be used as the expr at %L of an intrinsic "
   12407                 :             :                         "assignment statement in which the variable is of a "
   12408                 :             :                         "derived type if the derived type has a pointer "
   12409                 :             :                         "component at any level of component selection.",
   12410                 :             :                         &rhs->where);
   12411                 :           2 :           return rval;
   12412                 :             :         }
   12413                 :             : 
   12414                 :             :       /* Fortran 2008, C1283.  */
   12415                 :        3230 :       if (gfc_is_coindexed (lhs))
   12416                 :             :         {
   12417                 :           1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   12418                 :             :                      "procedure", &rhs->where);
   12419                 :           1 :           return rval;
   12420                 :             :         }
   12421                 :             :     }
   12422                 :             : 
   12423                 :      276671 :   if (gfc_implicit_pure (NULL))
   12424                 :             :     {
   12425                 :        6906 :       if (lhs->expr_type == EXPR_VARIABLE
   12426                 :        6906 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   12427                 :        4953 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   12428                 :         231 :         gfc_unset_implicit_pure (NULL);
   12429                 :             : 
   12430                 :        6906 :       if (lhs->ts.type == BT_DERIVED
   12431                 :         299 :             && lhs->expr_type == EXPR_VARIABLE
   12432                 :         299 :             && lhs->ts.u.derived->attr.pointer_comp
   12433                 :           7 :             && rhs->expr_type == EXPR_VARIABLE
   12434                 :        6913 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   12435                 :           7 :                 || gfc_is_coindexed (rhs)))
   12436                 :           0 :         gfc_unset_implicit_pure (NULL);
   12437                 :             : 
   12438                 :             :       /* Fortran 2008, C1283.  */
   12439                 :        6906 :       if (gfc_is_coindexed (lhs))
   12440                 :           0 :         gfc_unset_implicit_pure (NULL);
   12441                 :             :     }
   12442                 :             : 
   12443                 :             :   /* F2008, 7.2.1.2.  */
   12444                 :      276671 :   attr = gfc_expr_attr (lhs);
   12445                 :      276671 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   12446                 :             :     {
   12447                 :         855 :       if (attr.codimension)
   12448                 :             :         {
   12449                 :           1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   12450                 :             :                      "permitted", &lhs->where);
   12451                 :           1 :           return false;
   12452                 :             :         }
   12453                 :         854 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   12454                 :             :                            "polymorphic variable at %L", &lhs->where))
   12455                 :             :         return false;
   12456                 :         853 :       if (!flag_realloc_lhs)
   12457                 :             :         {
   12458                 :           1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   12459                 :             :                      "requires %<-frealloc-lhs%>", &lhs->where);
   12460                 :           1 :           return false;
   12461                 :             :         }
   12462                 :             :     }
   12463                 :      275816 :   else if (lhs->ts.type == BT_CLASS)
   12464                 :             :     {
   12465                 :           9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   12466                 :             :                  "assignment at %L - check that there is a matching specific "
   12467                 :             :                  "subroutine for %<=%> operator", &lhs->where);
   12468                 :           9 :       return false;
   12469                 :             :     }
   12470                 :             : 
   12471                 :      276659 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   12472                 :             : 
   12473                 :             :   /* F2008, Section 7.2.1.2.  */
   12474                 :      276659 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   12475                 :             :     {
   12476                 :           1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   12477                 :             :                  "component in assignment at %L", &lhs->where);
   12478                 :           1 :       return false;
   12479                 :             :     }
   12480                 :             : 
   12481                 :             :   /* Assign the 'data' of a class object to a derived type.  */
   12482                 :      276658 :   if (lhs->ts.type == BT_DERIVED
   12483                 :        6470 :       && rhs->ts.type == BT_CLASS
   12484                 :         137 :       && rhs->expr_type != EXPR_ARRAY)
   12485                 :         131 :     gfc_add_data_component (rhs);
   12486                 :             : 
   12487                 :             :   /* Make sure there is a vtable and, in particular, a _copy for the
   12488                 :             :      rhs type.  */
   12489                 :      276658 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   12490                 :         501 :     gfc_find_vtab (&rhs->ts);
   12491                 :             : 
   12492                 :      276658 :   gfc_check_assign (lhs, rhs, 1);
   12493                 :             : 
   12494                 :      276658 :   return false;
   12495                 :             : }
   12496                 :             : 
   12497                 :             : 
   12498                 :             : /* Add a component reference onto an expression.  */
   12499                 :             : 
   12500                 :             : static void
   12501                 :         665 : add_comp_ref (gfc_expr *e, gfc_component *c)
   12502                 :             : {
   12503                 :         665 :   gfc_ref **ref;
   12504                 :         665 :   ref = &(e->ref);
   12505                 :         889 :   while (*ref)
   12506                 :         224 :     ref = &((*ref)->next);
   12507                 :         665 :   *ref = gfc_get_ref ();
   12508                 :         665 :   (*ref)->type = REF_COMPONENT;
   12509                 :         665 :   (*ref)->u.c.sym = e->ts.u.derived;
   12510                 :         665 :   (*ref)->u.c.component = c;
   12511                 :         665 :   e->ts = c->ts;
   12512                 :             : 
   12513                 :             :   /* Add a full array ref, as necessary.  */
   12514                 :         665 :   if (c->as)
   12515                 :             :     {
   12516                 :          84 :       gfc_add_full_array_ref (e, c->as);
   12517                 :          84 :       e->rank = c->as->rank;
   12518                 :          84 :       e->corank = c->as->corank;
   12519                 :             :     }
   12520                 :         665 : }
   12521                 :             : 
   12522                 :             : 
   12523                 :             : /* Build an assignment.  Keep the argument 'op' for future use, so that
   12524                 :             :    pointer assignments can be made.  */
   12525                 :             : 
   12526                 :             : static gfc_code *
   12527                 :         898 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   12528                 :             :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   12529                 :             : {
   12530                 :         898 :   gfc_code *this_code;
   12531                 :             : 
   12532                 :         898 :   this_code = gfc_get_code (op);
   12533                 :         898 :   this_code->next = NULL;
   12534                 :         898 :   this_code->expr1 = gfc_copy_expr (expr1);
   12535                 :         898 :   this_code->expr2 = gfc_copy_expr (expr2);
   12536                 :         898 :   this_code->loc = loc;
   12537                 :         898 :   if (comp1 && comp2)
   12538                 :             :     {
   12539                 :         288 :       add_comp_ref (this_code->expr1, comp1);
   12540                 :         288 :       add_comp_ref (this_code->expr2, comp2);
   12541                 :             :     }
   12542                 :             : 
   12543                 :         898 :   return this_code;
   12544                 :             : }
   12545                 :             : 
   12546                 :             : 
   12547                 :             : /* Makes a temporary variable expression based on the characteristics of
   12548                 :             :    a given variable expression.  */
   12549                 :             : 
   12550                 :             : static gfc_expr*
   12551                 :         392 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
   12552                 :             : {
   12553                 :         392 :   static int serial = 0;
   12554                 :         392 :   char name[GFC_MAX_SYMBOL_LEN];
   12555                 :         392 :   gfc_symtree *tmp;
   12556                 :         392 :   gfc_array_spec *as;
   12557                 :         392 :   gfc_array_ref *aref;
   12558                 :         392 :   gfc_ref *ref;
   12559                 :             : 
   12560                 :         392 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   12561                 :         392 :   gfc_get_sym_tree (name, ns, &tmp, false);
   12562                 :         392 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   12563                 :             : 
   12564                 :         392 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   12565                 :           0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   12566                 :             :                                                     NULL,
   12567                 :             :                                                     e->value.character.length);
   12568                 :             : 
   12569                 :         392 :   as = NULL;
   12570                 :         392 :   ref = NULL;
   12571                 :         392 :   aref = NULL;
   12572                 :             : 
   12573                 :             :   /* Obtain the arrayspec for the temporary.  */
   12574                 :         392 :    if (e->rank && e->expr_type != EXPR_ARRAY
   12575                 :             :        && e->expr_type != EXPR_FUNCTION
   12576                 :             :        && e->expr_type != EXPR_OP)
   12577                 :             :     {
   12578                 :          52 :       aref = gfc_find_array_ref (e);
   12579                 :          52 :       if (e->expr_type == EXPR_VARIABLE
   12580                 :          52 :           && e->symtree->n.sym->as == aref->as)
   12581                 :             :         as = aref->as;
   12582                 :             :       else
   12583                 :             :         {
   12584                 :           0 :           for (ref = e->ref; ref; ref = ref->next)
   12585                 :           0 :             if (ref->type == REF_COMPONENT
   12586                 :           0 :                 && ref->u.c.component->as == aref->as)
   12587                 :             :               {
   12588                 :             :                 as = aref->as;
   12589                 :             :                 break;
   12590                 :             :               }
   12591                 :             :         }
   12592                 :             :     }
   12593                 :             : 
   12594                 :             :   /* Add the attributes and the arrayspec to the temporary.  */
   12595                 :         392 :   tmp->n.sym->attr = gfc_expr_attr (e);
   12596                 :         392 :   tmp->n.sym->attr.function = 0;
   12597                 :         392 :   tmp->n.sym->attr.proc_pointer = 0;
   12598                 :         392 :   tmp->n.sym->attr.result = 0;
   12599                 :         392 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   12600                 :         392 :   tmp->n.sym->attr.dummy = 0;
   12601                 :         392 :   tmp->n.sym->attr.use_assoc = 0;
   12602                 :         392 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   12603                 :             : 
   12604                 :             : 
   12605                 :         392 :   if (as)
   12606                 :             :     {
   12607                 :          52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   12608                 :          52 :       if (!ref)
   12609                 :          52 :         ref = e->ref;
   12610                 :          52 :       if (as->type == AS_DEFERRED)
   12611                 :          46 :         tmp->n.sym->attr.allocatable = 1;
   12612                 :             :     }
   12613                 :         340 :   else if ((e->rank || e->corank)
   12614                 :          48 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   12615                 :           0 :                || e->expr_type == EXPR_OP))
   12616                 :             :     {
   12617                 :          48 :       tmp->n.sym->as = gfc_get_array_spec ();
   12618                 :          48 :       tmp->n.sym->as->type = AS_DEFERRED;
   12619                 :          48 :       tmp->n.sym->as->rank = e->rank;
   12620                 :          48 :       tmp->n.sym->as->corank = e->corank;
   12621                 :          48 :       tmp->n.sym->attr.allocatable = 1;
   12622                 :          48 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   12623                 :          96 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   12624                 :             :     }
   12625                 :             :   else
   12626                 :         292 :     tmp->n.sym->attr.dimension = 0;
   12627                 :             : 
   12628                 :         392 :   gfc_set_sym_referenced (tmp->n.sym);
   12629                 :         392 :   gfc_commit_symbol (tmp->n.sym);
   12630                 :         392 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   12631                 :             : 
   12632                 :             :   /* Should the lhs be a section, use its array ref for the
   12633                 :             :      temporary expression.  */
   12634                 :         392 :   if (aref && aref->type != AR_FULL)
   12635                 :             :     {
   12636                 :           6 :       gfc_free_ref_list (e->ref);
   12637                 :           6 :       e->ref = gfc_copy_ref (ref);
   12638                 :             :     }
   12639                 :         392 :   return e;
   12640                 :             : }
   12641                 :             : 
   12642                 :             : 
   12643                 :             : /* Add one line of code to the code chain, making sure that 'head' and
   12644                 :             :    'tail' are appropriately updated.  */
   12645                 :             : 
   12646                 :             : static void
   12647                 :         656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   12648                 :             : {
   12649                 :         656 :   gcc_assert (this_code);
   12650                 :         656 :   if (*head == NULL)
   12651                 :         308 :     *head = *tail = *this_code;
   12652                 :             :   else
   12653                 :         348 :     *tail = gfc_append_code (*tail, *this_code);
   12654                 :         656 :   *this_code = NULL;
   12655                 :         656 : }
   12656                 :             : 
   12657                 :             : 
   12658                 :             : /* Generate a final call from a variable expression  */
   12659                 :             : 
   12660                 :             : static void
   12661                 :          81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   12662                 :             : {
   12663                 :          81 :   gfc_code *this_code;
   12664                 :          81 :   gfc_expr *final_expr = NULL;
   12665                 :          81 :   gfc_expr *size_expr;
   12666                 :          81 :   gfc_expr *fini_coarray;
   12667                 :             : 
   12668                 :          81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   12669                 :          81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   12670                 :          75 :     return;
   12671                 :             : 
   12672                 :             :   /* Now generate the finalizer call.  */
   12673                 :           6 :   this_code = gfc_get_code (EXEC_CALL);
   12674                 :           6 :   this_code->symtree = final_expr->symtree;
   12675                 :           6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   12676                 :             : 
   12677                 :             :   //* Expression to be finalized  */
   12678                 :           6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   12679                 :           6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   12680                 :             : 
   12681                 :             :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   12682                 :           6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   12683                 :           6 :   size_expr = gfc_get_expr ();
   12684                 :           6 :   size_expr->where = gfc_current_locus;
   12685                 :           6 :   size_expr->expr_type = EXPR_OP;
   12686                 :           6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   12687                 :           6 :   size_expr->value.op.op1
   12688                 :          12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   12689                 :             :                                     "storage_size", gfc_current_locus, 2,
   12690                 :           6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   12691                 :             :                                     gfc_get_int_expr (gfc_index_integer_kind,
   12692                 :             :                                                       NULL, 0));
   12693                 :           6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   12694                 :             :                                               gfc_character_storage_size);
   12695                 :           6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   12696                 :           6 :   size_expr->ts = size_expr->value.op.op1->ts;
   12697                 :           6 :   this_code->ext.actual->next->expr = size_expr;
   12698                 :             : 
   12699                 :             :   /* fini_coarray  */
   12700                 :           6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   12701                 :           6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   12702                 :             :                                         &tmp_expr->where);
   12703                 :           6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   12704                 :           6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   12705                 :             : 
   12706                 :           6 :   add_code_to_chain (&this_code, head, tail);
   12707                 :             : 
   12708                 :             : }
   12709                 :             : 
   12710                 :             : /* Counts the potential number of part array references that would
   12711                 :             :    result from resolution of typebound defined assignments.  */
   12712                 :             : 
   12713                 :             : 
   12714                 :             : static int
   12715                 :         243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   12716                 :             : {
   12717                 :         243 :   gfc_component *c;
   12718                 :         243 :   int c_depth = 0, t_depth;
   12719                 :             : 
   12720                 :         584 :   for (c= derived->components; c; c = c->next)
   12721                 :             :     {
   12722                 :         341 :       if ((!gfc_bt_struct (c->ts.type)
   12723                 :             :             || c->attr.pointer
   12724                 :         261 :             || c->attr.allocatable
   12725                 :         260 :             || c->attr.proc_pointer_comp
   12726                 :             :             || c->attr.class_pointer
   12727                 :         260 :             || c->attr.proc_pointer)
   12728                 :          81 :           && !c->attr.defined_assign_comp)
   12729                 :          81 :         continue;
   12730                 :             : 
   12731                 :         260 :       if (c->as && c_depth == 0)
   12732                 :         260 :         c_depth = 1;
   12733                 :             : 
   12734                 :         260 :       if (c->ts.u.derived->attr.defined_assign_comp)
   12735                 :         110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   12736                 :             :                                               c->as ? 1 : 0);
   12737                 :             :       else
   12738                 :             :         t_depth = 0;
   12739                 :             : 
   12740                 :         260 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   12741                 :             :     }
   12742                 :         243 :   return depth + c_depth;
   12743                 :             : }
   12744                 :             : 
   12745                 :             : 
   12746                 :             : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   12747                 :             :    "An intrinsic assignment where the variable is of derived type is performed
   12748                 :             :     as if each component of the variable were assigned from the corresponding
   12749                 :             :     component of expr using pointer assignment (10.2.2) for each pointer
   12750                 :             :     component, defined assignment for each nonpointer nonallocatable component
   12751                 :             :     of a type that has a type-bound defined assignment consistent with the
   12752                 :             :     component, intrinsic assignment for each other nonpointer nonallocatable
   12753                 :             :     component, and intrinsic assignment for each allocated coarray component.
   12754                 :             :     For unallocated coarray components, the corresponding component of the
   12755                 :             :     variable shall be unallocated. For a noncoarray allocatable component the
   12756                 :             :     following sequence of operations is applied.
   12757                 :             :         (1) If the component of the variable is allocated, it is deallocated.
   12758                 :             :         (2) If the component of the value of expr is allocated, the
   12759                 :             :             corresponding component of the variable is allocated with the same
   12760                 :             :             dynamic type and type parameters as the component of the value of
   12761                 :             :             expr. If it is an array, it is allocated with the same bounds. The
   12762                 :             :             value of the component of the value of expr is then assigned to the
   12763                 :             :             corresponding component of the variable using defined assignment if
   12764                 :             :             the declared type of the component has a type-bound defined
   12765                 :             :             assignment consistent with the component, and intrinsic assignment
   12766                 :             :             for the dynamic type of that component otherwise."
   12767                 :             : 
   12768                 :             :    The pointer assignments are taken care of by the intrinsic assignment of the
   12769                 :             :    structure itself.  This function recursively adds defined assignments where
   12770                 :             :    required.  The recursion is accomplished by calling gfc_resolve_code.
   12771                 :             : 
   12772                 :             :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   12773                 :             :    and the component of 'var' is finalizable, we need a temporary for the
   12774                 :             :    lhs.  In pseudo-code for an assignment var = expr:
   12775                 :             : 
   12776                 :             :    ! Confine finalization of temporaries, as far as possible.
   12777                 :             :      Enclose the code for the assignment in a block
   12778                 :             :    ! Only call function 'expr' once.
   12779                 :             :       #if ('expr is not a constant or an variable)
   12780                 :             :         temp_expr = expr
   12781                 :             :         expr = temp_x
   12782                 :             :    ! Do the intrinsic assignment
   12783                 :             :       #if typeof ('var') has a typebound final subroutine
   12784                 :             :         finalize (var)
   12785                 :             :       var = expr
   12786                 :             :    ! Now do the component assignments
   12787                 :             :       #do over derived type components [%cmp]
   12788                 :             :         #if (cmp is a pointer of any kind)
   12789                 :             :           continue
   12790                 :             :         build the assignment
   12791                 :             :         resolve the code
   12792                 :             :         #if the code is a typebound assignment
   12793                 :             :            #if (arg1 is INOUT or finalizable OUT && !t1)
   12794                 :             :              t1 = var
   12795                 :             :              arg1 = t1
   12796                 :             :              deal with allocatation or not of var and this component
   12797                 :             :         #elseif the code is an assignment by itself
   12798                 :             :            #if this component does not need finalization
   12799                 :             :              delete code and continue
   12800                 :             :         #else
   12801                 :             :            remove the leading assignment
   12802                 :             :         #endif
   12803                 :             :         commit the code
   12804                 :             :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   12805                 :             :            var%cmp = t1%cmp
   12806                 :             :       #enddo
   12807                 :             :       put all code chunks involving t1 to the top of the generated code
   12808                 :             :       insert the generated block in place of the original code
   12809                 :             : */
   12810                 :             : 
   12811                 :             : static bool
   12812                 :         381 : is_finalizable_type (gfc_typespec ts)
   12813                 :             : {
   12814                 :         381 :   gfc_component *c;
   12815                 :             : 
   12816                 :         381 :   if (ts.type != BT_DERIVED)
   12817                 :             :     return false;
   12818                 :             : 
   12819                 :             :   /* (1) Check for FINAL subroutines.  */
   12820                 :         381 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   12821                 :             :     return true;
   12822                 :             : 
   12823                 :             :   /* (2) Check for components of finalizable type.  */
   12824                 :         809 :   for (c = ts.u.derived->components; c; c = c->next)
   12825                 :         470 :     if (c->ts.type == BT_DERIVED
   12826                 :         243 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   12827                 :         242 :         && c->ts.u.derived->f2k_derived
   12828                 :         242 :         && c->ts.u.derived->f2k_derived->finalizers)
   12829                 :             :       return true;
   12830                 :             : 
   12831                 :             :   return false;
   12832                 :             : }
   12833                 :             : 
   12834                 :             : /* The temporary assignments have to be put on top of the additional
   12835                 :             :    code to avoid the result being changed by the intrinsic assignment.
   12836                 :             :    */
   12837                 :             : static int component_assignment_level = 0;
   12838                 :             : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   12839                 :             : static bool finalizable_comp;
   12840                 :             : 
   12841                 :             : static void
   12842                 :         188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   12843                 :             : {
   12844                 :         188 :   gfc_component *comp1, *comp2;
   12845                 :         188 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   12846                 :         188 :   gfc_code *tmp_code = NULL;
   12847                 :         188 :   gfc_expr *t1 = NULL;
   12848                 :         188 :   gfc_expr *tmp_expr = NULL;
   12849                 :         188 :   int error_count, depth;
   12850                 :         188 :   bool finalizable_lhs;
   12851                 :             : 
   12852                 :         188 :   gfc_get_errors (NULL, &error_count);
   12853                 :             : 
   12854                 :             :   /* Filter out continuing processing after an error.  */
   12855                 :         188 :   if (error_count
   12856                 :         188 :       || (*code)->expr1->ts.type != BT_DERIVED
   12857                 :         188 :       || (*code)->expr2->ts.type != BT_DERIVED)
   12858                 :         140 :     return;
   12859                 :             : 
   12860                 :             :   /* TODO: Handle more than one part array reference in assignments.  */
   12861                 :         188 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   12862                 :         188 :                                       (*code)->expr1->rank ? 1 : 0);
   12863                 :         188 :   if (depth > 1)
   12864                 :             :     {
   12865                 :           6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   12866                 :             :                    "done because multiple part array references would "
   12867                 :             :                    "occur in intermediate expressions.", &(*code)->loc);
   12868                 :           6 :       return;
   12869                 :             :     }
   12870                 :             : 
   12871                 :         182 :   if (!component_assignment_level)
   12872                 :         134 :     finalizable_comp = true;
   12873                 :             : 
   12874                 :             :   /* Build a block so that function result temporaries are finalized
   12875                 :             :      locally on exiting the rather than enclosing scope.  */
   12876                 :         182 :   if (!component_assignment_level)
   12877                 :             :     {
   12878                 :         134 :       ns = gfc_build_block_ns (ns);
   12879                 :         134 :       tmp_code = gfc_get_code (EXEC_NOP);
   12880                 :         134 :       *tmp_code = **code;
   12881                 :         134 :       tmp_code->next = NULL;
   12882                 :         134 :       (*code)->op = EXEC_BLOCK;
   12883                 :         134 :       (*code)->ext.block.ns = ns;
   12884                 :         134 :       (*code)->ext.block.assoc = NULL;
   12885                 :         134 :       (*code)->expr1 = (*code)->expr2 = NULL;
   12886                 :         134 :       ns->code = tmp_code;
   12887                 :         134 :       code = &ns->code;
   12888                 :             :     }
   12889                 :             : 
   12890                 :         182 :   component_assignment_level++;
   12891                 :             : 
   12892                 :         182 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   12893                 :             : 
   12894                 :             :   /* Create a temporary so that functions get called only once.  */
   12895                 :         182 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   12896                 :         182 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   12897                 :             :     {
   12898                 :             :       /* Assign the rhs to the temporary.  */
   12899                 :          81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   12900                 :          81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   12901                 :             :         {
   12902                 :             :           /* Use allocate on assignment for the sake of simplicity. The
   12903                 :             :              temporary must not take on the optional attribute. Assume
   12904                 :             :              that the assignment is guarded by a PRESENT condition if the
   12905                 :             :              lhs is optional.  */
   12906                 :          25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   12907                 :          25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   12908                 :          25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   12909                 :             :         }
   12910                 :         162 :       this_code = build_assignment (EXEC_ASSIGN,
   12911                 :             :                                     tmp_expr, (*code)->expr2,
   12912                 :          81 :                                     NULL, NULL, (*code)->loc);
   12913                 :          81 :       this_code->expr2->must_finalize = 1;
   12914                 :             :       /* Add the code and substitute the rhs expression.  */
   12915                 :          81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   12916                 :          81 :       gfc_free_expr ((*code)->expr2);
   12917                 :          81 :       (*code)->expr2 = tmp_expr;
   12918                 :             :     }
   12919                 :             : 
   12920                 :             :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   12921                 :             :      of the temporaries generated here, since the intrinsic assignment
   12922                 :             :      to the final result already does this.  */
   12923                 :         182 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   12924                 :             :     {
   12925                 :         182 :       if (finalizable_lhs)
   12926                 :          18 :         (*code)->expr1->must_finalize = 1;
   12927                 :         182 :       this_code = build_assignment (EXEC_ASSIGN,
   12928                 :             :                                     (*code)->expr1, (*code)->expr2,
   12929                 :             :                                     NULL, NULL, (*code)->loc);
   12930                 :         182 :       add_code_to_chain (&this_code, &head, &tail);
   12931                 :             :     }
   12932                 :             : 
   12933                 :         182 :   comp1 = (*code)->expr1->ts.u.derived->components;
   12934                 :         182 :   comp2 = (*code)->expr2->ts.u.derived->components;
   12935                 :             : 
   12936                 :         449 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   12937                 :             :     {
   12938                 :         267 :       bool inout = false;
   12939                 :         267 :       bool finalizable_out = false;
   12940                 :             : 
   12941                 :             :       /* The intrinsic assignment does the right thing for pointers
   12942                 :             :          of all kinds and allocatable components.  */
   12943                 :         267 :       if (!gfc_bt_struct (comp1->ts.type)
   12944                 :             :           || comp1->attr.pointer
   12945                 :         200 :           || comp1->attr.allocatable
   12946                 :         199 :           || comp1->attr.proc_pointer_comp
   12947                 :             :           || comp1->attr.class_pointer
   12948                 :         199 :           || comp1->attr.proc_pointer)
   12949                 :          68 :         continue;
   12950                 :             : 
   12951                 :         398 :       finalizable_comp = is_finalizable_type (comp1->ts)
   12952                 :         199 :                          && !finalizable_lhs;
   12953                 :             : 
   12954                 :             :       /* Make an assignment for this component.  */
   12955                 :         398 :       this_code = build_assignment (EXEC_ASSIGN,
   12956                 :             :                                     (*code)->expr1, (*code)->expr2,
   12957                 :         199 :                                     comp1, comp2, (*code)->loc);
   12958                 :             : 
   12959                 :             :       /* Convert the assignment if there is a defined assignment for
   12960                 :             :          this type.  Otherwise, using the call from gfc_resolve_code,
   12961                 :             :          recurse into its components.  */
   12962                 :         199 :       gfc_resolve_code (this_code, ns);
   12963                 :             : 
   12964                 :         199 :       if (this_code->op == EXEC_ASSIGN_CALL)
   12965                 :             :         {
   12966                 :         144 :           gfc_formal_arglist *dummy_args;
   12967                 :         144 :           gfc_symbol *rsym;
   12968                 :             :           /* Check that there is a typebound defined assignment.  If not,
   12969                 :             :              then this must be a module defined assignment.  We cannot
   12970                 :             :              use the defined_assign_comp attribute here because it must
   12971                 :             :              be this derived type that has the defined assignment and not
   12972                 :             :              a parent type.  */
   12973                 :         144 :           if (!(comp1->ts.u.derived->f2k_derived
   12974                 :             :                 && comp1->ts.u.derived->f2k_derived
   12975                 :         144 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   12976                 :             :             {
   12977                 :           1 :               gfc_free_statements (this_code);
   12978                 :           1 :               this_code = NULL;
   12979                 :           1 :               continue;
   12980                 :             :             }
   12981                 :             : 
   12982                 :             :           /* If the first argument of the subroutine has intent INOUT
   12983                 :             :              a temporary must be generated and used instead.  */
   12984                 :         143 :           rsym = this_code->resolved_sym;
   12985                 :         143 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   12986                 :         268 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   12987                 :          18 :                             && dummy_args
   12988                 :         161 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   12989                 :         286 :           inout = dummy_args
   12990                 :         268 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   12991                 :          72 :           if ((inout || finalizable_out)
   12992                 :          89 :               && !comp1->attr.allocatable)
   12993                 :             :             {
   12994                 :          89 :               gfc_code *temp_code;
   12995                 :          89 :               inout = true;
   12996                 :             : 
   12997                 :             :               /* Build the temporary required for the assignment and put
   12998                 :             :                  it at the head of the generated code.  */
   12999                 :          89 :               if (!t1)
   13000                 :             :                 {
   13001                 :          89 :                   gfc_namespace *tmp_ns = ns;
   13002                 :          89 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   13003                 :          18 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   13004                 :          89 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   13005                 :          89 :                   t1->symtree->n.sym->attr.artificial = 1;
   13006                 :         178 :                   temp_code = build_assignment (EXEC_ASSIGN,
   13007                 :             :                                                 t1, (*code)->expr1,
   13008                 :          89 :                                 NULL, NULL, (*code)->loc);
   13009                 :             : 
   13010                 :             :                   /* For allocatable LHS, check whether it is allocated.  Note
   13011                 :             :                      that allocatable components with defined assignment are
   13012                 :             :                      not yet support.  See PR 57696.  */
   13013                 :          89 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   13014                 :             :                     {
   13015                 :          24 :                       gfc_code *block;
   13016                 :          24 :                       gfc_expr *e =
   13017                 :          24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13018                 :          24 :                       block = gfc_get_code (EXEC_IF);
   13019                 :          24 :                       block->block = gfc_get_code (EXEC_IF);
   13020                 :          24 :                       block->block->expr1
   13021                 :          48 :                           = gfc_build_intrinsic_call (ns,
   13022                 :             :                                     GFC_ISYM_ALLOCATED, "allocated",
   13023                 :          24 :                                     (*code)->loc, 1, e);
   13024                 :          24 :                       block->block->next = temp_code;
   13025                 :          24 :                       temp_code = block;
   13026                 :             :                     }
   13027                 :          89 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   13028                 :             :                 }
   13029                 :             : 
   13030                 :             :               /* Replace the first actual arg with the component of the
   13031                 :             :                  temporary.  */
   13032                 :          89 :               gfc_free_expr (this_code->ext.actual->expr);
   13033                 :          89 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   13034                 :          89 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   13035                 :             : 
   13036                 :             :               /* If the LHS variable is allocatable and wasn't allocated and
   13037                 :             :                  the temporary is allocatable, pointer assign the address of
   13038                 :             :                  the freshly allocated LHS to the temporary.  */
   13039                 :          89 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   13040                 :          89 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   13041                 :             :                 {
   13042                 :          18 :                   gfc_code *block;
   13043                 :          18 :                   gfc_expr *cond;
   13044                 :             : 
   13045                 :          18 :                   cond = gfc_get_expr ();
   13046                 :          18 :                   cond->ts.type = BT_LOGICAL;
   13047                 :          18 :                   cond->ts.kind = gfc_default_logical_kind;
   13048                 :          18 :                   cond->expr_type = EXPR_OP;
   13049                 :          18 :                   cond->where = (*code)->loc;
   13050                 :          18 :                   cond->value.op.op = INTRINSIC_NOT;
   13051                 :          18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   13052                 :             :                                           GFC_ISYM_ALLOCATED, "allocated",
   13053                 :          18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   13054                 :          18 :                   block = gfc_get_code (EXEC_IF);
   13055                 :          18 :                   block->block = gfc_get_code (EXEC_IF);
   13056                 :          18 :                   block->block->expr1 = cond;
   13057                 :          36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13058                 :             :                                         t1, (*code)->expr1,
   13059                 :          18 :                                         NULL, NULL, (*code)->loc);
   13060                 :          18 :                   add_code_to_chain (&block, &head, &tail);
   13061                 :             :                 }
   13062                 :             :             }
   13063                 :             :         }
   13064                 :          55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   13065                 :             :         {
   13066                 :             :           /* Don't add intrinsic assignments since they are already
   13067                 :             :              effected by the intrinsic assignment of the structure, unless
   13068                 :             :              finalization is required.  */
   13069                 :           7 :           if (finalizable_comp)
   13070                 :           0 :             this_code->expr1->must_finalize = 1;
   13071                 :             :           else
   13072                 :             :             {
   13073                 :           7 :               gfc_free_statements (this_code);
   13074                 :           7 :               this_code = NULL;
   13075                 :           7 :               continue;
   13076                 :             :             }
   13077                 :             :         }
   13078                 :             :       else
   13079                 :             :         {
   13080                 :             :           /* Resolution has expanded an assignment of a derived type with
   13081                 :             :              defined assigned components.  Remove the redundant, leading
   13082                 :             :              assignment.  */
   13083                 :          48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   13084                 :          48 :           gfc_code *tmp = this_code;
   13085                 :          48 :           this_code = this_code->next;
   13086                 :          48 :           tmp->next = NULL;
   13087                 :          48 :           gfc_free_statements (tmp);
   13088                 :             :         }
   13089                 :             : 
   13090                 :         191 :       add_code_to_chain (&this_code, &head, &tail);
   13091                 :             : 
   13092                 :         191 :       if (t1 && (inout || finalizable_out))
   13093                 :             :         {
   13094                 :             :           /* Transfer the value to the final result.  */
   13095                 :         178 :           this_code = build_assignment (EXEC_ASSIGN,
   13096                 :             :                                         (*code)->expr1, t1,
   13097                 :          89 :                                         comp1, comp2, (*code)->loc);
   13098                 :          89 :           this_code->expr1->must_finalize = 0;
   13099                 :          89 :           add_code_to_chain (&this_code, &head, &tail);
   13100                 :             :         }
   13101                 :             :     }
   13102                 :             : 
   13103                 :             :   /* Put the temporary assignments at the top of the generated code.  */
   13104                 :         182 :   if (tmp_head && component_assignment_level == 1)
   13105                 :             :     {
   13106                 :         126 :       gfc_append_code (tmp_head, head);
   13107                 :         126 :       head = tmp_head;
   13108                 :         126 :       tmp_head = tmp_tail = NULL;
   13109                 :             :     }
   13110                 :             : 
   13111                 :             :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   13112                 :             :      not accidentally deallocated. Hence, nullify t1.  */
   13113                 :          89 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   13114                 :         271 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   13115                 :             :     {
   13116                 :          18 :       gfc_code *block;
   13117                 :          18 :       gfc_expr *cond;
   13118                 :          18 :       gfc_expr *e;
   13119                 :             : 
   13120                 :          18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13121                 :          18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   13122                 :          18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   13123                 :          18 :       block = gfc_get_code (EXEC_IF);
   13124                 :          18 :       block->block = gfc_get_code (EXEC_IF);
   13125                 :          18 :       block->block->expr1 = cond;
   13126                 :          18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13127                 :             :                                         t1, gfc_get_null_expr (&(*code)->loc),
   13128                 :          18 :                                         NULL, NULL, (*code)->loc);
   13129                 :          18 :       gfc_append_code (tail, block);
   13130                 :          18 :       tail = block;
   13131                 :             :     }
   13132                 :             : 
   13133                 :         182 :   component_assignment_level--;
   13134                 :             : 
   13135                 :             :   /* Make an explicit final call for the function result.  */
   13136                 :         182 :   if (tmp_expr)
   13137                 :          81 :     generate_final_call (tmp_expr, &head, &tail);
   13138                 :             : 
   13139                 :         182 :   if (tmp_code)
   13140                 :             :     {
   13141                 :         134 :       ns->code = head;
   13142                 :         134 :       return;
   13143                 :             :     }
   13144                 :             : 
   13145                 :             :   /* Now attach the remaining code chain to the input code.  Step on
   13146                 :             :      to the end of the new code since resolution is complete.  */
   13147                 :          48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   13148                 :          48 :   tail->next = (*code)->next;
   13149                 :             :   /* Overwrite 'code' because this would place the intrinsic assignment
   13150                 :             :      before the temporary for the lhs is created.  */
   13151                 :          48 :   gfc_free_expr ((*code)->expr1);
   13152                 :          48 :   gfc_free_expr ((*code)->expr2);
   13153                 :          48 :   **code = *head;
   13154                 :          48 :   if (head != tail)
   13155                 :          48 :     free (head);
   13156                 :          48 :   *code = tail;
   13157                 :             : }
   13158                 :             : 
   13159                 :             : 
   13160                 :             : /* F2008: Pointer function assignments are of the form:
   13161                 :             :         ptr_fcn (args) = expr
   13162                 :             :    This function breaks these assignments into two statements:
   13163                 :             :         temporary_pointer => ptr_fcn(args)
   13164                 :             :         temporary_pointer = expr  */
   13165                 :             : 
   13166                 :             : static bool
   13167                 :      277701 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   13168                 :             : {
   13169                 :      277701 :   gfc_expr *tmp_ptr_expr;
   13170                 :      277701 :   gfc_code *this_code;
   13171                 :      277701 :   gfc_component *comp;
   13172                 :      277701 :   gfc_symbol *s;
   13173                 :             : 
   13174                 :      277701 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   13175                 :             :     return false;
   13176                 :             : 
   13177                 :             :   /* Even if standard does not support this feature, continue to build
   13178                 :             :      the two statements to avoid upsetting frontend_passes.c.  */
   13179                 :         205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   13180                 :             :                   "%L", &(*code)->loc);
   13181                 :             : 
   13182                 :         205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   13183                 :             : 
   13184                 :         205 :   if (comp)
   13185                 :           6 :     s = comp->ts.interface;
   13186                 :             :   else
   13187                 :         199 :     s = (*code)->expr1->symtree->n.sym;
   13188                 :             : 
   13189                 :         205 :   if (s == NULL || !s->result->attr.pointer)
   13190                 :             :     {
   13191                 :           5 :       gfc_error ("The function result on the lhs of the assignment at "
   13192                 :             :                  "%L must have the pointer attribute.",
   13193                 :           5 :                  &(*code)->expr1->where);
   13194                 :           5 :       (*code)->op = EXEC_NOP;
   13195                 :           5 :       return false;
   13196                 :             :     }
   13197                 :             : 
   13198                 :         200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   13199                 :             : 
   13200                 :             :   /* get_temp_from_expression is set up for ordinary assignments. To that
   13201                 :             :      end, where array bounds are not known, arrays are made allocatable.
   13202                 :             :      Change the temporary to a pointer here.  */
   13203                 :         200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   13204                 :         200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   13205                 :         200 :   tmp_ptr_expr->where = (*code)->loc;
   13206                 :             : 
   13207                 :             :   /* A new charlen is required to ensure that the variable string length
   13208                 :             :      is different to that of the original lhs for deferred results.  */
   13209                 :         200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   13210                 :             :     {
   13211                 :          60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   13212                 :          60 :       tmp_ptr_expr->ts.deferred = 1;
   13213                 :          60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   13214                 :          60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   13215                 :          60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   13216                 :             :     }
   13217                 :             : 
   13218                 :         400 :   this_code = build_assignment (EXEC_ASSIGN,
   13219                 :             :                                 tmp_ptr_expr, (*code)->expr2,
   13220                 :         200 :                                 NULL, NULL, (*code)->loc);
   13221                 :         200 :   this_code->next = (*code)->next;
   13222                 :         200 :   (*code)->next = this_code;
   13223                 :         200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   13224                 :         200 :   (*code)->expr2 = (*code)->expr1;
   13225                 :         200 :   (*code)->expr1 = tmp_ptr_expr;
   13226                 :             : 
   13227                 :         200 :   return true;
   13228                 :             : }
   13229                 :             : 
   13230                 :             : 
   13231                 :             : /* Deferred character length assignments from an operator expression
   13232                 :             :    require a temporary because the character length of the lhs can
   13233                 :             :    change in the course of the assignment.  */
   13234                 :             : 
   13235                 :             : static bool
   13236                 :      276706 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   13237                 :             : {
   13238                 :      276706 :   gfc_expr *tmp_expr;
   13239                 :      276706 :   gfc_code *this_code;
   13240                 :             : 
   13241                 :      276706 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   13242                 :       25444 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   13243                 :         721 :          && (*code)->expr2->ts.type == BT_CHARACTER
   13244                 :         720 :          && (*code)->expr2->expr_type == EXPR_OP))
   13245                 :             :     return false;
   13246                 :             : 
   13247                 :          34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   13248                 :             :     return false;
   13249                 :             : 
   13250                 :          28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   13251                 :             :     return false;
   13252                 :             : 
   13253                 :          22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13254                 :          22 :   tmp_expr->where = (*code)->loc;
   13255                 :             : 
   13256                 :             :   /* A new charlen is required to ensure that the variable string
   13257                 :             :      length is different to that of the original lhs.  */
   13258                 :          22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   13259                 :          22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   13260                 :          22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   13261                 :          22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   13262                 :             : 
   13263                 :          22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   13264                 :             : 
   13265                 :          22 :   this_code = build_assignment (EXEC_ASSIGN,
   13266                 :          22 :                                 (*code)->expr1,
   13267                 :             :                                 gfc_copy_expr (tmp_expr),
   13268                 :             :                                 NULL, NULL, (*code)->loc);
   13269                 :             : 
   13270                 :          22 :   (*code)->expr1 = tmp_expr;
   13271                 :             : 
   13272                 :          22 :   this_code->next = (*code)->next;
   13273                 :          22 :   (*code)->next = this_code;
   13274                 :             : 
   13275                 :          22 :   return true;
   13276                 :             : }
   13277                 :             : 
   13278                 :             : 
   13279                 :             : static bool
   13280                 :          51 : check_team (gfc_expr *team, const char *intrinsic)
   13281                 :             : {
   13282                 :          51 :   if (team->rank != 0
   13283                 :          50 :       || team->ts.type != BT_DERIVED
   13284                 :          47 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   13285                 :          47 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   13286                 :             :     {
   13287                 :           4 :       gfc_error ("TEAM argument to %qs at %L must be a scalar expression "
   13288                 :             :                  "of type TEAM_TYPE", intrinsic, &team->where);
   13289                 :           4 :       return false;
   13290                 :             :     }
   13291                 :             : 
   13292                 :             :   return true;
   13293                 :             : }
   13294                 :             : 
   13295                 :             : 
   13296                 :             : /* Given a block of code, recursively resolve everything pointed to by this
   13297                 :             :    code block.  */
   13298                 :             : 
   13299                 :             : void
   13300                 :      646028 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   13301                 :             : {
   13302                 :      646028 :   int omp_workshare_save;
   13303                 :      646028 :   int forall_save, do_concurrent_save;
   13304                 :      646028 :   code_stack frame;
   13305                 :      646028 :   bool t;
   13306                 :             : 
   13307                 :      646028 :   frame.prev = cs_base;
   13308                 :      646028 :   frame.head = code;
   13309                 :      646028 :   cs_base = &frame;
   13310                 :             : 
   13311                 :      646028 :   find_reachable_labels (code);
   13312                 :             : 
   13313                 :     1734481 :   for (; code; code = code->next)
   13314                 :             :     {
   13315                 :     1088454 :       frame.current = code;
   13316                 :     1088454 :       forall_save = forall_flag;
   13317                 :     1088454 :       do_concurrent_save = gfc_do_concurrent_flag;
   13318                 :             : 
   13319                 :     1088454 :       if (code->op == EXEC_FORALL)
   13320                 :             :         {
   13321                 :        1990 :           forall_flag = 1;
   13322                 :        1990 :           gfc_resolve_forall (code, ns, forall_save);
   13323                 :        1990 :           forall_flag = 2;
   13324                 :             :         }
   13325                 :     1086464 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   13326                 :         109 :         for (gfc_omp_variant *variant
   13327                 :             :                = code->ext.omp_variants;
   13328                 :         359 :              variant; variant = variant->next)
   13329                 :         250 :           gfc_resolve_code (variant->code, ns);
   13330                 :     1086355 :       else if (code->block)
   13331                 :             :         {
   13332                 :      315497 :           omp_workshare_save = -1;
   13333                 :      315497 :           switch (code->op)
   13334                 :             :             {
   13335                 :       10095 :             case EXEC_OACC_PARALLEL_LOOP:
   13336                 :       10095 :             case EXEC_OACC_PARALLEL:
   13337                 :       10095 :             case EXEC_OACC_KERNELS_LOOP:
   13338                 :       10095 :             case EXEC_OACC_KERNELS:
   13339                 :       10095 :             case EXEC_OACC_SERIAL_LOOP:
   13340                 :       10095 :             case EXEC_OACC_SERIAL:
   13341                 :       10095 :             case EXEC_OACC_DATA:
   13342                 :       10095 :             case EXEC_OACC_HOST_DATA:
   13343                 :       10095 :             case EXEC_OACC_LOOP:
   13344                 :       10095 :               gfc_resolve_oacc_blocks (code, ns);
   13345                 :       10095 :               break;
   13346                 :          54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   13347                 :          54 :               omp_workshare_save = omp_workshare_flag;
   13348                 :          54 :               omp_workshare_flag = 1;
   13349                 :          54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   13350                 :          54 :               break;
   13351                 :        5904 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13352                 :        5904 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13353                 :        5904 :             case EXEC_OMP_MASKED_TASKLOOP:
   13354                 :        5904 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13355                 :        5904 :             case EXEC_OMP_MASTER_TASKLOOP:
   13356                 :        5904 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13357                 :        5904 :             case EXEC_OMP_PARALLEL:
   13358                 :        5904 :             case EXEC_OMP_PARALLEL_DO:
   13359                 :        5904 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   13360                 :        5904 :             case EXEC_OMP_PARALLEL_LOOP:
   13361                 :        5904 :             case EXEC_OMP_PARALLEL_MASKED:
   13362                 :        5904 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13363                 :        5904 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13364                 :        5904 :             case EXEC_OMP_PARALLEL_MASTER:
   13365                 :        5904 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13366                 :        5904 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13367                 :        5904 :             case EXEC_OMP_PARALLEL_SECTIONS:
   13368                 :        5904 :             case EXEC_OMP_TARGET_PARALLEL:
   13369                 :        5904 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   13370                 :        5904 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13371                 :        5904 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13372                 :        5904 :             case EXEC_OMP_TARGET_TEAMS:
   13373                 :        5904 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13374                 :        5904 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13375                 :        5904 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13376                 :        5904 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13377                 :        5904 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   13378                 :        5904 :             case EXEC_OMP_TASK:
   13379                 :        5904 :             case EXEC_OMP_TASKLOOP:
   13380                 :        5904 :             case EXEC_OMP_TASKLOOP_SIMD:
   13381                 :        5904 :             case EXEC_OMP_TEAMS:
   13382                 :        5904 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   13383                 :        5904 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13384                 :        5904 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13385                 :        5904 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13386                 :        5904 :             case EXEC_OMP_TEAMS_LOOP:
   13387                 :        5904 :               omp_workshare_save = omp_workshare_flag;
   13388                 :        5904 :               omp_workshare_flag = 0;
   13389                 :        5904 :               gfc_resolve_omp_parallel_blocks (code, ns);
   13390                 :        5904 :               break;
   13391                 :        3039 :             case EXEC_OMP_DISTRIBUTE:
   13392                 :        3039 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   13393                 :        3039 :             case EXEC_OMP_DO:
   13394                 :        3039 :             case EXEC_OMP_DO_SIMD:
   13395                 :        3039 :             case EXEC_OMP_LOOP:
   13396                 :        3039 :             case EXEC_OMP_SIMD:
   13397                 :        3039 :             case EXEC_OMP_TARGET_SIMD:
   13398                 :        3039 :             case EXEC_OMP_TILE:
   13399                 :        3039 :             case EXEC_OMP_UNROLL:
   13400                 :        3039 :               gfc_resolve_omp_do_blocks (code, ns);
   13401                 :        3039 :               break;
   13402                 :             :             case EXEC_SELECT_TYPE:
   13403                 :             :             case EXEC_SELECT_RANK:
   13404                 :             :               /* Blocks are handled in resolve_select_type/rank because we
   13405                 :             :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   13406                 :             :               break;
   13407                 :         174 :             case EXEC_DO_CONCURRENT:
   13408                 :         174 :               gfc_do_concurrent_flag = 1;
   13409                 :         174 :               gfc_resolve_blocks (code->block, ns);
   13410                 :         174 :               gfc_do_concurrent_flag = 2;
   13411                 :         174 :               break;
   13412                 :          39 :             case EXEC_OMP_WORKSHARE:
   13413                 :          39 :               omp_workshare_save = omp_workshare_flag;
   13414                 :          39 :               omp_workshare_flag = 1;
   13415                 :             :               /* FALL THROUGH */
   13416                 :      292369 :             default:
   13417                 :      292369 :               gfc_resolve_blocks (code->block, ns);
   13418                 :      292369 :               break;
   13419                 :             :             }
   13420                 :             : 
   13421                 :      311635 :           if (omp_workshare_save != -1)
   13422                 :        5997 :             omp_workshare_flag = omp_workshare_save;
   13423                 :             :         }
   13424                 :      770858 : start:
   13425                 :     1088659 :       t = true;
   13426                 :     1088659 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   13427                 :     1087277 :           t = gfc_resolve_expr (code->expr1);
   13428                 :             : 
   13429                 :     1088659 :       forall_flag = forall_save;
   13430                 :     1088659 :       gfc_do_concurrent_flag = do_concurrent_save;
   13431                 :             : 
   13432                 :     1088659 :       if (!gfc_resolve_expr (code->expr2))
   13433                 :         558 :         t = false;
   13434                 :             : 
   13435                 :     1088659 :       if (code->op == EXEC_ALLOCATE
   13436                 :     1088659 :           && !gfc_resolve_expr (code->expr3))
   13437                 :             :         t = false;
   13438                 :             : 
   13439                 :     1088659 :       switch (code->op)
   13440                 :             :         {
   13441                 :             :         case EXEC_NOP:
   13442                 :             :         case EXEC_END_BLOCK:
   13443                 :             :         case EXEC_END_NESTED_BLOCK:
   13444                 :             :         case EXEC_CYCLE:
   13445                 :             :         case EXEC_PAUSE:
   13446                 :             :           break;
   13447                 :             : 
   13448                 :      206665 :         case EXEC_STOP:
   13449                 :      206665 :         case EXEC_ERROR_STOP:
   13450                 :      206665 :           if (code->expr2 != NULL
   13451                 :          37 :               && (code->expr2->ts.type != BT_LOGICAL
   13452                 :          37 :                   || code->expr2->rank != 0))
   13453                 :           0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   13454                 :             :                        &code->expr2->where);
   13455                 :             :           break;
   13456                 :             : 
   13457                 :             :         case EXEC_EXIT:
   13458                 :             :         case EXEC_CONTINUE:
   13459                 :             :         case EXEC_DT_END:
   13460                 :             :         case EXEC_ASSIGN_CALL:
   13461                 :             :           break;
   13462                 :             : 
   13463                 :          35 :         case EXEC_CRITICAL:
   13464                 :          35 :           resolve_critical (code);
   13465                 :          35 :           break;
   13466                 :             : 
   13467                 :         747 :         case EXEC_SYNC_ALL:
   13468                 :         747 :         case EXEC_SYNC_IMAGES:
   13469                 :         747 :         case EXEC_SYNC_MEMORY:
   13470                 :         747 :           resolve_sync (code);
   13471                 :         747 :           break;
   13472                 :             : 
   13473                 :         136 :         case EXEC_LOCK:
   13474                 :         136 :         case EXEC_UNLOCK:
   13475                 :         136 :         case EXEC_EVENT_POST:
   13476                 :         136 :         case EXEC_EVENT_WAIT:
   13477                 :         136 :           resolve_lock_unlock_event (code);
   13478                 :         136 :           break;
   13479                 :             : 
   13480                 :             :         case EXEC_FAIL_IMAGE:
   13481                 :             :           break;
   13482                 :             : 
   13483                 :          30 :         case EXEC_FORM_TEAM:
   13484                 :          30 :           if (code->expr1 != NULL
   13485                 :          30 :               && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   13486                 :           2 :             gfc_error ("TEAM NUMBER argument to FORM TEAM at %L must be "
   13487                 :             :                        "a scalar INTEGER", &code->expr1->where);
   13488                 :          30 :           check_team (code->expr2, "FORM TEAM");
   13489                 :          30 :           break;
   13490                 :             : 
   13491                 :          20 :         case EXEC_CHANGE_TEAM:
   13492                 :          20 :           check_team (code->expr1, "CHANGE TEAM");
   13493                 :          20 :           break;
   13494                 :             : 
   13495                 :             :         case EXEC_END_TEAM:
   13496                 :             :           break;
   13497                 :             : 
   13498                 :           1 :         case EXEC_SYNC_TEAM:
   13499                 :           1 :           check_team (code->expr1, "SYNC TEAM");
   13500                 :           1 :           break;
   13501                 :             : 
   13502                 :        1420 :         case EXEC_ENTRY:
   13503                 :             :           /* Keep track of which entry we are up to.  */
   13504                 :        1420 :           current_entry_id = code->ext.entry->id;
   13505                 :        1420 :           break;
   13506                 :             : 
   13507                 :         450 :         case EXEC_WHERE:
   13508                 :         450 :           resolve_where (code, NULL);
   13509                 :         450 :           break;
   13510                 :             : 
   13511                 :        1248 :         case EXEC_GOTO:
   13512                 :        1248 :           if (code->expr1 != NULL)
   13513                 :             :             {
   13514                 :          78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   13515                 :          76 :                   || code->expr1->ts.type != BT_INTEGER
   13516                 :          76 :                   || (code->expr1->ref
   13517                 :           1 :                       && code->expr1->ref->type == REF_ARRAY)
   13518                 :          75 :                   || code->expr1->symtree == NULL
   13519                 :          75 :                   || (code->expr1->symtree->n.sym
   13520                 :          75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   13521                 :          75 :                           == FL_PARAMETER)))
   13522                 :           4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   13523                 :             :                            "scalar INTEGER variable", &code->expr1->where);
   13524                 :          74 :               else if (code->expr1->symtree->n.sym
   13525                 :          74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   13526                 :           1 :                 gfc_error ("Variable %qs has not been assigned a target "
   13527                 :             :                            "label at %L", code->expr1->symtree->n.sym->name,
   13528                 :             :                            &code->expr1->where);
   13529                 :             :             }
   13530                 :             :           else
   13531                 :        1170 :             resolve_branch (code->label1, code);
   13532                 :             :           break;
   13533                 :             : 
   13534                 :        3096 :         case EXEC_RETURN:
   13535                 :        3096 :           if (code->expr1 != NULL
   13536                 :          53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   13537                 :           1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   13538                 :             :                        "INTEGER return specifier", &code->expr1->where);
   13539                 :             :           break;
   13540                 :             : 
   13541                 :             :         case EXEC_INIT_ASSIGN:
   13542                 :             :         case EXEC_END_PROCEDURE:
   13543                 :             :           break;
   13544                 :             : 
   13545                 :      278671 :         case EXEC_ASSIGN:
   13546                 :      278671 :           if (!t)
   13547                 :             :             break;
   13548                 :             : 
   13549                 :      278079 :           if (flag_coarray == GFC_FCOARRAY_LIB
   13550                 :      278079 :               && gfc_is_coindexed (code->expr1))
   13551                 :             :             {
   13552                 :             :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   13553                 :             :                  coindexed variable.  */
   13554                 :         378 :               code->op = EXEC_CALL;
   13555                 :         378 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   13556                 :             :                                 true);
   13557                 :         378 :               code->resolved_sym = code->symtree->n.sym;
   13558                 :         378 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   13559                 :         378 :               code->resolved_sym->attr.intrinsic = 1;
   13560                 :         378 :               code->resolved_sym->attr.subroutine = 1;
   13561                 :         378 :               code->resolved_isym
   13562                 :         378 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   13563                 :         378 :               gfc_commit_symbol (code->resolved_sym);
   13564                 :         378 :               code->ext.actual = gfc_get_actual_arglist ();
   13565                 :         378 :               code->ext.actual->expr = code->expr1;
   13566                 :         378 :               code->ext.actual->next = gfc_get_actual_arglist ();
   13567                 :         378 :               code->ext.actual->next->expr = code->expr2;
   13568                 :             : 
   13569                 :         378 :               code->expr1 = NULL;
   13570                 :         378 :               code->expr2 = NULL;
   13571                 :         378 :               break;
   13572                 :             :             }
   13573                 :             : 
   13574                 :      277701 :           if (code->expr1->ts.type == BT_CLASS)
   13575                 :         970 :             gfc_find_vtab (&code->expr2->ts);
   13576                 :             : 
   13577                 :             :           /* If this is a pointer function in an lvalue variable context,
   13578                 :             :              the new code will have to be resolved afresh. This is also the
   13579                 :             :              case with an error, where the code is transformed into NOP to
   13580                 :             :              prevent ICEs downstream.  */
   13581                 :      277701 :           if (resolve_ptr_fcn_assign (&code, ns)
   13582                 :      277701 :               || code->op == EXEC_NOP)
   13583                 :         205 :             goto start;
   13584                 :             : 
   13585                 :      277496 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   13586                 :      277496 :                                          _("assignment")))
   13587                 :             :             break;
   13588                 :             : 
   13589                 :      277458 :           if (resolve_ordinary_assign (code, ns))
   13590                 :             :             {
   13591                 :         752 :               if (omp_workshare_flag)
   13592                 :             :                 {
   13593                 :           1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   13594                 :           1 :                              "at %L", &code->loc);
   13595                 :           1 :                   break;
   13596                 :             :                 }
   13597                 :         751 :               if (code->op == EXEC_COMPCALL)
   13598                 :         416 :                 goto compcall;
   13599                 :             :               else
   13600                 :         335 :                 goto call;
   13601                 :             :             }
   13602                 :             : 
   13603                 :             :           /* Check for dependencies in deferred character length array
   13604                 :             :              assignments and generate a temporary, if necessary.  */
   13605                 :      276706 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   13606                 :             :             break;
   13607                 :             : 
   13608                 :             :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   13609                 :      276684 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   13610                 :        6473 :               && code->expr1->ts.u.derived
   13611                 :        6473 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   13612                 :         188 :             generate_component_assignments (&code, ns);
   13613                 :      276496 :           else if (code->op == EXEC_ASSIGN)
   13614                 :             :             {
   13615                 :      276496 :               if (gfc_may_be_finalized (code->expr1->ts))
   13616                 :        1078 :                 code->expr1->must_finalize = 1;
   13617                 :      276496 :               if (code->expr2->expr_type == EXPR_ARRAY
   13618                 :      276496 :                   && gfc_may_be_finalized (code->expr2->ts))
   13619                 :          43 :                 code->expr2->must_finalize = 1;
   13620                 :             :             }
   13621                 :             : 
   13622                 :             :           break;
   13623                 :             : 
   13624                 :         126 :         case EXEC_LABEL_ASSIGN:
   13625                 :         126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   13626                 :           0 :             gfc_error ("Label %d referenced at %L is never defined",
   13627                 :             :                        code->label1->value, &code->label1->where);
   13628                 :         126 :           if (t
   13629                 :         126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   13630                 :         126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   13631                 :         126 :                   || code->expr1->symtree->n.sym->ts.kind
   13632                 :         126 :                      != gfc_default_integer_kind
   13633                 :         126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   13634                 :         125 :                   || code->expr1->symtree->n.sym->as != NULL))
   13635                 :           2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   13636                 :             :                        "default INTEGER variable", &code->expr1->where);
   13637                 :             :           break;
   13638                 :             : 
   13639                 :       10142 :         case EXEC_POINTER_ASSIGN:
   13640                 :       10142 :           {
   13641                 :       10142 :             gfc_expr* e;
   13642                 :             : 
   13643                 :       10142 :             if (!t)
   13644                 :             :               break;
   13645                 :             : 
   13646                 :             :             /* This is both a variable definition and pointer assignment
   13647                 :             :                context, so check both of them.  For rank remapping, a final
   13648                 :             :                array ref may be present on the LHS and fool gfc_expr_attr
   13649                 :             :                used in gfc_check_vardef_context.  Remove it.  */
   13650                 :       10137 :             e = remove_last_array_ref (code->expr1);
   13651                 :       20274 :             t = gfc_check_vardef_context (e, true, false, false,
   13652                 :       10137 :                                           _("pointer assignment"));
   13653                 :       10137 :             if (t)
   13654                 :       10116 :               t = gfc_check_vardef_context (e, false, false, false,
   13655                 :       10116 :                                             _("pointer assignment"));
   13656                 :       10137 :             gfc_free_expr (e);
   13657                 :             : 
   13658                 :     1098456 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   13659                 :             : 
   13660                 :       10003 :             if (!t)
   13661                 :             :               break;
   13662                 :             : 
   13663                 :             :             /* Assigning a class object always is a regular assign.  */
   13664                 :       10003 :             if (code->expr2->ts.type == BT_CLASS
   13665                 :         535 :                 && code->expr1->ts.type == BT_CLASS
   13666                 :         450 :                 && CLASS_DATA (code->expr2)
   13667                 :         449 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   13668                 :       10580 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   13669                 :          42 :                      && code->expr2->expr_type == EXPR_VARIABLE
   13670                 :          36 :                      && code->expr2->symtree->n.sym->attr.flavor
   13671                 :          36 :                         == FL_PROCEDURE))
   13672                 :         312 :               code->op = EXEC_ASSIGN;
   13673                 :             :             break;
   13674                 :             :           }
   13675                 :             : 
   13676                 :          72 :         case EXEC_ARITHMETIC_IF:
   13677                 :          72 :           {
   13678                 :          72 :             gfc_expr *e = code->expr1;
   13679                 :             : 
   13680                 :          72 :             gfc_resolve_expr (e);
   13681                 :          72 :             if (e->expr_type == EXPR_NULL)
   13682                 :           1 :               gfc_error ("Invalid NULL at %L", &e->where);
   13683                 :             : 
   13684                 :          72 :             if (t && (e->rank > 0
   13685                 :          68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   13686                 :           5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   13687                 :             :                          "REAL or INTEGER expression", &e->where);
   13688                 :             : 
   13689                 :          72 :             resolve_branch (code->label1, code);
   13690                 :          72 :             resolve_branch (code->label2, code);
   13691                 :          72 :             resolve_branch (code->label3, code);
   13692                 :             :           }
   13693                 :          72 :           break;
   13694                 :             : 
   13695                 :      219470 :         case EXEC_IF:
   13696                 :      219470 :           if (t && code->expr1 != NULL
   13697                 :           0 :               && (code->expr1->ts.type != BT_LOGICAL
   13698                 :           0 :                   || code->expr1->rank != 0))
   13699                 :           0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   13700                 :             :                        &code->expr1->where);
   13701                 :             :           break;
   13702                 :             : 
   13703                 :       76875 :         case EXEC_CALL:
   13704                 :       76875 :         call:
   13705                 :       76875 :           resolve_call (code);
   13706                 :       76875 :           break;
   13707                 :             : 
   13708                 :        1675 :         case EXEC_COMPCALL:
   13709                 :        1675 :         compcall:
   13710                 :        1675 :           resolve_typebound_subroutine (code);
   13711                 :        1675 :           break;
   13712                 :             : 
   13713                 :         123 :         case EXEC_CALL_PPC:
   13714                 :         123 :           resolve_ppc_call (code);
   13715                 :         123 :           break;
   13716                 :             : 
   13717                 :         683 :         case EXEC_SELECT:
   13718                 :             :           /* Select is complicated. Also, a SELECT construct could be
   13719                 :             :              a transformed computed GOTO.  */
   13720                 :         683 :           resolve_select (code, false);
   13721                 :         683 :           break;
   13722                 :             : 
   13723                 :        2869 :         case EXEC_SELECT_TYPE:
   13724                 :        2869 :           resolve_select_type (code, ns);
   13725                 :        2869 :           break;
   13726                 :             : 
   13727                 :        1018 :         case EXEC_SELECT_RANK:
   13728                 :        1018 :           resolve_select_rank (code, ns);
   13729                 :        1018 :           break;
   13730                 :             : 
   13731                 :        7497 :         case EXEC_BLOCK:
   13732                 :        7497 :           resolve_block_construct (code);
   13733                 :        7497 :           break;
   13734                 :             : 
   13735                 :       32224 :         case EXEC_DO:
   13736                 :       32224 :           if (code->ext.iterator != NULL)
   13737                 :             :             {
   13738                 :       32224 :               gfc_iterator *iter = code->ext.iterator;
   13739                 :       32224 :               if (gfc_resolve_iterator (iter, true, false))
   13740                 :       32210 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   13741                 :             :                                          true);
   13742                 :             :             }
   13743                 :             :           break;
   13744                 :             : 
   13745                 :         524 :         case EXEC_DO_WHILE:
   13746                 :         524 :           if (code->expr1 == NULL)
   13747                 :           0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   13748                 :             :                                 "DO WHILE");
   13749                 :         524 :           if (t
   13750                 :         524 :               && (code->expr1->rank != 0
   13751                 :         524 :                   || code->expr1->ts.type != BT_LOGICAL))
   13752                 :           0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   13753                 :             :                        "a scalar LOGICAL expression", &code->expr1->where);
   13754                 :             :           break;
   13755                 :             : 
   13756                 :       13533 :         case EXEC_ALLOCATE:
   13757                 :       13533 :           if (t)
   13758                 :       13531 :             resolve_allocate_deallocate (code, "ALLOCATE");
   13759                 :             : 
   13760                 :             :           break;
   13761                 :             : 
   13762                 :        5657 :         case EXEC_DEALLOCATE:
   13763                 :        5657 :           if (t)
   13764                 :        5657 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   13765                 :             : 
   13766                 :             :           break;
   13767                 :             : 
   13768                 :        3873 :         case EXEC_OPEN:
   13769                 :        3873 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   13770                 :             :             break;
   13771                 :             : 
   13772                 :        3646 :           resolve_branch (code->ext.open->err, code);
   13773                 :        3646 :           break;
   13774                 :             : 
   13775                 :        3061 :         case EXEC_CLOSE:
   13776                 :        3061 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   13777                 :             :             break;
   13778                 :             : 
   13779                 :        3027 :           resolve_branch (code->ext.close->err, code);
   13780                 :        3027 :           break;
   13781                 :             : 
   13782                 :        2773 :         case EXEC_BACKSPACE:
   13783                 :        2773 :         case EXEC_ENDFILE:
   13784                 :        2773 :         case EXEC_REWIND:
   13785                 :        2773 :         case EXEC_FLUSH:
   13786                 :        2773 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   13787                 :             :             break;
   13788                 :             : 
   13789                 :        2707 :           resolve_branch (code->ext.filepos->err, code);
   13790                 :        2707 :           break;
   13791                 :             : 
   13792                 :         817 :         case EXEC_INQUIRE:
   13793                 :         817 :           if (!gfc_resolve_inquire (code->ext.inquire))
   13794                 :             :               break;
   13795                 :             : 
   13796                 :         769 :           resolve_branch (code->ext.inquire->err, code);
   13797                 :         769 :           break;
   13798                 :             : 
   13799                 :          92 :         case EXEC_IOLENGTH:
   13800                 :          92 :           gcc_assert (code->ext.inquire != NULL);
   13801                 :          92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   13802                 :             :             break;
   13803                 :             : 
   13804                 :          90 :           resolve_branch (code->ext.inquire->err, code);
   13805                 :          90 :           break;
   13806                 :             : 
   13807                 :          89 :         case EXEC_WAIT:
   13808                 :          89 :           if (!gfc_resolve_wait (code->ext.wait))
   13809                 :             :             break;
   13810                 :             : 
   13811                 :          74 :           resolve_branch (code->ext.wait->err, code);
   13812                 :          74 :           resolve_branch (code->ext.wait->end, code);
   13813                 :          74 :           resolve_branch (code->ext.wait->eor, code);
   13814                 :          74 :           break;
   13815                 :             : 
   13816                 :       31335 :         case EXEC_READ:
   13817                 :       31335 :         case EXEC_WRITE:
   13818                 :       31335 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   13819                 :             :             break;
   13820                 :             : 
   13821                 :       31026 :           resolve_branch (code->ext.dt->err, code);
   13822                 :       31026 :           resolve_branch (code->ext.dt->end, code);
   13823                 :       31026 :           resolve_branch (code->ext.dt->eor, code);
   13824                 :       31026 :           break;
   13825                 :             : 
   13826                 :       44829 :         case EXEC_TRANSFER:
   13827                 :       44829 :           resolve_transfer (code);
   13828                 :       44829 :           break;
   13829                 :             : 
   13830                 :        2164 :         case EXEC_DO_CONCURRENT:
   13831                 :        2164 :         case EXEC_FORALL:
   13832                 :        2164 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   13833                 :             : 
   13834                 :        2164 :           if (code->expr1 != NULL
   13835                 :         730 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   13836                 :           2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   13837                 :             :                        "expression", &code->expr1->where);
   13838                 :             : 
   13839                 :        2164 :     if (code->op == EXEC_DO_CONCURRENT)
   13840                 :         174 :       resolve_locality_spec (code, ns);
   13841                 :             :           break;
   13842                 :             : 
   13843                 :       13088 :         case EXEC_OACC_PARALLEL_LOOP:
   13844                 :       13088 :         case EXEC_OACC_PARALLEL:
   13845                 :       13088 :         case EXEC_OACC_KERNELS_LOOP:
   13846                 :       13088 :         case EXEC_OACC_KERNELS:
   13847                 :       13088 :         case EXEC_OACC_SERIAL_LOOP:
   13848                 :       13088 :         case EXEC_OACC_SERIAL:
   13849                 :       13088 :         case EXEC_OACC_DATA:
   13850                 :       13088 :         case EXEC_OACC_HOST_DATA:
   13851                 :       13088 :         case EXEC_OACC_LOOP:
   13852                 :       13088 :         case EXEC_OACC_UPDATE:
   13853                 :       13088 :         case EXEC_OACC_WAIT:
   13854                 :       13088 :         case EXEC_OACC_CACHE:
   13855                 :       13088 :         case EXEC_OACC_ENTER_DATA:
   13856                 :       13088 :         case EXEC_OACC_EXIT_DATA:
   13857                 :       13088 :         case EXEC_OACC_ATOMIC:
   13858                 :       13088 :         case EXEC_OACC_DECLARE:
   13859                 :       13088 :           gfc_resolve_oacc_directive (code, ns);
   13860                 :       13088 :           break;
   13861                 :             : 
   13862                 :       16433 :         case EXEC_OMP_ALLOCATE:
   13863                 :       16433 :         case EXEC_OMP_ALLOCATORS:
   13864                 :       16433 :         case EXEC_OMP_ASSUME:
   13865                 :       16433 :         case EXEC_OMP_ATOMIC:
   13866                 :       16433 :         case EXEC_OMP_BARRIER:
   13867                 :       16433 :         case EXEC_OMP_CANCEL:
   13868                 :       16433 :         case EXEC_OMP_CANCELLATION_POINT:
   13869                 :       16433 :         case EXEC_OMP_CRITICAL:
   13870                 :       16433 :         case EXEC_OMP_FLUSH:
   13871                 :       16433 :         case EXEC_OMP_DEPOBJ:
   13872                 :       16433 :         case EXEC_OMP_DISPATCH:
   13873                 :       16433 :         case EXEC_OMP_DISTRIBUTE:
   13874                 :       16433 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13875                 :       16433 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13876                 :       16433 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13877                 :       16433 :         case EXEC_OMP_DO:
   13878                 :       16433 :         case EXEC_OMP_DO_SIMD:
   13879                 :       16433 :         case EXEC_OMP_ERROR:
   13880                 :       16433 :         case EXEC_OMP_INTEROP:
   13881                 :       16433 :         case EXEC_OMP_LOOP:
   13882                 :       16433 :         case EXEC_OMP_MASTER:
   13883                 :       16433 :         case EXEC_OMP_MASTER_TASKLOOP:
   13884                 :       16433 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13885                 :       16433 :         case EXEC_OMP_MASKED:
   13886                 :       16433 :         case EXEC_OMP_MASKED_TASKLOOP:
   13887                 :       16433 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13888                 :       16433 :         case EXEC_OMP_METADIRECTIVE:
   13889                 :       16433 :         case EXEC_OMP_ORDERED:
   13890                 :       16433 :         case EXEC_OMP_SCAN:
   13891                 :       16433 :         case EXEC_OMP_SCOPE:
   13892                 :       16433 :         case EXEC_OMP_SECTIONS:
   13893                 :       16433 :         case EXEC_OMP_SIMD:
   13894                 :       16433 :         case EXEC_OMP_SINGLE:
   13895                 :       16433 :         case EXEC_OMP_TARGET:
   13896                 :       16433 :         case EXEC_OMP_TARGET_DATA:
   13897                 :       16433 :         case EXEC_OMP_TARGET_ENTER_DATA:
   13898                 :       16433 :         case EXEC_OMP_TARGET_EXIT_DATA:
   13899                 :       16433 :         case EXEC_OMP_TARGET_PARALLEL:
   13900                 :       16433 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13901                 :       16433 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13902                 :       16433 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13903                 :       16433 :         case EXEC_OMP_TARGET_SIMD:
   13904                 :       16433 :         case EXEC_OMP_TARGET_TEAMS:
   13905                 :       16433 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13906                 :       16433 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13907                 :       16433 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13908                 :       16433 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13909                 :       16433 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13910                 :       16433 :         case EXEC_OMP_TARGET_UPDATE:
   13911                 :       16433 :         case EXEC_OMP_TASK:
   13912                 :       16433 :         case EXEC_OMP_TASKGROUP:
   13913                 :       16433 :         case EXEC_OMP_TASKLOOP:
   13914                 :       16433 :         case EXEC_OMP_TASKLOOP_SIMD:
   13915                 :       16433 :         case EXEC_OMP_TASKWAIT:
   13916                 :       16433 :         case EXEC_OMP_TASKYIELD:
   13917                 :       16433 :         case EXEC_OMP_TEAMS:
   13918                 :       16433 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13919                 :       16433 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13920                 :       16433 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13921                 :       16433 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13922                 :       16433 :         case EXEC_OMP_TEAMS_LOOP:
   13923                 :       16433 :         case EXEC_OMP_TILE:
   13924                 :       16433 :         case EXEC_OMP_UNROLL:
   13925                 :       16433 :         case EXEC_OMP_WORKSHARE:
   13926                 :       16433 :           gfc_resolve_omp_directive (code, ns);
   13927                 :       16433 :           break;
   13928                 :             : 
   13929                 :        3832 :         case EXEC_OMP_PARALLEL:
   13930                 :        3832 :         case EXEC_OMP_PARALLEL_DO:
   13931                 :        3832 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13932                 :        3832 :         case EXEC_OMP_PARALLEL_LOOP:
   13933                 :        3832 :         case EXEC_OMP_PARALLEL_MASKED:
   13934                 :        3832 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13935                 :        3832 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13936                 :        3832 :         case EXEC_OMP_PARALLEL_MASTER:
   13937                 :        3832 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13938                 :        3832 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13939                 :        3832 :         case EXEC_OMP_PARALLEL_SECTIONS:
   13940                 :        3832 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13941                 :        3832 :           omp_workshare_save = omp_workshare_flag;
   13942                 :        3832 :           omp_workshare_flag = 0;
   13943                 :        3832 :           gfc_resolve_omp_directive (code, ns);
   13944                 :        3832 :           omp_workshare_flag = omp_workshare_save;
   13945                 :        3832 :           break;
   13946                 :             : 
   13947                 :           0 :         default:
   13948                 :           0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   13949                 :             :         }
   13950                 :             :     }
   13951                 :             : 
   13952                 :      646027 :   cs_base = frame.prev;
   13953                 :      646027 : }
   13954                 :             : 
   13955                 :             : 
   13956                 :             : /* Resolve initial values and make sure they are compatible with
   13957                 :             :    the variable.  */
   13958                 :             : 
   13959                 :             : static void
   13960                 :     1747268 : resolve_values (gfc_symbol *sym)
   13961                 :             : {
   13962                 :     1747268 :   bool t;
   13963                 :             : 
   13964                 :     1747268 :   if (sym->value == NULL)
   13965                 :             :     return;
   13966                 :             : 
   13967                 :      398198 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   13968                 :           4 :     gfc_warning (OPT_Wdeprecated_declarations,
   13969                 :             :                  "Using parameter %qs declared at %L is deprecated",
   13970                 :             :                  sym->name, &sym->declared_at);
   13971                 :             : 
   13972                 :      398198 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   13973                 :       37604 :     t= resolve_structure_cons (sym->value, 1);
   13974                 :             :   else
   13975                 :      360594 :     t = gfc_resolve_expr (sym->value);
   13976                 :             : 
   13977                 :      398198 :   if (!t)
   13978                 :             :     return;
   13979                 :             : 
   13980                 :      398196 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   13981                 :             : }
   13982                 :             : 
   13983                 :             : 
   13984                 :             : /* Verify any BIND(C) derived types in the namespace so we can report errors
   13985                 :             :    for them once, rather than for each variable declared of that type.  */
   13986                 :             : 
   13987                 :             : static void
   13988                 :     1720420 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   13989                 :             : {
   13990                 :     1720420 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   13991                 :       76512 :       && derived_sym->attr.is_bind_c == 1)
   13992                 :       24190 :     verify_bind_c_derived_type (derived_sym);
   13993                 :             : 
   13994                 :     1720420 :   return;
   13995                 :             : }
   13996                 :             : 
   13997                 :             : 
   13998                 :             : /* Check the interfaces of DTIO procedures associated with derived
   13999                 :             :    type 'sym'.  These procedures can either have typebound bindings or
   14000                 :             :    can appear in DTIO generic interfaces.  */
   14001                 :             : 
   14002                 :             : static void
   14003                 :     1748236 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   14004                 :             : {
   14005                 :     1748236 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   14006                 :             :     return;
   14007                 :             : 
   14008                 :       85094 :   gfc_check_dtio_interfaces (sym);
   14009                 :             : 
   14010                 :       85094 :   return;
   14011                 :             : }
   14012                 :             : 
   14013                 :             : /* Verify that any binding labels used in a given namespace do not collide
   14014                 :             :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   14015                 :             :    for the same procedure are permitted.  Abstract interfaces and dummy
   14016                 :             :    arguments are not checked.  */
   14017                 :             : 
   14018                 :             : static void
   14019                 :     1748236 : gfc_verify_binding_labels (gfc_symbol *sym)
   14020                 :             : {
   14021                 :     1748236 :   gfc_gsymbol *gsym;
   14022                 :     1748236 :   const char *module;
   14023                 :             : 
   14024                 :     1748236 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   14025                 :       53269 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   14026                 :       28165 :       || sym->attr.abstract || sym->attr.dummy)
   14027                 :             :     return;
   14028                 :             : 
   14029                 :       28067 :   gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   14030                 :             : 
   14031                 :       28067 :   if (sym->module)
   14032                 :             :     module = sym->module;
   14033                 :       10581 :   else if (sym->ns && sym->ns->proc_name
   14034                 :       10581 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   14035                 :        4405 :     module = sym->ns->proc_name->name;
   14036                 :        6176 :   else if (sym->ns && sym->ns->parent
   14037                 :         358 :            && sym->ns && sym->ns->parent->proc_name
   14038                 :         358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   14039                 :         272 :     module = sym->ns->parent->proc_name->name;
   14040                 :             :   else
   14041                 :             :     module = NULL;
   14042                 :             : 
   14043                 :       28067 :   if (!gsym
   14044                 :       10011 :       || (!gsym->defined
   14045                 :        7185 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   14046                 :             :     {
   14047                 :       18056 :       if (!gsym)
   14048                 :       18056 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   14049                 :       25241 :       gsym->where = sym->declared_at;
   14050                 :       25241 :       gsym->sym_name = sym->name;
   14051                 :       25241 :       gsym->binding_label = sym->binding_label;
   14052                 :       25241 :       gsym->ns = sym->ns;
   14053                 :       25241 :       gsym->mod_name = module;
   14054                 :       25241 :       if (sym->attr.function)
   14055                 :       17675 :         gsym->type = GSYM_FUNCTION;
   14056                 :        7566 :       else if (sym->attr.subroutine)
   14057                 :        7427 :         gsym->type = GSYM_SUBROUTINE;
   14058                 :             :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   14059                 :       25241 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   14060                 :       25241 :       return;
   14061                 :             :     }
   14062                 :             : 
   14063                 :        2826 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   14064                 :             :     {
   14065                 :           1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   14066                 :             :                  "identifier as entity at %L", sym->name,
   14067                 :             :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14068                 :             :       /* Clear the binding label to prevent checking multiple times.  */
   14069                 :           1 :       sym->binding_label = NULL;
   14070                 :           1 :       return;
   14071                 :             :     }
   14072                 :             : 
   14073                 :        2825 :   if (sym->attr.flavor == FL_VARIABLE && module
   14074                 :          37 :       && (strcmp (module, gsym->mod_name) != 0
   14075                 :          35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   14076                 :             :     {
   14077                 :             :       /* This can only happen if the variable is defined in a module - if it
   14078                 :             :          isn't the same module, reject it.  */
   14079                 :           3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   14080                 :             :                  "uses the same global identifier as entity at %L from module %qs",
   14081                 :             :                  sym->name, module, sym->binding_label,
   14082                 :             :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   14083                 :           3 :       sym->binding_label = NULL;
   14084                 :           3 :       return;
   14085                 :             :     }
   14086                 :             : 
   14087                 :        2822 :   if ((sym->attr.function || sym->attr.subroutine)
   14088                 :        2786 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   14089                 :        2784 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   14090                 :        2471 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   14091                 :        2083 :       && (module != gsym->mod_name
   14092                 :        2079 :           || strcmp (gsym->sym_name, sym->name) != 0
   14093                 :        2079 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   14094                 :             :     {
   14095                 :             :       /* Print an error if the procedure is defined multiple times; we have to
   14096                 :             :          exclude references to the same procedure via module association or
   14097                 :             :          multiple checks for the same procedure.  */
   14098                 :           4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   14099                 :             :                  "global identifier as entity at %L", sym->name,
   14100                 :             :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14101                 :           4 :       sym->binding_label = NULL;
   14102                 :             :     }
   14103                 :             : }
   14104                 :             : 
   14105                 :             : 
   14106                 :             : /* Resolve an index expression.  */
   14107                 :             : 
   14108                 :             : static bool
   14109                 :      256666 : resolve_index_expr (gfc_expr *e)
   14110                 :             : {
   14111                 :      256666 :   if (!gfc_resolve_expr (e))
   14112                 :             :     return false;
   14113                 :             : 
   14114                 :      256656 :   if (!gfc_simplify_expr (e, 0))
   14115                 :             :     return false;
   14116                 :             : 
   14117                 :      256654 :   if (!gfc_specification_expr (e))
   14118                 :             :     return false;
   14119                 :             : 
   14120                 :             :   return true;
   14121                 :             : }
   14122                 :             : 
   14123                 :             : 
   14124                 :             : /* Resolve a charlen structure.  */
   14125                 :             : 
   14126                 :             : static bool
   14127                 :       99284 : resolve_charlen (gfc_charlen *cl)
   14128                 :             : {
   14129                 :       99284 :   int k;
   14130                 :       99284 :   bool saved_specification_expr;
   14131                 :             : 
   14132                 :       99284 :   if (cl->resolved)
   14133                 :             :     return true;
   14134                 :             : 
   14135                 :       91214 :   cl->resolved = 1;
   14136                 :       91214 :   saved_specification_expr = specification_expr;
   14137                 :       91214 :   specification_expr = true;
   14138                 :             : 
   14139                 :       91214 :   if (cl->length_from_typespec)
   14140                 :             :     {
   14141                 :        1066 :       if (!gfc_resolve_expr (cl->length))
   14142                 :             :         {
   14143                 :           1 :           specification_expr = saved_specification_expr;
   14144                 :           1 :           return false;
   14145                 :             :         }
   14146                 :             : 
   14147                 :        1065 :       if (!gfc_simplify_expr (cl->length, 0))
   14148                 :             :         {
   14149                 :           0 :           specification_expr = saved_specification_expr;
   14150                 :           0 :           return false;
   14151                 :             :         }
   14152                 :             : 
   14153                 :             :       /* cl->length has been resolved.  It should have an integer type.  */
   14154                 :        1065 :       if (cl->length
   14155                 :        1064 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   14156                 :             :         {
   14157                 :           4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   14158                 :             :                      &cl->length->where);
   14159                 :           4 :           return false;
   14160                 :             :         }
   14161                 :             :     }
   14162                 :             :   else
   14163                 :             :     {
   14164                 :       90148 :       if (!resolve_index_expr (cl->length))
   14165                 :             :         {
   14166                 :          19 :           specification_expr = saved_specification_expr;
   14167                 :          19 :           return false;
   14168                 :             :         }
   14169                 :             :     }
   14170                 :             : 
   14171                 :             :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   14172                 :             :      a negative value, the length of character entities declared is zero.  */
   14173                 :       91190 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   14174                 :       54454 :       && mpz_sgn (cl->length->value.integer) < 0)
   14175                 :           0 :     gfc_replace_expr (cl->length,
   14176                 :             :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   14177                 :             : 
   14178                 :             :   /* Check that the character length is not too large.  */
   14179                 :       91190 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   14180                 :       91190 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   14181                 :       54454 :       && cl->length->ts.type == BT_INTEGER
   14182                 :       54454 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   14183                 :             :     {
   14184                 :           4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   14185                 :           4 :       specification_expr = saved_specification_expr;
   14186                 :           4 :       return false;
   14187                 :             :     }
   14188                 :             : 
   14189                 :       91186 :   specification_expr = saved_specification_expr;
   14190                 :       91186 :   return true;
   14191                 :             : }
   14192                 :             : 
   14193                 :             : 
   14194                 :             : /* Test for non-constant shape arrays.  */
   14195                 :             : 
   14196                 :             : static bool
   14197                 :      113613 : is_non_constant_shape_array (gfc_symbol *sym)
   14198                 :             : {
   14199                 :      113613 :   gfc_expr *e;
   14200                 :      113613 :   int i;
   14201                 :      113613 :   bool not_constant;
   14202                 :             : 
   14203                 :      113613 :   not_constant = false;
   14204                 :      113613 :   if (sym->as != NULL)
   14205                 :             :     {
   14206                 :             :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   14207                 :             :          has not been simplified; parameter array references.  Do the
   14208                 :             :          simplification now.  */
   14209                 :      150048 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   14210                 :             :         {
   14211                 :       86500 :           if (i == GFC_MAX_DIMENSIONS)
   14212                 :             :             break;
   14213                 :             : 
   14214                 :       86498 :           e = sym->as->lower[i];
   14215                 :       86498 :           if (e && (!resolve_index_expr(e)
   14216                 :       83745 :                     || !gfc_is_constant_expr (e)))
   14217                 :             :             not_constant = true;
   14218                 :       86498 :           e = sym->as->upper[i];
   14219                 :       86498 :           if (e && (!resolve_index_expr(e)
   14220                 :       82746 :                     || !gfc_is_constant_expr (e)))
   14221                 :             :             not_constant = true;
   14222                 :             :         }
   14223                 :             :     }
   14224                 :      113613 :   return not_constant;
   14225                 :             : }
   14226                 :             : 
   14227                 :             : /* Given a symbol and an initialization expression, add code to initialize
   14228                 :             :    the symbol to the function entry.  */
   14229                 :             : static void
   14230                 :        1464 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   14231                 :             : {
   14232                 :        1464 :   gfc_expr *lval;
   14233                 :        1464 :   gfc_code *init_st;
   14234                 :        1464 :   gfc_namespace *ns = sym->ns;
   14235                 :             : 
   14236                 :             :   /* Search for the function namespace if this is a contained
   14237                 :             :      function without an explicit result.  */
   14238                 :        1464 :   if (sym->attr.function && sym == sym->result
   14239                 :         143 :       && sym->name != sym->ns->proc_name->name)
   14240                 :             :     {
   14241                 :         142 :       ns = ns->contained;
   14242                 :         610 :       for (;ns; ns = ns->sibling)
   14243                 :         556 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   14244                 :             :           break;
   14245                 :             :     }
   14246                 :             : 
   14247                 :        1464 :   if (ns == NULL)
   14248                 :             :     {
   14249                 :          54 :       gfc_free_expr (init);
   14250                 :          54 :       return;
   14251                 :             :     }
   14252                 :             : 
   14253                 :             :   /* Build an l-value expression for the result.  */
   14254                 :        1410 :   lval = gfc_lval_expr_from_sym (sym);
   14255                 :             : 
   14256                 :             :   /* Add the code at scope entry.  */
   14257                 :        1410 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   14258                 :        1410 :   init_st->next = ns->code;
   14259                 :        1410 :   ns->code = init_st;
   14260                 :             : 
   14261                 :             :   /* Assign the default initializer to the l-value.  */
   14262                 :        1410 :   init_st->loc = sym->declared_at;
   14263                 :        1410 :   init_st->expr1 = lval;
   14264                 :        1410 :   init_st->expr2 = init;
   14265                 :             : }
   14266                 :             : 
   14267                 :             : 
   14268                 :             : /* Whether or not we can generate a default initializer for a symbol.  */
   14269                 :             : 
   14270                 :             : static bool
   14271                 :       27817 : can_generate_init (gfc_symbol *sym)
   14272                 :             : {
   14273                 :       27817 :   symbol_attribute *a;
   14274                 :       27817 :   if (!sym)
   14275                 :             :     return false;
   14276                 :       27817 :   a = &sym->attr;
   14277                 :             : 
   14278                 :             :   /* These symbols should never have a default initialization.  */
   14279                 :       45463 :   return !(
   14280                 :             :        a->allocatable
   14281                 :             :     || a->external
   14282                 :       27817 :     || a->pointer
   14283                 :       26794 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   14284                 :        5451 :         && (CLASS_DATA (sym)->attr.class_pointer
   14285                 :        5451 :             || CLASS_DATA (sym)->attr.proc_pointer))
   14286                 :             :     || a->in_equivalence
   14287                 :             :     || a->in_common
   14288                 :       24913 :     || a->data
   14289                 :       24569 :     || sym->module
   14290                 :             :     || a->cray_pointee
   14291                 :       20860 :     || a->cray_pointer
   14292                 :       20798 :     || sym->assoc
   14293                 :       18262 :     || (!a->referenced && !a->result)
   14294                 :       17646 :     || (a->dummy && (a->intent != INTENT_OUT
   14295                 :        1049 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   14296                 :       17646 :     || (a->function && sym != sym->result)
   14297                 :             :   );
   14298                 :             : }
   14299                 :             : 
   14300                 :             : 
   14301                 :             : /* Assign the default initializer to a derived type variable or result.  */
   14302                 :             : 
   14303                 :             : static void
   14304                 :       10122 : apply_default_init (gfc_symbol *sym)
   14305                 :             : {
   14306                 :       10122 :   gfc_expr *init = NULL;
   14307                 :             : 
   14308                 :       10122 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   14309                 :             :     return;
   14310                 :             : 
   14311                 :       10122 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   14312                 :        9315 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   14313                 :             : 
   14314                 :       10122 :   if (init == NULL && sym->ts.type != BT_CLASS)
   14315                 :             :     return;
   14316                 :             : 
   14317                 :        1082 :   build_init_assign (sym, init);
   14318                 :        1082 :   sym->attr.referenced = 1;
   14319                 :             : }
   14320                 :             : 
   14321                 :             : 
   14322                 :             : /* Build an initializer for a local. Returns null if the symbol should not have
   14323                 :             :    a default initialization.  */
   14324                 :             : 
   14325                 :             : static gfc_expr *
   14326                 :      197518 : build_default_init_expr (gfc_symbol *sym)
   14327                 :             : {
   14328                 :             :   /* These symbols should never have a default initialization.  */
   14329                 :      197518 :   if (sym->attr.allocatable
   14330                 :             :       || sym->attr.external
   14331                 :             :       || sym->attr.dummy
   14332                 :             :       || sym->attr.pointer
   14333                 :             :       || sym->attr.in_equivalence
   14334                 :             :       || sym->attr.in_common
   14335                 :      197518 :       || sym->attr.data
   14336                 :      105464 :       || sym->module
   14337                 :             :       || sym->attr.cray_pointee
   14338                 :      103145 :       || sym->attr.cray_pointer
   14339                 :      102542 :       || sym->assoc)
   14340                 :             :     return NULL;
   14341                 :             : 
   14342                 :             :   /* Get the appropriate init expression.  */
   14343                 :       98149 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   14344                 :             : }
   14345                 :             : 
   14346                 :             : /* Add an initialization expression to a local variable.  */
   14347                 :             : static void
   14348                 :      197518 : apply_default_init_local (gfc_symbol *sym)
   14349                 :             : {
   14350                 :      197518 :   gfc_expr *init = NULL;
   14351                 :             : 
   14352                 :             :   /* The symbol should be a variable or a function return value.  */
   14353                 :      197518 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   14354                 :      197518 :       || (sym->attr.function && sym->result != sym))
   14355                 :             :     return;
   14356                 :             : 
   14357                 :             :   /* Try to build the initializer expression.  If we can't initialize
   14358                 :             :      this symbol, then init will be NULL.  */
   14359                 :      197518 :   init = build_default_init_expr (sym);
   14360                 :      197518 :   if (init == NULL)
   14361                 :             :     return;
   14362                 :             : 
   14363                 :             :   /* For saved variables, we don't want to add an initializer at function
   14364                 :             :      entry, so we just add a static initializer. Note that automatic variables
   14365                 :             :      are stack allocated even with -fno-automatic; we have also to exclude
   14366                 :             :      result variable, which are also nonstatic.  */
   14367                 :         419 :   if (!sym->attr.automatic
   14368                 :         419 :       && (sym->attr.save || sym->ns->save_all
   14369                 :         377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   14370                 :          27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   14371                 :          14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   14372                 :             :     {
   14373                 :             :       /* Don't clobber an existing initializer!  */
   14374                 :          37 :       gcc_assert (sym->value == NULL);
   14375                 :          37 :       sym->value = init;
   14376                 :          37 :       return;
   14377                 :             :     }
   14378                 :             : 
   14379                 :         382 :   build_init_assign (sym, init);
   14380                 :             : }
   14381                 :             : 
   14382                 :             : 
   14383                 :             : /* Resolution of common features of flavors variable and procedure.  */
   14384                 :             : 
   14385                 :             : static bool
   14386                 :      919558 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   14387                 :             : {
   14388                 :      919558 :   gfc_array_spec *as;
   14389                 :             : 
   14390                 :      919558 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   14391                 :       18299 :       && sym->ts.u.derived && CLASS_DATA (sym))
   14392                 :       18293 :     as = CLASS_DATA (sym)->as;
   14393                 :             :   else
   14394                 :      901265 :     as = sym->as;
   14395                 :             : 
   14396                 :             :   /* Constraints on deferred shape variable.  */
   14397                 :      919558 :   if (as == NULL || as->type != AS_DEFERRED)
   14398                 :             :     {
   14399                 :      896121 :       bool pointer, allocatable, dimension;
   14400                 :             : 
   14401                 :      896121 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   14402                 :       15228 :           && sym->ts.u.derived && CLASS_DATA (sym))
   14403                 :             :         {
   14404                 :       15222 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   14405                 :       15222 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   14406                 :       15222 :           dimension = CLASS_DATA (sym)->attr.dimension;
   14407                 :             :         }
   14408                 :             :       else
   14409                 :             :         {
   14410                 :      880899 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   14411                 :      880899 :           allocatable = sym->attr.allocatable;
   14412                 :      880899 :           dimension = sym->attr.dimension;
   14413                 :             :         }
   14414                 :             : 
   14415                 :      896121 :       if (allocatable)
   14416                 :             :         {
   14417                 :        7768 :           if (dimension
   14418                 :        7768 :               && as
   14419                 :         523 :               && as->type != AS_ASSUMED_RANK
   14420                 :           5 :               && !sym->attr.select_rank_temporary)
   14421                 :             :             {
   14422                 :           3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   14423                 :             :                          "shape or assumed rank", sym->name, &sym->declared_at);
   14424                 :           3 :               return false;
   14425                 :             :             }
   14426                 :        7765 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   14427                 :             :                                     "%qs at %L may not be ALLOCATABLE",
   14428                 :             :                                     sym->name, &sym->declared_at))
   14429                 :             :             return false;
   14430                 :             :         }
   14431                 :             : 
   14432                 :      896117 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   14433                 :             :         {
   14434                 :           4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   14435                 :             :                      "assumed rank", sym->name, &sym->declared_at);
   14436                 :           4 :           sym->error = 1;
   14437                 :           4 :           return false;
   14438                 :             :         }
   14439                 :             :     }
   14440                 :             :   else
   14441                 :             :     {
   14442                 :       23437 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   14443                 :        4477 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   14444                 :             :         {
   14445                 :           3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   14446                 :             :                      sym->name, &sym->declared_at);
   14447                 :           3 :           return false;
   14448                 :             :          }
   14449                 :             :     }
   14450                 :             : 
   14451                 :             :   /* Constraints on polymorphic variables.  */
   14452                 :      919547 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   14453                 :             :     {
   14454                 :             :       /* F03:C502.  */
   14455                 :       17665 :       if (sym->attr.class_ok
   14456                 :       17609 :           && sym->ts.u.derived
   14457                 :       17604 :           && !sym->attr.select_type_temporary
   14458                 :       16566 :           && !UNLIMITED_POLY (sym)
   14459                 :       14279 :           && CLASS_DATA (sym)
   14460                 :       14278 :           && CLASS_DATA (sym)->ts.u.derived
   14461                 :       31942 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   14462                 :             :         {
   14463                 :           5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   14464                 :           5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   14465                 :             :                      &sym->declared_at);
   14466                 :           5 :           return false;
   14467                 :             :         }
   14468                 :             : 
   14469                 :             :       /* F03:C509.  */
   14470                 :             :       /* Assume that use associated symbols were checked in the module ns.
   14471                 :             :          Class-variables that are associate-names are also something special
   14472                 :             :          and excepted from the test.  */
   14473                 :       17660 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   14474                 :             :           && !sym->attr.select_type_temporary
   14475                 :          54 :           && !sym->attr.select_rank_temporary)
   14476                 :             :         {
   14477                 :          54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   14478                 :             :                      "or pointer", sym->name, &sym->declared_at);
   14479                 :          54 :           return false;
   14480                 :             :         }
   14481                 :             :     }
   14482                 :             : 
   14483                 :             :   return true;
   14484                 :             : }
   14485                 :             : 
   14486                 :             : 
   14487                 :             : /* Additional checks for symbols with flavor variable and derived
   14488                 :             :    type.  To be called from resolve_fl_variable.  */
   14489                 :             : 
   14490                 :             : static bool
   14491                 :       75509 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   14492                 :             : {
   14493                 :       75509 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   14494                 :             : 
   14495                 :             :   /* Check to see if a derived type is blocked from being host
   14496                 :             :      associated by the presence of another class I symbol in the same
   14497                 :             :      namespace.  14.6.1.3 of the standard and the discussion on
   14498                 :             :      comp.lang.fortran.  */
   14499                 :       75509 :   if (sym->ts.u.derived
   14500                 :       75504 :       && sym->ns != sym->ts.u.derived->ns
   14501                 :       44860 :       && !sym->ts.u.derived->attr.use_assoc
   14502                 :       16293 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   14503                 :             :     {
   14504                 :       15421 :       gfc_symbol *s;
   14505                 :       15421 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   14506                 :       15421 :       if (s && s->attr.generic)
   14507                 :           0 :         s = gfc_find_dt_in_generic (s);
   14508                 :       15421 :       if (s && !gfc_fl_struct (s->attr.flavor))
   14509                 :             :         {
   14510                 :           2 :           gfc_error ("The type %qs cannot be host associated at %L "
   14511                 :             :                      "because it is blocked by an incompatible object "
   14512                 :             :                      "of the same name declared at %L",
   14513                 :           2 :                      sym->ts.u.derived->name, &sym->declared_at,
   14514                 :             :                      &s->declared_at);
   14515                 :           2 :           return false;
   14516                 :             :         }
   14517                 :             :     }
   14518                 :             : 
   14519                 :             :   /* 4th constraint in section 11.3: "If an object of a type for which
   14520                 :             :      component-initialization is specified (R429) appears in the
   14521                 :             :      specification-part of a module and does not have the ALLOCATABLE
   14522                 :             :      or POINTER attribute, the object shall have the SAVE attribute."
   14523                 :             : 
   14524                 :             :      The check for initializers is performed with
   14525                 :             :      gfc_has_default_initializer because gfc_default_initializer generates
   14526                 :             :      a hidden default for allocatable components.  */
   14527                 :       74920 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   14528                 :       17272 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   14529                 :         348 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   14530                 :         346 :       && !sym->attr.pointer && !sym->attr.allocatable
   14531                 :          20 :       && gfc_has_default_initializer (sym->ts.u.derived)
   14532                 :       75516 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   14533                 :             :                           "%qs at %L, needed due to the default "
   14534                 :             :                           "initialization", sym->name, &sym->declared_at))
   14535                 :             :     return false;
   14536                 :             : 
   14537                 :             :   /* Assign default initializer.  */
   14538                 :       75505 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   14539                 :       69592 :       && (!no_init_flag
   14540                 :       54089 :           || (sym->attr.intent == INTENT_OUT
   14541                 :        3173 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   14542                 :       18502 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   14543                 :             : 
   14544                 :             :   return true;
   14545                 :             : }
   14546                 :             : 
   14547                 :             : 
   14548                 :             : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   14549                 :             :    except in the declaration of an entity or component that has the POINTER
   14550                 :             :    or ALLOCATABLE attribute.  */
   14551                 :             : 
   14552                 :             : static bool
   14553                 :     1432812 : deferred_requirements (gfc_symbol *sym)
   14554                 :             : {
   14555                 :     1432812 :   if (sym->ts.deferred
   14556                 :          92 :       && !(sym->attr.pointer
   14557                 :        7520 :            || sym->attr.allocatable
   14558                 :             :            || sym->attr.associate_var
   14559                 :             :            || sym->attr.omp_udr_artificial_var))
   14560                 :             :     {
   14561                 :             :       /* If a function has a result variable, only check the variable.  */
   14562                 :           7 :       if (sym->result && sym->name != sym->result->name)
   14563                 :             :         return true;
   14564                 :             : 
   14565                 :           6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   14566                 :             :                  "requires either the POINTER or ALLOCATABLE attribute",
   14567                 :             :                  sym->name, &sym->declared_at);
   14568                 :           6 :       return false;
   14569                 :             :     }
   14570                 :             :   return true;
   14571                 :             : }
   14572                 :             : 
   14573                 :             : 
   14574                 :             : /* Resolve symbols with flavor variable.  */
   14575                 :             : 
   14576                 :             : static bool
   14577                 :      613004 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   14578                 :             : {
   14579                 :      613004 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   14580                 :             :                                  "SAVE attribute");
   14581                 :             : 
   14582                 :      613004 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   14583                 :             :     return false;
   14584                 :             : 
   14585                 :             :   /* Set this flag to check that variables are parameters of all entries.
   14586                 :             :      This check is effected by the call to gfc_resolve_expr through
   14587                 :             :      is_non_constant_shape_array.  */
   14588                 :      612944 :   bool saved_specification_expr = specification_expr;
   14589                 :      612944 :   specification_expr = true;
   14590                 :             : 
   14591                 :      612944 :   if (sym->ns->proc_name
   14592                 :      612857 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   14593                 :      608111 :           || sym->ns->proc_name->attr.is_main_program)
   14594                 :             :       && !sym->attr.use_assoc
   14595                 :             :       && !sym->attr.allocatable
   14596                 :       79412 :       && !sym->attr.pointer
   14597                 :      680451 :       && is_non_constant_shape_array (sym))
   14598                 :             :     {
   14599                 :             :       /* F08:C541. The shape of an array defined in a main program or module
   14600                 :             :        * needs to be constant.  */
   14601                 :           3 :       gfc_error ("The module or main program array %qs at %L must "
   14602                 :             :                  "have constant shape", sym->name, &sym->declared_at);
   14603                 :           3 :       specification_expr = saved_specification_expr;
   14604                 :           3 :       return false;
   14605                 :             :     }
   14606                 :             : 
   14607                 :             :   /* Constraints on deferred type parameter.  */
   14608                 :      612941 :   if (!deferred_requirements (sym))
   14609                 :             :     return false;
   14610                 :             : 
   14611                 :      612937 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   14612                 :             :     {
   14613                 :             :       /* Make sure that character string variables with assumed length are
   14614                 :             :          dummy arguments.  */
   14615                 :       34836 :       gfc_expr *e = NULL;
   14616                 :             : 
   14617                 :       34836 :       if (sym->ts.u.cl)
   14618                 :       34836 :         e = sym->ts.u.cl->length;
   14619                 :             :       else
   14620                 :             :         return false;
   14621                 :             : 
   14622                 :       34836 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   14623                 :        2420 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   14624                 :         907 :           && !sym->attr.omp_udr_artificial_var)
   14625                 :             :         {
   14626                 :           2 :           gfc_error ("Entity with assumed character length at %L must be a "
   14627                 :             :                      "dummy argument or a PARAMETER", &sym->declared_at);
   14628                 :           2 :           specification_expr = saved_specification_expr;
   14629                 :           2 :           return false;
   14630                 :             :         }
   14631                 :             : 
   14632                 :       20199 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   14633                 :             :         {
   14634                 :           1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   14635                 :           1 :           specification_expr = saved_specification_expr;
   14636                 :           1 :           return false;
   14637                 :             :         }
   14638                 :             : 
   14639                 :       34833 :       if (!gfc_is_constant_expr (e)
   14640                 :       34833 :           && !(e->expr_type == EXPR_VARIABLE
   14641                 :        1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   14642                 :             :         {
   14643                 :        2182 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   14644                 :        1678 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   14645                 :        1677 :                   || sym->ns->proc_name->attr.is_main_program))
   14646                 :             :             {
   14647                 :           3 :               gfc_error ("%qs at %L must have constant character length "
   14648                 :             :                         "in this context", sym->name, &sym->declared_at);
   14649                 :           3 :               specification_expr = saved_specification_expr;
   14650                 :           3 :               return false;
   14651                 :             :             }
   14652                 :        2179 :           if (sym->attr.in_common)
   14653                 :             :             {
   14654                 :           1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   14655                 :             :                          "character length", sym->name, &sym->declared_at);
   14656                 :           1 :               specification_expr = saved_specification_expr;
   14657                 :           1 :               return false;
   14658                 :             :             }
   14659                 :             :         }
   14660                 :             :     }
   14661                 :             : 
   14662                 :      612930 :   if (sym->value == NULL && sym->attr.referenced
   14663                 :      199389 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   14664                 :      197518 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   14665                 :             : 
   14666                 :             :   /* Determine if the symbol may not have an initializer.  */
   14667                 :      612930 :   int no_init_flag = 0, automatic_flag = 0;
   14668                 :      612930 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   14669                 :      612930 :       || sym->attr.intrinsic || sym->attr.result)
   14670                 :             :     no_init_flag = 1;
   14671                 :       38233 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   14672                 :      166900 :            && is_non_constant_shape_array (sym))
   14673                 :             :     {
   14674                 :        1324 :       no_init_flag = automatic_flag = 1;
   14675                 :             : 
   14676                 :             :       /* Also, they must not have the SAVE attribute.
   14677                 :             :          SAVE_IMPLICIT is checked below.  */
   14678                 :        1324 :       if (sym->as && sym->attr.codimension)
   14679                 :             :         {
   14680                 :           7 :           int corank = sym->as->corank;
   14681                 :           7 :           sym->as->corank = 0;
   14682                 :           7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   14683                 :           7 :           sym->as->corank = corank;
   14684                 :             :         }
   14685                 :        1324 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   14686                 :             :         {
   14687                 :           2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   14688                 :           2 :           specification_expr = saved_specification_expr;
   14689                 :           2 :           return false;
   14690                 :             :         }
   14691                 :             :     }
   14692                 :             : 
   14693                 :             :   /* Ensure that any initializer is simplified.  */
   14694                 :      612928 :   if (sym->value)
   14695                 :        7352 :     gfc_simplify_expr (sym->value, 1);
   14696                 :             : 
   14697                 :             :   /* Reject illegal initializers.  */
   14698                 :      612928 :   if (!sym->mark && sym->value)
   14699                 :             :     {
   14700                 :        7352 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   14701                 :          67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   14702                 :           1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   14703                 :             :                    sym->name, &sym->declared_at);
   14704                 :        7351 :       else if (sym->attr.external)
   14705                 :           0 :         gfc_error ("External %qs at %L cannot have an initializer",
   14706                 :             :                    sym->name, &sym->declared_at);
   14707                 :        7351 :       else if (sym->attr.dummy)
   14708                 :           3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   14709                 :             :                    sym->name, &sym->declared_at);
   14710                 :        7348 :       else if (sym->attr.intrinsic)
   14711                 :           0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   14712                 :             :                    sym->name, &sym->declared_at);
   14713                 :        7348 :       else if (sym->attr.result)
   14714                 :           1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   14715                 :             :                    sym->name, &sym->declared_at);
   14716                 :        7347 :       else if (automatic_flag)
   14717                 :           5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   14718                 :             :                    sym->name, &sym->declared_at);
   14719                 :             :       else
   14720                 :        7342 :         goto no_init_error;
   14721                 :          10 :       specification_expr = saved_specification_expr;
   14722                 :          10 :       return false;
   14723                 :             :     }
   14724                 :             : 
   14725                 :      605576 : no_init_error:
   14726                 :      612918 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   14727                 :             :     {
   14728                 :       75509 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   14729                 :       75509 :       specification_expr = saved_specification_expr;
   14730                 :       75509 :       return res;
   14731                 :             :     }
   14732                 :             : 
   14733                 :      537409 :   specification_expr = saved_specification_expr;
   14734                 :      537409 :   return true;
   14735                 :             : }
   14736                 :             : 
   14737                 :             : 
   14738                 :             : /* Compare the dummy characteristics of a module procedure interface
   14739                 :             :    declaration with the corresponding declaration in a submodule.  */
   14740                 :             : static gfc_formal_arglist *new_formal;
   14741                 :             : static char errmsg[200];
   14742                 :             : 
   14743                 :             : static void
   14744                 :        1086 : compare_fsyms (gfc_symbol *sym)
   14745                 :             : {
   14746                 :        1086 :   gfc_symbol *fsym;
   14747                 :             : 
   14748                 :        1086 :   if (sym == NULL || new_formal == NULL)
   14749                 :             :     return;
   14750                 :             : 
   14751                 :        1086 :   fsym = new_formal->sym;
   14752                 :             : 
   14753                 :        1086 :   if (sym == fsym)
   14754                 :             :     return;
   14755                 :             : 
   14756                 :        1062 :   if (strcmp (sym->name, fsym->name) == 0)
   14757                 :             :     {
   14758                 :         397 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   14759                 :           2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   14760                 :             :     }
   14761                 :             : }
   14762                 :             : 
   14763                 :             : 
   14764                 :             : /* Resolve a procedure.  */
   14765                 :             : 
   14766                 :             : static bool
   14767                 :      452390 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   14768                 :             : {
   14769                 :      452390 :   gfc_formal_arglist *arg;
   14770                 :      452390 :   bool allocatable_or_pointer = false;
   14771                 :             : 
   14772                 :      452390 :   if (sym->attr.function
   14773                 :      452390 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   14774                 :             :     return false;
   14775                 :             : 
   14776                 :             :   /* Constraints on deferred type parameter.  */
   14777                 :      452380 :   if (!deferred_requirements (sym))
   14778                 :             :     return false;
   14779                 :             : 
   14780                 :      452379 :   if (sym->ts.type == BT_CHARACTER)
   14781                 :             :     {
   14782                 :       11186 :       gfc_charlen *cl = sym->ts.u.cl;
   14783                 :             : 
   14784                 :        7171 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   14785                 :       12270 :              && !resolve_charlen (cl))
   14786                 :             :         return false;
   14787                 :             : 
   14788                 :       11185 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   14789                 :       10102 :           && sym->attr.proc == PROC_ST_FUNCTION)
   14790                 :             :         {
   14791                 :           0 :           gfc_error ("Character-valued statement function %qs at %L must "
   14792                 :             :                      "have constant length", sym->name, &sym->declared_at);
   14793                 :           0 :           return false;
   14794                 :             :         }
   14795                 :             :     }
   14796                 :             : 
   14797                 :             :   /* Ensure that derived type for are not of a private type.  Internal
   14798                 :             :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   14799                 :             :      externally accessible and can access all the objects accessible in
   14800                 :             :      the host.  */
   14801                 :      105942 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   14802                 :      105942 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   14803                 :      535668 :       && gfc_check_symbol_access (sym))
   14804                 :             :     {
   14805                 :      423084 :       gfc_interface *iface;
   14806                 :             : 
   14807                 :      881983 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   14808                 :             :         {
   14809                 :      458900 :           if (arg->sym
   14810                 :      458759 :               && arg->sym->ts.type == BT_DERIVED
   14811                 :       38835 :               && arg->sym->ts.u.derived
   14812                 :       38835 :               && !arg->sym->ts.u.derived->attr.use_assoc
   14813                 :        4140 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   14814                 :      458909 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   14815                 :             :                                   "and cannot be a dummy argument"
   14816                 :             :                                   " of %qs, which is PUBLIC at %L",
   14817                 :           9 :                                   arg->sym->name, sym->name,
   14818                 :             :                                   &sym->declared_at))
   14819                 :             :             {
   14820                 :             :               /* Stop this message from recurring.  */
   14821                 :           1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   14822                 :           1 :               return false;
   14823                 :             :             }
   14824                 :             :         }
   14825                 :             : 
   14826                 :             :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   14827                 :             :          PRIVATE to the containing module.  */
   14828                 :      604161 :       for (iface = sym->generic; iface; iface = iface->next)
   14829                 :             :         {
   14830                 :      421517 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   14831                 :             :             {
   14832                 :      240439 :               if (arg->sym
   14833                 :      240407 :                   && arg->sym->ts.type == BT_DERIVED
   14834                 :        7868 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   14835                 :         208 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   14836                 :      240443 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   14837                 :             :                                       "PUBLIC interface %qs at %L "
   14838                 :             :                                       "takes dummy arguments of %qs which "
   14839                 :             :                                       "is PRIVATE", iface->sym->name,
   14840                 :           4 :                                       sym->name, &iface->sym->declared_at,
   14841                 :           4 :                                       gfc_typename(&arg->sym->ts)))
   14842                 :             :                 {
   14843                 :             :                   /* Stop this message from recurring.  */
   14844                 :           1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   14845                 :           1 :                   return false;
   14846                 :             :                 }
   14847                 :             :              }
   14848                 :             :         }
   14849                 :             :     }
   14850                 :             : 
   14851                 :      452376 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   14852                 :          67 :       && !sym->attr.proc_pointer)
   14853                 :             :     {
   14854                 :           2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   14855                 :             :                  sym->name, &sym->declared_at);
   14856                 :             : 
   14857                 :             :       /* Make sure no second error is issued for this.  */
   14858                 :           2 :       sym->value->error = 1;
   14859                 :           2 :       return false;
   14860                 :             :     }
   14861                 :             : 
   14862                 :             :   /* An external symbol may not have an initializer because it is taken to be
   14863                 :             :      a procedure. Exception: Procedure Pointers.  */
   14864                 :      452374 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   14865                 :             :     {
   14866                 :           0 :       gfc_error ("External object %qs at %L may not have an initializer",
   14867                 :             :                  sym->name, &sym->declared_at);
   14868                 :           0 :       return false;
   14869                 :             :     }
   14870                 :             : 
   14871                 :             :   /* An elemental function is required to return a scalar 12.7.1  */
   14872                 :      452374 :   if (sym->attr.elemental && sym->attr.function
   14873                 :       86017 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   14874                 :           2 :                       && CLASS_DATA (sym)->as)))
   14875                 :             :     {
   14876                 :           3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   14877                 :             :                  "result", sym->name, &sym->declared_at);
   14878                 :             :       /* Reset so that the error only occurs once.  */
   14879                 :           3 :       sym->attr.elemental = 0;
   14880                 :           3 :       return false;
   14881                 :             :     }
   14882                 :             : 
   14883                 :      452371 :   if (sym->attr.proc == PROC_ST_FUNCTION
   14884                 :         220 :       && (sym->attr.allocatable || sym->attr.pointer))
   14885                 :             :     {
   14886                 :           2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   14887                 :             :                  "allocatable attribute", sym->name, &sym->declared_at);
   14888                 :           2 :       return false;
   14889                 :             :     }
   14890                 :             : 
   14891                 :             :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   14892                 :             :      char-len-param shall not be array-valued, pointer-valued, recursive
   14893                 :             :      or pure.  ....snip... A character value of * may only be used in the
   14894                 :             :      following ways: (i) Dummy arg of procedure - dummy associates with
   14895                 :             :      actual length; (ii) To declare a named constant; or (iii) External
   14896                 :             :      function - but length must be declared in calling scoping unit.  */
   14897                 :      452369 :   if (sym->attr.function
   14898                 :      306535 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   14899                 :        6387 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   14900                 :             :     {
   14901                 :         178 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   14902                 :         177 :           || (sym->attr.recursive) || (sym->attr.pure))
   14903                 :             :         {
   14904                 :           4 :           if (sym->as && sym->as->rank)
   14905                 :           1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   14906                 :             :                        "array-valued", sym->name, &sym->declared_at);
   14907                 :             : 
   14908                 :           4 :           if (sym->attr.pointer)
   14909                 :           1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   14910                 :             :                        "pointer-valued", sym->name, &sym->declared_at);
   14911                 :             : 
   14912                 :           4 :           if (sym->attr.pure)
   14913                 :           1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   14914                 :             :                        "pure", sym->name, &sym->declared_at);
   14915                 :             : 
   14916                 :           4 :           if (sym->attr.recursive)
   14917                 :           1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   14918                 :             :                        "recursive", sym->name, &sym->declared_at);
   14919                 :             : 
   14920                 :           4 :           return false;
   14921                 :             :         }
   14922                 :             : 
   14923                 :             :       /* Appendix B.2 of the standard.  Contained functions give an
   14924                 :             :          error anyway.  Deferred character length is an F2003 feature.
   14925                 :             :          Don't warn on intrinsic conversion functions, which start
   14926                 :             :          with two underscores.  */
   14927                 :         174 :       if (!sym->attr.contained && !sym->ts.deferred
   14928                 :         170 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   14929                 :         170 :         gfc_notify_std (GFC_STD_F95_OBS,
   14930                 :             :                         "CHARACTER(*) function %qs at %L",
   14931                 :             :                         sym->name, &sym->declared_at);
   14932                 :             :     }
   14933                 :             : 
   14934                 :             :   /* F2008, C1218.  */
   14935                 :      452365 :   if (sym->attr.elemental)
   14936                 :             :     {
   14937                 :       89181 :       if (sym->attr.proc_pointer)
   14938                 :             :         {
   14939                 :           7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   14940                 :             :                                                : sym->name);
   14941                 :           7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   14942                 :             :                      name, &sym->declared_at);
   14943                 :           7 :           return false;
   14944                 :             :         }
   14945                 :       89174 :       if (sym->attr.dummy)
   14946                 :             :         {
   14947                 :           3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   14948                 :             :                      sym->name, &sym->declared_at);
   14949                 :           3 :           return false;
   14950                 :             :         }
   14951                 :             :     }
   14952                 :             : 
   14953                 :             :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   14954                 :             :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   14955                 :             :      pointer is tested and caught elsewhere.  */
   14956                 :      452355 :   if (sym->result)
   14957                 :      257445 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   14958                 :      257445 :                              && CLASS_DATA (sym->result) ?
   14959                 :             :                              (CLASS_DATA (sym->result)->attr.allocatable
   14960                 :        1569 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   14961                 :             :                              (sym->result->attr.allocatable
   14962                 :      255876 :                               || sym->result->attr.pointer);
   14963                 :             : 
   14964                 :      452355 :   if (sym->attr.elemental && sym->result
   14965                 :       85643 :       && allocatable_or_pointer)
   14966                 :             :     {
   14967                 :           4 :       gfc_error ("Function result variable %qs at %L of elemental "
   14968                 :             :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   14969                 :             :                  "attribute", sym->result->name,
   14970                 :             :                  &sym->result->declared_at, sym->name);
   14971                 :           4 :       return false;
   14972                 :             :     }
   14973                 :             : 
   14974                 :      452351 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   14975                 :             :     {
   14976                 :        5890 :       gfc_formal_arglist *curr_arg;
   14977                 :        5890 :       int has_non_interop_arg = 0;
   14978                 :             : 
   14979                 :        5890 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   14980                 :             :                               sym->common_block))
   14981                 :             :         {
   14982                 :             :           /* Clear these to prevent looking at them again if there was an
   14983                 :             :              error.  */
   14984                 :           2 :           sym->attr.is_bind_c = 0;
   14985                 :           2 :           sym->attr.is_c_interop = 0;
   14986                 :           2 :           sym->ts.is_c_interop = 0;
   14987                 :             :         }
   14988                 :             :       else
   14989                 :             :         {
   14990                 :             :           /* So far, no errors have been found.  */
   14991                 :        5888 :           sym->attr.is_c_interop = 1;
   14992                 :        5888 :           sym->ts.is_c_interop = 1;
   14993                 :             :         }
   14994                 :             : 
   14995                 :        5890 :       curr_arg = gfc_sym_get_dummy_args (sym);
   14996                 :       26478 :       while (curr_arg != NULL)
   14997                 :             :         {
   14998                 :             :           /* Skip implicitly typed dummy args here.  */
   14999                 :       14698 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   15000                 :       14642 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   15001                 :             :               /* If something is found to fail, record the fact so we
   15002                 :             :                  can mark the symbol for the procedure as not being
   15003                 :             :                  BIND(C) to try and prevent multiple errors being
   15004                 :             :                  reported.  */
   15005                 :       14698 :               has_non_interop_arg = 1;
   15006                 :             : 
   15007                 :       14698 :           curr_arg = curr_arg->next;
   15008                 :             :         }
   15009                 :             : 
   15010                 :             :       /* See if any of the arguments were not interoperable and if so, clear
   15011                 :             :          the procedure symbol to prevent duplicate error messages.  */
   15012                 :        5890 :       if (has_non_interop_arg != 0)
   15013                 :             :         {
   15014                 :         128 :           sym->attr.is_c_interop = 0;
   15015                 :         128 :           sym->ts.is_c_interop = 0;
   15016                 :         128 :           sym->attr.is_bind_c = 0;
   15017                 :             :         }
   15018                 :             :     }
   15019                 :             : 
   15020                 :      452351 :   if (!sym->attr.proc_pointer)
   15021                 :             :     {
   15022                 :      451329 :       if (sym->attr.save == SAVE_EXPLICIT)
   15023                 :             :         {
   15024                 :           5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   15025                 :             :                      "in %qs at %L", sym->name, &sym->declared_at);
   15026                 :           5 :           return false;
   15027                 :             :         }
   15028                 :      451324 :       if (sym->attr.intent)
   15029                 :             :         {
   15030                 :           1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   15031                 :             :                      "in %qs at %L", sym->name, &sym->declared_at);
   15032                 :           1 :           return false;
   15033                 :             :         }
   15034                 :      451323 :       if (sym->attr.subroutine && sym->attr.result)
   15035                 :             :         {
   15036                 :           2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   15037                 :           2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   15038                 :           2 :           return false;
   15039                 :             :         }
   15040                 :      451321 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   15041                 :      130555 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   15042                 :      130552 :               || sym->attr.contained))
   15043                 :             :         {
   15044                 :           3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   15045                 :             :                      "in %qs at %L", sym->name, &sym->declared_at);
   15046                 :           3 :           return false;
   15047                 :             :         }
   15048                 :      451318 :       if (strcmp ("ppr@", sym->name) == 0)
   15049                 :             :         {
   15050                 :           0 :           gfc_error ("Procedure pointer result %qs at %L "
   15051                 :             :                      "is missing the pointer attribute",
   15052                 :           0 :                      sym->ns->proc_name->name, &sym->declared_at);
   15053                 :           0 :           return false;
   15054                 :             :         }
   15055                 :             :     }
   15056                 :             : 
   15057                 :             :   /* Assume that a procedure whose body is not known has references
   15058                 :             :      to external arrays.  */
   15059                 :      452340 :   if (sym->attr.if_source != IFSRC_DECL)
   15060                 :      309708 :     sym->attr.array_outer_dependency = 1;
   15061                 :             : 
   15062                 :             :   /* Compare the characteristics of a module procedure with the
   15063                 :             :      interface declaration. Ideally this would be done with
   15064                 :             :      gfc_compare_interfaces but, at present, the formal interface
   15065                 :             :      cannot be copied to the ts.interface.  */
   15066                 :      452340 :   if (sym->attr.module_procedure
   15067                 :      452340 :       && sym->attr.if_source == IFSRC_DECL)
   15068                 :             :     {
   15069                 :         526 :       gfc_symbol *iface;
   15070                 :         526 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   15071                 :         526 :       char *module_name;
   15072                 :         526 :       char *submodule_name;
   15073                 :         526 :       strcpy (name, sym->ns->proc_name->name);
   15074                 :         526 :       module_name = strtok (name, ".");
   15075                 :         526 :       submodule_name = strtok (NULL, ".");
   15076                 :             : 
   15077                 :         526 :       iface = sym->tlink;
   15078                 :         526 :       sym->tlink = NULL;
   15079                 :             : 
   15080                 :             :       /* Make sure that the result uses the correct charlen for deferred
   15081                 :             :          length results.  */
   15082                 :         526 :       if (iface && sym->result
   15083                 :         116 :           && iface->ts.type == BT_CHARACTER
   15084                 :          19 :           && iface->ts.deferred)
   15085                 :           6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   15086                 :             : 
   15087                 :           6 :       if (iface == NULL)
   15088                 :         169 :         goto check_formal;
   15089                 :             : 
   15090                 :             :       /* Check the procedure characteristics.  */
   15091                 :         357 :       if (sym->attr.elemental != iface->attr.elemental)
   15092                 :             :         {
   15093                 :           1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   15094                 :             :                      "PROCEDURE at %L and its interface in %s",
   15095                 :             :                      &sym->declared_at, module_name);
   15096                 :          10 :           return false;
   15097                 :             :         }
   15098                 :             : 
   15099                 :         356 :       if (sym->attr.pure != iface->attr.pure)
   15100                 :             :         {
   15101                 :           2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   15102                 :             :                      "PROCEDURE at %L and its interface in %s",
   15103                 :             :                      &sym->declared_at, module_name);
   15104                 :           2 :           return false;
   15105                 :             :         }
   15106                 :             : 
   15107                 :         354 :       if (sym->attr.recursive != iface->attr.recursive)
   15108                 :             :         {
   15109                 :           2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   15110                 :             :                      "PROCEDURE at %L and its interface in %s",
   15111                 :             :                      &sym->declared_at, module_name);
   15112                 :           2 :           return false;
   15113                 :             :         }
   15114                 :             : 
   15115                 :             :       /* Check the result characteristics.  */
   15116                 :         352 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   15117                 :             :         {
   15118                 :           5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   15119                 :             :                      "in MODULE %qs and the declaration at %L in "
   15120                 :             :                      "(SUB)MODULE %qs",
   15121                 :             :                      errmsg, module_name, &sym->declared_at,
   15122                 :             :                      submodule_name ? submodule_name : module_name);
   15123                 :           5 :           return false;
   15124                 :             :         }
   15125                 :             : 
   15126                 :         347 : check_formal:
   15127                 :             :       /* Check the characteristics of the formal arguments.  */
   15128                 :         516 :       if (sym->formal && sym->formal_ns)
   15129                 :             :         {
   15130                 :         988 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   15131                 :             :             {
   15132                 :         571 :               new_formal = arg;
   15133                 :         571 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   15134                 :             :             }
   15135                 :             :         }
   15136                 :             :     }
   15137                 :             : 
   15138                 :             :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   15139                 :             :      BIND(C) attribute.  */
   15140                 :      452330 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   15141                 :             :     {
   15142                 :           1 :       gfc_error ("Interface of %qs at %L must be explicit",
   15143                 :             :                  sym->name, &sym->declared_at);
   15144                 :           1 :       return false;
   15145                 :             :     }
   15146                 :             : 
   15147                 :             :   return true;
   15148                 :             : }
   15149                 :             : 
   15150                 :             : 
   15151                 :             : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   15152                 :             :    been defined and we now know their defined arguments, check that they fulfill
   15153                 :             :    the requirements of the standard for procedures used as finalizers.  */
   15154                 :             : 
   15155                 :             : static bool
   15156                 :      103413 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   15157                 :             : {
   15158                 :      103413 :   gfc_finalizer* list;
   15159                 :      103413 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   15160                 :      103413 :   bool result = true;
   15161                 :      103413 :   bool seen_scalar = false;
   15162                 :      103413 :   gfc_symbol *vtab;
   15163                 :      103413 :   gfc_component *c;
   15164                 :      103413 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   15165                 :             : 
   15166                 :      103413 :   if (parent)
   15167                 :       14688 :     gfc_resolve_finalizers (parent, finalizable);
   15168                 :             : 
   15169                 :             :   /* Ensure that derived-type components have a their finalizers resolved.  */
   15170                 :      103413 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   15171                 :      327699 :   for (c = derived->components; c; c = c->next)
   15172                 :      224286 :     if (c->ts.type == BT_DERIVED
   15173                 :       63234 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   15174                 :             :       {
   15175                 :        7671 :         bool has_final2 = false;
   15176                 :        7671 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   15177                 :           0 :           return false;  /* Error.  */
   15178                 :        7671 :         has_final = has_final || has_final2;
   15179                 :             :       }
   15180                 :             :   /* Return early if not finalizable.  */
   15181                 :      103413 :   if (!has_final)
   15182                 :             :     {
   15183                 :      101161 :       if (finalizable)
   15184                 :        7629 :         *finalizable = false;
   15185                 :      101161 :       return true;
   15186                 :             :     }
   15187                 :             : 
   15188                 :             :   /* Walk over the list of finalizer-procedures, check them, and if any one
   15189                 :             :      does not fit in with the standard's definition, print an error and remove
   15190                 :             :      it from the list.  */
   15191                 :        2252 :   prev_link = &derived->f2k_derived->finalizers;
   15192                 :        4648 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   15193                 :             :     {
   15194                 :        2396 :       gfc_formal_arglist *dummy_args;
   15195                 :        2396 :       gfc_symbol* arg;
   15196                 :        2396 :       gfc_finalizer* i;
   15197                 :        2396 :       int my_rank;
   15198                 :             : 
   15199                 :             :       /* Skip this finalizer if we already resolved it.  */
   15200                 :        2396 :       if (list->proc_tree)
   15201                 :             :         {
   15202                 :        1933 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   15203                 :         566 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   15204                 :        1367 :             seen_scalar = true;
   15205                 :        1933 :           prev_link = &(list->next);
   15206                 :        1933 :           continue;
   15207                 :             :         }
   15208                 :             : 
   15209                 :             :       /* Check this exists and is a SUBROUTINE.  */
   15210                 :         463 :       if (!list->proc_sym->attr.subroutine)
   15211                 :             :         {
   15212                 :           3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   15213                 :             :                      list->proc_sym->name, &list->where);
   15214                 :           3 :           goto error;
   15215                 :             :         }
   15216                 :             : 
   15217                 :             :       /* We should have exactly one argument.  */
   15218                 :         460 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   15219                 :         460 :       if (!dummy_args || dummy_args->next)
   15220                 :             :         {
   15221                 :           2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   15222                 :             :                      &list->where);
   15223                 :           2 :           goto error;
   15224                 :             :         }
   15225                 :         458 :       arg = dummy_args->sym;
   15226                 :             : 
   15227                 :         458 :       if (!arg)
   15228                 :             :         {
   15229                 :           1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   15230                 :           1 :                      &list->proc_sym->declared_at, derived->name);
   15231                 :           1 :           goto error;
   15232                 :             :         }
   15233                 :             : 
   15234                 :         457 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   15235                 :           6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   15236                 :             :         {
   15237                 :           0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   15238                 :             :                      "be the only finalizer with the same kind/type "
   15239                 :             :                      "(F2018: C790)", &list->where);
   15240                 :           0 :           goto error;
   15241                 :             :         }
   15242                 :             : 
   15243                 :             :       /* This argument must be of our type.  */
   15244                 :         457 :       if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
   15245                 :             :         {
   15246                 :           2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   15247                 :             :                      &arg->declared_at, derived->name);
   15248                 :           2 :           goto error;
   15249                 :             :         }
   15250                 :             : 
   15251                 :             :       /* It must neither be a pointer nor allocatable nor optional.  */
   15252                 :         455 :       if (arg->attr.pointer)
   15253                 :             :         {
   15254                 :           1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   15255                 :             :                      &arg->declared_at);
   15256                 :           1 :           goto error;
   15257                 :             :         }
   15258                 :         454 :       if (arg->attr.allocatable)
   15259                 :             :         {
   15260                 :           1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   15261                 :             :                      " ALLOCATABLE", &arg->declared_at);
   15262                 :           1 :           goto error;
   15263                 :             :         }
   15264                 :         453 :       if (arg->attr.optional)
   15265                 :             :         {
   15266                 :           1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   15267                 :             :                      &arg->declared_at);
   15268                 :           1 :           goto error;
   15269                 :             :         }
   15270                 :             : 
   15271                 :             :       /* It must not be INTENT(OUT).  */
   15272                 :         452 :       if (arg->attr.intent == INTENT_OUT)
   15273                 :             :         {
   15274                 :           1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   15275                 :             :                      " INTENT(OUT)", &arg->declared_at);
   15276                 :           1 :           goto error;
   15277                 :             :         }
   15278                 :             : 
   15279                 :             :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   15280                 :         451 :       if (warn_surprising && arg->as && arg->as->rank != 0
   15281                 :           3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   15282                 :           2 :         gfc_warning (OPT_Wsurprising,
   15283                 :             :                      "Non-scalar FINAL procedure at %L should have assumed"
   15284                 :             :                      " shape argument", &arg->declared_at);
   15285                 :             : 
   15286                 :             :       /* Check that it does not match in kind and rank with a FINAL procedure
   15287                 :             :          defined earlier.  To really loop over the *earlier* declarations,
   15288                 :             :          we need to walk the tail of the list as new ones were pushed at the
   15289                 :             :          front.  */
   15290                 :             :       /* TODO: Handle kind parameters once they are implemented.  */
   15291                 :         451 :       my_rank = (arg->as ? arg->as->rank : 0);
   15292                 :         540 :       for (i = list->next; i; i = i->next)
   15293                 :             :         {
   15294                 :          91 :           gfc_formal_arglist *dummy_args;
   15295                 :             : 
   15296                 :             :           /* Argument list might be empty; that is an error signalled earlier,
   15297                 :             :              but we nevertheless continued resolving.  */
   15298                 :          91 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   15299                 :          91 :           if (dummy_args)
   15300                 :             :             {
   15301                 :          89 :               gfc_symbol* i_arg = dummy_args->sym;
   15302                 :          89 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   15303                 :          89 :               if (i_rank == my_rank)
   15304                 :             :                 {
   15305                 :           2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   15306                 :             :                              " rank (%d) as %qs",
   15307                 :           2 :                              list->proc_sym->name, &list->where, my_rank,
   15308                 :           2 :                              i->proc_sym->name);
   15309                 :           2 :                   goto error;
   15310                 :             :                 }
   15311                 :             :             }
   15312                 :             :         }
   15313                 :             : 
   15314                 :             :         /* Is this the/a scalar finalizer procedure?  */
   15315                 :         449 :         if (my_rank == 0)
   15316                 :         329 :           seen_scalar = true;
   15317                 :             : 
   15318                 :             :         /* Find the symtree for this procedure.  */
   15319                 :         449 :         gcc_assert (!list->proc_tree);
   15320                 :         449 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   15321                 :             : 
   15322                 :         449 :         prev_link = &list->next;
   15323                 :         449 :         continue;
   15324                 :             : 
   15325                 :             :         /* Remove wrong nodes immediately from the list so we don't risk any
   15326                 :             :            troubles in the future when they might fail later expectations.  */
   15327                 :          14 : error:
   15328                 :          14 :         i = list;
   15329                 :          14 :         *prev_link = list->next;
   15330                 :          14 :         gfc_free_finalizer (i);
   15331                 :          14 :         result = false;
   15332                 :         449 :     }
   15333                 :             : 
   15334                 :        2252 :   if (result == false)
   15335                 :             :     return false;
   15336                 :             : 
   15337                 :             :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   15338                 :             :      were nodes in the list, must have been for arrays.  It is surely a good
   15339                 :             :      idea to have a scalar version there if there's something to finalize.  */
   15340                 :        2248 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   15341                 :           1 :     gfc_warning (OPT_Wsurprising,
   15342                 :             :                  "Only array FINAL procedures declared for derived type %qs"
   15343                 :             :                  " defined at %L, suggest also scalar one unless an assumed"
   15344                 :             :                  " rank finalizer has been declared",
   15345                 :             :                  derived->name, &derived->declared_at);
   15346                 :             : 
   15347                 :        2248 :   vtab = gfc_find_derived_vtab (derived);
   15348                 :        2248 :   c = vtab->ts.u.derived->components->next->next->next->next->next;
   15349                 :        2248 :   gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   15350                 :             : 
   15351                 :        2248 :   if (finalizable)
   15352                 :         590 :     *finalizable = true;
   15353                 :             : 
   15354                 :             :   return true;
   15355                 :             : }
   15356                 :             : 
   15357                 :             : 
   15358                 :             : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   15359                 :             : 
   15360                 :             : static bool
   15361                 :         380 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   15362                 :             :                              const char* generic_name, locus where)
   15363                 :             : {
   15364                 :         380 :   gfc_symbol *sym1, *sym2;
   15365                 :         380 :   const char *pass1, *pass2;
   15366                 :         380 :   gfc_formal_arglist *dummy_args;
   15367                 :             : 
   15368                 :         380 :   gcc_assert (t1->specific && t2->specific);
   15369                 :         380 :   gcc_assert (!t1->specific->is_generic);
   15370                 :         380 :   gcc_assert (!t2->specific->is_generic);
   15371                 :         380 :   gcc_assert (t1->is_operator == t2->is_operator);
   15372                 :             : 
   15373                 :         380 :   sym1 = t1->specific->u.specific->n.sym;
   15374                 :         380 :   sym2 = t2->specific->u.specific->n.sym;
   15375                 :             : 
   15376                 :         380 :   if (sym1 == sym2)
   15377                 :             :     return true;
   15378                 :             : 
   15379                 :             :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   15380                 :         380 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   15381                 :         378 :       || sym1->attr.function != sym2->attr.function)
   15382                 :             :     {
   15383                 :           2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   15384                 :             :                  " GENERIC %qs at %L",
   15385                 :             :                  sym1->name, sym2->name, generic_name, &where);
   15386                 :           2 :       return false;
   15387                 :             :     }
   15388                 :             : 
   15389                 :             :   /* Determine PASS arguments.  */
   15390                 :         378 :   if (t1->specific->nopass)
   15391                 :             :     pass1 = NULL;
   15392                 :         327 :   else if (t1->specific->pass_arg)
   15393                 :             :     pass1 = t1->specific->pass_arg;
   15394                 :             :   else
   15395                 :             :     {
   15396                 :         212 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   15397                 :         212 :       if (dummy_args)
   15398                 :         211 :         pass1 = dummy_args->sym->name;
   15399                 :             :       else
   15400                 :             :         pass1 = NULL;
   15401                 :             :     }
   15402                 :         378 :   if (t2->specific->nopass)
   15403                 :             :     pass2 = NULL;
   15404                 :         326 :   else if (t2->specific->pass_arg)
   15405                 :             :     pass2 = t2->specific->pass_arg;
   15406                 :             :   else
   15407                 :             :     {
   15408                 :         207 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   15409                 :         207 :       if (dummy_args)
   15410                 :         206 :         pass2 = dummy_args->sym->name;
   15411                 :             :       else
   15412                 :             :         pass2 = NULL;
   15413                 :             :     }
   15414                 :             : 
   15415                 :             :   /* Compare the interfaces.  */
   15416                 :         378 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   15417                 :             :                               NULL, 0, pass1, pass2))
   15418                 :             :     {
   15419                 :           8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   15420                 :             :                  sym1->name, sym2->name, generic_name, &where);
   15421                 :           8 :       return false;
   15422                 :             :     }
   15423                 :             : 
   15424                 :             :   return true;
   15425                 :             : }
   15426                 :             : 
   15427                 :             : 
   15428                 :             : /* Worker function for resolving a generic procedure binding; this is used to
   15429                 :             :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   15430                 :             : 
   15431                 :             :    The difference between those cases is finding possible inherited bindings
   15432                 :             :    that are overridden, as one has to look for them in tb_sym_root,
   15433                 :             :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   15434                 :             :    the super-type and set p->overridden correctly.  */
   15435                 :             : 
   15436                 :             : static bool
   15437                 :        2045 : resolve_tb_generic_targets (gfc_symbol* super_type,
   15438                 :             :                             gfc_typebound_proc* p, const char* name)
   15439                 :             : {
   15440                 :        2045 :   gfc_tbp_generic* target;
   15441                 :        2045 :   gfc_symtree* first_target;
   15442                 :        2045 :   gfc_symtree* inherited;
   15443                 :             : 
   15444                 :        2045 :   gcc_assert (p && p->is_generic);
   15445                 :             : 
   15446                 :             :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   15447                 :        2045 :   gcc_assert (p->u.generic);
   15448                 :        4334 :   for (target = p->u.generic; target; target = target->next)
   15449                 :        2306 :     if (!target->specific)
   15450                 :             :       {
   15451                 :        2168 :         gfc_typebound_proc* overridden_tbp;
   15452                 :        2168 :         gfc_tbp_generic* g;
   15453                 :        2168 :         const char* target_name;
   15454                 :             : 
   15455                 :        2168 :         target_name = target->specific_st->name;
   15456                 :             : 
   15457                 :             :         /* Defined for this type directly.  */
   15458                 :        2168 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   15459                 :             :           {
   15460                 :        2159 :             target->specific = target->specific_st->n.tb;
   15461                 :        2159 :             goto specific_found;
   15462                 :             :           }
   15463                 :             : 
   15464                 :             :         /* Look for an inherited specific binding.  */
   15465                 :           9 :         if (super_type)
   15466                 :             :           {
   15467                 :           5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   15468                 :             :                                                  true, NULL);
   15469                 :             : 
   15470                 :           5 :             if (inherited)
   15471                 :             :               {
   15472                 :           5 :                 gcc_assert (inherited->n.tb);
   15473                 :           5 :                 target->specific = inherited->n.tb;
   15474                 :           5 :                 goto specific_found;
   15475                 :             :               }
   15476                 :             :           }
   15477                 :             : 
   15478                 :           4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   15479                 :             :                    " at %L", target_name, name, &p->where);
   15480                 :           4 :         return false;
   15481                 :             : 
   15482                 :             :         /* Once we've found the specific binding, check it is not ambiguous with
   15483                 :             :            other specifics already found or inherited for the same GENERIC.  */
   15484                 :        2164 : specific_found:
   15485                 :        2164 :         gcc_assert (target->specific);
   15486                 :             : 
   15487                 :             :         /* This must really be a specific binding!  */
   15488                 :        2164 :         if (target->specific->is_generic)
   15489                 :             :           {
   15490                 :           3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   15491                 :             :                        " %qs is GENERIC, too", name, &p->where, target_name);
   15492                 :           3 :             return false;
   15493                 :             :           }
   15494                 :             : 
   15495                 :             :         /* Check those already resolved on this type directly.  */
   15496                 :        5032 :         for (g = p->u.generic; g; g = g->next)
   15497                 :         724 :           if (g != target && g->specific
   15498                 :        3240 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   15499                 :             :             return false;
   15500                 :             : 
   15501                 :             :         /* Check for ambiguity with inherited specific targets.  */
   15502                 :        2170 :         for (overridden_tbp = p->overridden; overridden_tbp;
   15503                 :          16 :              overridden_tbp = overridden_tbp->overridden)
   15504                 :          19 :           if (overridden_tbp->is_generic)
   15505                 :             :             {
   15506                 :          33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   15507                 :             :                 {
   15508                 :          18 :                   gcc_assert (g->specific);
   15509                 :          18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   15510                 :             :                     return false;
   15511                 :             :                 }
   15512                 :             :             }
   15513                 :             :       }
   15514                 :             : 
   15515                 :             :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   15516                 :        2028 :   if (p->overridden && !p->overridden->is_generic)
   15517                 :             :     {
   15518                 :           1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   15519                 :             :                  " the same name", name, &p->where);
   15520                 :           1 :       return false;
   15521                 :             :     }
   15522                 :             : 
   15523                 :             :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   15524                 :             :      all must have the same attributes here.  */
   15525                 :        2027 :   first_target = p->u.generic->specific->u.specific;
   15526                 :        2027 :   gcc_assert (first_target);
   15527                 :        2027 :   p->subroutine = first_target->n.sym->attr.subroutine;
   15528                 :        2027 :   p->function = first_target->n.sym->attr.function;
   15529                 :             : 
   15530                 :        2027 :   return true;
   15531                 :             : }
   15532                 :             : 
   15533                 :             : 
   15534                 :             : /* Resolve a GENERIC procedure binding for a derived type.  */
   15535                 :             : 
   15536                 :             : static bool
   15537                 :        1045 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   15538                 :             : {
   15539                 :        1045 :   gfc_symbol* super_type;
   15540                 :             : 
   15541                 :             :   /* Find the overridden binding if any.  */
   15542                 :        1045 :   st->n.tb->overridden = NULL;
   15543                 :        1045 :   super_type = gfc_get_derived_super_type (derived);
   15544                 :        1045 :   if (super_type)
   15545                 :             :     {
   15546                 :          40 :       gfc_symtree* overridden;
   15547                 :          40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   15548                 :             :                                             true, NULL);
   15549                 :             : 
   15550                 :          40 :       if (overridden && overridden->n.tb)
   15551                 :          21 :         st->n.tb->overridden = overridden->n.tb;
   15552                 :             :     }
   15553                 :             : 
   15554                 :             :   /* Resolve using worker function.  */
   15555                 :        1045 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   15556                 :             : }
   15557                 :             : 
   15558                 :             : 
   15559                 :             : /* Retrieve the target-procedure of an operator binding and do some checks in
   15560                 :             :    common for intrinsic and user-defined type-bound operators.  */
   15561                 :             : 
   15562                 :             : static gfc_symbol*
   15563                 :        1064 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   15564                 :             : {
   15565                 :        1064 :   gfc_symbol* target_proc;
   15566                 :             : 
   15567                 :        1064 :   gcc_assert (target->specific && !target->specific->is_generic);
   15568                 :        1064 :   target_proc = target->specific->u.specific->n.sym;
   15569                 :        1064 :   gcc_assert (target_proc);
   15570                 :             : 
   15571                 :             :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   15572                 :        1064 :   if (target->specific->nopass)
   15573                 :             :     {
   15574                 :           2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   15575                 :           2 :       return NULL;
   15576                 :             :     }
   15577                 :             : 
   15578                 :             :   return target_proc;
   15579                 :             : }
   15580                 :             : 
   15581                 :             : 
   15582                 :             : /* Resolve a type-bound intrinsic operator.  */
   15583                 :             : 
   15584                 :             : static bool
   15585                 :         958 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   15586                 :             :                                 gfc_typebound_proc* p)
   15587                 :             : {
   15588                 :         958 :   gfc_symbol* super_type;
   15589                 :         958 :   gfc_tbp_generic* target;
   15590                 :             : 
   15591                 :             :   /* If there's already an error here, do nothing (but don't fail again).  */
   15592                 :         958 :   if (p->error)
   15593                 :             :     return true;
   15594                 :             : 
   15595                 :             :   /* Operators should always be GENERIC bindings.  */
   15596                 :         958 :   gcc_assert (p->is_generic);
   15597                 :             : 
   15598                 :             :   /* Look for an overridden binding.  */
   15599                 :         958 :   super_type = gfc_get_derived_super_type (derived);
   15600                 :         958 :   if (super_type && super_type->f2k_derived)
   15601                 :           1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   15602                 :             :                                                      op, true, NULL);
   15603                 :             :   else
   15604                 :         957 :     p->overridden = NULL;
   15605                 :             : 
   15606                 :             :   /* Resolve general GENERIC properties using worker function.  */
   15607                 :         958 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   15608                 :           1 :     goto error;
   15609                 :             : 
   15610                 :             :   /* Check the targets to be procedures of correct interface.  */
   15611                 :        1958 :   for (target = p->u.generic; target; target = target->next)
   15612                 :             :     {
   15613                 :        1021 :       gfc_symbol* target_proc;
   15614                 :             : 
   15615                 :        1021 :       target_proc = get_checked_tb_operator_target (target, p->where);
   15616                 :        1021 :       if (!target_proc)
   15617                 :           1 :         goto error;
   15618                 :             : 
   15619                 :        1020 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   15620                 :           3 :         goto error;
   15621                 :             : 
   15622                 :             :       /* Add target to non-typebound operator list.  */
   15623                 :        1017 :       if (!target->specific->deferred && !derived->attr.use_assoc
   15624                 :         358 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   15625                 :             :         {
   15626                 :         356 :           gfc_interface *head, *intr;
   15627                 :             : 
   15628                 :             :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   15629                 :             :              mechanism for handling module procedures winds up resolving
   15630                 :             :              operator interfaces twice and would otherwise cause an error.  */
   15631                 :         424 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   15632                 :          82 :             if (intr->sym == target_proc
   15633                 :          16 :                 && target_proc->attr.used_in_submodule)
   15634                 :             :               return true;
   15635                 :             : 
   15636                 :         342 :           if (!gfc_check_new_interface (derived->ns->op[op],
   15637                 :             :                                         target_proc, p->where))
   15638                 :             :             return false;
   15639                 :         340 :           head = derived->ns->op[op];
   15640                 :         340 :           intr = gfc_get_interface ();
   15641                 :         340 :           intr->sym = target_proc;
   15642                 :         340 :           intr->where = p->where;
   15643                 :         340 :           intr->next = head;
   15644                 :         340 :           derived->ns->op[op] = intr;
   15645                 :             :         }
   15646                 :             :     }
   15647                 :             : 
   15648                 :             :   return true;
   15649                 :             : 
   15650                 :           5 : error:
   15651                 :           5 :   p->error = 1;
   15652                 :           5 :   return false;
   15653                 :             : }
   15654                 :             : 
   15655                 :             : 
   15656                 :             : /* Resolve a type-bound user operator (tree-walker callback).  */
   15657                 :             : 
   15658                 :             : static gfc_symbol* resolve_bindings_derived;
   15659                 :             : static bool resolve_bindings_result;
   15660                 :             : 
   15661                 :             : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   15662                 :             : 
   15663                 :             : static void
   15664                 :          42 : resolve_typebound_user_op (gfc_symtree* stree)
   15665                 :             : {
   15666                 :          42 :   gfc_symbol* super_type;
   15667                 :          42 :   gfc_tbp_generic* target;
   15668                 :             : 
   15669                 :          42 :   gcc_assert (stree && stree->n.tb);
   15670                 :             : 
   15671                 :          42 :   if (stree->n.tb->error)
   15672                 :             :     return;
   15673                 :             : 
   15674                 :             :   /* Operators should always be GENERIC bindings.  */
   15675                 :          42 :   gcc_assert (stree->n.tb->is_generic);
   15676                 :             : 
   15677                 :             :   /* Find overridden procedure, if any.  */
   15678                 :          42 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   15679                 :          42 :   if (super_type && super_type->f2k_derived)
   15680                 :             :     {
   15681                 :           0 :       gfc_symtree* overridden;
   15682                 :           0 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   15683                 :             :                                                stree->name, true, NULL);
   15684                 :             : 
   15685                 :           0 :       if (overridden && overridden->n.tb)
   15686                 :           0 :         stree->n.tb->overridden = overridden->n.tb;
   15687                 :             :     }
   15688                 :             :   else
   15689                 :          42 :     stree->n.tb->overridden = NULL;
   15690                 :             : 
   15691                 :             :   /* Resolve basically using worker function.  */
   15692                 :          42 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   15693                 :           0 :     goto error;
   15694                 :             : 
   15695                 :             :   /* Check the targets to be functions of correct interface.  */
   15696                 :          82 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   15697                 :             :     {
   15698                 :          43 :       gfc_symbol* target_proc;
   15699                 :             : 
   15700                 :          43 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   15701                 :          43 :       if (!target_proc)
   15702                 :           1 :         goto error;
   15703                 :             : 
   15704                 :          42 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   15705                 :           2 :         goto error;
   15706                 :             :     }
   15707                 :             : 
   15708                 :             :   return;
   15709                 :             : 
   15710                 :           3 : error:
   15711                 :           3 :   resolve_bindings_result = false;
   15712                 :           3 :   stree->n.tb->error = 1;
   15713                 :             : }
   15714                 :             : 
   15715                 :             : 
   15716                 :             : /* Resolve the type-bound procedures for a derived type.  */
   15717                 :             : 
   15718                 :             : static void
   15719                 :        9084 : resolve_typebound_procedure (gfc_symtree* stree)
   15720                 :             : {
   15721                 :        9084 :   gfc_symbol* proc;
   15722                 :        9084 :   locus where;
   15723                 :        9084 :   gfc_symbol* me_arg;
   15724                 :        9084 :   gfc_symbol* super_type;
   15725                 :        9084 :   gfc_component* comp;
   15726                 :             : 
   15727                 :        9084 :   gcc_assert (stree);
   15728                 :             : 
   15729                 :             :   /* Undefined specific symbol from GENERIC target definition.  */
   15730                 :        9084 :   if (!stree->n.tb)
   15731                 :        9002 :     return;
   15732                 :             : 
   15733                 :        9078 :   if (stree->n.tb->error)
   15734                 :             :     return;
   15735                 :             : 
   15736                 :             :   /* If this is a GENERIC binding, use that routine.  */
   15737                 :        9062 :   if (stree->n.tb->is_generic)
   15738                 :             :     {
   15739                 :        1045 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   15740                 :          17 :         goto error;
   15741                 :             :       return;
   15742                 :             :     }
   15743                 :             : 
   15744                 :             :   /* Get the target-procedure to check it.  */
   15745                 :        8017 :   gcc_assert (!stree->n.tb->is_generic);
   15746                 :        8017 :   gcc_assert (stree->n.tb->u.specific);
   15747                 :        8017 :   proc = stree->n.tb->u.specific->n.sym;
   15748                 :        8017 :   where = stree->n.tb->where;
   15749                 :             : 
   15750                 :             :   /* Default access should already be resolved from the parser.  */
   15751                 :        8017 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   15752                 :             : 
   15753                 :        8017 :   if (stree->n.tb->deferred)
   15754                 :             :     {
   15755                 :         669 :       if (!check_proc_interface (proc, &where))
   15756                 :           5 :         goto error;
   15757                 :             :     }
   15758                 :             :   else
   15759                 :             :     {
   15760                 :             :       /* If proc has not been resolved at this point, proc->name may
   15761                 :             :          actually be a USE associated entity. See PR fortran/89647. */
   15762                 :        7348 :       if (!proc->resolve_symbol_called
   15763                 :        4992 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   15764                 :             :         {
   15765                 :          11 :           gfc_symbol *tmp;
   15766                 :          11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   15767                 :          11 :           if (tmp && tmp->attr.use_assoc)
   15768                 :             :             {
   15769                 :           1 :               proc->module = tmp->module;
   15770                 :           1 :               proc->attr.proc = tmp->attr.proc;
   15771                 :           1 :               proc->attr.function = tmp->attr.function;
   15772                 :           1 :               proc->attr.subroutine = tmp->attr.subroutine;
   15773                 :           1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   15774                 :           1 :               proc->ts = tmp->ts;
   15775                 :           1 :               proc->result = tmp->result;
   15776                 :             :             }
   15777                 :             :         }
   15778                 :             : 
   15779                 :             :       /* Check for F08:C465.  */
   15780                 :        7348 :       if ((!proc->attr.subroutine && !proc->attr.function)
   15781                 :        7338 :           || (proc->attr.proc != PROC_MODULE
   15782                 :          21 :               && proc->attr.if_source != IFSRC_IFBODY
   15783                 :           7 :               && !proc->attr.module_procedure)
   15784                 :        7337 :           || proc->attr.abstract)
   15785                 :             :         {
   15786                 :          12 :           gfc_error ("%qs must be a module procedure or an external "
   15787                 :             :                      "procedure with an explicit interface at %L",
   15788                 :             :                      proc->name, &where);
   15789                 :          12 :           goto error;
   15790                 :             :         }
   15791                 :             :     }
   15792                 :             : 
   15793                 :        8000 :   stree->n.tb->subroutine = proc->attr.subroutine;
   15794                 :        8000 :   stree->n.tb->function = proc->attr.function;
   15795                 :             : 
   15796                 :             :   /* Find the super-type of the current derived type.  We could do this once and
   15797                 :             :      store in a global if speed is needed, but as long as not I believe this is
   15798                 :             :      more readable and clearer.  */
   15799                 :        8000 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   15800                 :             : 
   15801                 :             :   /* If PASS, resolve and check arguments if not already resolved / loaded
   15802                 :             :      from a .mod file.  */
   15803                 :        8000 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   15804                 :             :     {
   15805                 :        2654 :       gfc_formal_arglist *dummy_args;
   15806                 :             : 
   15807                 :        2654 :       dummy_args = gfc_sym_get_dummy_args (proc);
   15808                 :        2654 :       if (stree->n.tb->pass_arg)
   15809                 :             :         {
   15810                 :         453 :           gfc_formal_arglist *i;
   15811                 :             : 
   15812                 :             :           /* If an explicit passing argument name is given, walk the arg-list
   15813                 :             :              and look for it.  */
   15814                 :             : 
   15815                 :         453 :           me_arg = NULL;
   15816                 :         453 :           stree->n.tb->pass_arg_num = 1;
   15817                 :         573 :           for (i = dummy_args; i; i = i->next)
   15818                 :             :             {
   15819                 :         571 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   15820                 :             :                 {
   15821                 :             :                   me_arg = i->sym;
   15822                 :             :                   break;
   15823                 :             :                 }
   15824                 :         120 :               ++stree->n.tb->pass_arg_num;
   15825                 :             :             }
   15826                 :             : 
   15827                 :         453 :           if (!me_arg)
   15828                 :             :             {
   15829                 :           2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   15830                 :             :                          " argument %qs",
   15831                 :             :                          proc->name, stree->n.tb->pass_arg, &where,
   15832                 :             :                          stree->n.tb->pass_arg);
   15833                 :           2 :               goto error;
   15834                 :             :             }
   15835                 :             :         }
   15836                 :             :       else
   15837                 :             :         {
   15838                 :             :           /* Otherwise, take the first one; there should in fact be at least
   15839                 :             :              one.  */
   15840                 :        2201 :           stree->n.tb->pass_arg_num = 1;
   15841                 :        2201 :           if (!dummy_args)
   15842                 :             :             {
   15843                 :           2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   15844                 :             :                          " least one argument", proc->name, &where);
   15845                 :           2 :               goto error;
   15846                 :             :             }
   15847                 :        2199 :           me_arg = dummy_args->sym;
   15848                 :             :         }
   15849                 :             : 
   15850                 :             :       /* Now check that the argument-type matches and the passed-object
   15851                 :             :          dummy argument is generally fine.  */
   15852                 :             : 
   15853                 :        2199 :       gcc_assert (me_arg);
   15854                 :             : 
   15855                 :        2650 :       if (me_arg->ts.type != BT_CLASS)
   15856                 :             :         {
   15857                 :           5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   15858                 :             :                      " at %L", proc->name, &where);
   15859                 :           5 :           goto error;
   15860                 :             :         }
   15861                 :             : 
   15862                 :             :       /* The derived type is not a PDT template.  Resolve as usual.  */
   15863                 :        2645 :       if (!resolve_bindings_derived->attr.pdt_template
   15864                 :        2630 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   15865                 :             :         {
   15866                 :           0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   15867                 :             :                      "the derived-type %qs", me_arg->name, proc->name,
   15868                 :             :                      me_arg->name, &where, resolve_bindings_derived->name);
   15869                 :           0 :           goto error;
   15870                 :             :         }
   15871                 :             : 
   15872                 :        2645 :       if (resolve_bindings_derived->attr.pdt_template
   15873                 :        2660 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   15874                 :          15 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   15875                 :             :         {
   15876                 :           0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   15877                 :             :                      "the parametric derived-type %qs", me_arg->name,
   15878                 :             :                      proc->name, me_arg->name, &where,
   15879                 :             :                      resolve_bindings_derived->name);
   15880                 :           0 :           goto error;
   15881                 :             :         }
   15882                 :             : 
   15883                 :        2645 :       if (resolve_bindings_derived->attr.pdt_template
   15884                 :          15 :           && gfc_pdt_is_instance_of (resolve_bindings_derived,
   15885                 :          15 :                                      CLASS_DATA (me_arg)->ts.u.derived)
   15886                 :          15 :           && (me_arg->param_list != NULL)
   15887                 :        2660 :           && (gfc_spec_list_type (me_arg->param_list,
   15888                 :          15 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   15889                 :             :                                   != SPEC_ASSUMED))
   15890                 :             :         {
   15891                 :             : 
   15892                 :             :           /* Add a check to verify if there are any LEN parameters in the
   15893                 :             :              first place.  If there are LEN parameters, throw this error.
   15894                 :             :              If there are only KIND parameters, then don't trigger
   15895                 :             :              this error.  */
   15896                 :           7 :           gfc_component *c;
   15897                 :           7 :           bool seen_len_param = false;
   15898                 :           7 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   15899                 :             : 
   15900                 :           8 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   15901                 :             :             {
   15902                 :           7 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   15903                 :             :                                      me_arg_param->name, true, true, NULL);
   15904                 :             : 
   15905                 :           7 :               gcc_assert (c != NULL);
   15906                 :             : 
   15907                 :           7 :               if (c->attr.pdt_kind)
   15908                 :           1 :                 continue;
   15909                 :             : 
   15910                 :             :               /* Getting here implies that there is a pdt_len parameter
   15911                 :             :                  in the list.  */
   15912                 :             :               seen_len_param = true;
   15913                 :             :               break;
   15914                 :             :             }
   15915                 :             : 
   15916                 :           7 :             if (seen_len_param)
   15917                 :             :               {
   15918                 :           6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   15919                 :             :                            "argument %qs of %qs at %L must be ASSUMED.",
   15920                 :             :                            me_arg->name, proc->name, &where);
   15921                 :           6 :                 goto error;
   15922                 :             :               }
   15923                 :             :         }
   15924                 :             : 
   15925                 :        2639 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   15926                 :        2639 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   15927                 :             :         {
   15928                 :           1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   15929                 :             :                      " scalar", proc->name, &where);
   15930                 :           1 :           goto error;
   15931                 :             :         }
   15932                 :        2638 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   15933                 :             :         {
   15934                 :           2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   15935                 :             :                      " be ALLOCATABLE", proc->name, &where);
   15936                 :           2 :           goto error;
   15937                 :             :         }
   15938                 :        2636 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   15939                 :             :         {
   15940                 :           2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   15941                 :             :                      " be POINTER", proc->name, &where);
   15942                 :           2 :           goto error;
   15943                 :             :         }
   15944                 :             :     }
   15945                 :             : 
   15946                 :             :   /* If we are extending some type, check that we don't override a procedure
   15947                 :             :      flagged NON_OVERRIDABLE.  */
   15948                 :        7980 :   stree->n.tb->overridden = NULL;
   15949                 :        7980 :   if (super_type)
   15950                 :             :     {
   15951                 :        1480 :       gfc_symtree* overridden;
   15952                 :        1480 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   15953                 :             :                                             stree->name, true, NULL);
   15954                 :             : 
   15955                 :        1480 :       if (overridden)
   15956                 :             :         {
   15957                 :        1210 :           if (overridden->n.tb)
   15958                 :        1210 :             stree->n.tb->overridden = overridden->n.tb;
   15959                 :             : 
   15960                 :        1210 :           if (!gfc_check_typebound_override (stree, overridden))
   15961                 :          26 :             goto error;
   15962                 :             :         }
   15963                 :             :     }
   15964                 :             : 
   15965                 :             :   /* See if there's a name collision with a component directly in this type.  */
   15966                 :       18983 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   15967                 :       11030 :     if (!strcmp (comp->name, stree->name))
   15968                 :             :       {
   15969                 :           1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   15970                 :             :                    " %qs",
   15971                 :             :                    stree->name, &where, resolve_bindings_derived->name);
   15972                 :           1 :         goto error;
   15973                 :             :       }
   15974                 :             : 
   15975                 :             :   /* Try to find a name collision with an inherited component.  */
   15976                 :        7953 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   15977                 :             :                                         NULL))
   15978                 :             :     {
   15979                 :           1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   15980                 :             :                  " component of %qs",
   15981                 :             :                  stree->name, &where, resolve_bindings_derived->name);
   15982                 :           1 :       goto error;
   15983                 :             :     }
   15984                 :             : 
   15985                 :        7952 :   stree->n.tb->error = 0;
   15986                 :        7952 :   return;
   15987                 :             : 
   15988                 :          82 : error:
   15989                 :          82 :   resolve_bindings_result = false;
   15990                 :          82 :   stree->n.tb->error = 1;
   15991                 :             : }
   15992                 :             : 
   15993                 :             : 
   15994                 :             : static bool
   15995                 :       79066 : resolve_typebound_procedures (gfc_symbol* derived)
   15996                 :             : {
   15997                 :       79066 :   int op;
   15998                 :       79066 :   gfc_symbol* super_type;
   15999                 :             : 
   16000                 :       79066 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   16001                 :             :     return true;
   16002                 :             : 
   16003                 :        4453 :   super_type = gfc_get_derived_super_type (derived);
   16004                 :        4453 :   if (super_type)
   16005                 :         847 :     resolve_symbol (super_type);
   16006                 :             : 
   16007                 :        4453 :   resolve_bindings_derived = derived;
   16008                 :        4453 :   resolve_bindings_result = true;
   16009                 :             : 
   16010                 :        4453 :   if (derived->f2k_derived->tb_sym_root)
   16011                 :        4453 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   16012                 :             :                           &resolve_typebound_procedure);
   16013                 :             : 
   16014                 :        4453 :   if (derived->f2k_derived->tb_uop_root)
   16015                 :          38 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   16016                 :             :                           &resolve_typebound_user_op);
   16017                 :             : 
   16018                 :      129137 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   16019                 :             :     {
   16020                 :      124684 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   16021                 :      124684 :       if (p && !resolve_typebound_intrinsic_op (derived,
   16022                 :             :                                                 (gfc_intrinsic_op)op, p))
   16023                 :           7 :         resolve_bindings_result = false;
   16024                 :             :     }
   16025                 :             : 
   16026                 :        4453 :   return resolve_bindings_result;
   16027                 :             : }
   16028                 :             : 
   16029                 :             : 
   16030                 :             : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   16031                 :             :    to give all identical derived types the same backend_decl.  */
   16032                 :             : static void
   16033                 :      164048 : add_dt_to_dt_list (gfc_symbol *derived)
   16034                 :             : {
   16035                 :      164048 :   if (!derived->dt_next)
   16036                 :             :     {
   16037                 :       75213 :       if (gfc_derived_types)
   16038                 :             :         {
   16039                 :       61515 :           derived->dt_next = gfc_derived_types->dt_next;
   16040                 :       61515 :           gfc_derived_types->dt_next = derived;
   16041                 :             :         }
   16042                 :             :       else
   16043                 :             :         {
   16044                 :       13698 :           derived->dt_next = derived;
   16045                 :             :         }
   16046                 :       75213 :       gfc_derived_types = derived;
   16047                 :             :     }
   16048                 :      164048 : }
   16049                 :             : 
   16050                 :             : 
   16051                 :             : /* Ensure that a derived-type is really not abstract, meaning that every
   16052                 :             :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   16053                 :             : 
   16054                 :             : static bool
   16055                 :        6904 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   16056                 :             : {
   16057                 :        6904 :   if (!st)
   16058                 :             :     return true;
   16059                 :             : 
   16060                 :        2711 :   if (!ensure_not_abstract_walker (sub, st->left))
   16061                 :             :     return false;
   16062                 :        2709 :   if (!ensure_not_abstract_walker (sub, st->right))
   16063                 :             :     return false;
   16064                 :             : 
   16065                 :        2708 :   if (st->n.tb && st->n.tb->deferred)
   16066                 :             :     {
   16067                 :        1957 :       gfc_symtree* overriding;
   16068                 :        1957 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   16069                 :        1957 :       if (!overriding)
   16070                 :             :         return false;
   16071                 :        1956 :       gcc_assert (overriding->n.tb);
   16072                 :        1956 :       if (overriding->n.tb->deferred)
   16073                 :             :         {
   16074                 :           4 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   16075                 :             :                      " %qs is DEFERRED and not overridden",
   16076                 :             :                      sub->name, &sub->declared_at, st->name);
   16077                 :           4 :           return false;
   16078                 :             :         }
   16079                 :             :     }
   16080                 :             : 
   16081                 :             :   return true;
   16082                 :             : }
   16083                 :             : 
   16084                 :             : static bool
   16085                 :        1342 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   16086                 :             : {
   16087                 :             :   /* The algorithm used here is to recursively travel up the ancestry of sub
   16088                 :             :      and for each ancestor-type, check all bindings.  If any of them is
   16089                 :             :      DEFERRED, look it up starting from sub and see if the found (overriding)
   16090                 :             :      binding is not DEFERRED.
   16091                 :             :      This is not the most efficient way to do this, but it should be ok and is
   16092                 :             :      clearer than something sophisticated.  */
   16093                 :             : 
   16094                 :        1485 :   gcc_assert (ancestor && !sub->attr.abstract);
   16095                 :             : 
   16096                 :        1485 :   if (!ancestor->attr.abstract)
   16097                 :             :     return true;
   16098                 :             : 
   16099                 :             :   /* Walk bindings of this ancestor.  */
   16100                 :        1484 :   if (ancestor->f2k_derived)
   16101                 :             :     {
   16102                 :        1484 :       bool t;
   16103                 :        1484 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   16104                 :        1484 :       if (!t)
   16105                 :             :         return false;
   16106                 :             :     }
   16107                 :             : 
   16108                 :             :   /* Find next ancestor type and recurse on it.  */
   16109                 :        1479 :   ancestor = gfc_get_derived_super_type (ancestor);
   16110                 :        1479 :   if (ancestor)
   16111                 :             :     return ensure_not_abstract (sub, ancestor);
   16112                 :             : 
   16113                 :             :   return true;
   16114                 :             : }
   16115                 :             : 
   16116                 :             : 
   16117                 :             : /* This check for typebound defined assignments is done recursively
   16118                 :             :    since the order in which derived types are resolved is not always in
   16119                 :             :    order of the declarations.  */
   16120                 :             : 
   16121                 :             : static void
   16122                 :      167750 : check_defined_assignments (gfc_symbol *derived)
   16123                 :             : {
   16124                 :      167750 :   gfc_component *c;
   16125                 :             : 
   16126                 :      563893 :   for (c = derived->components; c; c = c->next)
   16127                 :             :     {
   16128                 :      397677 :       if (!gfc_bt_struct (c->ts.type)
   16129                 :       95958 :           || c->attr.pointer
   16130                 :       18011 :           || c->attr.proc_pointer_comp
   16131                 :             :           || c->attr.class_pointer
   16132                 :       18011 :           || c->attr.proc_pointer)
   16133                 :      380059 :         continue;
   16134                 :             : 
   16135                 :       17618 :       if (c->ts.u.derived->attr.defined_assign_comp
   16136                 :       17407 :           || (c->ts.u.derived->f2k_derived
   16137                 :       16838 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   16138                 :             :         {
   16139                 :        1510 :           derived->attr.defined_assign_comp = 1;
   16140                 :        1510 :           return;
   16141                 :             :         }
   16142                 :             : 
   16143                 :       16108 :       if (c->attr.allocatable)
   16144                 :        5492 :         continue;
   16145                 :             : 
   16146                 :       10616 :       check_defined_assignments (c->ts.u.derived);
   16147                 :       10616 :       if (c->ts.u.derived->attr.defined_assign_comp)
   16148                 :             :         {
   16149                 :          24 :           derived->attr.defined_assign_comp = 1;
   16150                 :          24 :           return;
   16151                 :             :         }
   16152                 :             :     }
   16153                 :             : }
   16154                 :             : 
   16155                 :             : 
   16156                 :             : /* Resolve a single component of a derived type or structure.  */
   16157                 :             : 
   16158                 :             : static bool
   16159                 :      379889 : resolve_component (gfc_component *c, gfc_symbol *sym)
   16160                 :             : {
   16161                 :      379889 :   gfc_symbol *super_type;
   16162                 :      379889 :   symbol_attribute *attr;
   16163                 :             : 
   16164                 :      379889 :   if (c->attr.artificial)
   16165                 :             :     return true;
   16166                 :             : 
   16167                 :             :   /* Do not allow vtype components to be resolved in nameless namespaces
   16168                 :             :      such as block data because the procedure pointers will cause ICEs
   16169                 :             :      and vtables are not needed in these contexts.  */
   16170                 :      257651 :   if (sym->attr.vtype && sym->attr.use_assoc
   16171                 :       46176 :       && sym->ns->proc_name == NULL)
   16172                 :             :     return true;
   16173                 :             : 
   16174                 :             :   /* F2008, C442.  */
   16175                 :      257642 :   if ((!sym->attr.is_class || c != sym->components)
   16176                 :      257642 :       && c->attr.codimension
   16177                 :         167 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   16178                 :             :     {
   16179                 :           4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   16180                 :             :                  "deferred shape", c->name, &c->loc);
   16181                 :           4 :       return false;
   16182                 :             :     }
   16183                 :             : 
   16184                 :             :   /* F2008, C443.  */
   16185                 :      257638 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   16186                 :          77 :       && c->ts.u.derived->ts.is_iso_c)
   16187                 :             :     {
   16188                 :           1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   16189                 :             :                  "shall not be a coarray", c->name, &c->loc);
   16190                 :           1 :       return false;
   16191                 :             :     }
   16192                 :             : 
   16193                 :             :   /* F2008, C444.  */
   16194                 :      257637 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   16195                 :          22 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   16196                 :          22 :           || c->attr.allocatable))
   16197                 :             :     {
   16198                 :           3 :       gfc_error ("Component %qs at %L with coarray component "
   16199                 :             :                  "shall be a nonpointer, nonallocatable scalar",
   16200                 :             :                  c->name, &c->loc);
   16201                 :           3 :       return false;
   16202                 :             :     }
   16203                 :             : 
   16204                 :             :   /* F2008, C448.  */
   16205                 :      257634 :   if (c->ts.type == BT_CLASS)
   16206                 :             :     {
   16207                 :        6502 :       if (c->attr.class_ok && CLASS_DATA (c))
   16208                 :             :         {
   16209                 :        6494 :           attr = &(CLASS_DATA (c)->attr);
   16210                 :             : 
   16211                 :             :           /* Fix up contiguous attribute.  */
   16212                 :        6494 :           if (c->attr.contiguous)
   16213                 :           3 :             attr->contiguous = 1;
   16214                 :             :         }
   16215                 :             :       else
   16216                 :             :         attr = NULL;
   16217                 :             :     }
   16218                 :             :   else
   16219                 :      251132 :     attr = &c->attr;
   16220                 :             : 
   16221                 :      257629 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   16222                 :             :     {
   16223                 :           5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   16224                 :             :                  "is not an array pointer", c->name, &c->loc);
   16225                 :           5 :       return false;
   16226                 :             :     }
   16227                 :             : 
   16228                 :             :   /* F2003, 15.2.1 - length has to be one.  */
   16229                 :       37482 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   16230                 :      257648 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   16231                 :          19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   16232                 :          19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   16233                 :             :     {
   16234                 :           1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   16235                 :             :                  c->name, &c->loc);
   16236                 :           1 :       return false;
   16237                 :             :     }
   16238                 :             : 
   16239                 :      257628 :   if (c->attr.proc_pointer && c->ts.interface)
   16240                 :             :     {
   16241                 :       13466 :       gfc_symbol *ifc = c->ts.interface;
   16242                 :             : 
   16243                 :       13466 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   16244                 :             :         {
   16245                 :           6 :           c->tb->error = 1;
   16246                 :           6 :           return false;
   16247                 :             :         }
   16248                 :             : 
   16249                 :       13460 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   16250                 :             :         {
   16251                 :             :           /* Resolve interface and copy attributes.  */
   16252                 :       13429 :           if (ifc->formal && !ifc->formal_ns)
   16253                 :        2483 :             resolve_symbol (ifc);
   16254                 :       13429 :           if (ifc->attr.intrinsic)
   16255                 :           0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   16256                 :             : 
   16257                 :       13429 :           if (ifc->result)
   16258                 :             :             {
   16259                 :        6654 :               c->ts = ifc->result->ts;
   16260                 :        6654 :               c->attr.allocatable = ifc->result->attr.allocatable;
   16261                 :        6654 :               c->attr.pointer = ifc->result->attr.pointer;
   16262                 :        6654 :               c->attr.dimension = ifc->result->attr.dimension;
   16263                 :        6654 :               c->as = gfc_copy_array_spec (ifc->result->as);
   16264                 :        6654 :               c->attr.class_ok = ifc->result->attr.class_ok;
   16265                 :             :             }
   16266                 :             :           else
   16267                 :             :             {
   16268                 :        6775 :               c->ts = ifc->ts;
   16269                 :        6775 :               c->attr.allocatable = ifc->attr.allocatable;
   16270                 :        6775 :               c->attr.pointer = ifc->attr.pointer;
   16271                 :        6775 :               c->attr.dimension = ifc->attr.dimension;
   16272                 :        6775 :               c->as = gfc_copy_array_spec (ifc->as);
   16273                 :        6775 :               c->attr.class_ok = ifc->attr.class_ok;
   16274                 :             :             }
   16275                 :       13429 :           c->ts.interface = ifc;
   16276                 :       13429 :           c->attr.function = ifc->attr.function;
   16277                 :       13429 :           c->attr.subroutine = ifc->attr.subroutine;
   16278                 :             : 
   16279                 :       13429 :           c->attr.pure = ifc->attr.pure;
   16280                 :       13429 :           c->attr.elemental = ifc->attr.elemental;
   16281                 :       13429 :           c->attr.recursive = ifc->attr.recursive;
   16282                 :       13429 :           c->attr.always_explicit = ifc->attr.always_explicit;
   16283                 :       13429 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   16284                 :             :           /* Copy char length.  */
   16285                 :       13429 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   16286                 :             :             {
   16287                 :         220 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   16288                 :         183 :               if (cl->length && !cl->resolved
   16289                 :         240 :                   && !gfc_resolve_expr (cl->length))
   16290                 :             :                 {
   16291                 :           0 :                   c->tb->error = 1;
   16292                 :           0 :                   return false;
   16293                 :             :                 }
   16294                 :         220 :               c->ts.u.cl = cl;
   16295                 :             :             }
   16296                 :             :         }
   16297                 :             :     }
   16298                 :      244162 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   16299                 :             :     {
   16300                 :             :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   16301                 :             :          interface must be a subroutine.  */
   16302                 :         115 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   16303                 :             :     }
   16304                 :             : 
   16305                 :             :   /* Procedure pointer components: Check PASS arg.  */
   16306                 :      257622 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   16307                 :         717 :       && !sym->attr.vtype)
   16308                 :             :     {
   16309                 :          94 :       gfc_symbol* me_arg;
   16310                 :             : 
   16311                 :          94 :       if (c->tb->pass_arg)
   16312                 :             :         {
   16313                 :          19 :           gfc_formal_arglist* i;
   16314                 :             : 
   16315                 :             :           /* If an explicit passing argument name is given, walk the arg-list
   16316                 :             :             and look for it.  */
   16317                 :             : 
   16318                 :          19 :           me_arg = NULL;
   16319                 :          19 :           c->tb->pass_arg_num = 1;
   16320                 :          33 :           for (i = c->ts.interface->formal; i; i = i->next)
   16321                 :             :             {
   16322                 :          32 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   16323                 :             :                 {
   16324                 :             :                   me_arg = i->sym;
   16325                 :             :                   break;
   16326                 :             :                 }
   16327                 :          14 :               c->tb->pass_arg_num++;
   16328                 :             :             }
   16329                 :             : 
   16330                 :          19 :           if (!me_arg)
   16331                 :             :             {
   16332                 :           1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   16333                 :             :                          "at %L has no argument %qs", c->name,
   16334                 :             :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   16335                 :           1 :               c->tb->error = 1;
   16336                 :           1 :               return false;
   16337                 :             :             }
   16338                 :             :         }
   16339                 :             :       else
   16340                 :             :         {
   16341                 :             :           /* Otherwise, take the first one; there should in fact be at least
   16342                 :             :             one.  */
   16343                 :          75 :           c->tb->pass_arg_num = 1;
   16344                 :          75 :           if (!c->ts.interface->formal)
   16345                 :             :             {
   16346                 :           3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   16347                 :             :                          "must have at least one argument",
   16348                 :             :                          c->name, &c->loc);
   16349                 :           3 :               c->tb->error = 1;
   16350                 :           3 :               return false;
   16351                 :             :             }
   16352                 :          72 :           me_arg = c->ts.interface->formal->sym;
   16353                 :             :         }
   16354                 :             : 
   16355                 :             :       /* Now check that the argument-type matches.  */
   16356                 :          72 :       gcc_assert (me_arg);
   16357                 :          90 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   16358                 :          89 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   16359                 :          89 :           || (me_arg->ts.type == BT_CLASS
   16360                 :          81 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   16361                 :             :         {
   16362                 :           1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   16363                 :             :                      " the derived type %qs", me_arg->name, c->name,
   16364                 :             :                      me_arg->name, &c->loc, sym->name);
   16365                 :           1 :           c->tb->error = 1;
   16366                 :           1 :           return false;
   16367                 :             :         }
   16368                 :             : 
   16369                 :             :       /* Check for F03:C453.  */
   16370                 :          89 :       if (CLASS_DATA (me_arg)->attr.dimension)
   16371                 :             :         {
   16372                 :           1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   16373                 :             :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   16374                 :             :                      &c->loc);
   16375                 :           1 :           c->tb->error = 1;
   16376                 :           1 :           return false;
   16377                 :             :         }
   16378                 :             : 
   16379                 :          88 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   16380                 :             :         {
   16381                 :           1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   16382                 :             :                      "may not have the POINTER attribute", me_arg->name,
   16383                 :             :                      c->name, me_arg->name, &c->loc);
   16384                 :           1 :           c->tb->error = 1;
   16385                 :           1 :           return false;
   16386                 :             :         }
   16387                 :             : 
   16388                 :          87 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   16389                 :             :         {
   16390                 :           1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   16391                 :             :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   16392                 :             :                      me_arg->name, &c->loc);
   16393                 :           1 :           c->tb->error = 1;
   16394                 :           1 :           return false;
   16395                 :             :         }
   16396                 :             : 
   16397                 :          86 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   16398                 :             :         {
   16399                 :           2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   16400                 :             :                      " at %L", c->name, &c->loc);
   16401                 :           2 :           return false;
   16402                 :             :         }
   16403                 :             : 
   16404                 :             :     }
   16405                 :             : 
   16406                 :             :   /* Check type-spec if this is not the parent-type component.  */
   16407                 :      257612 :   if (((sym->attr.is_class
   16408                 :       11498 :         && (!sym->components->ts.u.derived->attr.extension
   16409                 :        2318 :             || c != CLASS_DATA (sym->components)))
   16410                 :      247410 :        || (!sym->attr.is_class
   16411                 :      246114 :            && (!sym->attr.extension || c != sym->components)))
   16412                 :      249872 :       && !sym->attr.vtype
   16413                 :      405904 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   16414                 :             :     return false;
   16415                 :             : 
   16416                 :      257611 :   super_type = gfc_get_derived_super_type (sym);
   16417                 :             : 
   16418                 :             :   /* If this type is an extension, set the accessibility of the parent
   16419                 :             :      component.  */
   16420                 :      257611 :   if (super_type
   16421                 :       24019 :       && ((sym->attr.is_class
   16422                 :       11498 :            && c == CLASS_DATA (sym->components))
   16423                 :       16003 :           || (!sym->attr.is_class && c == sym->components))
   16424                 :       14460 :       && strcmp (super_type->name, c->name) == 0)
   16425                 :        6318 :     c->attr.access = super_type->attr.access;
   16426                 :             : 
   16427                 :             :   /* If this type is an extension, see if this component has the same name
   16428                 :             :      as an inherited type-bound procedure.  */
   16429                 :       24019 :   if (super_type && !sym->attr.is_class
   16430                 :       12521 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   16431                 :             :     {
   16432                 :           1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   16433                 :             :                  " inherited type-bound procedure",
   16434                 :             :                  c->name, sym->name, &c->loc);
   16435                 :           1 :       return false;
   16436                 :             :     }
   16437                 :             : 
   16438                 :      257610 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   16439                 :        9078 :         && !c->ts.deferred)
   16440                 :             :     {
   16441                 :        6979 :      if (c->ts.u.cl->length == NULL
   16442                 :        6973 :          || (!resolve_charlen(c->ts.u.cl))
   16443                 :       13951 :          || !gfc_is_constant_expr (c->ts.u.cl->length))
   16444                 :             :        {
   16445                 :           9 :          gfc_error ("Character length of component %qs needs to "
   16446                 :             :                     "be a constant specification expression at %L",
   16447                 :             :                     c->name,
   16448                 :           9 :                     c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   16449                 :           9 :          return false;
   16450                 :             :        }
   16451                 :             : 
   16452                 :        6970 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   16453                 :             :        {
   16454                 :           2 :          if (!c->ts.u.cl->length->error)
   16455                 :             :            {
   16456                 :           1 :              gfc_error ("Character length expression of component %qs at %L "
   16457                 :             :                         "must be of INTEGER type, found %s",
   16458                 :           1 :                         c->name, &c->ts.u.cl->length->where,
   16459                 :             :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   16460                 :           1 :              c->ts.u.cl->length->error = 1;
   16461                 :             :            }
   16462                 :           2 :          return false;
   16463                 :             :        }
   16464                 :             :     }
   16465                 :             : 
   16466                 :      257599 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   16467                 :        2135 :       && !c->attr.pointer && !c->attr.allocatable)
   16468                 :             :     {
   16469                 :           1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   16470                 :             :                  "length must be a POINTER or ALLOCATABLE",
   16471                 :             :                  c->name, sym->name, &c->loc);
   16472                 :           1 :       return false;
   16473                 :             :     }
   16474                 :             : 
   16475                 :             :   /* Add the hidden deferred length field.  */
   16476                 :      257598 :   if (c->ts.type == BT_CHARACTER
   16477                 :        9306 :       && (c->ts.deferred || c->attr.pdt_string)
   16478                 :        2270 :       && !c->attr.function
   16479                 :        2234 :       && !sym->attr.is_class)
   16480                 :             :     {
   16481                 :        2087 :       char name[GFC_MAX_SYMBOL_LEN+9];
   16482                 :        2087 :       gfc_component *strlen;
   16483                 :        2087 :       sprintf (name, "_%s_length", c->name);
   16484                 :        2087 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   16485                 :        2087 :       if (strlen == NULL)
   16486                 :             :         {
   16487                 :         442 :           if (!gfc_add_component (sym, name, &strlen))
   16488                 :           0 :             return false;
   16489                 :         442 :           strlen->ts.type = BT_INTEGER;
   16490                 :         442 :           strlen->ts.kind = gfc_charlen_int_kind;
   16491                 :         442 :           strlen->attr.access = ACCESS_PRIVATE;
   16492                 :         442 :           strlen->attr.artificial = 1;
   16493                 :             :         }
   16494                 :             :     }
   16495                 :             : 
   16496                 :      257598 :   if (c->ts.type == BT_DERIVED
   16497                 :       47895 :       && sym->component_access != ACCESS_PRIVATE
   16498                 :       46941 :       && gfc_check_symbol_access (sym)
   16499                 :       92000 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   16500                 :       45948 :       && !c->ts.u.derived->attr.use_assoc
   16501                 :       24379 :       && !gfc_check_symbol_access (c->ts.u.derived)
   16502                 :      257792 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   16503                 :             :                           "PRIVATE type and cannot be a component of "
   16504                 :             :                           "%qs, which is PUBLIC at %L", c->name,
   16505                 :             :                           sym->name, &sym->declared_at))
   16506                 :             :     return false;
   16507                 :             : 
   16508                 :      257597 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   16509                 :             :     {
   16510                 :           2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   16511                 :             :                  "type %s", c->name, &c->loc, sym->name);
   16512                 :           2 :       return false;
   16513                 :             :     }
   16514                 :             : 
   16515                 :      257595 :   if (sym->attr.sequence)
   16516                 :             :     {
   16517                 :        2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   16518                 :             :         {
   16519                 :           0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   16520                 :             :                      "not have the SEQUENCE attribute",
   16521                 :             :                      c->ts.u.derived->name, &sym->declared_at);
   16522                 :           0 :           return false;
   16523                 :             :         }
   16524                 :             :     }
   16525                 :             : 
   16526                 :      257595 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   16527                 :           0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   16528                 :      257595 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   16529                 :        6834 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   16530                 :           0 :     CLASS_DATA (c)->ts.u.derived
   16531                 :           0 :                     = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   16532                 :             : 
   16533                 :             :   /* If an allocatable component derived type is of the same type as
   16534                 :             :      the enclosing derived type, we need a vtable generating so that
   16535                 :             :      the __deallocate procedure is created.  */
   16536                 :      257595 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   16537                 :       54739 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   16538                 :         190 :     gfc_find_vtab (&c->ts);
   16539                 :             : 
   16540                 :             :   /* Ensure that all the derived type components are put on the
   16541                 :             :      derived type list; even in formal namespaces, where derived type
   16542                 :             :      pointer components might not have been declared.  */
   16543                 :      257595 :   if (c->ts.type == BT_DERIVED
   16544                 :       47894 :         && c->ts.u.derived
   16545                 :       47894 :         && c->ts.u.derived->components
   16546                 :       44795 :         && c->attr.pointer
   16547                 :       31442 :         && sym != c->ts.u.derived)
   16548                 :        4150 :     add_dt_to_dt_list (c->ts.u.derived);
   16549                 :             : 
   16550                 :      257595 :   if (c->as && c->as->type != AS_DEFERRED
   16551                 :        5468 :       && (c->attr.pointer || c->attr.allocatable))
   16552                 :             :     return false;
   16553                 :             : 
   16554                 :      257581 :   if (!gfc_resolve_array_spec (c->as,
   16555                 :      257581 :                                !(c->attr.pointer || c->attr.proc_pointer
   16556                 :             :                                  || c->attr.allocatable)))
   16557                 :             :     return false;
   16558                 :             : 
   16559                 :       96134 :   if (c->initializer && !sym->attr.vtype
   16560                 :       27770 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   16561                 :      284180 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   16562                 :             :     return false;
   16563                 :             : 
   16564                 :             :   return true;
   16565                 :             : }
   16566                 :             : 
   16567                 :             : 
   16568                 :             : /* Be nice about the locus for a structure expression - show the locus of the
   16569                 :             :    first non-null sub-expression if we can.  */
   16570                 :             : 
   16571                 :             : static locus *
   16572                 :           4 : cons_where (gfc_expr *struct_expr)
   16573                 :             : {
   16574                 :           4 :   gfc_constructor *cons;
   16575                 :             : 
   16576                 :           4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   16577                 :             : 
   16578                 :           4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   16579                 :          12 :   for (; cons; cons = gfc_constructor_next (cons))
   16580                 :             :     {
   16581                 :           8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   16582                 :           4 :         return &cons->expr->where;
   16583                 :             :     }
   16584                 :             : 
   16585                 :           0 :   return &struct_expr->where;
   16586                 :             : }
   16587                 :             : 
   16588                 :             : /* Resolve the components of a structure type. Much less work than derived
   16589                 :             :    types.  */
   16590                 :             : 
   16591                 :             : static bool
   16592                 :         913 : resolve_fl_struct (gfc_symbol *sym)
   16593                 :             : {
   16594                 :         913 :   gfc_component *c;
   16595                 :         913 :   gfc_expr *init = NULL;
   16596                 :         913 :   bool success;
   16597                 :             : 
   16598                 :             :   /* Make sure UNIONs do not have overlapping initializers.  */
   16599                 :         913 :   if (sym->attr.flavor == FL_UNION)
   16600                 :             :     {
   16601                 :         498 :       for (c = sym->components; c; c = c->next)
   16602                 :             :         {
   16603                 :         331 :           if (init && c->initializer)
   16604                 :             :             {
   16605                 :           2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   16606                 :             :                          cons_where (init), cons_where (c->initializer));
   16607                 :           2 :               gfc_free_expr (c->initializer);
   16608                 :           2 :               c->initializer = NULL;
   16609                 :             :             }
   16610                 :         291 :           if (init == NULL)
   16611                 :         291 :             init = c->initializer;
   16612                 :             :         }
   16613                 :             :     }
   16614                 :             : 
   16615                 :         913 :   success = true;
   16616                 :        2830 :   for (c = sym->components; c; c = c->next)
   16617                 :        1917 :     if (!resolve_component (c, sym))
   16618                 :           0 :       success = false;
   16619                 :             : 
   16620                 :         913 :   if (!success)
   16621                 :             :     return false;
   16622                 :             : 
   16623                 :         913 :   if (sym->components)
   16624                 :         862 :     add_dt_to_dt_list (sym);
   16625                 :             : 
   16626                 :             :   return true;
   16627                 :             : }
   16628                 :             : 
   16629                 :             : /* Figure if the derived type is using itself directly in one of its components
   16630                 :             :    or through referencing other derived types.  The information is required to
   16631                 :             :    generate the __deallocate and __final type bound procedures to ensure
   16632                 :             :    freeing larger hierarchies of derived types with allocatable objects.  */
   16633                 :             : 
   16634                 :             : static void
   16635                 :      127159 : resolve_cyclic_derived_type (gfc_symbol *derived)
   16636                 :             : {
   16637                 :      127159 :   hash_set<gfc_symbol *> seen, to_examin;
   16638                 :      127159 :   gfc_component *c;
   16639                 :      127159 :   seen.add (derived);
   16640                 :      127159 :   to_examin.add (derived);
   16641                 :      426384 :   while (!to_examin.is_empty ())
   16642                 :             :     {
   16643                 :      173957 :       gfc_symbol *cand = *to_examin.begin ();
   16644                 :      173957 :       to_examin.remove (cand);
   16645                 :      468437 :       for (c = cand->components; c; c = c->next)
   16646                 :      296371 :         if (c->ts.type == BT_DERIVED)
   16647                 :             :           {
   16648                 :       65254 :             if (c->ts.u.derived == derived)
   16649                 :             :               {
   16650                 :         951 :                 derived->attr.recursive = 1;
   16651                 :        1891 :                 return;
   16652                 :             :               }
   16653                 :       64303 :             else if (!seen.contains (c->ts.u.derived))
   16654                 :             :               {
   16655                 :       42516 :                 seen.add (c->ts.u.derived);
   16656                 :       42516 :                 to_examin.add (c->ts.u.derived);
   16657                 :             :               }
   16658                 :             :           }
   16659                 :      231117 :         else if (c->ts.type == BT_CLASS)
   16660                 :             :           {
   16661                 :        8641 :             if (!c->attr.class_ok)
   16662                 :           7 :               continue;
   16663                 :        8634 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   16664                 :             :               {
   16665                 :         940 :                 derived->attr.recursive = 1;
   16666                 :         940 :                 return;
   16667                 :             :               }
   16668                 :        7694 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   16669                 :             :               {
   16670                 :        4493 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   16671                 :        4493 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   16672                 :             :               }
   16673                 :             :           }
   16674                 :             :     }
   16675                 :      127159 : }
   16676                 :             : 
   16677                 :             : /* Resolve the components of a derived type. This does not have to wait until
   16678                 :             :    resolution stage, but can be done as soon as the dt declaration has been
   16679                 :             :    parsed.  */
   16680                 :             : 
   16681                 :             : static bool
   16682                 :      157218 : resolve_fl_derived0 (gfc_symbol *sym)
   16683                 :             : {
   16684                 :      157218 :   gfc_symbol* super_type;
   16685                 :      157218 :   gfc_component *c;
   16686                 :      157218 :   gfc_formal_arglist *f;
   16687                 :      157218 :   bool success;
   16688                 :             : 
   16689                 :      157218 :   if (sym->attr.unlimited_polymorphic)
   16690                 :             :     return true;
   16691                 :             : 
   16692                 :      157218 :   super_type = gfc_get_derived_super_type (sym);
   16693                 :             : 
   16694                 :             :   /* F2008, C432.  */
   16695                 :      157218 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   16696                 :             :     {
   16697                 :           2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   16698                 :             :                  "parent type %qs shall also have one", sym->name,
   16699                 :             :                  &sym->declared_at, super_type->name);
   16700                 :           2 :       return false;
   16701                 :             :     }
   16702                 :             : 
   16703                 :             :   /* Ensure the extended type gets resolved before we do.  */
   16704                 :       16361 :   if (super_type && !resolve_fl_derived0 (super_type))
   16705                 :             :     return false;
   16706                 :             : 
   16707                 :             :   /* An ABSTRACT type must be extensible.  */
   16708                 :      157210 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   16709                 :             :     {
   16710                 :           2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   16711                 :             :                  sym->name, &sym->declared_at);
   16712                 :           2 :       return false;
   16713                 :             :     }
   16714                 :             : 
   16715                 :             :   /* Resolving components below, may create vtabs for which the cyclic type
   16716                 :             :      information needs to be present.  */
   16717                 :      157208 :   if (!sym->attr.vtype)
   16718                 :      127159 :     resolve_cyclic_derived_type (sym);
   16719                 :             : 
   16720                 :      157208 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   16721                 :             :                            : sym->components;
   16722                 :             : 
   16723                 :             :   success = true;
   16724                 :      535180 :   for ( ; c != NULL; c = c->next)
   16725                 :      377972 :     if (!resolve_component (c, sym))
   16726                 :          83 :       success = false;
   16727                 :             : 
   16728                 :      157208 :   if (!success)
   16729                 :             :     return false;
   16730                 :             : 
   16731                 :             :   /* Now add the caf token field, where needed.  */
   16732                 :      157134 :   if (flag_coarray != GFC_FCOARRAY_NONE
   16733                 :        3193 :       && !sym->attr.is_class && !sym->attr.vtype)
   16734                 :             :     {
   16735                 :        4867 :       for (c = sym->components; c; c = c->next)
   16736                 :        3007 :         if (!c->attr.dimension && !c->attr.codimension
   16737                 :        2253 :             && (c->attr.allocatable || c->attr.pointer))
   16738                 :             :           {
   16739                 :         616 :             char name[GFC_MAX_SYMBOL_LEN+9];
   16740                 :         616 :             gfc_component *token;
   16741                 :         616 :             sprintf (name, "_caf_%s", c->name);
   16742                 :         616 :             token = gfc_find_component (sym, name, true, true, NULL);
   16743                 :         616 :             if (token == NULL)
   16744                 :             :               {
   16745                 :         136 :                 if (!gfc_add_component (sym, name, &token))
   16746                 :           0 :                   return false;
   16747                 :         136 :                 token->ts.type = BT_VOID;
   16748                 :         136 :                 token->ts.kind = gfc_default_integer_kind;
   16749                 :         136 :                 token->attr.access = ACCESS_PRIVATE;
   16750                 :         136 :                 token->attr.artificial = 1;
   16751                 :         136 :                 token->attr.caf_token = 1;
   16752                 :             :               }
   16753                 :         616 :             c->caf_token = token;
   16754                 :             :           }
   16755                 :             :     }
   16756                 :             : 
   16757                 :      157134 :   check_defined_assignments (sym);
   16758                 :             : 
   16759                 :      157134 :   if (!sym->attr.defined_assign_comp && super_type)
   16760                 :       15537 :     sym->attr.defined_assign_comp
   16761                 :       15537 :                         = super_type->attr.defined_assign_comp;
   16762                 :             : 
   16763                 :             :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   16764                 :             :      all DEFERRED bindings are overridden.  */
   16765                 :       16354 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   16766                 :        1345 :       && !sym->attr.is_class
   16767                 :        2852 :       && !ensure_not_abstract (sym, super_type))
   16768                 :             :     return false;
   16769                 :             : 
   16770                 :             :   /* Check that there is a component for every PDT parameter.  */
   16771                 :      157129 :   if (sym->attr.pdt_template)
   16772                 :             :     {
   16773                 :         970 :       for (f = sym->formal; f; f = f->next)
   16774                 :             :         {
   16775                 :         642 :           if (!f->sym)
   16776                 :           1 :             continue;
   16777                 :         641 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   16778                 :         641 :           if (c == NULL)
   16779                 :             :             {
   16780                 :           9 :               gfc_error ("Parameterized type %qs does not have a component "
   16781                 :             :                          "corresponding to parameter %qs at %L", sym->name,
   16782                 :           9 :                          f->sym->name, &sym->declared_at);
   16783                 :           9 :               break;
   16784                 :             :             }
   16785                 :             :         }
   16786                 :             :     }
   16787                 :             : 
   16788                 :             :   /* Add derived type to the derived type list.  */
   16789                 :      157129 :   add_dt_to_dt_list (sym);
   16790                 :             : 
   16791                 :      157129 :   return true;
   16792                 :             : }
   16793                 :             : 
   16794                 :             : /* The following procedure does the full resolution of a derived type,
   16795                 :             :    including resolution of all type-bound procedures (if present). In contrast
   16796                 :             :    to 'resolve_fl_derived0' this can only be done after the module has been
   16797                 :             :    parsed completely.  */
   16798                 :             : 
   16799                 :             : static bool
   16800                 :       81071 : resolve_fl_derived (gfc_symbol *sym)
   16801                 :             : {
   16802                 :       81071 :   gfc_symbol *gen_dt = NULL;
   16803                 :             : 
   16804                 :       81071 :   if (sym->attr.unlimited_polymorphic)
   16805                 :             :     return true;
   16806                 :             : 
   16807                 :       81071 :   if (!sym->attr.is_class)
   16808                 :       69248 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   16809                 :       52903 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   16810                 :        2169 :       && (!gen_dt->generic->sym->attr.use_assoc
   16811                 :        2069 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   16812                 :       81204 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   16813                 :             :                           "%qs at %L being the same name as derived "
   16814                 :             :                           "type at %L", sym->name,
   16815                 :             :                           gen_dt->generic->sym == sym
   16816                 :          11 :                           ? gen_dt->generic->next->sym->name
   16817                 :             :                           : gen_dt->generic->sym->name,
   16818                 :             :                           gen_dt->generic->sym == sym
   16819                 :          11 :                           ? &gen_dt->generic->next->sym->declared_at
   16820                 :             :                           : &gen_dt->generic->sym->declared_at,
   16821                 :             :                           &sym->declared_at))
   16822                 :             :     return false;
   16823                 :             : 
   16824                 :       81067 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   16825                 :             :     {
   16826                 :          13 :       gfc_error ("Derived type %qs at %L has not been declared",
   16827                 :             :                   sym->name, &sym->declared_at);
   16828                 :          13 :       return false;
   16829                 :             :     }
   16830                 :             : 
   16831                 :             :   /* Resolve the finalizer procedures.  */
   16832                 :       81054 :   if (!gfc_resolve_finalizers (sym, NULL))
   16833                 :             :     return false;
   16834                 :             : 
   16835                 :       81051 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   16836                 :             :     {
   16837                 :             :       /* Fix up incomplete CLASS symbols.  */
   16838                 :       11823 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   16839                 :       11823 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   16840                 :             : 
   16841                 :             :       /* Nothing more to do for unlimited polymorphic entities.  */
   16842                 :       11823 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   16843                 :             :         {
   16844                 :        1907 :           add_dt_to_dt_list (sym);
   16845                 :        1907 :           return true;
   16846                 :             :         }
   16847                 :        9916 :       else if (vptr->ts.u.derived == NULL)
   16848                 :             :         {
   16849                 :        5953 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   16850                 :        5953 :           gcc_assert (vtab);
   16851                 :        5953 :           vptr->ts.u.derived = vtab->ts.u.derived;
   16852                 :        5953 :           if (!resolve_fl_derived0 (vptr->ts.u.derived))
   16853                 :             :             return false;
   16854                 :             :         }
   16855                 :             :     }
   16856                 :             : 
   16857                 :       79144 :   if (!resolve_fl_derived0 (sym))
   16858                 :             :     return false;
   16859                 :             : 
   16860                 :             :   /* Resolve the type-bound procedures.  */
   16861                 :       79066 :   if (!resolve_typebound_procedures (sym))
   16862                 :             :     return false;
   16863                 :             : 
   16864                 :             :   /* Generate module vtables subject to their accessibility and their not
   16865                 :             :      being vtables or pdt templates. If this is not done class declarations
   16866                 :             :      in external procedures wind up with their own version and so SELECT TYPE
   16867                 :             :      fails because the vptrs do not have the same address.  */
   16868                 :       79025 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   16869                 :       78964 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   16870                 :       58964 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   16871                 :       20104 :       && sym->attr.access != ACCESS_PRIVATE
   16872                 :       20071 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   16873                 :             :     {
   16874                 :       18226 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   16875                 :       18226 :       gfc_set_sym_referenced (vtab);
   16876                 :             :     }
   16877                 :             : 
   16878                 :             :   return true;
   16879                 :             : }
   16880                 :             : 
   16881                 :             : 
   16882                 :             : static bool
   16883                 :         805 : resolve_fl_namelist (gfc_symbol *sym)
   16884                 :             : {
   16885                 :         805 :   gfc_namelist *nl;
   16886                 :         805 :   gfc_symbol *nlsym;
   16887                 :             : 
   16888                 :        2892 :   for (nl = sym->namelist; nl; nl = nl->next)
   16889                 :             :     {
   16890                 :             :       /* Check again, the check in match only works if NAMELIST comes
   16891                 :             :          after the decl.  */
   16892                 :        2092 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   16893                 :             :         {
   16894                 :           1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   16895                 :             :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   16896                 :           1 :           return false;
   16897                 :             :         }
   16898                 :             : 
   16899                 :         652 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   16900                 :        2099 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   16901                 :             :                               "with assumed shape in namelist %qs at %L",
   16902                 :             :                               nl->sym->name, sym->name, &sym->declared_at))
   16903                 :             :         return false;
   16904                 :             : 
   16905                 :        2090 :       if (is_non_constant_shape_array (nl->sym)
   16906                 :        2140 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   16907                 :             :                               "with nonconstant shape in namelist %qs at %L",
   16908                 :          50 :                               nl->sym->name, sym->name, &sym->declared_at))
   16909                 :             :         return false;
   16910                 :             : 
   16911                 :        2089 :       if (nl->sym->ts.type == BT_CHARACTER
   16912                 :         565 :           && (nl->sym->ts.u.cl->length == NULL
   16913                 :         526 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   16914                 :        2171 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   16915                 :             :                               "nonconstant character length in "
   16916                 :          82 :                               "namelist %qs at %L", nl->sym->name,
   16917                 :             :                               sym->name, &sym->declared_at))
   16918                 :             :         return false;
   16919                 :             : 
   16920                 :             :     }
   16921                 :             : 
   16922                 :             :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   16923                 :         800 :   if (gfc_check_symbol_access (sym))
   16924                 :             :     {
   16925                 :        2873 :       for (nl = sym->namelist; nl; nl = nl->next)
   16926                 :             :         {
   16927                 :        2086 :           if (!nl->sym->attr.use_assoc
   16928                 :        3948 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   16929                 :        4038 :               && !gfc_check_symbol_access (nl->sym))
   16930                 :             :             {
   16931                 :           2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   16932                 :             :                          "cannot be member of PUBLIC namelist %qs at %L",
   16933                 :           2 :                          nl->sym->name, sym->name, &sym->declared_at);
   16934                 :           2 :               return false;
   16935                 :             :             }
   16936                 :             : 
   16937                 :        2084 :           if (nl->sym->ts.type == BT_DERIVED
   16938                 :         466 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   16939                 :         466 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   16940                 :             :            {
   16941                 :           5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   16942                 :             :                                   "namelist %qs at %L with ALLOCATABLE "
   16943                 :             :                                   "or POINTER components", nl->sym->name,
   16944                 :             :                                   sym->name, &sym->declared_at))
   16945                 :             :                return false;
   16946                 :             :              return true;
   16947                 :             :            }
   16948                 :             : 
   16949                 :             :           /* Types with private components that came here by USE-association.  */
   16950                 :        2079 :           if (nl->sym->ts.type == BT_DERIVED
   16951                 :        2079 :               && derived_inaccessible (nl->sym->ts.u.derived))
   16952                 :             :             {
   16953                 :           6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   16954                 :             :                          "components and cannot be member of namelist %qs at %L",
   16955                 :             :                          nl->sym->name, sym->name, &sym->declared_at);
   16956                 :           6 :               return false;
   16957                 :             :             }
   16958                 :             : 
   16959                 :             :           /* Types with private components that are defined in the same module.  */
   16960                 :        2073 :           if (nl->sym->ts.type == BT_DERIVED
   16961                 :         910 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   16962                 :        2351 :               && nl->sym->ts.u.derived->attr.private_comp)
   16963                 :             :             {
   16964                 :           0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   16965                 :             :                          "cannot be a member of PUBLIC namelist %qs at %L",
   16966                 :             :                          nl->sym->name, sym->name, &sym->declared_at);
   16967                 :           0 :               return false;
   16968                 :             :             }
   16969                 :             :         }
   16970                 :             :     }
   16971                 :             : 
   16972                 :             : 
   16973                 :             :   /* 14.1.2 A module or internal procedure represent local entities
   16974                 :             :      of the same type as a namelist member and so are not allowed.  */
   16975                 :        2857 :   for (nl = sym->namelist; nl; nl = nl->next)
   16976                 :             :     {
   16977                 :        2073 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   16978                 :        1514 :         continue;
   16979                 :             : 
   16980                 :         559 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   16981                 :           7 :         if ((nl->sym == sym->ns->proc_name)
   16982                 :           1 :                ||
   16983                 :           1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   16984                 :           6 :           continue;
   16985                 :             : 
   16986                 :         553 :       nlsym = NULL;
   16987                 :         553 :       if (nl->sym->name)
   16988                 :         553 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   16989                 :         553 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   16990                 :             :         {
   16991                 :           3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   16992                 :             :                      "attribute in %qs at %L", nlsym->name,
   16993                 :             :                      &sym->declared_at);
   16994                 :           3 :           return false;
   16995                 :             :         }
   16996                 :             :     }
   16997                 :             : 
   16998                 :             :   return true;
   16999                 :             : }
   17000                 :             : 
   17001                 :             : 
   17002                 :             : static bool
   17003                 :      367508 : resolve_fl_parameter (gfc_symbol *sym)
   17004                 :             : {
   17005                 :             :   /* A parameter array's shape needs to be constant.  */
   17006                 :      367508 :   if (sym->as != NULL
   17007                 :      367508 :       && (sym->as->type == AS_DEFERRED
   17008                 :        5978 :           || is_non_constant_shape_array (sym)))
   17009                 :             :     {
   17010                 :          17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   17011                 :             :                  "or of deferred shape", sym->name, &sym->declared_at);
   17012                 :          17 :       return false;
   17013                 :             :     }
   17014                 :             : 
   17015                 :             :   /* Constraints on deferred type parameter.  */
   17016                 :      367491 :   if (!deferred_requirements (sym))
   17017                 :             :     return false;
   17018                 :             : 
   17019                 :             :   /* Make sure a parameter that has been implicitly typed still
   17020                 :             :      matches the implicit type, since PARAMETER statements can precede
   17021                 :             :      IMPLICIT statements.  */
   17022                 :      367490 :   if (sym->attr.implicit_type
   17023                 :      367490 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   17024                 :             :                                                              sym->ns)))
   17025                 :             :     {
   17026                 :           0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   17027                 :             :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   17028                 :           0 :       return false;
   17029                 :             :     }
   17030                 :             : 
   17031                 :             :   /* Make sure the types of derived parameters are consistent.  This
   17032                 :             :      type checking is deferred until resolution because the type may
   17033                 :             :      refer to a derived type from the host.  */
   17034                 :      367490 :   if (sym->ts.type == BT_DERIVED
   17035                 :      367490 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   17036                 :             :     {
   17037                 :           0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   17038                 :           0 :                  &sym->value->where);
   17039                 :           0 :       return false;
   17040                 :             :     }
   17041                 :             : 
   17042                 :             :   /* F03:C509,C514.  */
   17043                 :      367490 :   if (sym->ts.type == BT_CLASS)
   17044                 :             :     {
   17045                 :           0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   17046                 :             :                  sym->name, &sym->declared_at);
   17047                 :           0 :       return false;
   17048                 :             :     }
   17049                 :             : 
   17050                 :             :   return true;
   17051                 :             : }
   17052                 :             : 
   17053                 :             : 
   17054                 :             : /* Called by resolve_symbol to check PDTs.  */
   17055                 :             : 
   17056                 :             : static void
   17057                 :         522 : resolve_pdt (gfc_symbol* sym)
   17058                 :             : {
   17059                 :         522 :   gfc_symbol *derived = NULL;
   17060                 :         522 :   gfc_actual_arglist *param;
   17061                 :         522 :   gfc_component *c;
   17062                 :         522 :   bool const_len_exprs = true;
   17063                 :         522 :   bool assumed_len_exprs = false;
   17064                 :         522 :   symbol_attribute *attr;
   17065                 :             : 
   17066                 :         522 :   if (sym->ts.type == BT_DERIVED)
   17067                 :             :     {
   17068                 :         462 :       derived = sym->ts.u.derived;
   17069                 :         462 :       attr = &(sym->attr);
   17070                 :             :     }
   17071                 :          60 :   else if (sym->ts.type == BT_CLASS)
   17072                 :             :     {
   17073                 :          60 :       derived = CLASS_DATA (sym)->ts.u.derived;
   17074                 :          60 :       attr = &(CLASS_DATA (sym)->attr);
   17075                 :             :     }
   17076                 :             :   else
   17077                 :           0 :     gcc_unreachable ();
   17078                 :             : 
   17079                 :         522 :   gcc_assert (derived->attr.pdt_type);
   17080                 :             : 
   17081                 :        1383 :   for (param = sym->param_list; param; param = param->next)
   17082                 :             :     {
   17083                 :         861 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   17084                 :         861 :       gcc_assert (c);
   17085                 :         861 :       if (c->attr.pdt_kind)
   17086                 :         386 :         continue;
   17087                 :             : 
   17088                 :         292 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   17089                 :         502 :           && c->attr.pdt_len)
   17090                 :             :         const_len_exprs = false;
   17091                 :         448 :       else if (param->spec_type == SPEC_ASSUMED)
   17092                 :         142 :         assumed_len_exprs = true;
   17093                 :             : 
   17094                 :         475 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   17095                 :          10 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   17096                 :           8 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   17097                 :           3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   17098                 :             :                    "parameter %qs and requires either the POINTER "
   17099                 :             :                    "or ALLOCATABLE attribute",
   17100                 :             :                    sym->name, &sym->declared_at,
   17101                 :             :                    param->name);
   17102                 :             : 
   17103                 :             :     }
   17104                 :             : 
   17105                 :         522 :   if (!const_len_exprs
   17106                 :          27 :       && (sym->ns->proc_name->attr.is_main_program
   17107                 :          26 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   17108                 :          25 :           || sym->attr.save != SAVE_NONE))
   17109                 :           2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   17110                 :             :                "SAVE attribute or be a variable declared in the "
   17111                 :             :                "main program, a module or a submodule(F08/C513)",
   17112                 :             :                sym->name, &sym->declared_at);
   17113                 :             : 
   17114                 :         522 :   if (assumed_len_exprs && !(sym->attr.dummy
   17115                 :             :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   17116                 :           1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   17117                 :             :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   17118                 :             :                sym->name, &sym->declared_at);
   17119                 :         522 : }
   17120                 :             : 
   17121                 :             : 
   17122                 :             : /* Resolve the symbol's array spec.  */
   17123                 :             : 
   17124                 :             : static bool
   17125                 :     1609911 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   17126                 :             : {
   17127                 :     1609911 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   17128                 :     1609911 :   gfc_current_ns = gfc_get_spec_ns (sym);
   17129                 :             : 
   17130                 :     1609911 :   bool saved_specification_expr = specification_expr;
   17131                 :     1609911 :   specification_expr = true;
   17132                 :             : 
   17133                 :     1609911 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   17134                 :             : 
   17135                 :     1609911 :   specification_expr = saved_specification_expr;
   17136                 :     1609911 :   gfc_current_ns = orig_current_ns;
   17137                 :             : 
   17138                 :     1609911 :   return result;
   17139                 :             : }
   17140                 :             : 
   17141                 :             : 
   17142                 :             : /* Do anything necessary to resolve a symbol.  Right now, we just
   17143                 :             :    assume that an otherwise unknown symbol is a variable.  This sort
   17144                 :             :    of thing commonly happens for symbols in module.  */
   17145                 :             : 
   17146                 :             : static void
   17147                 :     1730668 : resolve_symbol (gfc_symbol *sym)
   17148                 :             : {
   17149                 :     1730668 :   int check_constant, mp_flag;
   17150                 :     1730668 :   gfc_symtree *symtree;
   17151                 :     1730668 :   gfc_symtree *this_symtree;
   17152                 :     1730668 :   gfc_namespace *ns;
   17153                 :     1730668 :   gfc_component *c;
   17154                 :     1730668 :   symbol_attribute class_attr;
   17155                 :     1730668 :   gfc_array_spec *as;
   17156                 :             : 
   17157                 :     1730668 :   if (sym->resolve_symbol_called >= 1)
   17158                 :      151813 :     return;
   17159                 :     1664692 :   sym->resolve_symbol_called = 1;
   17160                 :             : 
   17161                 :             :   /* No symbol will ever have union type; only components can be unions.
   17162                 :             :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   17163                 :             :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   17164                 :     1664692 :   gcc_assert (sym->ts.type != BT_UNION);
   17165                 :             : 
   17166                 :             :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   17167                 :             :      yet unsupported for -fcoarray=lib.  */
   17168                 :     1664692 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   17169                 :          86 :       && sym->ts.u.derived && CLASS_DATA (sym)
   17170                 :          86 :       && CLASS_DATA (sym)->attr.codimension
   17171                 :          72 :       && CLASS_DATA (sym)->ts.u.derived
   17172                 :          71 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   17173                 :          71 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   17174                 :             :     {
   17175                 :           6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   17176                 :             :                  "type coarrays at %L are unsupported", &sym->declared_at);
   17177                 :           6 :       return;
   17178                 :             :     }
   17179                 :             : 
   17180                 :     1664686 :   if (sym->attr.artificial)
   17181                 :             :     return;
   17182                 :             : 
   17183                 :     1581366 :   if (sym->attr.unlimited_polymorphic)
   17184                 :             :     return;
   17185                 :             : 
   17186                 :     1580001 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   17187                 :             :     {
   17188                 :           4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   17189                 :             :                  "the OpenMP DEPEND clause", &sym->declared_at);
   17190                 :           4 :       return;
   17191                 :             :     }
   17192                 :             : 
   17193                 :     1579997 :   if (sym->attr.flavor == FL_UNKNOWN
   17194                 :     1559511 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   17195                 :      452898 :           && !sym->attr.generic && !sym->attr.external
   17196                 :      173911 :           && sym->attr.if_source == IFSRC_UNKNOWN
   17197                 :       77649 :           && sym->ts.type == BT_UNKNOWN))
   17198                 :             :     {
   17199                 :             :       /* A symbol in a common block might not have been resolved yet properly.
   17200                 :             :          Do not try to find an interface with the same name.  */
   17201                 :       89965 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   17202                 :             :           && !sym->attr.generic && !sym->attr.external
   17203                 :       20486 :           && sym->attr.in_common)
   17204                 :        2530 :         goto skip_interfaces;
   17205                 :             : 
   17206                 :             :     /* If we find that a flavorless symbol is an interface in one of the
   17207                 :             :        parent namespaces, find its symtree in this namespace, free the
   17208                 :             :        symbol and set the symtree to point to the interface symbol.  */
   17209                 :      125012 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   17210                 :             :         {
   17211                 :       38212 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   17212                 :       38212 :           if (symtree && (symtree->n.sym->generic ||
   17213                 :         672 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   17214                 :         585 :                            && sym->ns->construct_entities)))
   17215                 :             :             {
   17216                 :         643 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   17217                 :             :                                                sym->name);
   17218                 :         643 :               if (this_symtree->n.sym == sym)
   17219                 :             :                 {
   17220                 :         635 :                   symtree->n.sym->refs++;
   17221                 :         635 :                   gfc_release_symbol (sym);
   17222                 :         635 :                   this_symtree->n.sym = symtree->n.sym;
   17223                 :         635 :                   return;
   17224                 :             :                 }
   17225                 :             :             }
   17226                 :             :         }
   17227                 :             : 
   17228                 :       86800 : skip_interfaces:
   17229                 :             :       /* Otherwise give it a flavor according to such attributes as
   17230                 :             :          it has.  */
   17231                 :       89330 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   17232                 :       20358 :           && sym->attr.intrinsic == 0)
   17233                 :       20304 :         sym->attr.flavor = FL_VARIABLE;
   17234                 :       69026 :       else if (sym->attr.flavor == FL_UNKNOWN)
   17235                 :             :         {
   17236                 :          54 :           sym->attr.flavor = FL_PROCEDURE;
   17237                 :          54 :           if (sym->attr.dimension)
   17238                 :           0 :             sym->attr.function = 1;
   17239                 :             :         }
   17240                 :             :     }
   17241                 :             : 
   17242                 :     1579362 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   17243                 :        2303 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   17244                 :             : 
   17245                 :        1403 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   17246                 :     1580765 :       && !resolve_procedure_interface (sym))
   17247                 :             :     return;
   17248                 :             : 
   17249                 :     1579351 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   17250                 :         130 :       && (sym->attr.procedure || sym->attr.external))
   17251                 :             :     {
   17252                 :           0 :       if (sym->attr.external)
   17253                 :           0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   17254                 :             :                    "at %L", &sym->declared_at);
   17255                 :             :       else
   17256                 :           0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   17257                 :             :                    "at %L", &sym->declared_at);
   17258                 :             : 
   17259                 :           0 :       return;
   17260                 :             :     }
   17261                 :             : 
   17262                 :             :   /* Ensure that variables of derived or class type having a finalizer are
   17263                 :             :      marked used even when the variable is not used anything else in the scope.
   17264                 :             :      This fixes PR118730.  */
   17265                 :     1579351 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   17266                 :      415155 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   17267                 :     1624293 :       && gfc_may_be_finalized (sym->ts))
   17268                 :        7957 :     gfc_set_sym_referenced (sym);
   17269                 :             : 
   17270                 :     1579351 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   17271                 :             :     return;
   17272                 :             : 
   17273                 :     1578581 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   17274                 :     1579344 :            && !resolve_fl_struct (sym))
   17275                 :             :     return;
   17276                 :             : 
   17277                 :             :   /* Symbols that are module procedures with results (functions) have
   17278                 :             :      the types and array specification copied for type checking in
   17279                 :             :      procedures that call them, as well as for saving to a module
   17280                 :             :      file.  These symbols can't stand the scrutiny that their results
   17281                 :             :      can.  */
   17282                 :     1579212 :   mp_flag = (sym->result != NULL && sym->result != sym);
   17283                 :             : 
   17284                 :             :   /* Make sure that the intrinsic is consistent with its internal
   17285                 :             :      representation. This needs to be done before assigning a default
   17286                 :             :      type to avoid spurious warnings.  */
   17287                 :     1547045 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   17288                 :     1610259 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   17289                 :             :     return;
   17290                 :             : 
   17291                 :             :   /* Resolve associate names.  */
   17292                 :     1579183 :   if (sym->assoc)
   17293                 :        6370 :     resolve_assoc_var (sym, true);
   17294                 :             : 
   17295                 :             :   /* Assign default type to symbols that need one and don't have one.  */
   17296                 :     1579183 :   if (sym->ts.type == BT_UNKNOWN)
   17297                 :             :     {
   17298                 :      378183 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   17299                 :             :         {
   17300                 :       11612 :           gfc_set_default_type (sym, 1, NULL);
   17301                 :             :         }
   17302                 :             : 
   17303                 :      378183 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   17304                 :      243184 :           && !sym->attr.function && !sym->attr.subroutine
   17305                 :      379796 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   17306                 :         564 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   17307                 :             : 
   17308                 :      378183 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   17309                 :             :         {
   17310                 :             :           /* The specific case of an external procedure should emit an error
   17311                 :             :              in the case that there is no implicit type.  */
   17312                 :       97350 :           if (!mp_flag)
   17313                 :             :             {
   17314                 :       91648 :               if (!sym->attr.mixed_entry_master)
   17315                 :       91542 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   17316                 :             :             }
   17317                 :             :           else
   17318                 :             :             {
   17319                 :             :               /* Result may be in another namespace.  */
   17320                 :        5702 :               resolve_symbol (sym->result);
   17321                 :             : 
   17322                 :        5702 :               if (!sym->result->attr.proc_pointer)
   17323                 :             :                 {
   17324                 :        5537 :                   sym->ts = sym->result->ts;
   17325                 :        5537 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   17326                 :        5537 :                   sym->attr.dimension = sym->result->attr.dimension;
   17327                 :        5537 :                   sym->attr.codimension = sym->result->attr.codimension;
   17328                 :        5537 :                   sym->attr.pointer = sym->result->attr.pointer;
   17329                 :        5537 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   17330                 :        5537 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   17331                 :             :                 }
   17332                 :             :             }
   17333                 :             :         }
   17334                 :             :     }
   17335                 :     1201000 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   17336                 :       31053 :     resolve_symbol_array_spec (sym->result, false);
   17337                 :             : 
   17338                 :             :   /* For a CLASS-valued function with a result variable, affirm that it has
   17339                 :             :      been resolved also when looking at the symbol 'sym'.  */
   17340                 :      409236 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   17341                 :         687 :     sym->attr.class_ok = sym->result->attr.class_ok;
   17342                 :             : 
   17343                 :     1579183 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   17344                 :       18302 :       && CLASS_DATA (sym))
   17345                 :             :     {
   17346                 :       18301 :       as = CLASS_DATA (sym)->as;
   17347                 :       18301 :       class_attr = CLASS_DATA (sym)->attr;
   17348                 :       18301 :       class_attr.pointer = class_attr.class_pointer;
   17349                 :             :     }
   17350                 :             :   else
   17351                 :             :     {
   17352                 :     1560882 :       class_attr = sym->attr;
   17353                 :     1560882 :       as = sym->as;
   17354                 :             :     }
   17355                 :             : 
   17356                 :             :   /* F2008, C530.  */
   17357                 :     1579183 :   if (sym->attr.contiguous
   17358                 :        6852 :       && (!class_attr.dimension
   17359                 :        6849 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   17360                 :         123 :               && !class_attr.pointer)))
   17361                 :             :     {
   17362                 :           7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   17363                 :             :                  "array pointer or an assumed-shape or assumed-rank array",
   17364                 :             :                  sym->name, &sym->declared_at);
   17365                 :           7 :       return;
   17366                 :             :     }
   17367                 :             : 
   17368                 :             :   /* Assumed size arrays and assumed shape arrays must be dummy
   17369                 :             :      arguments.  Array-spec's of implied-shape should have been resolved to
   17370                 :             :      AS_EXPLICIT already.  */
   17371                 :             : 
   17372                 :     1572450 :   if (as)
   17373                 :             :     {
   17374                 :             :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   17375                 :             :          specification expression.  */
   17376                 :      136736 :       if (as->type == AS_IMPLIED_SHAPE)
   17377                 :             :         {
   17378                 :             :           int i;
   17379                 :           1 :           for (i=0; i<as->rank; i++)
   17380                 :             :             {
   17381                 :           1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   17382                 :             :                 {
   17383                 :           1 :                   gfc_error ("Bad specification for assumed size array at %L",
   17384                 :             :                              &as->lower[i]->where);
   17385                 :           1 :                   return;
   17386                 :             :                 }
   17387                 :             :             }
   17388                 :           0 :           gcc_unreachable();
   17389                 :             :         }
   17390                 :             : 
   17391                 :      136735 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   17392                 :      107011 :            || as->type == AS_ASSUMED_SHAPE)
   17393                 :       40945 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   17394                 :          59 :           && !sym->attr.associate_var)
   17395                 :             :         {
   17396                 :           7 :           if (as->type == AS_ASSUMED_SIZE)
   17397                 :           7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   17398                 :             :                        &sym->declared_at);
   17399                 :             :           else
   17400                 :           0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   17401                 :             :                        &sym->declared_at);
   17402                 :           7 :           return;
   17403                 :             :         }
   17404                 :             :       /* TS 29113, C535a.  */
   17405                 :      136728 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   17406                 :          60 :           && !sym->attr.select_type_temporary
   17407                 :          60 :           && !(cs_base && cs_base->current
   17408                 :          45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   17409                 :           3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   17410                 :           0 :                         && cs_base->current->op == EXEC_BLOCK))))
   17411                 :             :         {
   17412                 :          18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   17413                 :             :                      &sym->declared_at);
   17414                 :          18 :           return;
   17415                 :             :         }
   17416                 :      136710 :       if (as->type == AS_ASSUMED_RANK
   17417                 :       23437 :           && (sym->attr.codimension || sym->attr.value))
   17418                 :             :         {
   17419                 :           2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   17420                 :             :                      "CODIMENSION attribute", &sym->declared_at);
   17421                 :           2 :           return;
   17422                 :             :         }
   17423                 :             :     }
   17424                 :             : 
   17425                 :             :   /* Make sure symbols with known intent or optional are really dummy
   17426                 :             :      variable.  Because of ENTRY statement, this has to be deferred
   17427                 :             :      until resolution time.  */
   17428                 :             : 
   17429                 :     1579148 :   if (!sym->attr.dummy
   17430                 :     1141931 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   17431                 :             :     {
   17432                 :           2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   17433                 :           2 :       return;
   17434                 :             :     }
   17435                 :             : 
   17436                 :     1579146 :   if (sym->attr.value && !sym->attr.dummy)
   17437                 :             :     {
   17438                 :           2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   17439                 :             :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   17440                 :           2 :       return;
   17441                 :             :     }
   17442                 :             : 
   17443                 :     1579144 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   17444                 :             :     {
   17445                 :         546 :       gfc_charlen *cl = sym->ts.u.cl;
   17446                 :         546 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   17447                 :             :         {
   17448                 :           2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   17449                 :             :                      "attribute must have constant length",
   17450                 :             :                      sym->name, &sym->declared_at);
   17451                 :           2 :           return;
   17452                 :             :         }
   17453                 :             : 
   17454                 :         544 :       if (sym->ts.is_c_interop
   17455                 :         376 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   17456                 :             :         {
   17457                 :           1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   17458                 :             :                      "with VALUE attribute must have length one",
   17459                 :             :                      sym->name, &sym->declared_at);
   17460                 :           1 :           return;
   17461                 :             :         }
   17462                 :             :     }
   17463                 :             : 
   17464                 :     1579141 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   17465                 :      111629 :       && sym->ts.u.derived->attr.generic)
   17466                 :             :     {
   17467                 :          20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   17468                 :          20 :       if (!sym->ts.u.derived)
   17469                 :             :         {
   17470                 :           0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   17471                 :             :                      "which has not been defined", sym->name,
   17472                 :             :                      &sym->declared_at, sym->ts.u.derived->name);
   17473                 :           0 :           sym->ts.type = BT_UNKNOWN;
   17474                 :           0 :           return;
   17475                 :             :         }
   17476                 :             :     }
   17477                 :             : 
   17478                 :             :     /* Use the same constraints as TYPE(*), except for the type check
   17479                 :             :        and that only scalars and assumed-size arrays are permitted.  */
   17480                 :     1579141 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   17481                 :             :       {
   17482                 :       11264 :         if (!sym->attr.dummy)
   17483                 :             :           {
   17484                 :           1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   17485                 :             :                        "a dummy argument", sym->name, &sym->declared_at);
   17486                 :           1 :             return;
   17487                 :             :           }
   17488                 :             : 
   17489                 :       11263 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   17490                 :           8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   17491                 :           0 :             && sym->ts.type != BT_COMPLEX)
   17492                 :             :           {
   17493                 :           0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   17494                 :             :                        "of type TYPE(*) or of an numeric intrinsic type",
   17495                 :             :                        sym->name, &sym->declared_at);
   17496                 :           0 :             return;
   17497                 :             :           }
   17498                 :             : 
   17499                 :       11263 :       if (sym->attr.allocatable || sym->attr.codimension
   17500                 :       11263 :           || sym->attr.pointer || sym->attr.value)
   17501                 :             :         {
   17502                 :           4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   17503                 :             :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   17504                 :             :                      "attribute", sym->name, &sym->declared_at);
   17505                 :           4 :           return;
   17506                 :             :         }
   17507                 :             : 
   17508                 :       11259 :       if (sym->attr.intent == INTENT_OUT)
   17509                 :             :         {
   17510                 :           0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   17511                 :             :                      "have the INTENT(OUT) attribute",
   17512                 :             :                      sym->name, &sym->declared_at);
   17513                 :           0 :           return;
   17514                 :             :         }
   17515                 :       11259 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   17516                 :             :         {
   17517                 :           1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   17518                 :             :                      "either be a scalar or an assumed-size array",
   17519                 :             :                      sym->name, &sym->declared_at);
   17520                 :           1 :           return;
   17521                 :             :         }
   17522                 :             : 
   17523                 :             :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   17524                 :             :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   17525                 :             :          packing.  */
   17526                 :       11258 :       sym->ts.type = BT_ASSUMED;
   17527                 :       11258 :       sym->as = gfc_get_array_spec ();
   17528                 :       11258 :       sym->as->type = AS_ASSUMED_SIZE;
   17529                 :       11258 :       sym->as->rank = 1;
   17530                 :       11258 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   17531                 :             :     }
   17532                 :     1567877 :   else if (sym->ts.type == BT_ASSUMED)
   17533                 :             :     {
   17534                 :             :       /* TS 29113, C407a.  */
   17535                 :        8178 :       if (!sym->attr.dummy)
   17536                 :             :         {
   17537                 :           7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   17538                 :             :                      "for dummy variables", sym->name, &sym->declared_at);
   17539                 :           7 :           return;
   17540                 :             :         }
   17541                 :        8171 :       if (sym->attr.allocatable || sym->attr.codimension
   17542                 :        8171 :           || sym->attr.pointer || sym->attr.value)
   17543                 :             :         {
   17544                 :           8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   17545                 :             :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   17546                 :             :                      sym->name, &sym->declared_at);
   17547                 :           8 :           return;
   17548                 :             :         }
   17549                 :        8163 :       if (sym->attr.intent == INTENT_OUT)
   17550                 :             :         {
   17551                 :           2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   17552                 :             :                      "INTENT(OUT) attribute",
   17553                 :             :                      sym->name, &sym->declared_at);
   17554                 :           2 :           return;
   17555                 :             :         }
   17556                 :        8161 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   17557                 :             :         {
   17558                 :           3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   17559                 :             :                      "explicit-shape array", sym->name, &sym->declared_at);
   17560                 :           3 :           return;
   17561                 :             :         }
   17562                 :             :     }
   17563                 :             : 
   17564                 :             :   /* If the symbol is marked as bind(c), that it is declared at module level
   17565                 :             :      scope and verify its type and kind.  Do not do the latter for symbols
   17566                 :             :      that are implicitly typed because that is handled in
   17567                 :             :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   17568                 :             :      separately.  Also, anything that is use associated is not handled here
   17569                 :             :      but instead is handled in the module it is declared in.  Finally, derived
   17570                 :             :      type definitions are allowed to be BIND(C) since that only implies that
   17571                 :             :      they're interoperable, and they are checked fully for interoperability
   17572                 :             :      when a variable is declared of that type.  */
   17573                 :     1579115 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   17574                 :     1579115 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   17575                 :         567 :       && sym->attr.flavor != FL_DERIVED)
   17576                 :             :     {
   17577                 :         167 :       bool t = true;
   17578                 :             : 
   17579                 :             :       /* First, make sure the variable is declared at the
   17580                 :             :          module-level scope (J3/04-007, Section 15.3).  */
   17581                 :         167 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   17582                 :           7 :           && !sym->attr.in_common)
   17583                 :             :         {
   17584                 :           6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   17585                 :             :                      "is neither a COMMON block nor declared at the "
   17586                 :             :                      "module level scope", sym->name, &(sym->declared_at));
   17587                 :           6 :           t = false;
   17588                 :             :         }
   17589                 :         161 :       else if (sym->ts.type == BT_CHARACTER
   17590                 :         161 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   17591                 :           1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   17592                 :           1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   17593                 :             :         {
   17594                 :           1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   17595                 :           1 :                      sym->name, &sym->declared_at);
   17596                 :           1 :           t = false;
   17597                 :             :         }
   17598                 :         160 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   17599                 :             :         {
   17600                 :           1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   17601                 :             :         }
   17602                 :         159 :       else if (sym->attr.implicit_type == 0)
   17603                 :             :         {
   17604                 :             :           /* If type() declaration, we need to verify that the components
   17605                 :             :              of the given type are all C interoperable, etc.  */
   17606                 :         157 :           if (sym->ts.type == BT_DERIVED &&
   17607                 :          24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   17608                 :             :             {
   17609                 :             :               /* Make sure the user marked the derived type as BIND(C).  If
   17610                 :             :                  not, call the verify routine.  This could print an error
   17611                 :             :                  for the derived type more than once if multiple variables
   17612                 :             :                  of that type are declared.  */
   17613                 :          14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   17614                 :           1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   17615                 :         157 :               t = false;
   17616                 :             :             }
   17617                 :             : 
   17618                 :             :           /* Verify the variable itself as C interoperable if it
   17619                 :             :              is BIND(C).  It is not possible for this to succeed if
   17620                 :             :              the verify_bind_c_derived_type failed, so don't have to handle
   17621                 :             :              any error returned by verify_bind_c_derived_type.  */
   17622                 :         157 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   17623                 :             :                                  sym->common_block);
   17624                 :             :         }
   17625                 :             : 
   17626                 :         165 :       if (!t)
   17627                 :             :         {
   17628                 :             :           /* clear the is_bind_c flag to prevent reporting errors more than
   17629                 :             :              once if something failed.  */
   17630                 :          10 :           sym->attr.is_bind_c = 0;
   17631                 :          10 :           return;
   17632                 :             :         }
   17633                 :             :     }
   17634                 :             : 
   17635                 :             :   /* If a derived type symbol has reached this point, without its
   17636                 :             :      type being declared, we have an error.  Notice that most
   17637                 :             :      conditions that produce undefined derived types have already
   17638                 :             :      been dealt with.  However, the likes of:
   17639                 :             :      implicit type(t) (t) ..... call foo (t) will get us here if
   17640                 :             :      the type is not declared in the scope of the implicit
   17641                 :             :      statement. Change the type to BT_UNKNOWN, both because it is so
   17642                 :             :      and to prevent an ICE.  */
   17643                 :     1579105 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   17644                 :      111627 :       && sym->ts.u.derived->components == NULL
   17645                 :        1027 :       && !sym->ts.u.derived->attr.zero_comp)
   17646                 :             :     {
   17647                 :           3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   17648                 :             :                  "which has not been defined", sym->name,
   17649                 :             :                   &sym->declared_at, sym->ts.u.derived->name);
   17650                 :           3 :       sym->ts.type = BT_UNKNOWN;
   17651                 :           3 :       return;
   17652                 :             :     }
   17653                 :             : 
   17654                 :             :   /* Make sure that the derived type has been resolved and that the
   17655                 :             :      derived type is visible in the symbol's namespace, if it is a
   17656                 :             :      module function and is not PRIVATE.  */
   17657                 :     1579102 :   if (sym->ts.type == BT_DERIVED
   17658                 :      118456 :         && sym->ts.u.derived->attr.use_assoc
   17659                 :      102676 :         && sym->ns->proc_name
   17660                 :      102668 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   17661                 :     1584793 :         && !resolve_fl_derived (sym->ts.u.derived))
   17662                 :             :     return;
   17663                 :             : 
   17664                 :             :   /* Unless the derived-type declaration is use associated, Fortran 95
   17665                 :             :      does not allow public entries of private derived types.
   17666                 :             :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   17667                 :             :      161 in 95-006r3.  */
   17668                 :     1579102 :   if (sym->ts.type == BT_DERIVED
   17669                 :      118456 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   17670                 :        7354 :       && !sym->ts.u.derived->attr.use_assoc
   17671                 :        1663 :       && gfc_check_symbol_access (sym)
   17672                 :        1490 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   17673                 :     1579113 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   17674                 :             :                           "derived type %qs",
   17675                 :          11 :                           (sym->attr.flavor == FL_PARAMETER)
   17676                 :             :                           ? "parameter" : "variable",
   17677                 :             :                           sym->name, &sym->declared_at,
   17678                 :          11 :                           sym->ts.u.derived->name))
   17679                 :             :     return;
   17680                 :             : 
   17681                 :             :   /* F2008, C1302.  */
   17682                 :     1579095 :   if (sym->ts.type == BT_DERIVED
   17683                 :      118449 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   17684                 :      118449 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   17685                 :      118425 :           || sym->ts.u.derived->attr.lock_comp)
   17686                 :          37 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   17687                 :             :     {
   17688                 :           4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   17689                 :             :                  "type LOCK_TYPE must be a coarray", sym->name,
   17690                 :             :                  &sym->declared_at);
   17691                 :           4 :       return;
   17692                 :             :     }
   17693                 :             : 
   17694                 :             :   /* TS18508, C702/C703.  */
   17695                 :     1579091 :   if (sym->ts.type == BT_DERIVED
   17696                 :      118445 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   17697                 :      118445 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   17698                 :      118435 :           || sym->ts.u.derived->attr.event_comp)
   17699                 :          10 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   17700                 :             :     {
   17701                 :           1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   17702                 :             :                  "type EVENT_TYPE must be a coarray", sym->name,
   17703                 :             :                  &sym->declared_at);
   17704                 :           1 :       return;
   17705                 :             :     }
   17706                 :             : 
   17707                 :             :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   17708                 :             :      default initialization is defined (5.1.2.4.4).  */
   17709                 :     1579090 :   if (sym->ts.type == BT_DERIVED
   17710                 :      118444 :       && sym->attr.dummy
   17711                 :       40642 :       && sym->attr.intent == INTENT_OUT
   17712                 :        2348 :       && sym->as
   17713                 :         381 :       && sym->as->type == AS_ASSUMED_SIZE)
   17714                 :             :     {
   17715                 :           1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   17716                 :             :         {
   17717                 :           1 :           if (c->initializer)
   17718                 :             :             {
   17719                 :           1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   17720                 :             :                          "ASSUMED SIZE and so cannot have a default initializer",
   17721                 :             :                          sym->name, &sym->declared_at);
   17722                 :           1 :               return;
   17723                 :             :             }
   17724                 :             :         }
   17725                 :             :     }
   17726                 :             : 
   17727                 :             :   /* F2008, C542.  */
   17728                 :     1579089 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   17729                 :       40641 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   17730                 :             :     {
   17731                 :           0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   17732                 :             :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   17733                 :           0 :       return;
   17734                 :             :     }
   17735                 :             : 
   17736                 :             :   /* TS18508.  */
   17737                 :     1579089 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   17738                 :       40641 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   17739                 :             :     {
   17740                 :           0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   17741                 :             :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   17742                 :           0 :       return;
   17743                 :             :     }
   17744                 :             : 
   17745                 :             :   /* F2008, C525.  */
   17746                 :     1579089 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   17747                 :     1579004 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   17748                 :       18305 :              && sym->ts.u.derived && CLASS_DATA (sym)
   17749                 :       18299 :              && CLASS_DATA (sym)->attr.coarray_comp))
   17750                 :     1579004 :        || class_attr.codimension)
   17751                 :        1502 :       && (sym->attr.result || sym->result == sym))
   17752                 :             :     {
   17753                 :           8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   17754                 :             :                  "a coarray component", sym->name, &sym->declared_at);
   17755                 :           8 :       return;
   17756                 :             :     }
   17757                 :             : 
   17758                 :             :   /* F2008, C524.  */
   17759                 :     1579081 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   17760                 :         369 :       && sym->ts.u.derived->ts.is_iso_c)
   17761                 :             :     {
   17762                 :           3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17763                 :             :                  "shall not be a coarray", sym->name, &sym->declared_at);
   17764                 :           3 :       return;
   17765                 :             :     }
   17766                 :             : 
   17767                 :             :   /* F2008, C525.  */
   17768                 :     1579078 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   17769                 :     1578996 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   17770                 :       18304 :             && sym->ts.u.derived && CLASS_DATA (sym)
   17771                 :       18298 :             && CLASS_DATA (sym)->attr.coarray_comp))
   17772                 :          82 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   17773                 :          82 :           || class_attr.allocatable))
   17774                 :             :     {
   17775                 :           4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   17776                 :             :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   17777                 :             :                  sym->name, &sym->declared_at);
   17778                 :           4 :       return;
   17779                 :             :     }
   17780                 :             : 
   17781                 :             :   /* F2008, C526.  The function-result case was handled above.  */
   17782                 :     1579074 :   if (class_attr.codimension
   17783                 :        1414 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   17784                 :             :            || sym->attr.select_type_temporary
   17785                 :         274 :            || sym->attr.associate_var
   17786                 :         192 :            || (sym->ns->save_all && !sym->attr.automatic)
   17787                 :         192 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   17788                 :         192 :            || sym->ns->proc_name->attr.is_main_program
   17789                 :             :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   17790                 :             :     {
   17791                 :           4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   17792                 :             :                  "nor a dummy argument", sym->name, &sym->declared_at);
   17793                 :           4 :       return;
   17794                 :             :     }
   17795                 :             :   /* F2008, C528.  */
   17796                 :     1579070 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   17797                 :        1334 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   17798                 :             :     {
   17799                 :           6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   17800                 :             :                  "deferred shape without allocatable", sym->name,
   17801                 :             :                  &sym->declared_at);
   17802                 :           6 :       return;
   17803                 :             :     }
   17804                 :     1579064 :   else if (class_attr.codimension && class_attr.allocatable && as
   17805                 :         507 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   17806                 :             :     {
   17807                 :           9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   17808                 :             :                  "deferred shape", sym->name, &sym->declared_at);
   17809                 :           9 :       return;
   17810                 :             :     }
   17811                 :             : 
   17812                 :             :   /* F2008, C541.  */
   17813                 :     1579055 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   17814                 :     1578977 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   17815                 :       18299 :             && sym->ts.u.derived && CLASS_DATA (sym)
   17816                 :       18293 :             && CLASS_DATA (sym)->attr.coarray_comp))
   17817                 :     1578977 :        || (class_attr.codimension && class_attr.allocatable))
   17818                 :         576 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   17819                 :             :     {
   17820                 :           3 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   17821                 :             :                  "allocatable coarray or have coarray components",
   17822                 :             :                  sym->name, &sym->declared_at);
   17823                 :           3 :       return;
   17824                 :             :     }
   17825                 :             : 
   17826                 :     1579052 :   if (class_attr.codimension && sym->attr.dummy
   17827                 :         425 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   17828                 :             :     {
   17829                 :           2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   17830                 :             :                  "procedure %qs", sym->name, &sym->declared_at,
   17831                 :             :                  sym->ns->proc_name->name);
   17832                 :           2 :       return;
   17833                 :             :     }
   17834                 :             : 
   17835                 :     1579050 :   if (sym->ts.type == BT_LOGICAL
   17836                 :      110839 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   17837                 :      110836 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   17838                 :       30736 :               && sym->ns->proc_name->attr.is_bind_c)))
   17839                 :             :     {
   17840                 :             :       int i;
   17841                 :         200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   17842                 :         200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   17843                 :             :           break;
   17844                 :          16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   17845                 :         181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   17846                 :             :                               "%L with non-C_Bool kind in BIND(C) procedure "
   17847                 :             :                               "%qs", sym->name, &sym->declared_at,
   17848                 :          13 :                               sym->ns->proc_name->name))
   17849                 :             :         return;
   17850                 :         167 :       else if (!gfc_logical_kinds[i].c_bool
   17851                 :         182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   17852                 :             :                                    "%qs at %L with non-C_Bool kind in "
   17853                 :             :                                    "BIND(C) procedure %qs", sym->name,
   17854                 :             :                                    &sym->declared_at,
   17855                 :          15 :                                    sym->attr.function ? sym->name
   17856                 :          13 :                                    : sym->ns->proc_name->name))
   17857                 :             :         return;
   17858                 :             :     }
   17859                 :             : 
   17860                 :     1579047 :   switch (sym->attr.flavor)
   17861                 :             :     {
   17862                 :      613004 :     case FL_VARIABLE:
   17863                 :      613004 :       if (!resolve_fl_variable (sym, mp_flag))
   17864                 :             :         return;
   17865                 :             :       break;
   17866                 :             : 
   17867                 :      452391 :     case FL_PROCEDURE:
   17868                 :      452391 :       if (sym->formal && !sym->formal_ns)
   17869                 :             :         {
   17870                 :             :           /* Check that none of the arguments are a namelist.  */
   17871                 :             :           gfc_formal_arglist *formal = sym->formal;
   17872                 :             : 
   17873                 :      102302 :           for (; formal; formal = formal->next)
   17874                 :       69605 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   17875                 :             :               {
   17876                 :           1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   17877                 :             :                            "subroutine or function at %L",
   17878                 :             :                            formal->sym->name, &sym->declared_at);
   17879                 :           1 :                 return;
   17880                 :             :               }
   17881                 :             :         }
   17882                 :             : 
   17883                 :      452390 :       if (!resolve_fl_procedure (sym, mp_flag))
   17884                 :             :         return;
   17885                 :             :       break;
   17886                 :             : 
   17887                 :         805 :     case FL_NAMELIST:
   17888                 :         805 :       if (!resolve_fl_namelist (sym))
   17889                 :             :         return;
   17890                 :             :       break;
   17891                 :             : 
   17892                 :      367508 :     case FL_PARAMETER:
   17893                 :      367508 :       if (!resolve_fl_parameter (sym))
   17894                 :             :         return;
   17895                 :             :       break;
   17896                 :             : 
   17897                 :             :     default:
   17898                 :             :       break;
   17899                 :             :     }
   17900                 :             : 
   17901                 :             :   /* Resolve array specifier. Check as well some constraints
   17902                 :             :      on COMMON blocks.  */
   17903                 :             : 
   17904                 :     1578858 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   17905                 :             : 
   17906                 :     1578858 :   resolve_symbol_array_spec (sym, check_constant);
   17907                 :             : 
   17908                 :             :   /* Resolve formal namespaces.  */
   17909                 :     1578858 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   17910                 :      246191 :       && !sym->attr.contained && !sym->attr.intrinsic)
   17911                 :      222371 :     gfc_resolve (sym->formal_ns);
   17912                 :             : 
   17913                 :             :   /* Make sure the formal namespace is present.  */
   17914                 :     1578858 :   if (sym->formal && !sym->formal_ns)
   17915                 :             :     {
   17916                 :             :       gfc_formal_arglist *formal = sym->formal;
   17917                 :       32898 :       while (formal && !formal->sym)
   17918                 :          11 :         formal = formal->next;
   17919                 :             : 
   17920                 :       32887 :       if (formal)
   17921                 :             :         {
   17922                 :       32876 :           sym->formal_ns = formal->sym->ns;
   17923                 :       32876 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   17924                 :       24680 :             sym->formal_ns->refs++;
   17925                 :             :         }
   17926                 :             :     }
   17927                 :             : 
   17928                 :             :   /* Check threadprivate restrictions.  */
   17929                 :     1578858 :   if (sym->attr.threadprivate
   17930                 :     1578858 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   17931                 :          32 :       && !(sym->ns->save_all && !sym->attr.automatic)
   17932                 :          31 :       && sym->module == NULL
   17933                 :          16 :       && (sym->ns->proc_name == NULL
   17934                 :          16 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   17935                 :           3 :               && !sym->ns->proc_name->attr.is_main_program)))
   17936                 :           1 :     gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   17937                 :             : 
   17938                 :             :   /* Check omp declare target restrictions.  */
   17939                 :     1578858 :   if (sym->attr.omp_declare_target
   17940                 :     1578858 :       && sym->attr.flavor == FL_VARIABLE
   17941                 :         604 :       && !sym->attr.save
   17942                 :         195 :       && !(sym->ns->save_all && !sym->attr.automatic)
   17943                 :         195 :       && (!sym->attr.in_common
   17944                 :         182 :           && sym->module == NULL
   17945                 :          92 :           && (sym->ns->proc_name == NULL
   17946                 :          92 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   17947                 :           2 :                   && !sym->ns->proc_name->attr.is_main_program))))
   17948                 :           1 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   17949                 :             :                sym->name, &sym->declared_at);
   17950                 :             : 
   17951                 :             :   /* If we have come this far we can apply default-initializers, as
   17952                 :             :      described in 14.7.5, to those variables that have not already
   17953                 :             :      been assigned one.  */
   17954                 :     1578858 :   if (sym->ts.type == BT_DERIVED
   17955                 :      118416 :       && !sym->value
   17956                 :       94374 :       && !sym->attr.allocatable
   17957                 :       91608 :       && !sym->attr.alloc_comp)
   17958                 :             :     {
   17959                 :       91608 :       symbol_attribute *a = &sym->attr;
   17960                 :             : 
   17961                 :       91608 :       if ((!a->save && !a->dummy && !a->pointer
   17962                 :       91608 :            && !a->in_common && !a->use_assoc
   17963                 :        9198 :            && a->referenced
   17964                 :        7276 :            && !((a->function || a->result)
   17965                 :        1338 :                 && (!a->dimension
   17966                 :         120 :                     || sym->ts.u.derived->attr.alloc_comp
   17967                 :         120 :                     || sym->ts.u.derived->attr.pointer_comp))
   17968                 :        6007 :            && !(a->function && sym != sym->result))
   17969                 :       85621 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   17970                 :        1522 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   17971                 :        7410 :         apply_default_init (sym);
   17972                 :       84198 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   17973                 :       11932 :                && (sym->ts.u.derived->attr.alloc_comp
   17974                 :       11932 :                    || sym->ts.u.derived->attr.pointer_comp))
   17975                 :             :         /* Mark the result symbol to be referenced, when it has allocatable
   17976                 :             :            components.  */
   17977                 :         812 :         sym->result->attr.referenced = 1;
   17978                 :       83386 :       else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc
   17979                 :        1909 :                && sym->result)
   17980                 :             :         /* Default initialization for function results.  */
   17981                 :        1905 :         apply_default_init (sym->result);
   17982                 :             :     }
   17983                 :             : 
   17984                 :     1578858 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   17985                 :       17849 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   17986                 :        1180 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   17987                 :        1105 :       && !CLASS_DATA (sym)->attr.class_pointer
   17988                 :        1105 :       && !CLASS_DATA (sym)->attr.allocatable)
   17989                 :         807 :     apply_default_init (sym);
   17990                 :             : 
   17991                 :             :   /* If this symbol has a type-spec, check it.  */
   17992                 :     1578858 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   17993                 :      598454 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   17994                 :     1286906 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   17995                 :             :       return;
   17996                 :             : 
   17997                 :     1578855 :   if (sym->param_list)
   17998                 :         522 :     resolve_pdt (sym);
   17999                 :             : }
   18000                 :             : 
   18001                 :             : 
   18002                 :             : /************* Resolve DATA statements *************/
   18003                 :             : 
   18004                 :             : static struct
   18005                 :             : {
   18006                 :             :   gfc_data_value *vnode;
   18007                 :             :   mpz_t left;
   18008                 :             : }
   18009                 :             : values;
   18010                 :             : 
   18011                 :             : 
   18012                 :             : /* Advance the values structure to point to the next value in the data list.  */
   18013                 :             : 
   18014                 :             : static bool
   18015                 :       10849 : next_data_value (void)
   18016                 :             : {
   18017                 :       16612 :   while (mpz_cmp_ui (values.left, 0) == 0)
   18018                 :             :     {
   18019                 :             : 
   18020                 :        8174 :       if (values.vnode->next == NULL)
   18021                 :             :         return false;
   18022                 :             : 
   18023                 :        5763 :       values.vnode = values.vnode->next;
   18024                 :        5763 :       mpz_set (values.left, values.vnode->repeat);
   18025                 :             :     }
   18026                 :             : 
   18027                 :             :   return true;
   18028                 :             : }
   18029                 :             : 
   18030                 :             : 
   18031                 :             : static bool
   18032                 :        3535 : check_data_variable (gfc_data_variable *var, locus *where)
   18033                 :             : {
   18034                 :        3535 :   gfc_expr *e;
   18035                 :        3535 :   mpz_t size;
   18036                 :        3535 :   mpz_t offset;
   18037                 :        3535 :   bool t;
   18038                 :        3535 :   ar_type mark = AR_UNKNOWN;
   18039                 :        3535 :   int i;
   18040                 :        3535 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   18041                 :        3535 :   int vector_offset[GFC_MAX_DIMENSIONS];
   18042                 :        3535 :   gfc_ref *ref;
   18043                 :        3535 :   gfc_array_ref *ar;
   18044                 :        3535 :   gfc_symbol *sym;
   18045                 :        3535 :   int has_pointer;
   18046                 :             : 
   18047                 :        3535 :   if (!gfc_resolve_expr (var->expr))
   18048                 :             :     return false;
   18049                 :             : 
   18050                 :        3535 :   ar = NULL;
   18051                 :        3535 :   e = var->expr;
   18052                 :             : 
   18053                 :        3535 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   18054                 :           0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   18055                 :           0 :     e = e->value.function.actual->expr;
   18056                 :             : 
   18057                 :        3535 :   if (e->expr_type != EXPR_VARIABLE)
   18058                 :             :     {
   18059                 :           0 :       gfc_error ("Expecting definable entity near %L", where);
   18060                 :           0 :       return false;
   18061                 :             :     }
   18062                 :             : 
   18063                 :        3535 :   sym = e->symtree->n.sym;
   18064                 :             : 
   18065                 :        3535 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   18066                 :             :     {
   18067                 :           2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   18068                 :             :                  sym->name, &sym->declared_at);
   18069                 :           2 :       return false;
   18070                 :             :     }
   18071                 :             : 
   18072                 :        3533 :   if (e->ref == NULL && sym->as)
   18073                 :             :     {
   18074                 :           1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   18075                 :             :                  " declaration", sym->name, where);
   18076                 :           1 :       return false;
   18077                 :             :     }
   18078                 :             : 
   18079                 :        3532 :   if (gfc_is_coindexed (e))
   18080                 :             :     {
   18081                 :           5 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   18082                 :             :                  where);
   18083                 :           5 :       return false;
   18084                 :             :     }
   18085                 :             : 
   18086                 :        3527 :   has_pointer = sym->attr.pointer;
   18087                 :             : 
   18088                 :        5963 :   for (ref = e->ref; ref; ref = ref->next)
   18089                 :             :     {
   18090                 :        2440 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   18091                 :             :         has_pointer = 1;
   18092                 :             : 
   18093                 :        2414 :       if (has_pointer)
   18094                 :             :         {
   18095                 :          29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   18096                 :             :             {
   18097                 :           1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   18098                 :             :                          "be a full array", sym->name, where);
   18099                 :           1 :               return false;
   18100                 :             :             }
   18101                 :             : 
   18102                 :          28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   18103                 :             :             {
   18104                 :           1 :               gfc_error ("DATA object near %L has the pointer attribute "
   18105                 :             :                          "and the corresponding DATA value is not a valid "
   18106                 :             :                          "initial-data-target", where);
   18107                 :           1 :               return false;
   18108                 :             :             }
   18109                 :             :         }
   18110                 :             : 
   18111                 :        2438 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   18112                 :             :         {
   18113                 :           1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   18114                 :             :                      "attribute", ref->u.c.component->name, &e->where);
   18115                 :           1 :           return false;
   18116                 :             :         }
   18117                 :             : 
   18118                 :             :       /* Reject substrings of strings of non-constant length.  */
   18119                 :        2437 :       if (ref->type == REF_SUBSTRING
   18120                 :          73 :           && ref->u.ss.length
   18121                 :          73 :           && ref->u.ss.length->length
   18122                 :        2510 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   18123                 :           1 :         goto bad_charlen;
   18124                 :             :     }
   18125                 :             : 
   18126                 :             :   /* Reject strings with deferred length or non-constant length.  */
   18127                 :        3523 :   if (e->ts.type == BT_CHARACTER
   18128                 :        3523 :       && (e->ts.deferred
   18129                 :         374 :           || (e->ts.u.cl->length
   18130                 :         323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   18131                 :           5 :     goto bad_charlen;
   18132                 :             : 
   18133                 :        3518 :   mpz_init_set_si (offset, 0);
   18134                 :             : 
   18135                 :        3518 :   if (e->rank == 0 || has_pointer)
   18136                 :             :     {
   18137                 :        2675 :       mpz_init_set_ui (size, 1);
   18138                 :        2675 :       ref = NULL;
   18139                 :             :     }
   18140                 :             :   else
   18141                 :             :     {
   18142                 :         843 :       ref = e->ref;
   18143                 :             : 
   18144                 :             :       /* Find the array section reference.  */
   18145                 :        1026 :       for (ref = e->ref; ref; ref = ref->next)
   18146                 :             :         {
   18147                 :        1026 :           if (ref->type != REF_ARRAY)
   18148                 :          92 :             continue;
   18149                 :         934 :           if (ref->u.ar.type == AR_ELEMENT)
   18150                 :          91 :             continue;
   18151                 :             :           break;
   18152                 :             :         }
   18153                 :         843 :       gcc_assert (ref);
   18154                 :             : 
   18155                 :             :       /* Set marks according to the reference pattern.  */
   18156                 :         843 :       switch (ref->u.ar.type)
   18157                 :             :         {
   18158                 :             :         case AR_FULL:
   18159                 :             :           mark = AR_FULL;
   18160                 :             :           break;
   18161                 :             : 
   18162                 :         149 :         case AR_SECTION:
   18163                 :         149 :           ar = &ref->u.ar;
   18164                 :             :           /* Get the start position of array section.  */
   18165                 :         149 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   18166                 :         149 :           mark = AR_SECTION;
   18167                 :         149 :           break;
   18168                 :             : 
   18169                 :           0 :         default:
   18170                 :           0 :           gcc_unreachable ();
   18171                 :             :         }
   18172                 :             : 
   18173                 :         843 :       if (!gfc_array_size (e, &size))
   18174                 :             :         {
   18175                 :           1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   18176                 :             :                      where);
   18177                 :           1 :           mpz_clear (offset);
   18178                 :           1 :           return false;
   18179                 :             :         }
   18180                 :             :     }
   18181                 :             : 
   18182                 :        3517 :   t = true;
   18183                 :             : 
   18184                 :       11893 :   while (mpz_cmp_ui (size, 0) > 0)
   18185                 :             :     {
   18186                 :        8439 :       if (!next_data_value ())
   18187                 :             :         {
   18188                 :           1 :           gfc_error ("DATA statement at %L has more variables than values",
   18189                 :             :                      where);
   18190                 :           1 :           t = false;
   18191                 :           1 :           break;
   18192                 :             :         }
   18193                 :             : 
   18194                 :        8438 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   18195                 :        8438 :       if (!t)
   18196                 :             :         break;
   18197                 :             : 
   18198                 :             :       /* If we have more than one element left in the repeat count,
   18199                 :             :          and we have more than one element left in the target variable,
   18200                 :             :          then create a range assignment.  */
   18201                 :             :       /* FIXME: Only done for full arrays for now, since array sections
   18202                 :             :          seem tricky.  */
   18203                 :        8419 :       if (mark == AR_FULL && ref && ref->next == NULL
   18204                 :        5362 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   18205                 :             :         {
   18206                 :         137 :           mpz_t range;
   18207                 :             : 
   18208                 :         137 :           if (mpz_cmp (size, values.left) >= 0)
   18209                 :             :             {
   18210                 :         126 :               mpz_init_set (range, values.left);
   18211                 :         126 :               mpz_sub (size, size, values.left);
   18212                 :         126 :               mpz_set_ui (values.left, 0);
   18213                 :             :             }
   18214                 :             :           else
   18215                 :             :             {
   18216                 :          11 :               mpz_init_set (range, size);
   18217                 :          11 :               mpz_sub (values.left, values.left, size);
   18218                 :          11 :               mpz_set_ui (size, 0);
   18219                 :             :             }
   18220                 :             : 
   18221                 :         137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   18222                 :             :                                      offset, &range);
   18223                 :             : 
   18224                 :         137 :           mpz_add (offset, offset, range);
   18225                 :         137 :           mpz_clear (range);
   18226                 :             : 
   18227                 :         137 :           if (!t)
   18228                 :             :             break;
   18229                 :         129 :         }
   18230                 :             : 
   18231                 :             :       /* Assign initial value to symbol.  */
   18232                 :             :       else
   18233                 :             :         {
   18234                 :        8282 :           mpz_sub_ui (values.left, values.left, 1);
   18235                 :        8282 :           mpz_sub_ui (size, size, 1);
   18236                 :             : 
   18237                 :        8282 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   18238                 :             :                                      offset, NULL);
   18239                 :        8282 :           if (!t)
   18240                 :             :             break;
   18241                 :             : 
   18242                 :        8247 :           if (mark == AR_FULL)
   18243                 :        5254 :             mpz_add_ui (offset, offset, 1);
   18244                 :             : 
   18245                 :             :           /* Modify the array section indexes and recalculate the offset
   18246                 :             :              for next element.  */
   18247                 :        2993 :           else if (mark == AR_SECTION)
   18248                 :         363 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   18249                 :             :         }
   18250                 :             :     }
   18251                 :             : 
   18252                 :        3517 :   if (mark == AR_SECTION)
   18253                 :             :     {
   18254                 :         340 :       for (i = 0; i < ar->dimen; i++)
   18255                 :         192 :         mpz_clear (section_index[i]);
   18256                 :             :     }
   18257                 :             : 
   18258                 :        3517 :   mpz_clear (size);
   18259                 :        3517 :   mpz_clear (offset);
   18260                 :             : 
   18261                 :        3517 :   return t;
   18262                 :             : 
   18263                 :           6 : bad_charlen:
   18264                 :           6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   18265                 :             :              &e->where);
   18266                 :           6 :   return false;
   18267                 :             : }
   18268                 :             : 
   18269                 :             : 
   18270                 :             : static bool traverse_data_var (gfc_data_variable *, locus *);
   18271                 :             : 
   18272                 :             : /* Iterate over a list of elements in a DATA statement.  */
   18273                 :             : 
   18274                 :             : static bool
   18275                 :         236 : traverse_data_list (gfc_data_variable *var, locus *where)
   18276                 :             : {
   18277                 :         236 :   mpz_t trip;
   18278                 :         236 :   iterator_stack frame;
   18279                 :         236 :   gfc_expr *e, *start, *end, *step;
   18280                 :         236 :   bool retval = true;
   18281                 :             : 
   18282                 :         236 :   mpz_init (frame.value);
   18283                 :         236 :   mpz_init (trip);
   18284                 :             : 
   18285                 :         236 :   start = gfc_copy_expr (var->iter.start);
   18286                 :         236 :   end = gfc_copy_expr (var->iter.end);
   18287                 :         236 :   step = gfc_copy_expr (var->iter.step);
   18288                 :             : 
   18289                 :         236 :   if (!gfc_simplify_expr (start, 1)
   18290                 :         236 :       || start->expr_type != EXPR_CONSTANT)
   18291                 :             :     {
   18292                 :           0 :       gfc_error ("start of implied-do loop at %L could not be "
   18293                 :             :                  "simplified to a constant value", &start->where);
   18294                 :           0 :       retval = false;
   18295                 :           0 :       goto cleanup;
   18296                 :             :     }
   18297                 :         236 :   if (!gfc_simplify_expr (end, 1)
   18298                 :         236 :       || end->expr_type != EXPR_CONSTANT)
   18299                 :             :     {
   18300                 :           0 :       gfc_error ("end of implied-do loop at %L could not be "
   18301                 :             :                  "simplified to a constant value", &end->where);
   18302                 :           0 :       retval = false;
   18303                 :           0 :       goto cleanup;
   18304                 :             :     }
   18305                 :         236 :   if (!gfc_simplify_expr (step, 1)
   18306                 :         236 :       || step->expr_type != EXPR_CONSTANT)
   18307                 :             :     {
   18308                 :           0 :       gfc_error ("step of implied-do loop at %L could not be "
   18309                 :             :                  "simplified to a constant value", &step->where);
   18310                 :           0 :       retval = false;
   18311                 :           0 :       goto cleanup;
   18312                 :             :     }
   18313                 :         236 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   18314                 :             :     {
   18315                 :           1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   18316                 :             :                  &step->where);
   18317                 :           1 :       retval = false;
   18318                 :           1 :       goto cleanup;
   18319                 :             :     }
   18320                 :             : 
   18321                 :         235 :   mpz_set (trip, end->value.integer);
   18322                 :         235 :   mpz_sub (trip, trip, start->value.integer);
   18323                 :         235 :   mpz_add (trip, trip, step->value.integer);
   18324                 :             : 
   18325                 :         235 :   mpz_div (trip, trip, step->value.integer);
   18326                 :             : 
   18327                 :         235 :   mpz_set (frame.value, start->value.integer);
   18328                 :             : 
   18329                 :         235 :   frame.prev = iter_stack;
   18330                 :         235 :   frame.variable = var->iter.var->symtree;
   18331                 :         235 :   iter_stack = &frame;
   18332                 :             : 
   18333                 :        1124 :   while (mpz_cmp_ui (trip, 0) > 0)
   18334                 :             :     {
   18335                 :         903 :       if (!traverse_data_var (var->list, where))
   18336                 :             :         {
   18337                 :          14 :           retval = false;
   18338                 :          14 :           goto cleanup;
   18339                 :             :         }
   18340                 :             : 
   18341                 :         889 :       e = gfc_copy_expr (var->expr);
   18342                 :         889 :       if (!gfc_simplify_expr (e, 1))
   18343                 :             :         {
   18344                 :           0 :           gfc_free_expr (e);
   18345                 :           0 :           retval = false;
   18346                 :           0 :           goto cleanup;
   18347                 :             :         }
   18348                 :             : 
   18349                 :         889 :       mpz_add (frame.value, frame.value, step->value.integer);
   18350                 :             : 
   18351                 :         889 :       mpz_sub_ui (trip, trip, 1);
   18352                 :             :     }
   18353                 :             : 
   18354                 :         221 : cleanup:
   18355                 :         236 :   mpz_clear (frame.value);
   18356                 :         236 :   mpz_clear (trip);
   18357                 :             : 
   18358                 :         236 :   gfc_free_expr (start);
   18359                 :         236 :   gfc_free_expr (end);
   18360                 :         236 :   gfc_free_expr (step);
   18361                 :             : 
   18362                 :         236 :   iter_stack = frame.prev;
   18363                 :         236 :   return retval;
   18364                 :             : }
   18365                 :             : 
   18366                 :             : 
   18367                 :             : /* Type resolve variables in the variable list of a DATA statement.  */
   18368                 :             : 
   18369                 :             : static bool
   18370                 :        3395 : traverse_data_var (gfc_data_variable *var, locus *where)
   18371                 :             : {
   18372                 :        3395 :   bool t;
   18373                 :             : 
   18374                 :        7070 :   for (; var; var = var->next)
   18375                 :             :     {
   18376                 :        3771 :       if (var->expr == NULL)
   18377                 :         236 :         t = traverse_data_list (var, where);
   18378                 :             :       else
   18379                 :        3535 :         t = check_data_variable (var, where);
   18380                 :             : 
   18381                 :        3771 :       if (!t)
   18382                 :             :         return false;
   18383                 :             :     }
   18384                 :             : 
   18385                 :             :   return true;
   18386                 :             : }
   18387                 :             : 
   18388                 :             : 
   18389                 :             : /* Resolve the expressions and iterators associated with a data statement.
   18390                 :             :    This is separate from the assignment checking because data lists should
   18391                 :             :    only be resolved once.  */
   18392                 :             : 
   18393                 :             : static bool
   18394                 :        2646 : resolve_data_variables (gfc_data_variable *d)
   18395                 :             : {
   18396                 :        5663 :   for (; d; d = d->next)
   18397                 :             :     {
   18398                 :        3022 :       if (d->list == NULL)
   18399                 :             :         {
   18400                 :        2870 :           if (!gfc_resolve_expr (d->expr))
   18401                 :             :             return false;
   18402                 :             :         }
   18403                 :             :       else
   18404                 :             :         {
   18405                 :         152 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   18406                 :             :             return false;
   18407                 :             : 
   18408                 :         149 :           if (!resolve_data_variables (d->list))
   18409                 :             :             return false;
   18410                 :             :         }
   18411                 :             :     }
   18412                 :             : 
   18413                 :             :   return true;
   18414                 :             : }
   18415                 :             : 
   18416                 :             : 
   18417                 :             : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   18418                 :             :    the value list into static variables, and then recursively traversing the
   18419                 :             :    variables list, expanding iterators and such.  */
   18420                 :             : 
   18421                 :             : static void
   18422                 :        2497 : resolve_data (gfc_data *d)
   18423                 :             : {
   18424                 :             : 
   18425                 :        2497 :   if (!resolve_data_variables (d->var))
   18426                 :             :     return;
   18427                 :             : 
   18428                 :        2492 :   values.vnode = d->value;
   18429                 :        2492 :   if (d->value == NULL)
   18430                 :           0 :     mpz_set_ui (values.left, 0);
   18431                 :             :   else
   18432                 :        2492 :     mpz_set (values.left, d->value->repeat);
   18433                 :             : 
   18434                 :        2492 :   if (!traverse_data_var (d->var, &d->where))
   18435                 :             :     return;
   18436                 :             : 
   18437                 :             :   /* At this point, we better not have any values left.  */
   18438                 :             : 
   18439                 :        2410 :   if (next_data_value ())
   18440                 :           0 :     gfc_error ("DATA statement at %L has more values than variables",
   18441                 :             :                &d->where);
   18442                 :             : }
   18443                 :             : 
   18444                 :             : 
   18445                 :             : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   18446                 :             :    accessed by host or use association, is a dummy argument to a pure function,
   18447                 :             :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   18448                 :             :    is storage associated with any such variable, shall not be used in the
   18449                 :             :    following contexts: (clients of this function).  */
   18450                 :             : 
   18451                 :             : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   18452                 :             :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   18453                 :             :    problem.  */
   18454                 :             : bool
   18455                 :       52514 : gfc_impure_variable (gfc_symbol *sym)
   18456                 :             : {
   18457                 :       52514 :   gfc_symbol *proc;
   18458                 :       52514 :   gfc_namespace *ns;
   18459                 :             : 
   18460                 :       52514 :   if (sym->attr.use_assoc || sym->attr.in_common)
   18461                 :             :     return 1;
   18462                 :             : 
   18463                 :             :   /* Check if the symbol's ns is inside the pure procedure.  */
   18464                 :       56292 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   18465                 :             :     {
   18466                 :       56013 :       if (ns == sym->ns)
   18467                 :             :         break;
   18468                 :        5735 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
   18469                 :             :         return 1;
   18470                 :             :     }
   18471                 :             : 
   18472                 :       50557 :   proc = sym->ns->proc_name;
   18473                 :       50557 :   if (sym->attr.dummy
   18474                 :       50557 :       && !sym->attr.value
   18475                 :        5502 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   18476                 :        5307 :           || proc->attr.function))
   18477                 :         674 :     return 1;
   18478                 :             : 
   18479                 :             :   /* TODO: Sort out what can be storage associated, if anything, and include
   18480                 :             :      it here.  In principle equivalences should be scanned but it does not
   18481                 :             :      seem to be possible to storage associate an impure variable this way.  */
   18482                 :             :   return 0;
   18483                 :             : }
   18484                 :             : 
   18485                 :             : 
   18486                 :             : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   18487                 :             :    current namespace is inside a pure procedure.  */
   18488                 :             : 
   18489                 :             : bool
   18490                 :     2201689 : gfc_pure (gfc_symbol *sym)
   18491                 :             : {
   18492                 :     2201689 :   symbol_attribute attr;
   18493                 :     2201689 :   gfc_namespace *ns;
   18494                 :             : 
   18495                 :     2201689 :   if (sym == NULL)
   18496                 :             :     {
   18497                 :             :       /* Check if the current namespace or one of its parents
   18498                 :             :         belongs to a pure procedure.  */
   18499                 :     3068036 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   18500                 :             :         {
   18501                 :     1813628 :           sym = ns->proc_name;
   18502                 :     1813628 :           if (sym == NULL)
   18503                 :             :             return 0;
   18504                 :     1812514 :           attr = sym->attr;
   18505                 :     1812514 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   18506                 :             :             return 1;
   18507                 :             :         }
   18508                 :             :       return 0;
   18509                 :             :     }
   18510                 :             : 
   18511                 :      939251 :   attr = sym->attr;
   18512                 :             : 
   18513                 :      939251 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   18514                 :             : }
   18515                 :             : 
   18516                 :             : 
   18517                 :             : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   18518                 :             :    checks if the current namespace is implicitly pure.  Note that this
   18519                 :             :    function returns false for a PURE procedure.  */
   18520                 :             : 
   18521                 :             : bool
   18522                 :      701009 : gfc_implicit_pure (gfc_symbol *sym)
   18523                 :             : {
   18524                 :      701009 :   gfc_namespace *ns;
   18525                 :             : 
   18526                 :      701009 :   if (sym == NULL)
   18527                 :             :     {
   18528                 :             :       /* Check if the current procedure is implicit_pure.  Walk up
   18529                 :             :          the procedure list until we find a procedure.  */
   18530                 :      962456 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   18531                 :             :         {
   18532                 :      688791 :           sym = ns->proc_name;
   18533                 :      688791 :           if (sym == NULL)
   18534                 :             :             return 0;
   18535                 :             : 
   18536                 :      688718 :           if (sym->attr.flavor == FL_PROCEDURE)
   18537                 :             :             break;
   18538                 :             :         }
   18539                 :             :     }
   18540                 :             : 
   18541                 :      700936 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   18542                 :      700936 :     && !sym->attr.pure;
   18543                 :             : }
   18544                 :             : 
   18545                 :             : 
   18546                 :             : void
   18547                 :      406309 : gfc_unset_implicit_pure (gfc_symbol *sym)
   18548                 :             : {
   18549                 :      406309 :   gfc_namespace *ns;
   18550                 :             : 
   18551                 :      406309 :   if (sym == NULL)
   18552                 :             :     {
   18553                 :             :       /* Check if the current procedure is implicit_pure.  Walk up
   18554                 :             :          the procedure list until we find a procedure.  */
   18555                 :      661801 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   18556                 :             :         {
   18557                 :      410475 :           sym = ns->proc_name;
   18558                 :      410475 :           if (sym == NULL)
   18559                 :             :             return;
   18560                 :             : 
   18561                 :      409669 :           if (sym->attr.flavor == FL_PROCEDURE)
   18562                 :             :             break;
   18563                 :             :         }
   18564                 :             :     }
   18565                 :             : 
   18566                 :      405503 :   if (sym->attr.flavor == FL_PROCEDURE)
   18567                 :      146511 :     sym->attr.implicit_pure = 0;
   18568                 :             :   else
   18569                 :      258992 :     sym->attr.pure = 0;
   18570                 :             : }
   18571                 :             : 
   18572                 :             : 
   18573                 :             : /* Test whether the current procedure is elemental or not.  */
   18574                 :             : 
   18575                 :             : bool
   18576                 :     1259117 : gfc_elemental (gfc_symbol *sym)
   18577                 :             : {
   18578                 :     1259117 :   symbol_attribute attr;
   18579                 :             : 
   18580                 :     1259117 :   if (sym == NULL)
   18581                 :           0 :     sym = gfc_current_ns->proc_name;
   18582                 :           0 :   if (sym == NULL)
   18583                 :             :     return 0;
   18584                 :     1259117 :   attr = sym->attr;
   18585                 :             : 
   18586                 :     1259117 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   18587                 :             : }
   18588                 :             : 
   18589                 :             : 
   18590                 :             : /* Warn about unused labels.  */
   18591                 :             : 
   18592                 :             : static void
   18593                 :        4425 : warn_unused_fortran_label (gfc_st_label *label)
   18594                 :             : {
   18595                 :        4428 :   if (label == NULL)
   18596                 :             :     return;
   18597                 :             : 
   18598                 :           4 :   warn_unused_fortran_label (label->left);
   18599                 :             : 
   18600                 :           4 :   if (label->defined == ST_LABEL_UNKNOWN)
   18601                 :             :     return;
   18602                 :             : 
   18603                 :           3 :   switch (label->referenced)
   18604                 :             :     {
   18605                 :           1 :     case ST_LABEL_UNKNOWN:
   18606                 :           1 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   18607                 :             :                    label->value, &label->where);
   18608                 :           1 :       break;
   18609                 :             : 
   18610                 :           1 :     case ST_LABEL_BAD_TARGET:
   18611                 :           1 :       gfc_warning (OPT_Wunused_label,
   18612                 :             :                    "Label %d at %L defined but cannot be used",
   18613                 :             :                    label->value, &label->where);
   18614                 :           1 :       break;
   18615                 :             : 
   18616                 :             :     default:
   18617                 :             :       break;
   18618                 :             :     }
   18619                 :             : 
   18620                 :           3 :   warn_unused_fortran_label (label->right);
   18621                 :             : }
   18622                 :             : 
   18623                 :             : 
   18624                 :             : /* Returns the sequence type of a symbol or sequence.  */
   18625                 :             : 
   18626                 :             : static seq_type
   18627                 :        1076 : sequence_type (gfc_typespec ts)
   18628                 :             : {
   18629                 :        1076 :   seq_type result;
   18630                 :        1076 :   gfc_component *c;
   18631                 :             : 
   18632                 :        1076 :   switch (ts.type)
   18633                 :             :   {
   18634                 :          49 :     case BT_DERIVED:
   18635                 :             : 
   18636                 :          49 :       if (ts.u.derived->components == NULL)
   18637                 :             :         return SEQ_NONDEFAULT;
   18638                 :             : 
   18639                 :          49 :       result = sequence_type (ts.u.derived->components->ts);
   18640                 :         103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   18641                 :          67 :         if (sequence_type (c->ts) != result)
   18642                 :             :           return SEQ_MIXED;
   18643                 :             : 
   18644                 :             :       return result;
   18645                 :             : 
   18646                 :         129 :     case BT_CHARACTER:
   18647                 :         129 :       if (ts.kind != gfc_default_character_kind)
   18648                 :           0 :           return SEQ_NONDEFAULT;
   18649                 :             : 
   18650                 :             :       return SEQ_CHARACTER;
   18651                 :             : 
   18652                 :         240 :     case BT_INTEGER:
   18653                 :         240 :       if (ts.kind != gfc_default_integer_kind)
   18654                 :          25 :           return SEQ_NONDEFAULT;
   18655                 :             : 
   18656                 :             :       return SEQ_NUMERIC;
   18657                 :             : 
   18658                 :         559 :     case BT_REAL:
   18659                 :         559 :       if (!(ts.kind == gfc_default_real_kind
   18660                 :         269 :             || ts.kind == gfc_default_double_kind))
   18661                 :           0 :           return SEQ_NONDEFAULT;
   18662                 :             : 
   18663                 :             :       return SEQ_NUMERIC;
   18664                 :             : 
   18665                 :          81 :     case BT_COMPLEX:
   18666                 :          81 :       if (ts.kind != gfc_default_complex_kind)
   18667                 :          48 :           return SEQ_NONDEFAULT;
   18668                 :             : 
   18669                 :             :       return SEQ_NUMERIC;
   18670                 :             : 
   18671                 :          17 :     case BT_LOGICAL:
   18672                 :          17 :       if (ts.kind != gfc_default_logical_kind)
   18673                 :           0 :           return SEQ_NONDEFAULT;
   18674                 :             : 
   18675                 :             :       return SEQ_NUMERIC;
   18676                 :             : 
   18677                 :             :     default:
   18678                 :             :       return SEQ_NONDEFAULT;
   18679                 :             :   }
   18680                 :             : }
   18681                 :             : 
   18682                 :             : 
   18683                 :             : /* Resolve derived type EQUIVALENCE object.  */
   18684                 :             : 
   18685                 :             : static bool
   18686                 :          80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   18687                 :             : {
   18688                 :          80 :   gfc_component *c = derived->components;
   18689                 :             : 
   18690                 :          80 :   if (!derived)
   18691                 :             :     return true;
   18692                 :             : 
   18693                 :             :   /* Shall not be an object of nonsequence derived type.  */
   18694                 :          80 :   if (!derived->attr.sequence)
   18695                 :             :     {
   18696                 :           0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   18697                 :             :                  "attribute to be an EQUIVALENCE object", sym->name,
   18698                 :             :                  &e->where);
   18699                 :           0 :       return false;
   18700                 :             :     }
   18701                 :             : 
   18702                 :             :   /* Shall not have allocatable components.  */
   18703                 :          80 :   if (derived->attr.alloc_comp)
   18704                 :             :     {
   18705                 :           1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   18706                 :             :                  "components to be an EQUIVALENCE object",sym->name,
   18707                 :             :                  &e->where);
   18708                 :           1 :       return false;
   18709                 :             :     }
   18710                 :             : 
   18711                 :          79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   18712                 :             :     {
   18713                 :           1 :       gfc_error ("Derived type variable %qs at %L with default "
   18714                 :             :                  "initialization cannot be in EQUIVALENCE with a variable "
   18715                 :             :                  "in COMMON", sym->name, &e->where);
   18716                 :           1 :       return false;
   18717                 :             :     }
   18718                 :             : 
   18719                 :         245 :   for (; c ; c = c->next)
   18720                 :             :     {
   18721                 :         167 :       if (gfc_bt_struct (c->ts.type)
   18722                 :         167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   18723                 :             :         return false;
   18724                 :             : 
   18725                 :             :       /* Shall not be an object of sequence derived type containing a pointer
   18726                 :             :          in the structure.  */
   18727                 :         167 :       if (c->attr.pointer)
   18728                 :             :         {
   18729                 :           0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   18730                 :             :                      "component(s) cannot be an EQUIVALENCE object",
   18731                 :             :                      sym->name, &e->where);
   18732                 :           0 :           return false;
   18733                 :             :         }
   18734                 :             :     }
   18735                 :             :   return true;
   18736                 :             : }
   18737                 :             : 
   18738                 :             : 
   18739                 :             : /* Resolve equivalence object.
   18740                 :             :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   18741                 :             :    an allocatable array, an object of nonsequence derived type, an object of
   18742                 :             :    sequence derived type containing a pointer at any level of component
   18743                 :             :    selection, an automatic object, a function name, an entry name, a result
   18744                 :             :    name, a named constant, a structure component, or a subobject of any of
   18745                 :             :    the preceding objects.  A substring shall not have length zero.  A
   18746                 :             :    derived type shall not have components with default initialization nor
   18747                 :             :    shall two objects of an equivalence group be initialized.
   18748                 :             :    Either all or none of the objects shall have an protected attribute.
   18749                 :             :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   18750                 :             :    are implemented here.  */
   18751                 :             : 
   18752                 :             : static void
   18753                 :        1565 : resolve_equivalence (gfc_equiv *eq)
   18754                 :             : {
   18755                 :        1565 :   gfc_symbol *sym;
   18756                 :        1565 :   gfc_symbol *first_sym;
   18757                 :        1565 :   gfc_expr *e;
   18758                 :        1565 :   gfc_ref *r;
   18759                 :        1565 :   locus *last_where = NULL;
   18760                 :        1565 :   seq_type eq_type, last_eq_type;
   18761                 :        1565 :   gfc_typespec *last_ts;
   18762                 :        1565 :   int object, cnt_protected;
   18763                 :        1565 :   const char *msg;
   18764                 :             : 
   18765                 :        1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   18766                 :             : 
   18767                 :        1565 :   first_sym = eq->expr->symtree->n.sym;
   18768                 :             : 
   18769                 :        1565 :   cnt_protected = 0;
   18770                 :             : 
   18771                 :        4727 :   for (object = 1; eq; eq = eq->eq, object++)
   18772                 :             :     {
   18773                 :        3171 :       e = eq->expr;
   18774                 :             : 
   18775                 :        3171 :       e->ts = e->symtree->n.sym->ts;
   18776                 :             :       /* match_varspec might not know yet if it is seeing
   18777                 :             :          array reference or substring reference, as it doesn't
   18778                 :             :          know the types.  */
   18779                 :        3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   18780                 :             :         {
   18781                 :        2152 :           gfc_ref *ref = e->ref;
   18782                 :        2152 :           sym = e->symtree->n.sym;
   18783                 :             : 
   18784                 :        2152 :           if (sym->attr.dimension)
   18785                 :             :             {
   18786                 :        1855 :               ref->u.ar.as = sym->as;
   18787                 :        1855 :               ref = ref->next;
   18788                 :             :             }
   18789                 :             : 
   18790                 :             :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   18791                 :        2152 :           if (e->ts.type == BT_CHARACTER
   18792                 :         592 :               && ref
   18793                 :         371 :               && ref->type == REF_ARRAY
   18794                 :         371 :               && ref->u.ar.dimen == 1
   18795                 :         371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   18796                 :         371 :               && ref->u.ar.stride[0] == NULL)
   18797                 :             :             {
   18798                 :         370 :               gfc_expr *start = ref->u.ar.start[0];
   18799                 :         370 :               gfc_expr *end = ref->u.ar.end[0];
   18800                 :         370 :               void *mem = NULL;
   18801                 :             : 
   18802                 :             :               /* Optimize away the (:) reference.  */
   18803                 :         370 :               if (start == NULL && end == NULL)
   18804                 :             :                 {
   18805                 :           9 :                   if (e->ref == ref)
   18806                 :           0 :                     e->ref = ref->next;
   18807                 :             :                   else
   18808                 :           9 :                     e->ref->next = ref->next;
   18809                 :             :                   mem = ref;
   18810                 :             :                 }
   18811                 :             :               else
   18812                 :             :                 {
   18813                 :         361 :                   ref->type = REF_SUBSTRING;
   18814                 :         361 :                   if (start == NULL)
   18815                 :           9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   18816                 :             :                                               NULL, 1);
   18817                 :         361 :                   ref->u.ss.start = start;
   18818                 :         361 :                   if (end == NULL && e->ts.u.cl)
   18819                 :          27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   18820                 :         361 :                   ref->u.ss.end = end;
   18821                 :         361 :                   ref->u.ss.length = e->ts.u.cl;
   18822                 :         361 :                   e->ts.u.cl = NULL;
   18823                 :             :                 }
   18824                 :         370 :               ref = ref->next;
   18825                 :         370 :               free (mem);
   18826                 :             :             }
   18827                 :             : 
   18828                 :             :           /* Any further ref is an error.  */
   18829                 :        1930 :           if (ref)
   18830                 :             :             {
   18831                 :           1 :               gcc_assert (ref->type == REF_ARRAY);
   18832                 :           1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   18833                 :             :                          &ref->u.ar.where);
   18834                 :           1 :               continue;
   18835                 :             :             }
   18836                 :             :         }
   18837                 :             : 
   18838                 :        3170 :       if (!gfc_resolve_expr (e))
   18839                 :           2 :         continue;
   18840                 :             : 
   18841                 :        3168 :       sym = e->symtree->n.sym;
   18842                 :             : 
   18843                 :        3168 :       if (sym->attr.is_protected)
   18844                 :           2 :         cnt_protected++;
   18845                 :        3168 :       if (cnt_protected > 0 && cnt_protected != object)
   18846                 :             :         {
   18847                 :           2 :               gfc_error ("Either all or none of the objects in the "
   18848                 :             :                          "EQUIVALENCE set at %L shall have the "
   18849                 :             :                          "PROTECTED attribute",
   18850                 :             :                          &e->where);
   18851                 :           2 :               break;
   18852                 :             :         }
   18853                 :             : 
   18854                 :             :       /* Shall not equivalence common block variables in a PURE procedure.  */
   18855                 :        3166 :       if (sym->ns->proc_name
   18856                 :        3150 :           && sym->ns->proc_name->attr.pure
   18857                 :           7 :           && sym->attr.in_common)
   18858                 :             :         {
   18859                 :             :           /* Need to check for symbols that may have entered the pure
   18860                 :             :              procedure via a USE statement.  */
   18861                 :           7 :           bool saw_sym = false;
   18862                 :           7 :           if (sym->ns->use_stmts)
   18863                 :             :             {
   18864                 :           6 :               gfc_use_rename *r;
   18865                 :          10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   18866                 :           4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   18867                 :             :             }
   18868                 :             :           else
   18869                 :             :             saw_sym = true;
   18870                 :             : 
   18871                 :           6 :           if (saw_sym)
   18872                 :           3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   18873                 :             :                        "EQUIVALENCE object in the pure procedure %qs",
   18874                 :             :                        sym->name, &e->where, sym->ns->proc_name->name);
   18875                 :             :           break;
   18876                 :             :         }
   18877                 :             : 
   18878                 :             :       /* Shall not be a named constant.  */
   18879                 :        3159 :       if (e->expr_type == EXPR_CONSTANT)
   18880                 :             :         {
   18881                 :           0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   18882                 :             :                      "object", sym->name, &e->where);
   18883                 :           0 :           continue;
   18884                 :             :         }
   18885                 :             : 
   18886                 :        3161 :       if (e->ts.type == BT_DERIVED
   18887                 :        3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   18888                 :           2 :         continue;
   18889                 :             : 
   18890                 :             :       /* Check that the types correspond correctly:
   18891                 :             :          Note 5.28:
   18892                 :             :          A numeric sequence structure may be equivalenced to another sequence
   18893                 :             :          structure, an object of default integer type, default real type, double
   18894                 :             :          precision real type, default logical type such that components of the
   18895                 :             :          structure ultimately only become associated to objects of the same
   18896                 :             :          kind. A character sequence structure may be equivalenced to an object
   18897                 :             :          of default character kind or another character sequence structure.
   18898                 :             :          Other objects may be equivalenced only to objects of the same type and
   18899                 :             :          kind parameters.  */
   18900                 :             : 
   18901                 :             :       /* Identical types are unconditionally OK.  */
   18902                 :        3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   18903                 :        2677 :         goto identical_types;
   18904                 :             : 
   18905                 :         480 :       last_eq_type = sequence_type (*last_ts);
   18906                 :         480 :       eq_type = sequence_type (sym->ts);
   18907                 :             : 
   18908                 :             :       /* Since the pair of objects is not of the same type, mixed or
   18909                 :             :          non-default sequences can be rejected.  */
   18910                 :             : 
   18911                 :         480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   18912                 :             :                "statement at %L with different type objects");
   18913                 :         481 :       if ((object ==2
   18914                 :         480 :            && last_eq_type == SEQ_MIXED
   18915                 :           7 :            && last_where
   18916                 :           7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   18917                 :         486 :           || (eq_type == SEQ_MIXED
   18918                 :           6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   18919                 :           1 :         continue;
   18920                 :             : 
   18921                 :         479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   18922                 :             :                "statement at %L with objects of different type");
   18923                 :         483 :       if ((object ==2
   18924                 :         479 :            && last_eq_type == SEQ_NONDEFAULT
   18925                 :          50 :            && last_where
   18926                 :          49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   18927                 :         525 :           || (eq_type == SEQ_NONDEFAULT
   18928                 :          24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   18929                 :           4 :         continue;
   18930                 :             : 
   18931                 :         475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   18932                 :             :                "EQUIVALENCE statement at %L");
   18933                 :         479 :       if (last_eq_type == SEQ_CHARACTER
   18934                 :         475 :           && eq_type != SEQ_CHARACTER
   18935                 :         475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   18936                 :           4 :                 continue;
   18937                 :             : 
   18938                 :         471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   18939                 :             :                "EQUIVALENCE statement at %L");
   18940                 :         473 :       if (last_eq_type == SEQ_NUMERIC
   18941                 :         471 :           && eq_type != SEQ_NUMERIC
   18942                 :         471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   18943                 :           2 :                 continue;
   18944                 :             : 
   18945                 :        3146 : identical_types:
   18946                 :             : 
   18947                 :        3146 :       last_ts =&sym->ts;
   18948                 :        3146 :       last_where = &e->where;
   18949                 :             : 
   18950                 :        3146 :       if (!e->ref)
   18951                 :        1003 :         continue;
   18952                 :             : 
   18953                 :             :       /* Shall not be an automatic array.  */
   18954                 :        2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   18955                 :             :         {
   18956                 :           3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   18957                 :             :                      "an EQUIVALENCE object", sym->name, &e->where);
   18958                 :           3 :           continue;
   18959                 :             :         }
   18960                 :             : 
   18961                 :        2140 :       r = e->ref;
   18962                 :        4326 :       while (r)
   18963                 :             :         {
   18964                 :             :           /* Shall not be a structure component.  */
   18965                 :        2187 :           if (r->type == REF_COMPONENT)
   18966                 :             :             {
   18967                 :           0 :               gfc_error ("Structure component %qs at %L cannot be an "
   18968                 :             :                          "EQUIVALENCE object",
   18969                 :           0 :                          r->u.c.component->name, &e->where);
   18970                 :           0 :               break;
   18971                 :             :             }
   18972                 :             : 
   18973                 :             :           /* A substring shall not have length zero.  */
   18974                 :        2187 :           if (r->type == REF_SUBSTRING)
   18975                 :             :             {
   18976                 :         341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   18977                 :             :                 {
   18978                 :           1 :                   gfc_error ("Substring at %L has length zero",
   18979                 :             :                              &r->u.ss.start->where);
   18980                 :           1 :                   break;
   18981                 :             :                 }
   18982                 :             :             }
   18983                 :        2186 :           r = r->next;
   18984                 :             :         }
   18985                 :             :     }
   18986                 :        1565 : }
   18987                 :             : 
   18988                 :             : 
   18989                 :             : /* Function called by resolve_fntype to flag other symbols used in the
   18990                 :             :    length type parameter specification of function results.  */
   18991                 :             : 
   18992                 :             : static bool
   18993                 :        4079 : flag_fn_result_spec (gfc_expr *expr,
   18994                 :             :                      gfc_symbol *sym,
   18995                 :             :                      int *f ATTRIBUTE_UNUSED)
   18996                 :             : {
   18997                 :        4079 :   gfc_namespace *ns;
   18998                 :        4079 :   gfc_symbol *s;
   18999                 :             : 
   19000                 :        4079 :   if (expr->expr_type == EXPR_VARIABLE)
   19001                 :             :     {
   19002                 :        1375 :       s = expr->symtree->n.sym;
   19003                 :        2147 :       for (ns = s->ns; ns; ns = ns->parent)
   19004                 :        2147 :         if (!ns->parent)
   19005                 :             :           break;
   19006                 :             : 
   19007                 :        1375 :       if (sym == s)
   19008                 :             :         {
   19009                 :           1 :           gfc_error ("Self reference in character length expression "
   19010                 :             :                      "for %qs at %L", sym->name, &expr->where);
   19011                 :           1 :           return true;
   19012                 :             :         }
   19013                 :             : 
   19014                 :        1374 :       if (!s->fn_result_spec
   19015                 :        1374 :           && s->attr.flavor == FL_PARAMETER)
   19016                 :             :         {
   19017                 :             :           /* Function contained in a module.... */
   19018                 :          63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   19019                 :             :             {
   19020                 :          32 :               gfc_symtree *st;
   19021                 :          32 :               s->fn_result_spec = 1;
   19022                 :             :               /* Make sure that this symbol is translated as a module
   19023                 :             :                  variable.  */
   19024                 :          32 :               st = gfc_get_unique_symtree (ns);
   19025                 :          32 :               st->n.sym = s;
   19026                 :          32 :               s->refs++;
   19027                 :          32 :             }
   19028                 :             :           /* ... which is use associated and called.  */
   19029                 :          31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   19030                 :           0 :                         ||
   19031                 :             :                   /* External function matched with an interface.  */
   19032                 :           0 :                   (s->ns->proc_name
   19033                 :           0 :                    && ((s->ns == ns
   19034                 :           0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   19035                 :           0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   19036                 :           0 :                    && s->ns->proc_name->attr.function))
   19037                 :          31 :             s->fn_result_spec = 1;
   19038                 :             :         }
   19039                 :             :     }
   19040                 :             :   return false;
   19041                 :             : }
   19042                 :             : 
   19043                 :             : 
   19044                 :             : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   19045                 :             : 
   19046                 :             : static void
   19047                 :      326171 : resolve_fntype (gfc_namespace *ns)
   19048                 :             : {
   19049                 :      326171 :   gfc_entry_list *el;
   19050                 :      326171 :   gfc_symbol *sym;
   19051                 :             : 
   19052                 :      326171 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   19053                 :             :     return;
   19054                 :             : 
   19055                 :             :   /* If there are any entries, ns->proc_name is the entry master
   19056                 :             :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   19057                 :      173000 :   if (ns->entries)
   19058                 :         564 :     sym = ns->entries->sym;
   19059                 :             :   else
   19060                 :             :     sym = ns->proc_name;
   19061                 :      173000 :   if (sym->result == sym
   19062                 :      140647 :       && sym->ts.type == BT_UNKNOWN
   19063                 :           6 :       && !gfc_set_default_type (sym, 0, NULL)
   19064                 :      173004 :       && !sym->attr.untyped)
   19065                 :             :     {
   19066                 :           3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   19067                 :             :                  sym->name, &sym->declared_at);
   19068                 :           3 :       sym->attr.untyped = 1;
   19069                 :             :     }
   19070                 :             : 
   19071                 :       11318 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   19072                 :        1530 :       && !sym->attr.contained
   19073                 :         250 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   19074                 :      173000 :       && gfc_check_symbol_access (sym))
   19075                 :             :     {
   19076                 :           0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   19077                 :             :                       "%L of PRIVATE type %qs", sym->name,
   19078                 :           0 :                       &sym->declared_at, sym->ts.u.derived->name);
   19079                 :             :     }
   19080                 :             : 
   19081                 :      173000 :     if (ns->entries)
   19082                 :        1189 :     for (el = ns->entries->next; el; el = el->next)
   19083                 :             :       {
   19084                 :         625 :         if (el->sym->result == el->sym
   19085                 :         413 :             && el->sym->ts.type == BT_UNKNOWN
   19086                 :           2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   19087                 :         627 :             && !el->sym->attr.untyped)
   19088                 :             :           {
   19089                 :           2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   19090                 :             :                        el->sym->name, &el->sym->declared_at);
   19091                 :           2 :             el->sym->attr.untyped = 1;
   19092                 :             :           }
   19093                 :             :       }
   19094                 :             : 
   19095                 :      173000 :   if (sym->ts.type == BT_CHARACTER
   19096                 :        6616 :       && sym->ts.u.cl->length
   19097                 :        1737 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   19098                 :        1732 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   19099                 :             : }
   19100                 :             : 
   19101                 :             : 
   19102                 :             : /* 12.3.2.1.1 Defined operators.  */
   19103                 :             : 
   19104                 :             : static bool
   19105                 :         371 : check_uop_procedure (gfc_symbol *sym, locus where)
   19106                 :             : {
   19107                 :         371 :   gfc_formal_arglist *formal;
   19108                 :             : 
   19109                 :         371 :   if (!sym->attr.function)
   19110                 :             :     {
   19111                 :           3 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   19112                 :             :                  sym->name, &where);
   19113                 :           3 :       return false;
   19114                 :             :     }
   19115                 :             : 
   19116                 :         368 :   if (sym->ts.type == BT_CHARACTER
   19117                 :          15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   19118                 :           2 :       && !(sym->result && ((sym->result->ts.u.cl
   19119                 :           2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   19120                 :             :     {
   19121                 :           2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   19122                 :             :                  "character length", sym->name, &where);
   19123                 :           2 :       return false;
   19124                 :             :     }
   19125                 :             : 
   19126                 :         366 :   formal = gfc_sym_get_dummy_args (sym);
   19127                 :         366 :   if (!formal || !formal->sym)
   19128                 :             :     {
   19129                 :           1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   19130                 :             :                  "one argument", sym->name, &where);
   19131                 :           1 :       return false;
   19132                 :             :     }
   19133                 :             : 
   19134                 :         365 :   if (formal->sym->attr.intent != INTENT_IN)
   19135                 :             :     {
   19136                 :           0 :       gfc_error ("First argument of operator interface at %L must be "
   19137                 :             :                  "INTENT(IN)", &where);
   19138                 :           0 :       return false;
   19139                 :             :     }
   19140                 :             : 
   19141                 :         365 :   if (formal->sym->attr.optional)
   19142                 :             :     {
   19143                 :           0 :       gfc_error ("First argument of operator interface at %L cannot be "
   19144                 :             :                  "optional", &where);
   19145                 :           0 :       return false;
   19146                 :             :     }
   19147                 :             : 
   19148                 :         365 :   formal = formal->next;
   19149                 :         365 :   if (!formal || !formal->sym)
   19150                 :             :     return true;
   19151                 :             : 
   19152                 :         232 :   if (formal->sym->attr.intent != INTENT_IN)
   19153                 :             :     {
   19154                 :           0 :       gfc_error ("Second argument of operator interface at %L must be "
   19155                 :             :                  "INTENT(IN)", &where);
   19156                 :           0 :       return false;
   19157                 :             :     }
   19158                 :             : 
   19159                 :         232 :   if (formal->sym->attr.optional)
   19160                 :             :     {
   19161                 :           1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   19162                 :             :                  "optional", &where);
   19163                 :           1 :       return false;
   19164                 :             :     }
   19165                 :             : 
   19166                 :         231 :   if (formal->next)
   19167                 :             :     {
   19168                 :           2 :       gfc_error ("Operator interface at %L must have, at most, two "
   19169                 :             :                  "arguments", &where);
   19170                 :           2 :       return false;
   19171                 :             :     }
   19172                 :             : 
   19173                 :             :   return true;
   19174                 :             : }
   19175                 :             : 
   19176                 :             : static void
   19177                 :      326837 : gfc_resolve_uops (gfc_symtree *symtree)
   19178                 :             : {
   19179                 :      326837 :   gfc_interface *itr;
   19180                 :             : 
   19181                 :      326837 :   if (symtree == NULL)
   19182                 :             :     return;
   19183                 :             : 
   19184                 :         333 :   gfc_resolve_uops (symtree->left);
   19185                 :         333 :   gfc_resolve_uops (symtree->right);
   19186                 :             : 
   19187                 :         662 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   19188                 :         329 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   19189                 :             : }
   19190                 :             : 
   19191                 :             : 
   19192                 :             : /* Examine all of the expressions associated with a program unit,
   19193                 :             :    assign types to all intermediate expressions, make sure that all
   19194                 :             :    assignments are to compatible types and figure out which names
   19195                 :             :    refer to which functions or subroutines.  It doesn't check code
   19196                 :             :    block, which is handled by gfc_resolve_code.  */
   19197                 :             : 
   19198                 :             : static void
   19199                 :      328609 : resolve_types (gfc_namespace *ns)
   19200                 :             : {
   19201                 :      328609 :   gfc_namespace *n;
   19202                 :      328609 :   gfc_charlen *cl;
   19203                 :      328609 :   gfc_data *d;
   19204                 :      328609 :   gfc_equiv *eq;
   19205                 :      328609 :   gfc_namespace* old_ns = gfc_current_ns;
   19206                 :      328609 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   19207                 :             : 
   19208                 :      328609 :   if (ns->types_resolved)
   19209                 :             :     return;
   19210                 :             : 
   19211                 :             :   /* Check that all IMPLICIT types are ok.  */
   19212                 :      326172 :   if (!ns->seen_implicit_none)
   19213                 :             :     {
   19214                 :             :       unsigned letter;
   19215                 :     8204599 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   19216                 :     7900725 :         if (ns->set_flag[letter]
   19217                 :     7900725 :             && !resolve_typespec_used (&ns->default_type[letter],
   19218                 :             :                                        &ns->implicit_loc[letter], NULL))
   19219                 :             :           return;
   19220                 :             :     }
   19221                 :             : 
   19222                 :      326171 :   gfc_current_ns = ns;
   19223                 :             : 
   19224                 :      326171 :   resolve_entries (ns);
   19225                 :             : 
   19226                 :      326171 :   resolve_common_vars (&ns->blank_common, false);
   19227                 :      326171 :   resolve_common_blocks (ns->common_root);
   19228                 :             : 
   19229                 :      326171 :   resolve_contained_functions (ns);
   19230                 :             : 
   19231                 :      326171 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   19232                 :      326123 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   19233                 :      180500 :     gfc_resolve_formal_arglist (ns->proc_name);
   19234                 :             : 
   19235                 :      326171 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   19236                 :             : 
   19237                 :      417398 :   for (cl = ns->cl_list; cl; cl = cl->next)
   19238                 :       91227 :     resolve_charlen (cl);
   19239                 :             : 
   19240                 :      326171 :   gfc_traverse_ns (ns, resolve_symbol);
   19241                 :             : 
   19242                 :      326171 :   resolve_fntype (ns);
   19243                 :             : 
   19244                 :      371462 :   for (n = ns->contained; n; n = n->sibling)
   19245                 :             :     {
   19246                 :             :       /* Exclude final wrappers with the test for the artificial attribute.  */
   19247                 :       45291 :       if (gfc_pure (ns->proc_name)
   19248                 :           5 :           && !gfc_pure (n->proc_name)
   19249                 :       45291 :           && !n->proc_name->attr.artificial)
   19250                 :           0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   19251                 :             :                    "also be PURE", n->proc_name->name,
   19252                 :             :                    &n->proc_name->declared_at);
   19253                 :             : 
   19254                 :       45291 :       resolve_types (n);
   19255                 :             :     }
   19256                 :             : 
   19257                 :      326171 :   forall_flag = 0;
   19258                 :      326171 :   gfc_do_concurrent_flag = 0;
   19259                 :      326171 :   gfc_check_interfaces (ns);
   19260                 :             : 
   19261                 :      326171 :   gfc_traverse_ns (ns, resolve_values);
   19262                 :             : 
   19263                 :      326171 :   if (ns->save_all || (!flag_automatic && !recursive))
   19264                 :         296 :     gfc_save_all (ns);
   19265                 :             : 
   19266                 :      326171 :   iter_stack = NULL;
   19267                 :      328668 :   for (d = ns->data; d; d = d->next)
   19268                 :        2497 :     resolve_data (d);
   19269                 :             : 
   19270                 :      326171 :   iter_stack = NULL;
   19271                 :      326171 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   19272                 :             : 
   19273                 :      326171 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   19274                 :             : 
   19275                 :      327736 :   for (eq = ns->equiv; eq; eq = eq->next)
   19276                 :        1565 :     resolve_equivalence (eq);
   19277                 :             : 
   19278                 :             :   /* Warn about unused labels.  */
   19279                 :      326171 :   if (warn_unused_label)
   19280                 :        4421 :     warn_unused_fortran_label (ns->st_labels);
   19281                 :             : 
   19282                 :      326171 :   gfc_resolve_uops (ns->uop_root);
   19283                 :             : 
   19284                 :      326171 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   19285                 :             : 
   19286                 :      326171 :   gfc_resolve_omp_declare (ns);
   19287                 :             : 
   19288                 :      326171 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   19289                 :             : 
   19290                 :      326171 :   ns->types_resolved = 1;
   19291                 :             : 
   19292                 :      326171 :   gfc_current_ns = old_ns;
   19293                 :             : }
   19294                 :             : 
   19295                 :             : 
   19296                 :             : /* Call gfc_resolve_code recursively.  */
   19297                 :             : 
   19298                 :             : static void
   19299                 :      328664 : resolve_codes (gfc_namespace *ns)
   19300                 :             : {
   19301                 :      328664 :   gfc_namespace *n;
   19302                 :      328664 :   bitmap_obstack old_obstack;
   19303                 :             : 
   19304                 :      328664 :   if (ns->resolved == 1)
   19305                 :       12794 :     return;
   19306                 :             : 
   19307                 :      361216 :   for (n = ns->contained; n; n = n->sibling)
   19308                 :       45346 :     resolve_codes (n);
   19309                 :             : 
   19310                 :      315870 :   gfc_current_ns = ns;
   19311                 :             : 
   19312                 :             :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   19313                 :      315870 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   19314                 :      304495 :     cs_base = NULL;
   19315                 :             : 
   19316                 :             :   /* Set to an out of range value.  */
   19317                 :      315870 :   current_entry_id = -1;
   19318                 :             : 
   19319                 :      315870 :   old_obstack = labels_obstack;
   19320                 :      315870 :   bitmap_obstack_initialize (&labels_obstack);
   19321                 :             : 
   19322                 :      315870 :   gfc_resolve_oacc_declare (ns);
   19323                 :      315870 :   gfc_resolve_oacc_routines (ns);
   19324                 :      315870 :   gfc_resolve_omp_local_vars (ns);
   19325                 :      315870 :   if (ns->omp_allocate)
   19326                 :          55 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   19327                 :      315870 :   gfc_resolve_code (ns->code, ns);
   19328                 :             : 
   19329                 :      315869 :   bitmap_obstack_release (&labels_obstack);
   19330                 :      315869 :   labels_obstack = old_obstack;
   19331                 :             : }
   19332                 :             : 
   19333                 :             : 
   19334                 :             : /* This function is called after a complete program unit has been compiled.
   19335                 :             :    Its purpose is to examine all of the expressions associated with a program
   19336                 :             :    unit, assign types to all intermediate expressions, make sure that all
   19337                 :             :    assignments are to compatible types and figure out which names refer to
   19338                 :             :    which functions or subroutines.  */
   19339                 :             : 
   19340                 :             : void
   19341                 :      287805 : gfc_resolve (gfc_namespace *ns)
   19342                 :             : {
   19343                 :      287805 :   gfc_namespace *old_ns;
   19344                 :      287805 :   code_stack *old_cs_base;
   19345                 :      287805 :   struct gfc_omp_saved_state old_omp_state;
   19346                 :             : 
   19347                 :      287805 :   if (ns->resolved)
   19348                 :        4487 :     return;
   19349                 :             : 
   19350                 :      283318 :   ns->resolved = -1;
   19351                 :      283318 :   old_ns = gfc_current_ns;
   19352                 :      283318 :   old_cs_base = cs_base;
   19353                 :             : 
   19354                 :             :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   19355                 :             :      body, we should clear any state associated to it, so that say NS's
   19356                 :             :      DO loops are not interpreted as OpenMP loops.  */
   19357                 :      283318 :   if (!ns->construct_entities)
   19358                 :      271943 :     gfc_omp_save_and_clear_state (&old_omp_state);
   19359                 :             : 
   19360                 :      283318 :   resolve_types (ns);
   19361                 :      283318 :   component_assignment_level = 0;
   19362                 :      283318 :   resolve_codes (ns);
   19363                 :             : 
   19364                 :      283317 :   if (ns->omp_assumes)
   19365                 :          13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   19366                 :             : 
   19367                 :      283317 :   gfc_current_ns = old_ns;
   19368                 :      283317 :   cs_base = old_cs_base;
   19369                 :      283317 :   ns->resolved = 1;
   19370                 :             : 
   19371                 :      283317 :   gfc_run_passes (ns);
   19372                 :             : 
   19373                 :      283317 :   if (!ns->construct_entities)
   19374                 :      271942 :     gfc_omp_restore_state (&old_omp_state);
   19375                 :             : }
        

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.