LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.1 % 10141 9539
Test Date: 2026-12-05 16:10:02 Functions: 99.6 % 260 259
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Perform type resolution on the various structures.
       2              :    Copyright (C) 2001-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andy Vaught
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #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              : /* The dummy whose character length or array bounds are currently being
      86              :    resolved as a specification expression.  */
      87              : static gfc_symbol *specification_expr_symbol = NULL;
      88              : 
      89              : /* The id of the last entry seen.  */
      90              : static int current_entry_id;
      91              : 
      92              : /* We use bitmaps to determine if a branch target is valid.  */
      93              : static bitmap_obstack labels_obstack;
      94              : 
      95              : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function.  */
      96              : static bool inquiry_argument = false;
      97              : 
      98              : static bool
      99          494 : entry_dummy_seen_p (gfc_symbol *sym)
     100              : {
     101          494 :   gfc_entry_list *entry;
     102          494 :   gfc_formal_arglist *formal;
     103              : 
     104          494 :   gcc_checking_assert (sym->attr.dummy && sym->ns == gfc_current_ns);
     105              : 
     106          494 :   for (entry = gfc_current_ns->entries;
     107          501 :        entry && entry->id <= current_entry_id;
     108            7 :        entry = entry->next)
     109          819 :     for (formal = entry->sym->formal; formal; formal = formal->next)
     110          812 :       if (formal->sym && sym->name == formal->sym->name)
     111              :         return true;
     112              : 
     113              :   return false;
     114              : }
     115              : 
     116              : 
     117              : /* Is the symbol host associated?  */
     118              : static bool
     119        58560 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        63833 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         5531 :       if (sym->ns == ns)
     124              :         return true;
     125              :     }
     126              : 
     127              :   return false;
     128              : }
     129              : 
     130              : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
     131              :    an ABSTRACT derived-type.  If where is not NULL, an error message with that
     132              :    locus is printed, optionally using name.  */
     133              : 
     134              : static bool
     135      1644278 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1644278 :   if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
     138              :     {
     139            5 :       if (where)
     140              :         {
     141            5 :           if (name)
     142            4 :             gfc_error ("%qs at %L is of the ABSTRACT type %qs",
     143              :                        name, where, ts->u.derived->name);
     144              :           else
     145            1 :             gfc_error ("ABSTRACT type %qs used at %L",
     146              :                        ts->u.derived->name, where);
     147              :         }
     148              : 
     149              :       return false;
     150              :     }
     151              : 
     152              :   return true;
     153              : }
     154              : 
     155              : 
     156              : static bool
     157         5954 : check_proc_interface (gfc_symbol *ifc, locus *where)
     158              : {
     159              :   /* Several checks for F08:C1216.  */
     160         5954 :   if (ifc->attr.procedure)
     161              :     {
     162            2 :       gfc_error ("Interface %qs at %L is declared "
     163              :                  "in a later PROCEDURE statement", ifc->name, where);
     164            2 :       return false;
     165              :     }
     166         5952 :   if (ifc->generic)
     167              :     {
     168              :       /* For generic interfaces, check if there is
     169              :          a specific procedure with the same name.  */
     170              :       gfc_interface *gen = ifc->generic;
     171           12 :       while (gen && strcmp (gen->sym->name, ifc->name) != 0)
     172            5 :         gen = gen->next;
     173            7 :       if (!gen)
     174              :         {
     175            4 :           gfc_error ("Interface %qs at %L may not be generic",
     176              :                      ifc->name, where);
     177            4 :           return false;
     178              :         }
     179              :     }
     180         5948 :   if (ifc->attr.proc == PROC_ST_FUNCTION)
     181              :     {
     182            4 :       gfc_error ("Interface %qs at %L may not be a statement function",
     183              :                  ifc->name, where);
     184            4 :       return false;
     185              :     }
     186         5944 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     187         5944 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     188           17 :     ifc->attr.intrinsic = 1;
     189         5944 :   if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
     190              :     {
     191            3 :       gfc_error ("Intrinsic procedure %qs not allowed in "
     192              :                  "PROCEDURE statement at %L", ifc->name, where);
     193            3 :       return false;
     194              :     }
     195         5941 :   if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
     196              :     {
     197            7 :       gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
     198            7 :       return false;
     199              :     }
     200              :   return true;
     201              : }
     202              : 
     203              : 
     204              : static void resolve_symbol (gfc_symbol *sym);
     205              : 
     206              : 
     207              : /* Resolve the interface for a PROCEDURE declaration or procedure pointer.  */
     208              : 
     209              : static bool
     210         2153 : resolve_procedure_interface (gfc_symbol *sym)
     211              : {
     212         2153 :   gfc_symbol *ifc = sym->ts.interface;
     213              : 
     214         2153 :   if (!ifc)
     215              :     return true;
     216              : 
     217         1992 :   if (ifc == sym)
     218              :     {
     219            2 :       gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
     220              :                  sym->name, &sym->declared_at);
     221            2 :       return false;
     222              :     }
     223         1990 :   if (!check_proc_interface (ifc, &sym->declared_at))
     224              :     return false;
     225              : 
     226         1981 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     227              :     {
     228              :       /* Resolve interface and copy attributes.  */
     229         1695 :       resolve_symbol (ifc);
     230         1695 :       if (ifc->attr.intrinsic)
     231           14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     232              : 
     233         1695 :       if (ifc->result)
     234              :         {
     235          784 :           sym->ts = ifc->result->ts;
     236          784 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     237          784 :           sym->attr.pointer = ifc->result->attr.pointer;
     238          784 :           sym->attr.dimension = ifc->result->attr.dimension;
     239          784 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     240          784 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     241          784 :           sym->result = sym;
     242              :         }
     243              :       else
     244              :         {
     245          911 :           sym->ts = ifc->ts;
     246          911 :           sym->attr.allocatable = ifc->attr.allocatable;
     247          911 :           sym->attr.pointer = ifc->attr.pointer;
     248          911 :           sym->attr.dimension = ifc->attr.dimension;
     249          911 :           sym->attr.class_ok = ifc->attr.class_ok;
     250          911 :           sym->as = gfc_copy_array_spec (ifc->as);
     251              :         }
     252         1695 :       sym->ts.interface = ifc;
     253         1695 :       sym->attr.function = ifc->attr.function;
     254         1695 :       sym->attr.subroutine = ifc->attr.subroutine;
     255              : 
     256         1695 :       sym->attr.pure = ifc->attr.pure;
     257         1695 :       sym->attr.elemental = ifc->attr.elemental;
     258         1695 :       sym->attr.contiguous = ifc->attr.contiguous;
     259         1695 :       sym->attr.recursive = ifc->attr.recursive;
     260         1695 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     261         1695 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     262         1695 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     263              :       /* Copy char length.  */
     264         1695 :       if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
     265              :         {
     266           45 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
     267           45 :           if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
     268           53 :               && !gfc_resolve_expr (sym->ts.u.cl->length))
     269              :             return false;
     270              :         }
     271              :     }
     272              : 
     273              :   return true;
     274              : }
     275              : 
     276              : 
     277              : /* Resolve types of formal argument lists.  These have to be done early so that
     278              :    the formal argument lists of module procedures can be copied to the
     279              :    containing module before the individual procedures are resolved
     280              :    individually.  We also resolve argument lists of procedures in interface
     281              :    blocks because they are self-contained scoping units.
     282              : 
     283              :    Since a dummy argument cannot be a non-dummy procedure, the only
     284              :    resort left for untyped names are the IMPLICIT types.  */
     285              : 
     286              : void
     287       568645 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       568645 :   gfc_formal_arglist *f;
     290       568645 :   gfc_symbol *sym;
     291       568645 :   bool saved_specification_expr;
     292       568645 :   int i;
     293              : 
     294       568645 :   if (proc->result != NULL)
     295       353004 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       568645 :   if (gfc_elemental (proc)
     300       405164 :       || sym->attr.pointer || sym->attr.allocatable
     301       961205 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       178445 :       proc->attr.always_explicit = 1;
     304       178445 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       568645 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       568645 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1469271 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       900628 :       gfc_array_spec *as;
     313       900628 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       900628 :       sym = f->sym;
     316              : 
     317       900628 :       if (sym == NULL)
     318              :         {
     319              :           /* Alternate return placeholder.  */
     320          171 :           if (gfc_elemental (proc))
     321            1 :             gfc_error ("Alternate return specifier in elemental subroutine "
     322              :                        "%qs at %L is not allowed", proc->name,
     323              :                        &proc->declared_at);
     324          171 :           if (proc->attr.function)
     325            1 :             gfc_error ("Alternate return specifier in function "
     326              :                        "%qs at %L is not allowed", proc->name,
     327              :                        &proc->declared_at);
     328          171 :           continue;
     329              :         }
     330              : 
     331          611 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     332       901068 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       900457 :       if (strcmp (proc->name, sym->name) == 0)
     336              :         {
     337            2 :           gfc_error ("Self-referential argument "
     338              :                      "%qs at %L is not allowed", sym->name,
     339              :                      &proc->declared_at);
     340            2 :           break;
     341              :         }
     342              : 
     343       900455 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          904 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       900455 :       if (sym->attr.subroutine || sym->attr.external)
     347              :         {
     348          913 :           if (sym->attr.flavor == FL_UNKNOWN)
     349            9 :             gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
     350              :         }
     351              :       else
     352              :         {
     353       899542 :           if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
     354         3688 :               && (!sym->attr.function || sym->result == sym))
     355         3650 :             gfc_set_default_type (sym, 1, sym->ns);
     356              :         }
     357              : 
     358       900455 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       915335 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       900455 :       saved_specification_expr = specification_expr;
     362       900455 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       900455 :       specification_expr = true;
     364       900455 :       specification_expr_symbol = sym;
     365       900455 :       gfc_resolve_array_spec (as, 0);
     366       900455 :       specification_expr = saved_specification_expr;
     367       900455 :       specification_expr_symbol = saved_specification_expr_symbol;
     368              : 
     369              :       /* We can't tell if an array with dimension (:) is assumed or deferred
     370              :          shape until we know if it has the pointer or allocatable attributes.
     371              :       */
     372       900455 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        13128 :           && ((sym->ts.type != BT_CLASS
     374        11866 :                && !(sym->attr.pointer || sym->attr.allocatable))
     375         5583 :               || (sym->ts.type == BT_CLASS
     376         1262 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     377         1049 :                        || CLASS_DATA (sym)->attr.allocatable)))
     378         8119 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         8118 :           as->type = AS_ASSUMED_SHAPE;
     381        18778 :           for (i = 0; i < as->rank; i++)
     382        10660 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       140573 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       125743 :           || (as && as->type == AS_ASSUMED_RANK)
     387       845298 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       834842 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        12394 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11896 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10883 :                   || CLASS_DATA (sym)->attr.target))
     392       833331 :           || sym->attr.optional)
     393              :         {
     394        84478 :           proc->attr.always_explicit = 1;
     395        84478 :           if (proc->result)
     396        39328 :             proc->result->attr.always_explicit = 1;
     397              :         }
     398              : 
     399              :       /* If the flavor is unknown at this point, it has to be a variable.
     400              :          A procedure specification would have already set the type.  */
     401              : 
     402       900455 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        53125 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       900455 :       if (gfc_pure (proc))
     406              :         {
     407       330092 :           if (sym->attr.flavor == FL_PROCEDURE)
     408              :             {
     409              :               /* F08:C1279.  */
     410           29 :               if (!gfc_pure (sym))
     411              :                 {
     412            1 :                   gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
     413              :                             "also be PURE", sym->name, &sym->declared_at);
     414            1 :                   continue;
     415              :                 }
     416              :             }
     417       330063 :           else if (!sym->attr.pointer)
     418              :             {
     419       330049 :               if (proc->attr.function && sym->attr.intent != INTENT_IN)
     420              :                 {
     421          111 :                   if (sym->attr.value)
     422          110 :                     gfc_notify_std (GFC_STD_F2008, "Argument %qs"
     423              :                                     " of pure function %qs at %L with VALUE "
     424              :                                     "attribute but without INTENT(IN)",
     425              :                                     sym->name, proc->name, &sym->declared_at);
     426              :                   else
     427            1 :                     gfc_error ("Argument %qs of pure function %qs at %L must "
     428              :                                "be INTENT(IN) or VALUE", sym->name, proc->name,
     429              :                                &sym->declared_at);
     430              :                 }
     431              : 
     432       330049 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
     433              :                 {
     434          159 :                   if (sym->attr.value)
     435          159 :                     gfc_notify_std (GFC_STD_F2008, "Argument %qs"
     436              :                                     " of pure subroutine %qs at %L with VALUE "
     437              :                                     "attribute but without INTENT", sym->name,
     438              :                                     proc->name, &sym->declared_at);
     439              :                   else
     440            0 :                     gfc_error ("Argument %qs of pure subroutine %qs at %L "
     441              :                                "must have its INTENT specified or have the "
     442              :                                "VALUE attribute", sym->name, proc->name,
     443              :                                &sym->declared_at);
     444              :                 }
     445              :             }
     446              : 
     447              :           /* F08:C1278a.  */
     448       330091 :           if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
     449              :             {
     450            1 :               gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
     451              :                          " may not be polymorphic", sym->name, proc->name,
     452              :                          &sym->declared_at);
     453            1 :               continue;
     454              :             }
     455              :         }
     456              : 
     457       900453 :       if (proc->attr.implicit_pure)
     458              :         {
     459        26879 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          337 :               if (!gfc_pure (sym))
     462          305 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        26542 :           else if (!sym->attr.pointer)
     465              :             {
     466        25740 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2766 :                   && !sym->value)
     468         2766 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        25740 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4363 :                   && !sym->value)
     472         4363 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       900453 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       304134 :           if (sym->attr.codimension
     480       304133 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     481          977 :                   && CLASS_DATA (sym)->attr.codimension))
     482              :             {
     483            3 :               gfc_error ("Coarray dummy argument %qs at %L to elemental "
     484              :                          "procedure", sym->name, &sym->declared_at);
     485            3 :               continue;
     486              :             }
     487              : 
     488       304131 :           if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     489          975 :                           && CLASS_DATA (sym)->as))
     490              :             {
     491            2 :               gfc_error ("Argument %qs of elemental procedure at %L must "
     492              :                          "be scalar", sym->name, &sym->declared_at);
     493            2 :               continue;
     494              :             }
     495              : 
     496       304129 :           if (sym->attr.allocatable
     497       304128 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     498          974 :                   && CLASS_DATA (sym)->attr.allocatable))
     499              :             {
     500            2 :               gfc_error ("Argument %qs of elemental procedure at %L cannot "
     501              :                          "have the ALLOCATABLE attribute", sym->name,
     502              :                          &sym->declared_at);
     503            2 :               continue;
     504              :             }
     505              : 
     506       304127 :           if (sym->attr.pointer
     507       304126 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     508          973 :                   && CLASS_DATA (sym)->attr.class_pointer))
     509              :             {
     510            2 :               gfc_error ("Argument %qs of elemental procedure at %L cannot "
     511              :                          "have the POINTER attribute", sym->name,
     512              :                          &sym->declared_at);
     513            2 :               continue;
     514              :             }
     515              : 
     516       304125 :           if (sym->attr.flavor == FL_PROCEDURE)
     517              :             {
     518            2 :               gfc_error ("Dummy procedure %qs not allowed in elemental "
     519              :                          "procedure %qs at %L", sym->name, proc->name,
     520              :                          &sym->declared_at);
     521            2 :               continue;
     522              :             }
     523              : 
     524              :           /* Fortran 2008 Corrigendum 1, C1290a.  */
     525       304123 :           if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
     526              :             {
     527            2 :               gfc_error ("Argument %qs of elemental procedure %qs at %L must "
     528              :                          "have its INTENT specified or have the VALUE "
     529              :                          "attribute", sym->name, proc->name,
     530              :                          &sym->declared_at);
     531            2 :               continue;
     532              :             }
     533              :         }
     534              : 
     535              :       /* Each dummy shall be specified to be scalar.  */
     536       900440 :       if (proc->attr.proc == PROC_ST_FUNCTION)
     537              :         {
     538          307 :           if (sym->as != NULL)
     539              :             {
     540              :               /* F03:C1263 (R1238) The function-name and each dummy-arg-name
     541              :                  shall be specified, explicitly or implicitly, to be scalar.  */
     542            1 :               gfc_error ("Argument %qs of statement function %qs at %L "
     543              :                          "must be scalar", sym->name, proc->name,
     544              :                          &proc->declared_at);
     545            1 :               continue;
     546              :             }
     547              : 
     548          306 :           if (sym->ts.type == BT_CHARACTER)
     549              :             {
     550           48 :               gfc_charlen *cl = sym->ts.u.cl;
     551           48 :               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
     552              :                 {
     553            0 :                   gfc_error ("Character-valued argument %qs of statement "
     554              :                              "function at %L must have constant length",
     555              :                              sym->name, &sym->declared_at);
     556            0 :                   continue;
     557              :                 }
     558              :             }
     559              :         }
     560              :     }
     561       568645 :   if (sym)
     562       568553 :     sym->formal_resolved = 1;
     563       568645 :   gfc_current_ns = orig_current_ns;
     564       568645 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1977763 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1977763 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       359104 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       356087 :   gfc_resolve_formal_arglist (sym);
     578              : }
     579              : 
     580              : 
     581              : /* Given a namespace, resolve all formal argument lists within the namespace.
     582              :  */
     583              : 
     584              : static void
     585       372899 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       372899 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        39605 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        39605 :   bool t;
     598              : 
     599        39605 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        39605 :       && sym->ns->parent
     601         1498 :       && sym->ns->parent->proc_name
     602         1498 :       && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
     603            0 :       && !strcmp (sym->name, sym->ns->parent->proc_name->name))
     604            0 :     gfc_error ("Contained procedure %qs at %L has the same name as its "
     605              :                "encompassing procedure", sym->name, &sym->declared_at);
     606              : 
     607              :   /* If this namespace is not a function or an entry master function,
     608              :      ignore it.  */
     609        39605 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        11571 :       || sym->attr.entry_master)
     611              :     return;
     612              : 
     613        11382 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        11382 :   if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
     618              :     {
     619           58 :       t = gfc_set_default_type (sym->result, 0, ns);
     620              : 
     621           58 :       if (!t && !sym->result->attr.untyped)
     622              :         {
     623           19 :           if (sym->result == sym)
     624            1 :             gfc_error ("Contained function %qs at %L has no IMPLICIT type",
     625              :                        sym->name, &sym->declared_at);
     626           18 :           else if (!sym->result->attr.proc_pointer)
     627            0 :             gfc_error ("Result %qs of contained function %qs at %L has "
     628              :                        "no IMPLICIT type", sym->result->name, sym->name,
     629              :                        &sym->result->declared_at);
     630           19 :           sym->result->attr.untyped = 1;
     631              :         }
     632              :     }
     633              : 
     634              :   /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
     635              :      type, lists the only ways a character length value of * can be used:
     636              :      dummy arguments of procedures, named constants, function results and
     637              :      in allocate statements if the allocate_object is an assumed length dummy
     638              :      in external functions.  Internal function results and results of module
     639              :      procedures are not on this list, ergo, not permitted.  */
     640              : 
     641        11382 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1283 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1283 :       if ((!cl || !cl->length) && !sym->result->ts.deferred)
     645              :         {
     646              :           /* See if this is a module-procedure and adapt error message
     647              :              accordingly.  */
     648            4 :           bool module_proc;
     649            4 :           gcc_assert (ns->parent && ns->parent->proc_name);
     650            4 :           module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
     651              : 
     652            7 :           gfc_error (module_proc
     653              :                      ? G_("Character-valued module procedure %qs at %L"
     654              :                           " must not be assumed length")
     655              :                      : G_("Character-valued internal function %qs at %L"
     656              :                           " must not be assumed length"),
     657              :                      sym->name, &sym->declared_at);
     658              :         }
     659              :     }
     660              : }
     661              : 
     662              : 
     663              : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
     664              :    introduce duplicates.  */
     665              : 
     666              : static void
     667         1503 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     668              : {
     669         1503 :   gfc_formal_arglist *f, *new_arglist;
     670         1503 :   gfc_symbol *new_sym;
     671              : 
     672         2698 :   for (; new_args != NULL; new_args = new_args->next)
     673              :     {
     674         1195 :       new_sym = new_args->sym;
     675              :       /* See if this arg is already in the formal argument list.  */
     676         2294 :       for (f = proc->formal; f; f = f->next)
     677              :         {
     678         1565 :           if (new_sym == f->sym)
     679              :             break;
     680              :         }
     681              : 
     682         1195 :       if (f)
     683          466 :         continue;
     684              : 
     685              :       /* Add a new argument.  Argument order is not important.  */
     686          729 :       new_arglist = gfc_get_formal_arglist ();
     687          729 :       new_arglist->sym = new_sym;
     688          729 :       new_arglist->next = proc->formal;
     689          729 :       proc->formal  = new_arglist;
     690              :     }
     691         1503 : }
     692              : 
     693              : 
     694              : /* Flag the arguments that are not present in all entries.  */
     695              : 
     696              : static void
     697         1503 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     698              : {
     699         1503 :   gfc_formal_arglist *f, *head;
     700         1503 :   head = new_args;
     701              : 
     702         3146 :   for (f = proc->formal; f; f = f->next)
     703              :     {
     704         1643 :       if (f->sym == NULL)
     705           36 :         continue;
     706              : 
     707         2858 :       for (new_args = head; new_args; new_args = new_args->next)
     708              :         {
     709         2401 :           if (new_args->sym == f->sym)
     710              :             break;
     711              :         }
     712              : 
     713         1607 :       if (new_args)
     714         1150 :         continue;
     715              : 
     716          457 :       f->sym->attr.not_always_present = 1;
     717              :     }
     718         1503 : }
     719              : 
     720              : 
     721              : /* Resolve alternate entry points.  If a symbol has multiple entry points we
     722              :    create a new master symbol for the main routine, and turn the existing
     723              :    symbol into an entry point.  */
     724              : 
     725              : static void
     726       411997 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       411997 :   gfc_namespace *old_ns;
     729       411997 :   gfc_code *c;
     730       411997 :   gfc_symbol *proc;
     731       411997 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       411997 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       411997 :   static int master_count = 0;
     735              : 
     736       411997 :   if (ns->proc_name == NULL)
     737       411288 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       411948 :   if (!ns->entries)
     742              :     return;
     743              : 
     744              :   /* We may already have resolved alternate entry points.  */
     745          960 :   if (ns->proc_name->attr.entry_master)
     746              :     return;
     747              : 
     748              :   /* If this isn't a procedure something has gone horribly wrong.  */
     749          709 :   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
     750              : 
     751              :   /* Remember the current namespace.  */
     752          709 :   old_ns = gfc_current_ns;
     753              : 
     754          709 :   gfc_current_ns = ns;
     755              : 
     756              :   /* Add the main entry point to the list of entry points.  */
     757          709 :   el = gfc_get_entry_list ();
     758          709 :   el->sym = ns->proc_name;
     759          709 :   el->id = 0;
     760          709 :   el->next = ns->entries;
     761          709 :   ns->entries = el;
     762          709 :   ns->proc_name->attr.entry = 1;
     763              : 
     764              :   /* If it is a module function, it needs to be in the right namespace
     765              :      so that gfc_get_fake_result_decl can gather up the results. The
     766              :      need for this arose in get_proc_name, where these beasts were
     767              :      left in their own namespace, to keep prior references linked to
     768              :      the entry declaration.*/
     769          709 :   if (ns->proc_name->attr.function
     770          596 :       && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
     771          189 :     el->sym->ns = ns;
     772              : 
     773              :   /* Do the same for entries where the master is not a module
     774              :      procedure.  These are retained in the module namespace because
     775              :      of the module procedure declaration.  */
     776         1503 :   for (el = el->next; el; el = el->next)
     777          794 :     if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
     778            0 :           && el->sym->attr.mod_proc)
     779            0 :       el->sym->ns = ns;
     780          709 :   el = ns->entries;
     781              : 
     782              :   /* Add an entry statement for it.  */
     783          709 :   c = gfc_get_code (EXEC_ENTRY);
     784          709 :   c->ext.entry = el;
     785          709 :   c->next = ns->code;
     786          709 :   ns->code = c;
     787              : 
     788              :   /* Create a new symbol for the master function.  */
     789              :   /* Give the internal function a unique name (within this file).
     790              :      Also include the function name so the user has some hope of figuring
     791              :      out what is going on.  */
     792          709 :   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
     793          709 :             master_count++, ns->proc_name->name);
     794          709 :   gfc_get_ha_symbol (name, &proc);
     795          709 :   gcc_assert (proc != NULL);
     796              : 
     797          709 :   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
     798          709 :   if (ns->proc_name->attr.subroutine)
     799          113 :     gfc_add_subroutine (&proc->attr, proc->name, NULL);
     800              :   else
     801              :     {
     802          596 :       gfc_symbol *sym;
     803          596 :       gfc_typespec *ts, *fts;
     804          596 :       gfc_array_spec *as, *fas;
     805          596 :       gfc_add_function (&proc->attr, proc->name, NULL);
     806          596 :       proc->result = proc;
     807          596 :       fas = ns->entries->sym->as;
     808          596 :       fas = fas ? fas : ns->entries->sym->result->as;
     809          596 :       fts = &ns->entries->sym->result->ts;
     810          596 :       if (fts->type == BT_UNKNOWN)
     811           51 :         fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
     812         1120 :       for (el = ns->entries->next; el; el = el->next)
     813              :         {
     814          635 :           ts = &el->sym->result->ts;
     815          635 :           as = el->sym->as;
     816          635 :           as = as ? as : el->sym->result->as;
     817          635 :           if (ts->type == BT_UNKNOWN)
     818           61 :             ts = gfc_get_default_type (el->sym->result->name, NULL);
     819              : 
     820          635 :           if (! gfc_compare_types (ts, fts)
     821          527 :               || (el->sym->result->attr.dimension
     822          527 :                   != ns->entries->sym->result->attr.dimension)
     823          635 :               || (el->sym->result->attr.pointer
     824          527 :                   != ns->entries->sym->result->attr.pointer))
     825              :             break;
     826           65 :           else if (as && fas && ns->entries->sym->result != el->sym->result
     827          589 :                       && gfc_compare_array_spec (as, fas) == 0)
     828            5 :             gfc_error ("Function %s at %L has entries with mismatched "
     829              :                        "array specifications", ns->entries->sym->name,
     830            5 :                        &ns->entries->sym->declared_at);
     831              :           /* The characteristics need to match and thus both need to have
     832              :              the same string length, i.e. both len=*, or both len=4.
     833              :              Having both len=<variable> is also possible, but difficult to
     834              :              check at compile time.  */
     835          522 :           else if (ts->type == BT_CHARACTER
     836          113 :                    && (el->sym->result->attr.allocatable
     837          113 :                        != ns->entries->sym->result->attr.allocatable))
     838              :             {
     839            3 :               gfc_error ("Function %s at %L has entry %s with mismatched "
     840              :                          "characteristics", ns->entries->sym->name,
     841              :                          &ns->entries->sym->declared_at, el->sym->name);
     842            3 :               goto cleanup;
     843              :             }
     844          519 :           else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
     845          110 :                    && (((ts->u.cl->length && !fts->u.cl->length)
     846          109 :                         ||(!ts->u.cl->length && fts->u.cl->length))
     847           90 :                        || (ts->u.cl->length
     848           53 :                            && ts->u.cl->length->expr_type
     849           53 :                               != fts->u.cl->length->expr_type)
     850           90 :                        || (ts->u.cl->length
     851           53 :                            && ts->u.cl->length->expr_type == EXPR_CONSTANT
     852           52 :                            && mpz_cmp (ts->u.cl->length->value.integer,
     853           52 :                                        fts->u.cl->length->value.integer) != 0)))
     854           21 :             gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
     855              :                             "entries returning variables of different "
     856              :                             "string lengths", ns->entries->sym->name,
     857           21 :                             &ns->entries->sym->declared_at);
     858          498 :           else if (el->sym->result->attr.allocatable
     859          498 :                    != ns->entries->sym->result->attr.allocatable)
     860              :             break;
     861              :         }
     862              : 
     863          593 :       if (el == NULL)
     864              :         {
     865          485 :           sym = ns->entries->sym->result;
     866              :           /* All result types the same.  */
     867          485 :           proc->ts = *fts;
     868          485 :           if (sym->attr.dimension)
     869           63 :             gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
     870          485 :           if (sym->attr.pointer)
     871           78 :             gfc_add_pointer (&proc->attr, NULL);
     872          485 :           if (sym->attr.allocatable)
     873           24 :             gfc_add_allocatable (&proc->attr, NULL);
     874              :         }
     875              :       else
     876              :         {
     877              :           /* Otherwise the result will be passed through a union by
     878              :              reference.  */
     879          108 :           proc->attr.mixed_entry_master = 1;
     880          346 :           for (el = ns->entries; el; el = el->next)
     881              :             {
     882          238 :               sym = el->sym->result;
     883          238 :               if (sym->attr.dimension)
     884              :                 {
     885            1 :                   if (el == ns->entries)
     886            0 :                     gfc_error ("FUNCTION result %s cannot be an array in "
     887              :                                "FUNCTION %s at %L", sym->name,
     888            0 :                                ns->entries->sym->name, &sym->declared_at);
     889              :                   else
     890            1 :                     gfc_error ("ENTRY result %s cannot be an array in "
     891              :                                "FUNCTION %s at %L", sym->name,
     892            1 :                                ns->entries->sym->name, &sym->declared_at);
     893              :                 }
     894          237 :               else if (sym->attr.pointer)
     895              :                 {
     896            1 :                   if (el == ns->entries)
     897            1 :                     gfc_error ("FUNCTION result %s cannot be a POINTER in "
     898              :                                "FUNCTION %s at %L", sym->name,
     899            1 :                                ns->entries->sym->name, &sym->declared_at);
     900              :                   else
     901            0 :                     gfc_error ("ENTRY result %s cannot be a POINTER in "
     902              :                                "FUNCTION %s at %L", sym->name,
     903            0 :                                ns->entries->sym->name, &sym->declared_at);
     904              :                 }
     905          236 :               else if (sym->attr.allocatable)
     906              :                 {
     907            0 :                   if (el == ns->entries)
     908            0 :                     gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
     909              :                                "FUNCTION %s at %L", sym->name,
     910            0 :                                ns->entries->sym->name, &sym->declared_at);
     911              :                   else
     912            0 :                     gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
     913              :                                "FUNCTION %s at %L", sym->name,
     914            0 :                                ns->entries->sym->name, &sym->declared_at);
     915              :                 }
     916              :               else
     917              :                 {
     918          236 :                   ts = &sym->ts;
     919          236 :                   if (ts->type == BT_UNKNOWN)
     920            9 :                     ts = gfc_get_default_type (sym->name, NULL);
     921          236 :                   switch (ts->type)
     922              :                     {
     923           85 :                     case BT_INTEGER:
     924           85 :                       if (ts->kind == gfc_default_integer_kind)
     925              :                         sym = NULL;
     926              :                       break;
     927          100 :                     case BT_REAL:
     928          100 :                       if (ts->kind == gfc_default_real_kind
     929           18 :                           || ts->kind == gfc_default_double_kind)
     930              :                         sym = NULL;
     931              :                       break;
     932           20 :                     case BT_COMPLEX:
     933           20 :                       if (ts->kind == gfc_default_complex_kind)
     934              :                         sym = NULL;
     935              :                       break;
     936           28 :                     case BT_LOGICAL:
     937           28 :                       if (ts->kind == gfc_default_logical_kind)
     938              :                         sym = NULL;
     939              :                       break;
     940              :                     case BT_UNKNOWN:
     941              :                       /* We will issue error elsewhere.  */
     942              :                       sym = NULL;
     943              :                       break;
     944              :                     default:
     945              :                       break;
     946              :                     }
     947            3 :                   if (sym)
     948              :                     {
     949            3 :                       if (el == ns->entries)
     950            1 :                         gfc_error ("FUNCTION result %s cannot be of type %s "
     951              :                                    "in FUNCTION %s at %L", sym->name,
     952            1 :                                    gfc_typename (ts), ns->entries->sym->name,
     953              :                                    &sym->declared_at);
     954              :                       else
     955            2 :                         gfc_error ("ENTRY result %s cannot be of type %s "
     956              :                                    "in FUNCTION %s at %L", sym->name,
     957            2 :                                    gfc_typename (ts), ns->entries->sym->name,
     958              :                                    &sym->declared_at);
     959              :                     }
     960              :                 }
     961              :             }
     962              :         }
     963              :     }
     964              : 
     965          108 : cleanup:
     966          709 :   proc->attr.access = ACCESS_PRIVATE;
     967          709 :   proc->attr.entry_master = 1;
     968              : 
     969              :   /* Merge all the entry point arguments.  */
     970         2212 :   for (el = ns->entries; el; el = el->next)
     971         1503 :     merge_argument_lists (proc, el->sym->formal);
     972              : 
     973              :   /* Check the master formal arguments for any that are not
     974              :      present in all entry points.  */
     975         2212 :   for (el = ns->entries; el; el = el->next)
     976         1503 :     check_argument_lists (proc, el->sym->formal);
     977              : 
     978              :   /* Use the master function for the function body.  */
     979          709 :   ns->proc_name = proc;
     980              : 
     981              :   /* Finalize the new symbols.  */
     982          709 :   gfc_commit_symbols ();
     983              : 
     984              :   /* Restore the original namespace.  */
     985          709 :   gfc_current_ns = old_ns;
     986              : }
     987              : 
     988              : 
     989              : /* Forward declaration.  */
     990              : static bool is_non_constant_shape_array (gfc_symbol *sym);
     991              : 
     992              : 
     993              : /* Resolve common variables.  */
     994              : static void
     995       374877 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       374877 :   gfc_symbol *csym = common_block->head;
     998       374877 :   gfc_gsymbol *gsym;
     999              : 
    1000       380930 :   for (; csym; csym = csym->common_next)
    1001              :     {
    1002         6053 :       gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
    1003         6053 :       if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
    1004              :         {
    1005            3 :           if (csym->common_block)
    1006            2 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
    1007              :                            "COMMON block at %L", gsym->name,
    1008              :                            &gsym->where, &csym->common_block->where);
    1009              :           else
    1010            1 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
    1011              :                            "COMMON block", gsym->name, &gsym->where);
    1012              :         }
    1013              : 
    1014              :       /* gfc_add_in_common may have been called before, but the reported errors
    1015              :          have been ignored to continue parsing.
    1016              :          We do the checks again here, unless the symbol is USE associated.  */
    1017         6053 :       if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
    1018              :         {
    1019         5780 :           gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
    1020         5780 :           gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
    1021              :                           &common_block->where);
    1022              :         }
    1023              : 
    1024         6053 :       if (csym->value || csym->attr.data)
    1025              :         {
    1026          149 :           if (!csym->ns->is_block_data)
    1027           33 :             gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
    1028              :                             "but only in BLOCK DATA initialization is "
    1029              :                             "allowed", csym->name, &csym->declared_at);
    1030          116 :           else if (!named_common)
    1031            8 :             gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
    1032              :                             "in a blank COMMON but initialization is only "
    1033              :                             "allowed in named common blocks", csym->name,
    1034              :                             &csym->declared_at);
    1035              :         }
    1036              : 
    1037         6053 :       if (UNLIMITED_POLY (csym))
    1038            1 :         gfc_error_now ("%qs at %L cannot appear in COMMON "
    1039              :                        "[F2008:C5100]", csym->name, &csym->declared_at);
    1040              : 
    1041         6053 :       if (csym->attr.dimension && is_non_constant_shape_array (csym))
    1042              :         {
    1043            1 :           gfc_error_now ("Automatic object %qs at %L cannot appear in "
    1044              :                          "COMMON at %L", csym->name, &csym->declared_at,
    1045              :                          &common_block->where);
    1046              :           /* Avoid confusing follow-on error.  */
    1047            1 :           csym->error = 1;
    1048              :         }
    1049              : 
    1050         6053 :       if (csym->ts.type != BT_DERIVED)
    1051         6006 :         continue;
    1052              : 
    1053           47 :       if (!(csym->ts.u.derived->attr.sequence
    1054            3 :             || csym->ts.u.derived->attr.is_bind_c))
    1055            2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1056              :                        "has neither the SEQUENCE nor the BIND(C) "
    1057              :                        "attribute", csym->name, &csym->declared_at);
    1058           47 :       if (csym->ts.u.derived->attr.alloc_comp)
    1059            3 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1060              :                        "has an ultimate component that is "
    1061              :                        "allocatable", csym->name, &csym->declared_at);
    1062           47 :       if (gfc_has_default_initializer (csym->ts.u.derived))
    1063            2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1064              :                        "may not have default initializer", csym->name,
    1065              :                        &csym->declared_at);
    1066              : 
    1067           47 :       if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
    1068           16 :         gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
    1069              :     }
    1070       374877 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       373430 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       373430 :   gfc_symbol *sym = NULL;
    1077       373430 :   gfc_gsymbol * gsym;
    1078              : 
    1079       373430 :   if (common_root == NULL)
    1080       373308 :     return;
    1081              : 
    1082         1978 :   if (common_root->left)
    1083          257 :     resolve_common_blocks (common_root->left);
    1084         1978 :   if (common_root->right)
    1085          274 :     resolve_common_blocks (common_root->right);
    1086              : 
    1087         1978 :   resolve_common_vars (common_root->n.common, true);
    1088              : 
    1089              :   /* The common name is a global name - in Fortran 2003 also if it has a
    1090              :      C binding name, since Fortran 2008 only the C binding name is a global
    1091              :      identifier.  */
    1092         1978 :   if (!common_root->n.common->binding_label
    1093         1978 :       || gfc_notification_std (GFC_STD_F2008))
    1094              :     {
    1095         3812 :       gsym = gfc_find_gsymbol (gfc_gsym_root,
    1096         1906 :                                common_root->n.common->name);
    1097              : 
    1098          820 :       if (gsym && gfc_notification_std (GFC_STD_F2008)
    1099           14 :           && gsym->type == GSYM_COMMON
    1100         1919 :           && ((common_root->n.common->binding_label
    1101            6 :                && (!gsym->binding_label
    1102            0 :                    || strcmp (common_root->n.common->binding_label,
    1103              :                               gsym->binding_label) != 0))
    1104            7 :               || (!common_root->n.common->binding_label
    1105            7 :                   && gsym->binding_label)))
    1106              :         {
    1107            6 :           gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
    1108              :                      "identifier and must thus have the same binding name "
    1109              :                      "as the same-named COMMON block at %L: %s vs %s",
    1110            6 :                      common_root->n.common->name, &common_root->n.common->where,
    1111              :                      &gsym->where,
    1112              :                      common_root->n.common->binding_label
    1113              :                      ? common_root->n.common->binding_label : "(blank)",
    1114            6 :                      gsym->binding_label ? gsym->binding_label : "(blank)");
    1115            6 :           return;
    1116              :         }
    1117              : 
    1118         1900 :       if (gsym && gsym->type != GSYM_COMMON
    1119            1 :           && !common_root->n.common->binding_label)
    1120              :         {
    1121            0 :           gfc_error ("COMMON block %qs at %L uses the same global identifier "
    1122              :                      "as entity at %L",
    1123            0 :                      common_root->n.common->name, &common_root->n.common->where,
    1124              :                      &gsym->where);
    1125            0 :           return;
    1126              :         }
    1127          814 :       if (gsym && gsym->type != GSYM_COMMON)
    1128              :         {
    1129            1 :           gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
    1130              :                      "%L sharing the identifier with global non-COMMON-block "
    1131            1 :                      "entity at %L", common_root->n.common->name,
    1132            1 :                      &common_root->n.common->where, &gsym->where);
    1133            1 :           return;
    1134              :         }
    1135         1086 :       if (!gsym)
    1136              :         {
    1137         1086 :           gsym = gfc_get_gsymbol (common_root->n.common->name, false);
    1138         1086 :           gsym->type = GSYM_COMMON;
    1139         1086 :           gsym->where = common_root->n.common->where;
    1140         1086 :           gsym->defined = 1;
    1141              :         }
    1142         1899 :       gsym->used = 1;
    1143              :     }
    1144              : 
    1145         1971 :   if (common_root->n.common->binding_label)
    1146              :     {
    1147           76 :       gsym = gfc_find_gsymbol (gfc_gsym_root,
    1148              :                                common_root->n.common->binding_label);
    1149           76 :       if (gsym && gsym->type != GSYM_COMMON)
    1150              :         {
    1151            1 :           gfc_error ("COMMON block at %L with binding label %qs uses the same "
    1152              :                      "global identifier as entity at %L",
    1153              :                      &common_root->n.common->where,
    1154            1 :                      common_root->n.common->binding_label, &gsym->where);
    1155            1 :           return;
    1156              :         }
    1157           57 :       if (!gsym)
    1158              :         {
    1159           57 :           gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
    1160           57 :           gsym->type = GSYM_COMMON;
    1161           57 :           gsym->where = common_root->n.common->where;
    1162           57 :           gsym->defined = 1;
    1163              :         }
    1164           75 :       gsym->used = 1;
    1165              :     }
    1166              : 
    1167         1970 :   gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
    1168         1970 :   if (sym == NULL)
    1169              :     return;
    1170              : 
    1171          122 :   if (sym->attr.flavor == FL_PARAMETER)
    1172            2 :     gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
    1173            2 :                sym->name, &common_root->n.common->where, &sym->declared_at);
    1174              : 
    1175          122 :   if (sym->attr.external)
    1176            1 :     gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
    1177            1 :                sym->name, &common_root->n.common->where);
    1178              : 
    1179          122 :   if (sym->attr.intrinsic)
    1180            2 :     gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
    1181            2 :                sym->name, &common_root->n.common->where);
    1182          120 :   else if (sym->attr.result
    1183          120 :            || gfc_is_function_return_value (sym, gfc_current_ns))
    1184            1 :     gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
    1185              :                     "that is also a function result", sym->name,
    1186            1 :                     &common_root->n.common->where);
    1187          119 :   else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
    1188            5 :            && sym->attr.proc != PROC_ST_FUNCTION)
    1189            3 :     gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
    1190              :                     "that is also a global procedure", sym->name,
    1191            3 :                     &common_root->n.common->where);
    1192              : }
    1193              : 
    1194              : 
    1195              : /* Resolve contained function types.  Because contained functions can call one
    1196              :    another, they have to be worked out before any of the contained procedures
    1197              :    can be resolved.
    1198              : 
    1199              :    The good news is that if a function doesn't already have a type, the only
    1200              :    way it can get one is through an IMPLICIT type or a RESULT variable, because
    1201              :    by definition contained functions are contained namespace they're contained
    1202              :    in, not in a sibling or parent namespace.  */
    1203              : 
    1204              : static void
    1205       372899 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       372899 :   gfc_namespace *child;
    1208       372899 :   gfc_entry_list *el;
    1209              : 
    1210       372899 :   resolve_formal_arglists (ns);
    1211              : 
    1212       411997 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        39098 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        39098 :       resolve_contained_fntype (child->proc_name, child);
    1219        39605 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       372899 : }
    1223              : 
    1224              : 
    1225              : 
    1226              : /* A Parameterized Derived Type constructor must contain values for
    1227              :    the PDT KIND parameters or they must have a default initializer.
    1228              :    Go through the constructor picking out the KIND expressions,
    1229              :    storing them in 'param_list' and then call gfc_get_pdt_instance
    1230              :    to obtain the PDT instance.  */
    1231              : 
    1232              : static gfc_actual_arglist *param_list, *param_tail, *param;
    1233              : 
    1234              : static bool
    1235          501 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
    1236              : {
    1237          501 :   param = gfc_get_actual_arglist ();
    1238          501 :   if (!param_list)
    1239          373 :     param_list = param_tail = param;
    1240              :   else
    1241              :     {
    1242          128 :       param_tail->next = param;
    1243          128 :       param_tail = param_tail->next;
    1244              :     }
    1245              : 
    1246          501 :   param_tail->name = c->name;
    1247          501 :   if (expr)
    1248          501 :     param_tail->expr = gfc_copy_expr (expr);
    1249            0 :   else if (c->initializer)
    1250            0 :     param_tail->expr = gfc_copy_expr (c->initializer);
    1251              :   else
    1252              :     {
    1253            0 :       param_tail->spec_type = SPEC_ASSUMED;
    1254            0 :       if (c->attr.pdt_kind)
    1255              :         {
    1256            0 :           gfc_error ("The KIND parameter %qs in the PDT constructor "
    1257              :                      "at %C has no value", param->name);
    1258            0 :           return false;
    1259              :         }
    1260              :     }
    1261              : 
    1262              :   return true;
    1263              : }
    1264              : 
    1265              : static bool
    1266          427 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
    1267              :                      gfc_symbol *derived)
    1268              : {
    1269          427 :   gfc_constructor *cons = NULL;
    1270          427 :   gfc_component *comp;
    1271          427 :   bool t = true;
    1272              : 
    1273          427 :   if (expr && expr->expr_type == EXPR_STRUCTURE)
    1274          391 :     cons = gfc_constructor_first (expr->value.constructor);
    1275           36 :   else if (constr)
    1276           36 :     cons = *constr;
    1277          427 :   gcc_assert (cons);
    1278              : 
    1279          427 :   comp = derived->components;
    1280              : 
    1281         1401 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1282              :     {
    1283          974 :       if (cons->expr
    1284          974 :           && cons->expr->expr_type == EXPR_STRUCTURE
    1285           18 :           && comp->ts.type == BT_DERIVED)
    1286              :         {
    1287           18 :           t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
    1288           18 :           if (!t)
    1289              :             return t;
    1290              :         }
    1291          956 :       else if (comp->ts.type == BT_DERIVED)
    1292              :         {
    1293           36 :           t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
    1294           36 :           if (!t)
    1295              :             return t;
    1296              :         }
    1297          920 :      else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
    1298          501 :                && derived->attr.pdt_template)
    1299              :         {
    1300          501 :           t = get_pdt_spec_expr (comp, cons->expr);
    1301          501 :           if (!t)
    1302              :             return t;
    1303              :         }
    1304              :     }
    1305              :   return t;
    1306              : }
    1307              : 
    1308              : 
    1309              : static bool resolve_fl_derived0 (gfc_symbol *sym);
    1310              : static bool resolve_fl_struct (gfc_symbol *sym);
    1311              : 
    1312              : 
    1313              : /* Resolve all of the elements of a structure constructor and make sure that
    1314              :    the types are correct. The 'init' flag indicates that the given
    1315              :    constructor is an initializer.  */
    1316              : 
    1317              : static bool
    1318        68292 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        68292 :   gfc_constructor *cons;
    1321        68292 :   gfc_component *comp;
    1322        68292 :   bool t;
    1323        68292 :   symbol_attribute a;
    1324              : 
    1325        68292 :   t = true;
    1326              : 
    1327        68292 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        65027 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        64877 :         resolve_fl_derived0 (expr->ts.u.derived);
    1331              :       else
    1332          150 :         resolve_fl_struct (expr->ts.u.derived);
    1333              : 
    1334              :       /* If this is a Parameterized Derived Type template, find the
    1335              :          instance corresponding to the PDT kind parameters.  */
    1336        65027 :       if (expr->ts.u.derived->attr.pdt_template)
    1337              :         {
    1338          373 :           param_list = NULL;
    1339          373 :           t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
    1340          373 :           if (!t)
    1341              :             return t;
    1342          373 :           gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
    1343              : 
    1344          373 :           expr->param_list = gfc_copy_actual_arglist (param_list);
    1345              : 
    1346          373 :           if (param_list)
    1347          373 :             gfc_free_actual_arglist (param_list);
    1348              : 
    1349          373 :           if (!expr->ts.u.derived->attr.pdt_type)
    1350              :             return false;
    1351              :         }
    1352              :     }
    1353              : 
    1354              :   /* A constructor may have references if it is the result of substituting a
    1355              :      parameter variable.  In this case we just pull out the component we
    1356              :      want.  */
    1357        68292 :   if (expr->ref)
    1358          166 :     comp = expr->ref->u.c.sym->components;
    1359        68126 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        68124 :            && expr->ts.u.derived)
    1362        68124 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        68290 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       299837 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       163259 :       int rank;
    1371              : 
    1372       163259 :       if (!cons->expr)
    1373        11300 :         continue;
    1374              : 
    1375              :       /* Unions use an EXPR_NULL contrived expression to tell the translation
    1376              :          phase to generate an initializer of the appropriate length.
    1377              :          Ignore it here.  */
    1378       151959 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       151944 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       151944 :       rank = comp->as ? comp->as->rank : 0;
    1388       151944 :       if (comp->ts.type == BT_CLASS
    1389         2061 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1390         2060 :           && CLASS_DATA (comp)->as)
    1391          695 :         rank = CLASS_DATA (comp)->as->rank;
    1392              : 
    1393       151944 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          276 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       151944 :       if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
    1397         1027 :           && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
    1398              :         {
    1399            4 :           gfc_error ("The rank of the element in the structure "
    1400              :                      "constructor at %L does not match that of the "
    1401              :                      "component (%d/%d)", &cons->expr->where,
    1402              :                      cons->expr->rank, rank);
    1403            4 :           t = false;
    1404              :         }
    1405              : 
    1406              :       /* If we don't have the right type, try to convert it.  */
    1407              : 
    1408       265586 :       if (!comp->attr.proc_pointer &&
    1409       113642 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        13849 :           if (strcmp (comp->name, "_extends") == 0)
    1412              :             {
    1413              :               /* Can afford to be brutal with the _extends initializer.
    1414              :                  The derived type can get lost because it is PRIVATE
    1415              :                  but it is not usage constrained by the standard.  */
    1416        10199 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3650 :           else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
    1419              :             {
    1420            2 :               gfc_error ("The element in the structure constructor at %L, "
    1421              :                          "for pointer component %qs, is %s but should be %s",
    1422            2 :                          &cons->expr->where, comp->name,
    1423            2 :                          gfc_basic_typename (cons->expr->ts.type),
    1424              :                          gfc_basic_typename (comp->ts.type));
    1425            2 :               t = false;
    1426              :             }
    1427         3648 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3567 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3567 :               if (t)
    1431       151944 :                 t = t2;
    1432              :             }
    1433              :         }
    1434              : 
    1435              :       /* For strings, the length of the constructor should be the same as
    1436              :          the one of the structure, ensure this if the lengths are known at
    1437              :          compile time and when we are dealing with PARAMETER or structure
    1438              :          constructors. Skip for PDT types which have type parameters.  */
    1439       151944 :       if (!IS_PDT (expr) && cons->expr->ts.type == BT_CHARACTER
    1440         4452 :           && comp->ts.type == BT_CHARACTER
    1441         4414 :           && comp->ts.u.cl && comp->ts.u.cl->length
    1442         3001 :           && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1443         2984 :           && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
    1444         1089 :           && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1445         1089 :           && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
    1446         1089 :           && comp->ts.u.cl->length->ts.type == BT_INTEGER
    1447         1089 :           && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
    1448         1089 :                       comp->ts.u.cl->length->value.integer) != 0)
    1449              :         {
    1450           11 :           if (comp->attr.pointer)
    1451              :             {
    1452            3 :               HOST_WIDE_INT la, lb;
    1453            3 :               la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
    1454            3 :               lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
    1455            3 :               gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
    1456              :                          "component %qs in constructor at %L",
    1457            3 :                          la, lb, comp->name, &cons->expr->where);
    1458            3 :               t = false;
    1459              :             }
    1460              : 
    1461           11 :           if (cons->expr->expr_type == EXPR_VARIABLE
    1462            4 :               && cons->expr->rank != 0
    1463            2 :               && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
    1464              :             {
    1465              :               /* Wrap the parameter in an array constructor (EXPR_ARRAY)
    1466              :                  to make use of the gfc_resolve_character_array_constructor
    1467              :                  machinery.  The expression is later simplified away to
    1468              :                  an array of string literals.  */
    1469            1 :               gfc_expr *para = cons->expr;
    1470            1 :               cons->expr = gfc_get_expr ();
    1471            1 :               cons->expr->ts = para->ts;
    1472            1 :               cons->expr->where = para->where;
    1473            1 :               cons->expr->expr_type = EXPR_ARRAY;
    1474            1 :               cons->expr->rank = para->rank;
    1475            1 :               cons->expr->corank = para->corank;
    1476            1 :               cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
    1477            1 :               gfc_constructor_append_expr (&cons->expr->value.constructor,
    1478            1 :                                            para, &cons->expr->where);
    1479              :             }
    1480              : 
    1481           11 :           if (cons->expr->expr_type == EXPR_ARRAY)
    1482              :             {
    1483              :               /* Rely on the cleanup of the namespace to deal correctly with
    1484              :                  the old charlen.  (There was a block here that attempted to
    1485              :                  remove the charlen but broke the chain in so doing.)  */
    1486            5 :               cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    1487            5 :               cons->expr->ts.u.cl->length_from_typespec = true;
    1488            5 :               cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
    1489            5 :               gfc_resolve_character_array_constructor (cons->expr);
    1490              :             }
    1491              :         }
    1492              : 
    1493       151944 :       if (cons->expr->expr_type == EXPR_NULL
    1494        45523 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        22648 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1496         1354 :                || (comp->ts.type == BT_CLASS
    1497         1352 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1498         1105 :                        || CLASS_DATA (comp)->attr.allocatable))))
    1499              :         {
    1500            2 :           t = false;
    1501            2 :           gfc_error ("The NULL in the structure constructor at %L is "
    1502              :                      "being applied to component %qs, which is neither "
    1503              :                      "a POINTER nor ALLOCATABLE", &cons->expr->where,
    1504              :                      comp->name);
    1505              :         }
    1506              : 
    1507       151944 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        17385 :           gfc_symbol *s2 = NULL;
    1511        17385 :           gfc_component *c2;
    1512        17385 :           const char *name;
    1513        17385 :           char err[200];
    1514              : 
    1515        17385 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        17385 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        17373 :           else if (cons->expr->expr_type == EXPR_FUNCTION)
    1522              :             {
    1523            0 :               s2 = cons->expr->symtree->n.sym->result;
    1524            0 :               name = cons->expr->symtree->n.sym->result->name;
    1525              :             }
    1526        17373 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        16903 :               s2 = cons->expr->symtree->n.sym;
    1529        16903 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        16915 :           if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
    1533              :                                              err, sizeof (err), NULL, NULL))
    1534              :             {
    1535            2 :               gfc_error_opt (0, "Interface mismatch for procedure-pointer "
    1536              :                              "component %qs in structure constructor at %L:"
    1537            2 :                              " %s", comp->name, &cons->expr->where, err);
    1538            2 :               return false;
    1539              :             }
    1540              :         }
    1541              : 
    1542              :       /* Validate shape, except for dynamic or PDT arrays.  */
    1543       151942 :       if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
    1544         2807 :           && comp->as && !comp->attr.allocatable && !comp->attr.pointer
    1545         1991 :           && !comp->attr.pdt_array)
    1546              :         {
    1547         1664 :           mpz_t len;
    1548         1664 :           mpz_init (len);
    1549         5103 :           for (int n = 0; n < rank; n++)
    1550              :             {
    1551         1780 :               if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
    1552         1775 :                   || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
    1553              :                 {
    1554            5 :                   gfc_error ("Bad array spec of component %qs referenced in "
    1555              :                              "structure constructor at %L",
    1556            5 :                              comp->name, &cons->expr->where);
    1557            5 :                   t = false;
    1558            5 :                   break;
    1559         1775 :                 };
    1560         1775 :               if (cons->expr->shape == NULL)
    1561           12 :                 continue;
    1562         1763 :               mpz_set_ui (len, 1);
    1563         1763 :               mpz_add (len, len, comp->as->upper[n]->value.integer);
    1564         1763 :               mpz_sub (len, len, comp->as->lower[n]->value.integer);
    1565         1763 :               if (mpz_cmp (cons->expr->shape[n], len) != 0)
    1566              :                 {
    1567            9 :                   gfc_error ("The shape of component %qs in the structure "
    1568              :                              "constructor at %L differs from the shape of the "
    1569              :                              "declared component for dimension %d (%ld/%ld)",
    1570              :                              comp->name, &cons->expr->where, n+1,
    1571              :                              mpz_get_si (cons->expr->shape[n]),
    1572              :                              mpz_get_si (len));
    1573            9 :                   t = false;
    1574              :                 }
    1575              :             }
    1576         1664 :           mpz_clear (len);
    1577              :         }
    1578              : 
    1579       151942 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        24493 :           || cons->expr->expr_type == EXPR_NULL)
    1581       140625 :         continue;
    1582              : 
    1583        11317 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        11317 :       if (!a.pointer && !a.target)
    1586              :         {
    1587            1 :           t = false;
    1588            1 :           gfc_error ("The element in the structure constructor at %L, "
    1589              :                      "for pointer component %qs should be a POINTER or "
    1590            1 :                      "a TARGET", &cons->expr->where, comp->name);
    1591              :         }
    1592              : 
    1593        11317 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596        11246 :           if (a.allocatable)
    1597              :             {
    1598            0 :               t = false;
    1599            0 :               gfc_error ("Pointer initialization target at %L "
    1600            0 :                          "must not be ALLOCATABLE", &cons->expr->where);
    1601              :             }
    1602        11246 :           if (!a.save)
    1603              :             {
    1604            0 :               t = false;
    1605            0 :               gfc_error ("Pointer initialization target at %L "
    1606            0 :                          "must have the SAVE attribute", &cons->expr->where);
    1607              :             }
    1608              :         }
    1609              : 
    1610              :       /* F2023:C770: A designator that is an initial-data-target shall ...
    1611              :          not have a vector subscript.  */
    1612        11317 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        22633 :           && gfc_has_vector_index (cons->expr))
    1614              :         {
    1615            1 :           gfc_error ("Pointer assignment target at %L has a vector subscript",
    1616            1 :                      &cons->expr->where);
    1617            1 :           t = false;
    1618              :         }
    1619              : 
    1620              :       /* F2003, C1272 (3).  */
    1621        11317 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        11317 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        11280 :                         || gfc_is_coindexed (cons->expr));
    1624           34 :       if (impure && gfc_pure (NULL))
    1625              :         {
    1626            1 :           t = false;
    1627            1 :           gfc_error ("Invalid expression in the structure constructor for "
    1628              :                      "pointer component %qs at %L in PURE procedure",
    1629            1 :                      comp->name, &cons->expr->where);
    1630              :         }
    1631              : 
    1632        11317 :       if (impure)
    1633           34 :         gfc_unset_implicit_pure (NULL);
    1634              :     }
    1635              : 
    1636              :   return t;
    1637              : }
    1638              : 
    1639              : 
    1640              : /****************** Expression name resolution ******************/
    1641              : 
    1642              : /* Returns 0 if a symbol was not declared with a type or
    1643              :    attribute declaration statement, nonzero otherwise.  */
    1644              : 
    1645              : static bool
    1646       768307 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       768307 :   symbol_attribute a;
    1649              : 
    1650       768307 :   a = sym->attr;
    1651              : 
    1652       768307 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       650908 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       642022 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       642020 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       641966 :       || a.asynchronous || a.codimension
    1659       641966 :       || (a.subroutine && a.proc != PROC_UNKNOWN) || a.result)
    1660        68825 :     return 1;
    1661              : 
    1662              :   return 0;
    1663              : }
    1664              : 
    1665              : 
    1666              : /* Determine if a symbol is generic or not.  */
    1667              : 
    1668              : static int
    1669       427472 : generic_sym (gfc_symbol *sym)
    1670              : {
    1671       427472 :   gfc_symbol *s;
    1672              : 
    1673       427472 :   if (sym->attr.generic ||
    1674       397004 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1675              :     return 1;
    1676              : 
    1677       395882 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1678              :     return 0;
    1679              : 
    1680        81328 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1681              : 
    1682        81328 :   if (s != NULL)
    1683              :     {
    1684          163 :       if (s == sym)
    1685              :         return 0;
    1686              :       else
    1687          162 :         return generic_sym (s);
    1688              :     }
    1689              : 
    1690              :   return 0;
    1691              : }
    1692              : 
    1693              : 
    1694              : /* Determine if a symbol is specific or not.  */
    1695              : 
    1696              : static int
    1697       395794 : specific_sym (gfc_symbol *sym)
    1698              : {
    1699       395794 :   gfc_symbol *s;
    1700              : 
    1701       395794 :   if (sym->attr.if_source == IFSRC_IFBODY
    1702       384198 :       || sym->attr.proc == PROC_MODULE
    1703       353869 :       || sym->attr.proc == PROC_INTERNAL
    1704       304338 :       || sym->attr.proc == PROC_ST_FUNCTION
    1705       304048 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1706       699111 :       || sym->attr.external)
    1707              :     return 1;
    1708              : 
    1709       300904 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1710              :     return 0;
    1711              : 
    1712        81226 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1713              : 
    1714        81226 :   return (s == NULL) ? 0 : specific_sym (s);
    1715              : }
    1716              : 
    1717              : 
    1718              : /* Figure out if the procedure is specific, generic or unknown.  */
    1719              : 
    1720              : enum proc_type
    1721              : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
    1722              : 
    1723              : static proc_type
    1724       427155 : procedure_kind (gfc_symbol *sym)
    1725              : {
    1726       427155 :   if (generic_sym (sym))
    1727              :     return PTYPE_GENERIC;
    1728              : 
    1729       395717 :   if (specific_sym (sym))
    1730        94890 :     return PTYPE_SPECIFIC;
    1731              : 
    1732              :   return PTYPE_UNKNOWN;
    1733              : }
    1734              : 
    1735              : /* Check references to assumed size arrays.  The flag need_full_assumed_size
    1736              :    is nonzero when matching actual arguments.  */
    1737              : 
    1738              : static int need_full_assumed_size = 0;
    1739              : 
    1740              : static bool
    1741      1471718 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1742              : {
    1743      1471718 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1744              :       return false;
    1745              : 
    1746              :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1747              :      What should it be?  */
    1748         3832 :   if (e->ref
    1749         3830 :       && e->ref->u.ar.as
    1750         3829 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1751         3316 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1752         3316 :       && (e->ref->u.ar.type == AR_FULL))
    1753              :     {
    1754           25 :       gfc_error ("The upper bound in the last dimension must "
    1755              :                  "appear in the reference to the assumed size "
    1756              :                  "array %qs at %L", sym->name, &e->where);
    1757           25 :       return true;
    1758              :     }
    1759              :   return false;
    1760              : }
    1761              : 
    1762              : 
    1763              : /* Look for bad assumed size array references in argument expressions
    1764              :   of elemental and array valued intrinsic procedures.  Since this is
    1765              :   called from procedure resolution functions, it only recurses at
    1766              :   operators.  */
    1767              : 
    1768              : static bool
    1769       236810 : resolve_assumed_size_actual (gfc_expr *e)
    1770              : {
    1771       236810 :   if (e == NULL)
    1772              :    return false;
    1773              : 
    1774       236235 :   switch (e->expr_type)
    1775              :     {
    1776       113781 :     case EXPR_VARIABLE:
    1777       113781 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1778              :         return true;
    1779              :       break;
    1780              : 
    1781        50303 :     case EXPR_OP:
    1782        50303 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1783        50303 :           || resolve_assumed_size_actual (e->value.op.op2))
    1784            0 :         return true;
    1785              :       break;
    1786              : 
    1787              :     default:
    1788              :       break;
    1789              :     }
    1790              :   return false;
    1791              : }
    1792              : 
    1793              : 
    1794              : /* Check a generic procedure, passed as an actual argument, to see if
    1795              :    there is a matching specific name.  If none, it is an error, and if
    1796              :    more than one, the reference is ambiguous.  */
    1797              : static int
    1798            8 : count_specific_procs (gfc_expr *e)
    1799              : {
    1800            8 :   int n;
    1801            8 :   gfc_interface *p;
    1802            8 :   gfc_symbol *sym;
    1803              : 
    1804            8 :   n = 0;
    1805            8 :   sym = e->symtree->n.sym;
    1806              : 
    1807           22 :   for (p = sym->generic; p; p = p->next)
    1808           14 :     if (strcmp (sym->name, p->sym->name) == 0)
    1809              :       {
    1810            8 :         e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
    1811              :                                        sym->name);
    1812            8 :         n++;
    1813              :       }
    1814              : 
    1815            8 :   if (n > 1)
    1816            1 :     gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
    1817              :                &e->where);
    1818              : 
    1819            8 :   if (n == 0)
    1820            1 :     gfc_error ("GENERIC procedure %qs is not allowed as an actual "
    1821              :                "argument at %L", sym->name, &e->where);
    1822              : 
    1823            8 :   return n;
    1824              : }
    1825              : 
    1826              : 
    1827              : /* See if a call to sym could possibly be a not allowed RECURSION because of
    1828              :    a missing RECURSIVE declaration.  This means that either sym is the current
    1829              :    context itself, or sym is the parent of a contained procedure calling its
    1830              :    non-RECURSIVE containing procedure.
    1831              :    This also works if sym is an ENTRY.  */
    1832              : 
    1833              : static bool
    1834       158519 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1835              : {
    1836       158519 :   gfc_symbol* proc_sym;
    1837       158519 :   gfc_symbol* context_proc;
    1838       158519 :   gfc_namespace* real_context;
    1839              : 
    1840       158519 :   if (sym->attr.flavor == FL_PROGRAM
    1841              :       || gfc_fl_struct (sym->attr.flavor))
    1842              :     return false;
    1843              : 
    1844              :   /* If we've got an ENTRY, find real procedure.  */
    1845       158518 :   if (sym->attr.entry && sym->ns->entries)
    1846           45 :     proc_sym = sym->ns->entries->sym;
    1847              :   else
    1848              :     proc_sym = sym;
    1849              : 
    1850              :   /* If sym is RECURSIVE, all is well of course.  */
    1851       158518 :   if (proc_sym->attr.recursive || flag_recursive)
    1852              :     return false;
    1853              : 
    1854              :   /* Find the context procedure's "real" symbol if it has entries.
    1855              :      We look for a procedure symbol, so recurse on the parents if we don't
    1856              :      find one (like in case of a BLOCK construct).  */
    1857         2315 :   for (real_context = context; ; real_context = real_context->parent)
    1858              :     {
    1859              :       /* We should find something, eventually!  */
    1860       134446 :       gcc_assert (real_context);
    1861              : 
    1862       134446 :       context_proc = (real_context->entries ? real_context->entries->sym
    1863              :                                             : real_context->proc_name);
    1864              : 
    1865              :       /* In some special cases, there may not be a proc_name, like for this
    1866              :          invalid code:
    1867              :          real(bad_kind()) function foo () ...
    1868              :          when checking the call to bad_kind ().
    1869              :          In these cases, we simply return here and assume that the
    1870              :          call is ok.  */
    1871       134446 :       if (!context_proc)
    1872              :         return false;
    1873              : 
    1874       134182 :       if (context_proc->attr.flavor != FL_LABEL)
    1875              :         break;
    1876              :     }
    1877              : 
    1878              :   /* A call from sym's body to itself is recursion, of course.  */
    1879       131867 :   if (context_proc == proc_sym)
    1880              :     return true;
    1881              : 
    1882              :   /* The same is true if context is a contained procedure and sym the
    1883              :      containing one.  */
    1884       131852 :   if (context_proc->attr.contained)
    1885              :     {
    1886        22259 :       gfc_symbol* parent_proc;
    1887              : 
    1888        22259 :       gcc_assert (context->parent);
    1889        22259 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1890              :                                               : context->parent->proc_name);
    1891              : 
    1892        22259 :       if (parent_proc == proc_sym)
    1893            9 :         return true;
    1894              :     }
    1895              : 
    1896              :   return false;
    1897              : }
    1898              : 
    1899              : 
    1900              : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
    1901              :    its typespec and formal argument list.  */
    1902              : 
    1903              : bool
    1904        48788 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1905              : {
    1906        48788 :   gfc_intrinsic_sym* isym = NULL;
    1907        48788 :   const char* symstd;
    1908              : 
    1909        48788 :   if (sym->resolve_symbol_called >= 2)
    1910              :     return true;
    1911              : 
    1912        38680 :   sym->resolve_symbol_called = 2;
    1913              : 
    1914              :   /* Already resolved.  */
    1915        38680 :   if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
    1916              :     return true;
    1917              : 
    1918              :   /* We already know this one is an intrinsic, so we don't call
    1919              :      gfc_is_intrinsic for full checking but rather use gfc_find_function and
    1920              :      gfc_find_subroutine directly to check whether it is a function or
    1921              :      subroutine.  */
    1922              : 
    1923        30598 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1924              :     {
    1925        13195 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1926        13195 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1927        13195 :     }
    1928        17403 :   else if (sym->intmod_sym_id)
    1929              :     {
    1930        13138 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1931        13138 :       isym = gfc_intrinsic_function_by_id (id);
    1932              :     }
    1933         4265 :   else if (!sym->attr.subroutine)
    1934         4178 :     isym = gfc_find_function (sym->name);
    1935              : 
    1936        30511 :   if (isym && !sym->attr.subroutine)
    1937              :     {
    1938        17271 :       if (sym->ts.type != BT_UNKNOWN && warn_surprising
    1939           24 :           && !sym->attr.implicit_type)
    1940           10 :         gfc_warning (OPT_Wsurprising,
    1941              :                      "Type specified for intrinsic function %qs at %L is"
    1942              :                       " ignored", sym->name, &sym->declared_at);
    1943              : 
    1944        22598 :       if (!sym->attr.function &&
    1945         5327 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1946              :         return false;
    1947              : 
    1948        17271 :       sym->ts = isym->ts;
    1949              :     }
    1950        13327 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1951              :     {
    1952        13324 :       if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
    1953              :         {
    1954            1 :           gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
    1955              :                       " specifier", sym->name, &sym->declared_at);
    1956            1 :           return false;
    1957              :         }
    1958              : 
    1959        13364 :       if (!sym->attr.subroutine &&
    1960           41 :           !gfc_add_subroutine(&sym->attr, sym->name, loc))
    1961              :         return false;
    1962              :     }
    1963              :   else
    1964              :     {
    1965            3 :       gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
    1966              :                  &sym->declared_at);
    1967            3 :       return false;
    1968              :     }
    1969              : 
    1970        30593 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1971              : 
    1972        30593 :   sym->attr.pure = isym->pure;
    1973        30593 :   sym->attr.elemental = isym->elemental;
    1974              : 
    1975              :   /* Check it is actually available in the standard settings.  */
    1976        30593 :   if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
    1977              :     {
    1978           31 :       gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
    1979              :                  "available in the current standard settings but %s. Use "
    1980              :                  "an appropriate %<-std=*%> option or enable "
    1981              :                  "%<-fall-intrinsics%> in order to use it.",
    1982              :                  sym->name, &sym->declared_at, symstd);
    1983           31 :       return false;
    1984              :     }
    1985              : 
    1986              :   return true;
    1987              : }
    1988              : 
    1989              : 
    1990              : /* Resolve a procedure expression, like passing it to a called procedure or as
    1991              :    RHS for a procedure pointer assignment.  */
    1992              : 
    1993              : static bool
    1994      1372212 : resolve_procedure_expression (gfc_expr* expr)
    1995              : {
    1996      1372212 :   gfc_symbol* sym;
    1997              : 
    1998      1372212 :   if (expr->expr_type != EXPR_VARIABLE)
    1999              :     return true;
    2000      1372195 :   gcc_assert (expr->symtree);
    2001              : 
    2002      1372195 :   sym = expr->symtree->n.sym;
    2003              : 
    2004      1372195 :   if (sym->attr.intrinsic)
    2005         1360 :     gfc_resolve_intrinsic (sym, &expr->where);
    2006              : 
    2007      1372195 :   if (sym->attr.flavor != FL_PROCEDURE
    2008        34043 :       || (sym->attr.function && sym->result == sym))
    2009              :     return true;
    2010              : 
    2011              :    /* A non-RECURSIVE procedure that is used as procedure expression within its
    2012              :      own body is in danger of being called recursively.  */
    2013        19022 :   if (is_illegal_recursion (sym, gfc_current_ns))
    2014              :     {
    2015           10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    2016            0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    2017              :                      " possibly calling itself recursively in procedure %qs. "
    2018              :                      " Declare it RECURSIVE or use %<-frecursive%>",
    2019            0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    2020              :       else
    2021           10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    2022              :                      " itself recursively.  Declare it RECURSIVE or use"
    2023              :                      " %<-frecursive%>", sym->name, &expr->where);
    2024              :     }
    2025              : 
    2026              :   return true;
    2027              : }
    2028              : 
    2029              : 
    2030              : /* Check that name is not a derived type.  */
    2031              : 
    2032              : static bool
    2033         3435 : is_dt_name (const char *name)
    2034              : {
    2035         3435 :   gfc_symbol *dt_list, *dt_first;
    2036              : 
    2037         3435 :   dt_list = dt_first = gfc_derived_types;
    2038         5889 :   for (; dt_list; dt_list = dt_list->dt_next)
    2039              :     {
    2040         3577 :       if (strcmp(dt_list->name, name) == 0)
    2041              :         return true;
    2042         3574 :       if (dt_first == dt_list->dt_next)
    2043              :         break;
    2044              :     }
    2045              :   return false;
    2046              : }
    2047              : 
    2048              : 
    2049              : /* Resolve an actual argument list.  Most of the time, this is just
    2050              :    resolving the expressions in the list.
    2051              :    The exception is that we sometimes have to decide whether arguments
    2052              :    that look like procedure arguments are really simple variable
    2053              :    references.  */
    2054              : 
    2055              : static bool
    2056       442580 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2057              :                         bool no_formal_args)
    2058              : {
    2059       442580 :   gfc_symbol *sym = NULL;
    2060       442580 :   gfc_symtree *parent_st;
    2061       442580 :   gfc_expr *e;
    2062       442580 :   gfc_component *comp;
    2063       442580 :   int save_need_full_assumed_size;
    2064       442580 :   bool return_value = false;
    2065       442580 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2066              : 
    2067       442580 :   actual_arg = true;
    2068       442580 :   first_actual_arg = true;
    2069              : 
    2070      1131000 :   for (; arg; arg = arg->next)
    2071              :     {
    2072       688521 :       e = arg->expr;
    2073       688521 :       if (e == NULL)
    2074              :         {
    2075              :           /* Check the label is a valid branching target.  */
    2076         2617 :           if (arg->label)
    2077              :             {
    2078          236 :               if (arg->label->defined == ST_LABEL_UNKNOWN)
    2079              :                 {
    2080            0 :                   gfc_error ("Label %d referenced at %L is never defined",
    2081              :                              arg->label->value, &arg->label->where);
    2082            0 :                   goto cleanup;
    2083              :                 }
    2084              :             }
    2085         2617 :           first_actual_arg = false;
    2086         2617 :           continue;
    2087              :         }
    2088              : 
    2089       685904 :       if (e->expr_type == EXPR_VARIABLE
    2090       303350 :             && e->symtree->n.sym->attr.generic
    2091            8 :             && no_formal_args
    2092       685909 :             && count_specific_procs (e) != 1)
    2093            2 :         goto cleanup;
    2094              : 
    2095       685902 :       if (e->ts.type != BT_PROCEDURE)
    2096              :         {
    2097       610946 :           save_need_full_assumed_size = need_full_assumed_size;
    2098       610946 :           if (e->expr_type != EXPR_VARIABLE)
    2099       382554 :             need_full_assumed_size = 0;
    2100       610946 :           if (!gfc_resolve_expr (e))
    2101           60 :             goto cleanup;
    2102       610886 :           need_full_assumed_size = save_need_full_assumed_size;
    2103       610886 :           goto argument_list;
    2104              :         }
    2105              : 
    2106              :       /* See if the expression node should really be a variable reference.  */
    2107              : 
    2108        74956 :       sym = e->symtree->n.sym;
    2109              : 
    2110        74956 :       if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
    2111              :         {
    2112            3 :           gfc_error ("Derived type %qs is used as an actual "
    2113              :                      "argument at %L", sym->name, &e->where);
    2114            3 :           goto cleanup;
    2115              :         }
    2116              : 
    2117        74953 :       if (sym->attr.flavor == FL_PROCEDURE
    2118        71521 :           || sym->attr.intrinsic
    2119        71521 :           || sym->attr.external)
    2120              :         {
    2121         3432 :           int actual_ok;
    2122              : 
    2123              :           /* If a procedure is not already determined to be something else
    2124              :              check if it is intrinsic.  */
    2125         3432 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2126         1254 :             sym->attr.intrinsic = 1;
    2127              : 
    2128         3432 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2129              :             {
    2130            2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2131              :                          "actual argument", sym->name, &e->where);
    2132              :             }
    2133              : 
    2134         6864 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2135         3432 :                                                sym->attr.subroutine);
    2136         3432 :           if (sym->attr.intrinsic && actual_ok == 0)
    2137              :             {
    2138            0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2139              :                          "actual argument", sym->name, &e->where);
    2140              :             }
    2141              : 
    2142         3432 :           if (sym->attr.contained && !sym->attr.use_assoc
    2143          444 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2144              :             {
    2145          256 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2146              :                                    " used as actual argument at %L",
    2147              :                                    sym->name, &e->where))
    2148            3 :                 goto cleanup;
    2149              :             }
    2150              : 
    2151         3429 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2152              :             {
    2153            2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2154              :                          "allowed as an actual argument at %L", sym->name,
    2155              :                          &e->where);
    2156              :             }
    2157              : 
    2158              :           /* Check if a generic interface has a specific procedure
    2159              :             with the same name before emitting an error.  */
    2160         3429 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2161            0 :             goto cleanup;
    2162              : 
    2163              :           /* Just in case a specific was found for the expression.  */
    2164         3429 :           sym = e->symtree->n.sym;
    2165              : 
    2166              :           /* If the symbol is the function that names the current (or
    2167              :              parent) scope, then we really have a variable reference.  */
    2168              : 
    2169         3429 :           if (gfc_is_function_return_value (sym, sym->ns))
    2170            0 :             goto got_variable;
    2171              : 
    2172              :           /* If all else fails, see if we have a specific intrinsic.  */
    2173         3429 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2174              :             {
    2175            0 :               gfc_intrinsic_sym *isym;
    2176              : 
    2177            0 :               isym = gfc_find_function (sym->name);
    2178            0 :               if (isym == NULL || !isym->specific)
    2179              :                 {
    2180            0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2181              :                              "for the reference %qs at %L", sym->name,
    2182              :                              &e->where);
    2183            0 :                   goto cleanup;
    2184              :                 }
    2185            0 :               sym->ts = isym->ts;
    2186            0 :               sym->attr.intrinsic = 1;
    2187            0 :               sym->attr.function = 1;
    2188              :             }
    2189              : 
    2190         3429 :           if (!gfc_resolve_expr (e))
    2191            0 :             goto cleanup;
    2192         3429 :           goto argument_list;
    2193              :         }
    2194              : 
    2195              :       /* See if the name is a module procedure in a parent unit.  */
    2196              : 
    2197        71521 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2198        71427 :         goto got_variable;
    2199              : 
    2200           94 :       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
    2201              :         {
    2202            0 :           gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
    2203            0 :           goto cleanup;
    2204              :         }
    2205              : 
    2206           94 :       if (parent_st == NULL)
    2207           94 :         goto got_variable;
    2208              : 
    2209            0 :       sym = parent_st->n.sym;
    2210            0 :       e->symtree = parent_st;                /* Point to the right thing.  */
    2211              : 
    2212            0 :       if (sym->attr.flavor == FL_PROCEDURE
    2213            0 :           || sym->attr.intrinsic
    2214            0 :           || sym->attr.external)
    2215              :         {
    2216            0 :           if (!gfc_resolve_expr (e))
    2217            0 :             goto cleanup;
    2218            0 :           goto argument_list;
    2219              :         }
    2220              : 
    2221            0 :     got_variable:
    2222        71521 :       e->expr_type = EXPR_VARIABLE;
    2223        71521 :       e->ts = sym->ts;
    2224        71521 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2225        36934 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2226         4021 :               && CLASS_DATA (sym)->as))
    2227              :         {
    2228        40393 :           gfc_array_spec *as
    2229        37490 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2230        37490 :           e->rank = as->rank;
    2231        37490 :           e->corank = as->corank;
    2232        37490 :           e->ref = gfc_get_ref ();
    2233        37490 :           e->ref->type = REF_ARRAY;
    2234        37490 :           e->ref->u.ar.type = AR_FULL;
    2235        37490 :           e->ref->u.ar.as = as;
    2236              :         }
    2237              : 
    2238              :       /* These symbols are set untyped by calls to gfc_set_default_type
    2239              :          with 'error_flag' = false.  Reset the untyped attribute so that
    2240              :          the error will be generated in gfc_resolve_expr.  */
    2241        71521 :       if (e->expr_type == EXPR_VARIABLE
    2242        71521 :           && sym->ts.type == BT_UNKNOWN
    2243           36 :           && sym->attr.untyped)
    2244            5 :         sym->attr.untyped = 0;
    2245              : 
    2246              :       /* Expressions are assigned a default ts.type of BT_PROCEDURE in
    2247              :          primary.cc (match_actual_arg). If above code determines that it
    2248              :          is a  variable instead, it needs to be resolved as it was not
    2249              :          done at the beginning of this function.  */
    2250        71521 :       save_need_full_assumed_size = need_full_assumed_size;
    2251        71521 :       if (e->expr_type != EXPR_VARIABLE)
    2252            0 :         need_full_assumed_size = 0;
    2253        71521 :       if (!gfc_resolve_expr (e))
    2254           22 :         goto cleanup;
    2255        71499 :       need_full_assumed_size = save_need_full_assumed_size;
    2256              : 
    2257       685814 :     argument_list:
    2258              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2259              :          nothing to do for %REF.  */
    2260       685814 :       if (arg->name && arg->name[0] == '%')
    2261              :         {
    2262           42 :           if (strcmp ("%VAL", arg->name) == 0)
    2263              :             {
    2264           28 :               if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
    2265              :                 {
    2266            2 :                   gfc_error ("By-value argument at %L is not of numeric "
    2267              :                              "type", &e->where);
    2268            2 :                   goto cleanup;
    2269              :                 }
    2270              : 
    2271           26 :               if (e->rank)
    2272              :                 {
    2273            1 :                   gfc_error ("By-value argument at %L cannot be an array or "
    2274              :                              "an array section", &e->where);
    2275            1 :                   goto cleanup;
    2276              :                 }
    2277              : 
    2278              :               /* Intrinsics are still PROC_UNKNOWN here.  However,
    2279              :                  since same file external procedures are not resolvable
    2280              :                  in gfortran, it is a good deal easier to leave them to
    2281              :                  intrinsic.cc.  */
    2282           25 :               if (ptype != PROC_UNKNOWN
    2283           25 :                   && ptype != PROC_DUMMY
    2284            9 :                   && ptype != PROC_EXTERNAL
    2285            9 :                   && ptype != PROC_MODULE)
    2286              :                 {
    2287            3 :                   gfc_error ("By-value argument at %L is not allowed "
    2288              :                              "in this context", &e->where);
    2289            3 :                   goto cleanup;
    2290              :                 }
    2291              :             }
    2292              : 
    2293              :           /* Statement functions have already been excluded above.  */
    2294           14 :           else if (strcmp ("%LOC", arg->name) == 0
    2295            8 :                    && e->ts.type == BT_PROCEDURE)
    2296              :             {
    2297            0 :               if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
    2298              :                 {
    2299            0 :                   gfc_error ("Passing internal procedure at %L by location "
    2300              :                              "not allowed", &e->where);
    2301            0 :                   goto cleanup;
    2302              :                 }
    2303              :             }
    2304              :         }
    2305              : 
    2306       685808 :       comp = gfc_get_proc_ptr_comp(e);
    2307       685808 :       if (e->expr_type == EXPR_VARIABLE
    2308       301963 :           && comp && comp->attr.elemental)
    2309              :         {
    2310            1 :             gfc_error ("ELEMENTAL procedure pointer component %qs is not "
    2311              :                        "allowed as an actual argument at %L", comp->name,
    2312              :                        &e->where);
    2313              :         }
    2314              : 
    2315              :       /* Fortran 2008, C1237.  */
    2316       301963 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2317       686261 :           && gfc_has_ultimate_pointer (e))
    2318              :         {
    2319            3 :           gfc_error ("Coindexed actual argument at %L with ultimate pointer "
    2320              :                      "component", &e->where);
    2321            3 :           goto cleanup;
    2322              :         }
    2323              : 
    2324       685805 :       if (e->expr_type == EXPR_VARIABLE
    2325       301960 :           && e->ts.type == BT_PROCEDURE
    2326         3429 :           && no_formal_args
    2327         1505 :           && sym->attr.flavor == FL_PROCEDURE
    2328         1505 :           && sym->attr.if_source == IFSRC_UNKNOWN
    2329          142 :           && !sym->attr.external
    2330            2 :           && !sym->attr.intrinsic
    2331            2 :           && !sym->attr.artificial
    2332            2 :           && !sym->ts.interface)
    2333              :         {
    2334              :           /* Emit a warning for -std=legacy and an error otherwise. */
    2335            2 :           if (gfc_option.warn_std == 0)
    2336            0 :             gfc_warning (0, "Procedure %qs at %L used as actual argument but "
    2337              :                          "does neither have an explicit interface nor the "
    2338              :                          "EXTERNAL attribute", sym->name, &e->where);
    2339              :           else
    2340              :             {
    2341            2 :               gfc_error ("Procedure %qs at %L used as actual argument but "
    2342              :                          "does neither have an explicit interface nor the "
    2343              :                          "EXTERNAL attribute", sym->name, &e->where);
    2344            2 :               goto cleanup;
    2345              :             }
    2346              :         }
    2347              : 
    2348       685803 :       first_actual_arg = false;
    2349              :     }
    2350              : 
    2351              :   return_value = true;
    2352              : 
    2353       442580 : cleanup:
    2354       442580 :   actual_arg = actual_arg_sav;
    2355       442580 :   first_actual_arg = first_actual_arg_sav;
    2356              : 
    2357       442580 :   return return_value;
    2358              : }
    2359              : 
    2360              : 
    2361              : /* Do the checks of the actual argument list that are specific to elemental
    2362              :    procedures.  If called with c == NULL, we have a function, otherwise if
    2363              :    expr == NULL, we have a subroutine.  */
    2364              : 
    2365              : static bool
    2366       337383 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2367              : {
    2368       337383 :   gfc_actual_arglist *arg0;
    2369       337383 :   gfc_actual_arglist *arg;
    2370       337383 :   gfc_symbol *esym = NULL;
    2371       337383 :   gfc_intrinsic_sym *isym = NULL;
    2372       337383 :   gfc_expr *e = NULL;
    2373       337383 :   gfc_intrinsic_arg *iformal = NULL;
    2374       337383 :   gfc_formal_arglist *eformal = NULL;
    2375       337383 :   bool formal_optional = false;
    2376       337383 :   bool set_by_optional = false;
    2377       337383 :   int i;
    2378       337383 :   int rank = 0;
    2379              : 
    2380              :   /* Is this an elemental procedure?  */
    2381       337383 :   if (expr && expr->value.function.actual != NULL)
    2382              :     {
    2383       244183 :       if (expr->value.function.esym != NULL
    2384        45001 :           && expr->value.function.esym->attr.elemental)
    2385              :         {
    2386              :           arg0 = expr->value.function.actual;
    2387              :           esym = expr->value.function.esym;
    2388              :         }
    2389       227862 :       else if (expr->value.function.isym != NULL
    2390       198113 :                && expr->value.function.isym->elemental)
    2391              :         {
    2392              :           arg0 = expr->value.function.actual;
    2393              :           isym = expr->value.function.isym;
    2394              :         }
    2395              :       else
    2396              :         return true;
    2397              :     }
    2398        93200 :   else if (c && c->ext.actual != NULL)
    2399              :     {
    2400        73530 :       arg0 = c->ext.actual;
    2401              : 
    2402        73530 :       if (c->resolved_sym)
    2403              :         esym = c->resolved_sym;
    2404              :       else
    2405          336 :         esym = c->symtree->n.sym;
    2406        73530 :       gcc_assert (esym);
    2407              : 
    2408        73530 :       if (!esym->attr.elemental)
    2409              :         return true;
    2410              :     }
    2411              :   else
    2412              :     return true;
    2413              : 
    2414              :   /* The rank of an elemental is the rank of its array argument(s).  */
    2415       179750 :   for (arg = arg0; arg; arg = arg->next)
    2416              :     {
    2417       116047 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2418              :         {
    2419        10831 :           rank = arg->expr->rank;
    2420        10831 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2421         5539 :               && arg->expr->symtree->n.sym->attr.optional)
    2422        10831 :             set_by_optional = true;
    2423              : 
    2424              :           /* Function specific; set the result rank and shape.  */
    2425        10831 :           if (expr)
    2426              :             {
    2427         8417 :               expr->rank = rank;
    2428         8417 :               expr->corank = arg->expr->corank;
    2429         8417 :               if (!expr->shape && arg->expr->shape)
    2430              :                 {
    2431         3999 :                   expr->shape = gfc_get_shape (rank);
    2432         8793 :                   for (i = 0; i < rank; i++)
    2433         4794 :                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
    2434              :                 }
    2435              :             }
    2436              :           break;
    2437              :         }
    2438              :     }
    2439              : 
    2440              :   /* If it is an array, it shall not be supplied as an actual argument
    2441              :      to an elemental procedure unless an array of the same rank is supplied
    2442              :      as an actual argument corresponding to a nonoptional dummy argument of
    2443              :      that elemental procedure(12.4.1.5).  */
    2444        74534 :   formal_optional = false;
    2445        74534 :   if (isym)
    2446        51994 :     iformal = isym->formal;
    2447              :   else
    2448        22540 :     eformal = esym->formal;
    2449              : 
    2450       196116 :   for (arg = arg0; arg; arg = arg->next)
    2451              :     {
    2452       121582 :       if (eformal)
    2453              :         {
    2454        40437 :           if (eformal->sym && eformal->sym->attr.optional)
    2455        40437 :             formal_optional = true;
    2456        40437 :           eformal = eformal->next;
    2457              :         }
    2458        81145 :       else if (isym && iformal)
    2459              :         {
    2460        70058 :           if (iformal->optional)
    2461        13796 :             formal_optional = true;
    2462        70058 :           iformal = iformal->next;
    2463              :         }
    2464        11087 :       else if (isym)
    2465        11079 :         formal_optional = true;
    2466              : 
    2467       121582 :       if (pedantic && arg->expr != NULL
    2468        69596 :           && arg->expr->expr_type == EXPR_VARIABLE
    2469        32931 :           && arg->expr->symtree->n.sym->attr.optional
    2470          578 :           && formal_optional
    2471          485 :           && arg->expr->rank
    2472          159 :           && (set_by_optional || arg->expr->rank != rank)
    2473           42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2474              :         {
    2475          114 :           bool t = false;
    2476              :           gfc_actual_arglist *a;
    2477              : 
    2478              :           /* Scan the argument list for a non-optional argument with the
    2479              :              same rank as arg.  */
    2480          114 :           for (a = arg0; a; a = a->next)
    2481           87 :             if (a != arg
    2482           45 :                 && a->expr->rank == arg->expr->rank
    2483           39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2484           37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2485           37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2486              :               {
    2487              :                 t = true;
    2488              :                 break;
    2489              :               }
    2490              : 
    2491           42 :           if (!t)
    2492           27 :             gfc_warning (OPT_Wpedantic,
    2493              :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2494              :                          "present, then it cannot be the actual argument of "
    2495              :                          "an ELEMENTAL procedure unless there is a non-optional"
    2496              :                          " argument with the same rank "
    2497              :                          "(Fortran 2018, 15.5.2.12)",
    2498              :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2499              :         }
    2500              :     }
    2501              : 
    2502       196105 :   for (arg = arg0; arg; arg = arg->next)
    2503              :     {
    2504       121580 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2505       107842 :         continue;
    2506              : 
    2507              :       /* Being elemental, the last upper bound of an assumed size array
    2508              :          argument must be present.  */
    2509        13738 :       if (resolve_assumed_size_actual (arg->expr))
    2510              :         return false;
    2511              : 
    2512              :       /* Elemental procedure's array actual arguments must conform.  */
    2513        13735 :       if (e != NULL)
    2514              :         {
    2515         2907 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2516              :             return false;
    2517              :         }
    2518              :       else
    2519        10828 :         e = arg->expr;
    2520              :     }
    2521              : 
    2522              :   /* INTENT(OUT) is only allowed for subroutines; if any actual argument
    2523              :      is an array, the intent inout/out variable needs to be also an array.  */
    2524        74525 :   if (rank > 0 && esym && expr == NULL)
    2525         7345 :     for (eformal = esym->formal, arg = arg0; arg && eformal;
    2526         4937 :          arg = arg->next, eformal = eformal->next)
    2527         4939 :       if (eformal->sym
    2528         4938 :           && (eformal->sym->attr.intent == INTENT_OUT
    2529         3856 :               || eformal->sym->attr.intent == INTENT_INOUT)
    2530         1716 :           && arg->expr && arg->expr->rank == 0)
    2531              :         {
    2532            2 :           gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
    2533              :                      "ELEMENTAL subroutine %qs is a scalar, but another "
    2534              :                      "actual argument is an array", &arg->expr->where,
    2535              :                      (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
    2536              :                      : "INOUT", eformal->sym->name, esym->name);
    2537            2 :           return false;
    2538              :         }
    2539              :   return true;
    2540              : }
    2541              : 
    2542              : 
    2543              : /* This function does the checking of references to global procedures
    2544              :    as defined in sections 18.1 and 14.1, respectively, of the Fortran
    2545              :    77 and 95 standards.  It checks for a gsymbol for the name, making
    2546              :    one if it does not already exist.  If it already exists, then the
    2547              :    reference being resolved must correspond to the type of gsymbol.
    2548              :    Otherwise, the new symbol is equipped with the attributes of the
    2549              :    reference.  The corresponding code that is called in creating
    2550              :    global entities is parse.cc.
    2551              : 
    2552              :    In addition, for all but -std=legacy, the gsymbols are used to
    2553              :    check the interfaces of external procedures from the same file.
    2554              :    The namespace of the gsymbol is resolved and then, once this is
    2555              :    done the interface is checked.  */
    2556              : 
    2557              : 
    2558              : static bool
    2559        15016 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2560              : {
    2561        15016 :   if (!gsym_ns->proc_name->attr.recursive)
    2562              :     return true;
    2563              : 
    2564          151 :   if (sym->ns == gsym_ns)
    2565              :     return false;
    2566              : 
    2567          151 :   if (sym->ns->parent && sym->ns->parent == gsym_ns)
    2568            0 :     return false;
    2569              : 
    2570              :   return true;
    2571              : }
    2572              : 
    2573              : static bool
    2574        15016 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2575              : {
    2576        15016 :   if (gsym_ns->entries)
    2577              :     {
    2578              :       gfc_entry_list *entry = gsym_ns->entries;
    2579              : 
    2580         3312 :       for (; entry; entry = entry->next)
    2581              :         {
    2582         2333 :           if (strcmp (sym->name, entry->sym->name) == 0)
    2583              :             {
    2584          971 :               if (strcmp (gsym_ns->proc_name->name,
    2585          971 :                           sym->ns->proc_name->name) == 0)
    2586              :                 return false;
    2587              : 
    2588          971 :               if (sym->ns->parent
    2589            0 :                   && strcmp (gsym_ns->proc_name->name,
    2590            0 :                              sym->ns->parent->proc_name->name) == 0)
    2591              :                 return false;
    2592              :             }
    2593              :         }
    2594              :     }
    2595              :   return true;
    2596              : }
    2597              : 
    2598              : 
    2599              : /* Check for the requirement of an explicit interface. F08:12.4.2.2.  */
    2600              : 
    2601              : bool
    2602        15882 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2603              : {
    2604        15882 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2605              : 
    2606        59229 :   for ( ; arg; arg = arg->next)
    2607              :     {
    2608        27885 :       if (!arg->sym)
    2609          157 :         continue;
    2610              : 
    2611        27728 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2612              :         {
    2613            0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2614            0 :           return true;
    2615              :         }
    2616        27728 :       else if (arg->sym->attr.asynchronous)
    2617              :         {
    2618            0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2619            0 :           return true;
    2620              :         }
    2621        27728 :       else if (arg->sym->attr.optional)
    2622              :         {
    2623           75 :           strncpy (errmsg, _("optional argument"), err_len);
    2624           75 :           return true;
    2625              :         }
    2626        27653 :       else if (arg->sym->attr.pointer)
    2627              :         {
    2628           12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2629           12 :           return true;
    2630              :         }
    2631        27641 :       else if (arg->sym->attr.target)
    2632              :         {
    2633           72 :           strncpy (errmsg, _("target argument"), err_len);
    2634           72 :           return true;
    2635              :         }
    2636        27569 :       else if (arg->sym->attr.value)
    2637              :         {
    2638           12 :           strncpy (errmsg, _("value argument"), err_len);
    2639           12 :           return true;
    2640              :         }
    2641        27557 :       else if (arg->sym->attr.volatile_)
    2642              :         {
    2643            1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2644            1 :           return true;
    2645              :         }
    2646        27556 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2647              :         {
    2648           81 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2649           81 :           return true;
    2650              :         }
    2651        27475 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK)  /* TS 29113, 6.2.  */
    2652              :         {
    2653            1 :           strncpy (errmsg, _("assumed-rank argument"), err_len);
    2654            1 :           return true;
    2655              :         }
    2656        27474 :       else if (arg->sym->attr.codimension)  /* (2c)  */
    2657              :         {
    2658            1 :           strncpy (errmsg, _("coarray argument"), err_len);
    2659            1 :           return true;
    2660              :         }
    2661        27473 :       else if (false)  /* (2d) TODO: parametrized derived type  */
    2662              :         {
    2663              :           strncpy (errmsg, _("parametrized derived type argument"), err_len);
    2664              :           return true;
    2665              :         }
    2666        27473 :       else if (arg->sym->ts.type == BT_CLASS)  /* (2e)  */
    2667              :         {
    2668          164 :           strncpy (errmsg, _("polymorphic argument"), err_len);
    2669          164 :           return true;
    2670              :         }
    2671        27309 :       else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2672              :         {
    2673            0 :           strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
    2674            0 :           return true;
    2675              :         }
    2676        27309 :       else if (arg->sym->ts.type == BT_ASSUMED)
    2677              :         {
    2678              :           /* As assumed-type is unlimited polymorphic (cf. above).
    2679              :              See also TS 29113, Note 6.1.  */
    2680            1 :           strncpy (errmsg, _("assumed-type argument"), err_len);
    2681            1 :           return true;
    2682              :         }
    2683              :     }
    2684              : 
    2685        15462 :   if (sym->attr.function)
    2686              :     {
    2687         3511 :       gfc_symbol *res = sym->result ? sym->result : sym;
    2688              : 
    2689         3511 :       if (res->attr.dimension)  /* (3a)  */
    2690              :         {
    2691           93 :           strncpy (errmsg, _("array result"), err_len);
    2692           93 :           return true;
    2693              :         }
    2694         3418 :       else if (res->attr.pointer || res->attr.allocatable)  /* (3b)  */
    2695              :         {
    2696           38 :           strncpy (errmsg, _("pointer or allocatable result"), err_len);
    2697           38 :           return true;
    2698              :         }
    2699         3380 :       else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
    2700          347 :                && res->ts.u.cl->length
    2701          166 :                && res->ts.u.cl->length->expr_type != EXPR_CONSTANT)  /* (3c)  */
    2702              :         {
    2703           12 :           strncpy (errmsg, _("result with non-constant character length"), err_len);
    2704           12 :           return true;
    2705              :         }
    2706              :     }
    2707              : 
    2708        15319 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2709              :     {
    2710            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2711            7 :       return true;
    2712              :     }
    2713        15312 :   else if (sym->attr.is_bind_c)  /* (5)  */
    2714              :     {
    2715            0 :       strncpy (errmsg, _("bind(c) procedure"), err_len);
    2716            0 :       return true;
    2717              :     }
    2718              : 
    2719              :   return false;
    2720              : }
    2721              : 
    2722              : 
    2723              : static void
    2724        30114 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2725              : {
    2726        30114 :   gfc_gsymbol * gsym;
    2727        30114 :   gfc_namespace *ns;
    2728        30114 :   enum gfc_symbol_type type;
    2729        30114 :   char reason[200];
    2730              : 
    2731        30114 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2732              : 
    2733        30114 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2734        30114 :                           sym->binding_label != NULL);
    2735              : 
    2736        30114 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2737            9 :     gfc_global_used (gsym, where);
    2738              : 
    2739        30114 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2740         9860 :        || sym->attr.if_source == IFSRC_IFBODY)
    2741        25229 :       && gsym->type != GSYM_UNKNOWN
    2742        23035 :       && !gsym->binding_label
    2743        20682 :       && gsym->ns
    2744        15016 :       && gsym->ns->proc_name
    2745        15016 :       && not_in_recursive (sym, gsym->ns)
    2746        45130 :       && not_entry_self_reference (sym, gsym->ns))
    2747              :     {
    2748        15016 :       gfc_symbol *def_sym;
    2749        15016 :       def_sym = gsym->ns->proc_name;
    2750              : 
    2751        15016 :       if (gsym->ns->resolved != -1)
    2752              :         {
    2753              : 
    2754              :           /* Resolve the gsymbol namespace if needed.  */
    2755        14994 :           if (!gsym->ns->resolved)
    2756              :             {
    2757         2794 :               gfc_symbol *old_dt_list;
    2758              : 
    2759              :               /* Stash away derived types so that the backend_decls
    2760              :                  do not get mixed up.  */
    2761         2794 :               old_dt_list = gfc_derived_types;
    2762         2794 :               gfc_derived_types = NULL;
    2763              : 
    2764         2794 :               gfc_resolve (gsym->ns);
    2765              : 
    2766              :               /* Store the new derived types with the global namespace.  */
    2767         2794 :               if (gfc_derived_types)
    2768          306 :                 gsym->ns->derived_types = gfc_derived_types;
    2769              : 
    2770              :               /* Restore the derived types of this namespace.  */
    2771         2794 :               gfc_derived_types = old_dt_list;
    2772              :             }
    2773              : 
    2774              :           /* Make sure that translation for the gsymbol occurs before
    2775              :              the procedure currently being resolved.  */
    2776        14994 :           ns = gfc_global_ns_list;
    2777        25467 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2778              :             {
    2779        17052 :               if (ns->sibling == gsym->ns)
    2780              :                 {
    2781         6579 :                   ns->sibling = gsym->ns->sibling;
    2782         6579 :                   gsym->ns->sibling = gfc_global_ns_list;
    2783         6579 :                   gfc_global_ns_list = gsym->ns;
    2784         6579 :                   break;
    2785              :                 }
    2786              :             }
    2787              : 
    2788              :           /* This can happen if a binding name has been specified.  */
    2789        14994 :           if (gsym->binding_label && gsym->sym_name != def_sym->name)
    2790            0 :             gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
    2791              :         }
    2792              : 
    2793              :       /* Look up the specific entry symbol so that interface checks use
    2794              :          the entry's own formal argument list, not the entry master's.
    2795              :          This must run even when resolved == -1 (recursive resolution in
    2796              :          progress), because def_sym starts as the namespace proc_name
    2797              :          which is the entry master with the combined formals.  */
    2798        15016 :       if (def_sym->attr.entry_master || def_sym->attr.entry)
    2799              :         {
    2800          979 :           gfc_entry_list *entry;
    2801         1699 :           for (entry = gsym->ns->entries; entry; entry = entry->next)
    2802         1699 :             if (strcmp (entry->sym->name, sym->name) == 0)
    2803              :               {
    2804          979 :                 def_sym = entry->sym;
    2805          979 :                 break;
    2806              :               }
    2807              :         }
    2808              : 
    2809        15016 :       if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
    2810              :         {
    2811            6 :           gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
    2812              :                      sym->name, &sym->declared_at, gfc_typename (&sym->ts),
    2813            6 :                      gfc_typename (&def_sym->ts));
    2814           28 :           goto done;
    2815              :         }
    2816              : 
    2817        15010 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2818        15010 :           && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
    2819              :         {
    2820            8 :           gfc_error ("Explicit interface required for %qs at %L: %s",
    2821              :                      sym->name, &sym->declared_at, reason);
    2822            8 :           goto done;
    2823              :         }
    2824              : 
    2825        15002 :       bool bad_result_characteristics;
    2826        15002 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2827              :                                    reason, sizeof(reason), NULL, NULL,
    2828              :                                    &bad_result_characteristics))
    2829              :         {
    2830              :           /* Turn errors into warnings with -std=gnu and -std=legacy,
    2831              :              unless a function returns a wrong type, which can lead
    2832              :              to all kinds of ICEs and wrong code.  */
    2833              : 
    2834           14 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2835            2 :               && !bad_result_characteristics)
    2836            2 :             gfc_errors_to_warnings (true);
    2837              : 
    2838           14 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2839              :                      sym->name, &sym->declared_at, reason);
    2840           14 :           sym->error = 1;
    2841           14 :           gfc_errors_to_warnings (false);
    2842           14 :           goto done;
    2843              :         }
    2844              :     }
    2845              : 
    2846        30114 : done:
    2847              : 
    2848        30114 :   if (gsym->type == GSYM_UNKNOWN)
    2849              :     {
    2850         4261 :       gsym->type = type;
    2851         4261 :       gsym->where = *where;
    2852              :     }
    2853              : 
    2854        30114 :   gsym->used = 1;
    2855        30114 : }
    2856              : 
    2857              : 
    2858              : /************* Function resolution *************/
    2859              : 
    2860              : /* Resolve a function call known to be generic.
    2861              :    Section 14.1.2.4.1.  */
    2862              : 
    2863              : static match
    2864        28698 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2865              : {
    2866        28698 :   gfc_symbol *s;
    2867              : 
    2868        28698 :   if (sym->attr.generic)
    2869              :     {
    2870        27534 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2871        27534 :       if (s != NULL)
    2872              :         {
    2873        20207 :           expr->value.function.name = s->name;
    2874        20207 :           expr->value.function.esym = s;
    2875              : 
    2876        20207 :           if (s->ts.type != BT_UNKNOWN)
    2877        20190 :             expr->ts = s->ts;
    2878           17 :           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
    2879           15 :             expr->ts = s->result->ts;
    2880              : 
    2881        20207 :           if (s->as != NULL)
    2882              :             {
    2883           55 :               expr->rank = s->as->rank;
    2884           55 :               expr->corank = s->as->corank;
    2885              :             }
    2886        20152 :           else if (s->result != NULL && s->result->as != NULL)
    2887              :             {
    2888            0 :               expr->rank = s->result->as->rank;
    2889            0 :               expr->corank = s->result->as->corank;
    2890              :             }
    2891              : 
    2892        20207 :           gfc_set_sym_referenced (expr->value.function.esym);
    2893              : 
    2894        20207 :           return MATCH_YES;
    2895              :         }
    2896              : 
    2897              :       /* TODO: Need to search for elemental references in generic
    2898              :          interface.  */
    2899              :     }
    2900              : 
    2901         8491 :   if (sym->attr.intrinsic)
    2902         1121 :     return gfc_intrinsic_func_interface (expr, 0);
    2903              : 
    2904              :   return MATCH_NO;
    2905              : }
    2906              : 
    2907              : 
    2908              : static bool
    2909        28548 : resolve_generic_f (gfc_expr *expr)
    2910              : {
    2911        28548 :   gfc_symbol *sym;
    2912        28548 :   match m;
    2913        28548 :   gfc_interface *intr = NULL;
    2914              : 
    2915        28548 :   sym = expr->symtree->n.sym;
    2916              : 
    2917        28698 :   for (;;)
    2918              :     {
    2919        28698 :       m = resolve_generic_f0 (expr, sym);
    2920        28698 :       if (m == MATCH_YES)
    2921              :         return true;
    2922         7372 :       else if (m == MATCH_ERROR)
    2923              :         return false;
    2924              : 
    2925         7372 : generic:
    2926         7375 :       if (!intr)
    2927         7337 :         for (intr = sym->generic; intr; intr = intr->next)
    2928         7253 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2929              :             break;
    2930              : 
    2931         7375 :       if (sym->ns->parent == NULL)
    2932              :         break;
    2933          322 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2934              : 
    2935          322 :       if (sym == NULL)
    2936              :         break;
    2937          153 :       if (!generic_sym (sym))
    2938            3 :         goto generic;
    2939              :     }
    2940              : 
    2941              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2942              :      that possesses a matching interface.  14.1.2.4  */
    2943         7222 :   if (sym  && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
    2944              :     {
    2945            5 :       if (gfc_init_expr_flag)
    2946            1 :         gfc_error ("Function %qs in initialization expression at %L "
    2947              :                    "must be an intrinsic function",
    2948            1 :                    expr->symtree->n.sym->name, &expr->where);
    2949              :       else
    2950            4 :         gfc_error ("There is no specific function for the generic %qs "
    2951            4 :                    "at %L", expr->symtree->n.sym->name, &expr->where);
    2952              :       return false;
    2953              :     }
    2954              : 
    2955         7217 :   if (intr)
    2956              :     {
    2957         7182 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2958              :                                                  NULL, false))
    2959              :         return false;
    2960         7155 :       if (!gfc_use_derived (expr->ts.u.derived))
    2961              :         return false;
    2962         7155 :       return resolve_structure_cons (expr, 0);
    2963              :     }
    2964              : 
    2965           35 :   m = gfc_intrinsic_func_interface (expr, 0);
    2966           35 :   if (m == MATCH_YES)
    2967              :     return true;
    2968              : 
    2969            3 :   if (m == MATCH_NO)
    2970            3 :     gfc_error ("Generic function %qs at %L is not consistent with a "
    2971            3 :                "specific intrinsic interface", expr->symtree->n.sym->name,
    2972              :                &expr->where);
    2973              : 
    2974              :   return false;
    2975              : }
    2976              : 
    2977              : 
    2978              : /* Resolve a function call known to be specific.  */
    2979              : 
    2980              : static match
    2981        29350 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2982              : {
    2983        29350 :   match m;
    2984              : 
    2985        29350 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2986              :     {
    2987         8348 :       if (sym->attr.dummy)
    2988              :         {
    2989          283 :           sym->attr.proc = PROC_DUMMY;
    2990          283 :           goto found;
    2991              :         }
    2992              : 
    2993         8065 :       sym->attr.proc = PROC_EXTERNAL;
    2994         8065 :       goto found;
    2995              :     }
    2996              : 
    2997        21002 :   if (sym->attr.proc == PROC_MODULE
    2998        11457 :       || sym->attr.proc == PROC_ST_FUNCTION
    2999        11167 :       || sym->attr.proc == PROC_INTERNAL)
    3000        20264 :     goto found;
    3001              : 
    3002          738 :   if (sym->attr.intrinsic)
    3003              :     {
    3004          731 :       m = gfc_intrinsic_func_interface (expr, 1);
    3005          731 :       if (m == MATCH_YES)
    3006              :         return MATCH_YES;
    3007            0 :       if (m == MATCH_NO)
    3008            0 :         gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
    3009              :                    "with an intrinsic", sym->name, &expr->where);
    3010              : 
    3011              :       return MATCH_ERROR;
    3012              :     }
    3013              : 
    3014              :   return MATCH_NO;
    3015              : 
    3016        28612 : found:
    3017        28612 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3018              : 
    3019        28612 :   if (sym->result)
    3020        28612 :     expr->ts = sym->result->ts;
    3021              :   else
    3022            0 :     expr->ts = sym->ts;
    3023        28612 :   expr->value.function.name = sym->name;
    3024        28612 :   expr->value.function.esym = sym;
    3025              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3026              :      error(s).  */
    3027        28612 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3028              :     return MATCH_ERROR;
    3029        28611 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
    3030              :     {
    3031          340 :       expr->rank = CLASS_DATA (sym)->as->rank;
    3032          340 :       expr->corank = CLASS_DATA (sym)->as->corank;
    3033              :     }
    3034        28271 :   else if (sym->as != NULL)
    3035              :     {
    3036         2599 :       expr->rank = sym->as->rank;
    3037         2599 :       expr->corank = sym->as->corank;
    3038              :     }
    3039              : 
    3040              :   return MATCH_YES;
    3041              : }
    3042              : 
    3043              : 
    3044              : static bool
    3045        29343 : resolve_specific_f (gfc_expr *expr)
    3046              : {
    3047        29343 :   gfc_symbol *sym;
    3048        29343 :   match m;
    3049              : 
    3050        29343 :   sym = expr->symtree->n.sym;
    3051              : 
    3052        29350 :   for (;;)
    3053              :     {
    3054        29350 :       m = resolve_specific_f0 (sym, expr);
    3055        29350 :       if (m == MATCH_YES)
    3056              :         return true;
    3057            8 :       if (m == MATCH_ERROR)
    3058              :         return false;
    3059              : 
    3060            7 :       if (sym->ns->parent == NULL)
    3061              :         break;
    3062              : 
    3063            7 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3064              : 
    3065            7 :       if (sym == NULL)
    3066              :         break;
    3067              :     }
    3068              : 
    3069            0 :   gfc_error ("Unable to resolve the specific function %qs at %L",
    3070            0 :              expr->symtree->n.sym->name, &expr->where);
    3071              : 
    3072            0 :   return true;
    3073              : }
    3074              : 
    3075              : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
    3076              :    candidates in CANDIDATES_LEN.  */
    3077              : 
    3078              : static void
    3079          212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
    3080              :                                        char **&candidates,
    3081              :                                        size_t &candidates_len)
    3082              : {
    3083          388 :   gfc_symtree *p;
    3084              : 
    3085          388 :   if (sym == NULL)
    3086              :     return;
    3087          388 :   if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
    3088          126 :       && sym->n.sym->attr.flavor == FL_PROCEDURE)
    3089           51 :     vec_push (candidates, candidates_len, sym->name);
    3090              : 
    3091          388 :   p = sym->left;
    3092          388 :   if (p)
    3093          155 :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3094              : 
    3095          388 :   p = sym->right;
    3096          388 :   if (p)
    3097              :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3098              : }
    3099              : 
    3100              : 
    3101              : /* Lookup function FN fuzzily, taking names in SYMROOT into account.  */
    3102              : 
    3103              : const char*
    3104           57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
    3105              : {
    3106           57 :   char **candidates = NULL;
    3107           57 :   size_t candidates_len = 0;
    3108           57 :   lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
    3109           57 :   return gfc_closest_fuzzy_match (fn, candidates);
    3110              : }
    3111              : 
    3112              : 
    3113              : /* Resolve a procedure call not known to be generic nor specific.  */
    3114              : 
    3115              : static bool
    3116       284824 : resolve_unknown_f (gfc_expr *expr)
    3117              : {
    3118       284824 :   gfc_symbol *sym;
    3119       284824 :   gfc_typespec *ts;
    3120              : 
    3121       284824 :   sym = expr->symtree->n.sym;
    3122              : 
    3123       284824 :   if (sym->attr.dummy)
    3124              :     {
    3125          293 :       sym->attr.proc = PROC_DUMMY;
    3126          293 :       expr->value.function.name = sym->name;
    3127          293 :       goto set_type;
    3128              :     }
    3129              : 
    3130              :   /* See if we have an intrinsic function reference.  */
    3131              : 
    3132       284531 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3133              :     {
    3134       282269 :       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
    3135              :         return true;
    3136          821 :       return false;
    3137              :     }
    3138              : 
    3139              :   /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
    3140              :   /* Intrinsics were handled above, only non-intrinsics left here.  */
    3141         2262 :   if (sym->attr.flavor == FL_PROCEDURE
    3142         2259 :       && sym->attr.implicit_type
    3143          376 :       && sym->ns
    3144          376 :       && sym->ns->has_implicit_none_export)
    3145              :     {
    3146            3 :           gfc_error ("Missing explicit declaration with EXTERNAL attribute "
    3147              :               "for symbol %qs at %L", sym->name, &sym->declared_at);
    3148            3 :           sym->error = 1;
    3149            3 :           return false;
    3150              :     }
    3151              : 
    3152              :   /* The reference is to an external name.  */
    3153              : 
    3154         2259 :   sym->attr.proc = PROC_EXTERNAL;
    3155         2259 :   expr->value.function.name = sym->name;
    3156         2259 :   expr->value.function.esym = expr->symtree->n.sym;
    3157              : 
    3158         2259 :   if (sym->as != NULL)
    3159              :     {
    3160            1 :       expr->rank = sym->as->rank;
    3161            1 :       expr->corank = sym->as->corank;
    3162              :     }
    3163              : 
    3164              :   /* Type of the expression is either the type of the symbol or the
    3165              :      default type of the symbol.  */
    3166              : 
    3167         2258 : set_type:
    3168         2552 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3169              : 
    3170         2552 :   if (sym->ts.type != BT_UNKNOWN)
    3171         2501 :     expr->ts = sym->ts;
    3172              :   else
    3173              :     {
    3174           51 :       ts = gfc_get_default_type (sym->name, sym->ns);
    3175              : 
    3176           51 :       if (ts->type == BT_UNKNOWN)
    3177              :         {
    3178           41 :           const char *guessed
    3179           41 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    3180           41 :           if (guessed)
    3181            3 :             gfc_error ("Function %qs at %L has no IMPLICIT type"
    3182              :                        "; did you mean %qs?",
    3183              :                        sym->name, &expr->where, guessed);
    3184              :           else
    3185           38 :             gfc_error ("Function %qs at %L has no IMPLICIT type",
    3186              :                        sym->name, &expr->where);
    3187              :           return false;
    3188              :         }
    3189              :       else
    3190           10 :         expr->ts = *ts;
    3191              :     }
    3192              : 
    3193              :   return true;
    3194              : }
    3195              : 
    3196              : 
    3197              : /* Return true, if the symbol is an external procedure.  */
    3198              : static bool
    3199       882379 : is_external_proc (gfc_symbol *sym)
    3200              : {
    3201       880654 :   if (!sym->attr.dummy && !sym->attr.contained
    3202       768476 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3203       168544 :         && sym->attr.proc != PROC_ST_FUNCTION
    3204       167947 :         && !sym->attr.proc_pointer
    3205       166735 :         && !sym->attr.use_assoc
    3206       942638 :         && sym->name)
    3207        60259 :     return true;
    3208              : 
    3209              :   return false;
    3210              : }
    3211              : 
    3212              : 
    3213              : /* Figure out if a function reference is pure or not.  Also set the name
    3214              :    of the function for a potential error message.  Return nonzero if the
    3215              :    function is PURE, zero if not.  */
    3216              : static bool
    3217              : pure_stmt_function (gfc_expr *, gfc_symbol *);
    3218              : 
    3219              : bool
    3220       265520 : gfc_pure_function (gfc_expr *e, const char **name)
    3221              : {
    3222       265520 :   bool pure;
    3223       265520 :   gfc_component *comp;
    3224              : 
    3225       265520 :   *name = NULL;
    3226              : 
    3227       265520 :   if (e->symtree != NULL
    3228       265129 :         && e->symtree->n.sym != NULL
    3229       265129 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3230          306 :     return pure_stmt_function (e, e->symtree->n.sym);
    3231              : 
    3232       265214 :   comp = gfc_get_proc_ptr_comp (e);
    3233       265214 :   if (comp)
    3234              :     {
    3235          498 :       pure = gfc_pure (comp->ts.interface);
    3236          498 :       *name = comp->name;
    3237              :     }
    3238       264716 :   else if (e->value.function.esym)
    3239              :     {
    3240        54494 :       pure = gfc_pure (e->value.function.esym);
    3241        54494 :       *name = e->value.function.esym->name;
    3242              :     }
    3243       210222 :   else if (e->value.function.isym)
    3244              :     {
    3245       418276 :       pure = e->value.function.isym->pure
    3246       209138 :              || e->value.function.isym->elemental;
    3247       209138 :       *name = e->value.function.isym->name;
    3248              :     }
    3249         1084 :   else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
    3250              :     {
    3251              :       /* The function has been resolved, but esym is not yet set.
    3252              :          This can happen with functions as dummy argument.  */
    3253          291 :       pure = e->symtree->n.sym->attr.pure;
    3254          291 :       *name = e->symtree->n.sym->name;
    3255              :     }
    3256              :   else
    3257              :     {
    3258              :       /* Implicit functions are not pure.  */
    3259          793 :       pure = 0;
    3260          793 :       *name = e->value.function.name;
    3261              :     }
    3262              : 
    3263              :   return pure;
    3264              : }
    3265              : 
    3266              : 
    3267              : /* Check if the expression is a reference to an implicitly pure function.  */
    3268              : 
    3269              : bool
    3270        39783 : gfc_implicit_pure_function (gfc_expr *e)
    3271              : {
    3272        39783 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3273        39783 :   if (comp)
    3274          476 :     return gfc_implicit_pure (comp->ts.interface);
    3275        39307 :   else if (e->value.function.esym)
    3276        33885 :     return gfc_implicit_pure (e->value.function.esym);
    3277              :   else
    3278              :     return 0;
    3279              : }
    3280              : 
    3281              : 
    3282              : static bool
    3283          983 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
    3284              :                  int *f ATTRIBUTE_UNUSED)
    3285              : {
    3286          983 :   const char *name;
    3287              : 
    3288              :   /* Don't bother recursing into other statement functions
    3289              :      since they will be checked individually for purity.  */
    3290          983 :   if (e->expr_type != EXPR_FUNCTION
    3291          344 :         || !e->symtree
    3292          344 :         || e->symtree->n.sym == sym
    3293           20 :         || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3294              :     return false;
    3295              : 
    3296           19 :   return !gfc_pure_function (e, &name);
    3297              : }
    3298              : 
    3299              : 
    3300              : static bool
    3301          306 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
    3302              : {
    3303          306 :   return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
    3304              : }
    3305              : 
    3306              : 
    3307              : /* Check if an impure function is allowed in the current context. */
    3308              : 
    3309       253311 : static bool check_pure_function (gfc_expr *e)
    3310              : {
    3311       253311 :   const char *name = NULL;
    3312       253311 :   code_stack *stack;
    3313       253311 :   bool saw_block = false;
    3314              : 
    3315              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3316              :      gfc_do_concurrent_flag = 0 when the check for an impure function
    3317              :      occurs.  Check the stack to see if the source code has a nested
    3318              :      BLOCK construct.  */
    3319              : 
    3320       586893 :   for (stack = cs_base; stack; stack = stack->prev)
    3321              :     {
    3322       333584 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3323              :         {
    3324         8281 :           saw_block = true;
    3325         8281 :           continue;
    3326              :         }
    3327              : 
    3328         5741 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3329              :         {
    3330           16 :           bool is_pure;
    3331       333582 :           is_pure = (e->value.function.isym
    3332           15 :                      && (e->value.function.isym->pure
    3333            1 :                          || e->value.function.isym->elemental))
    3334           17 :                     || (e->value.function.esym
    3335            1 :                         && (e->value.function.esym->attr.pure
    3336            1 :                             || e->value.function.esym->attr.elemental));
    3337            2 :           if (!is_pure)
    3338              :             {
    3339            2 :               gfc_error ("Reference to impure function at %L inside a "
    3340              :                          "DO CONCURRENT", &e->where);
    3341            2 :               return false;
    3342              :             }
    3343              :         }
    3344              :     }
    3345              : 
    3346       253309 :   if (!gfc_pure_function (e, &name) && name)
    3347              :     {
    3348        38467 :       if (forall_flag)
    3349              :         {
    3350            4 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3351              :                      "FORALL %s", name, &e->where,
    3352              :                      forall_flag == 2 ? "mask" : "block");
    3353            4 :           return false;
    3354              :         }
    3355        38463 :       else if (gfc_do_concurrent_flag)
    3356              :         {
    3357            2 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3358              :                      "DO CONCURRENT %s", name, &e->where,
    3359              :                      gfc_do_concurrent_flag == 2 ? "mask" : "block");
    3360            2 :           return false;
    3361              :         }
    3362        38461 :       else if (gfc_pure (NULL))
    3363              :         {
    3364            5 :           gfc_error ("Reference to impure function %qs at %L "
    3365              :                      "within a PURE procedure", name, &e->where);
    3366            5 :           return false;
    3367              :         }
    3368        38456 :       if (!gfc_implicit_pure_function (e))
    3369        31357 :         gfc_unset_implicit_pure (NULL);
    3370              :     }
    3371              :   return true;
    3372              : }
    3373              : 
    3374              : 
    3375              : /* Update current procedure's array_outer_dependency flag, considering
    3376              :    a call to procedure SYM.  */
    3377              : 
    3378              : static void
    3379       137353 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
    3380              : {
    3381              :   /* Check to see if this is a sibling function that has not yet
    3382              :      been resolved.  */
    3383       137353 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3384       256622 :   for (; sibling; sibling = sibling->sibling)
    3385              :     {
    3386       126787 :       if (sibling->proc_name == sym)
    3387              :         {
    3388         7518 :           gfc_resolve (sibling);
    3389         7518 :           break;
    3390              :         }
    3391              :     }
    3392              : 
    3393              :   /* If SYM has references to outer arrays, so has the procedure calling
    3394              :      SYM.  If SYM is a procedure pointer, we can assume the worst.  */
    3395       137353 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3396        69617 :       && gfc_current_ns->proc_name)
    3397        69573 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3398       137353 : }
    3399              : 
    3400              : 
    3401              : /* Resolve a function call, which means resolving the arguments, then figuring
    3402              :    out which entity the name refers to.  */
    3403              : 
    3404              : static bool
    3405       357129 : resolve_function (gfc_expr *expr)
    3406              : {
    3407       357129 :   gfc_actual_arglist *arg;
    3408       357129 :   gfc_symbol *sym;
    3409       357129 :   bool t;
    3410       357129 :   int temp;
    3411       357129 :   procedure_type p = PROC_INTRINSIC;
    3412       357129 :   bool no_formal_args;
    3413              : 
    3414       357129 :   sym = NULL;
    3415       357129 :   if (expr->symtree)
    3416       356738 :     sym = expr->symtree->n.sym;
    3417              : 
    3418              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3419       357129 :   if (gfc_is_proc_ptr_comp (expr))
    3420              :     return true;
    3421              : 
    3422              :   /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
    3423              :      another caf_get.  */
    3424       356713 :   if (sym && sym->attr.intrinsic
    3425         8821 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3426         8821 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3427              :     return true;
    3428              : 
    3429       356713 :   if (expr->ref)
    3430              :     {
    3431            1 :       gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
    3432              :                  &expr->where);
    3433            1 :       return false;
    3434              :     }
    3435              : 
    3436       356321 :   if (sym && sym->attr.intrinsic
    3437       365533 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3438              :     return false;
    3439              : 
    3440       356712 :   if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
    3441              :     {
    3442            4 :       gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
    3443            4 :       return false;
    3444              :     }
    3445              : 
    3446              :   /* If this is a deferred TBP with an abstract interface (which may
    3447              :      of course be referenced), expr->value.function.esym will be set.  */
    3448       356317 :   if (sym && sym->attr.abstract && !expr->value.function.esym)
    3449              :     {
    3450            1 :       gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3451              :                  sym->name, &expr->where);
    3452            1 :       return false;
    3453              :     }
    3454              : 
    3455              :   /* If this is a deferred TBP with an abstract interface, its result
    3456              :      cannot be an assumed length character (F2003: C418).  */
    3457       356316 :   if (sym && sym->attr.abstract && sym->attr.function
    3458          192 :       && sym->result->ts.u.cl
    3459          158 :       && sym->result->ts.u.cl->length == NULL
    3460            2 :       && !sym->result->ts.deferred)
    3461              :     {
    3462            1 :       gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
    3463              :                  "character length result (F2008: C418)", sym->name,
    3464              :                  &sym->declared_at);
    3465            1 :       return false;
    3466              :     }
    3467              : 
    3468              :   /* Switch off assumed size checking and do this again for certain kinds
    3469              :      of procedure, once the procedure itself is resolved.  */
    3470       356706 :   need_full_assumed_size++;
    3471              : 
    3472       356706 :   if (expr->symtree && expr->symtree->n.sym)
    3473       356315 :     p = expr->symtree->n.sym->attr.proc;
    3474              : 
    3475       356706 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3476         1260 :     inquiry_argument = true;
    3477       356315 :   no_formal_args = sym && is_external_proc (sym)
    3478       371103 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3479              : 
    3480       356706 :   if (!resolve_actual_arglist (expr->value.function.actual,
    3481              :                                p, no_formal_args))
    3482              :     {
    3483           67 :       inquiry_argument = false;
    3484           67 :       return false;
    3485              :     }
    3486              : 
    3487       356639 :   inquiry_argument = false;
    3488              : 
    3489              :   /* Resume assumed_size checking.  */
    3490       356639 :   need_full_assumed_size--;
    3491              : 
    3492              :   /* If the procedure is external, check for usage.  */
    3493       356639 :   if (sym && is_external_proc (sym))
    3494        14377 :     resolve_global_procedure (sym, &expr->where, 0);
    3495              : 
    3496       356639 :   if (sym && sym->ts.type == BT_CHARACTER
    3497         3480 :       && sym->ts.u.cl
    3498         3386 :       && sym->ts.u.cl->length == NULL
    3499          707 :       && !sym->attr.dummy
    3500          700 :       && !sym->ts.deferred
    3501            2 :       && expr->value.function.esym == NULL
    3502            2 :       && !sym->attr.contained)
    3503              :     {
    3504              :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3505            1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3506              :                  "be used at %L since it is not a dummy argument",
    3507              :                  sym->name, &expr->where);
    3508            1 :       return false;
    3509              :     }
    3510              : 
    3511              :   /* Add and check formal interface when -fc-prototypes-external is in
    3512              :      force, see comment in resolve_call().  */
    3513              : 
    3514       356638 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3515           18 :       && sym->attr.external)
    3516              :     {
    3517           18 :       if (sym->formal)
    3518              :         {
    3519            6 :           bool conflict;
    3520            6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3521              :                                                  sym->formal, 0, 0, 0, NULL);
    3522            6 :           if (conflict)
    3523              :             {
    3524            6 :               sym->ext_dummy_arglist_mismatch = 1;
    3525            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3526              :                            "Different argument lists in external dummy "
    3527              :                            "function %s at %L and %L", sym->name,
    3528              :                            &expr->where, &sym->other_loc);
    3529              :             }
    3530              :         }
    3531           12 :       else if (!sym->formal_resolved)
    3532              :         {
    3533            6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3534            6 :           sym->other_loc = expr->where;
    3535              :         }
    3536              :     }
    3537              :   /* See if function is already resolved.  */
    3538              : 
    3539       356638 :   if (expr->value.function.name != NULL
    3540       343573 :       || expr->value.function.isym != NULL)
    3541              :     {
    3542        13923 :       if (expr->ts.type == BT_UNKNOWN)
    3543            3 :         expr->ts = sym->ts;
    3544              :       t = true;
    3545              :     }
    3546              :   else
    3547              :     {
    3548              :       /* Apply the rules of section 14.1.2.  */
    3549              : 
    3550       342715 :       switch (procedure_kind (sym))
    3551              :         {
    3552        28548 :         case PTYPE_GENERIC:
    3553        28548 :           t = resolve_generic_f (expr);
    3554        28548 :           break;
    3555              : 
    3556        29343 :         case PTYPE_SPECIFIC:
    3557        29343 :           t = resolve_specific_f (expr);
    3558        29343 :           break;
    3559              : 
    3560       284824 :         case PTYPE_UNKNOWN:
    3561       284824 :           t = resolve_unknown_f (expr);
    3562       284824 :           break;
    3563              : 
    3564              :         default:
    3565              :           gfc_internal_error ("resolve_function(): bad function type");
    3566              :         }
    3567              :     }
    3568              : 
    3569              :   /* If the expression is still a function (it might have simplified),
    3570              :      then we check to see if we are calling an elemental function.  */
    3571              : 
    3572       356638 :   if (expr->expr_type != EXPR_FUNCTION)
    3573              :     return t;
    3574              : 
    3575              :   /* Walk the argument list looking for invalid BOZ.  */
    3576       764537 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3577       511701 :     if (arg->expr && arg->expr->ts.type == BT_BOZ)
    3578              :       {
    3579            5 :         gfc_error ("A BOZ literal constant at %L cannot appear as an "
    3580              :                    "actual argument in a function reference",
    3581              :                    &arg->expr->where);
    3582            5 :         return false;
    3583              :       }
    3584              : 
    3585       252836 :   temp = need_full_assumed_size;
    3586       252836 :   need_full_assumed_size = 0;
    3587              : 
    3588       252836 :   if (!resolve_elemental_actual (expr, NULL))
    3589              :     return false;
    3590              : 
    3591       252833 :   if (omp_workshare_flag
    3592           32 :       && expr->value.function.esym
    3593       252838 :       && ! gfc_elemental (expr->value.function.esym))
    3594              :     {
    3595            4 :       gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
    3596            4 :                  "in WORKSHARE construct", expr->value.function.esym->name,
    3597              :                  &expr->where);
    3598            4 :       t = false;
    3599              :     }
    3600              : 
    3601              : #define GENERIC_ID expr->value.function.isym->id
    3602       252829 :   else if (expr->value.function.actual != NULL
    3603       244180 :            && expr->value.function.isym != NULL
    3604       198112 :            && GENERIC_ID != GFC_ISYM_LBOUND
    3605              :            && GENERIC_ID != GFC_ISYM_LCOBOUND
    3606              :            && GENERIC_ID != GFC_ISYM_UCOBOUND
    3607              :            && GENERIC_ID != GFC_ISYM_LEN
    3608              :            && GENERIC_ID != GFC_ISYM_LOC
    3609              :            && GENERIC_ID != GFC_ISYM_C_LOC
    3610              :            && GENERIC_ID != GFC_ISYM_PRESENT)
    3611              :     {
    3612              :       /* Array intrinsics must also have the last upper bound of an
    3613              :          assumed size array argument.  UBOUND and SIZE have to be
    3614              :          excluded from the check if the second argument is anything
    3615              :          than a constant.  */
    3616              : 
    3617       557481 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3618              :         {
    3619       385780 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3620        47180 :               && arg == expr->value.function.actual
    3621        17354 :               && arg->next != NULL && arg->next->expr)
    3622              :             {
    3623         8501 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3624              :                 break;
    3625              : 
    3626         8277 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3627              :                 break;
    3628              : 
    3629         8277 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3630         8277 :                         < arg->expr->rank)
    3631              :                 break;
    3632              :             }
    3633              : 
    3634       383341 :           if (arg->expr != NULL
    3635       254558 :               && arg->expr->rank > 0
    3636       505807 :               && resolve_assumed_size_actual (arg->expr))
    3637              :             return false;
    3638              :         }
    3639              :     }
    3640              : #undef GENERIC_ID
    3641              : 
    3642       252830 :   need_full_assumed_size = temp;
    3643              : 
    3644       252830 :   if (!check_pure_function(expr))
    3645           12 :     t = false;
    3646              : 
    3647              :   /* Functions without the RECURSIVE attribution are not allowed to
    3648              :    * call themselves.  */
    3649       252830 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3650              :     {
    3651        53201 :       gfc_symbol *esym;
    3652        53201 :       esym = expr->value.function.esym;
    3653              : 
    3654        53201 :       if (is_illegal_recursion (esym, gfc_current_ns))
    3655              :       {
    3656            5 :         if (esym->attr.entry && esym->ns->entries)
    3657            3 :           gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
    3658              :                      " function %qs is not RECURSIVE",
    3659            3 :                      esym->name, &expr->where, esym->ns->entries->sym->name);
    3660              :         else
    3661            2 :           gfc_error ("Function %qs at %L cannot be called recursively, as it"
    3662              :                      " is not RECURSIVE", esym->name, &expr->where);
    3663              : 
    3664              :         t = false;
    3665              :       }
    3666              :     }
    3667              : 
    3668              :   /* Character lengths of use associated functions may contains references to
    3669              :      symbols not referenced from the current program unit otherwise.  Make sure
    3670              :      those symbols are marked as referenced.  */
    3671              : 
    3672       252830 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3673         3584 :       && expr->value.function.esym->attr.use_assoc)
    3674              :     {
    3675         1328 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3676              :     }
    3677              : 
    3678              :   /* Make sure that the expression has a typespec that works.  */
    3679       252830 :   if (expr->ts.type == BT_UNKNOWN)
    3680              :     {
    3681          932 :       if (expr->symtree->n.sym->result
    3682          923 :             && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
    3683          561 :             && !expr->symtree->n.sym->result->attr.proc_pointer)
    3684          561 :         expr->ts = expr->symtree->n.sym->result->ts;
    3685              :     }
    3686              : 
    3687              :   /* These derived types with an incomplete namespace, arising from use
    3688              :      association, cause gfc_get_derived_vtab to segfault. If the function
    3689              :      namespace does not suffice, something is badly wrong.  */
    3690       252830 :   if (expr->ts.type == BT_DERIVED
    3691         9966 :       && !expr->ts.u.derived->ns->proc_name)
    3692              :     {
    3693            3 :       gfc_symbol *der;
    3694            3 :       gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
    3695            3 :       if (der)
    3696              :         {
    3697            3 :           expr->ts.u.derived->refs--;
    3698            3 :           expr->ts.u.derived = der;
    3699            3 :           der->refs++;
    3700              :         }
    3701              :       else
    3702            0 :         expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
    3703              :     }
    3704              : 
    3705       252830 :   if (!expr->ref && !expr->value.function.isym)
    3706              :     {
    3707        54597 :       if (expr->value.function.esym)
    3708        53513 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3709              :       else
    3710         1084 :         update_current_proc_array_outer_dependency (sym);
    3711              :     }
    3712       198233 :   else if (expr->ref)
    3713              :     /* typebound procedure: Assume the worst.  */
    3714            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3715              : 
    3716       252830 :   if (expr->value.function.esym
    3717        53513 :       && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    3718           26 :     gfc_warning (OPT_Wdeprecated_declarations,
    3719              :                  "Using function %qs at %L is deprecated",
    3720              :                  sym->name, &expr->where);
    3721              : 
    3722              :   /* Check an external function supplied as a dummy argument has an external
    3723              :      attribute when a program unit uses 'implicit none (external)'.  */
    3724       252830 :   if (expr->expr_type == EXPR_FUNCTION
    3725       252830 :       && expr->symtree
    3726       252439 :       && expr->symtree->n.sym->attr.dummy
    3727          575 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3728       252831 :       && !gfc_is_intrinsic(expr->symtree->n.sym, 0, expr->where))
    3729              :     {
    3730            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    3731              :                  sym->name, &expr->where);
    3732            1 :       return false;
    3733              :     }
    3734              : 
    3735              :   return t;
    3736              : }
    3737              : 
    3738              : 
    3739              : /************* Subroutine resolution *************/
    3740              : 
    3741              : static bool
    3742        80189 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3743              : {
    3744        80189 :   code_stack *stack;
    3745        80189 :   bool saw_block = false;
    3746              : 
    3747        80189 :   if (gfc_pure (sym))
    3748              :     return true;
    3749              : 
    3750              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3751              :      gfc_do_concurrent_flag = 0 when the check for an impure subroutine
    3752              :      occurs.  Walk up the stack to see if the source code has a nested
    3753              :      construct.  */
    3754              : 
    3755       165516 :   for (stack = cs_base; stack; stack = stack->prev)
    3756              :     {
    3757        91002 :       if (stack->current->op == EXEC_BLOCK)
    3758              :         {
    3759         2052 :           saw_block = true;
    3760         2052 :           continue;
    3761              :         }
    3762              : 
    3763        88950 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3764              :         {
    3765              : 
    3766            2 :           bool is_pure = true;
    3767        91002 :           is_pure = sym->attr.pure || sym->attr.elemental;
    3768              : 
    3769            2 :           if (!is_pure)
    3770              :             {
    3771            2 :               gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
    3772              :                          "is not PURE", loc);
    3773            2 :               return false;
    3774              :             }
    3775              :         }
    3776              :     }
    3777              : 
    3778        74514 :   if (forall_flag)
    3779              :     {
    3780            0 :       gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
    3781              :                  name, loc);
    3782            0 :       return false;
    3783              :     }
    3784        74514 :   else if (gfc_do_concurrent_flag)
    3785              :     {
    3786            6 :       gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
    3787              :                  "PURE", name, loc);
    3788            6 :       return false;
    3789              :     }
    3790        74508 :   else if (gfc_pure (NULL))
    3791              :     {
    3792            4 :       gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
    3793            4 :       return false;
    3794              :     }
    3795              : 
    3796        74504 :   gfc_unset_implicit_pure (NULL);
    3797        74504 :   return true;
    3798              : }
    3799              : 
    3800              : 
    3801              : static match
    3802         2892 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3803              : {
    3804         2892 :   gfc_symbol *s;
    3805              : 
    3806         2892 :   if (sym->attr.generic)
    3807              :     {
    3808         2891 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3809         2891 :       if (s != NULL)
    3810              :         {
    3811         2882 :           c->resolved_sym = s;
    3812         2882 :           if (!pure_subroutine (s, s->name, &c->loc))
    3813              :             return MATCH_ERROR;
    3814         2882 :           return MATCH_YES;
    3815              :         }
    3816              : 
    3817              :       /* TODO: Need to search for elemental references in generic interface.  */
    3818              :     }
    3819              : 
    3820           10 :   if (sym->attr.intrinsic)
    3821            1 :     return gfc_intrinsic_sub_interface (c, 0);
    3822              : 
    3823              :   return MATCH_NO;
    3824              : }
    3825              : 
    3826              : 
    3827              : static bool
    3828         2890 : resolve_generic_s (gfc_code *c)
    3829              : {
    3830         2890 :   gfc_symbol *sym;
    3831         2890 :   match m;
    3832              : 
    3833         2890 :   sym = c->symtree->n.sym;
    3834              : 
    3835         2892 :   for (;;)
    3836              :     {
    3837         2892 :       m = resolve_generic_s0 (c, sym);
    3838         2892 :       if (m == MATCH_YES)
    3839              :         return true;
    3840            9 :       else if (m == MATCH_ERROR)
    3841              :         return false;
    3842              : 
    3843            9 : generic:
    3844            9 :       if (sym->ns->parent == NULL)
    3845              :         break;
    3846            3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3847              : 
    3848            3 :       if (sym == NULL)
    3849              :         break;
    3850            2 :       if (!generic_sym (sym))
    3851            0 :         goto generic;
    3852              :     }
    3853              : 
    3854              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3855              :      that possesses a matching interface.  14.1.2.4  */
    3856            7 :   sym = c->symtree->n.sym;
    3857              : 
    3858            7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3859              :     {
    3860            4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3861              :                  sym->name, &c->loc);
    3862            4 :       return false;
    3863              :     }
    3864              : 
    3865            3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3866            3 :   if (m == MATCH_YES)
    3867              :     return true;
    3868            1 :   if (m == MATCH_NO)
    3869            1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3870              :                "intrinsic subroutine interface", sym->name, &c->loc);
    3871              : 
    3872              :   return false;
    3873              : }
    3874              : 
    3875              : 
    3876              : /* Resolve a subroutine call known to be specific.  */
    3877              : 
    3878              : static match
    3879        65553 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3880              : {
    3881        65553 :   match m;
    3882              : 
    3883        65553 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3884              :     {
    3885         5747 :       if (sym->attr.dummy)
    3886              :         {
    3887          263 :           sym->attr.proc = PROC_DUMMY;
    3888          263 :           goto found;
    3889              :         }
    3890              : 
    3891         5484 :       sym->attr.proc = PROC_EXTERNAL;
    3892         5484 :       goto found;
    3893              :     }
    3894              : 
    3895        59806 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3896        59800 :     goto found;
    3897              : 
    3898            6 :   if (sym->attr.intrinsic)
    3899              :     {
    3900            0 :       m = gfc_intrinsic_sub_interface (c, 1);
    3901            0 :       if (m == MATCH_YES)
    3902              :         return MATCH_YES;
    3903            0 :       if (m == MATCH_NO)
    3904            0 :         gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
    3905              :                    "with an intrinsic", sym->name, &c->loc);
    3906              : 
    3907              :       return MATCH_ERROR;
    3908              :     }
    3909              : 
    3910              :   return MATCH_NO;
    3911              : 
    3912        65547 : found:
    3913        65547 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3914              : 
    3915        65547 :   c->resolved_sym = sym;
    3916        65547 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3917            7 :     return MATCH_ERROR;
    3918              : 
    3919              :   return MATCH_YES;
    3920              : }
    3921              : 
    3922              : 
    3923              : static bool
    3924        65547 : resolve_specific_s (gfc_code *c)
    3925              : {
    3926        65547 :   gfc_symbol *sym;
    3927        65547 :   match m;
    3928              : 
    3929        65547 :   sym = c->symtree->n.sym;
    3930              : 
    3931        65553 :   for (;;)
    3932              :     {
    3933        65553 :       m = resolve_specific_s0 (c, sym);
    3934        65553 :       if (m == MATCH_YES)
    3935              :         return true;
    3936           13 :       if (m == MATCH_ERROR)
    3937              :         return false;
    3938              : 
    3939            6 :       if (sym->ns->parent == NULL)
    3940              :         break;
    3941              : 
    3942            6 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3943              : 
    3944            6 :       if (sym == NULL)
    3945              :         break;
    3946              :     }
    3947              : 
    3948            0 :   sym = c->symtree->n.sym;
    3949            0 :   gfc_error ("Unable to resolve the specific subroutine %qs at %L",
    3950              :              sym->name, &c->loc);
    3951              : 
    3952            0 :   return false;
    3953              : }
    3954              : 
    3955              : 
    3956              : /* Resolve a subroutine call not known to be generic nor specific.  */
    3957              : 
    3958              : static bool
    3959        16003 : resolve_unknown_s (gfc_code *c)
    3960              : {
    3961        16003 :   gfc_symbol *sym;
    3962              : 
    3963        16003 :   sym = c->symtree->n.sym;
    3964              : 
    3965        16003 :   if (sym->attr.dummy)
    3966              :     {
    3967           26 :       sym->attr.proc = PROC_DUMMY;
    3968           26 :       goto found;
    3969              :     }
    3970              : 
    3971              :   /* See if we have an intrinsic function reference.  */
    3972              : 
    3973        15977 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3974              :     {
    3975         4366 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3976              :         return true;
    3977          332 :       return false;
    3978              :     }
    3979              : 
    3980              :   /* The reference is to an external name.  */
    3981              : 
    3982        11611 : found:
    3983        11637 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3984              : 
    3985        11637 :   c->resolved_sym = sym;
    3986              : 
    3987        11637 :   return pure_subroutine (sym, sym->name, &c->loc);
    3988              : }
    3989              : 
    3990              : 
    3991              : 
    3992              : static bool
    3993          805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
    3994              :                          gfc_code *c, gfc_namespace *ns)
    3995              : {
    3996          805 :   locus *here;
    3997              : 
    3998              :   /* If the type has been imported then its vtype functions are OK.  */
    3999          805 :   if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
    4000              :     return true;
    4001              : 
    4002              :   if (e)
    4003          791 :     here = &e->where;
    4004              :   else
    4005            7 :     here = &c->loc;
    4006              : 
    4007          798 :   if (s && !s->import_only)
    4008          705 :     s = gfc_find_symtree (ns->sym_root, sym->name);
    4009              : 
    4010          798 :   if (ns->import_state == IMPORT_ONLY
    4011           75 :       && sym->ns != ns
    4012           58 :       && (!s || !s->import_only))
    4013              :     {
    4014           21 :       gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
    4015              :                  "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
    4016           21 :       return false;
    4017              :     }
    4018          777 :   else if (ns->import_state == IMPORT_NONE
    4019           27 :            && sym->ns != ns)
    4020              :     {
    4021           12 :       gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
    4022              :                  "has IMPORT, NONE", sym->name, here);
    4023           12 :       return false;
    4024              :     }
    4025              :   return true;
    4026              : }
    4027              : 
    4028              : 
    4029              : static bool
    4030         7726 : check_import_status (gfc_expr *e)
    4031              : {
    4032         7726 :   gfc_symtree *st;
    4033         7726 :   gfc_ref *ref;
    4034         7726 :   gfc_symbol *sym, *der;
    4035         7726 :   gfc_namespace *ns = gfc_current_ns;
    4036              : 
    4037         7726 :   switch (e->expr_type)
    4038              :     {
    4039          727 :       case EXPR_VARIABLE:
    4040          727 :       case EXPR_FUNCTION:
    4041          727 :       case EXPR_SUBSTRING:
    4042          727 :         sym = e->symtree ? e->symtree->n.sym : NULL;
    4043              : 
    4044              :         /* Check the symbol itself.  */
    4045          727 :         if (sym
    4046          727 :             && !(ns->proc_name
    4047              :                  && (sym == ns->proc_name))
    4048         1450 :             && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
    4049              :           return false;
    4050              : 
    4051              :         /* Check the declared derived type.  */
    4052          717 :         if (sym->ts.type == BT_DERIVED)
    4053              :           {
    4054           16 :             der = sym->ts.u.derived;
    4055           16 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4056              : 
    4057           16 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4058              :               return false;
    4059              :           }
    4060          701 :         else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
    4061              :           {
    4062           44 :             der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
    4063              :                                    : sym->ts.u.derived;
    4064           44 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4065              : 
    4066           44 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4067              :               return false;
    4068              :           }
    4069              : 
    4070              :         /* Check the declared derived types of component references.  */
    4071          724 :         for (ref = e->ref; ref; ref = ref->next)
    4072           20 :           if (ref->type == REF_COMPONENT)
    4073              :             {
    4074           19 :               gfc_component *c = ref->u.c.component;
    4075           19 :               if (c->ts.type == BT_DERIVED)
    4076              :                 {
    4077            7 :                   der = c->ts.u.derived;
    4078            7 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4079            7 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4080              :                     return false;
    4081              :                 }
    4082           12 :               else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
    4083              :                 {
    4084            0 :                   der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
    4085              :                                        : c->ts.u.derived;
    4086            0 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4087            0 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4088              :                     return false;
    4089              :                 }
    4090              :             }
    4091              : 
    4092              :         break;
    4093              : 
    4094            8 :       case EXPR_ARRAY:
    4095            8 :       case EXPR_STRUCTURE:
    4096              :         /* Check the declared derived type.  */
    4097            8 :         if (e->ts.type == BT_DERIVED)
    4098              :           {
    4099            8 :             der = e->ts.u.derived;
    4100            8 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4101              : 
    4102            8 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4103              :               return false;
    4104              :           }
    4105            0 :         else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
    4106              :           {
    4107            0 :             der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
    4108              :                                    : e->ts.u.derived;
    4109            0 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4110              : 
    4111            0 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4112              :               return false;
    4113              :           }
    4114              : 
    4115              :         break;
    4116              : 
    4117              : /* Either not applicable or resolved away
    4118              :       case EXPR_OP:
    4119              :       case EXPR_UNKNOWN:
    4120              :       case EXPR_CONSTANT:
    4121              :       case EXPR_NULL:
    4122              :       case EXPR_COMPCALL:
    4123              :       case EXPR_PPC: */
    4124              : 
    4125              :       default:
    4126              :         break;
    4127              :     }
    4128              : 
    4129              :   return true;
    4130              : }
    4131              : 
    4132              : 
    4133              : /* If an elemental call has an INTENT_IN argument that has a dependency on an
    4134              :    argument which is not INTENT_IN and requires a temporary, build a temporary
    4135              :    for the INTENT_IN actual argument as well.  */
    4136              : 
    4137              : static void
    4138              : add_temp_assign_before_call (gfc_code *, gfc_namespace *, gfc_expr **);
    4139              : 
    4140              : static void
    4141         5257 : resolve_elemental_dependencies (gfc_code *c)
    4142              : {
    4143         5257 :   gfc_actual_arglist *arg1 = c->ext.actual;
    4144         5257 :   gfc_actual_arglist *arg2 = NULL;
    4145         5257 :   gfc_formal_arglist *formal1 = c->resolved_sym->formal;
    4146         5257 :   gfc_formal_arglist *formal2 = NULL;
    4147         5257 :   gfc_expr *expr1;
    4148         5257 :   gfc_expr **expr2;
    4149              : 
    4150        16645 :   for (; arg1 && formal1; arg1 = arg1->next, formal1 = formal1->next)
    4151              :     {
    4152        11388 :       if (formal1->sym
    4153        11388 :           && (formal1->sym->attr.intent == INTENT_IN
    4154         3536 :               || formal1->sym->attr.value))
    4155         8110 :         continue;
    4156              : 
    4157         3278 :       if (!arg1->expr || arg1->expr->expr_type != EXPR_VARIABLE)
    4158            0 :         continue;
    4159              : 
    4160         3278 :       arg2 = c->ext.actual;
    4161         3278 :       formal2 = c->resolved_sym->formal;
    4162        10696 :       for (; arg2 && formal2; arg2 = arg2->next, formal2 = formal2->next)
    4163              :         {
    4164         7418 :           if (arg2 == arg1 || !arg2->expr
    4165         4128 :               || !(formal2->sym && formal2->sym->attr.intent == INTENT_IN))
    4166         3304 :             continue;
    4167              : 
    4168         4114 :           expr1 = arg1->expr;
    4169         4114 :           expr2 = &arg2->expr;
    4170              : 
    4171              :           /* If the arg1 has something horrible like a vector index and
    4172              :              there is a dependency between arg1 and arg2, build a
    4173              :              temporary from arg2, assign the arg2 to it and use the
    4174              :              temporary in the call expression.  */
    4175         2009 :           if (expr1->rank && gfc_ref_needs_temporary_p (expr1->ref)
    4176         4234 :               && gfc_check_dependency (expr1, *expr2, false))
    4177           36 :             add_temp_assign_before_call (c, gfc_current_ns, expr2);
    4178              :         }
    4179              :     }
    4180         5257 : }
    4181              : 
    4182              : /* Resolve a subroutine call.  Although it was tempting to use the same code
    4183              :    for functions, subroutines and functions are stored differently and this
    4184              :    makes things awkward.  */
    4185              : 
    4186              : 
    4187              : static bool
    4188        84588 : resolve_call (gfc_code *c)
    4189              : {
    4190        84588 :   bool t;
    4191        84588 :   procedure_type ptype = PROC_INTRINSIC;
    4192        84588 :   gfc_symbol *csym, *sym;
    4193        84588 :   bool no_formal_args;
    4194              : 
    4195        84588 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4196              : 
    4197        84588 :   if (csym && csym->ts.type != BT_UNKNOWN)
    4198              :     {
    4199            4 :       gfc_error ("%qs at %L has a type, which is not consistent with "
    4200              :                  "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
    4201            4 :       return false;
    4202              :     }
    4203              : 
    4204        84584 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    4205              :     {
    4206        18051 :       gfc_symtree *st;
    4207        18051 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    4208        18051 :       sym = st ? st->n.sym : NULL;
    4209        18051 :       if (sym && csym != sym
    4210            3 :               && sym->ns == gfc_current_ns
    4211            3 :               && sym->attr.flavor == FL_PROCEDURE
    4212            3 :               && sym->attr.contained)
    4213              :         {
    4214            3 :           sym->refs++;
    4215            3 :           if (csym->attr.generic)
    4216            2 :             c->symtree->n.sym = sym;
    4217              :           else
    4218            1 :             c->symtree = st;
    4219            3 :           csym = c->symtree->n.sym;
    4220              :         }
    4221              :     }
    4222              : 
    4223              :   /* If this ia a deferred TBP, c->expr1 will be set.  */
    4224        84584 :   if (!c->expr1 && csym)
    4225              :     {
    4226        82801 :       if (csym->attr.abstract)
    4227              :         {
    4228            1 :           gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    4229              :                     csym->name, &c->loc);
    4230            1 :           return false;
    4231              :         }
    4232              : 
    4233              :       /* Subroutines without the RECURSIVE attribution are not allowed to
    4234              :          call themselves.  */
    4235        82800 :       if (is_illegal_recursion (csym, gfc_current_ns))
    4236              :         {
    4237            4 :           if (csym->attr.entry && csym->ns->entries)
    4238            2 :             gfc_error ("ENTRY %qs at %L cannot be called recursively, "
    4239              :                        "as subroutine %qs is not RECURSIVE",
    4240            2 :                        csym->name, &c->loc, csym->ns->entries->sym->name);
    4241              :           else
    4242            2 :             gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
    4243              :                        "as it is not RECURSIVE", csym->name, &c->loc);
    4244              : 
    4245        84583 :           t = false;
    4246              :         }
    4247              :     }
    4248              : 
    4249              :   /* Switch off assumed size checking and do this again for certain kinds
    4250              :      of procedure, once the procedure itself is resolved.  */
    4251        84583 :   need_full_assumed_size++;
    4252              : 
    4253        84583 :   if (csym)
    4254        84583 :     ptype = csym->attr.proc;
    4255              : 
    4256        84583 :   no_formal_args = csym && is_external_proc (csym)
    4257        15743 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4258        84583 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4259              :     return false;
    4260              : 
    4261              :   /* Resume assumed_size checking.  */
    4262        84549 :   need_full_assumed_size--;
    4263              : 
    4264              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4265              :      check for an 'external' attribute.  */
    4266        84549 :   if (csym->ns->has_implicit_none_export
    4267         4608 :       && csym->attr.external == 0 && csym->attr.dummy == 1)
    4268              :     {
    4269            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    4270              :                  csym->name, &c->loc);
    4271            1 :       return false;
    4272              :     }
    4273              : 
    4274              :   /* If external, check for usage.  */
    4275        84548 :   if (csym && is_external_proc (csym))
    4276        15737 :     resolve_global_procedure (csym, &c->loc, 1);
    4277              : 
    4278              :   /* If we have an external dummy argument, we want to write out its arguments
    4279              :      with -fc-prototypes-external.  Code like
    4280              : 
    4281              :      subroutine foo(a,n)
    4282              :        external a
    4283              :        if (n == 1) call a(1)
    4284              :        if (n == 2) call a(2,3)
    4285              :      end subroutine foo
    4286              : 
    4287              :      is actually legal Fortran, but it is not possible to generate a C23-
    4288              :      compliant prototype for this, so we just record the fact here and
    4289              :      handle that during -fc-prototypes-external processing.  */
    4290              : 
    4291        84548 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4292           14 :       && csym->attr.external)
    4293              :     {
    4294           14 :       if (csym->formal)
    4295              :         {
    4296            6 :           bool conflict;
    4297            6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4298              :                                                  0, 0, 0, NULL);
    4299            6 :           if (conflict)
    4300              :             {
    4301            6 :               csym->ext_dummy_arglist_mismatch = 1;
    4302            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4303              :                            "Different argument lists in external dummy "
    4304              :                            "subroutine %s at %L and %L", csym->name,
    4305              :                            &c->loc, &csym->other_loc);
    4306              :             }
    4307              :         }
    4308            8 :       else if (!csym->formal_resolved)
    4309              :         {
    4310            7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4311            7 :           csym->other_loc = c->loc;
    4312              :         }
    4313              :     }
    4314              : 
    4315        84548 :   t = true;
    4316        84548 :   if (c->resolved_sym == NULL)
    4317              :     {
    4318        84440 :       c->resolved_isym = NULL;
    4319        84440 :       switch (procedure_kind (csym))
    4320              :         {
    4321         2890 :         case PTYPE_GENERIC:
    4322         2890 :           t = resolve_generic_s (c);
    4323         2890 :           break;
    4324              : 
    4325        65547 :         case PTYPE_SPECIFIC:
    4326        65547 :           t = resolve_specific_s (c);
    4327        65547 :           break;
    4328              : 
    4329        16003 :         case PTYPE_UNKNOWN:
    4330        16003 :           t = resolve_unknown_s (c);
    4331        16003 :           break;
    4332              : 
    4333              :         default:
    4334              :           gfc_internal_error ("resolve_subroutine(): bad function type");
    4335              :         }
    4336              :     }
    4337              : 
    4338              :   /* Some checks of elemental subroutine actual arguments.  */
    4339        84547 :   if (!resolve_elemental_actual (NULL, c))
    4340              :     return false;
    4341              : 
    4342              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4343        84539 :   if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args
    4344         6212 :       && c->ext.actual && c->ext.actual->next)
    4345         5257 :     resolve_elemental_dependencies (c);
    4346              : 
    4347        84539 :   if (!c->expr1)
    4348        82756 :     update_current_proc_array_outer_dependency (csym);
    4349              :   else
    4350              :     /* Typebound procedure: Assume the worst.  */
    4351         1783 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4352              : 
    4353        84539 :   if (c->resolved_sym
    4354        84203 :       && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    4355           34 :     gfc_warning (OPT_Wdeprecated_declarations,
    4356              :                  "Using subroutine %qs at %L is deprecated",
    4357              :                  c->resolved_sym->name, &c->loc);
    4358              : 
    4359        84539 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4360        84539 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
    4361            2 :       && csym != gfc_current_ns->proc_name)
    4362            1 :     return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
    4363              : 
    4364              :   return t;
    4365              : }
    4366              : 
    4367              : 
    4368              : /* Compare the shapes of two arrays that have non-NULL shapes.  If both
    4369              :    op1->shape and op2->shape are non-NULL return true if their shapes
    4370              :    match.  If both op1->shape and op2->shape are non-NULL return false
    4371              :    if their shapes do not match.  If either op1->shape or op2->shape is
    4372              :    NULL, return true.  */
    4373              : 
    4374              : static bool
    4375        33846 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4376              : {
    4377        33846 :   bool t;
    4378        33846 :   int i;
    4379              : 
    4380        33846 :   t = true;
    4381              : 
    4382        33846 :   if (op1->shape != NULL && op2->shape != NULL)
    4383              :     {
    4384        44288 :       for (i = 0; i < op1->rank; i++)
    4385              :         {
    4386        23599 :           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
    4387              :            {
    4388            3 :              gfc_error ("Shapes for operands at %L and %L are not conformable",
    4389              :                         &op1->where, &op2->where);
    4390            3 :              t = false;
    4391            3 :              break;
    4392              :            }
    4393              :         }
    4394              :     }
    4395              : 
    4396        33846 :   return t;
    4397              : }
    4398              : 
    4399              : /* Convert a logical operator to the corresponding bitwise intrinsic call.
    4400              :    For example A .AND. B becomes IAND(A, B).  */
    4401              : static gfc_expr *
    4402          668 : logical_to_bitwise (gfc_expr *e)
    4403              : {
    4404          668 :   gfc_expr *tmp, *op1, *op2;
    4405          668 :   gfc_isym_id isym;
    4406          668 :   gfc_actual_arglist *args = NULL;
    4407              : 
    4408          668 :   gcc_assert (e->expr_type == EXPR_OP);
    4409              : 
    4410          668 :   isym = GFC_ISYM_NONE;
    4411          668 :   op1 = e->value.op.op1;
    4412          668 :   op2 = e->value.op.op2;
    4413              : 
    4414          668 :   switch (e->value.op.op)
    4415              :     {
    4416              :     case INTRINSIC_NOT:
    4417              :       isym = GFC_ISYM_NOT;
    4418              :       break;
    4419          126 :     case INTRINSIC_AND:
    4420          126 :       isym = GFC_ISYM_IAND;
    4421          126 :       break;
    4422          127 :     case INTRINSIC_OR:
    4423          127 :       isym = GFC_ISYM_IOR;
    4424          127 :       break;
    4425          270 :     case INTRINSIC_NEQV:
    4426          270 :       isym = GFC_ISYM_IEOR;
    4427          270 :       break;
    4428          126 :     case INTRINSIC_EQV:
    4429              :       /* "Bitwise eqv" is just the complement of NEQV === IEOR.
    4430              :          Change the old expression to NEQV, which will get replaced by IEOR,
    4431              :          and wrap it in NOT.  */
    4432          126 :       tmp = gfc_copy_expr (e);
    4433          126 :       tmp->value.op.op = INTRINSIC_NEQV;
    4434          126 :       tmp = logical_to_bitwise (tmp);
    4435          126 :       isym = GFC_ISYM_NOT;
    4436          126 :       op1 = tmp;
    4437          126 :       op2 = NULL;
    4438          126 :       break;
    4439            0 :     default:
    4440            0 :       gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
    4441              :     }
    4442              : 
    4443              :   /* Inherit the original operation's operands as arguments.  */
    4444          668 :   args = gfc_get_actual_arglist ();
    4445          668 :   args->expr = op1;
    4446          668 :   if (op2)
    4447              :     {
    4448          523 :       args->next = gfc_get_actual_arglist ();
    4449          523 :       args->next->expr = op2;
    4450              :     }
    4451              : 
    4452              :   /* Convert the expression to a function call.  */
    4453          668 :   e->expr_type = EXPR_FUNCTION;
    4454          668 :   e->value.function.actual = args;
    4455          668 :   e->value.function.isym = gfc_intrinsic_function_by_id (isym);
    4456          668 :   e->value.function.name = e->value.function.isym->name;
    4457          668 :   e->value.function.esym = NULL;
    4458              : 
    4459              :   /* Make up a pre-resolved function call symtree if we need to.  */
    4460          668 :   if (!e->symtree || !e->symtree->n.sym)
    4461              :     {
    4462          668 :       gfc_symbol *sym;
    4463          668 :       gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
    4464          668 :       sym = e->symtree->n.sym;
    4465          668 :       sym->result = sym;
    4466          668 :       sym->attr.flavor = FL_PROCEDURE;
    4467          668 :       sym->attr.function = 1;
    4468          668 :       sym->attr.elemental = 1;
    4469          668 :       sym->attr.pure = 1;
    4470          668 :       sym->attr.referenced = 1;
    4471          668 :       gfc_intrinsic_symbol (sym);
    4472          668 :       gfc_commit_symbol (sym);
    4473              :     }
    4474              : 
    4475          668 :   args->name = e->value.function.isym->formal->name;
    4476          668 :   if (e->value.function.isym->formal->next)
    4477          523 :     args->next->name = e->value.function.isym->formal->next->name;
    4478              : 
    4479          668 :   return e;
    4480              : }
    4481              : 
    4482              : /* Recursively append candidate UOP to CANDIDATES.  Store the number of
    4483              :    candidates in CANDIDATES_LEN.  */
    4484              : static void
    4485          114 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4486              :                                   char **&candidates,
    4487              :                                   size_t &candidates_len)
    4488              : {
    4489          116 :   gfc_symtree *p;
    4490              : 
    4491          116 :   if (uop == NULL)
    4492              :     return;
    4493              : 
    4494              :   /* Not sure how to properly filter here.  Use all for a start.
    4495              :      n.uop.op is NULL for empty interface operators (is that legal?) disregard
    4496              :      these as i suppose they don't make terribly sense.  */
    4497              : 
    4498          116 :   if (uop->n.uop->op != NULL)
    4499            2 :     vec_push (candidates, candidates_len, uop->name);
    4500              : 
    4501          116 :   p = uop->left;
    4502          116 :   if (p)
    4503           36 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4504              : 
    4505          116 :   p = uop->right;
    4506          116 :   if (p)
    4507              :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4508              : }
    4509              : 
    4510              : /* Lookup user-operator OP fuzzily, taking names in UOP into account.  */
    4511              : 
    4512              : static const char*
    4513           78 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4514              : {
    4515           78 :   char **candidates = NULL;
    4516           78 :   size_t candidates_len = 0;
    4517           78 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4518           78 :   return gfc_closest_fuzzy_match (op, candidates);
    4519              : }
    4520              : 
    4521              : 
    4522              : /* Callback finding an impure function as an operand to an .and. or
    4523              :    .or.  expression.  Remember the last function warned about to
    4524              :    avoid double warnings when recursing.  */
    4525              : 
    4526              : static int
    4527       195876 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4528              :                           void *data)
    4529              : {
    4530       195876 :   gfc_expr *f = *e;
    4531       195876 :   const char *name;
    4532       195876 :   static gfc_expr *last = NULL;
    4533       195876 :   bool *found = (bool *) data;
    4534              : 
    4535       195876 :   if (f->expr_type == EXPR_FUNCTION)
    4536              :     {
    4537        12185 :       *found = 1;
    4538        12180 :       if (f != last && !gfc_pure_function (f, &name)
    4539        13507 :           && !gfc_implicit_pure_function (f))
    4540              :         {
    4541         1183 :           if (name)
    4542         1183 :             gfc_warning (OPT_Wfunction_elimination,
    4543              :                          "Impure function %qs at %L might not be evaluated",
    4544              :                          name, &f->where);
    4545              :           else
    4546            0 :             gfc_warning (OPT_Wfunction_elimination,
    4547              :                          "Impure function at %L might not be evaluated",
    4548              :                          &f->where);
    4549              :         }
    4550        12185 :       last = f;
    4551              :     }
    4552              : 
    4553       195876 :   return 0;
    4554              : }
    4555              : 
    4556              : /* Return true if TYPE is character based, false otherwise.  */
    4557              : 
    4558              : static int
    4559         1373 : is_character_based (bt type)
    4560              : {
    4561         1373 :   return type == BT_CHARACTER || type == BT_HOLLERITH;
    4562              : }
    4563              : 
    4564              : 
    4565              : /* If expression is a hollerith, convert it to character and issue a warning
    4566              :    for the conversion.  */
    4567              : 
    4568              : static void
    4569          408 : convert_hollerith_to_character (gfc_expr *e)
    4570              : {
    4571          408 :   if (e->ts.type == BT_HOLLERITH)
    4572              :     {
    4573          108 :       gfc_typespec t;
    4574          108 :       gfc_clear_ts (&t);
    4575          108 :       t.type = BT_CHARACTER;
    4576          108 :       t.kind = e->ts.kind;
    4577          108 :       gfc_convert_type_warn (e, &t, 2, 1);
    4578              :     }
    4579          408 : }
    4580              : 
    4581              : /* Convert to numeric and issue a warning for the conversion.  */
    4582              : 
    4583              : static void
    4584          240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
    4585              : {
    4586          240 :   gfc_typespec t;
    4587          240 :   gfc_clear_ts (&t);
    4588          240 :   t.type = b->ts.type;
    4589          240 :   t.kind = b->ts.kind;
    4590          240 :   gfc_convert_type_warn (a, &t, 2, 1);
    4591          240 : }
    4592              : 
    4593              : /* Resolve an operator expression node.  This can involve replacing the
    4594              :    operation with a user defined function call.  CHECK_INTERFACES is a
    4595              :    helper macro.  */
    4596              : 
    4597              : #define CHECK_INTERFACES \
    4598              :   { \
    4599              :     match m = gfc_extend_expr (e); \
    4600              :     if (m == MATCH_YES) \
    4601              :       return true; \
    4602              :     if (m == MATCH_ERROR) \
    4603              :       return false; \
    4604              :   }
    4605              : 
    4606              : static bool
    4607       547630 : resolve_operator (gfc_expr *e)
    4608              : {
    4609       547630 :   gfc_expr *op1, *op2;
    4610              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4611       547630 :   bool t = true;
    4612              : 
    4613              :   /* Reduce stacked parentheses to single pair  */
    4614       547630 :   while (e->expr_type == EXPR_OP
    4615       547788 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4616        23883 :          && e->value.op.op1->expr_type == EXPR_OP
    4617       564769 :          && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
    4618              :     {
    4619          158 :       gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
    4620          158 :       gfc_replace_expr (e, tmp);
    4621              :     }
    4622              : 
    4623              :   /* Resolve all subnodes-- give them types.  */
    4624              : 
    4625       547630 :   switch (e->value.op.op)
    4626              :     {
    4627       494815 :     default:
    4628       494815 :       if (!gfc_resolve_expr (e->value.op.op2))
    4629       547630 :         t = false;
    4630              : 
    4631              :     /* Fall through.  */
    4632              : 
    4633       547630 :     case INTRINSIC_NOT:
    4634       547630 :     case INTRINSIC_UPLUS:
    4635       547630 :     case INTRINSIC_UMINUS:
    4636       547630 :     case INTRINSIC_PARENTHESES:
    4637       547630 :       if (!gfc_resolve_expr (e->value.op.op1))
    4638              :         return false;
    4639       547469 :       if (e->value.op.op1
    4640       547460 :           && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
    4641              :         {
    4642            0 :           gfc_error ("BOZ literal constant at %L cannot be an operand of "
    4643            0 :                      "unary operator %qs", &e->value.op.op1->where,
    4644              :                      gfc_op2string (e->value.op.op));
    4645            0 :           return false;
    4646              :         }
    4647       547469 :       if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
    4648            6 :           && e->value.op.op == INTRINSIC_UMINUS)
    4649              :         {
    4650            2 :           gfc_error ("Negation of unsigned expression at %L not permitted ",
    4651              :                      &e->value.op.op1->where);
    4652            2 :           return false;
    4653              :         }
    4654       547467 :       break;
    4655              :     }
    4656              : 
    4657              :   /* Typecheck the new node.  */
    4658              : 
    4659       547467 :   op1 = e->value.op.op1;
    4660       547467 :   op2 = e->value.op.op2;
    4661       547467 :   if (op1 == NULL && op2 == NULL)
    4662              :     return false;
    4663              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4664       547458 :   if (t == false)
    4665              :     return false;
    4666              : 
    4667              :   /* op1 and op2 cannot both be BOZ.  */
    4668       547392 :   if (op1 && op1->ts.type == BT_BOZ
    4669            0 :       && op2 && op2->ts.type == BT_BOZ)
    4670              :     {
    4671            0 :       gfc_error ("Operands at %L and %L cannot appear as operands of "
    4672            0 :                  "binary operator %qs", &op1->where, &op2->where,
    4673              :                  gfc_op2string (e->value.op.op));
    4674            0 :       return false;
    4675              :     }
    4676              : 
    4677       547392 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4678       547390 :       || (op2 && op2->expr_type == EXPR_NULL))
    4679              :     {
    4680            3 :       CHECK_INTERFACES
    4681            3 :       gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
    4682            3 :       return false;
    4683              :     }
    4684              : 
    4685       547389 :   switch (e->value.op.op)
    4686              :     {
    4687         8343 :     case INTRINSIC_UPLUS:
    4688         8343 :     case INTRINSIC_UMINUS:
    4689         8343 :       if (op1->ts.type == BT_INTEGER
    4690              :           || op1->ts.type == BT_REAL
    4691              :           || op1->ts.type == BT_COMPLEX
    4692              :           || op1->ts.type == BT_UNSIGNED)
    4693              :         {
    4694         8274 :           e->ts = op1->ts;
    4695         8274 :           break;
    4696              :         }
    4697              : 
    4698           69 :       CHECK_INTERFACES
    4699           43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4700              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4701           43 :       return false;
    4702              : 
    4703       160618 :     case INTRINSIC_POWER:
    4704       160618 :     case INTRINSIC_PLUS:
    4705       160618 :     case INTRINSIC_MINUS:
    4706       160618 :     case INTRINSIC_TIMES:
    4707       160618 :     case INTRINSIC_DIVIDE:
    4708              : 
    4709              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4710              :              conversion.  */
    4711       160618 :       if (flag_unsigned &&  gfc_invalid_unsigned_ops (op1, op2))
    4712              :         {
    4713            3 :           CHECK_INTERFACES
    4714            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are "
    4715              :                      "%s/%s", gfc_op2string (e->value.op.op), &e->where,
    4716              :                      gfc_typename (op1), gfc_typename (op2));
    4717            3 :           return false;
    4718              :         }
    4719              : 
    4720       160615 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4721              :         {
    4722              :           /* Do not perform conversions if operands are not conformable as
    4723              :              required for the binary intrinsic operators (F2018:10.1.5).
    4724              :              Defer to a possibly overloading user-defined operator.  */
    4725       160161 :           if (!gfc_op_rank_conformable (op1, op2))
    4726              :             {
    4727           36 :               CHECK_INTERFACES
    4728            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4729            0 :                          &op1->where, &op2->where);
    4730            0 :               return false;
    4731              :             }
    4732              : 
    4733       160125 :           gfc_type_convert_binary (e, 1);
    4734       160125 :           break;
    4735              :         }
    4736              : 
    4737          454 :       if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
    4738              :         {
    4739          225 :           CHECK_INTERFACES
    4740            2 :           gfc_error ("Unexpected derived-type entities in binary intrinsic "
    4741              :                      "numeric operator %qs at %L",
    4742              :                      gfc_op2string (e->value.op.op), &e->where);
    4743            2 :           return false;
    4744              :         }
    4745              :       else
    4746              :         {
    4747          229 :           CHECK_INTERFACES
    4748            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
    4749              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4750              :                      gfc_typename (op2));
    4751            3 :           return false;
    4752              :         }
    4753              : 
    4754         2459 :     case INTRINSIC_CONCAT:
    4755         2459 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4756         2434 :           && op1->ts.kind == op2->ts.kind)
    4757              :         {
    4758         2425 :           e->ts.type = BT_CHARACTER;
    4759         2425 :           e->ts.kind = op1->ts.kind;
    4760         2425 :           break;
    4761              :         }
    4762              : 
    4763           34 :       CHECK_INTERFACES
    4764           10 :       gfc_error ("Operands of string concatenation operator at %L are %s/%s",
    4765              :                  &e->where, gfc_typename (op1), gfc_typename (op2));
    4766           10 :       return false;
    4767              : 
    4768        70491 :     case INTRINSIC_AND:
    4769        70491 :     case INTRINSIC_OR:
    4770        70491 :     case INTRINSIC_EQV:
    4771        70491 :     case INTRINSIC_NEQV:
    4772        70491 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4773              :         {
    4774        69940 :           e->ts.type = BT_LOGICAL;
    4775        69940 :           e->ts.kind = gfc_kind_max (op1, op2);
    4776        69940 :           if (op1->ts.kind < e->ts.kind)
    4777          140 :             gfc_convert_type (op1, &e->ts, 2);
    4778        69800 :           else if (op2->ts.kind < e->ts.kind)
    4779          117 :             gfc_convert_type (op2, &e->ts, 2);
    4780              : 
    4781        69940 :           if (flag_frontend_optimize &&
    4782        58779 :             (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
    4783              :             {
    4784              :               /* Warn about short-circuiting
    4785              :                  with impure function as second operand.  */
    4786        52744 :               bool op2_f = false;
    4787        52744 :               gfc_expr_walker (&op2, impure_function_callback, &op2_f);
    4788              :             }
    4789              :           break;
    4790              :         }
    4791              : 
    4792              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4793          551 :       else if (flag_dec
    4794          523 :                && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
    4795              :         {
    4796          523 :           e->ts.type = BT_INTEGER;
    4797          523 :           e->ts.kind = gfc_kind_max (op1, op2);
    4798          523 :           if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
    4799          289 :             gfc_convert_type (op1, &e->ts, 1);
    4800          523 :           if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
    4801          144 :             gfc_convert_type (op2, &e->ts, 1);
    4802          523 :           e = logical_to_bitwise (e);
    4803          523 :           goto simplify_op;
    4804              :         }
    4805              : 
    4806           28 :       CHECK_INTERFACES
    4807           16 :       gfc_error ("Operands of logical operator %qs at %L are %s/%s",
    4808              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4809              :                  gfc_typename (op2));
    4810           16 :       return false;
    4811              : 
    4812        20731 :     case INTRINSIC_NOT:
    4813              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4814        20731 :       if (flag_dec && op1->ts.type == BT_INTEGER)
    4815              :         {
    4816           19 :           e->ts.type = BT_INTEGER;
    4817           19 :           e->ts.kind = op1->ts.kind;
    4818           19 :           e = logical_to_bitwise (e);
    4819           19 :           goto simplify_op;
    4820              :         }
    4821              : 
    4822        20712 :       if (op1->ts.type == BT_LOGICAL)
    4823              :         {
    4824        20706 :           e->ts.type = BT_LOGICAL;
    4825        20706 :           e->ts.kind = op1->ts.kind;
    4826        20706 :           break;
    4827              :         }
    4828              : 
    4829            6 :       CHECK_INTERFACES
    4830            3 :       gfc_error ("Operand of .not. operator at %L is %s", &e->where,
    4831              :                  gfc_typename (op1));
    4832            3 :       return false;
    4833              : 
    4834        21902 :     case INTRINSIC_GT:
    4835        21902 :     case INTRINSIC_GT_OS:
    4836        21902 :     case INTRINSIC_GE:
    4837        21902 :     case INTRINSIC_GE_OS:
    4838        21902 :     case INTRINSIC_LT:
    4839        21902 :     case INTRINSIC_LT_OS:
    4840        21902 :     case INTRINSIC_LE:
    4841        21902 :     case INTRINSIC_LE_OS:
    4842        21902 :       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
    4843              :         {
    4844           18 :           CHECK_INTERFACES
    4845            0 :           gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
    4846            0 :           return false;
    4847              :         }
    4848              : 
    4849              :       /* Fall through.  */
    4850              : 
    4851       260740 :     case INTRINSIC_EQ:
    4852       260740 :     case INTRINSIC_EQ_OS:
    4853       260740 :     case INTRINSIC_NE:
    4854       260740 :     case INTRINSIC_NE_OS:
    4855              : 
    4856       260740 :       if (flag_dec
    4857         1038 :           && is_character_based (op1->ts.type)
    4858       261075 :           && is_character_based (op2->ts.type))
    4859              :         {
    4860          204 :           convert_hollerith_to_character (op1);
    4861          204 :           convert_hollerith_to_character (op2);
    4862              :         }
    4863              : 
    4864       260740 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4865        39276 :           && op1->ts.kind == op2->ts.kind)
    4866              :         {
    4867        39239 :           e->ts.type = BT_LOGICAL;
    4868        39239 :           e->ts.kind = gfc_default_logical_kind;
    4869        39239 :           break;
    4870              :         }
    4871              : 
    4872              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4873       221501 :       if (op1->ts.type == BT_BOZ)
    4874              :         {
    4875            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
    4876              :                                "as an operand of a relational operator"),
    4877              :                                &op1->where))
    4878              :             return false;
    4879              : 
    4880            0 :           if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
    4881              :             return false;
    4882              : 
    4883            0 :           if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
    4884              :             return false;
    4885              :         }
    4886              : 
    4887              :       /* If op2 is BOZ, then op1 is not!.  Try to convert to type of op2. */
    4888       221501 :       if (op2->ts.type == BT_BOZ)
    4889              :         {
    4890            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
    4891              :                                " as an operand of a relational operator"),
    4892              :                                 &op2->where))
    4893              :             return false;
    4894              : 
    4895            0 :           if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
    4896              :             return false;
    4897              : 
    4898            0 :           if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
    4899              :             return false;
    4900              :         }
    4901       221501 :       if (flag_dec
    4902       221501 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4903          120 :         convert_to_numeric (op1, op2);
    4904              : 
    4905       221501 :       if (flag_dec
    4906       221501 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4907          120 :         convert_to_numeric (op2, op1);
    4908              : 
    4909       221501 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4910              :         {
    4911              :           /* Do not perform conversions if operands are not conformable as
    4912              :              required for the binary intrinsic operators (F2018:10.1.5).
    4913              :              Defer to a possibly overloading user-defined operator.  */
    4914       220372 :           if (!gfc_op_rank_conformable (op1, op2))
    4915              :             {
    4916           70 :               CHECK_INTERFACES
    4917            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4918            0 :                          &op1->where, &op2->where);
    4919            0 :               return false;
    4920              :             }
    4921              : 
    4922       220302 :           if (flag_unsigned  && gfc_invalid_unsigned_ops (op1, op2))
    4923              :             {
    4924            1 :               CHECK_INTERFACES
    4925            1 :               gfc_error ("Inconsistent types for operator at %L and %L: "
    4926            1 :                          "%s and %s", &op1->where, &op2->where,
    4927              :                          gfc_typename (op1), gfc_typename (op2));
    4928            1 :               return false;
    4929              :             }
    4930              : 
    4931       220301 :           gfc_type_convert_binary (e, 1);
    4932              : 
    4933       220301 :           e->ts.type = BT_LOGICAL;
    4934       220301 :           e->ts.kind = gfc_default_logical_kind;
    4935              : 
    4936       220301 :           if (warn_compare_reals)
    4937              :             {
    4938           70 :               gfc_intrinsic_op op = e->value.op.op;
    4939              : 
    4940              :               /* Type conversion has made sure that the types of op1 and op2
    4941              :                  agree, so it is only necessary to check the first one.   */
    4942           70 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4943           13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4944            6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4945              :                 {
    4946           13 :                   const char *msg;
    4947              : 
    4948           13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4949              :                     msg = G_("Equality comparison for %s at %L");
    4950              :                   else
    4951            6 :                     msg = G_("Inequality comparison for %s at %L");
    4952              : 
    4953           13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4954              :                                gfc_typename (op1), &op1->where);
    4955              :                 }
    4956              :             }
    4957              : 
    4958              :           break;
    4959              :         }
    4960              : 
    4961         1129 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4962              :         {
    4963            2 :           CHECK_INTERFACES
    4964            4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4965              :                      &e->where,
    4966            2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4967              :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4968            2 :         }
    4969              :       else
    4970              :         {
    4971         1127 :           CHECK_INTERFACES
    4972          113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4973              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4974              :                      gfc_typename (op2));
    4975              :         }
    4976              : 
    4977              :       return false;
    4978              : 
    4979          303 :     case INTRINSIC_USER:
    4980          303 :       if (e->value.op.uop->op == NULL)
    4981              :         {
    4982           78 :           const char *name = e->value.op.uop->name;
    4983           78 :           const char *guessed;
    4984           78 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4985           78 :           CHECK_INTERFACES
    4986            5 :           if (guessed)
    4987            1 :             gfc_error ("Unknown operator %qs at %L; did you mean "
    4988              :                         "%qs?", name, &e->where, guessed);
    4989              :           else
    4990            4 :             gfc_error ("Unknown operator %qs at %L", name, &e->where);
    4991              :         }
    4992          225 :       else if (op2 == NULL)
    4993              :         {
    4994           48 :           CHECK_INTERFACES
    4995            0 :           gfc_error ("Operand of user operator %qs at %L is %s",
    4996            0 :                   e->value.op.uop->name, &e->where, gfc_typename (op1));
    4997              :         }
    4998              :       else
    4999              :         {
    5000          177 :           e->value.op.uop->op->sym->attr.referenced = 1;
    5001          177 :           CHECK_INTERFACES
    5002            5 :           gfc_error ("Operands of user operator %qs at %L are %s/%s",
    5003            5 :                     e->value.op.uop->name, &e->where, gfc_typename (op1),
    5004              :                     gfc_typename (op2));
    5005              :         }
    5006              : 
    5007              :       return false;
    5008              : 
    5009        23686 :     case INTRINSIC_PARENTHESES:
    5010        23686 :       e->ts = op1->ts;
    5011        23686 :       if (e->ts.type == BT_CHARACTER)
    5012          342 :         e->ts.u.cl = op1->ts.u.cl;
    5013              :       break;
    5014              : 
    5015            0 :     default:
    5016            0 :       gfc_internal_error ("resolve_operator(): Bad intrinsic");
    5017              :     }
    5018              : 
    5019              :   /* Deal with arrayness of an operand through an operator.  */
    5020              : 
    5021       544696 :   switch (e->value.op.op)
    5022              :     {
    5023       492030 :     case INTRINSIC_PLUS:
    5024       492030 :     case INTRINSIC_MINUS:
    5025       492030 :     case INTRINSIC_TIMES:
    5026       492030 :     case INTRINSIC_DIVIDE:
    5027       492030 :     case INTRINSIC_POWER:
    5028       492030 :     case INTRINSIC_CONCAT:
    5029       492030 :     case INTRINSIC_AND:
    5030       492030 :     case INTRINSIC_OR:
    5031       492030 :     case INTRINSIC_EQV:
    5032       492030 :     case INTRINSIC_NEQV:
    5033       492030 :     case INTRINSIC_EQ:
    5034       492030 :     case INTRINSIC_EQ_OS:
    5035       492030 :     case INTRINSIC_NE:
    5036       492030 :     case INTRINSIC_NE_OS:
    5037       492030 :     case INTRINSIC_GT:
    5038       492030 :     case INTRINSIC_GT_OS:
    5039       492030 :     case INTRINSIC_GE:
    5040       492030 :     case INTRINSIC_GE_OS:
    5041       492030 :     case INTRINSIC_LT:
    5042       492030 :     case INTRINSIC_LT_OS:
    5043       492030 :     case INTRINSIC_LE:
    5044       492030 :     case INTRINSIC_LE_OS:
    5045              : 
    5046       492030 :       if (op1->rank == 0 && op2->rank == 0)
    5047       437934 :         e->rank = 0;
    5048              : 
    5049       492030 :       if (op1->rank == 0 && op2->rank != 0)
    5050              :         {
    5051         2646 :           e->rank = op2->rank;
    5052              : 
    5053         2646 :           if (e->shape == NULL)
    5054         2615 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    5055              :         }
    5056              : 
    5057       492030 :       if (op1->rank != 0 && op2->rank == 0)
    5058              :         {
    5059        17543 :           e->rank = op1->rank;
    5060              : 
    5061        17543 :           if (e->shape == NULL)
    5062        17519 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5063              :         }
    5064              : 
    5065       492030 :       if (op1->rank != 0 && op2->rank != 0)
    5066              :         {
    5067        33907 :           if (op1->rank == op2->rank)
    5068              :             {
    5069        33907 :               e->rank = op1->rank;
    5070        33907 :               if (e->shape == NULL)
    5071              :                 {
    5072        33846 :                   t = compare_shapes (op1, op2);
    5073        33846 :                   if (!t)
    5074            3 :                     e->shape = NULL;
    5075              :                   else
    5076        33843 :                     e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5077              :                 }
    5078              :             }
    5079              :           else
    5080              :             {
    5081              :               /* Allow higher level expressions to work.  */
    5082            0 :               e->rank = 0;
    5083              : 
    5084              :               /* Try user-defined operators, and otherwise throw an error.  */
    5085            0 :               CHECK_INTERFACES
    5086            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    5087            0 :                          &op1->where, &op2->where);
    5088            0 :               return false;
    5089              :             }
    5090              :         }
    5091              :       break;
    5092              : 
    5093        52666 :     case INTRINSIC_PARENTHESES:
    5094        52666 :     case INTRINSIC_NOT:
    5095        52666 :     case INTRINSIC_UPLUS:
    5096        52666 :     case INTRINSIC_UMINUS:
    5097              :       /* Simply copy arrayness attribute */
    5098        52666 :       e->rank = op1->rank;
    5099        52666 :       e->corank = op1->corank;
    5100              : 
    5101        52666 :       if (e->shape == NULL)
    5102        52656 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5103              : 
    5104              :       break;
    5105              : 
    5106              :     default:
    5107              :       break;
    5108              :     }
    5109              : 
    5110       545238 : simplify_op:
    5111              : 
    5112              :   /* Attempt to simplify the expression.  */
    5113            3 :   if (t)
    5114              :     {
    5115       545235 :       t = gfc_simplify_expr (e, 0);
    5116              :       /* Some calls do not succeed in simplification and return false
    5117              :          even though there is no error; e.g. variable references to
    5118              :          PARAMETER arrays.  */
    5119       545235 :       if (!gfc_is_constant_expr (e))
    5120       498224 :         t = true;
    5121              :     }
    5122              :   return t;
    5123              : }
    5124              : 
    5125              : static bool
    5126          170 : resolve_conditional (gfc_expr *expr)
    5127              : {
    5128          170 :   gfc_expr *condition, *true_expr, *false_expr;
    5129              : 
    5130          170 :   condition = expr->value.conditional.condition;
    5131          170 :   true_expr = expr->value.conditional.true_expr;
    5132          170 :   false_expr = expr->value.conditional.false_expr;
    5133              : 
    5134          340 :   if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
    5135          340 :       || !gfc_resolve_expr (false_expr))
    5136              :     return false;
    5137              : 
    5138          170 :   if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
    5139              :     {
    5140            2 :       gfc_error (
    5141              :         "Condition in conditional expression must be a scalar logical at %L",
    5142              :         &condition->where);
    5143            2 :       return false;
    5144              :     }
    5145              : 
    5146          168 :   if (true_expr->ts.type != false_expr->ts.type)
    5147              :     {
    5148            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5149              :                  "must have the same declared type",
    5150              :                  &true_expr->where, &false_expr->where);
    5151            1 :       return false;
    5152              :     }
    5153              : 
    5154          167 :   if (true_expr->ts.kind != false_expr->ts.kind)
    5155              :     {
    5156            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5157              :                  "must have the same kind parameter",
    5158              :                  &true_expr->where, &false_expr->where);
    5159            1 :       return false;
    5160              :     }
    5161              : 
    5162          166 :   if (true_expr->rank != false_expr->rank)
    5163              :     {
    5164            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5165              :                  "must have the same rank",
    5166              :                  &true_expr->where, &false_expr->where);
    5167            1 :       return false;
    5168              :     }
    5169              : 
    5170              :   /* TODO: support more data types for conditional expressions  */
    5171          165 :   if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
    5172          165 :       && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
    5173           67 :       && true_expr->ts.type != BT_CHARACTER)
    5174              :     {
    5175            1 :       gfc_error (
    5176              :         "Sorry, only integer, logical, real, complex and character types are "
    5177              :         "currently supported for conditional expressions at %L",
    5178              :         &expr->where);
    5179            1 :       return false;
    5180              :     }
    5181              : 
    5182              :   /* TODO: support arrays in conditional expressions  */
    5183          164 :   if (true_expr->rank > 0)
    5184              :     {
    5185            1 :       gfc_error ("Sorry, array is currently unsupported for conditional "
    5186              :                  "expressions at %L",
    5187              :                  &expr->where);
    5188            1 :       return false;
    5189              :     }
    5190              : 
    5191          163 :   expr->ts = true_expr->ts;
    5192          163 :   expr->rank = true_expr->rank;
    5193          163 :   return true;
    5194              : }
    5195              : 
    5196              : /************** Array resolution subroutines **************/
    5197              : 
    5198              : enum compare_result
    5199              : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
    5200              : 
    5201              : /* Compare two integer expressions.  */
    5202              : 
    5203              : static compare_result
    5204       482306 : compare_bound (gfc_expr *a, gfc_expr *b)
    5205              : {
    5206       482306 :   int i;
    5207              : 
    5208       482306 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5209       316881 :       || b == NULL || b->expr_type != EXPR_CONSTANT)
    5210              :     return CMP_UNKNOWN;
    5211              : 
    5212              :   /* If either of the types isn't INTEGER, we must have
    5213              :      raised an error earlier.  */
    5214              : 
    5215       217473 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5216              :     return CMP_UNKNOWN;
    5217              : 
    5218       217469 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5219              : 
    5220       217469 :   if (i < 0)
    5221              :     return CMP_LT;
    5222       102309 :   if (i > 0)
    5223        40631 :     return CMP_GT;
    5224              :   return CMP_EQ;
    5225              : }
    5226              : 
    5227              : 
    5228              : /* Compare an integer expression with an integer.  */
    5229              : 
    5230              : static compare_result
    5231        76674 : compare_bound_int (gfc_expr *a, int b)
    5232              : {
    5233        76674 :   int i;
    5234              : 
    5235        76674 :   if (a == NULL
    5236        33248 :       || a->expr_type != EXPR_CONSTANT
    5237        30193 :       || a->ts.type != BT_INTEGER)
    5238              :     return CMP_UNKNOWN;
    5239              : 
    5240        30193 :   i = mpz_cmp_si (a->value.integer, b);
    5241              : 
    5242        30193 :   if (i < 0)
    5243              :     return CMP_LT;
    5244        25701 :   if (i > 0)
    5245        22075 :     return CMP_GT;
    5246              :   return CMP_EQ;
    5247              : }
    5248              : 
    5249              : 
    5250              : /* Compare an integer expression with a mpz_t.  */
    5251              : 
    5252              : static compare_result
    5253        71376 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5254              : {
    5255        71376 :   int i;
    5256              : 
    5257        71376 :   if (a == NULL
    5258        58251 :       || a->expr_type != EXPR_CONSTANT
    5259        55991 :       || a->ts.type != BT_INTEGER)
    5260              :     return CMP_UNKNOWN;
    5261              : 
    5262        55988 :   i = mpz_cmp (a->value.integer, b);
    5263              : 
    5264        55988 :   if (i < 0)
    5265              :     return CMP_LT;
    5266        25456 :   if (i > 0)
    5267        10820 :     return CMP_GT;
    5268              :   return CMP_EQ;
    5269              : }
    5270              : 
    5271              : 
    5272              : /* Compute the last value of a sequence given by a triplet.
    5273              :    Return 0 if it wasn't able to compute the last value, or if the
    5274              :    sequence if empty, and 1 otherwise.  */
    5275              : 
    5276              : static int
    5277        53055 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5278              :                                 gfc_expr *stride, mpz_t last)
    5279              : {
    5280        53055 :   mpz_t rem;
    5281              : 
    5282        53055 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5283        37744 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5284        32953 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5285              :     return 0;
    5286              : 
    5287        32634 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5288        32633 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    5289              :     return 0;
    5290              : 
    5291         6811 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    5292              :     {
    5293        25948 :       if (compare_bound (start, end) == CMP_GT)
    5294              :         return 0;
    5295        24559 :       mpz_set (last, end->value.integer);
    5296        24559 :       return 1;
    5297              :     }
    5298              : 
    5299         6685 :   if (compare_bound_int (stride, 0) == CMP_GT)
    5300              :     {
    5301              :       /* Stride is positive */
    5302         5314 :       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
    5303              :         return 0;
    5304              :     }
    5305              :   else
    5306              :     {
    5307              :       /* Stride is negative */
    5308         1371 :       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
    5309              :         return 0;
    5310              :     }
    5311              : 
    5312         6665 :   mpz_init (rem);
    5313         6665 :   mpz_sub (rem, end->value.integer, start->value.integer);
    5314         6665 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    5315         6665 :   mpz_sub (last, end->value.integer, rem);
    5316         6665 :   mpz_clear (rem);
    5317              : 
    5318         6665 :   return 1;
    5319              : }
    5320              : 
    5321              : 
    5322              : /* Compare a single dimension of an array reference to the array
    5323              :    specification.  */
    5324              : 
    5325              : static bool
    5326       223785 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5327              : {
    5328       223785 :   mpz_t last_value;
    5329              : 
    5330       223785 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5331              :     {
    5332          598 :       gcc_assert (ar->stride[i] == NULL);
    5333              :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5334          598 :       if (ar->start[i] == NULL)
    5335              :         {
    5336          497 :           gcc_assert (ar->end[i] == NULL);
    5337              :           return true;
    5338              :         }
    5339              :     }
    5340              : 
    5341              : /* Given start, end and stride values, calculate the minimum and
    5342              :    maximum referenced indexes.  */
    5343              : 
    5344       223288 :   switch (ar->dimen_type[i])
    5345              :     {
    5346              :     case DIMEN_VECTOR:
    5347              :     case DIMEN_THIS_IMAGE:
    5348              :       break;
    5349              : 
    5350       161581 :     case DIMEN_STAR:
    5351       161581 :     case DIMEN_ELEMENT:
    5352       161581 :       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
    5353              :         {
    5354            2 :           if (i < as->rank)
    5355            2 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5356              :                          "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5357            2 :                          mpz_get_si (ar->start[i]->value.integer),
    5358            2 :                          mpz_get_si (as->lower[i]->value.integer), i+1);
    5359              :           else
    5360            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5361              :                          "(%ld < %ld) in codimension %d", &ar->c_where[i],
    5362            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5363            0 :                          mpz_get_si (as->lower[i]->value.integer),
    5364            0 :                          i + 1 - as->rank);
    5365              :           return true;
    5366              :         }
    5367       161579 :       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
    5368              :         {
    5369           39 :           if (i < as->rank)
    5370           39 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5371              :                          "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5372           39 :                          mpz_get_si (ar->start[i]->value.integer),
    5373           39 :                          mpz_get_si (as->upper[i]->value.integer), i+1);
    5374              :           else
    5375            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5376              :                          "(%ld > %ld) in codimension %d", &ar->c_where[i],
    5377            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5378            0 :                          mpz_get_si (as->upper[i]->value.integer),
    5379            0 :                          i + 1 - as->rank);
    5380              :           return true;
    5381              :         }
    5382              : 
    5383              :       break;
    5384              : 
    5385        53100 :     case DIMEN_RANGE:
    5386        53100 :       {
    5387              : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
    5388              : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
    5389              : 
    5390        53100 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5391        53100 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5392              : 
    5393              :         /* Check for zero stride, which is not allowed.  */
    5394        53100 :         if (comp_stride_zero == CMP_EQ)
    5395              :           {
    5396            1 :             gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
    5397            1 :             return false;
    5398              :           }
    5399              : 
    5400              :         /* if start == end || (stride > 0 && start < end)
    5401              :                            || (stride < 0 && start > end),
    5402              :            then the array section contains at least one element.  In this
    5403              :            case, there is an out-of-bounds access if
    5404              :            (start < lower || start > upper).  */
    5405        53099 :         if (comp_start_end == CMP_EQ
    5406        52332 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5407        49537 :                 && comp_start_end == CMP_LT)
    5408        23180 :             || (comp_stride_zero == CMP_LT
    5409        23180 :                 && comp_start_end == CMP_GT))
    5410              :           {
    5411        31270 :             if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
    5412              :               {
    5413           27 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5414              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5415           27 :                        mpz_get_si (AR_START->value.integer),
    5416           27 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5417           27 :                 return true;
    5418              :               }
    5419        31243 :             if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
    5420              :               {
    5421           17 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5422              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5423           17 :                        mpz_get_si (AR_START->value.integer),
    5424           17 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5425           17 :                 return true;
    5426              :               }
    5427              :           }
    5428              : 
    5429              :         /* If we can compute the highest index of the array section,
    5430              :            then it also has to be between lower and upper.  */
    5431        53055 :         mpz_init (last_value);
    5432        53055 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5433              :                                             last_value))
    5434              :           {
    5435        31224 :             if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
    5436              :               {
    5437            3 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5438              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5439              :                        mpz_get_si (last_value),
    5440            3 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5441            3 :                 mpz_clear (last_value);
    5442            3 :                 return true;
    5443              :               }
    5444        31221 :             if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
    5445              :               {
    5446            7 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5447              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5448              :                        mpz_get_si (last_value),
    5449            7 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5450            7 :                 mpz_clear (last_value);
    5451            7 :                 return true;
    5452              :               }
    5453              :           }
    5454        53045 :         mpz_clear (last_value);
    5455              : 
    5456              : #undef AR_START
    5457              : #undef AR_END
    5458              :       }
    5459        53045 :       break;
    5460              : 
    5461            0 :     default:
    5462            0 :       gfc_internal_error ("check_dimension(): Bad array reference");
    5463              :     }
    5464              : 
    5465              :   return true;
    5466              : }
    5467              : 
    5468              : 
    5469              : /* Compare an array reference with an array specification.  */
    5470              : 
    5471              : static bool
    5472       441435 : compare_spec_to_ref (gfc_array_ref *ar)
    5473              : {
    5474       441435 :   gfc_array_spec *as;
    5475       441435 :   int i;
    5476              : 
    5477       441435 :   as = ar->as;
    5478       441435 :   i = as->rank - 1;
    5479              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5480       441435 :   if (as->type == AS_ASSUMED_SIZE
    5481         5836 :       && (/*ar->type == AR_FULL
    5482         5836 :           ||*/ (ar->type == AR_SECTION
    5483          529 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5484              :     {
    5485            5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5486              :                  "not specified at %L", &ar->where);
    5487            5 :       return false;
    5488              :     }
    5489              : 
    5490       441430 :   if (ar->type == AR_FULL)
    5491              :     return true;
    5492              : 
    5493       170253 :   if (as->rank != ar->dimen)
    5494              :     {
    5495           28 :       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
    5496              :                  &ar->where, ar->dimen, as->rank);
    5497           28 :       return false;
    5498              :     }
    5499              : 
    5500              :   /* ar->codimen == 0 is a local array.  */
    5501       170225 :   if (as->corank != ar->codimen && ar->codimen != 0)
    5502              :     {
    5503            0 :       gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
    5504              :                  &ar->where, ar->codimen, as->corank);
    5505            0 :       return false;
    5506              :     }
    5507              : 
    5508       383775 :   for (i = 0; i < as->rank; i++)
    5509       213551 :     if (!check_dimension (i, ar, as))
    5510              :       return false;
    5511              : 
    5512              :   /* Local access has no coarray spec.  */
    5513       170224 :   if (ar->codimen != 0)
    5514        19638 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5515              :       {
    5516        10236 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5517         7122 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5518              :           {
    5519            2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5520            2 :                        i + 1 - as->rank, &ar->where);
    5521            2 :             return false;
    5522              :           }
    5523        10234 :         if (!check_dimension (i, ar, as))
    5524              :           return false;
    5525              :       }
    5526              : 
    5527              :   return true;
    5528              : }
    5529              : 
    5530              : 
    5531              : /* Resolve one part of an array index.  */
    5532              : 
    5533              : static bool
    5534       758578 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5535              :                      int force_index_integer_kind)
    5536              : {
    5537       758578 :   gfc_typespec ts;
    5538              : 
    5539       758578 :   if (index == NULL)
    5540              :     return true;
    5541              : 
    5542       225015 :   if (!gfc_resolve_expr (index))
    5543              :     return false;
    5544              : 
    5545       225004 :   if (check_scalar && index->rank != 0)
    5546              :     {
    5547            2 :       gfc_error ("Array index at %L must be scalar", &index->where);
    5548            2 :       return false;
    5549              :     }
    5550              : 
    5551       225002 :   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
    5552              :     {
    5553            4 :       gfc_error ("Array index at %L must be of INTEGER type, found %s",
    5554              :                  &index->where, gfc_basic_typename (index->ts.type));
    5555            4 :       return false;
    5556              :     }
    5557              : 
    5558       224998 :   if (index->ts.type == BT_REAL)
    5559          657 :     if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
    5560              :                          &index->where))
    5561              :       return false;
    5562              : 
    5563       224998 :   if ((index->ts.kind != gfc_index_integer_kind
    5564       219882 :        && force_index_integer_kind)
    5565       193094 :       || (index->ts.type != BT_INTEGER
    5566              :           && index->ts.type != BT_UNKNOWN))
    5567              :     {
    5568        32560 :       gfc_clear_ts (&ts);
    5569        32560 :       ts.type = BT_INTEGER;
    5570        32560 :       ts.kind = gfc_index_integer_kind;
    5571              : 
    5572        32560 :       gfc_convert_type_warn (index, &ts, 2, 0);
    5573              :     }
    5574              : 
    5575              :   return true;
    5576              : }
    5577              : 
    5578              : /* Resolve one part of an array index.  */
    5579              : 
    5580              : bool
    5581       505981 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5582              : {
    5583       505981 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5584              : }
    5585              : 
    5586              : /* Resolve a dim argument to an intrinsic function.  */
    5587              : 
    5588              : bool
    5589        23941 : gfc_resolve_dim_arg (gfc_expr *dim)
    5590              : {
    5591        23941 :   if (dim == NULL)
    5592              :     return true;
    5593              : 
    5594        23941 :   if (!gfc_resolve_expr (dim))
    5595              :     return false;
    5596              : 
    5597        23941 :   if (dim->rank != 0)
    5598              :     {
    5599            0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5600            0 :       return false;
    5601              : 
    5602              :     }
    5603              : 
    5604        23941 :   if (dim->ts.type != BT_INTEGER)
    5605              :     {
    5606            0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5607            0 :       return false;
    5608              :     }
    5609              : 
    5610        23941 :   if (dim->ts.kind != gfc_index_integer_kind)
    5611              :     {
    5612        15322 :       gfc_typespec ts;
    5613              : 
    5614        15322 :       gfc_clear_ts (&ts);
    5615        15322 :       ts.type = BT_INTEGER;
    5616        15322 :       ts.kind = gfc_index_integer_kind;
    5617              : 
    5618        15322 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5619              :     }
    5620              : 
    5621              :   return true;
    5622              : }
    5623              : 
    5624              : /* Given an expression that contains array references, update those array
    5625              :    references to point to the right array specifications.  While this is
    5626              :    filled in during matching, this information is difficult to save and load
    5627              :    in a module, so we take care of it here.
    5628              : 
    5629              :    The idea here is that the original array reference comes from the
    5630              :    base symbol.  We traverse the list of reference structures, setting
    5631              :    the stored reference to references.  Component references can
    5632              :    provide an additional array specification.  */
    5633              : static void
    5634              : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5635              : 
    5636              : static bool
    5637          967 : find_array_spec (gfc_expr *e)
    5638              : {
    5639          967 :   gfc_array_spec *as;
    5640          967 :   gfc_component *c;
    5641          967 :   gfc_ref *ref;
    5642          967 :   bool class_as = false;
    5643              : 
    5644          967 :   if (e->symtree->n.sym->assoc)
    5645              :     {
    5646          270 :       if (e->symtree->n.sym->assoc->target)
    5647          270 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5648          270 :       resolve_assoc_var (e->symtree->n.sym, false);
    5649              :     }
    5650              : 
    5651          967 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5652              :     {
    5653          130 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5654          130 :       class_as = true;
    5655              :     }
    5656              :   else
    5657          837 :     as = e->symtree->n.sym->as;
    5658              : 
    5659         2305 :   for (ref = e->ref; ref; ref = ref->next)
    5660         1346 :     switch (ref->type)
    5661              :       {
    5662         1005 :       case REF_ARRAY:
    5663         1005 :         if (as == NULL)
    5664              :           {
    5665            8 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5666           16 :                          ? ref->u.ar.where : e->where);
    5667            8 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5668              :                        &loc);
    5669            8 :             return false;
    5670              :           }
    5671              : 
    5672          997 :         ref->u.ar.as = as;
    5673          997 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5674              :         as = NULL;
    5675              :         break;
    5676              : 
    5677          317 :       case REF_COMPONENT:
    5678          317 :         c = ref->u.c.component;
    5679          317 :         if (c->attr.dimension)
    5680              :           {
    5681          107 :             if (as != NULL && !(class_as && as == c->as))
    5682            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5683          107 :             as = c->as;
    5684              :           }
    5685          210 :         else if (c->ts.type == BT_CLASS && CLASS_DATA (c)
    5686           54 :                  && CLASS_DATA (c)->attr.dimension
    5687           36 :                  && ref->next && ref->next->type == REF_ARRAY)
    5688              :           {
    5689              :             /* CLASS array component with an implicit '_data' reference.  */
    5690           36 :             if (as != NULL)
    5691            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5692           36 :             as = CLASS_DATA (c)->as;
    5693              :           }
    5694              : 
    5695              :         break;
    5696              : 
    5697              :       case REF_SUBSTRING:
    5698              :       case REF_INQUIRY:
    5699              :         break;
    5700              :       }
    5701              : 
    5702          959 :   if (as != NULL)
    5703            0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5704              : 
    5705              :   return true;
    5706              : }
    5707              : 
    5708              : 
    5709              : /* Resolve an array reference.  */
    5710              : 
    5711              : static bool
    5712       442149 : resolve_array_ref (gfc_array_ref *ar)
    5713              : {
    5714       442149 :   int i, check_scalar;
    5715       442149 :   gfc_expr *e;
    5716              : 
    5717       694729 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5718              :     {
    5719       252597 :       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
    5720              : 
    5721              :       /* Do not force gfc_index_integer_kind for the start.  We can
    5722              :          do fine with any integer kind.  This avoids temporary arrays
    5723              :          created for indexing with a vector.  */
    5724       252597 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5725              :         return false;
    5726       252582 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5727              :         return false;
    5728       252580 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5729              :         return false;
    5730              : 
    5731       252580 :       e = ar->start[i];
    5732              : 
    5733       252580 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5734       151412 :         switch (e->rank)
    5735              :           {
    5736       150253 :           case 0:
    5737       150253 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5738       150253 :             break;
    5739              : 
    5740         1159 :           case 1:
    5741         1159 :             ar->dimen_type[i] = DIMEN_VECTOR;
    5742         1159 :             if (e->expr_type == EXPR_VARIABLE
    5743          482 :                 && e->symtree->n.sym->ts.type == BT_DERIVED)
    5744           13 :               ar->start[i] = gfc_get_parentheses (e);
    5745              :             break;
    5746              : 
    5747            0 :           default:
    5748            0 :             gfc_error ("Array index at %L is an array of rank %d",
    5749              :                        &ar->c_where[i], e->rank);
    5750            0 :             return false;
    5751              :           }
    5752              : 
    5753              :       /* Fill in the upper bound, which may be lower than the
    5754              :          specified one for something like a(2:10:5), which is
    5755              :          identical to a(2:7:5).  Only relevant for strides not equal
    5756              :          to one.  Don't try a division by zero.  */
    5757       252580 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5758        73178 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5759         8580 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5760         8433 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5761              :         {
    5762         8432 :           mpz_t size, end;
    5763              : 
    5764         8432 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5765              :             {
    5766         6695 :               if (ar->end[i] == NULL)
    5767              :                 {
    5768         8062 :                   ar->end[i] =
    5769         4031 :                     gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
    5770              :                                            &ar->where);
    5771         4031 :                   mpz_set (ar->end[i]->value.integer, end);
    5772              :                 }
    5773         2664 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5774         2664 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5775              :                 {
    5776         2664 :                   mpz_set (ar->end[i]->value.integer, end);
    5777              :                 }
    5778              :               else
    5779            0 :                 gcc_unreachable ();
    5780              : 
    5781         6695 :               mpz_clear (size);
    5782         6695 :               mpz_clear (end);
    5783              :             }
    5784              :         }
    5785              :     }
    5786              : 
    5787       442132 :   if (ar->type == AR_FULL)
    5788              :     {
    5789       275134 :       if (ar->as->rank == 0)
    5790         3923 :         ar->type = AR_ELEMENT;
    5791              : 
    5792              :       /* Make sure array is the same as array(:,:), this way
    5793              :          we don't need to special case all the time.  */
    5794       275134 :       ar->dimen = ar->as->rank;
    5795       652935 :       for (i = 0; i < ar->dimen; i++)
    5796              :         {
    5797       377801 :           ar->dimen_type[i] = DIMEN_RANGE;
    5798              : 
    5799       377801 :           gcc_assert (ar->start[i] == NULL);
    5800       377801 :           gcc_assert (ar->end[i] == NULL);
    5801       377801 :           gcc_assert (ar->stride[i] == NULL);
    5802              :         }
    5803              :     }
    5804              : 
    5805              :   /* If the reference type is unknown, figure out what kind it is.  */
    5806              : 
    5807       442132 :   if (ar->type == AR_UNKNOWN)
    5808              :     {
    5809       153484 :       ar->type = AR_ELEMENT;
    5810       297891 :       for (i = 0; i < ar->dimen; i++)
    5811       183237 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5812       183237 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5813              :           {
    5814        38830 :             ar->type = AR_SECTION;
    5815        38830 :             break;
    5816              :           }
    5817              :     }
    5818              : 
    5819       442132 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5820              :     return false;
    5821              : 
    5822       442096 :   if (ar->as->corank && ar->codimen == 0)
    5823              :     {
    5824         2395 :       int n;
    5825         2395 :       ar->codimen = ar->as->corank;
    5826         6556 :       for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
    5827         4161 :         ar->dimen_type[n] = DIMEN_THIS_IMAGE;
    5828              :     }
    5829              : 
    5830       442096 :   if (ar->codimen)
    5831              :     {
    5832        14400 :       if (ar->team_type == TEAM_NUMBER)
    5833              :         {
    5834           60 :           if (!gfc_resolve_expr (ar->team))
    5835              :             return false;
    5836              : 
    5837           60 :           if (ar->team->rank != 0)
    5838              :             {
    5839            0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5840              :                          &ar->team->where);
    5841            0 :               return false;
    5842              :             }
    5843              : 
    5844           60 :           if (ar->team->ts.type != BT_INTEGER)
    5845              :             {
    5846            6 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5847              :                          "type, found %s",
    5848            6 :                          &ar->team->where,
    5849              :                          gfc_basic_typename (ar->team->ts.type));
    5850            6 :               return false;
    5851              :             }
    5852              :         }
    5853        14340 :       else if (ar->team_type == TEAM_TEAM)
    5854              :         {
    5855           42 :           if (!gfc_resolve_expr (ar->team))
    5856              :             return false;
    5857              : 
    5858           42 :           if (ar->team->rank != 0)
    5859              :             {
    5860            3 :               gfc_error ("TEAM argument at %L must be scalar",
    5861              :                          &ar->team->where);
    5862            3 :               return false;
    5863              :             }
    5864              : 
    5865           39 :           if (ar->team->ts.type != BT_DERIVED
    5866           36 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5867           36 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5868              :             {
    5869            3 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5870              :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5871            3 :                          &ar->team->where,
    5872              :                          gfc_basic_typename (ar->team->ts.type));
    5873            3 :               return false;
    5874              :             }
    5875              :         }
    5876        14388 :       if (ar->stat)
    5877              :         {
    5878           62 :           if (!gfc_resolve_expr (ar->stat))
    5879              :             return false;
    5880              : 
    5881           62 :           if (ar->stat->rank != 0)
    5882              :             {
    5883            3 :               gfc_error ("STAT argument at %L must be scalar",
    5884              :                          &ar->stat->where);
    5885            3 :               return false;
    5886              :             }
    5887              : 
    5888           59 :           if (ar->stat->ts.type != BT_INTEGER)
    5889              :             {
    5890            3 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5891              :                          "type, found %s",
    5892            3 :                          &ar->stat->where,
    5893              :                          gfc_basic_typename (ar->stat->ts.type));
    5894            3 :               return false;
    5895              :             }
    5896              : 
    5897           56 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5898              :             {
    5899            0 :               gfc_error ("STAT's expression at %L must be a variable",
    5900              :                          &ar->stat->where);
    5901            0 :               return false;
    5902              :             }
    5903              :         }
    5904              :     }
    5905              :   return true;
    5906              : }
    5907              : 
    5908              : 
    5909              : bool
    5910         9132 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5911              : {
    5912         9132 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5913              : 
    5914         9132 :   if (ref->u.ss.start != NULL)
    5915              :     {
    5916         9132 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5917              :         return false;
    5918              : 
    5919         9132 :       if (ref->u.ss.start->ts.type != BT_INTEGER)
    5920              :         {
    5921            1 :           gfc_error ("Substring start index at %L must be of type INTEGER",
    5922              :                      &ref->u.ss.start->where);
    5923            1 :           return false;
    5924              :         }
    5925              : 
    5926         9131 :       if (ref->u.ss.start->rank != 0)
    5927              :         {
    5928            0 :           gfc_error ("Substring start index at %L must be scalar",
    5929              :                      &ref->u.ss.start->where);
    5930            0 :           return false;
    5931              :         }
    5932              : 
    5933         9131 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5934         9131 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5935           37 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5936              :         {
    5937            1 :           gfc_error ("Substring start index at %L is less than one",
    5938              :                      &ref->u.ss.start->where);
    5939            1 :           return false;
    5940              :         }
    5941              :     }
    5942              : 
    5943         9130 :   if (ref->u.ss.end != NULL)
    5944              :     {
    5945         8936 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5946              :         return false;
    5947              : 
    5948         8936 :       if (ref->u.ss.end->ts.type != BT_INTEGER)
    5949              :         {
    5950            1 :           gfc_error ("Substring end index at %L must be of type INTEGER",
    5951              :                      &ref->u.ss.end->where);
    5952            1 :           return false;
    5953              :         }
    5954              : 
    5955         8935 :       if (ref->u.ss.end->rank != 0)
    5956              :         {
    5957            0 :           gfc_error ("Substring end index at %L must be scalar",
    5958              :                      &ref->u.ss.end->where);
    5959            0 :           return false;
    5960              :         }
    5961              : 
    5962         8935 :       if (ref->u.ss.length != NULL
    5963         8598 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5964         8947 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5965           12 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5966              :         {
    5967            4 :           gfc_error ("Substring end index at %L exceeds the string length",
    5968              :                      &ref->u.ss.start->where);
    5969            4 :           return false;
    5970              :         }
    5971              : 
    5972         8931 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5973         8931 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5974         8931 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5975            7 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5976              :         {
    5977            4 :           gfc_error ("Substring end index at %L is too large",
    5978              :                      &ref->u.ss.end->where);
    5979            4 :           return false;
    5980              :         }
    5981              :       /*  If the substring has the same length as the original
    5982              :           variable, the reference itself can be deleted.  */
    5983              : 
    5984         8927 :       if (ref->u.ss.length != NULL
    5985         8590 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5986         9874 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5987          242 :         *equal_length = true;
    5988              :     }
    5989              : 
    5990              :   return true;
    5991              : }
    5992              : 
    5993              : 
    5994              : /* This function supplies missing substring charlens.  */
    5995              : 
    5996              : void
    5997        10018 : gfc_resolve_substring_charlen (gfc_expr *e)
    5998              : {
    5999        10018 :   gfc_ref *char_ref;
    6000        10018 :   gfc_expr *start, *end;
    6001        10018 :   gfc_typespec *ts = NULL;
    6002        10018 :   mpz_t diff;
    6003              : 
    6004        14445 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    6005              :     {
    6006        12580 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    6007              :         break;
    6008         4427 :       if (char_ref->type == REF_COMPONENT)
    6009          364 :         ts = &char_ref->u.c.component->ts;
    6010              :     }
    6011              : 
    6012        10018 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    6013         2037 :     return;
    6014              : 
    6015         8153 :   gcc_assert (char_ref->next == NULL);
    6016              : 
    6017         8153 :   if (e->ts.u.cl)
    6018              :     {
    6019          120 :       if (e->ts.u.cl->length)
    6020          120 :         gfc_free_expr (e->ts.u.cl->length);
    6021            0 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    6022              :         return;
    6023              :     }
    6024              : 
    6025         8153 :   if (!e->ts.u.cl)
    6026         8033 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6027              : 
    6028         8153 :   if (char_ref->u.ss.start)
    6029         8153 :     start = gfc_copy_expr (char_ref->u.ss.start);
    6030              :   else
    6031            0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    6032              : 
    6033         8153 :   if (char_ref->u.ss.end)
    6034         7981 :     end = gfc_copy_expr (char_ref->u.ss.end);
    6035          172 :   else if (e->expr_type == EXPR_VARIABLE)
    6036              :     {
    6037          172 :       if (!ts)
    6038          154 :         ts = &e->symtree->n.sym->ts;
    6039          172 :       end = gfc_copy_expr (ts->u.cl->length);
    6040              :     }
    6041              :   else
    6042              :     end = NULL;
    6043              : 
    6044         8153 :   if (!start || !end)
    6045              :     {
    6046          172 :       gfc_free_expr (start);
    6047          172 :       gfc_free_expr (end);
    6048          172 :       return;
    6049              :     }
    6050              : 
    6051              :   /* Length = (end - start + 1).
    6052              :      Check first whether it has a constant length.  */
    6053         7981 :   if (gfc_dep_difference (end, start, &diff))
    6054              :     {
    6055         7331 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    6056              :                                              &e->where);
    6057              : 
    6058         7331 :       mpz_add_ui (len->value.integer, diff, 1);
    6059         7331 :       mpz_clear (diff);
    6060         7331 :       e->ts.u.cl->length = len;
    6061              :       /* The check for length < 0 is handled below */
    6062              :     }
    6063              :   else
    6064              :     {
    6065          650 :       e->ts.u.cl->length = gfc_subtract (end, start);
    6066          650 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    6067              :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    6068              :                                                       NULL, 1));
    6069              :     }
    6070              : 
    6071              :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    6072              :      be within the range 1, 2, ..., n unless the starting point exceeds
    6073              :      the ending point, in which case the substring has length zero.  */
    6074              : 
    6075         7981 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    6076           52 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    6077              : 
    6078         7981 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6079         7981 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6080              : 
    6081              :   /* Make sure that the length is simplified.  */
    6082         7981 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    6083         7981 :   gfc_resolve_expr (e->ts.u.cl->length);
    6084              : }
    6085              : 
    6086              : 
    6087              : /* Convert an array reference to an array element so that PDT KIND and LEN
    6088              :    or inquiry references are always scalar.  */
    6089              : 
    6090              : static void
    6091           81 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
    6092              : {
    6093           81 :   gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    6094           81 :   int dim;
    6095              : 
    6096           81 :   array_ref->u.ar.type = AR_ELEMENT;
    6097           81 :   expr->rank = 0;
    6098              :   /* Suppress the runtime bounds check.  */
    6099           81 :   expr->no_bounds_check = 1;
    6100          162 :   for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    6101              :     {
    6102           81 :       array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    6103           81 :       if (array_ref->u.ar.start[dim])
    6104           18 :         gfc_free_expr (array_ref->u.ar.start[dim]);
    6105              : 
    6106           81 :       if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
    6107           39 :         array_ref->u.ar.start[dim]
    6108           39 :                         = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
    6109              :       else
    6110           42 :         array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
    6111              : 
    6112           81 :       gfc_free_expr (array_ref->u.ar.end[dim]);
    6113           81 :       array_ref->u.ar.end[dim] = NULL;
    6114           81 :       gfc_free_expr (array_ref->u.ar.stride[dim]);
    6115           81 :       array_ref->u.ar.stride[dim] = NULL;
    6116              :     }
    6117           81 :   gfc_free_expr (unity);
    6118           81 : }
    6119              : 
    6120              : 
    6121              : /* Resolve subtype references.  */
    6122              : 
    6123              : bool
    6124       566108 : gfc_resolve_ref (gfc_expr *expr)
    6125              : {
    6126       566108 :   int current_part_dimension, n_components, seen_part_dimension;
    6127       566108 :   gfc_ref *ref, **prev, *array_ref;
    6128       566108 :   bool equal_length;
    6129       566108 :   gfc_symbol *last_pdt = NULL;
    6130              : 
    6131      1114365 :   for (ref = expr->ref; ref; ref = ref->next)
    6132       549224 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    6133              :       {
    6134          967 :         if (!find_array_spec (expr))
    6135              :           return false;
    6136              :         break;
    6137              :       }
    6138              : 
    6139      1664687 :   for (prev = &expr->ref; *prev != NULL;
    6140       549402 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6141       549481 :     switch ((*prev)->type)
    6142              :       {
    6143       442149 :       case REF_ARRAY:
    6144       442149 :         if (!resolve_array_ref (&(*prev)->u.ar))
    6145              :             return false;
    6146              :         break;
    6147              : 
    6148              :       case REF_COMPONENT:
    6149              :       case REF_INQUIRY:
    6150              :         break;
    6151              : 
    6152         8851 :       case REF_SUBSTRING:
    6153         8851 :         equal_length = false;
    6154         8851 :         if (!gfc_resolve_substring (*prev, &equal_length))
    6155              :             return false;
    6156              : 
    6157         8843 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    6158              :           {
    6159              :             /* Remove the reference and move the charlen, if any.  */
    6160          217 :             ref = *prev;
    6161          217 :             *prev = ref->next;
    6162          217 :             ref->next = NULL;
    6163          217 :             expr->ts.u.cl = ref->u.ss.length;
    6164          217 :             ref->u.ss.length = NULL;
    6165          217 :             gfc_free_ref_list (ref);
    6166              :           }
    6167              :         break;
    6168              :       }
    6169              : 
    6170              :   /* Check constraints on part references.  */
    6171              : 
    6172       566021 :   current_part_dimension = 0;
    6173       566021 :   seen_part_dimension = 0;
    6174       566021 :   n_components = 0;
    6175       566021 :   array_ref = NULL;
    6176              : 
    6177              :   /* Use the declared type of the base symbol to initialize last_pdt when the
    6178              :      expression is not itself a PDT. This matters for ASSOCIATE variables whose
    6179              :      component reference may still point to a PDT template.  */
    6180       566021 :   if (expr->expr_type == EXPR_VARIABLE
    6181       468078 :       && (IS_PDT (expr)
    6182       467244 :           || (expr->ref && expr->symtree && IS_PDT (expr->symtree->n.sym))))
    6183         5099 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6184              : 
    6185      1115181 :   for (ref = expr->ref; ref; ref = ref->next)
    6186              :     {
    6187       549171 :       switch (ref->type)
    6188              :         {
    6189       442071 :         case REF_ARRAY:
    6190       442071 :           array_ref = ref;
    6191       442071 :           switch (ref->u.ar.type)
    6192              :             {
    6193       271209 :             case AR_FULL:
    6194              :               /* Coarray scalar.  */
    6195       271209 :               if (ref->u.ar.as->rank == 0)
    6196              :                 {
    6197              :                   current_part_dimension = 0;
    6198              :                   break;
    6199              :                 }
    6200              :               /* Fall through.  */
    6201       313206 :             case AR_SECTION:
    6202       313206 :               current_part_dimension = 1;
    6203       313206 :               break;
    6204              : 
    6205       128865 :             case AR_ELEMENT:
    6206       128865 :               array_ref = NULL;
    6207       128865 :               current_part_dimension = 0;
    6208       128865 :               break;
    6209              : 
    6210            0 :             case AR_UNKNOWN:
    6211            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6212              :             }
    6213              : 
    6214              :           break;
    6215              : 
    6216        97515 :         case REF_COMPONENT:
    6217        97515 :           if (current_part_dimension || seen_part_dimension)
    6218              :             {
    6219              :               /* F03:C614.  */
    6220         7800 :               if (ref->u.c.component->attr.pointer
    6221         7797 :                   || ref->u.c.component->attr.proc_pointer
    6222         7796 :                   || (ref->u.c.component->ts.type == BT_CLASS
    6223            1 :                         && CLASS_DATA (ref->u.c.component)->attr.pointer))
    6224              :                 {
    6225            4 :                   gfc_error ("Component to the right of a part reference "
    6226              :                              "with nonzero rank must not have the POINTER "
    6227              :                              "attribute at %L", &expr->where);
    6228            4 :                   return false;
    6229              :                 }
    6230         7796 :               else if (ref->u.c.component->attr.allocatable
    6231         7790 :                         || (ref->u.c.component->ts.type == BT_CLASS
    6232            1 :                             && CLASS_DATA (ref->u.c.component)->attr.allocatable))
    6233              : 
    6234              :                 {
    6235            7 :                   gfc_error ("Component to the right of a part reference "
    6236              :                              "with nonzero rank must not have the ALLOCATABLE "
    6237              :                              "attribute at %L", &expr->where);
    6238            7 :                   return false;
    6239              :                 }
    6240              :             }
    6241              : 
    6242              :           /* Sometimes the component in a component reference is that of the
    6243              :              pdt_template. Point to the component of pdt_type instead. This
    6244              :              ensures that the component gets a backend_decl in translation.  */
    6245        97504 :           if (last_pdt)
    6246              :             {
    6247         5006 :               gfc_component *cmp = last_pdt->components;
    6248        14079 :               for (; cmp; cmp = cmp->next)
    6249        13714 :                 if (!strcmp (cmp->name, ref->u.c.component->name))
    6250              :                   {
    6251         4641 :                     ref->u.c.component = cmp;
    6252         4641 :                     break;
    6253              :                   }
    6254         5006 :               ref->u.c.sym = last_pdt;
    6255              :             }
    6256              : 
    6257              :           /* Convert pdt_templates, if necessary, and update 'last_pdt'.  */
    6258        97504 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6259              :             {
    6260        22430 :               if (ref->u.c.component->ts.u.derived->attr.pdt_template)
    6261              :                 {
    6262            0 :                   if (gfc_get_pdt_instance (ref->u.c.component->param_list,
    6263              :                                             &ref->u.c.component->ts.u.derived,
    6264              :                                             NULL) != MATCH_YES)
    6265              :                     return false;
    6266            0 :                   last_pdt = ref->u.c.component->ts.u.derived;
    6267              :                 }
    6268        22430 :               else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
    6269          815 :                 last_pdt = ref->u.c.component->ts.u.derived;
    6270              :               else
    6271              :                 last_pdt = NULL;
    6272              :             }
    6273              : 
    6274              :           /* The F08 standard requires(See R425, R431, R435, and in particular
    6275              :              Note 6.7) that a PDT parameter reference be a scalar even if
    6276              :              the designator is an array."  */
    6277        97504 :           if (array_ref && last_pdt && last_pdt->attr.pdt_type
    6278          257 :               && (ref->u.c.component->attr.pdt_kind
    6279          257 :                   || ref->u.c.component->attr.pdt_len))
    6280           55 :             reset_array_ref_to_scalar (expr, array_ref);
    6281              : 
    6282        97504 :           n_components++;
    6283        97504 :           break;
    6284              : 
    6285              :         case REF_SUBSTRING:
    6286              :           break;
    6287              : 
    6288          959 :         case REF_INQUIRY:
    6289              :           /* Implement requirement in note 9.7 of F2018 that the result of the
    6290              :              LEN inquiry be a scalar.  */
    6291          959 :           if (ref->u.i == INQUIRY_LEN && array_ref
    6292           52 :               && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
    6293           52 :                   || expr->ts.type == BT_INTEGER))
    6294           26 :             reset_array_ref_to_scalar (expr, array_ref);
    6295              :           break;
    6296              :         }
    6297              : 
    6298       549160 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6299       534691 :            || ref->next == NULL)
    6300              :           && current_part_dimension
    6301       478067 :           && seen_part_dimension)
    6302              :         {
    6303            0 :           gfc_error ("Two or more part references with nonzero rank must "
    6304              :                      "not be specified at %L", &expr->where);
    6305            0 :           return false;
    6306              :         }
    6307              : 
    6308       549160 :       if (ref->type == REF_COMPONENT)
    6309              :         {
    6310        97504 :           if (current_part_dimension)
    6311         7578 :             seen_part_dimension = 1;
    6312              : 
    6313              :           /* reset to make sure */
    6314              :           current_part_dimension = 0;
    6315              :         }
    6316              :     }
    6317              : 
    6318              :   return true;
    6319              : }
    6320              : 
    6321              : 
    6322              : /* Given an expression, determine its shape.  This is easier than it sounds.
    6323              :    Leaves the shape array NULL if it is not possible to determine the shape.  */
    6324              : 
    6325              : static void
    6326      2677055 : expression_shape (gfc_expr *e)
    6327              : {
    6328      2677055 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6329      2677055 :   int i;
    6330              : 
    6331      2677055 :   if (e->rank <= 0 || e->shape != NULL)
    6332      2492906 :     return;
    6333              : 
    6334       731190 :   for (i = 0; i < e->rank; i++)
    6335       493715 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6336       184149 :       goto fail;
    6337              : 
    6338       237475 :   e->shape = gfc_get_shape (e->rank);
    6339              : 
    6340       237475 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6341              : 
    6342       237475 :   return;
    6343              : 
    6344       184149 : fail:
    6345       185856 :   for (i--; i >= 0; i--)
    6346         1707 :     mpz_clear (array[i]);
    6347              : }
    6348              : 
    6349              : 
    6350              : /* Given a variable expression node, compute the rank of the expression by
    6351              :    examining the base symbol and any reference structures it may have.  */
    6352              : 
    6353              : void
    6354      2677055 : gfc_expression_rank (gfc_expr *e)
    6355              : {
    6356      2677055 :   gfc_ref *ref, *coarray_ref = nullptr;
    6357      2677055 :   int i, rank, corank;
    6358              : 
    6359              :   /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
    6360              :      could lead to serious confusion...  */
    6361      2677055 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6362              : 
    6363      2677055 :   if (e->ref == NULL)
    6364              :     {
    6365      1965883 :       if (e->expr_type == EXPR_ARRAY)
    6366        76261 :         goto done;
    6367              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6368              : 
    6369      1889622 :       if (e->symtree != NULL)
    6370              :         {
    6371              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6372      1889610 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    6373        14891 :                                 && e->symtree->n.sym->ts.u.derived
    6374        14886 :                                 && CLASS_DATA (e->symtree->n.sym))
    6375      1889610 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6376              :                                  : e->symtree->n.sym->as;
    6377      1889610 :           if (as)
    6378              :             {
    6379          680 :               e->rank = as->rank;
    6380          680 :               e->corank = as->corank;
    6381          680 :               goto done;
    6382              :             }
    6383              :         }
    6384      1888942 :       e->rank = 0;
    6385      1888942 :       e->corank = 0;
    6386      1888942 :       goto done;
    6387              :     }
    6388              : 
    6389              :   rank = 0;
    6390              :   corank = 0;
    6391              : 
    6392      1132474 :   for (ref = e->ref; ref; ref = ref->next)
    6393              :     {
    6394       828930 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    6395          619 :           && ref->u.c.component->attr.function && !ref->next)
    6396              :         {
    6397          423 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6398          423 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6399              :         }
    6400              : 
    6401              :       /* F2018:5.4.7(5): an allocatable or pointer component selector ends the
    6402              :          codimensions inherited from an enclosing coarray.  */
    6403       828930 :       if (ref->type == REF_COMPONENT)
    6404              :         {
    6405       165377 :           gfc_component *comp = ref->u.c.component;
    6406              : 
    6407       165377 :           if (comp->ts.type == BT_CLASS && comp->attr.class_ok)
    6408              :             {
    6409         8173 :               if (CLASS_DATA (comp)->attr.class_pointer
    6410         6466 :                   || CLASS_DATA (comp)->attr.allocatable)
    6411       828930 :                 coarray_ref = nullptr;
    6412              :             }
    6413       157204 :           else if (comp->attr.pointer || comp->attr.allocatable)
    6414       828930 :             coarray_ref = nullptr;
    6415              :         }
    6416              : 
    6417       828930 :       if (ref->type != REF_ARRAY)
    6418       173264 :         continue;
    6419              : 
    6420       655666 :       if (!coarray_ref && ref->u.ar.as && ref->u.ar.as->corank > 0)
    6421       655666 :         coarray_ref = ref;
    6422       655666 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6423              :         {
    6424       360905 :           rank = ref->u.ar.as->rank;
    6425       360905 :           break;
    6426              :         }
    6427              : 
    6428       294761 :       if (ref->u.ar.type == AR_SECTION)
    6429              :         {
    6430              :           /* Figure out the rank of the section.  */
    6431        46723 :           if (rank != 0)
    6432            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6433              : 
    6434       116461 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6435        69738 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6436        69738 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6437        60681 :               rank++;
    6438              : 
    6439              :           break;
    6440              :         }
    6441              :     }
    6442              :   /* The codimensions come from the reference carrying them, which need not be
    6443              :      the last array reference: a subobject of a coarray is itself a coarray.  */
    6444       711172 :   if (coarray_ref && coarray_ref->u.ar.as->rank != -1)
    6445              :     {
    6446        19784 :       for (i = coarray_ref->u.ar.as->rank;
    6447        36737 :            i < coarray_ref->u.ar.as->rank + coarray_ref->u.ar.as->corank; ++i)
    6448              :         {
    6449              :           /* For unknown dimen in non-resolved as assume full corank.  */
    6450        20921 :           if (coarray_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6451        20251 :               || (coarray_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
    6452          467 :                   && !coarray_ref->u.ar.as->resolved))
    6453              :             {
    6454              :               corank = coarray_ref->u.ar.as->corank;
    6455              :               break;
    6456              :             }
    6457        19784 :           else if (coarray_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6458        19784 :                    || coarray_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6459        19686 :                    || coarray_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6460        17226 :             corank++;
    6461         2558 :           else if (coarray_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
    6462            0 :             gfc_internal_error ("Illegal coarray index");
    6463              :         }
    6464              :     }
    6465              : 
    6466       711172 :   e->rank = rank;
    6467       711172 :   e->corank = corank;
    6468              : 
    6469      2677055 : done:
    6470      2677055 :   expression_shape (e);
    6471      2677055 : }
    6472              : 
    6473              : 
    6474              : /* Given two expressions, check that their rank is conformable, i.e. either
    6475              :    both have the same rank or at least one is a scalar.  */
    6476              : 
    6477              : bool
    6478     12278628 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6479              : {
    6480     12278628 :   if (op1->expr_type == EXPR_VARIABLE)
    6481       755965 :     gfc_expression_rank (op1);
    6482     12278628 :   if (op2->expr_type == EXPR_VARIABLE)
    6483       452357 :     gfc_expression_rank (op2);
    6484              : 
    6485        80463 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6486     12358765 :          && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
    6487           30 :              || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
    6488              : }
    6489              : 
    6490              : 
    6491              : /* Given an expression EXPR that is a variable, figure out what the ultimate
    6492              :    variable's type is and store it in TS, traversing the reference structures
    6493              :    if necessary.
    6494              : 
    6495              :    We start at the base symbol and store the type.  Component references
    6496              :    overwrite a completely new type.  */
    6497              : 
    6498              : static void
    6499      1357237 : get_data_ref_type (gfc_expr *expr, gfc_typespec *ts)
    6500              : {
    6501      1357237 :   gfc_ref *ref;
    6502      1357237 :   gfc_symbol *sym;
    6503      1357237 :   gfc_component *comp;
    6504      1357237 :   bool has_inquiry_part;
    6505      1357237 :   bool has_substring_ref = false;
    6506              : 
    6507      1357237 :   if (expr->expr_type != EXPR_VARIABLE
    6508           14 :       && expr->expr_type != EXPR_FUNCTION
    6509            0 :       && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
    6510            0 :     gfc_internal_error ("get_data_ref_type(): Expression isn't a variable");
    6511              : 
    6512      1357237 :   sym = expr->symtree->n.sym;
    6513              : 
    6514      1357237 :   if (ts != NULL && expr->ts.type == BT_UNKNOWN)
    6515        53486 :     *ts = sym->ts;
    6516              : 
    6517              :   /* Catch left-overs from match_actual_arg, where an actual argument of a
    6518              :      procedure is given a temporary ts.type == BT_PROCEDURE.  The fixup is
    6519              :      needed for structure constructors in DATA statements, where a pointer
    6520              :      is associated with a data target, and the argument has not been fully
    6521              :      resolved yet.  Components references are dealt with further below.  */
    6522        53486 :   if (ts != NULL
    6523      1357237 :       && expr->ts.type == BT_PROCEDURE
    6524         3114 :       && expr->ref == NULL
    6525         3114 :       && sym->attr.flavor != FL_PROCEDURE
    6526          161 :       && sym->attr.target)
    6527            1 :     *ts = sym->ts;
    6528              : 
    6529      1357237 :   has_inquiry_part = false;
    6530      1885910 :   for (ref = expr->ref; ref; ref = ref->next)
    6531       529473 :     if (ref->type == REF_SUBSTRING)
    6532              :       has_substring_ref = true;
    6533       521643 :     else if (ref->type == REF_INQUIRY)
    6534              :       {
    6535              :         has_inquiry_part = true;
    6536              :         break;
    6537              :       }
    6538              : 
    6539      1886717 :   for (ref = expr->ref; ref; ref = ref->next)
    6540       529480 :     switch (ref->type)
    6541              :       {
    6542        95275 :       case REF_COMPONENT:
    6543        95275 :         comp = ref->u.c.component;
    6544        95275 :         if (ts != NULL && !has_inquiry_part)
    6545              :           {
    6546        95136 :             *ts = comp->ts;
    6547              :             /* Don't set the string length if a substring reference
    6548              :                follows.  */
    6549        95136 :             if (ts->type == BT_CHARACTER && has_substring_ref)
    6550          318 :               ts->u.cl = NULL;
    6551              :           }
    6552              :         break;
    6553              : 
    6554              :       case REF_ARRAY:
    6555              :       case REF_INQUIRY:
    6556              :       case REF_SUBSTRING:
    6557              :         break;
    6558              :       }
    6559      1357237 : }
    6560              : 
    6561              : 
    6562              : /* Resolve a variable expression.  */
    6563              : 
    6564              : static bool
    6565      1373153 : resolve_variable (gfc_expr *e)
    6566              : {
    6567      1373153 :   gfc_symbol *sym;
    6568      1373153 :   bool t;
    6569              : 
    6570      1373153 :   t = true;
    6571              : 
    6572      1373153 :   if (e->symtree == NULL)
    6573              :     return false;
    6574      1372660 :   sym = e->symtree->n.sym;
    6575              : 
    6576              :   /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
    6577              :      as ts.type is set to BT_ASSUMED in resolve_symbol.  */
    6578      1372660 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    6579              :     {
    6580          183 :       if (!actual_arg || inquiry_argument)
    6581              :         {
    6582            2 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
    6583              :                      "be used as actual argument", sym->name, &e->where);
    6584            2 :           return false;
    6585              :         }
    6586              :     }
    6587              :   /* TS 29113, 407b.  */
    6588      1372477 :   else if (e->ts.type == BT_ASSUMED)
    6589              :     {
    6590          595 :       if (!actual_arg)
    6591              :         {
    6592           20 :           gfc_error ("Assumed-type variable %s at %L may only be used "
    6593              :                      "as actual argument", sym->name, &e->where);
    6594           20 :           return false;
    6595              :         }
    6596          575 :       else if (inquiry_argument && !first_actual_arg)
    6597              :         {
    6598              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6599              :              for all inquiry functions in resolve_function; the reason is
    6600              :              that the function-name resolution happens too late in that
    6601              :              function.  */
    6602            0 :           gfc_error ("Assumed-type variable %s at %L as actual argument to "
    6603              :                      "an inquiry function shall be the first argument",
    6604              :                      sym->name, &e->where);
    6605            0 :           return false;
    6606              :         }
    6607              :     }
    6608              :   /* TS 29113, C535b.  */
    6609      1371882 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6610        40675 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6611        40670 :              && CLASS_DATA (sym)->as
    6612        16097 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6613      1370870 :             || (sym->ts.type != BT_CLASS && sym->as
    6614       373363 :                 && sym->as->type == AS_ASSUMED_RANK))
    6615         8107 :            && !sym->attr.select_rank_temporary
    6616         8107 :            && !(sym->assoc && sym->assoc->ar))
    6617              :     {
    6618         8107 :       if (!actual_arg
    6619         1289 :           && !(cs_base && cs_base->current
    6620         1288 :                && (cs_base->current->op == EXEC_SELECT_RANK
    6621          188 :                    || sym->attr.target)))
    6622              :         {
    6623          144 :           gfc_error ("Assumed-rank variable %s at %L may only be used as "
    6624              :                      "actual argument", sym->name, &e->where);
    6625          144 :           return false;
    6626              :         }
    6627         7963 :       else if (inquiry_argument && !first_actual_arg)
    6628              :         {
    6629              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6630              :              for all inquiry functions in resolve_function; the reason is
    6631              :              that the function-name resolution happens too late in that
    6632              :              function.  */
    6633            0 :           gfc_error ("Assumed-rank variable %s at %L as actual argument "
    6634              :                      "to an inquiry function shall be the first argument",
    6635              :                      sym->name, &e->where);
    6636            0 :           return false;
    6637              :         }
    6638              :     }
    6639              : 
    6640      1372494 :   if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
    6641          181 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6642          180 :            && e->ref->next == NULL))
    6643              :     {
    6644            1 :       gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
    6645              :                  "a subobject reference", sym->name, &e->ref->u.ar.where);
    6646            1 :       return false;
    6647              :     }
    6648              :   /* TS 29113, 407b.  */
    6649      1372493 :   else if (e->ts.type == BT_ASSUMED && e->ref
    6650          711 :            && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6651          704 :                 && e->ref->next == NULL))
    6652              :     {
    6653            7 :       gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
    6654              :                  "reference", sym->name, &e->ref->u.ar.where);
    6655            7 :       return false;
    6656              :     }
    6657              : 
    6658              :   /* TS 29113, C535b.  */
    6659      1372486 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6660        40675 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6661        40670 :         && CLASS_DATA (sym)->as
    6662        16097 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6663      1371474 :        || (sym->ts.type != BT_CLASS && sym->as
    6664       373923 :            && sym->as->type == AS_ASSUMED_RANK))
    6665         8271 :       && !(sym->assoc && sym->assoc->ar)
    6666         8271 :       && e->ref
    6667         8271 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6668         8267 :            && e->ref->next == NULL))
    6669              :     {
    6670            4 :       gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
    6671              :                  "reference", sym->name, &e->ref->u.ar.where);
    6672            4 :       return false;
    6673              :     }
    6674              : 
    6675              :   /* Guessed type variables are associate_names whose selector had not been
    6676              :      parsed at the time that the construct was parsed. Now the namespace is
    6677              :      being resolved, the TKR of the selector will be available for fixup of
    6678              :      the associate_name.  */
    6679      1372482 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6680              :     {
    6681          739 :       gfc_fixup_inferred_type_refs (e);
    6682              :       /* KIND inquiry ref returns the kind of the target.  */
    6683          739 :       if (e->expr_type == EXPR_CONSTANT)
    6684              :         return true;
    6685              :     }
    6686      1371743 :   else if (IS_INFERRED_TYPE (e)
    6687          525 :            && sym->ts.type != BT_UNKNOWN
    6688          525 :            && (sym->ts.type != e->ts.type || sym->ts.kind != e->ts.kind))
    6689              :     /* No subobject ref, but the expression's typespec was set at parse
    6690              :        time before the target's actual type/kind was known.  Refresh from
    6691              :        the now-resolved associate-name symbol.  */
    6692          216 :     e->ts = sym->ts;
    6693      1371527 :   else if (sym->attr.select_type_temporary
    6694         9561 :            && sym->ns->assoc_name_inferred)
    6695           98 :     gfc_fixup_inferred_type_refs (e);
    6696              : 
    6697              :   /* For variables that are used in an associate (target => object) where
    6698              :      the object's basetype is array valued while the target is scalar,
    6699              :      the ts' type of the component refs is still array valued, which
    6700              :      can't be translated that way.  */
    6701      1372470 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6702          733 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6703          733 :       && sym->assoc->target->ts.u.derived
    6704          733 :       && CLASS_DATA (sym->assoc->target)
    6705          733 :       && CLASS_DATA (sym->assoc->target)->as)
    6706              :     {
    6707              :       gfc_ref *ref = e->ref;
    6708          721 :       while (ref)
    6709              :         {
    6710          565 :           switch (ref->type)
    6711              :             {
    6712          256 :             case REF_COMPONENT:
    6713          256 :               ref->u.c.sym = sym->ts.u.derived;
    6714              :               /* Stop the loop.  */
    6715          256 :               ref = NULL;
    6716          256 :               break;
    6717          309 :             default:
    6718          309 :               ref = ref->next;
    6719          309 :               break;
    6720              :             }
    6721              :         }
    6722              :     }
    6723              : 
    6724              :   /* If this is an associate-name, it may be parsed with an array reference
    6725              :      in error even though the target is scalar.  Fail directly in this case.
    6726              :      TODO Understand why class scalar expressions must be excluded.  */
    6727      1372470 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6728              :     {
    6729              :       /* Inferred-type references are already fixed up above.  */
    6730        13143 :       if (sym->ts.type == BT_CLASS && !IS_INFERRED_TYPE (e))
    6731          239 :         gfc_fix_class_refs (e);
    6732        13143 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6733         2682 :           && e->ref->type == REF_ARRAY)
    6734              :         {
    6735              :           /* Unambiguously scalar!  */
    6736            3 :           if (sym->assoc->target
    6737            3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6738            1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6739            2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6740              :                        sym->name, &e->where);
    6741              :           return false;
    6742              :         }
    6743        13140 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6744         7435 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6745              :         {
    6746              :           /* This can happen because the parser did not detect that the
    6747              :              associate name is an array and the expression had no array
    6748              :              part_ref.  */
    6749          237 :           gfc_ref *ref = gfc_get_ref ();
    6750          237 :           ref->type = REF_ARRAY;
    6751          237 :           ref->u.ar.type = AR_FULL;
    6752          237 :           if (sym->as)
    6753              :             {
    6754          236 :               ref->u.ar.as = sym->as;
    6755          236 :               ref->u.ar.dimen = sym->as->rank;
    6756              :             }
    6757          237 :           ref->next = e->ref;
    6758          237 :           e->ref = ref;
    6759              :         }
    6760              :     }
    6761              : 
    6762      1372467 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6763            0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6764              : 
    6765              :   /* On the other hand, the parser may not have known this is an array;
    6766              :      in this case, we have to add a FULL reference.  */
    6767      1372467 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6768              :     {
    6769            0 :       e->ref = gfc_get_ref ();
    6770            0 :       e->ref->type = REF_ARRAY;
    6771            0 :       e->ref->u.ar.type = AR_FULL;
    6772            0 :       e->ref->u.ar.dimen = 0;
    6773              :     }
    6774              : 
    6775              :   /* Like above, but for class types, where the checking whether an array
    6776              :      ref is present is more complicated.  Furthermore make sure not to add
    6777              :      the full array ref to _vptr or _len refs.  */
    6778      1372467 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6779         1218 :       && CLASS_DATA (sym)
    6780         1218 :       && (CLASS_DATA (sym)->attr.dimension
    6781          634 :           || CLASS_DATA (sym)->attr.codimension)
    6782          590 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6783              :     {
    6784          565 :       gfc_ref *ref, *newref;
    6785              : 
    6786          565 :       newref = gfc_get_ref ();
    6787          565 :       newref->type = REF_ARRAY;
    6788          565 :       newref->u.ar.type = AR_FULL;
    6789          565 :       newref->u.ar.dimen = 0;
    6790              : 
    6791              :       /* Because this is an associate var and the first ref either is a ref to
    6792              :          the _data component or not, no traversal of the ref chain is
    6793              :          needed.  The array ref needs to be inserted after the _data ref,
    6794              :          or when that is not present, which may happened for polymorphic
    6795              :          types, then at the first position.  */
    6796          565 :       ref = e->ref;
    6797          565 :       if (!ref)
    6798           18 :         e->ref = newref;
    6799          547 :       else if (ref->type == REF_COMPONENT
    6800          238 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6801              :         {
    6802          232 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6803              :             {
    6804           12 :               newref->next = ref->next;
    6805           12 :               ref->next = newref;
    6806              :             }
    6807              :           else
    6808              :             /* Array ref present already.  */
    6809          220 :             gfc_free_ref_list (newref);
    6810              :         }
    6811          315 :       else if (ref->type == REF_ARRAY)
    6812              :         /* Array ref present already.  */
    6813          309 :         gfc_free_ref_list (newref);
    6814              :       else
    6815              :         {
    6816            6 :           newref->next = ref;
    6817            6 :           e->ref = newref;
    6818              :         }
    6819              :     }
    6820      1371902 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6821              :     {
    6822          810 :       gfc_ref *ref;
    6823         1282 :       for (ref = e->ref; ref; ref = ref->next)
    6824          562 :         if (ref->type == REF_SUBSTRING || ref->type == REF_INQUIRY)
    6825              :           break;
    6826          810 :       if (ref == NULL)
    6827          720 :         e->ts = sym->ts;
    6828              :     }
    6829              : 
    6830      1372467 :   if (e->ref && !gfc_resolve_ref (e))
    6831              :     return false;
    6832              : 
    6833      1372373 :   if (sym->attr.flavor == FL_PROCEDURE
    6834        34061 :       && (!sym->attr.function
    6835        19760 :           || (sym->attr.function && sym->result
    6836        19302 :               && sym->result->attr.proc_pointer
    6837          729 :               && !sym->result->attr.function)))
    6838              :     {
    6839        14301 :       e->ts.type = BT_PROCEDURE;
    6840        14301 :       goto resolve_procedure;
    6841              :     }
    6842              : 
    6843      1358072 :   if (sym->ts.type != BT_UNKNOWN)
    6844      1357237 :     get_data_ref_type (e, &e->ts);
    6845          835 :   else if (sym->attr.flavor == FL_PROCEDURE
    6846           12 :            && sym->attr.function && sym->result
    6847           12 :            && sym->result->ts.type != BT_UNKNOWN
    6848           10 :            && sym->result->attr.proc_pointer)
    6849           10 :     e->ts = sym->result->ts;
    6850              :   else
    6851              :     {
    6852              :       /* Must be a simple variable reference.  */
    6853          825 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6854              :         return false;
    6855          690 :       e->ts = sym->ts;
    6856              :     }
    6857              : 
    6858      1357937 :   if (check_assumed_size_reference (sym, e))
    6859              :     return false;
    6860              : 
    6861              :   /* Deal with forward references to entries during gfc_resolve_code, to
    6862              :      satisfy, at least partially, 12.5.2.5.  */
    6863      1357918 :   if (gfc_current_ns->entries
    6864         3412 :       && current_entry_id == sym->entry_id
    6865         1104 :       && cs_base
    6866         1018 :       && cs_base->current
    6867         1018 :       && cs_base->current->op != EXEC_ENTRY)
    6868              :     {
    6869         1018 :       int n;
    6870         1018 :       bool saved_specification_expr;
    6871         1018 :       gfc_symbol *saved_specification_expr_symbol;
    6872              : 
    6873              :       /* If the symbol is a dummy...  */
    6874         1018 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6875              :         {
    6876              :           /*  If it has not been seen as a dummy, this is an error.  */
    6877          492 :           if (!entry_dummy_seen_p (sym))
    6878              :             {
    6879            5 :               if (specification_expr
    6880            4 :                   && specification_expr_symbol
    6881            4 :                   && specification_expr_symbol->attr.dummy
    6882            2 :                   && specification_expr_symbol->ns == gfc_current_ns
    6883            7 :                   && !entry_dummy_seen_p (specification_expr_symbol))
    6884              :                 ;
    6885            3 :               else if (specification_expr)
    6886            2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6887              :                            ", is referenced at %L before the ENTRY statement "
    6888              :                            "in which it is a parameter",
    6889              :                            sym->name, &cs_base->current->loc);
    6890              :               else
    6891            1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6892              :                            "statement in which it is a parameter",
    6893              :                            sym->name, &cs_base->current->loc);
    6894              :               t = false;
    6895              :             }
    6896              :         }
    6897              : 
    6898              :       /* Now do the same check on the specification expressions.  */
    6899         1018 :       saved_specification_expr = specification_expr;
    6900         1018 :       saved_specification_expr_symbol = specification_expr_symbol;
    6901         1018 :       specification_expr = true;
    6902         1018 :       specification_expr_symbol = sym;
    6903         1018 :       if (sym->ts.type == BT_CHARACTER
    6904         1018 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6905              :         t = false;
    6906              : 
    6907         1018 :       if (sym->as)
    6908              :         {
    6909          303 :           for (n = 0; n < sym->as->rank; n++)
    6910              :             {
    6911          176 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6912            0 :                 t = false;
    6913          176 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6914            1 :                 t = false;
    6915              :             }
    6916              :         }
    6917         1018 :       specification_expr = saved_specification_expr;
    6918         1018 :       specification_expr_symbol = saved_specification_expr_symbol;
    6919              : 
    6920         1018 :       if (t)
    6921              :         /* Update the symbol's entry level.  */
    6922         1011 :         sym->entry_id = current_entry_id + 1;
    6923              :     }
    6924              : 
    6925              :   /* If a symbol has been host_associated mark it.  This is used latter,
    6926              :      to identify if aliasing is possible via host association.  */
    6927      1357918 :   if (sym->attr.flavor == FL_VARIABLE
    6928      1319755 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6929         6804 :           || !sym->ns->code->ext.block.assoc)
    6930      1317296 :       && gfc_current_ns->parent
    6931       627277 :       && (gfc_current_ns->parent == sym->ns
    6932       585225 :           || (gfc_current_ns->parent->parent
    6933        12764 :               && gfc_current_ns->parent->parent == sym->ns)))
    6934        48953 :     sym->attr.host_assoc = 1;
    6935              : 
    6936      1357918 :   if (gfc_current_ns->proc_name
    6937      1353397 :       && sym->attr.dimension
    6938       366739 :       && (sym->ns != gfc_current_ns
    6939       341754 :           || sym->attr.use_assoc
    6940       337467 :           || sym->attr.in_common))
    6941        34061 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6942              : 
    6943      1372219 : resolve_procedure:
    6944      1372219 :   if (t && !resolve_procedure_expression (e))
    6945              :     t = false;
    6946              : 
    6947              :   /* F2008, C617 and C1229.  */
    6948      1371062 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6949      1478077 :       && gfc_is_coindexed (e))
    6950              :     {
    6951          380 :       gfc_ref *ref, *ref2 = NULL;
    6952              : 
    6953          463 :       for (ref = e->ref; ref; ref = ref->next)
    6954              :         {
    6955          463 :           if (ref->type == REF_COMPONENT)
    6956           83 :             ref2 = ref;
    6957          463 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6958              :             break;
    6959              :         }
    6960              : 
    6961          760 :       for ( ; ref; ref = ref->next)
    6962          392 :         if (ref->type == REF_COMPONENT)
    6963              :           break;
    6964              : 
    6965              :       /* Expression itself is not coindexed object.  */
    6966          380 :       if (ref && e->ts.type == BT_CLASS)
    6967              :         {
    6968            3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6969              :                      &e->where);
    6970            3 :           t = false;
    6971              :         }
    6972              : 
    6973              :       /* Expression itself is coindexed object.  */
    6974              :       if (ref == NULL)
    6975              :         {
    6976          368 :           gfc_component *c;
    6977          368 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6978          488 :           for ( ; c; c = c->next)
    6979          120 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6980              :               {
    6981            0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6982              :                          "subcomponent at %L", &e->where);
    6983            0 :                 t = false;
    6984            0 :                 break;
    6985              :               }
    6986              :         }
    6987              :     }
    6988              : 
    6989      1372219 :   if (t)
    6990      1372209 :     gfc_expression_rank (e);
    6991              : 
    6992      1372219 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6993            3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6994              :                  "Using variable %qs at %L is deprecated",
    6995              :                  sym->name, &e->where);
    6996              :   /* Simplify cases where access to a parameter array results in a
    6997              :      single constant.  Suppress errors since those will have been
    6998              :      issued before, as warnings.  */
    6999      1372219 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    7000              :     {
    7001         2704 :       gfc_push_suppress_errors ();
    7002         2704 :       gfc_simplify_expr (e, 1);
    7003         2704 :       gfc_pop_suppress_errors ();
    7004              :     }
    7005              : 
    7006              :   return t;
    7007              : }
    7008              : 
    7009              : 
    7010              : /* The reference at *LINK follows a reference to component C in the
    7011              :    inferred type expression E.  Make it agree with the array spec of C.  */
    7012              : 
    7013              : static void
    7014          968 : fixup_inferred_component_array_ref (gfc_expr *e, gfc_component *c,
    7015              :                                     gfc_ref **link)
    7016              : {
    7017          968 :   gfc_ref *ref = *link;
    7018          968 :   gfc_array_spec *as;
    7019              : 
    7020          968 :   if (c->ts.type == BT_CLASS)
    7021          351 :     as = CLASS_DATA (c) ? CLASS_DATA (c)->as : NULL;
    7022              :   else
    7023          617 :     as = c->as;
    7024              : 
    7025          968 :   if (ref && ref->type == REF_ARRAY)
    7026              :     {
    7027          373 :       if (as)
    7028          360 :         ref->u.ar.as = as;
    7029           13 :       else if (ref->u.ar.type == AR_FULL)
    7030              :         {
    7031              :           /* Drop the full array ref of a component guessed to be an array.  */
    7032            6 :           *link = ref->next;
    7033            6 :           free (ref);
    7034              :         }
    7035            7 :       else if (c->ts.type == BT_CHARACTER && ref->u.ar.dimen == 1
    7036            6 :                && ref->u.ar.dimen_type[0] == DIMEN_RANGE
    7037            6 :                && !ref->u.ar.stride[0])
    7038              :         {
    7039              :           /* A substring misidentified as an array section.  */
    7040            6 :           gfc_expr *start = ref->u.ar.start[0];
    7041            6 :           gfc_expr *end = ref->u.ar.end[0];
    7042            6 :           ref->type = REF_SUBSTRING;
    7043            6 :           ref->u.ss.start = start;
    7044            6 :           ref->u.ss.end = end;
    7045            6 :           ref->u.ss.length = c->ts.u.cl;
    7046            6 :         }
    7047              :       else
    7048              :         /* Leave the array ref of a scalar to find_array_spec to diagnose.  */
    7049            1 :         ref->u.ar.as = NULL;
    7050              :     }
    7051          595 :   else if (as && as->rank == 1 && ref && ref->type == REF_SUBSTRING)
    7052              :     {
    7053              :       /* An array section misidentified as a substring.  */
    7054            6 :       gfc_expr *start = ref->u.ss.start;
    7055            6 :       gfc_expr *end = ref->u.ss.end;
    7056            6 :       memset (&ref->u, 0, sizeof (ref->u));
    7057            6 :       ref->type = REF_ARRAY;
    7058            6 :       ref->u.ar.type = AR_SECTION;
    7059            6 :       ref->u.ar.dimen = 1;
    7060            6 :       ref->u.ar.dimen_type[0] = DIMEN_RANGE;
    7061            6 :       ref->u.ar.start[0] = start;
    7062            6 :       ref->u.ar.end[0] = end;
    7063            6 :       ref->u.ar.as = as;
    7064            6 :       ref->u.ar.where = e->where;
    7065            6 :     }
    7066              :   else if (as)
    7067              :     {
    7068              :       /* Supply the full array ref of a component guessed to be scalar.  */
    7069           24 :       ref = gfc_get_ref ();
    7070           24 :       ref->type = REF_ARRAY;
    7071           24 :       ref->u.ar.type = AR_FULL;
    7072           24 :       ref->u.ar.as = as;
    7073           24 :       ref->u.ar.where = e->where;
    7074           24 :       ref->next = *link;
    7075           24 :       *link = ref;
    7076              :     }
    7077          968 : }
    7078              : 
    7079              : 
    7080              : /* 'sym' was initially guessed to be derived type but has been corrected
    7081              :    in resolve_assoc_var to be a class entity or the derived type correcting.
    7082              :    If a class entity it will certainly need the _data reference or the
    7083              :    reference derived type symbol correcting in the first component ref if
    7084              :    a derived type.  */
    7085              : 
    7086              : void
    7087          879 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    7088              : {
    7089          879 :   gfc_ref *ref, *new_ref;
    7090          879 :   gfc_symbol *sym, *derived, *dt, *container;
    7091          879 :   gfc_expr *target;
    7092          879 :   sym = e->symtree->n.sym;
    7093              : 
    7094              :   /* An associate_name whose selector is (i) a component ref of a selector
    7095              :      that is a inferred type associate_name; or (ii) an intrinsic type that
    7096              :      has been inferred from an inquiry ref.  */
    7097          879 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    7098              :     {
    7099          198 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    7100          198 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    7101          198 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    7102              :         {
    7103           60 :           ref = e->ref;
    7104              :           /* A substring misidentified as an array section.  */
    7105           60 :           if (sym->ts.type == BT_CHARACTER
    7106           30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    7107            6 :               && !ref->u.ar.stride[0])
    7108              :             {
    7109            6 :               new_ref = gfc_get_ref ();
    7110            6 :               new_ref->type = REF_SUBSTRING;
    7111            6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    7112            6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    7113            6 :               new_ref->u.ss.length = sym->ts.u.cl;
    7114            6 :               *ref = *new_ref;
    7115            6 :               free (new_ref);
    7116              :             }
    7117              :           else
    7118              :             {
    7119           54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    7120           24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    7121           54 :               e->ref = ref->next;
    7122           54 :               free (ref);
    7123              :             }
    7124              :         }
    7125              : 
    7126              :       /* It is possible for an inquiry reference to be mistaken for a
    7127              :          component reference. Correct this now.  */
    7128          198 :       ref = e->ref;
    7129          198 :       if (ref && ref->type == REF_ARRAY)
    7130           66 :         ref = ref->next;
    7131          102 :       if (ref && ref->type == REF_COMPONENT
    7132           78 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    7133              :         {
    7134           12 :           e->symtree->n.sym = sym;
    7135           12 :           *ref = *new_ref;
    7136           12 :           gfc_free_ref_list (new_ref);
    7137              :         }
    7138              : 
    7139              :       /* The kind of the associate name is best evaluated directly from the
    7140              :          selector because of the guesses made in primary.cc, when the type
    7141              :          is still unknown.  */
    7142          198 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    7143              :         {
    7144           24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    7145           12 :                                            sym->assoc->target->ts.kind);
    7146           12 :           gfc_replace_expr (e, ne);
    7147           12 :         }
    7148           90 :       else if (ref && ref->type == REF_INQUIRY
    7149           78 :                && (ref->u.i == INQUIRY_RE || ref->u.i == INQUIRY_IM)
    7150           60 :                && sym->ts.type == BT_COMPLEX
    7151           60 :                && e->ts.type == BT_REAL
    7152           60 :                && e->ts.kind != sym->ts.kind)
    7153              :         /* primary.cc set the inquiry-result kind to the default real kind
    7154              :            when the associate-name's type was inferred from %re/%im before
    7155              :            the target was resolved.  Now use the (resolved) selector kind.  */
    7156           24 :         e->ts.kind = sym->ts.kind;
    7157              : 
    7158              :       /* Now that the references are all sorted out, set the expression rank
    7159              :          and return.  */
    7160          198 :       gfc_expression_rank (e);
    7161          397 :       return;
    7162              :     }
    7163              : 
    7164          681 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    7165              :                                      : sym->ts.u.derived;
    7166              : 
    7167              :   /* Ensure that class symbols have an array spec and ensure that there
    7168              :      is a _data field reference following class type references.  */
    7169          681 :   if (sym->ts.type == BT_CLASS
    7170          193 :       && sym->assoc->target->ts.type == BT_CLASS)
    7171              :     {
    7172          193 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    7173          193 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    7174          193 :       sym->attr.dimension = 0;
    7175          193 :       sym->attr.codimension = 0;
    7176          193 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    7177          193 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    7178          193 :       if (e->ref && (e->ref->type != REF_COMPONENT
    7179          157 :                      || e->ref->u.c.component->name[0] != '_'))
    7180              :         {
    7181          127 :           ref = gfc_get_ref ();
    7182          127 :           ref->type = REF_COMPONENT;
    7183          127 :           ref->next = e->ref;
    7184          127 :           e->ref = ref;
    7185          127 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    7186              :                                                    true, true, NULL);
    7187          127 :           ref->u.c.sym = sym->ts.u.derived;
    7188              :         }
    7189              :     }
    7190              : 
    7191              :   /* The type used to build the component references at parse time was only
    7192              :      a guess, so they can point into a different derived type that happens
    7193              :      to have a component of the same name.  Bind each one by name to the
    7194              :      corrected type and fix up the array ref that follows it.  */
    7195          681 :   dt = derived;
    7196          681 :   container = sym->ts.type == BT_CLASS ? sym->ts.u.derived : NULL;
    7197          681 :   gfc_component *comp = NULL;
    7198          681 :   gfc_ref **link;
    7199          681 :   for (link = &e->ref; ; link = &(*link)->next)
    7200              :     {
    7201         2512 :       gfc_component *c;
    7202              : 
    7203         2512 :       ref = *link;
    7204         2512 :       if (comp && !(ref && ref->type == REF_COMPONENT
    7205          117 :                     && ref->u.c.component->name[0] == '_'))
    7206              :         {
    7207          968 :           fixup_inferred_component_array_ref (e, comp, link);
    7208          968 :           comp = NULL;
    7209          968 :           ref = *link;
    7210              :         }
    7211              : 
    7212         2512 :       if (!ref)
    7213              :         break;
    7214              : 
    7215         1832 :       if (ref->type != REF_COMPONENT)
    7216          670 :         continue;
    7217              : 
    7218              :       /* '_data', '_vptr' and '_len' are fields of the preceding class
    7219              :          container; 'dt' already is the declared type that follows '_data'.  */
    7220         1162 :       if (ref->u.c.component->name[0] == '_')
    7221              :         {
    7222          193 :           if (!container)
    7223              :             break;
    7224          193 :           c = gfc_find_component (container, ref->u.c.component->name,
    7225              :                                   true, true, NULL);
    7226          193 :           if (!c)
    7227              :             break;
    7228          193 :           ref->u.c.sym = container;
    7229          193 :           ref->u.c.component = c;
    7230          193 :           container = NULL;
    7231          193 :           if (strcmp (c->name, "_data") != 0)
    7232              :             break;
    7233          193 :           continue;
    7234              :         }
    7235              : 
    7236          969 :       if (!dt)
    7237              :         break;
    7238              : 
    7239          969 :       c = gfc_find_component (dt, ref->u.c.component->name, false, true, NULL);
    7240          969 :       if (!c)
    7241              :         {
    7242            1 :           if (gfc_find_component (dt, ref->u.c.component->name,
    7243              :                                   true, true, NULL))
    7244            1 :             gfc_error ("Component %qs at %L is a PRIVATE component of %qs",
    7245            1 :                        ref->u.c.component->name, &e->where, dt->name);
    7246              :           else
    7247            0 :             gfc_error ("%qs at %L is not a member of the %qs structure",
    7248            0 :                        ref->u.c.component->name, &e->where, dt->name);
    7249              :           return;
    7250              :         }
    7251              : 
    7252          968 :       ref->u.c.sym = dt;
    7253          968 :       ref->u.c.component = c;
    7254          968 :       comp = c;
    7255              : 
    7256          968 :       container = NULL;
    7257          968 :       if (c->ts.type == BT_DERIVED)
    7258          168 :         dt = c->ts.u.derived;
    7259          800 :       else if (c->ts.type == BT_CLASS && CLASS_DATA (c))
    7260              :         {
    7261          351 :           container = c->ts.u.derived;
    7262          351 :           dt = CLASS_DATA (c)->ts.u.derived;
    7263              :         }
    7264              :       else
    7265              :         dt = NULL;
    7266         1831 :     }
    7267              : 
    7268              :   /* Verify that the type inference mechanism has not introduced a spurious
    7269              :      array reference.  This can happen with an associate name, whose selector
    7270              :      is an element of another inferred type.  */
    7271          680 :   target = e->symtree->n.sym->assoc->target;
    7272          680 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7273          402 :       && e != target && !target->rank)
    7274              :     {
    7275              :       /* First case: array ref after the scalar class or derived
    7276              :          associate_name.  */
    7277          402 :       if (e->ref && e->ref->type == REF_ARRAY
    7278            7 :           && e->ref->u.ar.type != AR_ELEMENT)
    7279              :         {
    7280            7 :           ref = e->ref;
    7281            7 :           if (ref->u.ar.type == AR_UNKNOWN)
    7282            1 :             gfc_error ("Invalid array reference at %L", &e->where);
    7283            7 :           e->ref = ref->next;
    7284            7 :           free (ref);
    7285              : 
    7286              :           /* If it hasn't a ref to the '_data' field supply one.  */
    7287            7 :           if (sym->ts.type == BT_CLASS
    7288            0 :               && !(e->ref->type == REF_COMPONENT
    7289            0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    7290              :             {
    7291            0 :               gfc_ref *new_ref;
    7292            0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    7293              :                                   "_data", true, true, &new_ref);
    7294            0 :               new_ref->next = e->ref;
    7295            0 :               e->ref = new_ref;
    7296              :             }
    7297              :         }
    7298              :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    7299          395 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7300          395 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    7301          133 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    7302            0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    7303              :         {
    7304            0 :           ref = e->ref->next;
    7305            0 :           if (ref->u.ar.type == AR_UNKNOWN)
    7306            0 :             gfc_error ("Invalid array reference at %L", &e->where);
    7307            0 :           e->ref->next = e->ref->next->next;
    7308            0 :           free (ref);
    7309              :         }
    7310              :     }
    7311              : 
    7312              :   /* Now that all the references are OK, get the expression rank.  */
    7313          680 :   gfc_expression_rank (e);
    7314              : }
    7315              : 
    7316              : 
    7317              : /* Checks to see that the correct symbol has been host associated.
    7318              :    The only situations where this arises are:
    7319              :         (i)  That in which a twice contained function is parsed after
    7320              :              the host association is made. On detecting this, change
    7321              :              the symbol in the expression and convert the array reference
    7322              :              into an actual arglist if the old symbol is a variable; or
    7323              :         (ii) That in which an external function is typed but not declared
    7324              :              explicitly to be external. Here, the old symbol is changed
    7325              :              from a variable to an external function.  */
    7326              : static bool
    7327      1730282 : check_host_association (gfc_expr *e)
    7328              : {
    7329      1730282 :   gfc_symbol *sym, *old_sym;
    7330      1730282 :   gfc_symtree *st;
    7331      1730282 :   int n;
    7332      1730282 :   gfc_ref *ref;
    7333      1730282 :   gfc_actual_arglist *arg, *tail = NULL;
    7334      1730282 :   bool retval = e->expr_type == EXPR_FUNCTION;
    7335              : 
    7336              :   /*  If the expression is the result of substitution in
    7337              :       interface.cc(gfc_extend_expr) because there is no way in
    7338              :       which the host association can be wrong.  */
    7339      1730282 :   if (e->symtree == NULL
    7340      1729398 :         || e->symtree->n.sym == NULL
    7341      1729398 :         || e->user_operator)
    7342              :     return retval;
    7343              : 
    7344      1727618 :   old_sym = e->symtree->n.sym;
    7345              : 
    7346      1727618 :   if (gfc_current_ns->parent
    7347       758418 :         && old_sym->ns != gfc_current_ns)
    7348              :     {
    7349              :       /* Use the 'USE' name so that renamed module symbols are
    7350              :          correctly handled.  */
    7351        98059 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7352              : 
    7353        98059 :       if (sym && old_sym != sym
    7354          717 :               && sym->attr.flavor == FL_PROCEDURE
    7355          111 :               && sym->attr.contained)
    7356              :         {
    7357              :           /* Clear the shape, since it might not be valid.  */
    7358           83 :           gfc_free_shape (&e->shape, e->rank);
    7359              : 
    7360              :           /* Give the expression the right symtree!  */
    7361           83 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    7362           83 :           gcc_assert (st != NULL);
    7363              : 
    7364           83 :           if (old_sym->attr.flavor == FL_PROCEDURE
    7365           59 :                 || e->expr_type == EXPR_FUNCTION)
    7366              :             {
    7367              :               /* Original was function so point to the new symbol, since
    7368              :                  the actual argument list is already attached to the
    7369              :                  expression.  */
    7370           30 :               e->value.function.esym = NULL;
    7371           30 :               e->symtree = st;
    7372              :             }
    7373              :           else
    7374              :             {
    7375              :               /* Original was variable so convert array references into
    7376              :                  an actual arglist. This does not need any checking now
    7377              :                  since resolve_function will take care of it.  */
    7378           53 :               e->value.function.actual = NULL;
    7379           53 :               e->expr_type = EXPR_FUNCTION;
    7380           53 :               e->symtree = st;
    7381              : 
    7382              :               /* Ambiguity will not arise if the array reference is not
    7383              :                  the last reference.  */
    7384           55 :               for (ref = e->ref; ref; ref = ref->next)
    7385           38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    7386              :                   break;
    7387              : 
    7388           53 :               if ((ref == NULL || ref->type != REF_ARRAY)
    7389           17 :                   && sym->attr.proc == PROC_INTERNAL)
    7390              :                 {
    7391            4 :                   gfc_error ("%qs at %L is host associated at %L into "
    7392              :                              "a contained procedure with an internal "
    7393              :                              "procedure of the same name", sym->name,
    7394              :                               &old_sym->declared_at, &e->where);
    7395            4 :                   return false;
    7396              :                 }
    7397              : 
    7398           13 :               if (ref == NULL)
    7399              :                 return false;
    7400              : 
    7401           36 :               gcc_assert (ref->type == REF_ARRAY);
    7402              : 
    7403              :               /* Grab the start expressions from the array ref and
    7404              :                  copy them into actual arguments.  */
    7405           84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7406              :                 {
    7407           48 :                   arg = gfc_get_actual_arglist ();
    7408           48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    7409           48 :                   if (e->value.function.actual == NULL)
    7410           36 :                     tail = e->value.function.actual = arg;
    7411              :                   else
    7412              :                     {
    7413           12 :                       tail->next = arg;
    7414           12 :                       tail = arg;
    7415              :                     }
    7416              :                 }
    7417              : 
    7418              :               /* Dump the reference list and set the rank.  */
    7419           36 :               gfc_free_ref_list (e->ref);
    7420           36 :               e->ref = NULL;
    7421           36 :               e->rank = sym->as ? sym->as->rank : 0;
    7422           36 :               e->corank = sym->as ? sym->as->corank : 0;
    7423              :             }
    7424              : 
    7425           66 :           gfc_resolve_expr (e);
    7426           66 :           sym->refs++;
    7427              :         }
    7428              :       /* This case corresponds to a call, from a block or a contained
    7429              :          procedure, to an external function, which has not been declared
    7430              :          as being external in the main program but has been typed.  */
    7431        97976 :       else if (sym && old_sym != sym
    7432          634 :                && !e->ref
    7433          362 :                && sym->ts.type == BT_UNKNOWN
    7434           27 :                && old_sym->ts.type != BT_UNKNOWN
    7435           19 :                && sym->attr.flavor == FL_PROCEDURE
    7436           19 :                && old_sym->attr.flavor == FL_VARIABLE
    7437            7 :                && sym->ns->parent == old_sym->ns
    7438            7 :                && sym->ns->proc_name
    7439            7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    7440            6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    7441            6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    7442              :         {
    7443            6 :           old_sym->attr.flavor = FL_PROCEDURE;
    7444            6 :           old_sym->attr.external = 1;
    7445            6 :           old_sym->attr.function = 1;
    7446            6 :           old_sym->result = old_sym;
    7447            6 :           gfc_resolve_expr (e);
    7448              :         }
    7449              :     }
    7450              :   /* This might have changed!  */
    7451      1727601 :   return e->expr_type == EXPR_FUNCTION;
    7452              : }
    7453              : 
    7454              : 
    7455              : static void
    7456         1556 : gfc_resolve_character_operator (gfc_expr *e)
    7457              : {
    7458         1556 :   gfc_expr *op1 = e->value.op.op1;
    7459         1556 :   gfc_expr *op2 = e->value.op.op2;
    7460         1556 :   gfc_expr *e1 = NULL;
    7461         1556 :   gfc_expr *e2 = NULL;
    7462              : 
    7463         1556 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7464              : 
    7465         1556 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7466          880 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7467          676 :   else if (op1->expr_type == EXPR_CONSTANT)
    7468          280 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7469          280 :                            op1->value.character.length);
    7470              : 
    7471         1556 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7472          831 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7473          725 :   else if (op2->expr_type == EXPR_CONSTANT)
    7474          492 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7475          492 :                            op2->value.character.length);
    7476              : 
    7477         1556 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7478              : 
    7479         1556 :   if (!e1 || !e2)
    7480              :     {
    7481          521 :       gfc_free_expr (e1);
    7482          521 :       gfc_free_expr (e2);
    7483              : 
    7484          521 :       return;
    7485              :     }
    7486              : 
    7487         1035 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7488         1035 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7489         1035 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7490         1035 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7491         1035 :   gfc_resolve_expr (e->ts.u.cl->length);
    7492              : 
    7493         1035 :   return;
    7494              : }
    7495              : 
    7496              : 
    7497              : /*  Ensure that an character expression has a charlen and, if possible, a
    7498              :     length expression.  */
    7499              : 
    7500              : static void
    7501       183413 : fixup_charlen (gfc_expr *e)
    7502              : {
    7503              :   /* The cases fall through so that changes in expression type and the need
    7504              :      for multiple fixes are picked up.  In all circumstances, a charlen should
    7505              :      be available for the middle end to hang a backend_decl on.  */
    7506       183413 :   switch (e->expr_type)
    7507              :     {
    7508         1556 :     case EXPR_OP:
    7509         1556 :       gfc_resolve_character_operator (e);
    7510              :       /* FALLTHRU */
    7511              : 
    7512         1623 :     case EXPR_ARRAY:
    7513         1623 :       if (e->expr_type == EXPR_ARRAY)
    7514           67 :         gfc_resolve_character_array_constructor (e);
    7515              :       /* FALLTHRU */
    7516              : 
    7517         2083 :     case EXPR_SUBSTRING:
    7518         2083 :       if (!e->ts.u.cl && e->ref)
    7519          456 :         gfc_resolve_substring_charlen (e);
    7520              :       /* FALLTHRU */
    7521              : 
    7522       183413 :     default:
    7523       183413 :       if (!e->ts.u.cl)
    7524       181334 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7525              : 
    7526       183413 :       break;
    7527              :     }
    7528       183413 : }
    7529              : 
    7530              : 
    7531              : /* Update an actual argument to include the passed-object for type-bound
    7532              :    procedures at the right position.  */
    7533              : 
    7534              : static gfc_actual_arglist*
    7535         3116 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7536              :                      const char *name)
    7537              : {
    7538         3140 :   gcc_assert (argpos > 0);
    7539              : 
    7540         3140 :   if (argpos == 1)
    7541              :     {
    7542         2991 :       gfc_actual_arglist* result;
    7543              : 
    7544         2991 :       result = gfc_get_actual_arglist ();
    7545         2991 :       result->expr = po;
    7546         2991 :       result->next = lst;
    7547         2991 :       if (name)
    7548          514 :         result->name = name;
    7549              : 
    7550              :       return result;
    7551              :     }
    7552              : 
    7553          149 :   if (lst)
    7554          125 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    7555              :   else
    7556           24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    7557              :   return lst;
    7558              : }
    7559              : 
    7560              : 
    7561              : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    7562              : 
    7563              : static gfc_expr*
    7564         7605 : extract_compcall_passed_object (gfc_expr* e)
    7565              : {
    7566         7605 :   gfc_expr* po;
    7567              : 
    7568         7605 :   if (e->expr_type == EXPR_UNKNOWN)
    7569              :     {
    7570            0 :       gfc_error ("Error in typebound call at %L",
    7571              :                  &e->where);
    7572            0 :       return NULL;
    7573              :     }
    7574              : 
    7575         7605 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7576              : 
    7577         7605 :   if (e->value.compcall.base_object)
    7578         1704 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7579              :   else
    7580              :     {
    7581         5901 :       po = gfc_get_expr ();
    7582         5901 :       po->expr_type = EXPR_VARIABLE;
    7583         5901 :       po->symtree = e->symtree;
    7584         5901 :       po->ref = gfc_copy_ref (e->ref);
    7585         5901 :       po->where = e->where;
    7586              :     }
    7587              : 
    7588         7605 :   if (!gfc_resolve_expr (po))
    7589            3 :     return NULL;
    7590              : 
    7591              :   return po;
    7592              : }
    7593              : 
    7594              : 
    7595              : /* Update the arglist of an EXPR_COMPCALL expression to include the
    7596              :    passed-object.  */
    7597              : 
    7598              : static bool
    7599         3498 : update_compcall_arglist (gfc_expr* e)
    7600              : {
    7601         3498 :   gfc_expr* po;
    7602         3498 :   gfc_typebound_proc* tbp;
    7603              : 
    7604         3498 :   tbp = e->value.compcall.tbp;
    7605              : 
    7606         3498 :   if (tbp->error)
    7607              :     return false;
    7608              : 
    7609         3497 :   po = extract_compcall_passed_object (e);
    7610         3497 :   if (!po)
    7611              :     return false;
    7612              : 
    7613         3497 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7614              :     {
    7615         1188 :       gfc_free_expr (po);
    7616         1188 :       return true;
    7617              :     }
    7618              : 
    7619         2309 :   if (tbp->pass_arg_num <= 0)
    7620              :     return false;
    7621              : 
    7622         2308 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7623              :                                                   tbp->pass_arg_num,
    7624              :                                                   tbp->pass_arg);
    7625              : 
    7626         2308 :   return true;
    7627              : }
    7628              : 
    7629              : 
    7630              : /* Extract the passed object from a PPC call (a copy of it).  */
    7631              : 
    7632              : static gfc_expr*
    7633           85 : extract_ppc_passed_object (gfc_expr *e)
    7634              : {
    7635           85 :   gfc_expr *po;
    7636           85 :   gfc_ref **ref;
    7637              : 
    7638           85 :   po = gfc_get_expr ();
    7639           85 :   po->expr_type = EXPR_VARIABLE;
    7640           85 :   po->symtree = e->symtree;
    7641           85 :   po->ref = gfc_copy_ref (e->ref);
    7642           85 :   po->where = e->where;
    7643              : 
    7644              :   /* Remove PPC reference.  */
    7645           85 :   ref = &po->ref;
    7646           91 :   while ((*ref)->next)
    7647            6 :     ref = &(*ref)->next;
    7648           85 :   gfc_free_ref_list (*ref);
    7649           85 :   *ref = NULL;
    7650              : 
    7651           85 :   if (!gfc_resolve_expr (po))
    7652            0 :     return NULL;
    7653              : 
    7654              :   return po;
    7655              : }
    7656              : 
    7657              : 
    7658              : /* Update the actual arglist of a procedure pointer component to include the
    7659              :    passed-object.  */
    7660              : 
    7661              : static bool
    7662          607 : update_ppc_arglist (gfc_expr* e)
    7663              : {
    7664          607 :   gfc_expr* po;
    7665          607 :   gfc_component *ppc;
    7666          607 :   gfc_typebound_proc* tb;
    7667              : 
    7668          607 :   ppc = gfc_get_proc_ptr_comp (e);
    7669          607 :   if (!ppc)
    7670              :     return false;
    7671              : 
    7672          607 :   tb = ppc->tb;
    7673              : 
    7674          607 :   if (tb->error)
    7675              :     return false;
    7676          605 :   else if (tb->nopass)
    7677              :     return true;
    7678              : 
    7679           85 :   po = extract_ppc_passed_object (e);
    7680           85 :   if (!po)
    7681              :     return false;
    7682              : 
    7683              :   /* F08:R739.  */
    7684           85 :   if (po->rank != 0)
    7685              :     {
    7686            0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7687            0 :       return false;
    7688              :     }
    7689              : 
    7690              :   /* F08:C611.  */
    7691           85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7692              :     {
    7693            1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7694              :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7695            1 :       return false;
    7696              :     }
    7697              : 
    7698           84 :   gcc_assert (tb->pass_arg_num > 0);
    7699           84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7700              :                                                   tb->pass_arg_num,
    7701              :                                                   tb->pass_arg);
    7702              : 
    7703           84 :   return true;
    7704              : }
    7705              : 
    7706              : 
    7707              : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7708              :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7709              : 
    7710              : static bool
    7711         3509 : check_typebound_baseobject (gfc_expr* e)
    7712              : {
    7713         3509 :   gfc_expr* base;
    7714         3509 :   bool return_value = false;
    7715              : 
    7716         3509 :   base = extract_compcall_passed_object (e);
    7717         3509 :   if (!base)
    7718              :     return false;
    7719              : 
    7720         3506 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7721              :     {
    7722            1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7723            1 :       goto cleanup;
    7724              :     }
    7725              : 
    7726         3505 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7727            1 :     return false;
    7728              : 
    7729              :   /* F08:C611.  */
    7730         3504 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7731              :     {
    7732            3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7733              :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7734            3 :       goto cleanup;
    7735              :     }
    7736              : 
    7737              :   /* F08:C1230. If the procedure called is NOPASS,
    7738              :      the base object must be scalar.  */
    7739         3501 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7740              :     {
    7741            1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7742              :                  " be scalar", &e->where);
    7743            1 :       goto cleanup;
    7744              :     }
    7745              : 
    7746              :   return_value = true;
    7747              : 
    7748         3505 : cleanup:
    7749         3505 :   gfc_free_expr (base);
    7750         3505 :   return return_value;
    7751              : }
    7752              : 
    7753              : 
    7754              : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7755              :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7756              :    arglist and the target-procedure symtree are returned.  */
    7757              : 
    7758              : static bool
    7759         3498 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7760              :                           gfc_actual_arglist** actual)
    7761              : {
    7762         3498 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7763         3498 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7764              : 
    7765              :   /* Update the actual arglist for PASS.  */
    7766         3498 :   if (!update_compcall_arglist (e))
    7767              :     return false;
    7768              : 
    7769         3496 :   *actual = e->value.compcall.actual;
    7770         3496 :   *target = e->value.compcall.tbp->u.specific;
    7771              : 
    7772         3496 :   gfc_free_ref_list (e->ref);
    7773         3496 :   e->ref = NULL;
    7774         3496 :   e->value.compcall.actual = NULL;
    7775              : 
    7776              :   /* If we find a deferred typebound procedure, check for derived types
    7777              :      that an overriding typebound procedure has not been missed.  */
    7778         3496 :   if (e->value.compcall.name
    7779         3496 :       && !e->value.compcall.tbp->non_overridable
    7780         3478 :       && e->value.compcall.base_object
    7781          852 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7782              :     {
    7783          559 :       gfc_symtree *st;
    7784          559 :       gfc_symbol *derived;
    7785              : 
    7786              :       /* Use the derived type of the base_object.  */
    7787          559 :       derived = e->value.compcall.base_object->ts.u.derived;
    7788          559 :       st = NULL;
    7789              : 
    7790              :       /* If necessary, go through the inheritance chain.  */
    7791         1685 :       while (!st && derived)
    7792              :         {
    7793              :           /* Look for the typebound procedure 'name'.  */
    7794          567 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7795          559 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7796              :                                    e->value.compcall.name);
    7797          567 :           if (!st)
    7798            8 :             derived = gfc_get_derived_super_type (derived);
    7799              :         }
    7800              : 
    7801              :       /* Now find the specific name in the derived type namespace.  */
    7802          559 :       if (st && st->n.tb && st->n.tb->u.specific)
    7803          559 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7804          559 :                            derived->ns, 1, &st);
    7805          559 :       if (st)
    7806          559 :         *target = st;
    7807              :     }
    7808              : 
    7809         3496 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7810         3496 :       && !e->value.compcall.tbp->deferred)
    7811            1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7812              :                  " itself recursively.  Declare it RECURSIVE or use"
    7813              :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7814              : 
    7815              :   return true;
    7816              : }
    7817              : 
    7818              : 
    7819              : /* Get the ultimate declared type from an expression.  In addition,
    7820              :    return the last class/derived type reference and the copy of the
    7821              :    reference list.  If check_types is set true, derived types are
    7822              :    identified as well as class references.  */
    7823              : static gfc_symbol*
    7824         3473 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7825              :                         gfc_expr *e, bool check_types)
    7826              : {
    7827         3473 :   gfc_symbol *declared;
    7828         3473 :   gfc_ref *ref;
    7829              : 
    7830         3473 :   declared = NULL;
    7831         3473 :   if (class_ref)
    7832         2960 :     *class_ref = NULL;
    7833         3473 :   if (new_ref)
    7834         2667 :     *new_ref = gfc_copy_ref (e->ref);
    7835              : 
    7836         4508 :   for (ref = e->ref; ref; ref = ref->next)
    7837              :     {
    7838         1035 :       if (ref->type != REF_COMPONENT)
    7839          376 :         continue;
    7840              : 
    7841          659 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7842          328 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7843          584 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7844              :         {
    7845          510 :           declared = ref->u.c.component->ts.u.derived;
    7846          510 :           if (class_ref)
    7847          410 :             *class_ref = ref;
    7848              :         }
    7849              :     }
    7850              : 
    7851         3473 :   if (declared == NULL)
    7852         3049 :     declared = e->symtree->n.sym->ts.u.derived;
    7853              : 
    7854         3473 :   return declared;
    7855              : }
    7856              : 
    7857              : 
    7858              : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7859              :    which of the specific bindings (if any) matches the arglist and transform
    7860              :    the expression into a call of that binding.  */
    7861              : 
    7862              : static bool
    7863         3500 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7864              : {
    7865         3500 :   gfc_typebound_proc* genproc;
    7866         3500 :   const char* genname;
    7867         3500 :   gfc_symtree *st;
    7868         3500 :   gfc_symbol *derived;
    7869              : 
    7870         3500 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7871         3500 :   genname = e->value.compcall.name;
    7872         3500 :   genproc = e->value.compcall.tbp;
    7873              : 
    7874         3500 :   if (!genproc->is_generic)
    7875              :     return true;
    7876              : 
    7877              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7878          457 :   for (; genproc; genproc = genproc->overridden)
    7879              :     {
    7880          455 :       gfc_tbp_generic* g;
    7881              : 
    7882          455 :       gcc_assert (genproc->is_generic);
    7883          695 :       for (g = genproc->u.generic; g; g = g->next)
    7884              :         {
    7885          685 :           gfc_symbol* target;
    7886          685 :           gfc_actual_arglist* args;
    7887          685 :           bool matches;
    7888              : 
    7889          685 :           gcc_assert (g->specific);
    7890              : 
    7891          685 :           if (g->specific->error)
    7892            0 :             continue;
    7893              : 
    7894          685 :           target = g->specific->u.specific->n.sym;
    7895              : 
    7896              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7897          685 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7898          685 :           if (!g->specific->nopass)
    7899              :             {
    7900          599 :               gfc_expr* po;
    7901          599 :               po = extract_compcall_passed_object (e);
    7902          599 :               if (!po)
    7903              :                 {
    7904            0 :                   gfc_free_actual_arglist (args);
    7905            0 :                   return false;
    7906              :                 }
    7907              : 
    7908          599 :               gcc_assert (g->specific->pass_arg_num > 0);
    7909          599 :               gcc_assert (!g->specific->error);
    7910          599 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7911              :                                           g->specific->pass_arg);
    7912              :             }
    7913         1375 :           resolve_actual_arglist (args, target->attr.proc,
    7914          685 :                                   is_external_proc (target)
    7915            5 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7916              : 
    7917              :           /* Check if this arglist matches the formal.  */
    7918          685 :           matches = gfc_arglist_matches_symbol (&args, target);
    7919              : 
    7920              :           /* Clean up and break out of the loop if we've found it.  */
    7921          685 :           gfc_free_actual_arglist (args);
    7922          685 :           if (matches)
    7923              :             {
    7924          445 :               e->value.compcall.tbp = g->specific;
    7925          445 :               genname = g->specific_st->name;
    7926              :               /* Pass along the name for CLASS methods, where the vtab
    7927              :                  procedure pointer component has to be referenced.  */
    7928          445 :               if (name)
    7929          161 :                 *name = genname;
    7930          445 :               goto success;
    7931              :             }
    7932              :         }
    7933              :     }
    7934              : 
    7935              :   /* Nothing matching found!  */
    7936            2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7937              :              " %qs at %L", genname, &e->where);
    7938            2 :   return false;
    7939              : 
    7940          445 : success:
    7941              :   /* Make sure that we have the right specific instance for the name.  */
    7942          445 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7943              : 
    7944          445 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7945          445 :   if (st)
    7946          445 :     e->value.compcall.tbp = st->n.tb;
    7947              : 
    7948              :   return true;
    7949              : }
    7950              : 
    7951              : 
    7952              : /* Resolve a call to a type-bound subroutine.  */
    7953              : 
    7954              : static bool
    7955         1798 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7956              : {
    7957         1798 :   gfc_actual_arglist* newactual;
    7958         1798 :   gfc_symtree* target;
    7959              : 
    7960              :   /* Check that's really a SUBROUTINE.  */
    7961         1798 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7962              :     {
    7963           17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7964           15 :           && c->expr1->value.compcall.tbp->u.specific
    7965           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7966           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7967           12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7968              :       else
    7969              :         {
    7970            5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7971              :                      c->expr1->value.compcall.name, &c->loc);
    7972            5 :           return false;
    7973              :         }
    7974              :     }
    7975              : 
    7976         1793 :   if (!check_typebound_baseobject (c->expr1))
    7977              :     return false;
    7978              : 
    7979              :   /* Pass along the name for CLASS methods, where the vtab
    7980              :      procedure pointer component has to be referenced.  */
    7981         1786 :   if (name)
    7982          492 :     *name = c->expr1->value.compcall.name;
    7983              : 
    7984         1786 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7985              :     return false;
    7986              : 
    7987              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7988         1785 :   if (overridable)
    7989          383 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7990              : 
    7991              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7992              : 
    7993         1785 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7994              :     return false;
    7995              : 
    7996         1783 :   c->ext.actual = newactual;
    7997         1783 :   c->symtree = target;
    7998         1783 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7999              : 
    8000         1783 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    8001              : 
    8002         1783 :   gfc_free_expr (c->expr1);
    8003         1783 :   c->expr1 = gfc_get_expr ();
    8004         1783 :   c->expr1->expr_type = EXPR_FUNCTION;
    8005         1783 :   c->expr1->symtree = target;
    8006         1783 :   c->expr1->where = c->loc;
    8007              : 
    8008         1783 :   return resolve_call (c);
    8009              : }
    8010              : 
    8011              : 
    8012              : /* Resolve a component-call expression.  */
    8013              : static bool
    8014         1723 : resolve_compcall (gfc_expr* e, const char **name)
    8015              : {
    8016         1723 :   gfc_actual_arglist* newactual;
    8017         1723 :   gfc_symtree* target;
    8018              : 
    8019              :   /* Check that's really a FUNCTION.  */
    8020         1723 :   if (!e->value.compcall.tbp->function)
    8021              :     {
    8022            7 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    8023            5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    8024              :                    &e->where);
    8025              :       return false;
    8026              :     }
    8027              : 
    8028              : 
    8029              :   /* These must not be assign-calls!  */
    8030         1716 :   gcc_assert (!e->value.compcall.assign);
    8031              : 
    8032         1716 :   if (!check_typebound_baseobject (e))
    8033              :     return false;
    8034              : 
    8035              :   /* Pass along the name for CLASS methods, where the vtab
    8036              :      procedure pointer component has to be referenced.  */
    8037         1714 :   if (name)
    8038          900 :     *name = e->value.compcall.name;
    8039              : 
    8040         1714 :   if (!resolve_typebound_generic_call (e, name))
    8041              :     return false;
    8042         1713 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    8043              : 
    8044              :   /* Take the rank from the function's symbol.  */
    8045         1713 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    8046              :     {
    8047          155 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    8048          155 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    8049              :     }
    8050              : 
    8051              :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    8052              :      arglist to the TBP's binding target.  */
    8053              : 
    8054         1713 :   if (!resolve_typebound_static (e, &target, &newactual))
    8055              :     return false;
    8056              : 
    8057         1713 :   e->value.function.actual = newactual;
    8058         1713 :   e->value.function.name = NULL;
    8059         1713 :   e->value.function.esym = target->n.sym;
    8060         1713 :   e->value.function.isym = NULL;
    8061         1713 :   e->symtree = target;
    8062         1713 :   e->ts = target->n.sym->ts;
    8063         1713 :   e->expr_type = EXPR_FUNCTION;
    8064              : 
    8065              :   /* Resolution is not necessary if this is a class subroutine; this
    8066              :      function only has to identify the specific proc. Resolution of
    8067              :      the call will be done next in resolve_typebound_call.  */
    8068         1713 :   return gfc_resolve_expr (e);
    8069              : }
    8070              : 
    8071              : 
    8072              : static bool resolve_fl_derived (gfc_symbol *sym);
    8073              : 
    8074              : 
    8075              : /* Fix up the references of the EXPR_COMPCALL or EXPR_PPC E, whose base
    8076              :    object is an inferred type associate name, as for the equivalent
    8077              :    variable.  Look up the type-bound procedure again in the corrected
    8078              :    type.  */
    8079              : 
    8080              : static void
    8081         3276 : fixup_inferred_compcall_refs (gfc_expr *e)
    8082              : {
    8083         3276 :   gfc_symbol *sym, *dt;
    8084         3276 :   gfc_symtree *tbp;
    8085         3276 :   gfc_expr *var;
    8086         3276 :   bool t;
    8087              : 
    8088         3276 :   sym = e->symtree->n.sym;
    8089         3276 :   if (!sym->assoc || !sym->assoc->inferred_type
    8090           74 :       || (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS))
    8091         3208 :     return;
    8092              : 
    8093           74 :   if (e->ref)
    8094              :     {
    8095           42 :       var = gfc_get_expr ();
    8096           42 :       var->expr_type = EXPR_VARIABLE;
    8097           42 :       var->symtree = e->symtree;
    8098           42 :       var->ref = e->ref;
    8099           42 :       var->ts = sym->ts;
    8100           42 :       var->where = e->where;
    8101           42 :       gfc_fixup_inferred_type_refs (var);
    8102           42 :       e->ref = var->ref;
    8103           42 :       var->ref = NULL;
    8104           42 :       gfc_free_expr (var);
    8105              :     }
    8106              : 
    8107           74 :   if (e->expr_type != EXPR_COMPCALL || !e->value.compcall.name)
    8108              :     return;
    8109              : 
    8110           68 :   dt = get_declared_from_expr (NULL, NULL, e, true);
    8111           68 :   if (dt->attr.is_class && dt->components)
    8112           48 :     dt = dt->components->ts.u.derived;
    8113              : 
    8114           68 :   tbp = gfc_find_typebound_proc (dt, &t, e->value.compcall.name, false,
    8115              :                                  &e->where);
    8116           68 :   if (tbp && t)
    8117           68 :     e->value.compcall.tbp = tbp->n.tb;
    8118              : }
    8119              : 
    8120              : 
    8121              : /* Resolve a typebound function, or 'method'. First separate all
    8122              :    the non-CLASS references by calling resolve_compcall directly.  */
    8123              : 
    8124              : static bool
    8125         1723 : resolve_typebound_function (gfc_expr* e)
    8126              : {
    8127         1723 :   gfc_symbol *declared;
    8128         1723 :   gfc_component *c;
    8129         1723 :   gfc_ref *new_ref;
    8130         1723 :   gfc_ref *class_ref;
    8131         1723 :   gfc_symtree *st;
    8132         1723 :   const char *name;
    8133         1723 :   gfc_typespec ts;
    8134         1723 :   gfc_expr *expr;
    8135         1723 :   bool overridable;
    8136              : 
    8137         1723 :   st = e->symtree;
    8138              : 
    8139              :   /* Deal with typebound operators for CLASS objects.  */
    8140         1723 :   expr = e->value.compcall.base_object;
    8141         1723 :   overridable = !e->value.compcall.tbp->non_overridable;
    8142         1723 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    8143              :     {
    8144              :       /* Since the typebound operators are generic, we have to ensure
    8145              :          that any delays in resolution are corrected and that the vtab
    8146              :          is present.  */
    8147          184 :       ts = expr->ts;
    8148          184 :       declared = ts.u.derived;
    8149          184 :       if (!resolve_fl_derived (declared))
    8150              :         return false;
    8151              : 
    8152          184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    8153          184 :       if (c->ts.u.derived == NULL)
    8154            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8155              : 
    8156          184 :       if (!resolve_compcall (e, &name))
    8157              :         return false;
    8158              : 
    8159              :       /* Use the generic name if it is there.  */
    8160          184 :       name = name ? name : e->value.function.esym->name;
    8161          184 :       e->symtree = expr->symtree;
    8162          184 :       e->ref = gfc_copy_ref (expr->ref);
    8163          184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    8164              : 
    8165              :       /* Trim away the extraneous references that emerge from nested
    8166              :          use of interface.cc (extend_expr).  */
    8167          184 :       if (class_ref && class_ref->next)
    8168              :         {
    8169            0 :           gfc_free_ref_list (class_ref->next);
    8170            0 :           class_ref->next = NULL;
    8171              :         }
    8172          184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    8173              :         {
    8174            0 :           gfc_free_ref_list (e->ref);
    8175            0 :           e->ref = NULL;
    8176              :         }
    8177              : 
    8178          184 :       gfc_add_vptr_component (e);
    8179          184 :       gfc_add_component_ref (e, name);
    8180          184 :       e->value.function.esym = NULL;
    8181          184 :       if (expr->expr_type != EXPR_VARIABLE)
    8182           80 :         e->base_expr = expr;
    8183              :       return true;
    8184              :     }
    8185              : 
    8186         1539 :   if (st == NULL)
    8187          195 :     return resolve_compcall (e, NULL);
    8188              : 
    8189         1344 :   fixup_inferred_compcall_refs (e);
    8190              : 
    8191         1344 :   if (!gfc_resolve_ref (e))
    8192              :     return false;
    8193              : 
    8194              :   /* It can happen that a generic, typebound procedure is marked as overridable
    8195              :      with all of the specific procedures being non-overridable. If this is the
    8196              :      case, it is safe to resolve the compcall.  */
    8197         1344 :   if (!expr && overridable
    8198         1336 :       && e->value.compcall.tbp->is_generic
    8199          210 :       && e->value.compcall.tbp->u.generic->specific
    8200          209 :       && e->value.compcall.tbp->u.generic->specific->non_overridable)
    8201              :     {
    8202              :       gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
    8203            6 :       for (; g; g = g->next)
    8204            4 :         if (!g->specific->non_overridable)
    8205              :           break;
    8206            2 :       if (g == NULL && resolve_compcall (e, &name))
    8207              :         return true;
    8208              :     }
    8209              : 
    8210              :   /* Get the CLASS declared type.  */
    8211         1342 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    8212              : 
    8213         1342 :   if (!resolve_fl_derived (declared))
    8214              :     return false;
    8215              : 
    8216              :   /* Weed out cases of the ultimate component being a derived type.  */
    8217         1342 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8218         1236 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8219              :     {
    8220          626 :       gfc_free_ref_list (new_ref);
    8221          626 :       return resolve_compcall (e, NULL);
    8222              :     }
    8223              : 
    8224          716 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    8225              : 
    8226              :   /* Treat the call as if it is a typebound procedure, in order to roll
    8227              :      out the correct name for the specific function.  */
    8228          716 :   if (!resolve_compcall (e, &name))
    8229              :     {
    8230            3 :       gfc_free_ref_list (new_ref);
    8231            3 :       return false;
    8232              :     }
    8233          713 :   ts = e->ts;
    8234              : 
    8235          713 :   if (overridable)
    8236              :     {
    8237              :       /* Convert the expression to a procedure pointer component call.  */
    8238          711 :       e->value.function.esym = NULL;
    8239          711 :       e->symtree = st;
    8240              : 
    8241          711 :       if (new_ref)
    8242          155 :         e->ref = new_ref;
    8243              : 
    8244              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8245          711 :       gfc_add_vptr_component (e);
    8246          711 :       gfc_add_component_ref (e, name);
    8247              : 
    8248              :       /* Recover the typespec for the expression.  This is really only
    8249              :         necessary for generic procedures, where the additional call
    8250              :         to gfc_add_component_ref seems to throw the collection of the
    8251              :         correct typespec.  */
    8252          711 :       e->ts = ts;
    8253              :     }
    8254            2 :   else if (new_ref)
    8255            0 :     gfc_free_ref_list (new_ref);
    8256              : 
    8257              :   return true;
    8258              : }
    8259              : 
    8260              : /* Resolve a typebound subroutine, or 'method'. First separate all
    8261              :    the non-CLASS references by calling resolve_typebound_call
    8262              :    directly.  */
    8263              : 
    8264              : static bool
    8265         1798 : resolve_typebound_subroutine (gfc_code *code)
    8266              : {
    8267         1798 :   gfc_symbol *declared;
    8268         1798 :   gfc_component *c;
    8269         1798 :   gfc_ref *new_ref;
    8270         1798 :   gfc_ref *class_ref;
    8271         1798 :   gfc_symtree *st;
    8272         1798 :   const char *name;
    8273         1798 :   gfc_typespec ts;
    8274         1798 :   gfc_expr *expr;
    8275         1798 :   bool overridable;
    8276              : 
    8277         1798 :   st = code->expr1->symtree;
    8278              : 
    8279              :   /* Deal with typebound operators for CLASS objects.  */
    8280         1798 :   expr = code->expr1->value.compcall.base_object;
    8281         1798 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    8282         1798 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    8283              :     {
    8284              :       /* If the base_object is not a variable, the corresponding actual
    8285              :          argument expression must be stored in e->base_expression so
    8286              :          that the corresponding tree temporary can be used as the base
    8287              :          object in gfc_conv_procedure_call.  */
    8288          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8289              :         {
    8290              :           gfc_actual_arglist *args;
    8291              : 
    8292              :           args= code->expr1->value.function.actual;
    8293              :           for (; args; args = args->next)
    8294              :             if (expr == args->expr)
    8295              :               expr = args->expr;
    8296              :         }
    8297              : 
    8298              :       /* Since the typebound operators are generic, we have to ensure
    8299              :          that any delays in resolution are corrected and that the vtab
    8300              :          is present.  */
    8301          109 :       declared = expr->ts.u.derived;
    8302          109 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    8303          109 :       if (c->ts.u.derived == NULL)
    8304            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8305              : 
    8306          109 :       if (!resolve_typebound_call (code, &name, NULL))
    8307              :         return false;
    8308              : 
    8309              :       /* Use the generic name if it is there.  */
    8310          109 :       name = name ? name : code->expr1->value.function.esym->name;
    8311          109 :       code->expr1->symtree = expr->symtree;
    8312          109 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    8313              : 
    8314              :       /* Trim away the extraneous references that emerge from nested
    8315              :          use of interface.cc (extend_expr).  */
    8316          109 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    8317          109 :       if (class_ref && class_ref->next)
    8318              :         {
    8319            0 :           gfc_free_ref_list (class_ref->next);
    8320            0 :           class_ref->next = NULL;
    8321              :         }
    8322          109 :       else if (code->expr1->ref && !class_ref)
    8323              :         {
    8324           18 :           gfc_free_ref_list (code->expr1->ref);
    8325           18 :           code->expr1->ref = NULL;
    8326              :         }
    8327              : 
    8328              :       /* Now use the procedure in the vtable.  */
    8329          109 :       gfc_add_vptr_component (code->expr1);
    8330          109 :       gfc_add_component_ref (code->expr1, name);
    8331          109 :       code->expr1->value.function.esym = NULL;
    8332          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8333            0 :         code->expr1->base_expr = expr;
    8334              :       return true;
    8335              :     }
    8336              : 
    8337         1689 :   if (st == NULL)
    8338          364 :     return resolve_typebound_call (code, NULL, NULL);
    8339              : 
    8340         1325 :   fixup_inferred_compcall_refs (code->expr1);
    8341              : 
    8342         1325 :   if (!gfc_resolve_ref (code->expr1))
    8343              :     return false;
    8344              : 
    8345              :   /* Get the CLASS declared type.  */
    8346         1325 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    8347              : 
    8348              :   /* Weed out cases of the ultimate component being a derived type.  */
    8349         1325 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8350         1260 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8351              :     {
    8352          937 :       gfc_free_ref_list (new_ref);
    8353          937 :       return resolve_typebound_call (code, NULL, NULL);
    8354              :     }
    8355              : 
    8356          388 :   if (!resolve_typebound_call (code, &name, &overridable))
    8357              :     {
    8358            5 :       gfc_free_ref_list (new_ref);
    8359            5 :       return false;
    8360              :     }
    8361          383 :   ts = code->expr1->ts;
    8362              : 
    8363          383 :   if (overridable)
    8364              :     {
    8365              :       /* Convert the expression to a procedure pointer component call.  */
    8366          381 :       code->expr1->value.function.esym = NULL;
    8367          381 :       code->expr1->symtree = st;
    8368              : 
    8369          381 :       if (new_ref)
    8370           99 :         code->expr1->ref = new_ref;
    8371              : 
    8372              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8373          381 :       gfc_add_vptr_component (code->expr1);
    8374          381 :       gfc_add_component_ref (code->expr1, name);
    8375              : 
    8376              :       /* Recover the typespec for the expression.  This is really only
    8377              :         necessary for generic procedures, where the additional call
    8378              :         to gfc_add_component_ref seems to throw the collection of the
    8379              :         correct typespec.  */
    8380          381 :       code->expr1->ts = ts;
    8381              :     }
    8382            2 :   else if (new_ref)
    8383            0 :     gfc_free_ref_list (new_ref);
    8384              : 
    8385              :   return true;
    8386              : }
    8387              : 
    8388              : 
    8389              : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    8390              : 
    8391              : static bool
    8392          124 : resolve_ppc_call (gfc_code* c)
    8393              : {
    8394          124 :   gfc_component *comp;
    8395              : 
    8396          124 :   fixup_inferred_compcall_refs (c->expr1);
    8397              : 
    8398          124 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    8399          124 :   gcc_assert (comp != NULL);
    8400              : 
    8401          124 :   c->resolved_sym = c->expr1->symtree->n.sym;
    8402          124 :   c->expr1->expr_type = EXPR_VARIABLE;
    8403              : 
    8404          124 :   if (!comp->attr.subroutine)
    8405            1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    8406              : 
    8407          124 :   if (!gfc_resolve_ref (c->expr1))
    8408              :     return false;
    8409              : 
    8410          124 :   if (!update_ppc_arglist (c->expr1))
    8411              :     return false;
    8412              : 
    8413          123 :   c->ext.actual = c->expr1->value.compcall.actual;
    8414              : 
    8415          123 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    8416          123 :                                !(comp->ts.interface
    8417           93 :                                  && comp->ts.interface->formal)))
    8418              :     return false;
    8419              : 
    8420          123 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    8421              :     return false;
    8422              : 
    8423          122 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    8424              : 
    8425          122 :   return true;
    8426              : }
    8427              : 
    8428              : 
    8429              : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    8430              : 
    8431              : static bool
    8432          483 : resolve_expr_ppc (gfc_expr* e)
    8433              : {
    8434          483 :   gfc_component *comp;
    8435              : 
    8436          483 :   fixup_inferred_compcall_refs (e);
    8437              : 
    8438          483 :   comp = gfc_get_proc_ptr_comp (e);
    8439          483 :   gcc_assert (comp != NULL);
    8440              : 
    8441              :   /* Convert to EXPR_FUNCTION.  */
    8442          483 :   e->expr_type = EXPR_FUNCTION;
    8443          483 :   e->value.function.isym = NULL;
    8444          483 :   e->value.function.actual = e->value.compcall.actual;
    8445          483 :   e->ts = comp->ts;
    8446          483 :   if (comp->as != NULL)
    8447              :     {
    8448           28 :       e->rank = comp->as->rank;
    8449           28 :       e->corank = comp->as->corank;
    8450              :     }
    8451              : 
    8452          483 :   if (!comp->attr.function)
    8453            3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    8454              : 
    8455          483 :   if (!gfc_resolve_ref (e))
    8456              :     return false;
    8457              : 
    8458          483 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    8459          483 :                                !(comp->ts.interface
    8460          482 :                                  && comp->ts.interface->formal)))
    8461              :     return false;
    8462              : 
    8463          483 :   if (!update_ppc_arglist (e))
    8464              :     return false;
    8465              : 
    8466          481 :   if (!check_pure_function(e))
    8467              :     return false;
    8468              : 
    8469          480 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    8470              : 
    8471          480 :   return true;
    8472              : }
    8473              : 
    8474              : 
    8475              : static bool
    8476        12953 : gfc_is_expandable_expr (gfc_expr *e)
    8477              : {
    8478        12953 :   gfc_constructor *con;
    8479              : 
    8480        12953 :   if (e->expr_type == EXPR_ARRAY)
    8481              :     {
    8482              :       /* Traverse the constructor looking for variables that are flavor
    8483              :          parameter.  Parameters must be expanded since they are fully used at
    8484              :          compile time.  */
    8485        12953 :       con = gfc_constructor_first (e->value.constructor);
    8486        34179 :       for (; con; con = gfc_constructor_next (con))
    8487              :         {
    8488        14924 :           if (con->expr->expr_type == EXPR_VARIABLE
    8489         5764 :               && con->expr->symtree
    8490         5764 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8491         5764 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8492              :             return true;
    8493         9160 :           if (con->expr->expr_type == EXPR_ARRAY
    8494         9160 :               && gfc_is_expandable_expr (con->expr))
    8495              :             return true;
    8496              :         }
    8497              :     }
    8498              : 
    8499              :   return false;
    8500              : }
    8501              : 
    8502              : 
    8503              : /* Sometimes variables in specification expressions of the result
    8504              :    of module procedures in submodules wind up not being the 'real'
    8505              :    dummy.  Find this, if possible, in the namespace of the first
    8506              :    formal argument.  */
    8507              : 
    8508              : static void
    8509         4767 : fixup_unique_dummy (gfc_expr *e)
    8510              : {
    8511         4767 :   gfc_symtree *st = NULL;
    8512         4767 :   gfc_symbol *s = NULL;
    8513              : 
    8514         4767 :   if (e->symtree->n.sym->ns->proc_name
    8515         4767 :       && e->symtree->n.sym->ns->proc_name->formal)
    8516         4767 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8517              : 
    8518         4767 :   if (s != NULL)
    8519         4767 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8520              : 
    8521         4767 :   if (st != NULL
    8522           14 :       && st->n.sym != NULL
    8523           14 :       && st->n.sym->attr.dummy)
    8524           14 :     e->symtree = st;
    8525         4767 : }
    8526              : 
    8527              : 
    8528              : /* Resolve an expression.  That is, make sure that types of operands agree
    8529              :    with their operators, intrinsic operators are converted to function calls
    8530              :    for overloaded types and unresolved function references are resolved.  */
    8531              : 
    8532              : bool
    8533      7234080 : gfc_resolve_expr (gfc_expr *e)
    8534              : {
    8535      7234080 :   bool t;
    8536      7234080 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8537              : 
    8538      7234080 :   if (e == NULL || e->do_not_resolve_again)
    8539              :     return true;
    8540              : 
    8541              :   /* inquiry_argument only applies to variables.  */
    8542      5241442 :   inquiry_save = inquiry_argument;
    8543      5241442 :   actual_arg_save = actual_arg;
    8544      5241442 :   first_actual_arg_save = first_actual_arg;
    8545              : 
    8546      5241442 :   if (e->expr_type != EXPR_VARIABLE)
    8547              :     {
    8548      3868253 :       inquiry_argument = false;
    8549      3868253 :       actual_arg = false;
    8550      3868253 :       first_actual_arg = false;
    8551              :     }
    8552      1373189 :   else if (e->symtree != NULL
    8553      1372696 :            && *e->symtree->name == '@'
    8554         5501 :            && e->symtree->n.sym->attr.dummy)
    8555              :     {
    8556              :       /* Deal with submodule specification expressions that are not
    8557              :          found to be referenced in module.cc(read_cleanup).  */
    8558         4767 :       fixup_unique_dummy (e);
    8559              :     }
    8560              : 
    8561      5241442 :   switch (e->expr_type)
    8562              :     {
    8563       547630 :     case EXPR_OP:
    8564       547630 :       t = resolve_operator (e);
    8565       547630 :       break;
    8566              : 
    8567          170 :     case EXPR_CONDITIONAL:
    8568          170 :       t = resolve_conditional (e);
    8569          170 :       break;
    8570              : 
    8571      1730282 :     case EXPR_FUNCTION:
    8572      1730282 :     case EXPR_VARIABLE:
    8573              : 
    8574      1730282 :       if (check_host_association (e))
    8575       357129 :         t = resolve_function (e);
    8576              :       else
    8577      1373153 :         t = resolve_variable (e);
    8578              : 
    8579      1730282 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref)
    8580         7595 :         gfc_resolve_substring_charlen (e);
    8581              : 
    8582              :       break;
    8583              : 
    8584         1723 :     case EXPR_COMPCALL:
    8585         1723 :       t = resolve_typebound_function (e);
    8586         1723 :       break;
    8587              : 
    8588          511 :     case EXPR_SUBSTRING:
    8589          511 :       t = gfc_resolve_ref (e);
    8590          511 :       break;
    8591              : 
    8592              :     case EXPR_CONSTANT:
    8593              :     case EXPR_NULL:
    8594              :       t = true;
    8595              :       break;
    8596              : 
    8597          483 :     case EXPR_PPC:
    8598          483 :       t = resolve_expr_ppc (e);
    8599          483 :       break;
    8600              : 
    8601        76493 :     case EXPR_ARRAY:
    8602        76493 :       t = false;
    8603        76493 :       if (!gfc_resolve_ref (e))
    8604              :         break;
    8605              : 
    8606        76493 :       t = gfc_resolve_array_constructor (e);
    8607              :       /* Also try to expand a constructor.  */
    8608        76493 :       if (t)
    8609              :         {
    8610        76388 :           gfc_expression_rank (e);
    8611        76388 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8612        71421 :             gfc_expand_constructor (e, false);
    8613              :         }
    8614              : 
    8615              :       /* This provides the opportunity for the length of constructors with
    8616              :          character valued function elements to propagate the string length
    8617              :          to the expression.  */
    8618        76388 :       if (t && e->ts.type == BT_CHARACTER)
    8619              :         {
    8620              :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    8621              :              here rather then add a duplicate test for it above.  */
    8622        11373 :           gfc_expand_constructor (e, false);
    8623        11373 :           t = gfc_resolve_character_array_constructor (e);
    8624              :         }
    8625              : 
    8626              :       break;
    8627              : 
    8628        17783 :     case EXPR_STRUCTURE:
    8629        17783 :       t = gfc_resolve_ref (e);
    8630        17783 :       if (!t)
    8631              :         break;
    8632              : 
    8633        17783 :       t = resolve_structure_cons (e, 0);
    8634        17783 :       if (!t)
    8635              :         break;
    8636              : 
    8637        17771 :       t = gfc_simplify_expr (e, 0);
    8638        17771 :       break;
    8639              : 
    8640            0 :     default:
    8641            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8642              :     }
    8643              : 
    8644      5241442 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8645       183413 :     fixup_charlen (e);
    8646              : 
    8647      5241442 :   inquiry_argument = inquiry_save;
    8648      5241442 :   actual_arg = actual_arg_save;
    8649      5241442 :   first_actual_arg = first_actual_arg_save;
    8650              : 
    8651              :   /* For some reason, resolving these expressions a second time mangles
    8652              :      the typespec of the expression itself.  */
    8653      5241442 :   if (t && e->expr_type == EXPR_VARIABLE
    8654      1370322 :       && e->symtree->n.sym->attr.select_rank_temporary
    8655         3482 :       && UNLIMITED_POLY (e->symtree->n.sym))
    8656           83 :     e->do_not_resolve_again = 1;
    8657              : 
    8658      5238842 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8659         7726 :     t = check_import_status (e);
    8660              : 
    8661              :   return t;
    8662              : }
    8663              : 
    8664              : 
    8665              : /* Resolve an expression from an iterator.  They must be scalar and have
    8666              :    INTEGER or (optionally) REAL type.  */
    8667              : 
    8668              : static bool
    8669       159101 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8670              :                            const char *name_msgid)
    8671              : {
    8672       159101 :   if (!gfc_resolve_expr (expr))
    8673              :     return false;
    8674              : 
    8675       159096 :   if (expr->rank != 0)
    8676              :     {
    8677            0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    8678            0 :       return false;
    8679              :     }
    8680              : 
    8681       159096 :   if (expr->ts.type != BT_INTEGER)
    8682              :     {
    8683          317 :       if (expr->ts.type == BT_REAL)
    8684              :         {
    8685          317 :           if (real_ok)
    8686          314 :             return gfc_notify_std (GFC_STD_F95_DEL,
    8687              :                                    "%s at %L must be integer",
    8688          314 :                                    _(name_msgid), &expr->where);
    8689              :           else
    8690              :             {
    8691            3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    8692              :                          &expr->where);
    8693            3 :               return false;
    8694              :             }
    8695              :         }
    8696              :       else
    8697              :         {
    8698            0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    8699            0 :           return false;
    8700              :         }
    8701              :     }
    8702              :   return true;
    8703              : }
    8704              : 
    8705              : 
    8706              : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    8707              :    false allow only INTEGER type iterators, otherwise allow REAL types.
    8708              :    Set own_scope to true for ac-implied-do and data-implied-do as those
    8709              :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    8710              : 
    8711              : bool
    8712        39784 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8713              : {
    8714        39784 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8715              :     return false;
    8716              : 
    8717        39780 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8718        39780 :                                  _("iterator variable")))
    8719              :     return false;
    8720              : 
    8721        39774 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8722              :                                   "Start expression in DO loop"))
    8723              :     return false;
    8724              : 
    8725        39773 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8726              :                                   "End expression in DO loop"))
    8727              :     return false;
    8728              : 
    8729        39770 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8730              :                                   "Step expression in DO loop"))
    8731              :     return false;
    8732              : 
    8733              :   /* Convert start, end, and step to the same type as var.  */
    8734        39769 :   if (iter->start->ts.kind != iter->var->ts.kind
    8735        39424 :       || iter->start->ts.type != iter->var->ts.type)
    8736          409 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8737              : 
    8738        39769 :   if (iter->end->ts.kind != iter->var->ts.kind
    8739        39451 :       || iter->end->ts.type != iter->var->ts.type)
    8740          361 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8741              : 
    8742        39769 :   if (iter->step->ts.kind != iter->var->ts.kind
    8743        39461 :       || iter->step->ts.type != iter->var->ts.type)
    8744          374 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8745              : 
    8746        39769 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8747              :     {
    8748        38576 :       if ((iter->step->ts.type == BT_INTEGER
    8749        38463 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8750        77037 :           || (iter->step->ts.type == BT_REAL
    8751          113 :               && mpfr_sgn (iter->step->value.real) == 0))
    8752              :         {
    8753            3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8754            3 :                      &iter->step->where);
    8755            3 :           return false;
    8756              :         }
    8757              :     }
    8758              : 
    8759        39766 :   if (iter->start->expr_type == EXPR_CONSTANT
    8760        36567 :       && iter->end->expr_type == EXPR_CONSTANT
    8761        28432 :       && iter->step->expr_type == EXPR_CONSTANT)
    8762              :     {
    8763        28146 :       int sgn, cmp;
    8764        28146 :       if (iter->start->ts.type == BT_INTEGER)
    8765              :         {
    8766        28091 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8767        28091 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8768              :         }
    8769              :       else
    8770              :         {
    8771           55 :           sgn = mpfr_sgn (iter->step->value.real);
    8772           55 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8773              :         }
    8774        28146 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8775          146 :         gfc_warning (OPT_Wzerotrip,
    8776              :                      "DO loop at %L will be executed zero times",
    8777          146 :                      &iter->step->where);
    8778              :     }
    8779              : 
    8780        39766 :   if (iter->end->expr_type == EXPR_CONSTANT
    8781        28855 :       && iter->end->ts.type == BT_INTEGER
    8782        28800 :       && iter->step->expr_type == EXPR_CONSTANT
    8783        28420 :       && iter->step->ts.type == BT_INTEGER
    8784        28420 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8785        28042 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8786              :     {
    8787        27263 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8788        27263 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8789              : 
    8790        27263 :       if (is_step_positive
    8791        26885 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8792            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8793              :                      "DO loop at %L is undefined as it overflows",
    8794            7 :                      &iter->step->where);
    8795              :       else if (!is_step_positive
    8796          378 :                && mpz_cmp (iter->end->value.integer,
    8797          378 :                            gfc_integer_kinds[k].min_int) == 0)
    8798            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8799              :                      "DO loop at %L is undefined as it underflows",
    8800            7 :                      &iter->step->where);
    8801              :     }
    8802              : 
    8803        39766 :   gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8804              :                           VALUE_USED);
    8805        39766 :   gfc_value_used_expr (iter->start, VALUE_USED);
    8806        39766 :   gfc_value_used_expr (iter->end, VALUE_USED);
    8807        39766 :   gfc_value_used_expr (iter->step, VALUE_USED);
    8808              : 
    8809        39766 :   return true;
    8810              : }
    8811              : 
    8812              : 
    8813              : /* Traversal function for find_forall_index.  f == 2 signals that
    8814              :    that variable itself is not to be checked - only the references.  */
    8815              : 
    8816              : static bool
    8817        43392 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8818              : {
    8819        43392 :   if (expr->expr_type != EXPR_VARIABLE)
    8820              :     return false;
    8821              : 
    8822              :   /* A scalar assignment  */
    8823        18455 :   if (!expr->ref || *f == 1)
    8824              :     {
    8825        12339 :       if (expr->symtree->n.sym == sym)
    8826              :         return true;
    8827              :       else
    8828         8269 :         return false;
    8829              :     }
    8830              : 
    8831         6116 :   if (*f == 2)
    8832         1731 :     *f = 1;
    8833              :   return false;
    8834              : }
    8835              : 
    8836              : 
    8837              : /* Check whether the FORALL index appears in the expression or not.
    8838              :    Returns true if SYM is found in EXPR.  */
    8839              : 
    8840              : bool
    8841        27704 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8842              : {
    8843        27704 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8844              :     return true;
    8845              :   else
    8846              :     return false;
    8847              : }
    8848              : 
    8849              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8850              :    This constraint specifies rules for variables in locality-specs.  */
    8851              : 
    8852              : static int
    8853         1239 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8854              : {
    8855         1239 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8856              : 
    8857         1239 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8858              :     {
    8859           64 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8860           64 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8861           66 :            list; list = list->next)
    8862              :         {
    8863            5 :           if (list->expr->symtree->n.sym == sym)
    8864              :             {
    8865            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8866              :                          "must not appear in LOCAL locality-spec at %L",
    8867              :                          sym->name, &(*expr)->where, &list->expr->where);
    8868            3 :               *walk_subtrees = 0;
    8869            3 :               return 1;
    8870              :             }
    8871              :         }
    8872              :     }
    8873              : 
    8874         1236 :     *walk_subtrees = 1;
    8875         1236 :     return 0;
    8876              : }
    8877              : 
    8878              : static int
    8879         5218 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8880              : {
    8881         5218 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8882              : 
    8883         5218 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8884              :     {
    8885         2554 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8886              : 
    8887         2554 :       if (d->sym_hash->contains (sym))
    8888         1471 :         sym->mark = 1;
    8889              : 
    8890         1083 :       else if (d->default_none)
    8891              :         {
    8892            8 :           gfc_namespace *ns2 = d->ns;
    8893           13 :           while (ns2)
    8894              :             {
    8895            8 :               if (ns2 == sym->ns)
    8896              :                 break;
    8897            5 :               ns2 = ns2->parent;
    8898              :             }
    8899              : 
    8900              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.
    8901              :              Use d->code (the DO CONCURRENT node) rather than sym->ns->code,
    8902              :              which may be a different code type (e.g. EXEC_ASSOCIATE) whose
    8903              :              ext union would be read incorrectly.  */
    8904            8 :           for (gfc_forall_iterator *iter = d->code->ext.concur.forall_iterator;
    8905           17 :                iter; iter = iter->next)
    8906              :             {
    8907           10 :               if (!iter->var || !iter->var->symtree)
    8908            0 :                 continue;
    8909           10 :               const char *iter_name = iter->var->symtree->name;
    8910              :               /* Use the user-visible name of a shadow iterator.  */
    8911           10 :               if (iter->shadow)
    8912            0 :                 iter_name = iter->shadow_outer_sym->name;
    8913           10 :               if (strcmp (sym->name, iter_name) == 0)
    8914            1 :                 return 0;
    8915              :             }
    8916              : 
    8917              :           /* A named constant is not a variable, so skip test.  */
    8918            7 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8919              :             {
    8920            2 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8921              :                         "of DO CONCURRENT at %L but required due to "
    8922              :                         "DEFAULT (NONE)",
    8923              :                         sym->name, &(*e)->where, &d->code->loc);
    8924            2 :               d->sym_hash->add (sym);
    8925              :             }
    8926              :         }
    8927              :     }
    8928              :   return 0;
    8929              : }
    8930              : 
    8931              : static void
    8932          370 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8933              : {
    8934          370 :   struct check_default_none_data data;
    8935          370 :   data.code = code;
    8936          370 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8937          370 :   data.ns = ns;
    8938          370 :   data.default_none = code->ext.concur.default_none;
    8939              : 
    8940         1850 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8941              :     {
    8942         1480 :       const char *name;
    8943         1480 :       switch (locality)
    8944              :         {
    8945              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8946          370 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8947          370 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8948          370 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8949              :           default: gcc_unreachable ();
    8950              :         }
    8951              : 
    8952         2019 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8953          539 :            list = list->next)
    8954              :         {
    8955          539 :           gfc_expr *expr = list->expr;
    8956              : 
    8957          539 :           if (locality == LOCALITY_REDUCE
    8958          294 :               && (expr->expr_type == EXPR_FUNCTION
    8959          196 :                   || expr->expr_type == EXPR_OP))
    8960          109 :             continue;
    8961              : 
    8962          441 :           if (!gfc_resolve_expr (expr))
    8963            3 :             continue;
    8964              : 
    8965          438 :           if (expr->expr_type != EXPR_VARIABLE
    8966          438 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8967          438 :               || (expr->ref
    8968          151 :                   && (expr->ref->type != REF_ARRAY
    8969          151 :                       || expr->ref->u.ar.type != AR_FULL
    8970          147 :                       || expr->ref->next)))
    8971              :             {
    8972            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8973              :                          name, &expr->where);
    8974            4 :                 continue;
    8975              :             }
    8976              : 
    8977          434 :           gfc_symbol *sym = expr->symtree->n.sym;
    8978              : 
    8979          434 :           if (data.sym_hash->contains (sym))
    8980              :             {
    8981            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8982              :                          "locality-spec", sym->name, &expr->where);
    8983            4 :               continue;
    8984              :             }
    8985              : 
    8986          430 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8987          864 :                iter; iter = iter->next)
    8988              :             {
    8989          434 :               if (iter->var->symtree->n.sym == sym)
    8990              :                 {
    8991            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8992              :                              "locality-spec", sym->name, &expr->where);
    8993            1 :                   continue;
    8994              :                 }
    8995              : 
    8996          433 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8997              :             }
    8998              : 
    8999          430 :           if (locality == LOCALITY_LOCAL
    9000          430 :               || locality == LOCALITY_LOCAL_INIT
    9001          430 :               || locality == LOCALITY_REDUCE)
    9002              :             {
    9003          272 :               if (sym->attr.optional)
    9004            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    9005              :                            "locality-spec at %L",
    9006              :                            sym->name, name, &expr->where);
    9007              : 
    9008          272 :               if (sym->attr.dimension
    9009           66 :                   && sym->as
    9010           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    9011            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    9012              :                            "locality-spec at %L",
    9013              :                            sym->name, name, &expr->where);
    9014              : 
    9015          272 :               gfc_check_vardef_context (expr, false, false, false, name);
    9016              :             }
    9017              : 
    9018          272 :           if (locality == LOCALITY_LOCAL
    9019              :               || locality == LOCALITY_LOCAL_INIT)
    9020              :             {
    9021          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    9022              : 
    9023          181 :               if (attr.allocatable)
    9024            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    9025              :                            "locality-spec at %L",
    9026              :                            sym->name, name, &expr->where);
    9027              : 
    9028          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    9029            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    9030              :                            " for %qs in %s locality-spec at %L",
    9031              :                            sym->name, name, &expr->where);
    9032              : 
    9033          177 :               else if (attr.codimension)
    9034            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    9035              :                            "at %L",
    9036              :                            sym->name, name, &expr->where);
    9037              : 
    9038          177 :               else if (expr->ts.type == BT_DERIVED
    9039          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    9040            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    9041              :                            "locality-spec at %L",
    9042              :                            sym->name, name, &expr->where);
    9043              : 
    9044          177 :               else if (gfc_has_ultimate_allocatable (expr))
    9045            4 :                 gfc_error ("Type with ultimate allocatable component not "
    9046              :                            "permitted for %qs in %s locality-spec at %L",
    9047              :                            sym->name, name, &expr->where);
    9048              :             }
    9049              : 
    9050          249 :           else if (locality == LOCALITY_REDUCE)
    9051              :             {
    9052           91 :               if (sym->attr.asynchronous)
    9053            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    9054              :                            "REDUCE locality-spec at %L",
    9055              :                            sym->name, &expr->where);
    9056           91 :               if (sym->attr.volatile_)
    9057            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    9058              :                            "locality-spec at %L", sym->name, &expr->where);
    9059              :             }
    9060              : 
    9061          430 :           data.sym_hash->add (sym);
    9062              :         }
    9063              : 
    9064         1480 :       if (locality == LOCALITY_LOCAL)
    9065              :         {
    9066          370 :           gcc_assert (locality == 0);
    9067              : 
    9068          370 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    9069          771 :                iter; iter = iter->next)
    9070              :             {
    9071          401 :               gfc_expr_walker (&iter->start,
    9072              :                                do_concur_locality_specs_f2023,
    9073              :                                &data);
    9074              : 
    9075          401 :               gfc_expr_walker (&iter->end,
    9076              :                                do_concur_locality_specs_f2023,
    9077              :                                &data);
    9078              : 
    9079          401 :               gfc_expr_walker (&iter->stride,
    9080              :                                do_concur_locality_specs_f2023,
    9081              :                                &data);
    9082              :             }
    9083              : 
    9084          370 :           if (code->expr1)
    9085            7 :             gfc_expr_walker (&code->expr1,
    9086              :                              do_concur_locality_specs_f2023,
    9087              :                              &data);
    9088              :         }
    9089              :     }
    9090              : 
    9091          370 :   gfc_expr *reduce_op = NULL;
    9092              : 
    9093          370 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    9094          566 :        list; list = list->next)
    9095              :     {
    9096          196 :       gfc_expr *expr = list->expr;
    9097              : 
    9098          196 :       if (expr->expr_type != EXPR_VARIABLE)
    9099              :         {
    9100           98 :           reduce_op = expr;
    9101           98 :           continue;
    9102              :         }
    9103              : 
    9104           98 :       if (reduce_op->expr_type == EXPR_OP)
    9105              :         {
    9106           91 :           switch (reduce_op->value.op.op)
    9107              :             {
    9108           91 :               case INTRINSIC_PLUS:
    9109           91 :               case INTRINSIC_TIMES:
    9110           91 :                 if (!gfc_numeric_ts (&expr->ts))
    9111            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    9112            3 :                              "got %s", expr->symtree->n.sym->name,
    9113              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    9114              :                 break;
    9115            0 :               case INTRINSIC_AND:
    9116            0 :               case INTRINSIC_OR:
    9117            0 :               case INTRINSIC_EQV:
    9118            0 :               case INTRINSIC_NEQV:
    9119            0 :                 if (expr->ts.type != BT_LOGICAL)
    9120            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    9121            0 :                              "got %qs", expr->symtree->n.sym->name,
    9122              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    9123              :                 break;
    9124            0 :               default:
    9125            0 :                 gcc_unreachable ();
    9126              :             }
    9127              :         }
    9128              : 
    9129            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    9130              :         {
    9131            7 :           switch (reduce_op->value.function.isym->id)
    9132              :             {
    9133            6 :               case GFC_ISYM_MIN:
    9134            6 :               case GFC_ISYM_MAX:
    9135            6 :                 if (expr->ts.type != BT_INTEGER
    9136              :                     && expr->ts.type != BT_REAL
    9137              :                     && expr->ts.type != BT_CHARACTER)
    9138            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    9139              :                              "in REDUCE with MIN/MAX at %L, got %s",
    9140            2 :                              expr->symtree->n.sym->name, &expr->where,
    9141              :                              gfc_basic_typename (expr->ts.type));
    9142              :                 break;
    9143            1 :               case GFC_ISYM_IAND:
    9144            1 :               case GFC_ISYM_IOR:
    9145            1 :               case GFC_ISYM_IEOR:
    9146            1 :                 if (expr->ts.type != BT_INTEGER)
    9147            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    9148              :                              "IAND/IOR/IEOR at %L, got %s",
    9149            1 :                              expr->symtree->n.sym->name, &expr->where,
    9150              :                              gfc_basic_typename (expr->ts.type));
    9151              :                 break;
    9152            0 :               default:
    9153            0 :                 gcc_unreachable ();
    9154              :             }
    9155              :         }
    9156              : 
    9157              :       else
    9158            0 :         gcc_unreachable ();
    9159              :     }
    9160              : 
    9161         1850 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    9162              :     {
    9163         2019 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    9164          539 :            list = list->next)
    9165              :         {
    9166          539 :           if (list->expr->expr_type == EXPR_VARIABLE)
    9167          441 :             list->expr->symtree->n.sym->mark = 0;
    9168              :         }
    9169              :     }
    9170              : 
    9171          370 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    9172              :                    check_default_none_expr, &data);
    9173              : 
    9174         2220 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    9175              :     {
    9176         1480 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    9177         2019 :       while (*plist)
    9178              :         {
    9179          539 :           gfc_expr *expr = (*plist)->expr;
    9180          539 :           if (expr->expr_type == EXPR_VARIABLE)
    9181              :             {
    9182          441 :               gfc_symbol *sym = expr->symtree->n.sym;
    9183          441 :               if (sym->mark == 0)
    9184              :                 {
    9185           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    9186              :                                "locality-spec at %L is not used",
    9187              :                                sym->name, &expr->where);
    9188           70 :                   gfc_expr_list *tmp = *plist;
    9189           70 :                   *plist = (*plist)->next;
    9190           70 :                   gfc_free_expr (tmp->expr);
    9191           70 :                   free (tmp);
    9192           70 :                   continue;
    9193           70 :                 }
    9194              :             }
    9195          469 :           plist = &((*plist)->next);
    9196              :         }
    9197              :     }
    9198              : 
    9199          740 :   delete data.sym_hash;
    9200          370 : }
    9201              : 
    9202              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    9203              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    9204              :    INTEGERs, and if stride is a constant it must be nonzero.
    9205              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    9206              :    not contain a reference to any index-name in the
    9207              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    9208              : 
    9209              : static void
    9210         2363 : resolve_forall_iterators (gfc_forall_iterator *it)
    9211              : {
    9212         2363 :   gfc_forall_iterator *iter, *iter2;
    9213              : 
    9214         6656 :   for (iter = it; iter; iter = iter->next)
    9215              :     {
    9216         4293 :       if (gfc_resolve_expr (iter->var)
    9217         4293 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    9218            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    9219              :                    &iter->var->where);
    9220              : 
    9221         4293 :       if (gfc_resolve_expr (iter->start)
    9222         4293 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    9223            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    9224              :                    &iter->start->where);
    9225         4293 :       if (iter->var->ts.kind != iter->start->ts.kind)
    9226            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    9227              : 
    9228         4293 :       if (gfc_resolve_expr (iter->end)
    9229         4293 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    9230            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    9231              :                    &iter->end->where);
    9232         4293 :       if (iter->var->ts.kind != iter->end->ts.kind)
    9233            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    9234              : 
    9235         4293 :       if (gfc_resolve_expr (iter->stride))
    9236              :         {
    9237         4293 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    9238            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    9239              :                        &iter->stride->where, "INTEGER");
    9240              : 
    9241         4293 :           if (iter->stride->expr_type == EXPR_CONSTANT
    9242         4271 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    9243            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    9244              :                        &iter->stride->where);
    9245              :         }
    9246         4293 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    9247            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    9248              : 
    9249         4293 :       gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    9250              :                               VALUE_USED);
    9251         4293 :       gfc_value_used_expr (iter->start, VALUE_USED);
    9252         4293 :       gfc_value_used_expr (iter->end, VALUE_USED);
    9253         4293 :       gfc_value_used_expr (iter->stride, VALUE_USED);
    9254              :     }
    9255              : 
    9256         6656 :   for (iter = it; iter; iter = iter->next)
    9257        11442 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    9258              :       {
    9259         7149 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    9260         7147 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    9261        14294 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    9262            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    9263            6 :                      "specification at %L", iter->var->symtree->name,
    9264            6 :                      &iter2->start->where);
    9265              :       }
    9266         2363 : }
    9267              : 
    9268              : 
    9269              : /* Given a pointer to a symbol that is a derived type, see if it's
    9270              :    inaccessible, i.e. if it's defined in another module and the components are
    9271              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    9272              :    inaccessible components are found, nonzero otherwise.  */
    9273              : 
    9274              : static bool
    9275         1367 : derived_inaccessible (gfc_symbol *sym)
    9276              : {
    9277         1367 :   gfc_component *c;
    9278              : 
    9279         1367 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    9280              :     return 1;
    9281              : 
    9282         4035 :   for (c = sym->components; c; c = c->next)
    9283              :     {
    9284              :         /* Prevent an infinite loop through this function.  */
    9285         2681 :         if (c->ts.type == BT_DERIVED
    9286          289 :             && (c->attr.pointer || c->attr.allocatable)
    9287           72 :             && sym == c->ts.u.derived)
    9288           72 :           continue;
    9289              : 
    9290         2609 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    9291              :           return 1;
    9292              :     }
    9293              : 
    9294              :   return 0;
    9295              : }
    9296              : 
    9297              : 
    9298              : /* Resolve the argument of a deallocate expression.  The expression must be
    9299              :    a pointer or a full array.  */
    9300              : 
    9301              : static bool
    9302         8802 : resolve_deallocate_expr (gfc_expr *e)
    9303              : {
    9304         8802 :   symbol_attribute attr;
    9305         8802 :   int allocatable, pointer;
    9306         8802 :   gfc_ref *ref;
    9307         8802 :   gfc_symbol *sym;
    9308         8802 :   gfc_component *c;
    9309         8802 :   bool unlimited;
    9310              : 
    9311         8802 :   if (!gfc_resolve_expr (e))
    9312              :     return false;
    9313              : 
    9314         8802 :   if (e->expr_type != EXPR_VARIABLE)
    9315            0 :     goto bad;
    9316              : 
    9317         8802 :   sym = e->symtree->n.sym;
    9318         8802 :   unlimited = UNLIMITED_POLY(sym);
    9319              : 
    9320         8802 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    9321              :     {
    9322         1713 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    9323         1713 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    9324              :     }
    9325              :   else
    9326              :     {
    9327         7089 :       allocatable = sym->attr.allocatable;
    9328         7089 :       pointer = sym->attr.pointer;
    9329              :     }
    9330        17574 :   for (ref = e->ref; ref; ref = ref->next)
    9331              :     {
    9332         8772 :       switch (ref->type)
    9333              :         {
    9334         6551 :         case REF_ARRAY:
    9335         6551 :           if (ref->u.ar.type != AR_FULL
    9336         6809 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9337          258 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9338              :             allocatable = 0;
    9339              :           break;
    9340              : 
    9341         2221 :         case REF_COMPONENT:
    9342         2221 :           c = ref->u.c.component;
    9343         2221 :           if (c->ts.type == BT_CLASS)
    9344              :             {
    9345          303 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9346          303 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9347              :             }
    9348              :           else
    9349              :             {
    9350         1918 :               allocatable = c->attr.allocatable;
    9351         1918 :               pointer = c->attr.pointer;
    9352              :             }
    9353              :           break;
    9354              : 
    9355              :         case REF_SUBSTRING:
    9356              :         case REF_INQUIRY:
    9357         8772 :           allocatable = 0;
    9358              :           break;
    9359              :         }
    9360              :     }
    9361              : 
    9362         8802 :   attr = gfc_expr_attr (e);
    9363              : 
    9364         8802 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9365              :     {
    9366            3 :     bad:
    9367            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9368              :                  &e->where);
    9369            3 :       return false;
    9370              :     }
    9371              : 
    9372              :   /* F2008, C644.  */
    9373         8799 :   if (gfc_is_coindexed (e))
    9374              :     {
    9375            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9376            1 :       return false;
    9377              :     }
    9378              : 
    9379         8798 :   if (pointer
    9380        11208 :       && !gfc_check_vardef_context (e, true, true, false,
    9381         2410 :                                     _("DEALLOCATE object")))
    9382              :     return false;
    9383         8796 :   if (!gfc_check_vardef_context (e, false, true, false,
    9384         8796 :                                  _("DEALLOCATE object")))
    9385              :     return false;
    9386              : 
    9387              :   return true;
    9388              : }
    9389              : 
    9390              : 
    9391              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9392              : static bool
    9393        48803 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9394              : {
    9395        48803 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9396         2081 :     return true;
    9397              : 
    9398              :   return false;
    9399              : }
    9400              : 
    9401              : bool
    9402        20509 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9403              : {
    9404        20509 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9405              : }
    9406              : 
    9407              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9408              :    of character expressions.  */
    9409              : static bool
    9410        20910 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9411              : {
    9412            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9413              : }
    9414              : 
    9415              : 
    9416              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9417              :    allocated, get the expression node to be initialized afterwards (needed for
    9418              :    derived types with default initializers, and derived types with allocatable
    9419              :    components that need nullification.)  */
    9420              : 
    9421              : gfc_expr *
    9422         6257 : gfc_expr_to_initialize (gfc_expr *e)
    9423              : {
    9424         6257 :   gfc_expr *result;
    9425         6257 :   gfc_ref *ref;
    9426         6257 :   int i;
    9427              : 
    9428         6257 :   result = gfc_copy_expr (e);
    9429              : 
    9430              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9431        12631 :   for (ref = result->ref; ref; ref = ref->next)
    9432         9917 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9433              :       {
    9434         3543 :         if (ref->u.ar.dimen == 0
    9435           89 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9436              :           return result;
    9437              : 
    9438         3454 :         ref->u.ar.type = AR_FULL;
    9439              : 
    9440         7766 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9441         4312 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9442              : 
    9443              :         break;
    9444              :       }
    9445              : 
    9446         6168 :   gfc_free_shape (&result->shape, result->rank);
    9447              : 
    9448              :   /* Recalculate rank, shape, etc.  */
    9449         6168 :   gfc_resolve_expr (result);
    9450         6168 :   return result;
    9451              : }
    9452              : 
    9453              : 
    9454              : /* If the last ref of an expression is an array ref, return a copy of the
    9455              :    expression with that one removed.  Otherwise, a copy of the original
    9456              :    expression.  This is used for allocate-expressions and pointer assignment
    9457              :    LHS, where there may be an array specification that needs to be stripped
    9458              :    off when using gfc_check_vardef_context.  */
    9459              : 
    9460              : static gfc_expr*
    9461        29144 : remove_last_array_ref (gfc_expr* e)
    9462              : {
    9463        29144 :   gfc_expr* e2;
    9464        29144 :   gfc_ref** r;
    9465              : 
    9466        29144 :   e2 = gfc_copy_expr (e);
    9467        37860 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9468        25980 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9469              :       {
    9470        17264 :         gfc_free_ref_list (*r);
    9471        17264 :         *r = NULL;
    9472        17264 :         break;
    9473              :       }
    9474              : 
    9475        29144 :   return e2;
    9476              : }
    9477              : 
    9478              : 
    9479              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9480              :    a source-expr are conformable.  This does not catch all possible
    9481              :    cases; in particular a runtime checking is needed.  */
    9482              : 
    9483              : static bool
    9484         1984 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9485              : {
    9486         1984 :   gfc_ref *tail;
    9487         1984 :   bool scalar;
    9488              : 
    9489         2806 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9490              : 
    9491              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9492              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9493              :      so let's emit a warning if -Wsurprising is given.  */
    9494         1984 :   scalar = !tail || tail->type == REF_COMPONENT;
    9495         1984 :   if (e1->mold && e1->rank > 0
    9496          166 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9497              :     {
    9498           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9499           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9500              :                      "but MOLD= expression at %L has rank %d",
    9501            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9502              :                      &e1->where, e1->rank);
    9503              :       return true;
    9504              :     }
    9505              : 
    9506              :   /* First compare rank.  */
    9507         1954 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9508            2 :       || (!tail && e1->rank != e2->rank))
    9509              :     {
    9510            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9511              :                  "same rank as the allocate-object at %L",
    9512              :                  &e1->where, &e2->where);
    9513            7 :       return false;
    9514              :     }
    9515              : 
    9516         1947 :   if (e1->shape)
    9517              :     {
    9518         1423 :       int i;
    9519         1423 :       mpz_t s;
    9520              : 
    9521         1423 :       mpz_init (s);
    9522              : 
    9523         3290 :       for (i = 0; i < e1->rank; i++)
    9524              :         {
    9525         1429 :           if (tail->u.ar.start[i] == NULL)
    9526              :             break;
    9527              : 
    9528          444 :           if (tail->u.ar.end[i])
    9529              :             {
    9530           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9531           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9532           54 :               mpz_add_ui (s, s, 1);
    9533              :             }
    9534              :           else
    9535              :             {
    9536          390 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9537              :             }
    9538              : 
    9539          444 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9540              :             {
    9541            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9542              :                          "have the same shape", &e1->where, &e2->where);
    9543            0 :               mpz_clear (s);
    9544            0 :               return false;
    9545              :             }
    9546              :         }
    9547              : 
    9548         1423 :       mpz_clear (s);
    9549              :     }
    9550              : 
    9551              :   return true;
    9552              : }
    9553              : 
    9554              : 
    9555              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9556              :    checks to see whether the expression is OK or not.  The expression must
    9557              :    have a trailing array reference that gives the size of the array.  */
    9558              : 
    9559              : static bool
    9560        18359 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9561              : {
    9562        18359 :   int i, pointer, allocatable, dimension, is_abstract;
    9563        18359 :   int codimension;
    9564        18359 :   bool coindexed;
    9565        18359 :   bool unlimited;
    9566        18359 :   symbol_attribute attr;
    9567        18359 :   gfc_ref *ref, *ref2;
    9568        18359 :   gfc_expr *e2;
    9569        18359 :   gfc_array_ref *ar;
    9570        18359 :   gfc_symbol *sym = NULL;
    9571        18359 :   gfc_alloc *a;
    9572        18359 :   gfc_component *c;
    9573        18359 :   bool t;
    9574              : 
    9575              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9576              :      checking of coarrays.  */
    9577        23632 :   for (ref = e->ref; ref; ref = ref->next)
    9578        19085 :     if (ref->next == NULL)
    9579              :       break;
    9580              : 
    9581        18359 :   if (ref && ref->type == REF_ARRAY)
    9582        12581 :     ref->u.ar.in_allocate = true;
    9583              : 
    9584        18359 :   if (!gfc_resolve_expr (e))
    9585            1 :     goto failure;
    9586              : 
    9587              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9588              :      pointer, the next-to-last reference must be a pointer.  */
    9589              : 
    9590        18358 :   ref2 = NULL;
    9591        18358 :   if (e->symtree)
    9592        18358 :     sym = e->symtree->n.sym;
    9593              : 
    9594              :   /* Check whether ultimate component is abstract and CLASS.  */
    9595        36716 :   is_abstract = 0;
    9596              : 
    9597              :   /* Is the allocate-object unlimited polymorphic?  */
    9598        18358 :   unlimited = UNLIMITED_POLY(e);
    9599              : 
    9600        18358 :   if (e->expr_type != EXPR_VARIABLE)
    9601              :     {
    9602            0 :       allocatable = 0;
    9603            0 :       attr = gfc_expr_attr (e);
    9604            0 :       pointer = attr.pointer;
    9605            0 :       dimension = attr.dimension;
    9606            0 :       codimension = attr.codimension;
    9607              :     }
    9608              :   else
    9609              :     {
    9610        18358 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9611              :         {
    9612         3832 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9613         3832 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9614         3832 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9615         3832 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9616         3832 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9617              :         }
    9618              :       else
    9619              :         {
    9620        14526 :           allocatable = sym->attr.allocatable;
    9621        14526 :           pointer = sym->attr.pointer;
    9622        14526 :           dimension = sym->attr.dimension;
    9623        14526 :           codimension = sym->attr.codimension;
    9624              :         }
    9625              : 
    9626        18358 :       coindexed = false;
    9627              : 
    9628        37437 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9629              :         {
    9630        19081 :           switch (ref->type)
    9631              :             {
    9632        14214 :               case REF_ARRAY:
    9633        14214 :                 if (ref->u.ar.codimen > 0)
    9634              :                   {
    9635          866 :                     int n;
    9636         1173 :                     for (n = ref->u.ar.dimen;
    9637         1173 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9638          907 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9639              :                         {
    9640              :                           coindexed = true;
    9641              :                           break;
    9642              :                         }
    9643              :                    }
    9644              : 
    9645        14214 :                 if (ref->next != NULL)
    9646         1635 :                   pointer = 0;
    9647              :                 break;
    9648              : 
    9649         4867 :               case REF_COMPONENT:
    9650              :                 /* F2008, C644.  */
    9651         4867 :                 if (coindexed)
    9652              :                   {
    9653            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9654              :                                &e->where);
    9655            2 :                     goto failure;
    9656              :                   }
    9657              : 
    9658         4865 :                 c = ref->u.c.component;
    9659         4865 :                 if (c->ts.type == BT_CLASS)
    9660              :                   {
    9661         1084 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9662         1084 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9663         1084 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9664         1084 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9665         1084 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9666              :                   }
    9667              :                 else
    9668              :                   {
    9669         3781 :                     allocatable = c->attr.allocatable;
    9670         3781 :                     pointer = c->attr.pointer;
    9671         3781 :                     dimension = c->attr.dimension;
    9672         3781 :                     codimension = c->attr.codimension;
    9673         3781 :                     is_abstract = c->attr.abstract;
    9674              :                   }
    9675              :                 break;
    9676              : 
    9677            0 :               case REF_SUBSTRING:
    9678            0 :               case REF_INQUIRY:
    9679            0 :                 allocatable = 0;
    9680            0 :                 pointer = 0;
    9681            0 :                 break;
    9682              :             }
    9683              :         }
    9684              :     }
    9685              : 
    9686              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9687              :      pointer or an allocatable variable.  */
    9688        18356 :   if (allocatable == 0 && pointer == 0)
    9689              :     {
    9690            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9691              :                  &e->where);
    9692            4 :       goto failure;
    9693              :     }
    9694              : 
    9695              :   /* Some checks for the SOURCE tag.  */
    9696        18352 :   if (code->expr3)
    9697              :     {
    9698              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9699              :          rank as allocate-object".  This would require the MOLD argument to
    9700              :          NULL() as source-expr for subsequent checking.  However, even the
    9701              :          resulting disassociated pointer or unallocated array has no shape that
    9702              :          could be used for SOURCE= or MOLD=.  */
    9703         4118 :       if (code->expr3->expr_type == EXPR_NULL)
    9704              :         {
    9705            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9706              :                      &code->expr3->where);
    9707            4 :           goto failure;
    9708              :         }
    9709              : 
    9710              :       /* Check F03:C631.  */
    9711         4114 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9712              :         {
    9713           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9714           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9715           10 :           goto failure;
    9716              :         }
    9717              : 
    9718              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9719         4104 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9720            7 :         goto failure;
    9721              : 
    9722              :       /* Check F03:C633.  */
    9723         4097 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9724              :         {
    9725            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9726              :                      "shall have the same kind type parameter",
    9727              :                      &e->where, &code->expr3->where);
    9728            1 :           goto failure;
    9729              :         }
    9730              : 
    9731              :       /* Check F2008, C642.  */
    9732         4096 :       if (code->expr3->ts.type == BT_DERIVED
    9733         4096 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9734         1362 :               || (code->expr3->ts.u.derived->from_intmod
    9735              :                      == INTMOD_ISO_FORTRAN_ENV
    9736            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9737              :                      == ISOFORTRAN_LOCK_TYPE)))
    9738              :         {
    9739            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9740              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9741              :                       "allocate-object at %L is a coarray",
    9742            0 :                       &code->expr3->where, &e->where);
    9743            0 :           goto failure;
    9744              :         }
    9745              : 
    9746              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9747              :          allocate-object and source-expr shall have the same values."  */
    9748         4096 :       if (e->ts.type == BT_CHARACTER
    9749          822 :           && !e->ts.deferred
    9750          162 :           && e->ts.u.cl->length
    9751          162 :           && code->expr3->ts.type == BT_CHARACTER
    9752         4258 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9753              :                                      "SOURCE= or MOLD= specifier"))
    9754           17 :             goto failure;
    9755              : 
    9756              :       /* Check TS18508, C702/C703.  */
    9757         4079 :       if (code->expr3->ts.type == BT_DERIVED
    9758         5441 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9759         1362 :               || (code->expr3->ts.u.derived->from_intmod
    9760              :                      == INTMOD_ISO_FORTRAN_ENV
    9761            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9762              :                      == ISOFORTRAN_EVENT_TYPE)))
    9763              :         {
    9764            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9765              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9766              :                       "allocate-object at %L is a coarray",
    9767            0 :                       &code->expr3->where, &e->where);
    9768            0 :           goto failure;
    9769              :         }
    9770              :     }
    9771              : 
    9772              :   /* Check F08:C629.  */
    9773        18313 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9774          159 :       && !code->expr3)
    9775              :     {
    9776            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9777            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9778              :                  "type-spec or source-expr", sym->name, &e->where);
    9779            2 :       goto failure;
    9780              :     }
    9781              : 
    9782              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9783              :      if and only if each allocate-object is a dummy argument for which the
    9784              :      corresponding type parameter is assumed.  */
    9785        18311 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9786          533 :       && code->ext.alloc.ts.u.cl->length != NULL
    9787          518 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9788           23 :       && e->ts.u.cl->length == NULL
    9789            2 :       && e->symtree->n.sym->attr.dummy)
    9790              :     {
    9791            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9792              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9793              :                  "dummy argument with assumed type parameter",
    9794              :                  sym->name, &e->where);
    9795            2 :       goto failure;
    9796              :     }
    9797              : 
    9798              :   /* Check F08:C632.  */
    9799        18309 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9800           60 :       && !UNLIMITED_POLY (e))
    9801              :     {
    9802           36 :       int cmp = 0;
    9803              : 
    9804           36 :       if (!e->ts.u.cl->length)
    9805           15 :         goto failure;
    9806              : 
    9807           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9808           21 :                                   code->ext.alloc.ts.u.cl->length);
    9809           21 :       if (cmp == 1 || cmp == -1)
    9810              :         {
    9811            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9812              :                      "character-length parameter as in the declaration",
    9813              :                      sym->name, &e->where);
    9814            2 :           goto failure;
    9815              :         }
    9816              :     }
    9817              : 
    9818              :   /* In the variable definition context checks, gfc_expr_attr is used
    9819              :      on the expression.  This is fooled by the array specification
    9820              :      present in e, thus we have to eliminate that one temporarily.  */
    9821        18292 :   e2 = remove_last_array_ref (e);
    9822        18292 :   t = true;
    9823        18292 :   if (t && pointer)
    9824         3948 :     t = gfc_check_vardef_context (e2, true, true, false,
    9825         3948 :                                   _("ALLOCATE object"));
    9826         3948 :   if (t)
    9827        18284 :     t = gfc_check_vardef_context (e2, false, true, false,
    9828        18284 :                                   _("ALLOCATE object"));
    9829        18292 :   gfc_free_expr (e2);
    9830        18292 :   if (!t)
    9831           11 :     goto failure;
    9832              : 
    9833        18281 :   code->ext.alloc.expr3_not_explicit = 0;
    9834        18281 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9835         1832 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9836              :     {
    9837              :       /* For class arrays, the initialization with SOURCE is done
    9838              :          using _copy and trans_call. It is convenient to exploit that
    9839              :          when the allocated type is different from the declared type but
    9840              :          no SOURCE exists by setting expr3.  */
    9841          395 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9842          395 :       code->ext.alloc.expr3_not_explicit = 1;
    9843              :     }
    9844        17886 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9845         2847 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9846            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9847              :     {
    9848              :       /* We have to zero initialize the integer variable.  */
    9849            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9850            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9851              :     }
    9852              : 
    9853        18281 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9854              :     {
    9855              :       /* Make sure the vtab symbol is present when
    9856              :          the module variables are generated.  */
    9857         3312 :       gfc_typespec ts = e->ts;
    9858         3312 :       if (code->expr3)
    9859         1465 :         ts = code->expr3->ts;
    9860         1847 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9861          805 :         ts = code->ext.alloc.ts;
    9862              : 
    9863              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9864              :          statement is necessary.  */
    9865         3312 :       gfc_find_derived_vtab (ts.u.derived);
    9866         3312 :     }
    9867        14969 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9868              :     {
    9869              :       /* Again, make sure the vtab symbol is present when
    9870              :          the module variables are generated.  */
    9871          524 :       gfc_typespec *ts = NULL;
    9872          524 :       if (code->expr3)
    9873          395 :         ts = &code->expr3->ts;
    9874              :       else
    9875              :         {
    9876          129 :           ts = &code->ext.alloc.ts;
    9877          129 :           if (ts->type == BT_DERIVED && ts->u.derived->attr.pdt_type)
    9878              :             {
    9879           42 :               code->expr3 = gfc_default_initializer (ts);
    9880           42 :               code->expr3->mold = 1;
    9881           42 :               code->ext.alloc.expr3_not_explicit = 1;
    9882              :             }
    9883              :         }
    9884              : 
    9885          395 :       gcc_assert (ts);
    9886              : 
    9887              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9888              :          statement is necessary.  */
    9889          524 :       gfc_find_vtab (ts);
    9890              :     }
    9891              : 
    9892        18281 :   if (dimension == 0 && codimension == 0)
    9893         5731 :     goto success;
    9894              : 
    9895              :   /* Make sure the last reference node is an array specification.  */
    9896              : 
    9897        12550 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9898        11268 :       || (dimension && ref2->u.ar.dimen == 0))
    9899              :     {
    9900              :       /* F08:C633.  */
    9901         1282 :       if (code->expr3)
    9902              :         {
    9903         1281 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9904              :                                "in ALLOCATE statement at %L", &e->where))
    9905            0 :             goto failure;
    9906         1281 :           if (code->expr3->rank != 0)
    9907         1280 :             *array_alloc_wo_spec = true;
    9908              :           else
    9909              :             {
    9910            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9911              :                          "expression required in ALLOCATE statement at %L",
    9912              :                          &e->where);
    9913            1 :               goto failure;
    9914              :             }
    9915              :         }
    9916              :       else
    9917              :         {
    9918            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9919              :                      "at %L", &e->where);
    9920            1 :           goto failure;
    9921              :         }
    9922              :     }
    9923              : 
    9924              :   /* Make sure that the array section reference makes sense in the
    9925              :      context of an ALLOCATE specification.  */
    9926              : 
    9927        12548 :   ar = &ref2->u.ar;
    9928              : 
    9929        12548 :   if (codimension)
    9930         1382 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9931              :       {
    9932          795 :         switch (ar->dimen_type[i])
    9933              :           {
    9934            2 :           case DIMEN_THIS_IMAGE:
    9935            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9936              :                        "at %L", &e->where);
    9937            2 :             goto failure;
    9938              : 
    9939           98 :           case  DIMEN_RANGE:
    9940              :             /* F2018:R937:
    9941              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9942              :              */
    9943           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9944              :               {
    9945            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9946              :                            "at %L", &e->where);
    9947            8 :                 goto failure;
    9948              :               }
    9949           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9950              :               {
    9951            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9952            2 :                            &ar->start[i]->where);
    9953            2 :                 goto failure;
    9954              :               }
    9955              :             break;
    9956              : 
    9957          108 :           case DIMEN_ELEMENT:
    9958          108 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9959              :               {
    9960          100 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9961          100 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9962              :                   {
    9963            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9964              :                                "of 1 at %L", &ar->start[i]->where);
    9965            1 :                     goto failure;
    9966              :                   }
    9967              :               }
    9968              :             break;
    9969              : 
    9970              :           case DIMEN_STAR:
    9971              :             break;
    9972              : 
    9973            0 :           default:
    9974            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9975              :                        &e->where);
    9976            0 :             goto failure;
    9977              : 
    9978              :           }
    9979              :       }
    9980        30478 :   for (i = 0; i < ar->dimen; i++)
    9981              :     {
    9982        17947 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9983        15208 :         goto check_symbols;
    9984              : 
    9985         2739 :       switch (ar->dimen_type[i])
    9986              :         {
    9987              :         case DIMEN_ELEMENT:
    9988              :           break;
    9989              : 
    9990         2473 :         case DIMEN_RANGE:
    9991         2473 :           if (ar->start[i] != NULL
    9992         2473 :               && ar->end[i] != NULL
    9993         2472 :               && ar->stride[i] == NULL)
    9994              :             break;
    9995              : 
    9996              :           /* Fall through.  */
    9997              : 
    9998            1 :         case DIMEN_UNKNOWN:
    9999            1 :         case DIMEN_VECTOR:
   10000            1 :         case DIMEN_STAR:
   10001            1 :         case DIMEN_THIS_IMAGE:
   10002            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
   10003              :                      &e->where);
   10004            1 :           goto failure;
   10005              :         }
   10006              : 
   10007         2472 : check_symbols:
   10008        46576 :       for (a = code->ext.alloc.list; a; a = a->next)
   10009              :         {
   10010        28633 :           sym = a->expr->symtree->n.sym;
   10011              : 
   10012              :           /* TODO - check derived type components.  */
   10013        28633 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
   10014         9765 :             continue;
   10015              : 
   10016        18868 :           if ((ar->start[i] != NULL
   10017        18187 :                && gfc_find_var_in_expr (sym, ar->start[i]))
   10018        37052 :               || (ar->end[i] != NULL
   10019         2723 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
   10020              :             {
   10021            3 :               gfc_error ("%qs must not appear in the array specification at "
   10022              :                          "%L in the same ALLOCATE statement where it is "
   10023              :                          "itself allocated", sym->name, &ar->where);
   10024            3 :               goto failure;
   10025              :             }
   10026              :         }
   10027              :     }
   10028              : 
   10029        12725 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
   10030              :     {
   10031          974 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
   10032          780 :           || ar->dimen_type[i] == DIMEN_RANGE)
   10033              :         {
   10034          194 :           if (i == (ar->dimen + ar->codimen - 1))
   10035              :             {
   10036            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
   10037              :                          "statement at %L", &e->where);
   10038            0 :               goto failure;
   10039              :             }
   10040          194 :           continue;
   10041              :         }
   10042              : 
   10043          586 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
   10044          586 :           && ar->stride[i] == NULL)
   10045              :         break;
   10046              : 
   10047            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
   10048              :                  &e->where);
   10049            0 :       goto failure;
   10050              :     }
   10051              : 
   10052        12531 : success:
   10053        18262 :   gfc_used_in_allocate_expr (e, &e->where, ALLOCATED_ALLOCATE_STMT);
   10054              : 
   10055        18262 :   if (code->expr3)
   10056         4370 :     gfc_value_set_at (e->symtree->n.sym, &code->expr3->where, VALUE_VARDEF);
   10057              : 
   10058              :   return true;
   10059              : 
   10060        18359 : failure:
   10061              :   return false;
   10062              : }
   10063              : 
   10064              : 
   10065              : static void
   10066        21754 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
   10067              : {
   10068        21754 :   gfc_expr *stat, *errmsg, *pe, *qe;
   10069        21754 :   gfc_alloc *a, *p, *q;
   10070              : 
   10071        21754 :   stat = code->expr1;
   10072        21754 :   errmsg = code->expr2;
   10073              : 
   10074              :   /* Check the stat variable.  */
   10075        21754 :   if (stat)
   10076              :     {
   10077          716 :       if (!gfc_check_vardef_context (stat, false, false, false,
   10078          716 :                                      _("STAT variable")))
   10079            8 :           goto done_stat;
   10080              : 
   10081          708 :       if (stat->ts.type != BT_INTEGER
   10082          699 :           || stat->rank > 0)
   10083           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
   10084              :                    "variable", &stat->where);
   10085              : 
   10086          708 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
   10087            0 :         goto done_stat;
   10088              : 
   10089              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
   10090              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
   10091              :        */
   10092         1464 :       for (p = code->ext.alloc.list; p; p = p->next)
   10093          763 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
   10094              :           {
   10095            9 :             gfc_ref *ref1, *ref2;
   10096            9 :             bool found = true;
   10097              : 
   10098           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
   10099            7 :                  ref1 = ref1->next, ref2 = ref2->next)
   10100              :               {
   10101            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
   10102            5 :                   continue;
   10103            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
   10104              :                   {
   10105              :                     found = false;
   10106              :                     break;
   10107              :                   }
   10108              :               }
   10109              : 
   10110            9 :             if (found)
   10111              :               {
   10112            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
   10113              :                            "the same %s statement", &stat->where, fcn, fcn);
   10114            7 :                 break;
   10115              :               }
   10116              :           }
   10117              :     }
   10118              : 
   10119        21038 : done_stat:
   10120              : 
   10121              :   /* Check the errmsg variable.  */
   10122        21754 :   if (errmsg)
   10123              :     {
   10124          169 :       if (!stat)
   10125            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
   10126              :                      &errmsg->where);
   10127              : 
   10128          169 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
   10129          169 :                                      _("ERRMSG variable")))
   10130            6 :           goto done_errmsg;
   10131              : 
   10132              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
   10133              :          F18:R930  errmsg-variable       is scalar-default-char-variable
   10134              :          F18:R906  default-char-variable is variable
   10135              :          F18:C906  default-char-variable shall be default character.  */
   10136          163 :       if (errmsg->ts.type != BT_CHARACTER
   10137          161 :           || errmsg->rank > 0
   10138          160 :           || errmsg->ts.kind != gfc_default_character_kind)
   10139            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
   10140              :                    "variable", &errmsg->where);
   10141              : 
   10142          163 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
   10143            0 :         goto done_errmsg;
   10144              : 
   10145              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
   10146              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
   10147              :        */
   10148          324 :       for (p = code->ext.alloc.list; p; p = p->next)
   10149          166 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
   10150              :           {
   10151            9 :             gfc_ref *ref1, *ref2;
   10152            9 :             bool found = true;
   10153              : 
   10154           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
   10155            7 :                  ref1 = ref1->next, ref2 = ref2->next)
   10156              :               {
   10157           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
   10158            4 :                   continue;
   10159            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
   10160              :                   {
   10161              :                     found = false;
   10162              :                     break;
   10163              :                   }
   10164              :               }
   10165              : 
   10166            9 :             if (found)
   10167              :               {
   10168            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
   10169              :                            "the same %s statement", &errmsg->where, fcn, fcn);
   10170            5 :                 break;
   10171              :               }
   10172              :           }
   10173              :     }
   10174              : 
   10175        21585 : done_errmsg:
   10176              : 
   10177              :   /* Check that an allocate-object appears only once in the statement.  */
   10178              : 
   10179        48915 :   for (p = code->ext.alloc.list; p; p = p->next)
   10180              :     {
   10181        27161 :       pe = p->expr;
   10182        36606 :       for (q = p->next; q; q = q->next)
   10183              :         {
   10184         9445 :           qe = q->expr;
   10185         9445 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
   10186              :             {
   10187              :               /* This is a potential collision.  */
   10188         2094 :               gfc_ref *pr = pe->ref;
   10189         2094 :               gfc_ref *qr = qe->ref;
   10190              : 
   10191              :               /* Follow the references  until
   10192              :                  a) They start to differ, in which case there is no error;
   10193              :                  you can deallocate a%b and a%c in a single statement
   10194              :                  b) Both of them stop, which is an error
   10195              :                  c) One of them stops, which is also an error.  */
   10196         4518 :               while (1)
   10197              :                 {
   10198         3306 :                   if (pr == NULL && qr == NULL)
   10199              :                     {
   10200            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
   10201              :                                  &pe->where, &qe->where);
   10202            7 :                       break;
   10203              :                     }
   10204         3299 :                   else if (pr != NULL && qr == NULL)
   10205              :                     {
   10206            2 :                       gfc_error ("Allocate-object at %L is subobject of"
   10207              :                                  " object at %L", &pe->where, &qe->where);
   10208            2 :                       break;
   10209              :                     }
   10210         3297 :                   else if (pr == NULL && qr != NULL)
   10211              :                     {
   10212            2 :                       gfc_error ("Allocate-object at %L is subobject of"
   10213              :                                  " object at %L", &qe->where, &pe->where);
   10214            2 :                       break;
   10215              :                     }
   10216              :                   /* Here, pr != NULL && qr != NULL  */
   10217         3295 :                   gcc_assert(pr->type == qr->type);
   10218         3295 :                   if (pr->type == REF_ARRAY)
   10219              :                     {
   10220              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
   10221              :                          which are legal.  */
   10222         1065 :                       gcc_assert (qr->type == REF_ARRAY);
   10223              : 
   10224         1065 :                       if (pr->next && qr->next)
   10225              :                         {
   10226              :                           int i;
   10227              :                           gfc_array_ref *par = &(pr->u.ar);
   10228              :                           gfc_array_ref *qar = &(qr->u.ar);
   10229              : 
   10230         1840 :                           for (i=0; i<par->dimen; i++)
   10231              :                             {
   10232          954 :                               if ((par->start[i] != NULL
   10233            0 :                                    || qar->start[i] != NULL)
   10234         1908 :                                   && gfc_dep_compare_expr (par->start[i],
   10235          954 :                                                            qar->start[i]) != 0)
   10236          168 :                                 goto break_label;
   10237              :                             }
   10238              :                         }
   10239              :                     }
   10240              :                   else
   10241              :                     {
   10242         2230 :                       if (pr->u.c.component->name != qr->u.c.component->name)
   10243              :                         break;
   10244              :                     }
   10245              : 
   10246         1212 :                   pr = pr->next;
   10247         1212 :                   qr = qr->next;
   10248         1212 :                 }
   10249         9445 :             break_label:
   10250              :               ;
   10251              :             }
   10252              :         }
   10253              :     }
   10254              : 
   10255        21754 :   if (strcmp (fcn, "ALLOCATE") == 0)
   10256              :     {
   10257        15272 :       bool arr_alloc_wo_spec = false;
   10258              : 
   10259              :       /* Resolve and mark as used the length of the type spec.  */
   10260        15272 :       if (code->ext.alloc.ts.type == BT_CHARACTER)
   10261              :         {
   10262          491 :           gfc_expr *length = code->ext.alloc.ts.u.cl->length;
   10263          491 :           gfc_resolve_expr (length);
   10264          491 :           gfc_value_used_expr (length, VALUE_USED);
   10265              :         }
   10266              : 
   10267              :       /* Resolving the expr3 in the loop over all objects to allocate would
   10268              :          execute loop invariant code for each loop item.  Therefore do it just
   10269              :          once here.  */
   10270        15272 :       if (code->expr3 && code->expr3->mold
   10271          387 :           && code->expr3->ts.type == BT_DERIVED
   10272           54 :           && !IS_PDT (code->expr3) && !IS_CLASS_PDT (code->expr3)
   10273           27 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
   10274              :         {
   10275              :           /* Default initialization via MOLD (non-polymorphic).  */
   10276           25 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
   10277           25 :           if (rhs != NULL)
   10278              :             {
   10279            6 :               gfc_resolve_expr (rhs);
   10280            6 :               gfc_free_expr (code->expr3);
   10281            6 :               code->expr3 = rhs;
   10282              :             }
   10283              :         }
   10284        33631 :       for (a = code->ext.alloc.list; a; a = a->next)
   10285        18359 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
   10286              : 
   10287        15272 :       if (arr_alloc_wo_spec && code->expr3)
   10288              :         {
   10289              :           /* Mark the allocate to have to take the array specification
   10290              :              from the expr3.  */
   10291         1274 :           code->ext.alloc.arr_spec_from_expr3 = 1;
   10292              :         }
   10293              :     }
   10294              :   else
   10295              :     {
   10296        15284 :       for (a = code->ext.alloc.list; a; a = a->next)
   10297         8802 :         resolve_deallocate_expr (a->expr);
   10298              :     }
   10299        21754 : }
   10300              : 
   10301              : 
   10302              : /************ SELECT CASE resolution subroutines ************/
   10303              : 
   10304              : /* Callback function for our mergesort variant.  Determines interval
   10305              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
   10306              :    op1 > op2.  Assumes we're not dealing with the default case.
   10307              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
   10308              :    There are nine situations to check.  */
   10309              : 
   10310              : static int
   10311         1623 : compare_cases (const gfc_case *op1, const gfc_case *op2)
   10312              : {
   10313         1623 :   int retval;
   10314              : 
   10315         1623 :   if (op1->low == NULL) /* op1 = (:L)  */
   10316              :     {
   10317              :       /* op2 = (:N), so overlap.  */
   10318           52 :       retval = 0;
   10319              :       /* op2 = (M:) or (M:N),  L < M  */
   10320           52 :       if (op2->low != NULL
   10321           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10322              :         retval = -1;
   10323              :     }
   10324         1571 :   else if (op1->high == NULL) /* op1 = (K:)  */
   10325              :     {
   10326              :       /* op2 = (M:), so overlap.  */
   10327           10 :       retval = 0;
   10328              :       /* op2 = (:N) or (M:N), K > N  */
   10329           10 :       if (op2->high != NULL
   10330           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10331              :         retval = 1;
   10332              :     }
   10333              :   else /* op1 = (K:L)  */
   10334              :     {
   10335         1561 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
   10336           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10337           18 :                  ? 1 : 0;
   10338         1543 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
   10339           10 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10340           10 :                  ? -1 : 0;
   10341              :       else                      /* op2 = (M:N)  */
   10342              :         {
   10343         1533 :           retval =  0;
   10344              :           /* L < M  */
   10345         1533 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10346              :             retval =  -1;
   10347              :           /* K > N  */
   10348          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10349          438 :             retval =  1;
   10350              :         }
   10351              :     }
   10352              : 
   10353         1623 :   return retval;
   10354              : }
   10355              : 
   10356              : 
   10357              : /* Merge-sort a double linked case list, detecting overlap in the
   10358              :    process.  LIST is the head of the double linked case list before it
   10359              :    is sorted.  Returns the head of the sorted list if we don't see any
   10360              :    overlap, or NULL otherwise.  */
   10361              : 
   10362              : static gfc_case *
   10363          672 : check_case_overlap (gfc_case *list)
   10364              : {
   10365          672 :   gfc_case *p, *q, *e, *tail;
   10366          672 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10367              : 
   10368              :   /* If the passed list was empty, return immediately.  */
   10369          672 :   if (!list)
   10370              :     return NULL;
   10371              : 
   10372              :   overlap_seen = 0;
   10373              :   insize = 1;
   10374              : 
   10375              :   /* Loop unconditionally.  The only exit from this loop is a return
   10376              :      statement, when we've finished sorting the case list.  */
   10377         1404 :   for (;;)
   10378              :     {
   10379         1038 :       p = list;
   10380         1038 :       list = NULL;
   10381         1038 :       tail = NULL;
   10382              : 
   10383              :       /* Count the number of merges we do in this pass.  */
   10384         1038 :       nmerges = 0;
   10385              : 
   10386              :       /* Loop while there exists a merge to be done.  */
   10387         2617 :       while (p)
   10388              :         {
   10389         1579 :           int i;
   10390              : 
   10391              :           /* Count this merge.  */
   10392         1579 :           nmerges++;
   10393              : 
   10394              :           /* Cut the list in two pieces by stepping INSIZE places
   10395              :              forward in the list, starting from P.  */
   10396         1579 :           psize = 0;
   10397         1579 :           q = p;
   10398         3307 :           for (i = 0; i < insize; i++)
   10399              :             {
   10400         2311 :               psize++;
   10401         2311 :               q = q->right;
   10402         2311 :               if (!q)
   10403              :                 break;
   10404              :             }
   10405         1579 :           qsize = insize;
   10406              : 
   10407              :           /* Now we have two lists.  Merge them!  */
   10408         5169 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10409              :             {
   10410              :               /* See from which the next case to merge comes from.  */
   10411          841 :               if (psize == 0)
   10412              :                 {
   10413              :                   /* P is empty so the next case must come from Q.  */
   10414          841 :                   e = q;
   10415          841 :                   q = q->right;
   10416          841 :                   qsize--;
   10417              :                 }
   10418         2749 :               else if (qsize == 0 || q == NULL)
   10419              :                 {
   10420              :                   /* Q is empty.  */
   10421         1126 :                   e = p;
   10422         1126 :                   p = p->right;
   10423         1126 :                   psize--;
   10424              :                 }
   10425              :               else
   10426              :                 {
   10427         1623 :                   cmp = compare_cases (p, q);
   10428         1623 :                   if (cmp < 0)
   10429              :                     {
   10430              :                       /* The whole case range for P is less than the
   10431              :                          one for Q.  */
   10432         1181 :                       e = p;
   10433         1181 :                       p = p->right;
   10434         1181 :                       psize--;
   10435              :                     }
   10436          442 :                   else if (cmp > 0)
   10437              :                     {
   10438              :                       /* The whole case range for Q is greater than
   10439              :                          the case range for P.  */
   10440          438 :                       e = q;
   10441          438 :                       q = q->right;
   10442          438 :                       qsize--;
   10443              :                     }
   10444              :                   else
   10445              :                     {
   10446              :                       /* The cases overlap, or they are the same
   10447              :                          element in the list.  Either way, we must
   10448              :                          issue an error and get the next case from P.  */
   10449              :                       /* FIXME: Sort P and Q by line number.  */
   10450            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10451              :                                  "label at %L", &p->where, &q->where);
   10452            4 :                       overlap_seen = 1;
   10453            4 :                       e = p;
   10454            4 :                       p = p->right;
   10455            4 :                       psize--;
   10456              :                     }
   10457              :                 }
   10458              : 
   10459              :                 /* Add the next element to the merged list.  */
   10460         3590 :               if (tail)
   10461         2552 :                 tail->right = e;
   10462              :               else
   10463              :                 list = e;
   10464         3590 :               e->left = tail;
   10465         3590 :               tail = e;
   10466              :             }
   10467              : 
   10468              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10469              :              they're the same.  */
   10470              :           p = q;
   10471              :         }
   10472         1038 :       tail->right = NULL;
   10473              : 
   10474              :       /* If we have done only one merge or none at all, we've
   10475              :          finished sorting the cases.  */
   10476         1038 :       if (nmerges <= 1)
   10477              :         {
   10478          672 :           if (!overlap_seen)
   10479              :             return list;
   10480              :           else
   10481            4 :             return NULL;
   10482              :         }
   10483              : 
   10484              :       /* Otherwise repeat, merging lists twice the size.  */
   10485          366 :       insize *= 2;
   10486          366 :     }
   10487              : }
   10488              : 
   10489              : 
   10490              : /* Check to see if an expression is suitable for use in a CASE statement.
   10491              :    Makes sure that all case expressions are scalar constants of the same
   10492              :    type.  Return false if anything is wrong.  */
   10493              : 
   10494              : static bool
   10495         3421 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10496              : {
   10497         3421 :   if (e == NULL) return true;
   10498              : 
   10499         3328 :   if (e->ts.type != case_expr->ts.type)
   10500              :     {
   10501            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10502              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10503            4 :       return false;
   10504              :     }
   10505              : 
   10506              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10507              :      the same type as case-expr.  For character type, length differences
   10508              :      are allowed, but the kind type parameters shall be the same.  */
   10509              : 
   10510         3324 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10511              :     {
   10512            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10513              :                  &e->where, case_expr->ts.kind);
   10514            4 :       return false;
   10515              :     }
   10516              : 
   10517              :   /* Convert the case value kind to that of case expression kind,
   10518              :      if needed */
   10519              : 
   10520         3320 :   if (e->ts.kind != case_expr->ts.kind)
   10521           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10522              : 
   10523         3320 :   if (e->rank != 0)
   10524              :     {
   10525            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10526              :                  &e->where);
   10527            0 :       return false;
   10528              :     }
   10529              : 
   10530              :   return true;
   10531              : }
   10532              : 
   10533              : 
   10534              : /* Given a completely parsed select statement, we:
   10535              : 
   10536              :      - Validate all expressions and code within the SELECT.
   10537              :      - Make sure that the selection expression is not of the wrong type.
   10538              :      - Make sure that no case ranges overlap.
   10539              :      - Eliminate unreachable cases and unreachable code resulting from
   10540              :        removing case labels.
   10541              : 
   10542              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10543              :    they are a hassle for code generation, and to prevent that, we just
   10544              :    cut them out here.  This is not necessary for overlapping cases
   10545              :    because they are illegal and we never even try to generate code.
   10546              : 
   10547              :    We have the additional caveat that a SELECT construct could have
   10548              :    been a computed GOTO in the source code. Fortunately we can fairly
   10549              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10550              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10551              :    we have to do is make sure that the case_expr is a scalar integer
   10552              :    expression.  */
   10553              : 
   10554              : static void
   10555          713 : resolve_select (gfc_code *code, bool select_type)
   10556              : {
   10557          713 :   gfc_code *body;
   10558          713 :   gfc_expr *case_expr;
   10559          713 :   gfc_case *cp, *default_case, *tail, *head;
   10560          713 :   int seen_unreachable;
   10561          713 :   int seen_logical;
   10562          713 :   int ncases;
   10563          713 :   bt type;
   10564          713 :   bool t;
   10565              : 
   10566          713 :   if (code->expr1 == NULL)
   10567              :     {
   10568              :       /* This was actually a computed GOTO statement.  */
   10569            5 :       case_expr = code->expr2;
   10570            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10571            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10572              :                    "at %L must be a scalar integer expression",
   10573              :                    &case_expr->where);
   10574              : 
   10575              :       /* Further checking is not necessary because this SELECT was built
   10576              :          by the compiler, so it should always be OK.  Just move the
   10577              :          case_expr from expr2 to expr so that we can handle computed
   10578              :          GOTOs as normal SELECTs from here on.  */
   10579            5 :       code->expr1 = code->expr2;
   10580            5 :       code->expr2 = NULL;
   10581            5 :       gfc_value_used_expr (code->expr1, VALUE_USED);
   10582            5 :       return;
   10583              :     }
   10584              : 
   10585          708 :   case_expr = code->expr1;
   10586          708 :   type = case_expr->ts.type;
   10587              : 
   10588              :   /* F08:C830.  */
   10589          708 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10590            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10591              : 
   10592              :     {
   10593            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10594              :                  &case_expr->where, gfc_typename (case_expr));
   10595              : 
   10596              :       /* Punt. Going on here just produce more garbage error messages.  */
   10597            0 :       return;
   10598              :     }
   10599              : 
   10600              :   /* F08:R842.  */
   10601          708 :   if (!select_type && case_expr->rank != 0)
   10602              :     {
   10603            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10604              :                  "expression", &case_expr->where);
   10605              : 
   10606              :       /* Punt.  */
   10607            1 :       return;
   10608              :     }
   10609              : 
   10610              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10611              :      the case-expr. Later, all expressions will be promoted to the
   10612              :      largest kind of all case-labels.  */
   10613              : 
   10614          707 :   if (type == BT_INTEGER)
   10615         2017 :     for (body = code->block; body; body = body->block)
   10616         2980 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10617              :         {
   10618         1526 :           if (cp->low
   10619         1526 :               && gfc_check_integer_range (cp->low->value.integer,
   10620              :                                           case_expr->ts.kind) != ARITH_OK)
   10621            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10622            6 :                          "not in the range of %s", &cp->low->where,
   10623              :                          gfc_typename (case_expr));
   10624              : 
   10625         1526 :           if (cp->high
   10626         1235 :               && cp->low != cp->high
   10627         1634 :               && gfc_check_integer_range (cp->high->value.integer,
   10628              :                                           case_expr->ts.kind) != ARITH_OK)
   10629            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10630            0 :                          "not in the range of %s", &cp->high->where,
   10631              :                          gfc_typename (case_expr));
   10632              :         }
   10633              : 
   10634              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10635              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10636              :      of case values, and if we find a mismatch, promote case_expr to
   10637              :      the appropriate kind.  */
   10638              : 
   10639          707 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10640              :     {
   10641         2203 :       for (body = code->block; body; body = body->block)
   10642              :         {
   10643              :           /* Walk the case label list.  */
   10644         3241 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10645              :             {
   10646              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10647         1661 :               if (cp->low == NULL && cp->high == NULL)
   10648          299 :                 continue;
   10649              : 
   10650              :               /* Unreachable case ranges are discarded, so ignore.  */
   10651         1317 :               if (cp->low != NULL && cp->high != NULL
   10652         1269 :                   && cp->low != cp->high
   10653         1427 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10654           33 :                 continue;
   10655              : 
   10656         1329 :               if (cp->low != NULL
   10657         1329 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10658           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10659              : 
   10660         1329 :               if (cp->high != NULL
   10661         1329 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10662            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10663              :             }
   10664              :          }
   10665              :     }
   10666              : 
   10667              :   /* Assume there is no DEFAULT case.  */
   10668          707 :   default_case = NULL;
   10669          707 :   head = tail = NULL;
   10670          707 :   ncases = 0;
   10671          707 :   seen_logical = 0;
   10672              : 
   10673         2592 :   for (body = code->block; body; body = body->block)
   10674              :     {
   10675              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10676         1885 :       t = true;
   10677         1885 :       seen_unreachable = 0;
   10678              : 
   10679              :       /* Walk the case label list, making sure that all case labels
   10680              :          are legal.  */
   10681         3957 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10682              :         {
   10683              :           /* Count the number of cases in the whole construct.  */
   10684         2083 :           ncases++;
   10685              : 
   10686              :           /* Intercept the DEFAULT case.  */
   10687         2083 :           if (cp->low == NULL && cp->high == NULL)
   10688              :             {
   10689          369 :               if (default_case != NULL)
   10690              :                 {
   10691            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10692              :                              "by a second DEFAULT CASE at %L",
   10693              :                              &default_case->where, &cp->where);
   10694            0 :                   t = false;
   10695            0 :                   break;
   10696              :                 }
   10697              :               else
   10698              :                 {
   10699          369 :                   default_case = cp;
   10700          369 :                   continue;
   10701              :                 }
   10702              :             }
   10703              : 
   10704              :           /* Deal with single value cases and case ranges.  Errors are
   10705              :              issued from the validation function.  */
   10706         1714 :           if (!validate_case_label_expr (cp->low, case_expr)
   10707         1714 :               || !validate_case_label_expr (cp->high, case_expr))
   10708              :             {
   10709              :               t = false;
   10710              :               break;
   10711              :             }
   10712              : 
   10713         1706 :           if (type == BT_LOGICAL
   10714           78 :               && ((cp->low == NULL || cp->high == NULL)
   10715           76 :                   || cp->low != cp->high))
   10716              :             {
   10717            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10718              :                          "allowed",
   10719            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10720            2 :               t = false;
   10721            2 :               break;
   10722              :             }
   10723              : 
   10724           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10725              :             {
   10726           76 :               int value;
   10727           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10728           76 :               if (value & seen_logical)
   10729              :                 {
   10730            1 :                   gfc_error ("Constant logical value in CASE statement "
   10731              :                              "is repeated at %L",
   10732              :                              &cp->low->where);
   10733            1 :                   t = false;
   10734            1 :                   break;
   10735              :                 }
   10736           75 :               seen_logical |= value;
   10737              :             }
   10738              : 
   10739         1659 :           if (cp->low != NULL && cp->high != NULL
   10740         1612 :               && cp->low != cp->high
   10741         1815 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10742              :             {
   10743           35 :               if (warn_surprising)
   10744            1 :                 gfc_warning (OPT_Wsurprising,
   10745              :                              "Range specification at %L can never be matched",
   10746              :                              &cp->where);
   10747              : 
   10748           35 :               cp->unreachable = 1;
   10749           35 :               seen_unreachable = 1;
   10750              :             }
   10751              :           else
   10752              :             {
   10753              :               /* If the case range can be matched, it can also overlap with
   10754              :                  other cases.  To make sure it does not, we put it in a
   10755              :                  double linked list here.  We sort that with a merge sort
   10756              :                  later on to detect any overlapping cases.  */
   10757         1668 :               if (!head)
   10758              :                 {
   10759          672 :                   head = tail = cp;
   10760          672 :                   head->right = head->left = NULL;
   10761              :                 }
   10762              :               else
   10763              :                 {
   10764          996 :                   tail->right = cp;
   10765          996 :                   tail->right->left = tail;
   10766          996 :                   tail = tail->right;
   10767          996 :                   tail->right = NULL;
   10768              :                 }
   10769              :             }
   10770              :         }
   10771              : 
   10772              :       /* It there was a failure in the previous case label, give up
   10773              :          for this case label list.  Continue with the next block.  */
   10774         1885 :       if (!t)
   10775           11 :         continue;
   10776              : 
   10777              :       /* See if any case labels that are unreachable have been seen.
   10778              :          If so, we eliminate them.  This is a bit of a kludge because
   10779              :          the case lists for a single case statement (label) is a
   10780              :          single forward linked lists.  */
   10781         1874 :       if (seen_unreachable)
   10782              :       {
   10783              :         /* Advance until the first case in the list is reachable.  */
   10784           69 :         while (body->ext.block.case_list != NULL
   10785           69 :                && body->ext.block.case_list->unreachable)
   10786              :           {
   10787           34 :             gfc_case *n = body->ext.block.case_list;
   10788           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10789           34 :             n->next = NULL;
   10790           34 :             gfc_free_case_list (n);
   10791              :           }
   10792              : 
   10793              :         /* Strip all other unreachable cases.  */
   10794           35 :         if (body->ext.block.case_list)
   10795              :           {
   10796            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10797              :               {
   10798            1 :                 if (cp->next->unreachable)
   10799              :                   {
   10800            1 :                     gfc_case *n = cp->next;
   10801            1 :                     cp->next = cp->next->next;
   10802            1 :                     n->next = NULL;
   10803            1 :                     gfc_free_case_list (n);
   10804              :                   }
   10805              :               }
   10806              :           }
   10807              :       }
   10808              :     }
   10809              : 
   10810              :   /* See if there were overlapping cases.  If the check returns NULL,
   10811              :      there was overlap.  In that case we don't do anything.  If head
   10812              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10813              :      then used during code generation for SELECT CASE constructs with
   10814              :      a case expression of a CHARACTER type.  */
   10815          707 :   if (head)
   10816              :     {
   10817          672 :       head = check_case_overlap (head);
   10818              : 
   10819              :       /* Prepend the default_case if it is there.  */
   10820          672 :       if (head != NULL && default_case)
   10821              :         {
   10822          352 :           default_case->left = NULL;
   10823          352 :           default_case->right = head;
   10824          352 :           head->left = default_case;
   10825              :         }
   10826              :     }
   10827              : 
   10828              :   /* Eliminate dead blocks that may be the result if we've seen
   10829              :      unreachable case labels for a block.  */
   10830         2558 :   for (body = code; body && body->block; body = body->block)
   10831              :     {
   10832         1851 :       if (body->block->ext.block.case_list == NULL)
   10833              :         {
   10834              :           /* Cut the unreachable block from the code chain.  */
   10835           34 :           gfc_code *c = body->block;
   10836           34 :           body->block = c->block;
   10837              : 
   10838              :           /* Kill the dead block, but not the blocks below it.  */
   10839           34 :           c->block = NULL;
   10840           34 :           gfc_free_statements (c);
   10841              :         }
   10842              :     }
   10843              : 
   10844              :   /* More than two cases is legal but insane for logical selects.
   10845              :      Issue a warning for it.  */
   10846          707 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10847            0 :     gfc_warning (OPT_Wsurprising,
   10848              :                  "Logical SELECT CASE block at %L has more that two cases",
   10849              :                  &code->loc);
   10850              : 
   10851              :   /* Finally, mark the expression as used.  */
   10852          707 :   gfc_value_used_expr (case_expr, VALUE_USED);
   10853              : }
   10854              : 
   10855              : 
   10856              : /* Check if a derived type is extensible.  */
   10857              : 
   10858              : bool
   10859        25661 : gfc_type_is_extensible (gfc_symbol *sym)
   10860              : {
   10861        25661 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10862        25645 :            || (sym->attr.is_class
   10863         2235 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10864              : }
   10865              : 
   10866              : 
   10867              : static void
   10868              : resolve_types (gfc_namespace *ns);
   10869              : 
   10870              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10871              :    correct as well as possibly the array-spec.  */
   10872              : 
   10873              : static void
   10874        14111 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10875              : {
   10876        14111 :   gfc_expr* target;
   10877              : 
   10878        14111 :   gcc_assert (sym->assoc);
   10879        14111 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10880              : 
   10881        14111 :   if (sym->assoc->target
   10882         8560 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10883          752 :       && sym->assoc->target->symtree
   10884          752 :       && sym->assoc->target->symtree->n.sym
   10885          752 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10886              :     {
   10887           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10888           33 :         sym->ts = sym->assoc->target->ts;
   10889              :       else
   10890              :         {
   10891            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10892            0 :                      sym->assoc->target->symtree->n.sym->name,
   10893            0 :                      &sym->assoc->target->where);
   10894            0 :           return;
   10895              :         }
   10896              :     }
   10897              : 
   10898              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10899              :      case, return.  Resolution will be called later manually again when
   10900              :      this is done.  */
   10901        14111 :   target = sym->assoc->target;
   10902        14111 :   if (!target)
   10903              :     return;
   10904         8560 :   gcc_assert (!sym->assoc->dangling);
   10905              : 
   10906         8560 :   if (resolve_target && !gfc_resolve_expr (target))
   10907              :     return;
   10908              : 
   10909         8555 :   if (sym->assoc->ar)
   10910              :     {
   10911              :       int dim;
   10912              :       gfc_array_ref *ar = sym->assoc->ar;
   10913           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10914              :         {
   10915           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10916           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10917           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10918           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10919            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10920              :                        "remapping of associate name %s at %L",
   10921              :                        sym->name, &sym->declared_at);
   10922              :         }
   10923              :     }
   10924              : 
   10925              :   /* F2023:C1105 - the selector shall be neither a procedure pointer
   10926              :      designator nor a function reference returning a procedure pointer.
   10927              :      Note that calling a procedure pointer is fine; what matters here is
   10928              :      whether the function result is itself a procedure pointer.  */
   10929         8555 :   if (target->expr_type == EXPR_VARIABLE
   10930         8555 :       && gfc_expr_attr (target).proc_pointer)
   10931              :     {
   10932            2 :       gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10933            2 :                  target->symtree->n.sym->name, &target->where);
   10934            2 :       return;
   10935              :     }
   10936              : 
   10937         8553 :   if (target->expr_type == EXPR_FUNCTION)
   10938              :     {
   10939          700 :       gfc_symbol *fsym = target->value.function.esym;
   10940          700 :       gfc_symbol *result = NULL;
   10941              : 
   10942          700 :       if (fsym && fsym->attr.proc_pointer)
   10943              :         /* Called through a procedure pointer, whose own result symbol is
   10944              :            itself; the result is described by the interface instead.  */
   10945            3 :         result = fsym->ts.interface ? fsym->ts.interface->result : NULL;
   10946          697 :       else if (fsym)
   10947          643 :         result = fsym->result;
   10948           54 :       else if (target->symtree && target->symtree->n.sym->ts.interface)
   10949            0 :         result = target->symtree->n.sym->ts.interface->result;
   10950              : 
   10951          646 :       if (result && result->attr.proc_pointer)
   10952              :         {
   10953            4 :           gfc_error ("Selector at %L returns a procedure pointer",
   10954              :                      &target->where);
   10955            4 :           return;
   10956              :         }
   10957              :     }
   10958              : 
   10959              :   /* For variable targets, we get some attributes from the target.  */
   10960         8549 :   if (target->expr_type == EXPR_VARIABLE
   10961         1315 :       || (target->expr_type == EXPR_OP
   10962          317 :           && target->value.op.op == INTRINSIC_PARENTHESES
   10963           86 :           && target->value.op.op1->expr_type == EXPR_VARIABLE))
   10964              :     {
   10965         7307 :       gfc_symbol *tsym, *dsym;
   10966              : 
   10967         7307 :       tsym = target->expr_type == EXPR_VARIABLE ? target->symtree->n.sym :
   10968           73 :                                   target->value.op.op1->symtree->n.sym;
   10969              : 
   10970           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10971            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10972         7308 :           && dsym->attr.flavor == FL_DERIVED)
   10973              :         {
   10974            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10975              :                      tsym->name, &target->where);
   10976            1 :           return;
   10977              :         }
   10978              : 
   10979         7306 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10980              :         {
   10981           73 :           bool is_error = true;
   10982           73 :           if (tsym->attr.function && tsym->result == tsym)
   10983          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10984          137 :               if (tsym == ns->proc_name)
   10985              :                 {
   10986              :                   is_error = false;
   10987              :                   break;
   10988              :                 }
   10989           64 :           if (is_error)
   10990              :             {
   10991           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10992              :                          tsym->name, &target->where);
   10993           13 :               return;
   10994              :             }
   10995              :         }
   10996              : 
   10997         7293 :       if (target->expr_type == EXPR_VARIABLE)
   10998              :         {
   10999         7222 :           sym->attr.asynchronous = tsym->attr.asynchronous;
   11000         7222 :           sym->attr.volatile_ = tsym->attr.volatile_;
   11001              : 
   11002        14444 :           sym->attr.target = tsym->attr.target
   11003         7222 :                              || gfc_expr_attr (target).pointer;
   11004         7222 :           if (is_subref_array (target))
   11005          475 :             sym->attr.subref_array_pointer = 1;
   11006              :         }
   11007              :     }
   11008         1242 :   else if (target->ts.type == BT_PROCEDURE)
   11009              :     {
   11010            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   11011              :                  &target->where);
   11012            0 :       return;
   11013              :     }
   11014              : 
   11015              :   /* The elements of a pointer function result may be spaced by more than
   11016              :      the element size.  */
   11017         8535 :   if (target->expr_type == EXPR_FUNCTION
   11018          696 :       && target->rank > 0
   11019         9231 :       && gfc_expr_attr (target).pointer)
   11020           37 :     sym->attr.subref_array_pointer = 1;
   11021              : 
   11022         8535 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   11023              :     {
   11024              :       /* By now, the type of the target has been fixed up.  */
   11025          444 :       symbol_attribute attr;
   11026              : 
   11027          444 :       if (sym->ts.type == BT_DERIVED
   11028          305 :           && target->ts.type == BT_CLASS
   11029           56 :           && !UNLIMITED_POLY (target))
   11030              :         {
   11031              :           /* Inferred to be derived type but the target has type class.  */
   11032           56 :           sym->ts = CLASS_DATA (target)->ts;
   11033           56 :           if (!sym->as)
   11034           56 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   11035           56 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   11036           56 :           sym->attr.dimension = target->rank ? 1 : 0;
   11037           56 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   11038              :                             target->corank);
   11039           56 :           sym->as = NULL;
   11040              :         }
   11041          388 :       else if (target->ts.type == BT_DERIVED
   11042          249 :                && target->symtree && target->symtree->n.sym
   11043          225 :                && target->symtree->n.sym->ts.type == BT_CLASS
   11044            0 :                && IS_INFERRED_TYPE (target)
   11045            0 :                && target->ref && target->ref->next
   11046            0 :                && target->ref->next->type == REF_ARRAY
   11047            0 :                && !target->ref->next->next)
   11048              :         {
   11049              :           /* A inferred type selector whose symbol has been determined to be
   11050              :              a class array but which only has an array reference. Change the
   11051              :              associate name and the selector to class type.  */
   11052            0 :           sym->ts = target->ts;
   11053            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   11054            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   11055            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   11056              :                             target->corank);
   11057            0 :           sym->as = NULL;
   11058            0 :           target->ts = sym->ts;
   11059              :         }
   11060          388 :       else if ((target->ts.type == BT_DERIVED)
   11061          139 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   11062           67 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   11063              :         /* Confirmed to be either a derived type or misidentified to be a
   11064              :            scalar class object, when the selector is a class array.  */
   11065          255 :         sym->ts = target->ts;
   11066          133 :       else if (sym->assoc->inferred_type
   11067          120 :                && (sym->ts.type == BT_COMPLEX
   11068           78 :                    || sym->ts.type == BT_CHARACTER)
   11069           66 :                && target->ts.type == sym->ts.type
   11070           66 :                && sym->ts.kind != target->ts.kind)
   11071              :         /* The inferred type was set from a %re, %im or %len inquiry on
   11072              :            the associate name with the default kind, before the target's
   11073              :            actual type was known.  Now that the target has been resolved,
   11074              :            update the kind to match.  */
   11075            6 :         sym->ts = target->ts;
   11076              :     }
   11077              : 
   11078              : 
   11079         8535 :   if (target->expr_type == EXPR_NULL)
   11080              :     {
   11081            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   11082            1 :       return;
   11083              :     }
   11084         8534 :   else if (target->ts.type == BT_UNKNOWN)
   11085              :     {
   11086            2 :       gfc_error ("Selector at %L has no type", &target->where);
   11087            2 :       return;
   11088              :     }
   11089              : 
   11090              :   /* Get type if this was not already set.  Note that it can be
   11091              :      some other type than the target in case this is a SELECT TYPE
   11092              :      selector!  So we must not update when the type is already there.  */
   11093         8532 :   if (sym->ts.type == BT_UNKNOWN)
   11094          266 :     sym->ts = target->ts;
   11095              : 
   11096         8532 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   11097              : 
   11098              :   /* See if this is a valid association-to-variable.  */
   11099        17064 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   11100         7222 :                            && !gfc_has_vector_subscript (target))
   11101         8559 :                           || gfc_is_ptr_fcn (target));
   11102              : 
   11103              :   /* A type parameter inquiry is not a variable.  */
   11104         8532 :   if (sym->assoc->variable && target->expr_type == EXPR_VARIABLE)
   11105        16007 :     for (gfc_ref *ref = target->ref; ref; ref = ref->next)
   11106         8836 :       if (ref->type == REF_INQUIRY
   11107           24 :           && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
   11108              :         {
   11109           24 :           sym->assoc->variable = false;
   11110           24 :           break;
   11111              :         }
   11112              : 
   11113              :   /* Finally resolve if this is an array or not.  */
   11114         8532 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   11115          310 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   11116              :     {
   11117          212 :       gfc_expression_rank (target);
   11118          212 :       if (target->ts.type == BT_DERIVED
   11119          140 :           && !sym->as
   11120          140 :           && target->symtree->n.sym->as)
   11121              :         {
   11122            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   11123            0 :           sym->attr.dimension = 1;
   11124              :         }
   11125          212 :       else if (target->ts.type == BT_CLASS
   11126           72 :                && CLASS_DATA (target)->as)
   11127              :         {
   11128            0 :           target->rank = CLASS_DATA (target)->as->rank;
   11129            0 :           target->corank = CLASS_DATA (target)->as->corank;
   11130            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   11131              :             {
   11132            0 :               sym->ts = target->ts;
   11133            0 :               sym->attr.dimension = 0;
   11134              :             }
   11135              :         }
   11136              :     }
   11137              : 
   11138              : 
   11139         8532 :   if (sym->attr.dimension && target->rank == 0)
   11140              :     {
   11141              :       /* primary.cc makes the assumption that a reference to an associate
   11142              :          name followed by a left parenthesis is an array reference.  */
   11143           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   11144              :         {
   11145           12 :           gfc_expression_rank (sym->assoc->target);
   11146           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   11147           12 :           if (!sym->attr.dimension && sym->as)
   11148            0 :             sym->as = NULL;
   11149              :         }
   11150              : 
   11151           17 :       if (sym->attr.dimension && target->rank == 0)
   11152              :         {
   11153            5 :           if (sym->ts.type != BT_CHARACTER)
   11154            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   11155              :                        sym->name, &sym->declared_at);
   11156            5 :           sym->attr.dimension = 0;
   11157            5 :           return;
   11158              :         }
   11159              :     }
   11160              : 
   11161              :   /* We cannot deal with class selectors that need temporaries.  */
   11162         8527 :   if (target->ts.type == BT_CLASS
   11163         8527 :         && gfc_ref_needs_temporary_p (target->ref))
   11164              :     {
   11165            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   11166              :                  "yet implemented", &target->where);
   11167            1 :       return;
   11168              :     }
   11169              : 
   11170         8526 :   if (target->ts.type == BT_CLASS)
   11171         3160 :     gfc_fix_class_refs (target);
   11172              : 
   11173         8526 :   if ((target->rank > 0 || target->corank > 0)
   11174         3092 :       && !sym->attr.select_rank_temporary)
   11175              :     {
   11176         3092 :       gfc_array_spec *as;
   11177              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   11178              :          it is corrected now.  */
   11179         3092 :       if (sym->ts.type != BT_CLASS
   11180         2396 :           && (!sym->as || sym->as->corank != target->corank))
   11181              :         {
   11182          187 :           if (!sym->as)
   11183          180 :             sym->as = gfc_get_array_spec ();
   11184          187 :           as = sym->as;
   11185          187 :           as->rank = target->rank;
   11186          187 :           as->type = AS_DEFERRED;
   11187          187 :           as->corank = target->corank;
   11188          187 :           sym->attr.dimension = 1;
   11189          187 :           if (as->corank != 0)
   11190            7 :             sym->attr.codimension = 1;
   11191              :         }
   11192         2905 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   11193          695 :                && (!CLASS_DATA (sym)->as
   11194          689 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   11195              :         {
   11196            6 :           if (!CLASS_DATA (sym)->as)
   11197            6 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   11198            6 :           as = CLASS_DATA (sym)->as;
   11199            6 :           as->rank = target->rank;
   11200            6 :           as->type = AS_DEFERRED;
   11201            6 :           as->corank = target->corank;
   11202            6 :           CLASS_DATA (sym)->attr.dimension = 1;
   11203            6 :           if (as->corank != 0)
   11204            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   11205              :         }
   11206              :     }
   11207         5434 :   else if (!sym->attr.select_rank_temporary)
   11208              :     {
   11209              :       /* target's rank is 0, but the type of the sym is still array valued,
   11210              :          which has to be corrected.  */
   11211         3997 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   11212          817 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   11213              :         {
   11214           24 :           gfc_array_spec *as;
   11215           24 :           symbol_attribute attr;
   11216              :           /* The associated variable's type is still the array type
   11217              :              correct this now.  */
   11218           24 :           gfc_typespec *ts = &target->ts;
   11219           24 :           gfc_ref *ref;
   11220              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   11221              :            */
   11222           24 :           bool internal_ref = true;
   11223              : 
   11224           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   11225              :             {
   11226           48 :               switch (ref->type)
   11227              :                 {
   11228           24 :                 case REF_COMPONENT:
   11229           24 :                   ts = &ref->u.c.component->ts;
   11230           24 :                   internal_ref
   11231           24 :                     = target->ref == ref && ref->next
   11232           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   11233              :                   break;
   11234           24 :                 case REF_ARRAY:
   11235           24 :                   if (ts->type == BT_CLASS)
   11236            0 :                     ts = &ts->u.derived->components->ts;
   11237           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   11238            0 :                     for (int i = ref->u.ar.dimen;
   11239              :                          internal_ref
   11240            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   11241              :                          ++i)
   11242            0 :                       internal_ref
   11243            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   11244              :                   break;
   11245              :                 default:
   11246              :                   break;
   11247              :                 }
   11248              :             }
   11249              :           /* Only rewrite the type of this symbol, when the refs are not the
   11250              :              internal ones for class and co-array this-image.  */
   11251           24 :           if (!internal_ref)
   11252              :             {
   11253              :               /* Create a scalar instance of the current class type.  Because
   11254              :                  the rank of a class array goes into its name, the type has to
   11255              :                  be rebuilt.  The alternative of (re-)setting just the
   11256              :                  attributes and as in the current type, destroys the type also
   11257              :                  in other places.  */
   11258            0 :               as = NULL;
   11259            0 :               sym->ts = *ts;
   11260            0 :               sym->ts.type = BT_CLASS;
   11261            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   11262            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   11263            0 :               sym->as = NULL;
   11264              :             }
   11265              :         }
   11266              :     }
   11267              : 
   11268              :   /* Mark this as an associate variable.  */
   11269         8526 :   sym->attr.associate_var = 1;
   11270              : 
   11271              :   /* Fix up the type-spec for CHARACTER types.  */
   11272         8526 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   11273              :     {
   11274          563 :       gfc_ref *ref;
   11275          853 :       for (ref = target->ref; ref; ref = ref->next)
   11276          316 :         if (ref->type == REF_SUBSTRING
   11277           74 :             && (ref->u.ss.start == NULL
   11278           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   11279           74 :                 || ref->u.ss.end == NULL
   11280           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   11281              :           break;
   11282              : 
   11283          563 :       if (!sym->ts.u.cl)
   11284          182 :         sym->ts.u.cl = target->ts.u.cl;
   11285              : 
   11286          563 :       if (sym->ts.deferred
   11287          231 :           && sym->ts.u.cl == target->ts.u.cl)
   11288              :         {
   11289          116 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   11290          116 :           sym->ts.deferred = 1;
   11291              :         }
   11292              : 
   11293          563 :       if (!sym->ts.u.cl->length
   11294          357 :           && !sym->ts.deferred
   11295          126 :           && target->expr_type == EXPR_CONSTANT)
   11296              :         {
   11297           30 :           sym->ts.u.cl->length =
   11298           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   11299           30 :                                   target->value.character.length);
   11300              :         }
   11301          533 :       else if (((!sym->ts.u.cl->length
   11302          206 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   11303          333 :                 && target->expr_type != EXPR_VARIABLE)
   11304          403 :                || ref)
   11305              :         {
   11306          156 :           if (!sym->ts.deferred)
   11307              :             {
   11308           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   11309           45 :               sym->ts.deferred = 1;
   11310              :             }
   11311              : 
   11312              :           /* This is reset in trans-stmt.cc after the assignment
   11313              :              of the target expression to the associate name.  */
   11314          156 :           if (ref && sym->as)
   11315           26 :             sym->attr.pointer = 1;
   11316              :           else
   11317          130 :             sym->attr.allocatable = 1;
   11318              :         }
   11319              :     }
   11320              : 
   11321         8526 :   if (sym->ts.type == BT_CLASS
   11322         1670 :       && IS_INFERRED_TYPE (target)
   11323           19 :       && target->ts.type == BT_DERIVED
   11324            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   11325            0 :       && target->ref && target->ref->next && !target->ref->next->next
   11326            0 :       && target->ref->next->type == REF_ARRAY)
   11327            0 :     target->ts = target->symtree->n.sym->ts;
   11328              : 
   11329              :   /* If the target is a good class object, so is the associate variable.  */
   11330         8526 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   11331         1441 :     sym->attr.class_ok = 1;
   11332              : 
   11333              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   11334         8526 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   11335            4 :     sym->attr.contiguous = 1;
   11336              : }
   11337              : 
   11338              : 
   11339              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   11340              :    array reference, where necessary.  The symbols are artificial and so
   11341              :    the dimension attribute and arrayspec can also be set.  In addition,
   11342              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   11343              :    This is corrected here as well.*/
   11344              : 
   11345              : static void
   11346         1887 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   11347              :                  gfc_ref *ref)
   11348              : {
   11349         1887 :   gfc_ref *nref = (*expr1)->ref;
   11350         1887 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   11351         1887 :   gfc_symbol *sym2;
   11352         1887 :   gfc_expr *selector = gfc_copy_expr (expr2);
   11353              : 
   11354         1887 :   (*expr1)->rank = rank;
   11355         1887 :   (*expr1)->corank = corank;
   11356         1887 :   if (selector)
   11357              :     {
   11358          384 :       gfc_resolve_expr (selector);
   11359          384 :       if (selector->expr_type == EXPR_OP
   11360            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   11361            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   11362          382 :       else if (selector->expr_type == EXPR_VARIABLE
   11363           13 :                || selector->expr_type == EXPR_FUNCTION)
   11364          382 :         sym2 = selector->symtree->n.sym;
   11365              :       else
   11366            0 :         gcc_unreachable ();
   11367              :     }
   11368              :   else
   11369              :     sym2 = NULL;
   11370              : 
   11371         1887 :   if (sym1->ts.type == BT_CLASS)
   11372              :     {
   11373         1887 :       if ((*expr1)->ts.type != BT_CLASS)
   11374           19 :         (*expr1)->ts = sym1->ts;
   11375              : 
   11376         1887 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   11377         1887 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   11378         1887 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   11379            1 :         CLASS_DATA (sym1)->as
   11380            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   11381              :     }
   11382              :   else
   11383              :     {
   11384            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   11385            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   11386            0 :       if (sym1->as == NULL && sym2)
   11387            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   11388              :     }
   11389              : 
   11390         3384 :   for (; nref; nref = nref->next)
   11391         3000 :     if (nref->next == NULL)
   11392              :       break;
   11393              : 
   11394         1887 :   if (ref && nref && nref->type != REF_ARRAY)
   11395            6 :     nref->next = gfc_copy_ref (ref);
   11396         1881 :   else if (ref && !nref)
   11397          369 :     (*expr1)->ref = gfc_copy_ref (ref);
   11398         1512 :   else if (ref && nref->u.ar.codimen != corank)
   11399              :     {
   11400          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   11401          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11402           61 :       nref->u.ar.codimen = corank;
   11403              :     }
   11404         1887 : }
   11405              : 
   11406              : 
   11407              : static gfc_expr *
   11408         7406 : build_loc_call (gfc_expr *sym_expr)
   11409              : {
   11410         7406 :   gfc_expr *loc_call;
   11411         7406 :   loc_call = gfc_get_expr ();
   11412         7406 :   loc_call->expr_type = EXPR_FUNCTION;
   11413         7406 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11414         7406 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11415         7406 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11416         7406 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11417         7406 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11418         7406 :   loc_call->ts.type = BT_INTEGER;
   11419         7406 :   loc_call->ts.kind = gfc_index_integer_kind;
   11420         7406 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11421         7406 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11422         7406 :   loc_call->value.function.actual->expr = sym_expr;
   11423         7406 :   loc_call->where = sym_expr->where;
   11424         7406 :   return loc_call;
   11425              : }
   11426              : 
   11427              : /* Resolve a SELECT TYPE statement.  */
   11428              : 
   11429              : static void
   11430         3340 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11431              : {
   11432         3340 :   gfc_symbol *selector_type;
   11433         3340 :   gfc_code *body, *new_st, *if_st, *tail;
   11434         3340 :   gfc_code *class_is = NULL, *default_case = NULL;
   11435         3340 :   gfc_case *c;
   11436         3340 :   gfc_symtree *st;
   11437         3340 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11438         3340 :   gfc_namespace *ns;
   11439         3340 :   int error = 0;
   11440         3340 :   int rank = 0, corank = 0;
   11441         3340 :   gfc_ref* ref = NULL;
   11442         3340 :   gfc_expr *selector_expr = NULL;
   11443         3340 :   gfc_code *old_code = code;
   11444              : 
   11445         3340 :   ns = code->ext.block.ns;
   11446         3340 :   if (code->expr2)
   11447              :     {
   11448              :       /* Set this, or coarray checks in resolve will fail.  */
   11449          757 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11450              :     }
   11451         3340 :   gfc_resolve (ns);
   11452              : 
   11453              :   /* Check for F03:C813.  */
   11454         3340 :   if (code->expr1->ts.type != BT_CLASS
   11455           42 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11456              :     {
   11457           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11458              :                  "at %L", &code->loc);
   11459           42 :       return;
   11460              :     }
   11461              : 
   11462              :   /* Prevent segfault, when class type is not initialized due to previous
   11463              :      error.  */
   11464         3327 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11465         3325 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11466              :     return;
   11467              : 
   11468         3320 :   if (code->expr2)
   11469              :     {
   11470          748 :       gfc_ref *ref2 = NULL;
   11471         1733 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11472          985 :          if (ref->type == REF_COMPONENT
   11473          501 :              && ref->u.c.component->ts.type == BT_CLASS)
   11474          985 :            ref2 = ref;
   11475              : 
   11476          748 :       if (ref2)
   11477              :         {
   11478          401 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11479            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11480          401 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11481              :         }
   11482              :       else
   11483              :         {
   11484          347 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11485           34 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11486              :           /* Sometimes the selector expression is given the typespec of the
   11487              :              '_data' field, which is logical enough but inappropriate here. */
   11488          347 :           if (code->expr2->ts.type == BT_DERIVED
   11489           73 :               && code->expr2->symtree
   11490           73 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11491           73 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11492          347 :           selector_type = CLASS_DATA (code->expr2)
   11493              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11494              :         }
   11495              : 
   11496          748 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11497              :         {
   11498          364 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11499          364 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11500          364 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11501              :         }
   11502              : 
   11503              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11504          748 :       if (gfc_is_coindexed (code->expr2))
   11505              :         {
   11506            4 :           gfc_error ("Selector at %L must not be coindexed",
   11507            4 :                      &code->expr2->where);
   11508            4 :           return;
   11509              :         }
   11510              : 
   11511              :     }
   11512              :   else
   11513              :     {
   11514         2572 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11515              : 
   11516         2572 :       if (gfc_is_coindexed (code->expr1))
   11517              :         {
   11518            0 :           gfc_error ("Selector at %L must not be coindexed",
   11519            0 :                      &code->expr1->where);
   11520            0 :           return;
   11521              :         }
   11522              :     }
   11523              : 
   11524              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11525         9219 :   for (body = code->block; body; body = body->block)
   11526              :     {
   11527         5904 :       c = body->ext.block.case_list;
   11528              : 
   11529         5904 :       if (!error)
   11530              :         {
   11531              :           /* Check for repeated cases.  */
   11532         9105 :           for (tail = code->block; tail; tail = tail->block)
   11533              :             {
   11534         9105 :               gfc_case *d = tail->ext.block.case_list;
   11535         9105 :               if (tail == body)
   11536              :                 break;
   11537              : 
   11538         3210 :               if (c->ts.type == d->ts.type
   11539          546 :                   && ((c->ts.type == BT_DERIVED
   11540          424 :                        && c->ts.u.derived && d->ts.u.derived
   11541          424 :                        && !strcmp (c->ts.u.derived->name,
   11542              :                                    d->ts.u.derived->name))
   11543          545 :                       || c->ts.type == BT_UNKNOWN
   11544          545 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11545           55 :                           && c->ts.kind == d->ts.kind)))
   11546              :                 {
   11547            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11548              :                              &c->where, &d->where);
   11549            1 :                   return;
   11550              :                 }
   11551              :             }
   11552              :         }
   11553              : 
   11554              :       /* Check F03:C815.  */
   11555         3742 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11556         2546 :           && selector_type
   11557         2546 :           && !selector_type->attr.unlimited_polymorphic
   11558         8071 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11559              :         {
   11560            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11561            1 :                      c->ts.u.derived->name, &c->where);
   11562            1 :           error++;
   11563            1 :           continue;
   11564              :         }
   11565              : 
   11566              :       /* Check F03:C816.  */
   11567         5908 :       if (c->ts.type != BT_UNKNOWN
   11568         4106 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11569         8073 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11570         2167 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11571              :         {
   11572            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11573            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11574            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11575              :           else
   11576            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11577              :                        gfc_basic_typename (c->ts.type), &c->where);
   11578            6 :           error++;
   11579            6 :           continue;
   11580              :         }
   11581              : 
   11582              :       /* Check F03:C814.  */
   11583         5896 :       if (c->ts.type == BT_CHARACTER
   11584          772 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11585              :         {
   11586            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11587              :                      "type parameter is assumed", &c->where);
   11588            0 :           error++;
   11589            0 :           continue;
   11590              :         }
   11591              : 
   11592              :       /* Intercept the DEFAULT case.  */
   11593         5896 :       if (c->ts.type == BT_UNKNOWN)
   11594              :         {
   11595              :           /* Check F03:C818.  */
   11596         1796 :           if (default_case)
   11597              :             {
   11598            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11599              :                          "by a second DEFAULT CASE at %L",
   11600            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11601            1 :               error++;
   11602            1 :               continue;
   11603              :             }
   11604              : 
   11605              :           default_case = body;
   11606              :         }
   11607              :     }
   11608              : 
   11609         3315 :   if (error > 0)
   11610              :     return;
   11611              : 
   11612              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11613              :      target if present.  If there are any EXIT statements referring to the
   11614              :      SELECT TYPE construct, this is no problem because the gfc_code
   11615              :      reference stays the same and EXIT is equally possible from the BLOCK
   11616              :      it is changed to.  */
   11617         3312 :   code->op = EXEC_BLOCK;
   11618         3312 :   if (code->expr2)
   11619              :     {
   11620          744 :       gfc_association_list* assoc;
   11621              : 
   11622          744 :       assoc = gfc_get_association_list ();
   11623          744 :       assoc->st = code->expr1->symtree;
   11624          744 :       assoc->target = gfc_copy_expr (code->expr2);
   11625          744 :       assoc->target->where = code->expr2->where;
   11626              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11627              : 
   11628          744 :       code->ext.block.assoc = assoc;
   11629          744 :       code->expr1->symtree->n.sym->assoc = assoc;
   11630              : 
   11631          744 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11632              :     }
   11633              :   else
   11634         2568 :     code->ext.block.assoc = NULL;
   11635              : 
   11636              :   /* Ensure that the selector rank and arrayspec are available to
   11637              :      correct expressions in which they might be missing.  */
   11638         3312 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11639              :     {
   11640          384 :       rank = code->expr2->rank;
   11641          384 :       corank = code->expr2->corank;
   11642          704 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11643          689 :         if (ref->next == NULL)
   11644              :           break;
   11645          384 :       if (ref && ref->type == REF_ARRAY)
   11646          369 :         ref = gfc_copy_ref (ref);
   11647              : 
   11648              :       /* Fixup expr1 if necessary.  */
   11649          384 :       if (rank || corank)
   11650          384 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11651              :     }
   11652         2928 :   else if (code->expr1->rank || code->expr1->corank)
   11653              :     {
   11654          941 :       rank = code->expr1->rank;
   11655          941 :       corank = code->expr1->corank;
   11656          941 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11657          941 :         if (ref->next == NULL)
   11658              :           break;
   11659          941 :       if (ref && ref->type == REF_ARRAY)
   11660          941 :         ref = gfc_copy_ref (ref);
   11661              :     }
   11662              : 
   11663         3312 :   gfc_expr *orig_expr1 = code->expr1;
   11664              : 
   11665              :   /* Add EXEC_SELECT to switch on type.  */
   11666         3312 :   new_st = gfc_get_code (code->op);
   11667         3312 :   new_st->expr1 = code->expr1;
   11668         3312 :   new_st->expr2 = code->expr2;
   11669         3312 :   new_st->block = code->block;
   11670         3312 :   code->expr1 = code->expr2 =  NULL;
   11671         3312 :   code->block = NULL;
   11672         3312 :   if (!ns->code)
   11673         3312 :     ns->code = new_st;
   11674              :   else
   11675            0 :     ns->code->next = new_st;
   11676         3312 :   code = new_st;
   11677         3312 :   code->op = EXEC_SELECT_TYPE;
   11678              : 
   11679              :   /* Use the intrinsic LOC function to generate an integer expression
   11680              :      for the vtable of the selector.  Note that the rank of the selector
   11681              :      expression has to be set to zero.  */
   11682         3312 :   gfc_add_vptr_component (code->expr1);
   11683         3312 :   code->expr1->rank = 0;
   11684         3312 :   code->expr1->corank = 0;
   11685         3312 :   code->expr1 = build_loc_call (code->expr1);
   11686         3312 :   selector_expr = code->expr1->value.function.actual->expr;
   11687              : 
   11688              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11689         9200 :   for (body = code->block; body; body = body->block)
   11690              :     {
   11691         5888 :       gfc_symbol *vtab;
   11692         5888 :       c = body->ext.block.case_list;
   11693              : 
   11694              :       /* Generate an index integer expression for address of the
   11695              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11696              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11697         5888 :       if (c->ts.type != BT_UNKNOWN)
   11698              :         {
   11699         4094 :           gfc_expr *e;
   11700         4094 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11701              :             {
   11702         2537 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11703         2537 :               gcc_assert (vtab);
   11704         2537 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11705         2537 :                                           c->ts.u.derived->hash_value);
   11706              :             }
   11707              :           else
   11708              :             {
   11709         1557 :               vtab = gfc_find_vtab (&c->ts);
   11710         1557 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11711         1557 :               e = CLASS_DATA (vtab)->initializer;
   11712         1557 :               c->high = gfc_copy_expr (e);
   11713         1557 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11714              :                 {
   11715            1 :                   gfc_typespec ts;
   11716            1 :                   ts.kind = gfc_integer_4_kind;
   11717            1 :                   ts.type = BT_INTEGER;
   11718            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11719              :                 }
   11720              :             }
   11721              : 
   11722         4094 :           e = gfc_lval_expr_from_sym (vtab);
   11723         4094 :           c->low = build_loc_call (e);
   11724              :         }
   11725              :       else
   11726         1794 :         continue;
   11727              : 
   11728              :       /* Associate temporary to selector.  This should only be done
   11729              :          when this case is actually true, so build a new ASSOCIATE
   11730              :          that does precisely this here (instead of using the
   11731              :          'global' one).  */
   11732              : 
   11733              :       /* First check the derived type import status.  */
   11734         4094 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11735            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11736              :         {
   11737           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11738            6 :                                  c->ts.u.derived->name);
   11739            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11740              :                                         gfc_current_ns))
   11741            6 :             error++;
   11742              :         }
   11743              : 
   11744         4094 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11745         4094 :       if (c->ts.type == BT_CLASS)
   11746          383 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11747          383 :                   c->ts.u.derived->name, var_name);
   11748         3711 :       else if (c->ts.type == BT_DERIVED)
   11749         2154 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11750         2154 :                   c->ts.u.derived->name, var_name);
   11751         1557 :       else if (c->ts.type == BT_CHARACTER)
   11752              :         {
   11753          772 :           HOST_WIDE_INT charlen = 0;
   11754          772 :           if (c->ts.u.cl && c->ts.u.cl->length
   11755            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11756            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11757          772 :           snprintf (name, sizeof (name),
   11758              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11759              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11760              :                     var_name);
   11761              :         }
   11762              :       else
   11763          785 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11764              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11765              : 
   11766         4094 :       st = gfc_find_symtree (ns->sym_root, name);
   11767         4094 :       gcc_assert (st->n.sym->assoc);
   11768         4094 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11769         4094 :       st->n.sym->assoc->target->where = selector_expr->where;
   11770         4094 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11771              :         {
   11772         3711 :           gfc_add_data_component (st->n.sym->assoc->target);
   11773              :           /* Fixup the target expression if necessary.  */
   11774         3711 :           if (rank || corank)
   11775         1503 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11776              :                              ref);
   11777              :         }
   11778              : 
   11779         4094 :       new_st = gfc_get_code (EXEC_BLOCK);
   11780         4094 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11781         4094 :       new_st->ext.block.ns->code = body->next;
   11782         4094 :       body->next = new_st;
   11783              : 
   11784              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11785              :          there is a CASE label overlap and this is already used.  Just ignore,
   11786              :          the error is diagnosed elsewhere.  */
   11787         4094 :       if (st->n.sym->assoc->dangling)
   11788              :         {
   11789         4093 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11790         4093 :           st->n.sym->assoc->dangling = 0;
   11791              :         }
   11792              : 
   11793         4094 :       resolve_assoc_var (st->n.sym, false);
   11794              :     }
   11795              : 
   11796              :   /* Take out CLASS IS cases for separate treatment.  */
   11797              :   body = code;
   11798         9200 :   while (body && body->block)
   11799              :     {
   11800         5888 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11801              :         {
   11802              :           /* Add to class_is list.  */
   11803          383 :           if (class_is == NULL)
   11804              :             {
   11805          334 :               class_is = body->block;
   11806          334 :               tail = class_is;
   11807              :             }
   11808              :           else
   11809              :             {
   11810           67 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11811           49 :               tail->block = body->block;
   11812           49 :               tail = tail->block;
   11813              :             }
   11814              :           /* Remove from EXEC_SELECT list.  */
   11815          383 :           body->block = body->block->block;
   11816          383 :           tail->block = NULL;
   11817              :         }
   11818              :       else
   11819              :         body = body->block;
   11820              :     }
   11821              : 
   11822         3312 :   if (class_is)
   11823              :     {
   11824          334 :       gfc_symbol *vtab;
   11825              : 
   11826          334 :       if (!default_case)
   11827              :         {
   11828              :           /* Add a default case to hold the CLASS IS cases.  */
   11829          334 :           for (tail = code; tail->block; tail = tail->block) ;
   11830          224 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11831          224 :           tail = tail->block;
   11832          224 :           tail->ext.block.case_list = gfc_get_case ();
   11833          224 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11834          224 :           tail->next = NULL;
   11835          224 :           default_case = tail;
   11836              :         }
   11837              : 
   11838              :       /* More than one CLASS IS block?  */
   11839          334 :       if (class_is->block)
   11840              :         {
   11841           49 :           gfc_code **c1,*c2;
   11842           49 :           bool swapped;
   11843              :           /* Sort CLASS IS blocks by extension level.  */
   11844           48 :           do
   11845              :             {
   11846           49 :               swapped = false;
   11847          127 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11848              :                 {
   11849              : #define C1_DERIVED (*c1)->ext.block.case_list->ts.u.derived
   11850              : #define C2_DERIVED c2->ext.block.case_list->ts.u.derived
   11851           79 :                   c2 = (*c1)->block;
   11852              : 
   11853              :                   /* F03:C817 (check for doubles).  */
   11854           79 :                   if (C1_DERIVED->hash_value == C2_DERIVED->hash_value)
   11855              :                     {
   11856            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11857              :                                  "statement at %L",
   11858              :                                  &c2->ext.block.case_list->where);
   11859            1 :                       return;
   11860              :                     }
   11861              : #undef C1_DERIVED
   11862              : #undef C2_DERIVED
   11863           78 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11864           78 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11865              :                     {
   11866              :                       /* Swap.  */
   11867           24 :                       (*c1)->block = c2->block;
   11868           24 :                       c2->block = *c1;
   11869           24 :                       *c1 = c2;
   11870           24 :                       swapped = true;
   11871              :                     }
   11872              :                 }
   11873              :             }
   11874              :           while (swapped);
   11875              :         }
   11876              : 
   11877              :       /* Generate IF chain.  */
   11878          333 :       if_st = gfc_get_code (EXEC_IF);
   11879          333 :       new_st = if_st;
   11880          714 :       for (body = class_is; body; body = body->block)
   11881              :         {
   11882          381 :           new_st->block = gfc_get_code (EXEC_IF);
   11883          381 :           new_st = new_st->block;
   11884              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11885          381 :           new_st->expr1 = gfc_get_expr ();
   11886          381 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11887          381 :           new_st->expr1->ts.type = BT_LOGICAL;
   11888          381 :           new_st->expr1->ts.kind = 4;
   11889          381 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11890          381 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11891          381 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11892              :           /* Set up arguments.  */
   11893          381 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11894          381 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11895          381 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11896          381 :           new_st->expr1->where = code->loc;
   11897          381 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11898          381 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11899          381 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11900          381 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11901          381 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11902          381 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11903              :           /* Set up types in formal arg list.  */
   11904          381 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11905          381 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11906          381 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11907          381 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11908              : 
   11909          381 :           new_st->next = body->next;
   11910              :         }
   11911          333 :         if (default_case->next)
   11912              :           {
   11913          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11914          110 :             new_st = new_st->block;
   11915          110 :             new_st->next = default_case->next;
   11916              :           }
   11917              : 
   11918              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11919          333 :         default_case->next = if_st;
   11920              :     }
   11921              : 
   11922              :   /* Resolve the internal code.  This cannot be done earlier because
   11923              :      it requires that the sym->assoc of selectors is set already.  */
   11924         3311 :   gfc_current_ns = ns;
   11925         3311 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11926         3311 :   gfc_current_ns = old_ns;
   11927              : 
   11928         3311 :   free (ref);
   11929              : }
   11930              : 
   11931              : 
   11932              : /* Resolve a SELECT RANK statement.  */
   11933              : 
   11934              : static void
   11935         1060 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11936              : {
   11937         1060 :   gfc_namespace *ns;
   11938         1060 :   gfc_code *body, *new_st, *tail;
   11939         1060 :   gfc_case *c;
   11940         1060 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11941         1060 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11942         1060 :   gfc_symtree *st;
   11943         1060 :   gfc_expr *selector_expr = NULL;
   11944         1060 :   int case_value;
   11945         1060 :   HOST_WIDE_INT charlen = 0;
   11946              : 
   11947         1060 :   ns = code->ext.block.ns;
   11948         1060 :   gfc_resolve (ns);
   11949              : 
   11950         1060 :   code->op = EXEC_BLOCK;
   11951         1060 :   if (code->expr2)
   11952              :     {
   11953           42 :       gfc_association_list* assoc;
   11954              : 
   11955           42 :       assoc = gfc_get_association_list ();
   11956           42 :       assoc->st = code->expr1->symtree;
   11957           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11958           42 :       assoc->target->where = code->expr2->where;
   11959              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11960              : 
   11961           42 :       code->ext.block.assoc = assoc;
   11962           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11963              : 
   11964           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11965              :     }
   11966              :   else
   11967         1018 :     code->ext.block.assoc = NULL;
   11968              : 
   11969              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11970              :      cascade of further errors because the case blocks do not compile
   11971              :      correctly.  */
   11972         3452 :   for (body = code->block; body; body = body->block)
   11973              :     {
   11974         2392 :       c = body->ext.block.case_list;
   11975         2392 :       if (c->low)
   11976         1437 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11977              :       else
   11978              :         case_value = -2;
   11979              : 
   11980              :       /* Check for repeated cases.  */
   11981         5986 :       for (tail = code->block; tail; tail = tail->block)
   11982              :         {
   11983         5986 :           gfc_case *d = tail->ext.block.case_list;
   11984         5986 :           int case_value2;
   11985              : 
   11986         5986 :           if (tail == body)
   11987              :             break;
   11988              : 
   11989              :           /* Check F2018: C1153.  */
   11990         3594 :           if (!c->low && !d->low)
   11991            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11992              :                        &c->where, &d->where);
   11993              : 
   11994         3594 :           if (!c->low || !d->low)
   11995         1301 :             continue;
   11996              : 
   11997              :           /* Check F2018: C1153.  */
   11998         2293 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11999         2293 :           if ((case_value == case_value2) && case_value == -1)
   12000            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   12001              :                        &c->where, &d->where);
   12002         2292 :           else if (case_value == case_value2)
   12003            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   12004              :                        case_value, &c->where, &d->where);
   12005              :         }
   12006              : 
   12007         2392 :       if (!c->low)
   12008          955 :         continue;
   12009              : 
   12010              :       /* Check F2018: C1155.  */
   12011         1437 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   12012         1437 :                                || gfc_expr_attr (code->expr1).pointer))
   12013            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   12014            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   12015              :     }
   12016              : 
   12017              :   /* Add EXEC_SELECT to switch on rank.  */
   12018         1060 :   new_st = gfc_get_code (code->op);
   12019         1060 :   new_st->expr1 = code->expr1;
   12020         1060 :   new_st->expr2 = code->expr2;
   12021         1060 :   new_st->block = code->block;
   12022         1060 :   code->expr1 = code->expr2 =  NULL;
   12023         1060 :   code->block = NULL;
   12024         1060 :   if (!ns->code)
   12025         1060 :     ns->code = new_st;
   12026              :   else
   12027            0 :     ns->code->next = new_st;
   12028         1060 :   code = new_st;
   12029         1060 :   code->op = EXEC_SELECT_RANK;
   12030              : 
   12031         1060 :   selector_expr = code->expr1;
   12032              : 
   12033              :   /* Loop over SELECT RANK cases.  */
   12034         3452 :   for (body = code->block; body; body = body->block)
   12035              :     {
   12036         2392 :       c = body->ext.block.case_list;
   12037         2392 :       int case_value;
   12038              : 
   12039              :       /* Pass on the default case.  */
   12040         2392 :       if (c->low == NULL)
   12041          955 :         continue;
   12042              : 
   12043              :       /* Associate temporary to selector.  This should only be done
   12044              :          when this case is actually true, so build a new ASSOCIATE
   12045              :          that does precisely this here (instead of using the
   12046              :          'global' one).  */
   12047         1437 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   12048          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   12049          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   12050              : 
   12051         1437 :       if (c->ts.type == BT_CLASS)
   12052          157 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   12053         1280 :       else if (c->ts.type == BT_DERIVED)
   12054          116 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   12055         1164 :       else if (c->ts.type != BT_CHARACTER)
   12056          605 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   12057              :       else
   12058          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   12059              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   12060              : 
   12061         1437 :       case_value = (int) mpz_get_si (c->low->value.integer);
   12062         1437 :       if (case_value >= 0)
   12063         1404 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   12064              :       else
   12065           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   12066              : 
   12067         1437 :       st = gfc_find_symtree (ns->sym_root, name);
   12068         1437 :       gcc_assert (st->n.sym->assoc);
   12069              : 
   12070         1437 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   12071         1437 :       st->n.sym->assoc->target->where = selector_expr->where;
   12072              : 
   12073         1437 :       new_st = gfc_get_code (EXEC_BLOCK);
   12074         1437 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   12075         1437 :       new_st->ext.block.ns->code = body->next;
   12076         1437 :       body->next = new_st;
   12077              : 
   12078              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   12079              :          there is a CASE label overlap and this is already used.  Just ignore,
   12080              :          the error is diagnosed elsewhere.  */
   12081         1437 :       if (st->n.sym->assoc->dangling)
   12082              :         {
   12083         1435 :           new_st->ext.block.assoc = st->n.sym->assoc;
   12084         1435 :           st->n.sym->assoc->dangling = 0;
   12085              :         }
   12086              : 
   12087         1437 :       resolve_assoc_var (st->n.sym, false);
   12088              :     }
   12089              : 
   12090         1060 :   gfc_current_ns = ns;
   12091         1060 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   12092         1060 :   gfc_current_ns = old_ns;
   12093         1060 : }
   12094              : 
   12095              : 
   12096              : /* Resolve a transfer statement. This is making sure that:
   12097              :    -- a derived type being transferred has only non-pointer components
   12098              :    -- a derived type being transferred doesn't have private components, unless
   12099              :       it's being transferred from the module where the type was defined
   12100              :    -- we're not trying to transfer a whole assumed size array.  */
   12101              : 
   12102              : static void
   12103        48284 : resolve_transfer (gfc_code *code)
   12104              : {
   12105        48284 :   gfc_symbol *sym, *derived;
   12106        48284 :   gfc_ref *ref;
   12107        48284 :   gfc_expr *exp;
   12108        48284 :   bool write = false;
   12109        48284 :   bool formatted = false;
   12110        48284 :   gfc_dt *dt = code->ext.dt;
   12111        48284 :   gfc_symbol *dtio_sub = NULL;
   12112              : 
   12113        48284 :   exp = code->expr1;
   12114              : 
   12115        96574 :   while (exp != NULL && exp->expr_type == EXPR_OP
   12116        49236 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   12117            6 :     exp = exp->value.op.op1;
   12118              : 
   12119        48284 :   if (exp && exp->expr_type == EXPR_NULL
   12120            2 :       && code->ext.dt)
   12121              :     {
   12122            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   12123              :                  &exp->where);
   12124            2 :       return;
   12125              :     }
   12126              : 
   12127        48282 :   if (dt && (dt->dt_io_kind->value.iokind == M_WRITE
   12128        48130 :              || dt->dt_io_kind->value.iokind == M_PRINT))
   12129        40365 :     gfc_value_used_expr (exp, VALUE_USED);
   12130              : 
   12131        48282 :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   12132              :                       && exp->expr_type != EXPR_FUNCTION
   12133              :                       && exp->expr_type != EXPR_ARRAY
   12134              :                       && exp->expr_type != EXPR_STRUCTURE))
   12135              :     return;
   12136              : 
   12137        26895 :   if (dt && dt->dt_io_kind->value.iokind == M_READ)
   12138              :     {
   12139              :       /* If we are reading, the variable will be changed.  Note that
   12140              :          code->ext.dt may be NULL if the TRANSFER is related to an INQUIRE
   12141              :          statement -- but in this case, we are not reading, either.  */
   12142         7765 :       if (!gfc_check_vardef_context (exp, false, false, false,
   12143         7765 :                                      _("item in READ")))
   12144              :         return;
   12145              : 
   12146         7761 :       gfc_expr_set_at (exp, &exp->where, VALUE_READ);
   12147              :     }
   12148              : 
   12149        26891 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   12150        26891 :                         || exp->expr_type == EXPR_FUNCTION
   12151        22425 :                         || exp->expr_type == EXPR_ARRAY
   12152        49316 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   12153              : 
   12154              :   /* Go to actual component transferred.  */
   12155        34881 :   for (ref = exp->ref; ref; ref = ref->next)
   12156         7990 :     if (ref->type == REF_COMPONENT)
   12157         2230 :       ts = &ref->u.c.component->ts;
   12158              : 
   12159        26891 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   12160        26743 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   12161              :     {
   12162          721 :       derived = ts->u.derived;
   12163              : 
   12164              :       /* Determine when to use the formatted DTIO procedure.  */
   12165          721 :       if (dt && (dt->format_expr || dt->format_label))
   12166          646 :         formatted = true;
   12167              : 
   12168          721 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   12169          721 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   12170          721 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   12171              : 
   12172          721 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   12173              :         {
   12174          450 :           dt->udtio = exp;
   12175          450 :           sym = exp->symtree->n.sym->ns->proc_name;
   12176              :           /* Check to see if this is a nested DTIO call, with the
   12177              :              dummy as the io-list object.  */
   12178          450 :           if (sym && sym == dtio_sub && sym->formal
   12179           30 :               && sym->formal->sym == exp->symtree->n.sym
   12180           30 :               && exp->ref == NULL)
   12181              :             {
   12182            0 :               if (!sym->attr.recursive)
   12183              :                 {
   12184            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   12185              :                              sym->name, &sym->declared_at);
   12186            0 :                   return;
   12187              :                 }
   12188              :             }
   12189              :         }
   12190              :     }
   12191              : 
   12192        26891 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   12193              :     {
   12194            1 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   12195              :                 "it is processed by a defined input/output procedure",
   12196              :                 &code->loc);
   12197            1 :       return;
   12198              :     }
   12199              : 
   12200        26890 :   if (ts->type == BT_DERIVED)
   12201              :     {
   12202              :       /* Check that transferred derived type doesn't contain POINTER
   12203              :          components unless it is processed by a defined input/output
   12204              :          procedure".  */
   12205          691 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   12206              :         {
   12207            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   12208              :                      "components unless it is processed by a defined "
   12209              :                      "input/output procedure", &code->loc);
   12210            2 :           return;
   12211              :         }
   12212              : 
   12213              :       /* F08:C935.  */
   12214          689 :       if (ts->u.derived->attr.proc_pointer_comp)
   12215              :         {
   12216            2 :           gfc_error ("Data transfer element at %L cannot have "
   12217              :                      "procedure pointer components", &code->loc);
   12218            2 :           return;
   12219              :         }
   12220              : 
   12221          687 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   12222              :         {
   12223            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   12224              :                      "components unless it is processed by a defined "
   12225              :                      "input/output procedure", &code->loc);
   12226            6 :           return;
   12227              :         }
   12228              : 
   12229              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   12230              :          be printed.  However, if -std=gnu and not -pedantic, allow
   12231              :          the component to be printed to help debugging.  */
   12232          681 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   12233              :         {
   12234            4 :           gfc_error ("Data transfer element at %L "
   12235              :                      "cannot have PRIVATE components", &code->loc);
   12236            4 :             return;
   12237              :         }
   12238          677 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   12239              :         {
   12240            4 :           gfc_error ("Data transfer element at %L cannot have "
   12241              :                      "PRIVATE components unless it is processed by "
   12242              :                      "a defined input/output procedure", &code->loc);
   12243            4 :           return;
   12244              :         }
   12245              :     }
   12246              : 
   12247        26872 :   if (exp->expr_type == EXPR_STRUCTURE)
   12248              :     return;
   12249              : 
   12250        26827 :   if (exp->expr_type == EXPR_ARRAY)
   12251              :     return;
   12252              : 
   12253        26442 :   sym = exp->symtree->n.sym;
   12254              : 
   12255        26442 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   12256           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   12257              :     {
   12258            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   12259              :                  "an assumed-size array", &code->loc);
   12260            1 :       return;
   12261              :     }
   12262              : 
   12263              : }
   12264              : 
   12265              : 
   12266              : /*********** Toplevel code resolution subroutines ***********/
   12267              : 
   12268              : /* Find the set of labels that are reachable from this block.  We also
   12269              :    record the last statement in each block.  */
   12270              : 
   12271              : static void
   12272       716563 : find_reachable_labels (gfc_code *block)
   12273              : {
   12274       716563 :   gfc_code *c;
   12275              : 
   12276       716563 :   if (!block)
   12277              :     return;
   12278              : 
   12279       442052 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   12280              : 
   12281              :   /* Collect labels in this block.  We don't keep those corresponding
   12282              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   12283              :      up through the code_stack.  */
   12284      1616651 :   for (c = block; c; c = c->next)
   12285              :     {
   12286      1174599 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   12287         3721 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   12288              :     }
   12289              : 
   12290              :   /* Merge with labels from parent block.  */
   12291       442052 :   if (cs_base->prev)
   12292              :     {
   12293       361927 :       gcc_assert (cs_base->prev->reachable_labels);
   12294       361927 :       bitmap_ior_into (cs_base->reachable_labels,
   12295              :                        cs_base->prev->reachable_labels);
   12296              :     }
   12297              : }
   12298              : 
   12299              : static void
   12300          197 : resolve_lock_unlock_event (gfc_code *code)
   12301              : {
   12302          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   12303          197 :       && (code->expr1->ts.type != BT_DERIVED
   12304          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   12305          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   12306          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   12307          136 :           || code->expr1->rank != 0
   12308          181 :           || (!gfc_is_coarray (code->expr1) &&
   12309           46 :               !gfc_is_coindexed (code->expr1))))
   12310            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   12311            4 :                &code->expr1->where);
   12312          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   12313           58 :            && (code->expr1->ts.type != BT_DERIVED
   12314           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   12315           58 :                || code->expr1->ts.u.derived->from_intmod
   12316              :                   != INTMOD_ISO_FORTRAN_ENV
   12317           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   12318              :                   != ISOFORTRAN_EVENT_TYPE
   12319           58 :                || code->expr1->rank != 0))
   12320            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   12321              :                &code->expr1->where);
   12322           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   12323          209 :            && !gfc_is_coindexed (code->expr1))
   12324            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   12325            0 :                &code->expr1->where);
   12326          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   12327            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   12328            0 :                "coindexed", &code->expr1->where);
   12329              : 
   12330              :   /* Check STAT.  */
   12331          197 :   if (code->expr2
   12332           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   12333           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   12334            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12335              :                &code->expr2->where);
   12336              : 
   12337          197 :   if (code->expr2
   12338          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   12339           54 :                                     _("STAT variable")))
   12340              :     return;
   12341              : 
   12342              :   /* Check ERRMSG.  */
   12343          197 :   if (code->expr3
   12344            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   12345            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   12346            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12347              :                &code->expr3->where);
   12348              : 
   12349          197 :   if (code->expr3
   12350          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   12351            2 :                                     _("ERRMSG variable")))
   12352              :     return;
   12353              : 
   12354              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   12355          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12356           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   12357           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   12358            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   12359              :                "variable", &code->expr4->where);
   12360              : 
   12361          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12362          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   12363           22 :                                     _("ACQUIRED_LOCK variable")))
   12364              :     return;
   12365              : 
   12366              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   12367          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   12368              :     {
   12369           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   12370           36 :           || code->expr4->rank != 0)
   12371            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   12372            0 :                    "expression", &code->expr4->where);
   12373              :     }
   12374              : }
   12375              : 
   12376              : static void
   12377          363 : resolve_team_argument (gfc_expr *team)
   12378              : {
   12379          363 :   gfc_resolve_expr (team);
   12380          363 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   12381          356 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   12382          356 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   12383              :     {
   12384            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   12385              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   12386              :                  &team->where);
   12387              :     }
   12388          363 : }
   12389              : 
   12390              : static bool
   12391         1719 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   12392              :                                 gfc_expr *e)
   12393              : {
   12394         1719 :   gfc_resolve_expr (e);
   12395         1719 :   if (e
   12396          173 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   12397          163 :           || e->expr_type != EXPR_VARIABLE))
   12398              :     {
   12399           10 :       gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   12400              :                  "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   12401              :                  exp_kind);
   12402           10 :       return false;
   12403              :     }
   12404              :   return true;
   12405              : }
   12406              : 
   12407              : bool
   12408          862 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   12409              : {
   12410          862 :   if (!resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat))
   12411              :     return false;
   12412          857 :   if (!resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   12413              :                                        gfc_default_character_kind,
   12414              :                                        sync_stat->errmsg))
   12415              :     return false;
   12416              :   return true;
   12417              : }
   12418              : 
   12419              : static void
   12420          368 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   12421              :                          gfc_expr *e)
   12422              : {
   12423          368 :   gfc_resolve_expr (e);
   12424          368 :   if (e
   12425          217 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12426            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12427              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12428          368 : }
   12429              : 
   12430              : static void
   12431          184 : resolve_form_team (gfc_code *code)
   12432              : {
   12433          184 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12434              :                            code->expr1);
   12435          184 :   resolve_team_argument (code->expr2);
   12436          184 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12437              :                            code->expr3);
   12438          184 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12439          184 : }
   12440              : 
   12441              : static void resolve_block_construct (gfc_code *);
   12442              : 
   12443              : static void
   12444          131 : resolve_change_team (gfc_code *code)
   12445              : {
   12446          131 :   resolve_team_argument (code->expr1);
   12447          131 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12448          262 :   resolve_block_construct (code);
   12449              :   /* Map the coarray bounds as selected.  */
   12450          134 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12451            3 :     if (a->ar)
   12452              :       {
   12453            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12454            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12455            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12456              :         else
   12457            3 :           dst = a->st->n.sym->as;
   12458            3 :         dst->corank = src->corank;
   12459            3 :         dst->cotype = src->cotype;
   12460            6 :         for (int i = 0; i < src->corank; ++i)
   12461              :           {
   12462            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12463            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12464            3 :             src->lower[i] = src->upper[i] = nullptr;
   12465              :           }
   12466            3 :         gfc_free_array_spec (src);
   12467            3 :         free (a->ar);
   12468            3 :         a->ar = nullptr;
   12469            3 :         dst->resolved = false;
   12470            3 :         gfc_resolve_array_spec (dst, 0);
   12471              :       }
   12472          131 : }
   12473              : 
   12474              : static void
   12475           48 : resolve_sync_team (gfc_code *code)
   12476              : {
   12477           48 :   resolve_team_argument (code->expr1);
   12478           48 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12479           48 : }
   12480              : 
   12481              : static void
   12482          129 : resolve_end_team (gfc_code *code)
   12483              : {
   12484          129 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12485          129 : }
   12486              : 
   12487              : static void
   12488           54 : resolve_critical (gfc_code *code)
   12489              : {
   12490           54 :   gfc_symtree *symtree;
   12491           54 :   gfc_symbol *lock_type;
   12492           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12493           54 :   static int serial = 0;
   12494              : 
   12495           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12496              : 
   12497           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12498           30 :     return;
   12499              : 
   12500           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12501              :                               GFC_PREFIX ("lock_type"));
   12502           24 :   if (symtree)
   12503           12 :     lock_type = symtree->n.sym;
   12504              :   else
   12505              :     {
   12506           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12507              :                             false) != 0)
   12508            0 :         gcc_unreachable ();
   12509           12 :       lock_type = symtree->n.sym;
   12510           12 :       lock_type->attr.flavor = FL_DERIVED;
   12511           12 :       lock_type->attr.zero_comp = 1;
   12512           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12513           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12514              :     }
   12515              : 
   12516           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12517           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12518            0 :     gcc_unreachable ();
   12519              : 
   12520           24 :   code->resolved_sym = symtree->n.sym;
   12521           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12522           24 :   symtree->n.sym->attr.referenced = 1;
   12523           24 :   symtree->n.sym->attr.artificial = 1;
   12524           24 :   symtree->n.sym->attr.codimension = 1;
   12525           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12526           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12527           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12528           24 :   symtree->n.sym->as->corank = 1;
   12529           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12530           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12531           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12532              :                                                    NULL, 1);
   12533           24 :   gfc_commit_symbols();
   12534              : }
   12535              : 
   12536              : 
   12537              : static void
   12538         1450 : resolve_sync (gfc_code *code)
   12539              : {
   12540              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12541         1450 :   if (code->expr1)
   12542              :     {
   12543          107 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12544            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12545              :                    "INTEGER expression", &code->expr1->where);
   12546          107 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12547           48 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12548            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12549              :                    &code->expr1->where);
   12550          106 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12551          106 :                && gfc_simplify_expr (code->expr1, 0))
   12552              :         {
   12553           35 :            gfc_constructor *cons;
   12554           35 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12555          120 :            for (; cons; cons = gfc_constructor_next (cons))
   12556           50 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12557           50 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12558            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12559              :                           "num_images()", &cons->expr->where);
   12560              :         }
   12561              :     }
   12562              : 
   12563              :   /* Check STAT.  */
   12564         1450 :   gfc_resolve_expr (code->expr2);
   12565         1450 :   if (code->expr2)
   12566              :     {
   12567          161 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12568            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12569              :                    &code->expr2->where);
   12570              :       else
   12571          160 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12572          160 :                                   _("STAT variable"));
   12573              :     }
   12574              : 
   12575              :   /* Check ERRMSG.  */
   12576         1450 :   gfc_resolve_expr (code->expr3);
   12577         1450 :   if (code->expr3)
   12578              :     {
   12579           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12580            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12581              :                    &code->expr3->where);
   12582              :       else
   12583           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12584           86 :                                   _("ERRMSG variable"));
   12585              :     }
   12586         1450 : }
   12587              : 
   12588              : 
   12589              : /* Given a branch to a label, see if the branch is conforming.
   12590              :    The code node describes where the branch is located.  */
   12591              : 
   12592              : static void
   12593       113715 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12594              : {
   12595       113715 :   code_stack *stack;
   12596              : 
   12597       113715 :   if (label == NULL)
   12598              :     return;
   12599              : 
   12600              :   /* Step one: is this a valid branching target?  */
   12601              : 
   12602         2516 :   if (label->defined == ST_LABEL_UNKNOWN)
   12603              :     {
   12604            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12605              :                  &code->loc);
   12606            4 :       return;
   12607              :     }
   12608              : 
   12609         2512 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12610              :     {
   12611            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12612              :                  "for the branch statement at %L", &label->where, &code->loc);
   12613            4 :       return;
   12614              :     }
   12615              : 
   12616              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12617              : 
   12618         2508 :   if (code->here == label)
   12619              :     {
   12620            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12621              :                    &code->loc);
   12622            0 :       return;
   12623              :     }
   12624              : 
   12625              :   /* Step three:  See if the label is in the same block as the
   12626              :      branching statement.  The hard work has been done by setting up
   12627              :      the bitmap reachable_labels.  */
   12628              : 
   12629         2508 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12630              :     {
   12631              :       /* Check now whether there is a CRITICAL construct; if so, check
   12632              :          whether the label is still visible outside of the CRITICAL block,
   12633              :          which is invalid.  */
   12634         6380 :       for (stack = cs_base; stack; stack = stack->prev)
   12635              :         {
   12636         3940 :           if (stack->current->op == EXEC_CRITICAL
   12637         3940 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12638            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12639              :                       "label at %L", &code->loc, &label->where);
   12640         3938 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12641         3938 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12642            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12643              :                       "for label at %L", &code->loc, &label->where);
   12644         3938 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12645         3938 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12646            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12647              :                       "for label at %L", &code->loc, &label->where);
   12648              :         }
   12649              : 
   12650              :       return;
   12651              :     }
   12652              : 
   12653              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12654              :     still be the label of the END of the enclosing block, in which
   12655              :     case we find it by going up the code_stack.  */
   12656              : 
   12657          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12658              :     {
   12659          131 :       if (stack->current->next && stack->current->next->here == label)
   12660              :         break;
   12661          101 :       if (stack->current->op == EXEC_CRITICAL)
   12662              :         {
   12663              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12664              :              construct as END CRITICAL is still part of it.  */
   12665            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12666              :                       " at %L", &code->loc, &label->where);
   12667            2 :           return;
   12668              :         }
   12669           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12670              :         {
   12671            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12672              :                      "label at %L", &code->loc, &label->where);
   12673            0 :           return;
   12674              :         }
   12675              :     }
   12676              : 
   12677           66 :   if (stack)
   12678              :     {
   12679           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12680              :       return;
   12681              :     }
   12682              : 
   12683              :   /* The label is not in an enclosing block, so illegal.  This was
   12684              :      allowed in Fortran 66, so we allow it as extension.  No
   12685              :      further checks are necessary in this case.  */
   12686           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12687              :                   "as the GOTO statement at %L", &label->where,
   12688              :                   &code->loc);
   12689           36 :   return;
   12690              : }
   12691              : 
   12692              : 
   12693              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12694              : 
   12695              : static bool
   12696         1479 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12697              : {
   12698         1479 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12699         1479 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12700         1479 :   bool result = false;
   12701         1479 :   int i;
   12702              : 
   12703              :   /* Compare the rank.  */
   12704         1479 :   if (expr1->rank != expr2->rank)
   12705              :     return result;
   12706              : 
   12707              :   /* Compare the size of each dimension.  */
   12708         2835 :   for (i=0; i<expr1->rank; i++)
   12709              :     {
   12710         1507 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12711          151 :         goto ignore;
   12712              : 
   12713         1356 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12714            0 :         goto ignore;
   12715              : 
   12716         1356 :       if (mpz_cmp (shape[i], shape2[i]))
   12717            0 :         goto over;
   12718              :     }
   12719              : 
   12720              :   /* When either of the two expression is an assumed size array, we
   12721              :      ignore the comparison of dimension sizes.  */
   12722         1328 : ignore:
   12723              :   result = true;
   12724              : 
   12725         1479 : over:
   12726         1479 :   gfc_clear_shape (shape, i);
   12727         1479 :   gfc_clear_shape (shape2, i);
   12728         1479 :   return result;
   12729              : }
   12730              : 
   12731              : 
   12732              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12733              :    has the same shape as the outermost WHERE mask expression.  */
   12734              : 
   12735              : static void
   12736          515 : resolve_where (gfc_code *code, gfc_expr *mask)
   12737              : {
   12738          515 :   gfc_code *cblock;
   12739          515 :   gfc_code *cnext;
   12740          515 :   gfc_expr *e = NULL;
   12741              : 
   12742          515 :   cblock = code->block;
   12743              : 
   12744              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12745              :      In case of nested WHERE, only the outermost one is stored.  */
   12746          515 :   if (mask == NULL) /* outermost WHERE */
   12747          459 :     e = cblock->expr1;
   12748              :   else /* inner WHERE */
   12749          515 :     e = mask;
   12750              : 
   12751         1399 :   while (cblock)
   12752              :     {
   12753          884 :       if (cblock->expr1)
   12754              :         {
   12755              :           /* Check if the mask-expr has a consistent shape with the
   12756              :              outermost WHERE mask-expr.  */
   12757          720 :           if (!resolve_where_shape (cblock->expr1, e))
   12758            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12759            0 :                        &cblock->expr1->where);
   12760              :          }
   12761              : 
   12762              :       /* the assignment statement of a WHERE statement, or the first
   12763              :          statement in where-body-construct of a WHERE construct */
   12764          884 :       cnext = cblock->next;
   12765         1745 :       while (cnext)
   12766              :         {
   12767          861 :           switch (cnext->op)
   12768              :             {
   12769              :             /* WHERE assignment statement */
   12770          759 :             case EXEC_ASSIGN:
   12771              : 
   12772              :               /* Check shape consistent for WHERE assignment target.  */
   12773          759 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12774            0 :                gfc_error ("WHERE assignment target at %L has "
   12775            0 :                           "inconsistent shape", &cnext->expr1->where);
   12776              : 
   12777          759 :               if (cnext->op == EXEC_ASSIGN
   12778          759 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12779            0 :                 cnext->expr1->must_finalize = 1;
   12780              : 
   12781              :               break;
   12782              : 
   12783              : 
   12784           46 :             case EXEC_ASSIGN_CALL:
   12785           46 :               resolve_call (cnext);
   12786           46 :               if (!cnext->resolved_sym->attr.elemental)
   12787            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12788            2 :                           &cnext->ext.actual->expr->where);
   12789              :               break;
   12790              : 
   12791              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12792           56 :             case EXEC_WHERE:
   12793           56 :               resolve_where (cnext, e);
   12794           56 :               break;
   12795              : 
   12796            0 :             default:
   12797            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12798              :                          &cnext->loc);
   12799              :             }
   12800              :          /* the next statement within the same where-body-construct */
   12801          861 :          cnext = cnext->next;
   12802              :        }
   12803              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12804          884 :     cblock = cblock->block;
   12805              :   }
   12806          515 : }
   12807              : 
   12808              : 
   12809              : /* Resolve assignment in FORALL construct.
   12810              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12811              :    FORALL index variables.  */
   12812              : 
   12813              : static void
   12814         2468 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12815              : {
   12816         2468 :   int n;
   12817         2468 :   gfc_symbol *forall_index;
   12818              : 
   12819         7000 :   for (n = 0; n < nvar; n++)
   12820              :     {
   12821         4532 :       forall_index = var_expr[n]->symtree->n.sym;
   12822              : 
   12823              :       /* Check whether the assignment target is one of the FORALL index
   12824              :          variable.  */
   12825         4532 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12826         4532 :           && (code->expr1->symtree->n.sym == forall_index))
   12827            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12828              :                    &code->expr1->where);
   12829              :       else
   12830              :         {
   12831              :           /* If one of the FORALL index variables doesn't appear in the
   12832              :              assignment variable, then there could be a many-to-one
   12833              :              assignment.  Emit a warning rather than an error because the
   12834              :              mask could be resolving this problem.
   12835              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12836              :              many-to-one assignments are semantically valid (formalized with
   12837              :              the REDUCE locality-spec in Fortran 2023).  */
   12838         4532 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12839         4532 :               && !gfc_do_concurrent_flag)
   12840            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12841              :                          "left side of the assignment at %L and so might "
   12842              :                          "cause multiple assignment to this object",
   12843            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12844              :         }
   12845              :     }
   12846         2468 : }
   12847              : 
   12848              : 
   12849              : /* Resolve WHERE statement in FORALL construct.  */
   12850              : 
   12851              : static void
   12852           53 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12853              :                                   gfc_expr **var_expr)
   12854              : {
   12855           53 :   gfc_code *cblock;
   12856           53 :   gfc_code *cnext;
   12857              : 
   12858           53 :   cblock = code->block;
   12859          125 :   while (cblock)
   12860              :     {
   12861              :       /* the assignment statement of a WHERE statement, or the first
   12862              :          statement in where-body-construct of a WHERE construct */
   12863           72 :       cnext = cblock->next;
   12864          144 :       while (cnext)
   12865              :         {
   12866           72 :           switch (cnext->op)
   12867              :             {
   12868              :             /* WHERE assignment statement */
   12869           72 :             case EXEC_ASSIGN:
   12870           72 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12871              : 
   12872           72 :               if (cnext->op == EXEC_ASSIGN
   12873           72 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12874            0 :                 cnext->expr1->must_finalize = 1;
   12875              : 
   12876              :               break;
   12877              : 
   12878              :             /* WHERE operator assignment statement */
   12879            0 :             case EXEC_ASSIGN_CALL:
   12880            0 :               resolve_call (cnext);
   12881            0 :               if (!cnext->resolved_sym->attr.elemental)
   12882            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12883            0 :                           &cnext->ext.actual->expr->where);
   12884              :               break;
   12885              : 
   12886              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12887            0 :             case EXEC_WHERE:
   12888            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12889            0 :               break;
   12890              : 
   12891            0 :             default:
   12892            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12893              :                          &cnext->loc);
   12894              :             }
   12895              :           /* the next statement within the same where-body-construct */
   12896           72 :           cnext = cnext->next;
   12897              :         }
   12898              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12899           72 :       cblock = cblock->block;
   12900              :     }
   12901           53 : }
   12902              : 
   12903              : 
   12904              : /* Traverse the FORALL body to check whether the following errors exist:
   12905              :    1. For assignment, check if a many-to-one assignment happens.
   12906              :    2. For WHERE statement, check the WHERE body to see if there is any
   12907              :       many-to-one assignment.  */
   12908              : 
   12909              : static void
   12910         2363 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12911              : {
   12912         2363 :   gfc_code *c;
   12913              : 
   12914         2363 :   c = code->block->next;
   12915         5172 :   while (c)
   12916              :     {
   12917         2809 :       switch (c->op)
   12918              :         {
   12919         2396 :         case EXEC_ASSIGN:
   12920         2396 :         case EXEC_POINTER_ASSIGN:
   12921         2396 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12922              : 
   12923         2396 :           if (c->op == EXEC_ASSIGN
   12924         2396 :               && gfc_may_be_finalized (c->expr1->ts))
   12925            0 :             c->expr1->must_finalize = 1;
   12926              : 
   12927              :           break;
   12928              : 
   12929            0 :         case EXEC_ASSIGN_CALL:
   12930            0 :           resolve_call (c);
   12931            0 :           break;
   12932              : 
   12933              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12934              :            there is no need to handle it here.  */
   12935              :         case EXEC_FORALL:
   12936              :           break;
   12937           53 :         case EXEC_WHERE:
   12938           53 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12939           53 :           break;
   12940              :         default:
   12941              :           break;
   12942              :         }
   12943              :       /* The next statement in the FORALL body.  */
   12944         2809 :       c = c->next;
   12945              :     }
   12946         2363 : }
   12947              : 
   12948              : 
   12949              : /* Counts the number of iterators needed inside a forall construct, including
   12950              :    nested forall constructs. This is used to allocate the needed memory
   12951              :    in gfc_resolve_forall.  */
   12952              : 
   12953              : static int gfc_count_forall_iterators (gfc_code *code);
   12954              : 
   12955              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12956              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12957              : 
   12958              : static int
   12959         2621 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12960              : {
   12961         2621 :   int max_iters = 0;
   12962              : 
   12963         5723 :   for (gfc_code *c = code; c; c = c->next)
   12964              :     {
   12965         3102 :       int sub_iters = 0;
   12966              : 
   12967         3102 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12968          124 :         sub_iters = gfc_count_forall_iterators (c);
   12969         2978 :       else if (c->op == EXEC_BLOCK)
   12970              :         {
   12971              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12972              :              not in the generic c->block arm list used by IF/SELECT.  */
   12973           40 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12974           40 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12975              :         }
   12976         2938 :       else if (c->block)
   12977          403 :         for (gfc_code *b = c->block; b; b = b->block)
   12978              :           {
   12979          218 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12980          218 :             if (arm_iters > sub_iters)
   12981              :               sub_iters = arm_iters;
   12982              :           }
   12983              : 
   12984         3102 :       if (sub_iters > max_iters)
   12985              :         max_iters = sub_iters;
   12986              :     }
   12987              : 
   12988         2621 :   return max_iters;
   12989              : }
   12990              : 
   12991              : 
   12992              : static int
   12993         2363 : gfc_count_forall_iterators (gfc_code *code)
   12994              : {
   12995         2363 :   int current_iters = 0;
   12996         2363 :   gfc_forall_iterator *fa;
   12997              : 
   12998         2363 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12999              : 
   13000         6656 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   13001         4293 :     current_iters++;
   13002              : 
   13003         2363 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   13004              : }
   13005              : 
   13006              : 
   13007              : /* Given a FORALL construct.
   13008              :    1) Resolve the FORALL iterator.
   13009              :    2) Check for shadow index-name(s) and update code block.
   13010              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   13011              : 
   13012              : /* Shadow variable that replace_forall_var substitutes in; set by
   13013              :    replace_in_expr_recursive before each traversal.  */
   13014              : 
   13015              : static gfc_symtree *forall_shadow_st;
   13016              : 
   13017              : /* gfc_traverse_expr callback: point a reference to OLD_SYM at the
   13018              :    construct-scoped shadow variable.  */
   13019              : 
   13020              : static bool
   13021          872 : replace_forall_var (gfc_expr *expr, gfc_symbol *old_sym,
   13022              :                     int *f ATTRIBUTE_UNUSED)
   13023              : {
   13024          872 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   13025              :     {
   13026          198 :       expr->symtree = forall_shadow_st;
   13027          198 :       expr->ts = forall_shadow_st->n.sym->ts;
   13028              :     }
   13029              : 
   13030          872 :   return false;
   13031              : }
   13032              : 
   13033              : 
   13034              : /* Replace every reference to OLD_SYM in EXPR with NEW_ST.  Traversal is
   13035              :    left to gfc_traverse_expr so that all expression forms are covered;
   13036              :    character length type parameters are skipped since those belong to
   13037              :    declarations that may be shared outside the construct.  */
   13038              : 
   13039              : static void
   13040          974 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym,
   13041              :                            gfc_symtree *new_st)
   13042              : {
   13043          974 :   if (!expr)
   13044              :     return;
   13045              : 
   13046          412 :   forall_shadow_st = new_st;
   13047          412 :   gfc_traverse_expr (expr, old_sym, replace_forall_var, -1);
   13048              : }
   13049              : 
   13050              : 
   13051              : /* Walk code tree and replace all variable references */
   13052              : 
   13053              : static void
   13054          200 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   13055              : {
   13056          200 :   if (!code)
   13057              :     return;
   13058              : 
   13059          412 :   for (gfc_code *c = code; c; c = c->next)
   13060              :     {
   13061              :       /* Replace in expressions associated with this code node */
   13062          212 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   13063          212 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   13064          212 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   13065          212 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   13066              : 
   13067              :       /* Handle special code types with additional expressions */
   13068          212 :       switch (c->op)
   13069              :         {
   13070            0 :         case EXEC_DO:
   13071            0 :           if (c->ext.iterator)
   13072              :             {
   13073            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   13074            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   13075            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   13076              :             }
   13077              :           break;
   13078              : 
   13079            0 :         case EXEC_CALL:
   13080            0 :         case EXEC_ASSIGN_CALL:
   13081            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   13082            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   13083              :           break;
   13084              : 
   13085            6 :         case EXEC_SELECT:
   13086            6 :         case EXEC_SELECT_TYPE:
   13087            6 :         case EXEC_SELECT_RANK:
   13088           12 :           for (gfc_code *b = c->block; b; b = b->block)
   13089              :             {
   13090           12 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   13091              :                 {
   13092            6 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   13093            6 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   13094              :                 }
   13095            6 :               replace_in_code_recursive (b->next, old_sym, new_st);
   13096              :             }
   13097              :           break;
   13098              : 
   13099           18 :         case EXEC_IF:
   13100           18 :         case EXEC_WHERE:
   13101              :           /* Each block in the chain holds its condition or mask in EXPR1
   13102              :              and its body in NEXT; the trailing ELSE/ELSEWHERE has no
   13103              :              condition.  The generic recursion below only reaches the first
   13104              :              branch, so walk the whole chain here.  */
   13105           48 :           for (gfc_code *b = c->block; b; b = b->block)
   13106              :             {
   13107           30 :               replace_in_expr_recursive (b->expr1, old_sym, new_st);
   13108           30 :               replace_in_code_recursive (b->next, old_sym, new_st);
   13109              :             }
   13110              :           break;
   13111              : 
   13112            6 :         case EXEC_ALLOCATE:
   13113            6 :         case EXEC_DEALLOCATE:
   13114              :           /* Bounds and lengths of the allocate-objects.  */
   13115           12 :           for (gfc_alloc *al = c->ext.alloc.list; al; al = al->next)
   13116            6 :             replace_in_expr_recursive (al->expr, old_sym, new_st);
   13117              :           break;
   13118              : 
   13119           24 :         case EXEC_FORALL:
   13120           24 :         case EXEC_DO_CONCURRENT:
   13121           48 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   13122              :             {
   13123           24 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   13124           24 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   13125           24 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   13126              :             }
   13127              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   13128              :              they'll be handled separately */
   13129              :           break;
   13130              : 
   13131           12 :         case EXEC_BLOCK:
   13132              :           /* Replace in ASSOCIATE selector expressions and the body.
   13133              :              The body of an EXEC_BLOCK lives in c->ext.block.ns->code, not
   13134              :              c->block->next, so without this case both selectors and body
   13135              :              are silently skipped, leaving shadow iterator references unreplaced
   13136              :              and producing wrong values at runtime.  */
   13137           12 :           for (gfc_association_list *alist = c->ext.block.assoc;
   13138           18 :                alist; alist = alist->next)
   13139            6 :             replace_in_expr_recursive (alist->target, old_sym, new_st);
   13140           12 :           if (c->ext.block.ns)
   13141           12 :             replace_in_code_recursive (c->ext.block.ns->code, old_sym, new_st);
   13142              :           break;
   13143              : 
   13144              :         default:
   13145              :           break;
   13146              :         }
   13147              : 
   13148              :       /* Recurse into blocks, except a nested FORALL/DO CONCURRENT that
   13149              :          shadows OLD_SYM itself.  */
   13150          212 :       bool skip_nested_shadow = false;
   13151          212 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   13152           24 :         for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator;
   13153           24 :              fa; fa = fa->next)
   13154           24 :           if (fa->shadow && fa->shadow_outer_sym == old_sym)
   13155              :             {
   13156              :               skip_nested_shadow = true;
   13157              :               break;
   13158              :             }
   13159              : 
   13160          212 :       if (c->block && !skip_nested_shadow)
   13161           24 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   13162              :     }
   13163              : }
   13164              : 
   13165              : 
   13166              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   13167              : 
   13168              : static void
   13169          128 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   13170              :                               gfc_symtree *shadow_st)
   13171              : {
   13172              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   13173            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   13174            0 : }
   13175              : 
   13176              : 
   13177              : static void
   13178         2363 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   13179              : {
   13180         2363 :   static gfc_expr **var_expr;
   13181         2363 :   static int total_var = 0;
   13182         2363 :   static int nvar = 0;
   13183         2363 :   int i, old_nvar, tmp;
   13184         2363 :   gfc_forall_iterator *fa;
   13185         2363 :   bool shadow = false;
   13186              : 
   13187         2363 :   old_nvar = nvar;
   13188              : 
   13189              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   13190         2363 :   if (code->op == EXEC_FORALL
   13191         2363 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   13192              :     return;
   13193              : 
   13194              :   /* Start to resolve a FORALL construct   */
   13195              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   13196              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   13197              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   13198              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   13199         2363 :   if (forall_save == 0 && nvar == 0)
   13200              :     {
   13201              :       /* Count the total number of FORALL indices in the nested FORALL
   13202              :          construct in order to allocate the VAR_EXPR with proper size.  */
   13203         2239 :       total_var = gfc_count_forall_iterators (code);
   13204              : 
   13205              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   13206         2239 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   13207              :     }
   13208              : 
   13209              :   /* The information about FORALL iterator, including FORALL indices start,
   13210              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   13211              :      stride.  Check for a shadow index-name.  */
   13212         6656 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   13213              :     {
   13214              :       /* Fortran 2008: C738 (R753).  */
   13215         4293 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   13216              :         {
   13217            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   13218              :                      "of type integer", &fa->var->where);
   13219            2 :           continue;
   13220              :         }
   13221              : 
   13222              :       /* Check if any outer FORALL index name is the same as the current
   13223              :          one.  Skip this check if the iterator is a shadow variable (from
   13224              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   13225         7356 :       for (i = 0; i < nvar; i++)
   13226              :         {
   13227         3065 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   13228         3065 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   13229            0 :             gfc_error ("An outer FORALL construct already has an index "
   13230              :                         "with this name %L", &fa->var->where);
   13231              :         }
   13232              : 
   13233         4291 :       if (fa->shadow)
   13234          128 :         shadow = true;
   13235              : 
   13236              :       /* Record the current FORALL index.  */
   13237         4291 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   13238              : 
   13239         4291 :       nvar++;
   13240              : 
   13241              :       /* No memory leak.  */
   13242         4291 :       gcc_assert (nvar <= total_var);
   13243              :     }
   13244              : 
   13245              :   /* Need to walk the code and replace references to the index-name with
   13246              :      references to the shadow index-name. This must be done BEFORE resolving
   13247              :      the body so that resolution uses the correct shadow variables.  */
   13248         2363 :   if (shadow)
   13249              :     {
   13250              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   13251          262 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   13252              :         {
   13253          134 :           if (fa->shadow)
   13254              :             {
   13255          128 :               gfc_symtree *shadow_st = fa->var->symtree;
   13256              : 
   13257          128 :               gfc_symbol *iter_sym = fa->shadow_outer_sym;
   13258              : 
   13259              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   13260          128 :               if (code->block && code->block->next)
   13261          128 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   13262              :             }
   13263              :         }
   13264              :     }
   13265              : 
   13266              :   /* Resolve the FORALL body.  */
   13267         2363 :   gfc_resolve_forall_body (code, nvar, var_expr);
   13268              : 
   13269              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   13270         2363 :   gfc_resolve_blocks (code->block, ns);
   13271              : 
   13272         2363 :   tmp = nvar;
   13273         2363 :   nvar = old_nvar;
   13274              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   13275         6654 :   for (i = nvar; i < tmp; i++)
   13276         4291 :      gfc_free_expr (var_expr[i]);
   13277              : 
   13278         2363 :   if (nvar == 0)
   13279              :     {
   13280              :       /* We are in the outermost FORALL construct.  */
   13281         2239 :       gcc_assert (forall_save == 0);
   13282              : 
   13283              :       /* VAR_EXPR is not needed any more.  */
   13284         2239 :       free (var_expr);
   13285         2239 :       total_var = 0;
   13286              :     }
   13287              : }
   13288              : 
   13289              : 
   13290              : /* Resolve a BLOCK construct statement.  */
   13291              : 
   13292              : static void
   13293         9013 : resolve_block_construct (gfc_code* code)
   13294              : {
   13295         9013 :   gfc_namespace *ns = code->ext.block.ns;
   13296              : 
   13297              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   13298              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   13299              :      namespace.  However, marking variables as used ans defined requires
   13300              :      passing ext.block.assoc.  */
   13301         9013 :   gfc_resolve (ns, code->ext.block.assoc);
   13302         8882 : }
   13303              : 
   13304              : /* Mark everything in an association list as used and set if applicable,
   13305              :    respectively.  */
   13306              : 
   13307              : static void
   13308       323360 : mark_assoc_used (gfc_association_list *a)
   13309              : {
   13310       330860 :   while (a != NULL)
   13311              :     {
   13312         7500 :       gfc_symbol *n_sym = a->st->n.sym;
   13313         7500 :       if (n_sym->attr.value_used != VALUE_UNUSED)
   13314         5357 :         gfc_value_used_expr (a->target, n_sym->attr.value_used);
   13315              : 
   13316         7500 :       if (a->variable && n_sym->attr.value_set != VALUE_UNSET)
   13317         1426 :         gfc_expr_set_at (a->target, &n_sym->other_loc, n_sym->attr.value_set);
   13318              : 
   13319         7500 :       a = a->next;
   13320              :     }
   13321       323360 : }
   13322              : 
   13323              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   13324              :    DO code nodes.  */
   13325              : 
   13326              : void
   13327       343335 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   13328              : {
   13329       343335 :   bool t;
   13330              : 
   13331       698510 :   for (; b; b = b->block)
   13332              :     {
   13333       355175 :       t = gfc_resolve_expr (b->expr1);
   13334       355175 :       if (!gfc_resolve_expr (b->expr2))
   13335            0 :         t = false;
   13336              : 
   13337       355175 :       switch (b->op)
   13338              :         {
   13339       245206 :         case EXEC_IF:
   13340       245206 :           if (t && b->expr1 != NULL
   13341       240856 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   13342            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   13343              :                        &b->expr1->where);
   13344              :           break;
   13345              : 
   13346          770 :         case EXEC_WHERE:
   13347          770 :           if (t
   13348          770 :               && b->expr1 != NULL
   13349          637 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   13350            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   13351              :                        &b->expr1->where);
   13352              :           break;
   13353              : 
   13354           76 :         case EXEC_GOTO:
   13355           76 :           resolve_branch (b->label1, b);
   13356           76 :           break;
   13357              : 
   13358            0 :         case EXEC_BLOCK:
   13359            0 :           resolve_block_construct (b);
   13360            0 :           break;
   13361              : 
   13362              :         case EXEC_SELECT:
   13363              :         case EXEC_SELECT_TYPE:
   13364              :         case EXEC_SELECT_RANK:
   13365              :         case EXEC_FORALL:
   13366              :         case EXEC_DO:
   13367              :         case EXEC_DO_WHILE:
   13368              :         case EXEC_DO_CONCURRENT:
   13369              :         case EXEC_CRITICAL:
   13370              :         case EXEC_READ:
   13371              :         case EXEC_WRITE:
   13372              :         case EXEC_IOLENGTH:
   13373              :         case EXEC_WAIT:
   13374              :           break;
   13375              : 
   13376         2710 :         case EXEC_OMP_ATOMIC:
   13377         2710 :         case EXEC_OACC_ATOMIC:
   13378         2710 :           {
   13379              :             /* Verify this before calling gfc_resolve_code, which might
   13380              :                change it.  */
   13381         2710 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   13382              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   13383              :           }
   13384              :           break;
   13385              : 
   13386              :         case EXEC_OACC_PARALLEL_LOOP:
   13387              :         case EXEC_OACC_PARALLEL:
   13388              :         case EXEC_OACC_KERNELS_LOOP:
   13389              :         case EXEC_OACC_KERNELS:
   13390              :         case EXEC_OACC_SERIAL_LOOP:
   13391              :         case EXEC_OACC_SERIAL:
   13392              :         case EXEC_OACC_DATA:
   13393              :         case EXEC_OACC_HOST_DATA:
   13394              :         case EXEC_OACC_LOOP:
   13395              :         case EXEC_OACC_UPDATE:
   13396              :         case EXEC_OACC_WAIT:
   13397              :         case EXEC_OACC_CACHE:
   13398              :         case EXEC_OACC_ENTER_DATA:
   13399              :         case EXEC_OACC_EXIT_DATA:
   13400              :         case EXEC_OACC_ROUTINE:
   13401              :         case EXEC_OACC_INIT:
   13402              :         case EXEC_OACC_SHUTDOWN:
   13403              :         case EXEC_OACC_SET:
   13404              :         case EXEC_OMP_ALLOCATE:
   13405              :         case EXEC_OMP_ALLOCATORS:
   13406              :         case EXEC_OMP_ASSUME:
   13407              :         case EXEC_OMP_CRITICAL:
   13408              :         case EXEC_OMP_DISPATCH:
   13409              :         case EXEC_OMP_DISTRIBUTE:
   13410              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13411              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13412              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13413              :         case EXEC_OMP_DO:
   13414              :         case EXEC_OMP_DO_SIMD:
   13415              :         case EXEC_OMP_ERROR:
   13416              :         case EXEC_OMP_LOOP:
   13417              :         case EXEC_OMP_MASKED:
   13418              :         case EXEC_OMP_MASKED_TASKLOOP:
   13419              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13420              :         case EXEC_OMP_MASTER:
   13421              :         case EXEC_OMP_MASTER_TASKLOOP:
   13422              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13423              :         case EXEC_OMP_ORDERED:
   13424              :         case EXEC_OMP_PARALLEL:
   13425              :         case EXEC_OMP_PARALLEL_DO:
   13426              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13427              :         case EXEC_OMP_PARALLEL_LOOP:
   13428              :         case EXEC_OMP_PARALLEL_MASKED:
   13429              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13430              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13431              :         case EXEC_OMP_PARALLEL_MASTER:
   13432              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13433              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13434              :         case EXEC_OMP_PARALLEL_SECTIONS:
   13435              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13436              :         case EXEC_OMP_SECTIONS:
   13437              :         case EXEC_OMP_SIMD:
   13438              :         case EXEC_OMP_SCOPE:
   13439              :         case EXEC_OMP_SINGLE:
   13440              :         case EXEC_OMP_TARGET:
   13441              :         case EXEC_OMP_TARGET_DATA:
   13442              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13443              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13444              :         case EXEC_OMP_TARGET_PARALLEL:
   13445              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13446              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13447              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13448              :         case EXEC_OMP_TARGET_SIMD:
   13449              :         case EXEC_OMP_TARGET_TEAMS:
   13450              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13451              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13452              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13453              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13454              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13455              :         case EXEC_OMP_TARGET_UPDATE:
   13456              :         case EXEC_OMP_TASK:
   13457              :         case EXEC_OMP_TASKGROUP:
   13458              :         case EXEC_OMP_TASKLOOP:
   13459              :         case EXEC_OMP_TASKLOOP_SIMD:
   13460              :         case EXEC_OMP_TASKWAIT:
   13461              :         case EXEC_OMP_TASKYIELD:
   13462              :         case EXEC_OMP_TEAMS:
   13463              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13464              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13465              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13466              :         case EXEC_OMP_TEAMS_LOOP:
   13467              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13468              :         case EXEC_OMP_TILE:
   13469              :         case EXEC_OMP_UNROLL:
   13470              :         case EXEC_OMP_WORKSHARE:
   13471              :           break;
   13472              : 
   13473            0 :         default:
   13474            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13475              :         }
   13476       355175 :       gfc_value_used_expr (b->expr1, VALUE_USED);
   13477       355175 :       gfc_value_used_expr (b->expr2, VALUE_USED);
   13478       355175 :       gfc_resolve_code (b->next, ns);
   13479              :     }
   13480       343335 : }
   13481              : 
   13482              : bool
   13483            0 : caf_possible_reallocate (gfc_expr *e)
   13484              : {
   13485            0 :   symbol_attribute caf_attr;
   13486            0 :   gfc_ref *last_arr_ref = nullptr;
   13487              : 
   13488            0 :   caf_attr = gfc_caf_attr (e);
   13489            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13490              :     return false;
   13491              : 
   13492              :   /* Only full array refs can indicate a needed reallocation.  */
   13493            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13494            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13495            0 :       last_arr_ref = ref;
   13496              : 
   13497            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13498              : }
   13499              : 
   13500              : /* Does everything to resolve an ordinary assignment.  Returns true
   13501              :    if this is an interface assignment.  */
   13502              : static bool
   13503       292879 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13504              : {
   13505       292879 :   bool rval = false;
   13506       292879 :   gfc_expr *lhs;
   13507       292879 :   gfc_expr *rhs;
   13508       292879 :   int n;
   13509       292879 :   gfc_ref *ref;
   13510       292879 :   symbol_attribute attr;
   13511              : 
   13512       292879 :   if (gfc_extend_assign (code, ns))
   13513              :     {
   13514          942 :       gfc_expr** rhsptr;
   13515              : 
   13516          942 :       if (code->op == EXEC_ASSIGN_CALL)
   13517              :         {
   13518          469 :           lhs = code->ext.actual->expr;
   13519          469 :           rhsptr = &code->ext.actual->next->expr;
   13520              :         }
   13521              :       else
   13522              :         {
   13523          473 :           gfc_actual_arglist* args;
   13524          473 :           gfc_typebound_proc* tbp;
   13525              : 
   13526          473 :           gcc_assert (code->op == EXEC_COMPCALL);
   13527              : 
   13528          473 :           args = code->expr1->value.compcall.actual;
   13529          473 :           lhs = args->expr;
   13530          473 :           rhsptr = &args->next->expr;
   13531              : 
   13532          473 :           tbp = code->expr1->value.compcall.tbp;
   13533          473 :           gcc_assert (!tbp->is_generic);
   13534              :         }
   13535              : 
   13536              :       /* Make a temporary rhs when there is a default initializer
   13537              :          and rhs is the same symbol as the lhs.  */
   13538          942 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13539          531 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13540          460 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13541         1254 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13542           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13543              : 
   13544              :       return true;
   13545              :     }
   13546              : 
   13547       291937 :   lhs = code->expr1;
   13548       291937 :   rhs = code->expr2;
   13549              : 
   13550       291937 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13551       270155 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13552        24829 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13553       316766 :       && gfc_expr_attr (lhs).proc_pointer)
   13554              :     {
   13555            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13556              :                  "pointer component",
   13557              :                  &lhs->where);
   13558            1 :       return false;
   13559              :     }
   13560              : 
   13561       344482 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13562       254932 :       && rhs->ts.type == BT_CHARACTER
   13563       292329 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13564              :     {
   13565              :       /* Use of -fdec-char-conversions allows assignment of character data
   13566              :          to non-character variables.  This not permitted for nonconstant
   13567              :          strings.  */
   13568           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13569              :                  gfc_typename (lhs), &rhs->where);
   13570           29 :       return false;
   13571              :     }
   13572              : 
   13573       291907 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13574              :     {
   13575            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13576              :                    gfc_typename (lhs), &rhs->where);
   13577            0 :       return false;
   13578              :     }
   13579              : 
   13580              :   /* Handle the case of a BOZ literal on the RHS.  */
   13581       291907 :   if (rhs->ts.type == BT_BOZ)
   13582              :     {
   13583            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13584              :                            "statement value nor an actual argument of "
   13585              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13586              :                            &rhs->where))
   13587              :         return false;
   13588              : 
   13589            1 :       switch (lhs->ts.type)
   13590              :         {
   13591            0 :         case BT_INTEGER:
   13592            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13593              :             return false;
   13594              :           break;
   13595            1 :         case BT_REAL:
   13596            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13597              :             return false;
   13598              :           break;
   13599            0 :         default:
   13600            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13601            0 :           return false;
   13602              :         }
   13603              :     }
   13604              : 
   13605       291905 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13606              :     {
   13607           67 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13608           67 :       if (lhs->ts.u.cl != NULL
   13609           67 :             && lhs->ts.u.cl->length != NULL
   13610           61 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13611           61 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13612              : 
   13613           67 :       if (rhs->expr_type == EXPR_CONSTANT)
   13614           29 :         rlen = rhs->value.character.length;
   13615              : 
   13616           38 :       else if (rhs->ts.u.cl != NULL
   13617           38 :                  && rhs->ts.u.cl->length != NULL
   13618           36 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13619           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13620              : 
   13621           67 :       if (rlen && llen && rlen > llen)
   13622           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13623              :                          "CHARACTER expression will be truncated "
   13624              :                          "in assignment (%wd/%wd) at %L",
   13625              :                          llen, rlen, &code->loc);
   13626              :     }
   13627              : 
   13628              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13629              :      to a temporary if the lvalue symbol is referenced in it.  */
   13630       291905 :   if (lhs->rank)
   13631              :     {
   13632       117582 :       for (ref = lhs->ref; ref; ref= ref->next)
   13633        63173 :         if (ref->type == REF_ARRAY)
   13634              :           {
   13635       137471 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13636        81086 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13637        81316 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13638          230 :                                            ref->u.ar.start[n]))
   13639           14 :                 ref->u.ar.start[n]
   13640           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13641              :           }
   13642              :     }
   13643              : 
   13644       291905 :   if (gfc_pure (NULL))
   13645              :     {
   13646         3666 :       if (lhs->ts.type == BT_DERIVED
   13647          139 :             && lhs->expr_type == EXPR_VARIABLE
   13648          139 :             && lhs->ts.u.derived->attr.pointer_comp
   13649            4 :             && rhs->expr_type == EXPR_VARIABLE
   13650         3669 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13651            2 :                 || gfc_is_coindexed (rhs)))
   13652              :         {
   13653              :           /* F2008, C1283.  */
   13654            2 :           if (gfc_is_coindexed (rhs))
   13655            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13656              :                         "a derived type variable with a POINTER "
   13657              :                         "component in a PURE procedure",
   13658              :                         &rhs->where);
   13659              :           else
   13660              :           /* F2008, C1283 (4).  */
   13661            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13662              :                         "shall not be used as the expr at %L of an intrinsic "
   13663              :                         "assignment statement in which the variable is of a "
   13664              :                         "derived type if the derived type has a pointer "
   13665              :                         "component at any level of component selection.",
   13666              :                         &rhs->where);
   13667              :           return rval;
   13668              :         }
   13669              : 
   13670              :       /* Fortran 2008, C1283.  */
   13671         3664 :       if (gfc_is_coindexed (lhs))
   13672              :         {
   13673            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13674              :                      "procedure", &rhs->where);
   13675            1 :           return rval;
   13676              :         }
   13677              :     }
   13678              : 
   13679       291902 :   if (gfc_implicit_pure (NULL))
   13680              :     {
   13681         7976 :       if (lhs->expr_type == EXPR_VARIABLE
   13682         7976 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13683         5722 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13684          269 :         gfc_unset_implicit_pure (NULL);
   13685              : 
   13686         7976 :       if (lhs->ts.type == BT_DERIVED
   13687          385 :             && lhs->expr_type == EXPR_VARIABLE
   13688          385 :             && lhs->ts.u.derived->attr.pointer_comp
   13689            7 :             && rhs->expr_type == EXPR_VARIABLE
   13690         7983 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13691            7 :                 || gfc_is_coindexed (rhs)))
   13692            0 :         gfc_unset_implicit_pure (NULL);
   13693              : 
   13694              :       /* Fortran 2008, C1283.  */
   13695         7976 :       if (gfc_is_coindexed (lhs))
   13696            0 :         gfc_unset_implicit_pure (NULL);
   13697              :     }
   13698              : 
   13699              :   /* F2008, 7.2.1.2.  */
   13700       291902 :   attr = gfc_expr_attr (lhs);
   13701       291902 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13702              :     {
   13703         1096 :       if (attr.codimension)
   13704              :         {
   13705            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13706              :                      "permitted", &lhs->where);
   13707            1 :           return false;
   13708              :         }
   13709         1095 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13710              :                            "polymorphic variable at %L", &lhs->where))
   13711              :         return false;
   13712         1094 :       if (!flag_realloc_lhs)
   13713              :         {
   13714            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13715              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13716            1 :           return false;
   13717              :         }
   13718              :     }
   13719       290806 :   else if (lhs->ts.type == BT_CLASS)
   13720              :     {
   13721            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13722              :                  "assignment at %L - check that there is a matching specific "
   13723              :                  "subroutine for %<=%> operator", &lhs->where);
   13724            9 :       return false;
   13725              :     }
   13726              : 
   13727       291890 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13728              : 
   13729              :   /* F2008, Section 7.2.1.2.  */
   13730       291890 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13731              :     {
   13732            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13733              :                  "component in assignment at %L", &lhs->where);
   13734            1 :       return false;
   13735              :     }
   13736              : 
   13737              :   /* Assign the 'data' of a class object to a derived type.  */
   13738       291889 :   if (lhs->ts.type == BT_DERIVED
   13739         7831 :       && rhs->ts.type == BT_CLASS
   13740          192 :       && (rhs->expr_type != EXPR_ARRAY
   13741          192 :           && rhs->expr_type != EXPR_OP))
   13742          186 :     gfc_add_data_component (rhs);
   13743              : 
   13744              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13745              :      rhs type.  */
   13746       291889 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13747          689 :     gfc_find_vtab (&rhs->ts);
   13748              : 
   13749       291889 :   gfc_check_assign (lhs, rhs, 1);
   13750              : 
   13751       291889 :   return false;
   13752              : }
   13753              : 
   13754              : 
   13755              : /* Add a component reference onto an expression.  */
   13756              : 
   13757              : static void
   13758          731 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13759              : {
   13760          731 :   gfc_ref **ref;
   13761          731 :   ref = &(e->ref);
   13762          955 :   while (*ref)
   13763          224 :     ref = &((*ref)->next);
   13764          731 :   *ref = gfc_get_ref ();
   13765          731 :   (*ref)->type = REF_COMPONENT;
   13766          731 :   (*ref)->u.c.sym = e->ts.u.derived;
   13767          731 :   (*ref)->u.c.component = c;
   13768          731 :   e->ts = c->ts;
   13769              : 
   13770              :   /* Add a full array ref, as necessary.  */
   13771          731 :   if (c->as)
   13772              :     {
   13773           84 :       gfc_add_full_array_ref (e, c->as);
   13774           84 :       e->rank = c->as->rank;
   13775           84 :       e->corank = c->as->corank;
   13776              :     }
   13777          731 : }
   13778              : 
   13779              : 
   13780              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13781              :    pointer assignments can be made.  */
   13782              : 
   13783              : static gfc_code *
   13784         1066 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13785              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13786              : {
   13787         1066 :   gfc_code *this_code;
   13788              : 
   13789         1066 :   this_code = gfc_get_code (op);
   13790         1066 :   this_code->next = NULL;
   13791         1066 :   this_code->expr1 = gfc_copy_expr (expr1);
   13792         1066 :   this_code->expr2 = gfc_copy_expr (expr2);
   13793         1066 :   this_code->loc = loc;
   13794         1066 :   if (comp1 && comp2)
   13795              :     {
   13796          324 :       add_comp_ref (this_code->expr1, comp1);
   13797          324 :       add_comp_ref (this_code->expr2, comp2);
   13798              :     }
   13799              : 
   13800         1066 :   return this_code;
   13801              : }
   13802              : 
   13803              : 
   13804              : /* Makes a temporary variable expression based on the characteristics of
   13805              :    a given variable expression.  If allocatable is set, the temporary is
   13806              :    unconditionally allocatable*/
   13807              : 
   13808              : static gfc_expr*
   13809          554 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13810              :                     bool allocatable = false)
   13811              : {
   13812          554 :   static int serial = 0;
   13813          554 :   char name[GFC_MAX_SYMBOL_LEN];
   13814          554 :   gfc_symtree *tmp;
   13815          554 :   gfc_array_spec *as;
   13816          554 :   gfc_array_ref *aref;
   13817          554 :   gfc_ref *ref;
   13818              : 
   13819          554 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13820          554 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13821          554 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13822              : 
   13823          554 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13824            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13825              :                                                     NULL,
   13826            0 :                                                     e->value.character.length);
   13827              : 
   13828          554 :   as = NULL;
   13829          554 :   ref = NULL;
   13830          554 :   aref = NULL;
   13831              : 
   13832              :   /* Obtain the arrayspec for the temporary.  */
   13833          554 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13834              :        && e->expr_type != EXPR_FUNCTION
   13835              :        && e->expr_type != EXPR_OP)
   13836              :     {
   13837           52 :       aref = gfc_find_array_ref (e);
   13838           52 :       if (e->expr_type == EXPR_VARIABLE
   13839           52 :           && e->symtree->n.sym->as == aref->as)
   13840              :         as = aref->as;
   13841              :       else
   13842              :         {
   13843            0 :           for (ref = e->ref; ref; ref = ref->next)
   13844            0 :             if (ref->type == REF_COMPONENT
   13845            0 :                 && ref->u.c.component->as == aref->as)
   13846              :               {
   13847              :                 as = aref->as;
   13848              :                 break;
   13849              :               }
   13850              :         }
   13851              :     }
   13852              : 
   13853              :   /* Add the attributes and the arrayspec to the temporary.  */
   13854          554 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13855          554 :   tmp->n.sym->attr.function = 0;
   13856          554 :   tmp->n.sym->attr.proc_pointer = 0;
   13857          554 :   tmp->n.sym->attr.result = 0;
   13858          554 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13859          554 :   tmp->n.sym->attr.dummy = 0;
   13860          554 :   tmp->n.sym->attr.use_assoc = 0;
   13861          554 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13862              : 
   13863              : 
   13864          554 :   if (as && !allocatable)
   13865              :     {
   13866           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13867           52 :       if (!ref)
   13868           52 :         ref = e->ref;
   13869           52 :       if (as->type == AS_DEFERRED)
   13870           46 :         tmp->n.sym->attr.allocatable = 1;
   13871              :     }
   13872          502 :   else if ((e->rank || e->corank)
   13873          136 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13874           24 :                || e->expr_type == EXPR_OP || allocatable))
   13875              :     {
   13876          136 :       tmp->n.sym->as = gfc_get_array_spec ();
   13877          136 :       tmp->n.sym->as->type = AS_DEFERRED;
   13878          136 :       tmp->n.sym->as->rank = e->rank;
   13879          136 :       tmp->n.sym->as->corank = e->corank;
   13880          136 :       tmp->n.sym->attr.allocatable = 1;
   13881          136 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13882          272 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13883              :     }
   13884              :   else
   13885          366 :     tmp->n.sym->attr.dimension = 0;
   13886              : 
   13887          554 :   gfc_set_sym_referenced (tmp->n.sym);
   13888          554 :   gfc_commit_symbol (tmp->n.sym);
   13889          554 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13890              : 
   13891              :   /* Should the lhs be a section, use its array ref for the
   13892              :      temporary expression.  */
   13893          554 :   if (aref && aref->type != AR_FULL && !allocatable)
   13894              :     {
   13895            6 :       gfc_free_ref_list (e->ref);
   13896            6 :       e->ref = gfc_copy_ref (ref);
   13897              :     }
   13898          554 :   return e;
   13899              : }
   13900              : 
   13901              : 
   13902              : gfc_expr*
   13903           54 : gfc_get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13904              :                         bool allocatable = false)
   13905              : {
   13906           54 :   return get_temp_from_expr (e, ns, allocatable);
   13907              : }
   13908              : 
   13909              : 
   13910              : /* Helper function to take an argument in a subroutine call with a dependency
   13911              :    on another argument, copy it to an allocatable temporary and use the
   13912              :    temporary in the call expression. The new code is embedded in a block to
   13913              :    ensure local, automatic deallocation.  */
   13914              : 
   13915              : static void
   13916           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13917              :                              gfc_expr **rhsptr)
   13918              : {
   13919           36 :   gfc_namespace *block_ns;
   13920           36 :   gfc_expr *tmp_var;
   13921              : 
   13922              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13923           36 :   block_ns = gfc_build_block_ns (ns);
   13924              : 
   13925              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13926              :      the assignment would be converted to a call and, in any case, the modified
   13927              :      call fails in gfc_check_conformance.  */
   13928           36 :   block_ns->resolved = 1;
   13929              : 
   13930              :   /* Assign the original expression to the temporary.  */
   13931           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13932           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13933           36 :                                      NULL, NULL, (*rhsptr)->where);
   13934              : 
   13935              :   /* Transfer the call to the block and terminate block code.  */
   13936           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13937           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13938           36 :   *(block_ns->code->next) = *code;
   13939           36 :   block_ns->code->next->next = NULL;
   13940              : 
   13941              :   /* Convert the original code to execute the block.  */
   13942           36 :   code->op = EXEC_BLOCK;
   13943           36 :   code->ext.block.ns = block_ns;
   13944           36 :   code->ext.block.assoc = NULL;
   13945           36 :   code->expr1 = code->expr2 = NULL;
   13946           36 : }
   13947              : 
   13948              : 
   13949              : /* Add one line of code to the code chain, making sure that 'head' and
   13950              :    'tail' are appropriately updated.  */
   13951              : 
   13952              : static void
   13953          752 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13954              : {
   13955          752 :   gcc_assert (this_code);
   13956          752 :   if (*head == NULL)
   13957          338 :     *head = *tail = *this_code;
   13958              :   else
   13959          414 :     *tail = gfc_append_code (*tail, *this_code);
   13960          752 :   *this_code = NULL;
   13961          752 : }
   13962              : 
   13963              : 
   13964              : /* Generate a final call from a variable expression  */
   13965              : 
   13966              : static void
   13967           99 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13968              : {
   13969           99 :   gfc_code *this_code;
   13970           99 :   gfc_expr *final_expr = NULL;
   13971           99 :   gfc_expr *size_expr;
   13972           99 :   gfc_expr *fini_coarray;
   13973              : 
   13974           99 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13975           99 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13976           75 :     return;
   13977              : 
   13978              :   /* Now generate the finalizer call.  */
   13979           24 :   this_code = gfc_get_code (EXEC_CALL);
   13980           24 :   this_code->symtree = final_expr->symtree;
   13981           24 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13982              : 
   13983              :   //* Expression to be finalized  */
   13984           24 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13985           24 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13986              : 
   13987              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13988           24 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13989           24 :   size_expr = gfc_get_expr ();
   13990           24 :   size_expr->where = gfc_current_locus;
   13991           24 :   size_expr->expr_type = EXPR_OP;
   13992           24 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13993           24 :   size_expr->value.op.op1
   13994           48 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13995              :                                     "storage_size", gfc_current_locus, 2,
   13996           24 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13997              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13998              :                                                       NULL, 0));
   13999           24 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   14000              :                                               gfc_character_storage_size);
   14001           24 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   14002           24 :   size_expr->ts = size_expr->value.op.op1->ts;
   14003           24 :   this_code->ext.actual->next->expr = size_expr;
   14004              : 
   14005              :   /* fini_coarray  */
   14006           24 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   14007           24 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   14008              :                                         &tmp_expr->where);
   14009           24 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   14010           24 :   this_code->ext.actual->next->next->expr = fini_coarray;
   14011              : 
   14012           24 :   add_code_to_chain (&this_code, head, tail);
   14013              : 
   14014              : }
   14015              : 
   14016              : /* Counts the potential number of part array references that would
   14017              :    result from resolution of typebound defined assignments.  */
   14018              : 
   14019              : 
   14020              : static int
   14021          267 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   14022              : {
   14023          267 :   gfc_component *c;
   14024          267 :   int c_depth = 0, t_depth;
   14025              : 
   14026          662 :   for (c= derived->components; c; c = c->next)
   14027              :     {
   14028          395 :       if ((!gfc_bt_struct (c->ts.type)
   14029          285 :             || c->attr.pointer
   14030          285 :             || c->attr.allocatable
   14031          284 :             || c->attr.proc_pointer_comp
   14032          284 :             || c->attr.class_pointer
   14033          284 :             || c->attr.proc_pointer)
   14034          111 :           && !c->attr.defined_assign_comp)
   14035          111 :         continue;
   14036              : 
   14037          284 :       if (c->as && c_depth == 0)
   14038          284 :         c_depth = 1;
   14039              : 
   14040          284 :       if (c->ts.u.derived->attr.defined_assign_comp)
   14041          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   14042              :                                               c->as ? 1 : 0);
   14043              :       else
   14044              :         t_depth = 0;
   14045              : 
   14046          284 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   14047              :     }
   14048          267 :   return depth + c_depth;
   14049              : }
   14050              : 
   14051              : 
   14052              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   14053              :    "An intrinsic assignment where the variable is of derived type is performed
   14054              :     as if each component of the variable were assigned from the corresponding
   14055              :     component of expr using pointer assignment (10.2.2) for each pointer
   14056              :     component, defined assignment for each nonpointer nonallocatable component
   14057              :     of a type that has a type-bound defined assignment consistent with the
   14058              :     component, intrinsic assignment for each other nonpointer nonallocatable
   14059              :     component, and intrinsic assignment for each allocated coarray component.
   14060              :     For unallocated coarray components, the corresponding component of the
   14061              :     variable shall be unallocated. For a noncoarray allocatable component the
   14062              :     following sequence of operations is applied.
   14063              :         (1) If the component of the variable is allocated, it is deallocated.
   14064              :         (2) If the component of the value of expr is allocated, the
   14065              :             corresponding component of the variable is allocated with the same
   14066              :             dynamic type and type parameters as the component of the value of
   14067              :             expr. If it is an array, it is allocated with the same bounds. The
   14068              :             value of the component of the value of expr is then assigned to the
   14069              :             corresponding component of the variable using defined assignment if
   14070              :             the declared type of the component has a type-bound defined
   14071              :             assignment consistent with the component, and intrinsic assignment
   14072              :             for the dynamic type of that component otherwise."
   14073              : 
   14074              :    The pointer assignments are taken care of by the intrinsic assignment of the
   14075              :    structure itself.  This function recursively adds defined assignments where
   14076              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   14077              : 
   14078              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   14079              :    and the component of 'var' is finalizable, we need a temporary for the
   14080              :    lhs.  In pseudo-code for an assignment var = expr:
   14081              : 
   14082              :    ! Confine finalization of temporaries, as far as possible.
   14083              :      Enclose the code for the assignment in a block
   14084              :    ! Only call function 'expr' once.
   14085              :       #if ('expr is not a constant or an variable)
   14086              :         temp_expr = expr
   14087              :         expr = temp_x
   14088              :    ! Do the intrinsic assignment
   14089              :       #if typeof ('var') has a typebound final subroutine
   14090              :         finalize (var)
   14091              :       var = expr
   14092              :    ! Now do the component assignments
   14093              :       #do over derived type components [%cmp]
   14094              :         #if (cmp is a pointer of any kind)
   14095              :           continue
   14096              :         build the assignment
   14097              :         resolve the code
   14098              :         #if the code is a typebound assignment
   14099              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   14100              :              t1 = var
   14101              :              arg1 = t1
   14102              :              deal with allocatation or not of var and this component
   14103              :         #elseif the code is an assignment by itself
   14104              :            #if this component does not need finalization
   14105              :              delete code and continue
   14106              :         #else
   14107              :            remove the leading assignment
   14108              :         #endif
   14109              :         commit the code
   14110              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   14111              :            var%cmp = t1%cmp
   14112              :       #enddo
   14113              :       put all code chunks involving t1 to the top of the generated code
   14114              :       insert the generated block in place of the original code
   14115              : */
   14116              : 
   14117              : static bool
   14118          429 : is_finalizable_type (gfc_typespec ts)
   14119              : {
   14120          429 :   gfc_component *c;
   14121              : 
   14122          429 :   if (ts.type != BT_DERIVED)
   14123              :     return false;
   14124              : 
   14125              :   /* (1) Check for FINAL subroutines.  */
   14126          429 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   14127              :     return true;
   14128              : 
   14129              :   /* (2) Check for components of finalizable type.  */
   14130          839 :   for (c = ts.u.derived->components; c; c = c->next)
   14131          494 :     if (c->ts.type == BT_DERIVED
   14132          261 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   14133          260 :         && c->ts.u.derived->f2k_derived
   14134          260 :         && c->ts.u.derived->f2k_derived->finalizers)
   14135              :       return true;
   14136              : 
   14137              :   return false;
   14138              : }
   14139              : 
   14140              : /* The temporary assignments have to be put on top of the additional
   14141              :    code to avoid the result being changed by the intrinsic assignment.
   14142              :    */
   14143              : static int component_assignment_level = 0;
   14144              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   14145              : static bool finalizable_comp;
   14146              : 
   14147              : static void
   14148          212 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   14149              : {
   14150          212 :   gfc_component *comp1, *comp2;
   14151          212 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   14152          212 :   gfc_code *tmp_code = NULL;
   14153          212 :   gfc_expr *t1 = NULL;
   14154          212 :   gfc_expr *tmp_expr = NULL;
   14155          212 :   int error_count, depth;
   14156          212 :   bool finalizable_lhs;
   14157          212 :   bool use_finalize_only;
   14158              : 
   14159          212 :   gfc_get_errors (NULL, &error_count);
   14160              : 
   14161              :   /* Filter out continuing processing after an error.  */
   14162          212 :   if (error_count
   14163          212 :       || (*code)->expr1->ts.type != BT_DERIVED
   14164          212 :       || (*code)->expr2->ts.type != BT_DERIVED)
   14165          164 :     return;
   14166              : 
   14167              :   /* TODO: Handle more than one part array reference in assignments.  */
   14168          212 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   14169          212 :                                       (*code)->expr1->rank ? 1 : 0);
   14170          212 :   if (depth > 1)
   14171              :     {
   14172            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   14173              :                    "done because multiple part array references would "
   14174              :                    "occur in intermediate expressions.", &(*code)->loc);
   14175            6 :       return;
   14176              :     }
   14177              : 
   14178          206 :   if (!component_assignment_level)
   14179          158 :     finalizable_comp = true;
   14180              : 
   14181              :   /* Build a block so that function result temporaries are finalized
   14182              :      locally on exiting the rather than enclosing scope.  */
   14183          206 :   if (!component_assignment_level)
   14184              :     {
   14185          158 :       ns = gfc_build_block_ns (ns);
   14186          158 :       tmp_code = gfc_get_code (EXEC_NOP);
   14187          158 :       *tmp_code = **code;
   14188          158 :       tmp_code->next = NULL;
   14189          158 :       (*code)->op = EXEC_BLOCK;
   14190          158 :       (*code)->ext.block.ns = ns;
   14191          158 :       (*code)->ext.block.assoc = NULL;
   14192          158 :       (*code)->expr1 = (*code)->expr2 = NULL;
   14193          158 :       ns->code = tmp_code;
   14194          158 :       code = &ns->code;
   14195              :     }
   14196              : 
   14197          206 :   component_assignment_level++;
   14198              : 
   14199          206 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   14200              : 
   14201              :   /* When the lhs is finalized as a whole and none of its components needs the
   14202              :      structure copy to handle it (no pointer or allocatable components), the
   14203              :      copy can be done component by component.  The whole-derived-type assignment
   14204              :      then only finalizes the lhs and a component with a defined assignment keeps
   14205              :      its post-finalization value for the INTENT (OUT) finalization in that
   14206              :      defined assignment.  */
   14207          206 :   use_finalize_only = finalizable_lhs;
   14208          206 :   if (use_finalize_only)
   14209          120 :     for (comp1 = (*code)->expr1->ts.u.derived->components; comp1;
   14210           78 :          comp1 = comp1->next)
   14211           90 :       if (comp1->attr.pointer || comp1->attr.allocatable
   14212           78 :           || comp1->attr.proc_pointer_comp || comp1->attr.class_pointer
   14213           78 :           || comp1->attr.proc_pointer)
   14214              :         {
   14215              :           use_finalize_only = false;
   14216              :           break;
   14217              :         }
   14218              : 
   14219              :   /* Create a temporary so that functions get called only once.  */
   14220          206 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   14221          206 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   14222              :     {
   14223              :       /* Assign the rhs to the temporary.  */
   14224           99 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14225           99 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   14226              :         {
   14227              :           /* Use allocate on assignment for the sake of simplicity. The
   14228              :              temporary must not take on the optional attribute. Assume
   14229              :              that the assignment is guarded by a PRESENT condition if the
   14230              :              lhs is optional.  */
   14231           31 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   14232           31 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   14233           31 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   14234              :         }
   14235          198 :       this_code = build_assignment (EXEC_ASSIGN,
   14236              :                                     tmp_expr, (*code)->expr2,
   14237           99 :                                     NULL, NULL, (*code)->loc);
   14238           99 :       this_code->expr2->must_finalize = 1;
   14239              :       /* Add the code and substitute the rhs expression.  */
   14240           99 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   14241           99 :       gfc_free_expr ((*code)->expr2);
   14242           99 :       (*code)->expr2 = tmp_expr;
   14243              :     }
   14244              : 
   14245              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   14246              :      of the temporaries generated here, since the intrinsic assignment
   14247              :      to the final result already does this.  */
   14248          206 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   14249              :     {
   14250          206 :       if (finalizable_lhs)
   14251           42 :         (*code)->expr1->must_finalize = 1;
   14252          206 :       this_code = build_assignment (EXEC_ASSIGN,
   14253              :                                     (*code)->expr1, (*code)->expr2,
   14254              :                                     NULL, NULL, (*code)->loc);
   14255          206 :       if (use_finalize_only)
   14256           30 :         this_code->expr1->finalize_only = 1;
   14257          206 :       add_code_to_chain (&this_code, &head, &tail);
   14258              :     }
   14259              : 
   14260          206 :   comp1 = (*code)->expr1->ts.u.derived->components;
   14261          206 :   comp2 = (*code)->expr2->ts.u.derived->components;
   14262              : 
   14263          527 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   14264              :     {
   14265          321 :       bool inout = false;
   14266          321 :       bool finalizable_out = false;
   14267              : 
   14268              :       /* The intrinsic assignment does the right thing for pointers
   14269              :          of all kinds and allocatable components.  */
   14270          321 :       if (!gfc_bt_struct (comp1->ts.type)
   14271          224 :           || comp1->attr.pointer
   14272          224 :           || comp1->attr.allocatable
   14273          223 :           || comp1->attr.proc_pointer_comp
   14274          223 :           || comp1->attr.class_pointer
   14275          223 :           || comp1->attr.proc_pointer)
   14276              :         {
   14277              :           /* With finalize_only the whole-derived-type assignment does not copy
   14278              :              the components, so emit the copy for this one here.  Only plain
   14279              :              components reach this point, since use_finalize_only excludes
   14280              :              pointer and allocatable components.  */
   14281           98 :           if (use_finalize_only)
   14282              :             {
   14283           36 :               this_code = build_assignment (EXEC_ASSIGN,
   14284              :                                             (*code)->expr1, (*code)->expr2,
   14285           18 :                                             comp1, comp2, (*code)->loc);
   14286           18 :               add_code_to_chain (&this_code, &head, &tail);
   14287              :             }
   14288           98 :           continue;
   14289              :         }
   14290              : 
   14291          446 :       finalizable_comp = is_finalizable_type (comp1->ts)
   14292          223 :                          && !finalizable_lhs;
   14293              : 
   14294              :       /* Make an assignment for this component.  */
   14295          446 :       this_code = build_assignment (EXEC_ASSIGN,
   14296              :                                     (*code)->expr1, (*code)->expr2,
   14297          223 :                                     comp1, comp2, (*code)->loc);
   14298              : 
   14299              :       /* Convert the assignment if there is a defined assignment for
   14300              :          this type.  Otherwise, using the call from gfc_resolve_code,
   14301              :          recurse into its components.  */
   14302          223 :       gfc_resolve_code (this_code, ns);
   14303              : 
   14304          223 :       if (this_code->op == EXEC_ASSIGN_CALL)
   14305              :         {
   14306          168 :           gfc_formal_arglist *dummy_args;
   14307          168 :           gfc_symbol *rsym;
   14308              :           /* Check that there is a typebound defined assignment.  If not,
   14309              :              then this must be a module defined assignment.  We cannot
   14310              :              use the defined_assign_comp attribute here because it must
   14311              :              be this derived type that has the defined assignment and not
   14312              :              a parent type.  */
   14313          168 :           if (!(comp1->ts.u.derived->f2k_derived
   14314              :                 && comp1->ts.u.derived->f2k_derived
   14315          168 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   14316              :             {
   14317            1 :               gfc_free_statements (this_code);
   14318            1 :               this_code = NULL;
   14319            1 :               continue;
   14320              :             }
   14321              : 
   14322              :           /* If the first argument of the subroutine has intent INOUT
   14323              :              a temporary must be generated and used instead.  */
   14324          167 :           rsym = this_code->resolved_sym;
   14325          167 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   14326          298 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   14327           36 :                             && dummy_args
   14328          203 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   14329          310 :           inout = dummy_args
   14330          298 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   14331              :           /* With finalize_only the lhs component keeps its post-finalization
   14332              :              value, so the defined assignment can finalize it directly through
   14333              :              its INTENT (OUT) argument and no temporary is needed.  */
   14334           96 :           if ((inout || (finalizable_out && !use_finalize_only))
   14335           83 :               && !comp1->attr.allocatable)
   14336              :             {
   14337           83 :               gfc_code *temp_code;
   14338           83 :               inout = true;
   14339              : 
   14340              :               /* Build the temporary required for the assignment and put
   14341              :                  it at the head of the generated code.  */
   14342           83 :               if (!t1)
   14343              :                 {
   14344           83 :                   gfc_namespace *tmp_ns = ns;
   14345           83 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   14346           12 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   14347           83 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   14348           83 :                   t1->symtree->n.sym->attr.artificial = 1;
   14349              :                   /* The temporary is a copy of the lhs, not a pointer.  */
   14350           83 :                   if (t1->rank == 0)
   14351           77 :                     t1->symtree->n.sym->attr.pointer = 0;
   14352          166 :                   temp_code = build_assignment (EXEC_ASSIGN,
   14353              :                                                 t1, (*code)->expr1,
   14354           83 :                                 NULL, NULL, (*code)->loc);
   14355              : 
   14356              :                   /* For allocatable LHS, check whether it is allocated.  Note
   14357              :                      that allocatable components with defined assignment are
   14358              :                      not yet support.  See PR 57696.  */
   14359           83 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   14360              :                     {
   14361           24 :                       gfc_code *block;
   14362           24 :                       gfc_expr *e =
   14363           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14364           24 :                       block = gfc_get_code (EXEC_IF);
   14365           24 :                       block->block = gfc_get_code (EXEC_IF);
   14366           24 :                       block->block->expr1
   14367           48 :                           = gfc_build_intrinsic_call (ns,
   14368              :                                     GFC_ISYM_ALLOCATED, "allocated",
   14369           24 :                                     (*code)->loc, 1, e);
   14370           24 :                       block->block->next = temp_code;
   14371           24 :                       temp_code = block;
   14372              :                     }
   14373           83 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   14374              :                 }
   14375              : 
   14376              :               /* Replace the first actual arg with the component of the
   14377              :                  temporary.  */
   14378           83 :               gfc_free_expr (this_code->ext.actual->expr);
   14379           83 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   14380           83 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   14381              : 
   14382              :               /* If the LHS variable is allocatable and wasn't allocated and
   14383              :                  the temporary is allocatable, pointer assign the address of
   14384              :                  the freshly allocated LHS to the temporary.  */
   14385           83 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   14386           83 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   14387              :                 {
   14388           18 :                   gfc_code *block;
   14389           18 :                   gfc_expr *cond;
   14390              : 
   14391           18 :                   cond = gfc_get_expr ();
   14392           18 :                   cond->ts.type = BT_LOGICAL;
   14393           18 :                   cond->ts.kind = gfc_default_logical_kind;
   14394           18 :                   cond->expr_type = EXPR_OP;
   14395           18 :                   cond->where = (*code)->loc;
   14396           18 :                   cond->value.op.op = INTRINSIC_NOT;
   14397           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   14398              :                                           GFC_ISYM_ALLOCATED, "allocated",
   14399           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   14400           18 :                   block = gfc_get_code (EXEC_IF);
   14401           18 :                   block->block = gfc_get_code (EXEC_IF);
   14402           18 :                   block->block->expr1 = cond;
   14403           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14404              :                                         t1, (*code)->expr1,
   14405           18 :                                         NULL, NULL, (*code)->loc);
   14406           18 :                   add_code_to_chain (&block, &head, &tail);
   14407              :                 }
   14408              :             }
   14409              :         }
   14410           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   14411              :         {
   14412              :           /* Don't add intrinsic assignments since they are already
   14413              :              effected by the intrinsic assignment of the structure, unless
   14414              :              finalization is required or, with finalize_only, the structure
   14415              :              assignment does not copy the components.  */
   14416            7 :           if (finalizable_comp)
   14417            0 :             this_code->expr1->must_finalize = 1;
   14418            7 :           else if (!use_finalize_only)
   14419              :             {
   14420            1 :               gfc_free_statements (this_code);
   14421            1 :               this_code = NULL;
   14422            1 :               continue;
   14423              :             }
   14424              :         }
   14425              :       else
   14426              :         {
   14427              :           /* Resolution has expanded an assignment of a derived type with
   14428              :              defined assigned components.  Remove the redundant, leading
   14429              :              assignment.  */
   14430           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   14431           48 :           gfc_code *tmp = this_code;
   14432           48 :           this_code = this_code->next;
   14433           48 :           tmp->next = NULL;
   14434           48 :           gfc_free_statements (tmp);
   14435              :         }
   14436              : 
   14437          221 :       add_code_to_chain (&this_code, &head, &tail);
   14438              : 
   14439          221 :       if (t1 && (inout || (finalizable_out && !use_finalize_only)))
   14440              :         {
   14441              :           /* Transfer the value to the final result.  */
   14442          166 :           this_code = build_assignment (EXEC_ASSIGN,
   14443              :                                         (*code)->expr1, t1,
   14444           83 :                                         comp1, comp2, (*code)->loc);
   14445           83 :           this_code->expr1->must_finalize = 0;
   14446           83 :           add_code_to_chain (&this_code, &head, &tail);
   14447              :         }
   14448              :     }
   14449              : 
   14450              :   /* Put the temporary assignments at the top of the generated code.  */
   14451          206 :   if (tmp_head && component_assignment_level == 1)
   14452              :     {
   14453          132 :       gfc_append_code (tmp_head, head);
   14454          132 :       head = tmp_head;
   14455          132 :       tmp_head = tmp_tail = NULL;
   14456              :     }
   14457              : 
   14458              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   14459              :      not accidentally deallocated. Hence, nullify t1.  */
   14460           83 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   14461          289 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   14462              :     {
   14463           18 :       gfc_code *block;
   14464           18 :       gfc_expr *cond;
   14465           18 :       gfc_expr *e;
   14466              : 
   14467           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14468           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   14469           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   14470           18 :       block = gfc_get_code (EXEC_IF);
   14471           18 :       block->block = gfc_get_code (EXEC_IF);
   14472           18 :       block->block->expr1 = cond;
   14473           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14474              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   14475           18 :                                         NULL, NULL, (*code)->loc);
   14476           18 :       gfc_append_code (tail, block);
   14477           18 :       tail = block;
   14478              :     }
   14479              : 
   14480          206 :   component_assignment_level--;
   14481              : 
   14482              :   /* Make an explicit final call for the function result.  */
   14483          206 :   if (tmp_expr)
   14484           99 :     generate_final_call (tmp_expr, &head, &tail);
   14485              : 
   14486          206 :   if (tmp_code)
   14487              :     {
   14488          158 :       ns->code = head;
   14489          158 :       return;
   14490              :     }
   14491              : 
   14492              :   /* Now attach the remaining code chain to the input code.  Step on
   14493              :      to the end of the new code since resolution is complete.  */
   14494           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14495           48 :   tail->next = (*code)->next;
   14496              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14497              :      before the temporary for the lhs is created.  */
   14498           48 :   gfc_free_expr ((*code)->expr1);
   14499           48 :   gfc_free_expr ((*code)->expr2);
   14500           48 :   **code = *head;
   14501           48 :   if (head != tail)
   14502           48 :     free (head);
   14503           48 :   *code = tail;
   14504              : }
   14505              : 
   14506              : 
   14507              : /* F2008: Pointer function assignments are of the form:
   14508              :         ptr_fcn (args) = expr
   14509              :    This function breaks these assignments into two statements:
   14510              :         temporary_pointer => ptr_fcn(args)
   14511              :         temporary_pointer = expr  */
   14512              : 
   14513              : static bool
   14514       293134 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14515              : {
   14516       293134 :   gfc_expr *tmp_ptr_expr;
   14517       293134 :   gfc_code *this_code;
   14518       293134 :   gfc_component *comp;
   14519       293134 :   gfc_symbol *s;
   14520              : 
   14521       293134 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14522              :     return false;
   14523              : 
   14524              :   /* Even if standard does not support this feature, continue to build
   14525              :      the two statements to avoid upsetting frontend_passes.c.  */
   14526          211 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14527              :                   "%L", &(*code)->loc);
   14528              : 
   14529          211 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14530              : 
   14531          211 :   if (comp)
   14532            6 :     s = comp->ts.interface;
   14533              :   else
   14534          205 :     s = (*code)->expr1->symtree->n.sym;
   14535              : 
   14536          211 :   if (s == NULL || !s->result->attr.pointer)
   14537              :     {
   14538            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14539              :                  "%L must have the pointer attribute.",
   14540            5 :                  &(*code)->expr1->where);
   14541            5 :       (*code)->op = EXEC_NOP;
   14542            5 :       return false;
   14543              :     }
   14544              : 
   14545          206 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14546              : 
   14547              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14548              :      end, where array bounds are not known, arrays are made allocatable.
   14549              :      Change the temporary to a pointer here.  */
   14550          206 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14551          206 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14552          206 :   tmp_ptr_expr->where = (*code)->loc;
   14553              : 
   14554              :   /* A new charlen is required to ensure that the variable string length
   14555              :      is different to that of the original lhs for deferred results.  */
   14556          206 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14557              :     {
   14558           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14559           60 :       tmp_ptr_expr->ts.deferred = 1;
   14560           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14561           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14562           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14563              :     }
   14564              : 
   14565          412 :   this_code = build_assignment (EXEC_ASSIGN,
   14566              :                                 tmp_ptr_expr, (*code)->expr2,
   14567          206 :                                 NULL, NULL, (*code)->loc);
   14568          206 :   this_code->next = (*code)->next;
   14569          206 :   (*code)->next = this_code;
   14570          206 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14571          206 :   (*code)->expr2 = (*code)->expr1;
   14572          206 :   (*code)->expr1 = tmp_ptr_expr;
   14573              : 
   14574          206 :   return true;
   14575              : }
   14576              : 
   14577              : 
   14578              : /* Deferred character length assignments from an operator expression
   14579              :    require a temporary because the character length of the lhs can
   14580              :    change in the course of the assignment.  */
   14581              : 
   14582              : static bool
   14583       291937 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14584              : {
   14585       291937 :   gfc_expr *tmp_expr;
   14586       291937 :   gfc_code *this_code;
   14587              : 
   14588       291937 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14589        28066 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14590          866 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14591          865 :          && (*code)->expr2->expr_type == EXPR_OP))
   14592              :     return false;
   14593              : 
   14594           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14595              :     return false;
   14596              : 
   14597           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14598              :     return false;
   14599              : 
   14600           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14601           22 :   tmp_expr->where = (*code)->loc;
   14602              : 
   14603              :   /* A new charlen is required to ensure that the variable string
   14604              :      length is different to that of the original lhs.  */
   14605           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14606           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14607           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14608           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14609              : 
   14610           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14611              : 
   14612           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14613           22 :                                 (*code)->expr1,
   14614              :                                 gfc_copy_expr (tmp_expr),
   14615              :                                 NULL, NULL, (*code)->loc);
   14616              : 
   14617           22 :   (*code)->expr1 = tmp_expr;
   14618              : 
   14619           22 :   this_code->next = (*code)->next;
   14620           22 :   (*code)->next = this_code;
   14621              : 
   14622           22 :   return true;
   14623              : }
   14624              : 
   14625              : static void mark_lhs_assignments_set (gfc_code *code);
   14626              : 
   14627              : /* Given a block of code, recursively resolve everything pointed to by this
   14628              :    code block.  */
   14629              : 
   14630              : void
   14631       716563 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14632              : {
   14633       716563 :   int omp_workshare_save;
   14634       716563 :   int forall_save, do_concurrent_save;
   14635       716563 :   code_stack frame;
   14636       716563 :   bool t;
   14637       716563 :   gfc_code *orig_code = code;
   14638              : 
   14639       716563 :   frame.prev = cs_base;
   14640       716563 :   frame.head = code;
   14641       716563 :   cs_base = &frame;
   14642              : 
   14643       716563 :   find_reachable_labels (code);
   14644              : 
   14645      2608005 :   for (; code; code = code->next)
   14646              :     {
   14647      1174880 :       frame.current = code;
   14648      1174880 :       forall_save = forall_flag;
   14649      1174880 :       do_concurrent_save = gfc_do_concurrent_flag;
   14650              : 
   14651      1174880 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14652              :         {
   14653         2363 :           if (code->op == EXEC_FORALL)
   14654         1993 :             forall_flag = 1;
   14655          370 :           else if (code->op == EXEC_DO_CONCURRENT)
   14656          370 :             gfc_do_concurrent_flag = 1;
   14657         2363 :           gfc_resolve_forall (code, ns, forall_save);
   14658         2363 :           if (code->op == EXEC_FORALL)
   14659         1993 :             forall_flag = 2;
   14660          370 :           else if (code->op == EXEC_DO_CONCURRENT)
   14661          370 :             gfc_do_concurrent_flag = 2;
   14662              :         }
   14663      1172517 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14664          146 :         for (gfc_omp_variant *variant
   14665              :                = code->ext.omp_variants;
   14666          473 :              variant; variant = variant->next)
   14667          327 :           gfc_resolve_code (variant->code, ns);
   14668      1172371 :       else if (code->block)
   14669              :         {
   14670       340975 :           omp_workshare_save = -1;
   14671       340975 :           switch (code->op)
   14672              :             {
   14673        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14674        10119 :             case EXEC_OACC_PARALLEL:
   14675        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14676        10119 :             case EXEC_OACC_KERNELS:
   14677        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14678        10119 :             case EXEC_OACC_SERIAL:
   14679        10119 :             case EXEC_OACC_DATA:
   14680        10119 :             case EXEC_OACC_HOST_DATA:
   14681        10119 :             case EXEC_OACC_LOOP:
   14682        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14683        10119 :               break;
   14684           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14685           54 :               omp_workshare_save = omp_workshare_flag;
   14686           54 :               omp_workshare_flag = 1;
   14687           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14688           54 :               break;
   14689         6217 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14690         6217 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14691         6217 :             case EXEC_OMP_MASKED_TASKLOOP:
   14692         6217 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14693         6217 :             case EXEC_OMP_MASTER_TASKLOOP:
   14694         6217 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14695         6217 :             case EXEC_OMP_PARALLEL:
   14696         6217 :             case EXEC_OMP_PARALLEL_DO:
   14697         6217 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14698         6217 :             case EXEC_OMP_PARALLEL_LOOP:
   14699         6217 :             case EXEC_OMP_PARALLEL_MASKED:
   14700         6217 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14701         6217 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14702         6217 :             case EXEC_OMP_PARALLEL_MASTER:
   14703         6217 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14704         6217 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14705         6217 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14706         6217 :             case EXEC_OMP_TARGET_PARALLEL:
   14707         6217 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14708         6217 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14709         6217 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14710         6217 :             case EXEC_OMP_TARGET_TEAMS:
   14711         6217 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14712         6217 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14713         6217 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14714         6217 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14715         6217 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14716         6217 :             case EXEC_OMP_TASK:
   14717         6217 :             case EXEC_OMP_TASKLOOP:
   14718         6217 :             case EXEC_OMP_TASKLOOP_SIMD:
   14719         6217 :             case EXEC_OMP_TEAMS:
   14720         6217 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14721         6217 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14722         6217 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14723         6217 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14724         6217 :             case EXEC_OMP_TEAMS_LOOP:
   14725         6217 :               omp_workshare_save = omp_workshare_flag;
   14726         6217 :               omp_workshare_flag = 0;
   14727         6217 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14728         6217 :               break;
   14729         3184 :             case EXEC_OMP_DISTRIBUTE:
   14730         3184 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14731         3184 :             case EXEC_OMP_DO:
   14732         3184 :             case EXEC_OMP_DO_SIMD:
   14733         3184 :             case EXEC_OMP_LOOP:
   14734         3184 :             case EXEC_OMP_SIMD:
   14735         3184 :             case EXEC_OMP_TARGET_SIMD:
   14736         3184 :             case EXEC_OMP_TILE:
   14737         3184 :             case EXEC_OMP_UNROLL:
   14738         3184 :               gfc_resolve_omp_do_blocks (code, ns);
   14739         3184 :               break;
   14740              :             case EXEC_SELECT_TYPE:
   14741              :             case EXEC_SELECT_RANK:
   14742              :               /* Blocks are handled in resolve_select_type/rank because we
   14743              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14744              :               break;
   14745              :             case EXEC_DO_CONCURRENT:
   14746              :               gfc_do_concurrent_flag = 1;
   14747              :               gfc_resolve_blocks (code->block, ns);
   14748              :               gfc_do_concurrent_flag = 2;
   14749              :               break;
   14750           39 :             case EXEC_OMP_WORKSHARE:
   14751           39 :               omp_workshare_save = omp_workshare_flag;
   14752           39 :               omp_workshare_flag = 1;
   14753              :               /* FALL THROUGH */
   14754       317027 :             default:
   14755       317027 :               gfc_resolve_blocks (code->block, ns);
   14756       317027 :               break;
   14757              :             }
   14758              : 
   14759       336601 :           if (omp_workshare_save != -1)
   14760         6310 :             omp_workshare_flag = omp_workshare_save;
   14761              :         }
   14762      1174880 : start:
   14763      1175091 :       t = true;
   14764      1175091 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14765      1173642 :           t = gfc_resolve_expr (code->expr1);
   14766              : 
   14767      1175091 :       forall_flag = forall_save;
   14768      1175091 :       gfc_do_concurrent_flag = do_concurrent_save;
   14769              : 
   14770      1175091 :       if (!gfc_resolve_expr (code->expr2))
   14771          650 :         t = false;
   14772              : 
   14773      1175091 :       if (code->op == EXEC_ALLOCATE
   14774      1175091 :           && !gfc_resolve_expr (code->expr3))
   14775              :         t = false;
   14776              : 
   14777      1175091 :       switch (code->op)
   14778              :         {
   14779              :         case EXEC_NOP:
   14780              :         case EXEC_END_BLOCK:
   14781              :         case EXEC_END_NESTED_BLOCK:
   14782              :         case EXEC_CYCLE:
   14783              :           break;
   14784              : 
   14785       224972 :         case EXEC_STOP:
   14786       224972 :         case EXEC_ERROR_STOP:
   14787       224972 :           if (code->expr1 != NULL && t)
   14788              :             {
   14789       204450 :               if (!(code->expr1->ts.type == BT_CHARACTER
   14790              :                     || code->expr1->ts.type == BT_INTEGER))
   14791            1 :                 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER "
   14792              :                            "type", &code->expr1->where);
   14793       204449 :               else if (code->expr1->rank != 0)
   14794            0 :                 gfc_error ("STOP code at %L must be scalar",
   14795              :                            &code->expr1->where);
   14796       204449 :               else if (code->expr1->ts.type == BT_CHARACTER
   14797          498 :                        && code->expr1->ts.kind != gfc_default_character_kind)
   14798            0 :                 gfc_error ("STOP code at %L must be default character KIND=%d",
   14799              :                            &code->expr1->where, (int) gfc_default_character_kind);
   14800       204449 :               else if (code->expr1->ts.type == BT_INTEGER
   14801       203951 :                        && code->expr1->ts.kind != gfc_default_integer_kind)
   14802            8 :                 gfc_notify_std (GFC_STD_F2018, "STOP code at %L must be default "
   14803              :                                 "integer KIND=%d", &code->expr1->where,
   14804              :                                 (int) gfc_default_integer_kind);
   14805              :             }
   14806       224972 :           if (code->expr2 != NULL
   14807           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14808           37 :                   || code->expr2->rank != 0))
   14809            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14810              :                        &code->expr2->where);
   14811              : 
   14812              :           /* Fall through.  */
   14813       225002 :         case EXEC_PAUSE:
   14814       225002 :           gfc_value_used_expr (code->expr1, VALUE_USED);
   14815       225002 :           break;
   14816              : 
   14817              :         case EXEC_EXIT:
   14818              :         case EXEC_CONTINUE:
   14819              :         case EXEC_DT_END:
   14820              :         case EXEC_ASSIGN_CALL:
   14821              :           break;
   14822              : 
   14823           54 :         case EXEC_CRITICAL:
   14824           54 :           resolve_critical (code);
   14825           54 :           break;
   14826              : 
   14827         1450 :         case EXEC_SYNC_ALL:
   14828         1450 :         case EXEC_SYNC_IMAGES:
   14829         1450 :         case EXEC_SYNC_MEMORY:
   14830         1450 :           resolve_sync (code);
   14831         1450 :           break;
   14832              : 
   14833          197 :         case EXEC_LOCK:
   14834          197 :         case EXEC_UNLOCK:
   14835          197 :         case EXEC_EVENT_POST:
   14836          197 :         case EXEC_EVENT_WAIT:
   14837          197 :           resolve_lock_unlock_event (code);
   14838          197 :           break;
   14839              : 
   14840              :         case EXEC_FAIL_IMAGE:
   14841              :           break;
   14842              : 
   14843          184 :         case EXEC_FORM_TEAM:
   14844          184 :           resolve_form_team (code);
   14845          184 :           break;
   14846              : 
   14847          131 :         case EXEC_CHANGE_TEAM:
   14848          131 :           resolve_change_team (code);
   14849          131 :           break;
   14850              : 
   14851          129 :         case EXEC_END_TEAM:
   14852          129 :           resolve_end_team (code);
   14853          129 :           break;
   14854              : 
   14855           48 :         case EXEC_SYNC_TEAM:
   14856           48 :           resolve_sync_team (code);
   14857           48 :           break;
   14858              : 
   14859         1503 :         case EXEC_ENTRY:
   14860              :           /* Keep track of which entry we are up to.  */
   14861         1503 :           current_entry_id = code->ext.entry->id;
   14862         1503 :           break;
   14863              : 
   14864          459 :         case EXEC_WHERE:
   14865          459 :           resolve_where (code, NULL);
   14866          459 :           break;
   14867              : 
   14868         1306 :         case EXEC_GOTO:
   14869         1306 :           if (code->expr1 != NULL)
   14870              :             {
   14871           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14872           76 :                   || code->expr1->ts.type != BT_INTEGER
   14873           76 :                   || (code->expr1->ref
   14874            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14875           75 :                   || code->expr1->symtree == NULL
   14876           75 :                   || (code->expr1->symtree->n.sym
   14877           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14878           75 :                           == FL_PARAMETER)))
   14879            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14880              :                            "scalar INTEGER variable", &code->expr1->where);
   14881           74 :               else if (code->expr1->symtree->n.sym
   14882           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14883            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14884              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14885              :                            &code->expr1->where);
   14886              :             }
   14887              :           else
   14888         1228 :             resolve_branch (code->label1, code);
   14889              :           break;
   14890              : 
   14891         3285 :         case EXEC_RETURN:
   14892         3285 :           if (code->expr1 != NULL
   14893           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14894            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14895              :                        "INTEGER return specifier", &code->expr1->where);
   14896              :           break;
   14897              : 
   14898              :         case EXEC_INIT_ASSIGN:
   14899              :         case EXEC_END_PROCEDURE:
   14900              :           break;
   14901              : 
   14902       294322 :         case EXEC_ASSIGN:
   14903       294322 :           if (!t)
   14904              :             break;
   14905              : 
   14906       293634 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14907       293634 :               && gfc_is_coindexed (code->expr1))
   14908              :             {
   14909              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14910              :                  coindexed variable.  */
   14911          500 :               code->op = EXEC_CALL;
   14912          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14913              :                                 true);
   14914          500 :               code->resolved_sym = code->symtree->n.sym;
   14915          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14916          500 :               code->resolved_sym->attr.intrinsic = 1;
   14917          500 :               code->resolved_sym->attr.subroutine = 1;
   14918          500 :               code->resolved_isym
   14919          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14920          500 :               gfc_commit_symbol (code->resolved_sym);
   14921          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14922          500 :               code->ext.actual->expr = code->expr1;
   14923          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14924          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14925          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14926              :                 {
   14927              :                   /* Convert assignments of expr1[...] = expr2 into
   14928              :                         tvar = expr2
   14929              :                         expr1[...] = tvar
   14930              :                      when expr2 is not trivial.  */
   14931           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14932           54 :                   gfc_code next_code = *code;
   14933           54 :                   gfc_code *rhs_code
   14934          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14935           54 :                                         NULL, code->expr2->where);
   14936           54 :                   *code = *rhs_code;
   14937           54 :                   code->next = rhs_code;
   14938           54 :                   *rhs_code = next_code;
   14939              : 
   14940           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14941           54 :                   rhs_code->expr1 = NULL;
   14942           54 :                   rhs_code->expr2 = NULL;
   14943              :                 }
   14944              :               else
   14945              :                 {
   14946          446 :                   code->ext.actual->next->expr = code->expr2;
   14947              : 
   14948          446 :                   code->expr1 = NULL;
   14949          446 :                   code->expr2 = NULL;
   14950              :                 }
   14951              :               break;
   14952              :             }
   14953              : 
   14954       293134 :           if (code->expr1->ts.type == BT_CLASS)
   14955         1223 :             gfc_find_vtab (&code->expr2->ts);
   14956              : 
   14957              :           /* If this is a pointer function in an lvalue variable context,
   14958              :              the new code will have to be resolved afresh. This is also the
   14959              :              case with an error, where the code is transformed into NOP to
   14960              :              prevent ICEs downstream.  */
   14961       293134 :           if (resolve_ptr_fcn_assign (&code, ns)
   14962       293134 :               || code->op == EXEC_NOP)
   14963          211 :             goto start;
   14964              : 
   14965       292923 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14966       292923 :                                          _("assignment")))
   14967              :             break;
   14968              : 
   14969       292879 :           if (resolve_ordinary_assign (code, ns))
   14970              :             {
   14971          942 :               if (omp_workshare_flag)
   14972              :                 {
   14973            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14974            1 :                              "at %L", &code->loc);
   14975            1 :                   break;
   14976              :                 }
   14977          941 :               if (code->op == EXEC_COMPCALL)
   14978          473 :                 goto compcall;
   14979              :               else
   14980          468 :                 goto call;
   14981              :             }
   14982              : 
   14983              :           /* Check for dependencies in deferred character length array
   14984              :              assignments and generate a temporary, if necessary.  */
   14985       291937 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14986              :             break;
   14987              : 
   14988              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14989       291915 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14990         7834 :               && code->expr1->ts.u.derived
   14991         7834 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14992          212 :             generate_component_assignments (&code, ns);
   14993       291703 :           else if (code->op == EXEC_ASSIGN)
   14994              :             {
   14995       291703 :               if (gfc_may_be_finalized (code->expr1->ts))
   14996         1428 :                 code->expr1->must_finalize = 1;
   14997       291703 :               if (code->expr2->expr_type == EXPR_ARRAY
   14998       291703 :                   && gfc_may_be_finalized (code->expr2->ts))
   14999           66 :                 code->expr2->must_finalize = 1;
   15000              :             }
   15001              : 
   15002              :           break;
   15003              : 
   15004          126 :         case EXEC_LABEL_ASSIGN:
   15005          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   15006            0 :             gfc_error ("Label %d referenced at %L is never defined",
   15007              :                        code->label1->value, &code->label1->where);
   15008          126 :           if (t
   15009          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   15010          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   15011          126 :                   || code->expr1->symtree->n.sym->ts.kind
   15012          126 :                      != gfc_default_integer_kind
   15013          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   15014          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   15015            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   15016              :                        "default INTEGER variable", &code->expr1->where);
   15017              :           break;
   15018              : 
   15019        10857 :         case EXEC_POINTER_ASSIGN:
   15020        10857 :           {
   15021        10857 :             gfc_expr* e;
   15022              : 
   15023        10857 :             if (!t)
   15024              :               break;
   15025              : 
   15026              :             /* This is both a variable definition and pointer assignment
   15027              :                context, so check both of them.  For rank remapping, a final
   15028              :                array ref may be present on the LHS and fool gfc_expr_attr
   15029              :                used in gfc_check_vardef_context.  Remove it.  */
   15030        10852 :             e = remove_last_array_ref (code->expr1);
   15031        21704 :             t = gfc_check_vardef_context (e, true, false, false,
   15032        10852 :                                           _("pointer assignment"));
   15033        10852 :             if (t)
   15034        10819 :               t = gfc_check_vardef_context (e, false, false, false,
   15035        10819 :                                             _("pointer assignment"));
   15036        10852 :             gfc_free_expr (e);
   15037              : 
   15038        10852 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   15039              : 
   15040        10702 :             if (!t)
   15041              :               break;
   15042              : 
   15043              :             /* Assigning a class object always is a regular assign.  */
   15044        10702 :             if (code->expr2->ts.type == BT_CLASS
   15045          617 :                 && code->expr1->ts.type == BT_CLASS
   15046          520 :                 && CLASS_DATA (code->expr2)
   15047          519 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   15048        11374 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   15049           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   15050           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   15051           43 :                         == FL_PROCEDURE))
   15052          340 :               code->op = EXEC_ASSIGN;
   15053              :             break;
   15054              :           }
   15055              : 
   15056           72 :         case EXEC_ARITHMETIC_IF:
   15057           72 :           {
   15058           72 :             gfc_expr *e = code->expr1;
   15059              : 
   15060           72 :             gfc_resolve_expr (e);
   15061           72 :             if (e->expr_type == EXPR_NULL)
   15062            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   15063              : 
   15064           72 :             if (t && (e->rank > 0
   15065           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   15066            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   15067              :                          "REAL or INTEGER expression", &e->where);
   15068              : 
   15069           72 :             resolve_branch (code->label1, code);
   15070           72 :             resolve_branch (code->label2, code);
   15071           72 :             resolve_branch (code->label3, code);
   15072              :           }
   15073           72 :           break;
   15074              : 
   15075       238929 :         case EXEC_IF:
   15076       238929 :           if (t && code->expr1 != NULL
   15077            0 :               && (code->expr1->ts.type != BT_LOGICAL
   15078            0 :                   || code->expr1->rank != 0))
   15079            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   15080              :                        &code->expr1->where);
   15081              :           break;
   15082              : 
   15083        82759 :         case EXEC_CALL:
   15084        82759 :         call:
   15085        82759 :           resolve_call (code);
   15086        82759 :           break;
   15087              : 
   15088         1798 :         case EXEC_COMPCALL:
   15089         1798 :         compcall:
   15090         1798 :           resolve_typebound_subroutine (code);
   15091         1798 :           break;
   15092              : 
   15093          124 :         case EXEC_CALL_PPC:
   15094          124 :           resolve_ppc_call (code);
   15095          124 :           break;
   15096              : 
   15097          713 :         case EXEC_SELECT:
   15098              :           /* Select is complicated. Also, a SELECT construct could be
   15099              :              a transformed computed GOTO.  */
   15100          713 :           resolve_select (code, false);
   15101          713 :           break;
   15102              : 
   15103         3340 :         case EXEC_SELECT_TYPE:
   15104         3340 :           resolve_select_type (code, ns);
   15105         3340 :           break;
   15106              : 
   15107         1060 :         case EXEC_SELECT_RANK:
   15108         1060 :           resolve_select_rank (code, ns);
   15109         1060 :           break;
   15110              : 
   15111         8882 :         case EXEC_BLOCK:
   15112         8882 :           resolve_block_construct (code);
   15113         8882 :           break;
   15114              : 
   15115        34048 :         case EXEC_DO:
   15116        34048 :           if (code->ext.iterator != NULL)
   15117              :             {
   15118        34048 :               gfc_iterator *iter = code->ext.iterator;
   15119        34048 :               if (gfc_resolve_iterator (iter, true, false))
   15120        34034 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   15121              :                                          true);
   15122              :             }
   15123              :           break;
   15124              : 
   15125          567 :         case EXEC_DO_WHILE:
   15126          567 :           if (code->expr1 == NULL)
   15127            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   15128              :                                 "DO WHILE");
   15129          567 :           if (t
   15130          567 :               && (code->expr1->rank != 0
   15131          567 :                   || code->expr1->ts.type != BT_LOGICAL))
   15132            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   15133              :                        "a scalar LOGICAL expression", &code->expr1->where);
   15134              :           break;
   15135              : 
   15136        15274 :         case EXEC_ALLOCATE:
   15137        15274 :           if (t)
   15138        15272 :             resolve_allocate_deallocate (code, "ALLOCATE");
   15139              : 
   15140              :           break;
   15141              : 
   15142         6482 :         case EXEC_DEALLOCATE:
   15143         6482 :           if (t)
   15144         6482 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   15145              : 
   15146              :           break;
   15147              : 
   15148         4021 :         case EXEC_OPEN:
   15149         4021 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   15150              :             break;
   15151              : 
   15152         3794 :           resolve_branch (code->ext.open->err, code);
   15153         3794 :           break;
   15154              : 
   15155         3214 :         case EXEC_CLOSE:
   15156         3214 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   15157              :             break;
   15158              : 
   15159         3180 :           resolve_branch (code->ext.close->err, code);
   15160         3180 :           break;
   15161              : 
   15162         2935 :         case EXEC_BACKSPACE:
   15163         2935 :         case EXEC_ENDFILE:
   15164         2935 :         case EXEC_REWIND:
   15165         2935 :         case EXEC_FLUSH:
   15166         2935 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   15167              :             break;
   15168              : 
   15169         2869 :           resolve_branch (code->ext.filepos->err, code);
   15170         2869 :           break;
   15171              : 
   15172          874 :         case EXEC_INQUIRE:
   15173          874 :           if (!gfc_resolve_inquire (code->ext.inquire))
   15174              :               break;
   15175              : 
   15176          826 :           resolve_branch (code->ext.inquire->err, code);
   15177          826 :           break;
   15178              : 
   15179           92 :         case EXEC_IOLENGTH:
   15180           92 :           gcc_assert (code->ext.inquire != NULL);
   15181           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   15182              :             break;
   15183              : 
   15184           90 :           resolve_branch (code->ext.inquire->err, code);
   15185           90 :           break;
   15186              : 
   15187           89 :         case EXEC_WAIT:
   15188           89 :           if (!gfc_resolve_wait (code->ext.wait))
   15189              :             break;
   15190              : 
   15191           74 :           resolve_branch (code->ext.wait->err, code);
   15192           74 :           resolve_branch (code->ext.wait->end, code);
   15193           74 :           resolve_branch (code->ext.wait->eor, code);
   15194           74 :           break;
   15195              : 
   15196        34046 :         case EXEC_READ:
   15197        34046 :         case EXEC_WRITE:
   15198        34046 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   15199              :             break;
   15200              : 
   15201        33738 :           resolve_branch (code->ext.dt->err, code);
   15202        33738 :           resolve_branch (code->ext.dt->end, code);
   15203        33738 :           resolve_branch (code->ext.dt->eor, code);
   15204        33738 :           break;
   15205              : 
   15206        48284 :         case EXEC_TRANSFER:
   15207        48284 :           resolve_transfer (code);
   15208        48284 :           break;
   15209              : 
   15210         2363 :         case EXEC_DO_CONCURRENT:
   15211         2363 :         case EXEC_FORALL:
   15212         2363 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   15213              : 
   15214         2363 :           if (code->expr1 != NULL
   15215          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   15216            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   15217              :                        "expression", &code->expr1->where);
   15218              : 
   15219         2363 :     if (code->op == EXEC_DO_CONCURRENT)
   15220          370 :       resolve_locality_spec (code, ns);
   15221              :           break;
   15222              : 
   15223        13538 :         case EXEC_OACC_PARALLEL_LOOP:
   15224        13538 :         case EXEC_OACC_PARALLEL:
   15225        13538 :         case EXEC_OACC_KERNELS_LOOP:
   15226        13538 :         case EXEC_OACC_KERNELS:
   15227        13538 :         case EXEC_OACC_SERIAL_LOOP:
   15228        13538 :         case EXEC_OACC_SERIAL:
   15229        13538 :         case EXEC_OACC_DATA:
   15230        13538 :         case EXEC_OACC_HOST_DATA:
   15231        13538 :         case EXEC_OACC_LOOP:
   15232        13538 :         case EXEC_OACC_UPDATE:
   15233        13538 :         case EXEC_OACC_WAIT:
   15234        13538 :         case EXEC_OACC_CACHE:
   15235        13538 :         case EXEC_OACC_ENTER_DATA:
   15236        13538 :         case EXEC_OACC_EXIT_DATA:
   15237        13538 :         case EXEC_OACC_ATOMIC:
   15238        13538 :         case EXEC_OACC_DECLARE:
   15239        13538 :         case EXEC_OACC_INIT:
   15240        13538 :         case EXEC_OACC_SHUTDOWN:
   15241        13538 :         case EXEC_OACC_SET:
   15242        13538 :           gfc_resolve_oacc_directive (code, ns);
   15243        13538 :           break;
   15244              : 
   15245        17734 :         case EXEC_OMP_ALLOCATE:
   15246        17734 :         case EXEC_OMP_ALLOCATORS:
   15247        17734 :         case EXEC_OMP_ASSUME:
   15248        17734 :         case EXEC_OMP_ATOMIC:
   15249        17734 :         case EXEC_OMP_BARRIER:
   15250        17734 :         case EXEC_OMP_CANCEL:
   15251        17734 :         case EXEC_OMP_CANCELLATION_POINT:
   15252        17734 :         case EXEC_OMP_CRITICAL:
   15253        17734 :         case EXEC_OMP_FLUSH:
   15254        17734 :         case EXEC_OMP_DEPOBJ:
   15255        17734 :         case EXEC_OMP_DISPATCH:
   15256        17734 :         case EXEC_OMP_DISTRIBUTE:
   15257        17734 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   15258        17734 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   15259        17734 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   15260        17734 :         case EXEC_OMP_DO:
   15261        17734 :         case EXEC_OMP_DO_SIMD:
   15262        17734 :         case EXEC_OMP_ERROR:
   15263        17734 :         case EXEC_OMP_INTEROP:
   15264        17734 :         case EXEC_OMP_LOOP:
   15265        17734 :         case EXEC_OMP_MASTER:
   15266        17734 :         case EXEC_OMP_MASTER_TASKLOOP:
   15267        17734 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   15268        17734 :         case EXEC_OMP_MASKED:
   15269        17734 :         case EXEC_OMP_MASKED_TASKLOOP:
   15270        17734 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   15271        17734 :         case EXEC_OMP_METADIRECTIVE:
   15272        17734 :         case EXEC_OMP_ORDERED:
   15273        17734 :         case EXEC_OMP_SCAN:
   15274        17734 :         case EXEC_OMP_SCOPE:
   15275        17734 :         case EXEC_OMP_SECTIONS:
   15276        17734 :         case EXEC_OMP_SIMD:
   15277        17734 :         case EXEC_OMP_SINGLE:
   15278        17734 :         case EXEC_OMP_TARGET:
   15279        17734 :         case EXEC_OMP_TARGET_DATA:
   15280        17734 :         case EXEC_OMP_TARGET_ENTER_DATA:
   15281        17734 :         case EXEC_OMP_TARGET_EXIT_DATA:
   15282        17734 :         case EXEC_OMP_TARGET_PARALLEL:
   15283        17734 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   15284        17734 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   15285        17734 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   15286        17734 :         case EXEC_OMP_TARGET_SIMD:
   15287        17734 :         case EXEC_OMP_TARGET_TEAMS:
   15288        17734 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   15289        17734 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   15290        17734 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   15291        17734 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   15292        17734 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   15293        17734 :         case EXEC_OMP_TARGET_UPDATE:
   15294        17734 :         case EXEC_OMP_TASK:
   15295        17734 :         case EXEC_OMP_TASKGROUP:
   15296        17734 :         case EXEC_OMP_TASKLOOP:
   15297        17734 :         case EXEC_OMP_TASKLOOP_SIMD:
   15298        17734 :         case EXEC_OMP_TASKWAIT:
   15299        17734 :         case EXEC_OMP_TASKYIELD:
   15300        17734 :         case EXEC_OMP_TEAMS:
   15301        17734 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   15302        17734 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   15303        17734 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   15304        17734 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   15305        17734 :         case EXEC_OMP_TEAMS_LOOP:
   15306        17734 :         case EXEC_OMP_TILE:
   15307        17734 :         case EXEC_OMP_UNROLL:
   15308        17734 :         case EXEC_OMP_WORKSHARE:
   15309        17734 :           gfc_resolve_omp_directive (code, ns);
   15310        17734 :           break;
   15311              : 
   15312         4017 :         case EXEC_OMP_PARALLEL:
   15313         4017 :         case EXEC_OMP_PARALLEL_DO:
   15314         4017 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   15315         4017 :         case EXEC_OMP_PARALLEL_LOOP:
   15316         4017 :         case EXEC_OMP_PARALLEL_MASKED:
   15317         4017 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   15318         4017 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   15319         4017 :         case EXEC_OMP_PARALLEL_MASTER:
   15320         4017 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   15321         4017 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   15322         4017 :         case EXEC_OMP_PARALLEL_SECTIONS:
   15323         4017 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   15324         4017 :           omp_workshare_save = omp_workshare_flag;
   15325         4017 :           omp_workshare_flag = 0;
   15326         4017 :           gfc_resolve_omp_directive (code, ns);
   15327         4017 :           omp_workshare_flag = omp_workshare_save;
   15328         4017 :           break;
   15329              : 
   15330            0 :         default:
   15331            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   15332              :         }
   15333      1174879 :       gfc_value_used_expr (code->expr2, VALUE_USED);
   15334      1174879 :       gfc_value_used_expr (code->expr3, VALUE_USED);
   15335      1174879 :       gfc_value_used_expr (code->expr4, VALUE_USED);
   15336              :     }
   15337              : 
   15338       716562 :   mark_lhs_assignments_set (orig_code);
   15339              : 
   15340       716562 :   cs_base = frame.prev;
   15341       716562 : }
   15342              : 
   15343              : 
   15344              : /* Resolve initial values and make sure they are compatible with
   15345              :    the variable.  */
   15346              : 
   15347              : static void
   15348      2010988 : resolve_values (gfc_symbol *sym)
   15349              : {
   15350      2010988 :   bool t;
   15351              : 
   15352      2010988 :   if (sym->value == NULL)
   15353              :     return;
   15354              : 
   15355       458450 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   15356           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   15357              :                  "Using parameter %qs declared at %L is deprecated",
   15358              :                  sym->name, &sym->declared_at);
   15359              : 
   15360       458450 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   15361        43354 :     t= resolve_structure_cons (sym->value, 1);
   15362              :   else
   15363       415096 :     t = gfc_resolve_expr (sym->value);
   15364              : 
   15365       458450 :   if (!t)
   15366              :     return;
   15367              : 
   15368       458448 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   15369              : }
   15370              : 
   15371              : 
   15372              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   15373              :    for them once, rather than for each variable declared of that type.  */
   15374              : 
   15375              : static void
   15376      1977907 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   15377              : {
   15378      1977907 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   15379        90794 :       && derived_sym->attr.is_bind_c == 1)
   15380        28085 :     verify_bind_c_derived_type (derived_sym);
   15381              : 
   15382      1977907 :   return;
   15383              : }
   15384              : 
   15385              : 
   15386              : /* Check the interfaces of DTIO procedures associated with derived
   15387              :    type 'sym'.  These procedures can either have typebound bindings or
   15388              :    can appear in DTIO generic interfaces.  */
   15389              : 
   15390              : static void
   15391      2011959 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   15392              : {
   15393      2011959 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   15394              :     return;
   15395              : 
   15396       101221 :   gfc_check_dtio_interfaces (sym);
   15397              : 
   15398       101221 :   return;
   15399              : }
   15400              : 
   15401              : /* Auxiliary function, checks if an argument decays to a pointer.  */
   15402              : 
   15403              : static bool
   15404        72768 : decays_to_pointer (gfc_symbol *sym)
   15405              : {
   15406        72768 :   if (!sym->as)
   15407              :     return true;
   15408              : 
   15409        19649 :   if (sym->as->type == AS_ASSUMED_SHAPE)
   15410              :     return false;
   15411              : 
   15412        15892 :   if (sym->as->type == AS_ASSUMED_RANK)
   15413              :     return false;
   15414              : 
   15415        10778 :   if (sym->as->type == AS_DEFERRED && sym->attr.dummy)
   15416          968 :     return false;
   15417              : 
   15418              :   return true;
   15419              : }
   15420              : 
   15421              : /* Helper function, returns true if the types conform according to the C
   15422              :    standard, when they are not equal on the Fortran side.  If we decide to
   15423              :    include or exclude any types from this, this is the place to change.  */
   15424              : 
   15425              : static bool
   15426          390 : c_types_conform (gfc_typespec *ts1, gfc_typespec *ts2)
   15427              : {
   15428          390 :   if (ts1->type == BT_ASSUMED || ts2->type == BT_ASSUMED)
   15429              :     return true;
   15430              : 
   15431          384 :   if (ts1->kind == ts2->kind
   15432              :       && (ts1->type == BT_CHARACTER || ts1->type == BT_INTEGER
   15433              :           || ts1->type == BT_UNSIGNED)
   15434              :       && (ts2->type == BT_CHARACTER || ts2->type == BT_INTEGER
   15435              :           || ts2->type == BT_UNSIGNED))
   15436          384 :     return true;
   15437              : 
   15438              :   return false;
   15439              : 
   15440              : }
   15441              : 
   15442              : /* Check argument lists of BIND(C) procedures against each other, return
   15443              :    false if they do not. */
   15444              : 
   15445              : static bool
   15446        13626 : compare_c_binding_arglists (gfc_symbol *osym, gfc_symbol *nsym)
   15447              : {
   15448        13626 :   gfc_formal_arglist *oarg, *narg;
   15449        13626 :   bool ret = true;
   15450        13626 :   locus *oloc, *nloc;
   15451              : 
   15452        13626 :   oarg = osym->formal;
   15453        13626 :   narg = nsym->formal;
   15454        13626 :   oloc = &osym->declared_at;
   15455        13626 :   nloc = &nsym->declared_at;
   15456        50020 :   for ( ; oarg && narg ; oarg = oarg->next, narg = narg->next)
   15457              :     {
   15458        36394 :       oloc = &oarg->sym->declared_at;
   15459        36394 :       nloc = &narg->sym->declared_at;
   15460              : 
   15461        36394 :       if (!gfc_compare_types (&oarg->sym->ts, &narg->sym->ts)
   15462        36394 :           && (pedantic || !c_types_conform (&oarg->sym->ts, &narg->sym->ts)))
   15463              :         {
   15464           24 :           gfc_error ("Type mismatch in argument %qs at %L (%s/%s) "
   15465            8 :                      "originally declared at %L", narg->sym->name,
   15466            8 :                      nloc, gfc_typename (&narg->sym->ts),
   15467            8 :                      gfc_typename (&oarg->sym->ts), oloc);
   15468            8 :                      ret = false;
   15469            8 :                      continue;
   15470              :         }
   15471        36386 :       if (oarg->sym->attr.value != narg->sym->attr.value)
   15472              :         {
   15473            1 :           gfc_error ("VALUE attribute mismatch in argument %qs at %L "
   15474              :                      "originally declared at %L",narg->sym->name,
   15475              :                      nloc, oloc);
   15476            1 :           ret = false;
   15477            1 :           continue;
   15478              :         }
   15479              : 
   15480              :       /* According to the Fortran standard, ranks have to match for arguments.
   15481              :          In this case, this makes little sense because both decay to C
   15482              :          pointers.  Only issue an error if -pedantic or if the argument does
   15483              :          not decay to a pointer.  Same thing for CFI_desc arrays, which include
   15484              :          assumed rank.  */
   15485              : 
   15486        36385 :       int orank = gfc_symbol_rank (oarg->sym);
   15487        36385 :       int nrank = gfc_symbol_rank (narg->sym);
   15488        36385 :       if (orank != nrank && pedantic)
   15489              :         {
   15490            1 :           gfc_error ("Rank mismatch in argument %qs (%d/%d) at %L originally "
   15491            1 :                      "declared at %L", narg->sym->name, nrank, orank,  nloc,
   15492              :                      oloc);
   15493            1 :           ret = false;
   15494            1 :           continue;
   15495              :         }
   15496              : 
   15497              :       /* Confusion between CFI_desc and "normal" arrays.  */
   15498              : 
   15499        36384 :       if (decays_to_pointer (oarg->sym) != decays_to_pointer (narg->sym))
   15500              :         {
   15501            1 :           gfc_error ("Array specification mismatch in argument %qs at %L "
   15502              :                      "originally declared at %L", narg->sym->name,
   15503              :                      nloc, oloc);
   15504            1 :           ret = false;
   15505            1 :           continue;
   15506              :         }
   15507              :     }
   15508              : 
   15509        13626 :   if (oarg && !narg)
   15510              :     {
   15511            0 :       gfc_error ("Not enough arguments for procedure %qs with binding label "
   15512              :                  "%qs after %L, originally declared at %L", nsym->name,
   15513            0 :                  nsym->binding_label, nloc, &oarg->sym->declared_at);
   15514            0 :       ret = false;
   15515              :     }
   15516              : 
   15517        13626 :   if (!oarg && narg)
   15518              :     {
   15519            2 :       gfc_error ("Too many arguments for procedure %qs with binding label "
   15520              :                  "%qs at %L, originally declared at %L", nsym->name,
   15521            2 :                  nsym->binding_label, &narg->sym->declared_at, oloc);
   15522            2 :       ret = false;
   15523              :     }
   15524              : 
   15525        13626 :   return ret;
   15526              : }
   15527              : 
   15528              : 
   15529              : /* Verify that any binding labels used in a given namespace do not collide
   15530              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   15531              :    for the same procedure are permitted.  Abstract interfaces and dummy
   15532              :    arguments are not checked.  */
   15533              : 
   15534              : static void
   15535      2011959 : gfc_verify_binding_labels (gfc_symbol *sym)
   15536              : {
   15537      2011959 :   gfc_gsymbol *gsym;
   15538      2011959 :   const char *module;
   15539              : 
   15540      2011959 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   15541        74562 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   15542        45536 :       || sym->attr.abstract || sym->attr.dummy)
   15543              :     return;
   15544              : 
   15545              :   /* Avoid double error reporting.  */
   15546        45400 :   if (sym->error)
   15547              :     return;
   15548              : 
   15549              :   /* TODO: Check the names of reserved external C identifiers here, see
   15550              :      PR 125251.  */
   15551              : 
   15552              :   /* According to the Fortran standard, global identifiers are case
   15553              :      insensitive, which also holds for C identifiers.  This was probably done
   15554              :      for systems which had case-insensitive linkers.  Such systems could not
   15555              :      accommodate the C standards referenced, so this restriction makes little
   15556              :      sense for modern systems. Therefore, check case-sensitive labels unless
   15557              :      -pedantic is in force.  */
   15558              : 
   15559        45400 :   if (pedantic)
   15560         4691 :     gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   15561              :   else
   15562        40709 :     gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
   15563              : 
   15564        45400 :   if (sym->module)
   15565              :     module = sym->module;
   15566        13605 :   else if (sym->ns && sym->ns->proc_name
   15567        13605 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   15568         4623 :     module = sym->ns->proc_name->name;
   15569         8982 :   else if (sym->ns && sym->ns->parent
   15570          358 :            && sym->ns && sym->ns->parent->proc_name
   15571          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15572          272 :     module = sym->ns->parent->proc_name->name;
   15573              :   else
   15574              :     module = NULL;
   15575              : 
   15576        45400 :   if (gsym)
   15577              :     {
   15578        13670 :       if (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)
   15579              :         {
   15580        13629 :           gfc_symbol *global_sym;
   15581        13629 :           gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &global_sym);
   15582              : 
   15583              :           /* For when the symtree does not match the symbol name, which can happen
   15584              :              in modules with PRIVATE.  */
   15585              : 
   15586        13629 :           if (global_sym == NULL)
   15587            1 :             gfc_find_symbol_by_name (gsym->sym_name, gsym->ns, &global_sym);
   15588              : 
   15589        13629 :           gcc_assert (global_sym);
   15590              : 
   15591              :           /* If subroutines and functions are conflated, there is little point
   15592              :              in continuing checks.  */
   15593        13629 :           if ((sym->attr.function && gsym->type == GSYM_SUBROUTINE)
   15594        13629 :               || (sym->attr.subroutine && gsym->type == GSYM_FUNCTION))
   15595              :             {
   15596            1 :               gfc_global_used (gsym, &sym->declared_at);
   15597            1 :               sym->binding_label = NULL;
   15598            1 :               sym->error = 1;
   15599           13 :               return;
   15600              :             }
   15601              : 
   15602         7639 :           if (gsym->type == GSYM_FUNCTION && sym->attr.function
   15603        21267 :               && !gfc_compare_types (&sym->ts, &global_sym->ts))
   15604              :             {
   15605            2 :               gfc_error ("Return type mismatch of function %qs with binding "
   15606              :                          "label %qs at %L (%s/%s), originally declared at %L",
   15607              :                          sym->name, sym->binding_label,
   15608              :                          &sym->declared_at,
   15609              :                          gfc_typename (&sym->ts),
   15610            2 :                          gfc_typename (&global_sym->ts),
   15611              :                          &gsym->where);
   15612            2 :               sym->binding_label = NULL;
   15613            2 :               sym->error = 1;
   15614            2 :               return;
   15615              :             }
   15616        13626 :           if (!compare_c_binding_arglists (global_sym, sym))
   15617              :             {
   15618           10 :               sym->binding_label = NULL;
   15619           10 :               sym->error = 1;
   15620           10 :               return;
   15621              :             }
   15622              :         }
   15623              :     }
   15624              : 
   15625        13616 :   if (!gsym
   15626        13657 :       || (!gsym->defined
   15627        10701 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   15628              :     {
   15629        31730 :       if (!gsym)
   15630        31730 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   15631        42431 :       gsym->where = sym->declared_at;
   15632        42431 :       gsym->sym_name = sym->name;
   15633        42431 :       gsym->binding_label = sym->binding_label;
   15634        42431 :       gsym->ns = sym->ns;
   15635        42431 :       gsym->mod_name = module;
   15636        42431 :       if (sym->attr.function)
   15637        28285 :         gsym->type = GSYM_FUNCTION;
   15638        14146 :       else if (sym->attr.subroutine)
   15639        14006 :         gsym->type = GSYM_SUBROUTINE;
   15640              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   15641        42431 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   15642        42431 :       return;
   15643              :     }
   15644              : 
   15645         2956 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   15646              :     {
   15647            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   15648              :                  "identifier as entity at %L", sym->name,
   15649              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15650              :       /* Clear the binding label to prevent checking multiple times.  */
   15651            1 :       sym->binding_label = NULL;
   15652            1 :       return;
   15653              :     }
   15654              : 
   15655         2955 :   if (sym->attr.flavor == FL_VARIABLE && module
   15656           37 :       && (strcmp (module, gsym->mod_name) != 0
   15657           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   15658              :     {
   15659              :       /* This can only happen if the variable is defined in a module - if it
   15660              :          isn't the same module, reject it.  */
   15661            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   15662              :                  "uses the same global identifier as entity at %L from module %qs",
   15663              :                  sym->name, module, sym->binding_label,
   15664              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   15665            3 :       sym->binding_label = NULL;
   15666            3 :       return;
   15667              :     }
   15668              : 
   15669         2952 :   if ((sym->attr.function || sym->attr.subroutine)
   15670         2916 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   15671         2914 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   15672         2529 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   15673         2097 :       && (module != gsym->mod_name
   15674         2093 :           || strcmp (gsym->sym_name, sym->name) != 0
   15675         2093 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   15676              :     {
   15677              :       /* Print an error if the procedure is defined multiple times; we have to
   15678              :          exclude references to the same procedure via module association or
   15679              :          multiple checks for the same procedure.  */
   15680            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   15681              :                  "global identifier as entity at %L", sym->name,
   15682              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15683            4 :       sym->binding_label = NULL;
   15684            4 :       return;
   15685              :     }
   15686              : }
   15687              : 
   15688              : 
   15689              : /* Resolve an index expression.  */
   15690              : 
   15691              : static bool
   15692       273395 : resolve_index_expr (gfc_expr *e)
   15693              : {
   15694       273395 :   if (!gfc_resolve_expr (e))
   15695              :     return false;
   15696              : 
   15697       273385 :   if (!gfc_simplify_expr (e, 0))
   15698              :     return false;
   15699              : 
   15700       273383 :   if (!gfc_specification_expr (e))
   15701              :     return false;
   15702              : 
   15703              :   return true;
   15704              : }
   15705              : 
   15706              : 
   15707              : /* Resolve a charlen structure.  */
   15708              : 
   15709              : static bool
   15710       107690 : resolve_charlen (gfc_charlen *cl)
   15711              : {
   15712       107690 :   int k;
   15713       107690 :   bool saved_specification_expr;
   15714              : 
   15715       107690 :   if (cl->resolved)
   15716              :     return true;
   15717              : 
   15718        97701 :   cl->resolved = 1;
   15719        97701 :   saved_specification_expr = specification_expr;
   15720        97701 :   specification_expr = true;
   15721              : 
   15722        97701 :   if (cl->length_from_typespec)
   15723              :     {
   15724         1520 :       if (!gfc_resolve_expr (cl->length))
   15725              :         {
   15726            1 :           specification_expr = saved_specification_expr;
   15727            1 :           return false;
   15728              :         }
   15729              : 
   15730         1519 :       if (!gfc_simplify_expr (cl->length, 0))
   15731              :         {
   15732            0 :           specification_expr = saved_specification_expr;
   15733            0 :           return false;
   15734              :         }
   15735              : 
   15736              :       /* cl->length has been resolved.  It should have an integer type.  */
   15737         1519 :       if (cl->length
   15738         1518 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15739              :         {
   15740            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15741              :                      &cl->length->where);
   15742            4 :           return false;
   15743              :         }
   15744              :     }
   15745              :   else
   15746              :     {
   15747        96181 :       if (!resolve_index_expr (cl->length))
   15748              :         {
   15749           19 :           specification_expr = saved_specification_expr;
   15750           19 :           return false;
   15751              :         }
   15752              :     }
   15753              : 
   15754              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15755              :      a negative value, the length of character entities declared is zero.  */
   15756        97677 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15757        58450 :       && mpz_sgn (cl->length->value.integer) < 0)
   15758            0 :     gfc_replace_expr (cl->length,
   15759              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15760              : 
   15761              :   /* Check that the character length is not too large.  */
   15762        97677 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15763        97677 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15764        58450 :       && cl->length->ts.type == BT_INTEGER
   15765        58450 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15766              :     {
   15767            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15768            4 :       specification_expr = saved_specification_expr;
   15769            4 :       return false;
   15770              :     }
   15771              : 
   15772        97673 :   specification_expr = saved_specification_expr;
   15773        97673 :   return true;
   15774              : }
   15775              : 
   15776              : 
   15777              : /* Test for non-constant shape arrays.  */
   15778              : 
   15779              : static bool
   15780       122555 : is_non_constant_shape_array (gfc_symbol *sym)
   15781              : {
   15782       122555 :   gfc_expr *e;
   15783       122555 :   int i;
   15784       122555 :   bool not_constant;
   15785              : 
   15786       122555 :   not_constant = false;
   15787       122555 :   if (sym->as != NULL)
   15788              :     {
   15789              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15790              :          has not been simplified; parameter array references.  Do the
   15791              :          simplification now.  */
   15792       159928 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15793              :         {
   15794        92213 :           if (i == GFC_MAX_DIMENSIONS)
   15795              :             break;
   15796              : 
   15797        92211 :           e = sym->as->lower[i];
   15798        92211 :           if (e && (!resolve_index_expr(e)
   15799        89241 :                     || !gfc_is_constant_expr (e)))
   15800              :             not_constant = true;
   15801        92211 :           e = sym->as->upper[i];
   15802        92211 :           if (e && (!resolve_index_expr(e)
   15803        87945 :                     || !gfc_is_constant_expr (e)))
   15804              :             not_constant = true;
   15805              :         }
   15806              :     }
   15807       122555 :   return not_constant;
   15808              : }
   15809              : 
   15810              : /* Given a symbol and an initialization expression, add code to initialize
   15811              :    the symbol to the function entry.  */
   15812              : static void
   15813         2371 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15814              : {
   15815         2371 :   gfc_expr *lval;
   15816         2371 :   gfc_code *init_st;
   15817         2371 :   gfc_namespace *ns = sym->ns;
   15818              : 
   15819         2371 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15820              :     {
   15821           46 :       gfc_free_expr (init);
   15822           46 :       return;
   15823              :     }
   15824              : 
   15825              :   /* Search for the function namespace if this is a contained
   15826              :      function without an explicit result.  */
   15827         2325 :   if (sym->attr.function && sym == sym->result
   15828          299 :       && sym->name != sym->ns->proc_name->name)
   15829              :     {
   15830          298 :       ns = ns->contained;
   15831         1376 :       for (;ns; ns = ns->sibling)
   15832         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15833              :           break;
   15834              :     }
   15835              : 
   15836         2325 :   if (ns == NULL)
   15837              :     {
   15838           61 :       gfc_free_expr (init);
   15839           61 :       return;
   15840              :     }
   15841              : 
   15842              :   /* Build an l-value expression for the result.  */
   15843         2264 :   lval = gfc_lval_expr_from_sym (sym);
   15844              : 
   15845              :   /* Add the code at scope entry.  */
   15846         2264 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15847         2264 :   init_st->next = ns->code;
   15848         2264 :   ns->code = init_st;
   15849              : 
   15850              :   /* Assign the default initializer to the l-value.  */
   15851         2264 :   init_st->loc = sym->declared_at;
   15852         2264 :   init_st->expr1 = lval;
   15853         2264 :   init_st->expr2 = init;
   15854              : }
   15855              : 
   15856              : 
   15857              : /* Whether or not we can generate a default initializer for a symbol.  */
   15858              : 
   15859              : static bool
   15860        32889 : can_generate_init (gfc_symbol *sym)
   15861              : {
   15862        32889 :   symbol_attribute *a;
   15863        32889 :   if (!sym)
   15864              :     return false;
   15865        32889 :   a = &sym->attr;
   15866              : 
   15867              :   /* These symbols should never have a default initialization.  */
   15868        54175 :   return !(
   15869        32889 :        a->allocatable
   15870        32889 :     || a->external
   15871        31710 :     || a->pointer
   15872        31710 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15873         6488 :         && (CLASS_DATA (sym)->attr.class_pointer
   15874         4276 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15875        29498 :     || a->in_equivalence
   15876        29377 :     || a->in_common
   15877        29330 :     || a->data
   15878        29152 :     || sym->module
   15879        25082 :     || a->cray_pointee
   15880        25020 :     || a->cray_pointer
   15881        25020 :     || sym->assoc
   15882        21999 :     || (!a->referenced && !a->result)
   15883        21286 :     || (a->dummy && (a->intent != INTENT_OUT
   15884         1225 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15885        21286 :     || (a->function && sym != sym->result)
   15886              :   );
   15887              : }
   15888              : 
   15889              : 
   15890              : /* Assign the default initializer to a derived type variable or result.  */
   15891              : 
   15892              : static void
   15893        12489 : apply_default_init (gfc_symbol *sym)
   15894              : {
   15895        12489 :   gfc_expr *init = NULL;
   15896              : 
   15897        12489 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15898              :     return;
   15899              : 
   15900        12009 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15901        11060 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15902              : 
   15903        12009 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15904              :     return;
   15905              : 
   15906         1989 :   build_init_assign (sym, init);
   15907         1989 :   sym->attr.referenced = 1;
   15908              : }
   15909              : 
   15910              : 
   15911              : /* Build an initializer for a local. Returns null if the symbol should not have
   15912              :    a default initialization.  */
   15913              : 
   15914              : static gfc_expr *
   15915       214982 : build_default_init_expr (gfc_symbol *sym)
   15916              : {
   15917              :   /* These symbols should never have a default initialization.  */
   15918       214982 :   if (sym->attr.allocatable
   15919       200564 :       || sym->attr.external
   15920       200564 :       || sym->attr.dummy
   15921       131518 :       || sym->attr.pointer
   15922       122997 :       || sym->attr.in_equivalence
   15923       120621 :       || sym->attr.in_common
   15924       117519 :       || sym->attr.data
   15925       115221 :       || sym->module
   15926       112385 :       || sym->attr.cray_pointee
   15927       112084 :       || sym->attr.cray_pointer
   15928       111782 :       || sym->assoc)
   15929              :     return NULL;
   15930              : 
   15931              :   /* Get the appropriate init expression.  */
   15932       106422 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15933              : }
   15934              : 
   15935              : /* Add an initialization expression to a local variable.  */
   15936              : static void
   15937       214982 : apply_default_init_local (gfc_symbol *sym)
   15938              : {
   15939       214982 :   gfc_expr *init = NULL;
   15940              : 
   15941              :   /* The symbol should be a variable or a function return value.  */
   15942       214982 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15943       214982 :       || (sym->attr.function && sym->result != sym))
   15944              :     return;
   15945              : 
   15946              :   /* Try to build the initializer expression.  If we can't initialize
   15947              :      this symbol, then init will be NULL.  */
   15948       214982 :   init = build_default_init_expr (sym);
   15949       214982 :   if (init == NULL)
   15950              :     return;
   15951              : 
   15952              :   /* For saved variables, we don't want to add an initializer at function
   15953              :      entry, so we just add a static initializer. Note that automatic variables
   15954              :      are stack allocated even with -fno-automatic; we have also to exclude
   15955              :      result variable, which are also nonstatic.  */
   15956          419 :   if (!sym->attr.automatic
   15957          419 :       && (sym->attr.save || sym->ns->save_all
   15958          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15959           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15960           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15961              :     {
   15962              :       /* Don't clobber an existing initializer!  */
   15963           37 :       gcc_assert (sym->value == NULL);
   15964           37 :       sym->value = init;
   15965           37 :       return;
   15966              :     }
   15967              : 
   15968          382 :   build_init_assign (sym, init);
   15969              : }
   15970              : 
   15971              : 
   15972              : /* Resolution of common features of flavors variable and procedure.  */
   15973              : 
   15974              : static bool
   15975      1036476 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15976              : {
   15977      1036476 :   gfc_array_spec *as;
   15978              : 
   15979      1036476 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15980        21159 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15981        21154 :     as = CLASS_DATA (sym)->as;
   15982              :   else
   15983      1015322 :     as = sym->as;
   15984              : 
   15985              :   /* Constraints on deferred shape variable.  */
   15986      1036476 :   if (as == NULL || as->type != AS_DEFERRED)
   15987              :     {
   15988      1010572 :       bool pointer, allocatable, dimension;
   15989              : 
   15990      1010572 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15991        17529 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15992              :         {
   15993        17524 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15994        17524 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15995        17524 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15996              :         }
   15997              :       else
   15998              :         {
   15999       993048 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   16000       993048 :           allocatable = sym->attr.allocatable;
   16001       993048 :           dimension = sym->attr.dimension;
   16002              :         }
   16003              : 
   16004      1010572 :       if (allocatable)
   16005              :         {
   16006         8664 :           if (dimension
   16007         8664 :               && as
   16008          524 :               && as->type != AS_ASSUMED_RANK
   16009            5 :               && !sym->attr.select_rank_temporary)
   16010              :             {
   16011            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   16012              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   16013            3 :               return false;
   16014              :             }
   16015         8661 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   16016              :                                     "%qs at %L may not be ALLOCATABLE",
   16017              :                                     sym->name, &sym->declared_at))
   16018              :             return false;
   16019              :         }
   16020              : 
   16021      1010568 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   16022              :         {
   16023            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   16024              :                      "assumed rank", sym->name, &sym->declared_at);
   16025            4 :           sym->error = 1;
   16026            4 :           return false;
   16027              :         }
   16028              :     }
   16029              :   else
   16030              :     {
   16031        25904 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   16032         5204 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   16033              :         {
   16034            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   16035              :                      sym->name, &sym->declared_at);
   16036            3 :           return false;
   16037              :          }
   16038              :     }
   16039              : 
   16040              :   /* Constraints on polymorphic variables.  */
   16041      1036465 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   16042              :     {
   16043              :       /* F03:C502.  */
   16044        20419 :       if (sym->attr.class_ok
   16045        20362 :           && sym->ts.u.derived
   16046        20357 :           && !sym->attr.select_type_temporary
   16047        19095 :           && !UNLIMITED_POLY (sym)
   16048        16266 :           && CLASS_DATA (sym)
   16049        16266 :           && CLASS_DATA (sym)->ts.u.derived
   16050        36684 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   16051              :         {
   16052            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   16053            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   16054              :                      &sym->declared_at);
   16055            5 :           return false;
   16056              :         }
   16057              : 
   16058              :       /* F03:C509.  */
   16059              :       /* Assume that use associated symbols were checked in the module ns.
   16060              :          Class-variables that are associate-names are also something special
   16061              :          and excepted from the test.  */
   16062        20414 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   16063           55 :           && !sym->attr.select_type_temporary
   16064           55 :           && !sym->attr.select_rank_temporary)
   16065              :         {
   16066           55 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   16067              :                      "or pointer", sym->name, &sym->declared_at);
   16068           55 :           return false;
   16069              :         }
   16070              :     }
   16071              : 
   16072              :   return true;
   16073              : }
   16074              : 
   16075              : 
   16076              : /* Additional checks for symbols with flavor variable and derived
   16077              :    type.  To be called from resolve_fl_variable.  */
   16078              : 
   16079              : static bool
   16080        87876 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   16081              : {
   16082        87876 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   16083              : 
   16084              :   /* Check to see if a derived type is blocked from being host
   16085              :      associated by the presence of another class I symbol in the same
   16086              :      namespace.  14.6.1.3 of the standard and the discussion on
   16087              :      comp.lang.fortran.  */
   16088        87876 :   if (sym->ts.u.derived
   16089        87871 :       && sym->ns != sym->ts.u.derived->ns
   16090        50495 :       && !sym->ts.u.derived->attr.use_assoc
   16091        19287 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   16092              :     {
   16093        18228 :       gfc_symbol *s;
   16094        18228 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   16095        18228 :       if (s && s->attr.generic)
   16096            2 :         s = gfc_find_dt_in_generic (s);
   16097        18228 :       if (s && !gfc_fl_struct (s->attr.flavor))
   16098              :         {
   16099            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   16100              :                      "because it is blocked by an incompatible object "
   16101              :                      "of the same name declared at %L",
   16102            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   16103              :                      &s->declared_at);
   16104            2 :           return false;
   16105              :         }
   16106              :     }
   16107              : 
   16108              :   /* 4th constraint in section 11.3: "If an object of a type for which
   16109              :      component-initialization is specified (R429) appears in the
   16110              :      specification-part of a module and does not have the ALLOCATABLE
   16111              :      or POINTER attribute, the object shall have the SAVE attribute."
   16112              : 
   16113              :      The check for initializers is performed with
   16114              :      gfc_has_default_initializer because gfc_default_initializer generates
   16115              :      a hidden default for allocatable components.  */
   16116        87128 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   16117        20436 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   16118          473 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   16119           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   16120           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   16121        87883 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   16122              :                           "%qs at %L, needed due to the default "
   16123              :                           "initialization", sym->name, &sym->declared_at))
   16124              :     return false;
   16125              : 
   16126              :   /* Assign default initializer.  */
   16127        87872 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   16128        81132 :       && (!no_init_flag
   16129        62618 :           || (sym->attr.intent == INTENT_OUT
   16130         3489 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   16131        21829 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   16132              : 
   16133              :   return true;
   16134              : }
   16135              : 
   16136              : 
   16137              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   16138              :    except in the declaration of an entity or component that has the POINTER
   16139              :    or ALLOCATABLE attribute.  */
   16140              : 
   16141              : static bool
   16142      1629161 : deferred_requirements (gfc_symbol *sym)
   16143              : {
   16144      1629161 :   if (sym->ts.deferred
   16145         8278 :       && !(sym->attr.pointer
   16146         2514 :            || sym->attr.allocatable
   16147          127 :            || sym->attr.associate_var
   16148            7 :            || sym->attr.omp_udr_artificial_var))
   16149              :     {
   16150              :       /* If a function has a result variable, only check the variable.  */
   16151            7 :       if (sym->result && sym->name != sym->result->name)
   16152              :         return true;
   16153              : 
   16154            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   16155              :                  "requires either the POINTER or ALLOCATABLE attribute",
   16156              :                  sym->name, &sym->declared_at);
   16157            6 :       return false;
   16158              :     }
   16159              :   return true;
   16160              : }
   16161              : 
   16162              : 
   16163              : /* Resolve symbols with flavor variable.  */
   16164              : 
   16165              : static bool
   16166       694075 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   16167              : {
   16168       694075 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   16169              :                                  "SAVE attribute");
   16170              : 
   16171       694075 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   16172              :     return false;
   16173              : 
   16174              :   /* Set this flag to check that variables are parameters of all entries.
   16175              :      This check is effected by the call to gfc_resolve_expr through
   16176              :      is_non_constant_shape_array.  */
   16177       694014 :   bool saved_specification_expr = specification_expr;
   16178       694014 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   16179       694014 :   specification_expr = true;
   16180       694014 :   specification_expr_symbol = sym;
   16181              : 
   16182       694014 :   if (sym->ns->proc_name
   16183       693919 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   16184       688482 :           || sym->ns->proc_name->attr.is_main_program)
   16185        87032 :       && !sym->attr.use_assoc
   16186        83464 :       && !sym->attr.allocatable
   16187        77301 :       && !sym->attr.pointer
   16188       767551 :       && is_non_constant_shape_array (sym))
   16189              :     {
   16190              :       /* F08:C541. The shape of an array defined in a main program or module
   16191              :        * needs to be constant.  */
   16192            3 :       gfc_error ("The module or main program array %qs at %L must "
   16193              :                  "have constant shape", sym->name, &sym->declared_at);
   16194            3 :       specification_expr = saved_specification_expr;
   16195            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   16196            3 :       return false;
   16197              :     }
   16198              : 
   16199              :   /* Constraints on deferred type parameter.  */
   16200       694011 :   if (!deferred_requirements (sym))
   16201              :     return false;
   16202              : 
   16203       694007 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   16204              :     {
   16205              :       /* Make sure that character string variables with assumed length are
   16206              :          dummy arguments.  */
   16207        37329 :       gfc_expr *e = NULL;
   16208              : 
   16209        37329 :       if (sym->ts.u.cl)
   16210        37329 :         e = sym->ts.u.cl->length;
   16211              :       else
   16212              :         return false;
   16213              : 
   16214        37329 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   16215         2736 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   16216            2 :           && !sym->attr.omp_udr_artificial_var)
   16217              :         {
   16218            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   16219              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   16220            2 :           specification_expr = saved_specification_expr;
   16221            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   16222            2 :           return false;
   16223              :         }
   16224              : 
   16225        21700 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   16226              :         {
   16227            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   16228            1 :           specification_expr = saved_specification_expr;
   16229            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   16230            1 :           return false;
   16231              :         }
   16232              : 
   16233        37326 :       if (!gfc_is_constant_expr (e)
   16234        37326 :           && !(e->expr_type == EXPR_VARIABLE
   16235         1466 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   16236              :         {
   16237         2286 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   16238         1764 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   16239         1763 :                   || sym->ns->proc_name->attr.is_main_program))
   16240              :             {
   16241            3 :               gfc_error ("%qs at %L must have constant character length "
   16242              :                         "in this context", sym->name, &sym->declared_at);
   16243            3 :               specification_expr = saved_specification_expr;
   16244            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   16245            3 :               return false;
   16246              :             }
   16247         2283 :           if (sym->attr.in_common)
   16248              :             {
   16249            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   16250              :                          "character length", sym->name, &sym->declared_at);
   16251            1 :               specification_expr = saved_specification_expr;
   16252            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   16253            1 :               return false;
   16254              :             }
   16255              :         }
   16256              :     }
   16257              : 
   16258       694000 :   if (sym->value == NULL && sym->attr.referenced
   16259       216948 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   16260       214982 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   16261              : 
   16262              :   /* Determine if the symbol may not have an initializer.  */
   16263       694000 :   int no_init_flag = 0, automatic_flag = 0;
   16264       694000 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   16265       178074 :       || sym->attr.intrinsic || sym->attr.result)
   16266              :     no_init_flag = 1;
   16267       145028 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   16268       180823 :            && is_non_constant_shape_array (sym))
   16269              :     {
   16270         1356 :       no_init_flag = automatic_flag = 1;
   16271              : 
   16272              :       /* Also, they must not have the SAVE attribute.
   16273              :          SAVE_IMPLICIT is checked below.  */
   16274         1356 :       if (sym->as && sym->attr.codimension)
   16275              :         {
   16276            7 :           int corank = sym->as->corank;
   16277            7 :           sym->as->corank = 0;
   16278            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   16279            7 :           sym->as->corank = corank;
   16280              :         }
   16281         1356 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   16282              :         {
   16283            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   16284            2 :           specification_expr = saved_specification_expr;
   16285            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   16286            2 :           return false;
   16287              :         }
   16288              :     }
   16289              : 
   16290              :   /* Ensure that any initializer is simplified.  */
   16291       693998 :   if (sym->value)
   16292         8723 :     gfc_simplify_expr (sym->value, 1);
   16293              : 
   16294              :   /* Reject illegal initializers.  */
   16295       693998 :   if (!sym->mark && sym->value)
   16296              :     {
   16297         8723 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   16298           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   16299            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   16300              :                    sym->name, &sym->declared_at);
   16301         8722 :       else if (sym->attr.external)
   16302            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   16303              :                    sym->name, &sym->declared_at);
   16304         8722 :       else if (sym->attr.dummy)
   16305            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   16306              :                    sym->name, &sym->declared_at);
   16307         8719 :       else if (sym->attr.intrinsic)
   16308            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   16309              :                    sym->name, &sym->declared_at);
   16310         8719 :       else if (sym->attr.result)
   16311            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   16312              :                    sym->name, &sym->declared_at);
   16313         8718 :       else if (automatic_flag)
   16314            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   16315              :                    sym->name, &sym->declared_at);
   16316              :       else
   16317         8713 :         goto no_init_error;
   16318           10 :       specification_expr = saved_specification_expr;
   16319           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   16320           10 :       return false;
   16321              :     }
   16322              : 
   16323       685275 : no_init_error:
   16324       693988 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   16325              :     {
   16326        87876 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   16327        87876 :       specification_expr = saved_specification_expr;
   16328        87876 :       specification_expr_symbol = saved_specification_expr_symbol;
   16329        87876 :       return res;
   16330              :     }
   16331              : 
   16332       606112 :   specification_expr = saved_specification_expr;
   16333       606112 :   specification_expr_symbol = saved_specification_expr_symbol;
   16334       606112 :   return true;
   16335              : }
   16336              : 
   16337              : 
   16338              : /* Compare the dummy characteristics of a module procedure interface
   16339              :    declaration with the corresponding declaration in a submodule.  */
   16340              : static gfc_formal_arglist *new_formal;
   16341              : static char errmsg[200];
   16342              : 
   16343              : static void
   16344         1428 : compare_fsyms (gfc_symbol *sym)
   16345              : {
   16346         1428 :   gfc_symbol *fsym;
   16347              : 
   16348         1428 :   if (sym == NULL || new_formal == NULL)
   16349              :     return;
   16350              : 
   16351         1428 :   fsym = new_formal->sym;
   16352              : 
   16353         1428 :   if (sym == fsym)
   16354              :     return;
   16355              : 
   16356         1404 :   if (strcmp (sym->name, fsym->name) == 0)
   16357              :     {
   16358          561 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   16359            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   16360              :     }
   16361              : }
   16362              : 
   16363              : 
   16364              : /* Resolve a procedure.  */
   16365              : 
   16366              : static bool
   16367       514961 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   16368              : {
   16369       514961 :   gfc_formal_arglist *arg;
   16370       514961 :   bool allocatable_or_pointer = false;
   16371              : 
   16372       514961 :   if (sym->attr.function
   16373       514961 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   16374              :     return false;
   16375              : 
   16376              :   /* Constraints on deferred type parameter.  */
   16377       514951 :   if (!deferred_requirements (sym))
   16378              :     return false;
   16379              : 
   16380       514950 :   if (sym->ts.type == BT_CHARACTER)
   16381              :     {
   16382        12152 :       gfc_charlen *cl = sym->ts.u.cl;
   16383              : 
   16384         7897 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   16385        13519 :              && !resolve_charlen (cl))
   16386              :         return false;
   16387              : 
   16388        12151 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   16389        10785 :           && sym->attr.proc == PROC_ST_FUNCTION)
   16390              :         {
   16391            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   16392              :                      "have constant length", sym->name, &sym->declared_at);
   16393            0 :           return false;
   16394              :         }
   16395              :     }
   16396              : 
   16397              :   /* Ensure that derived type for are not of a private type.  Internal
   16398              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   16399              :      externally accessible and can access all the objects accessible in
   16400              :      the host.  */
   16401       119155 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   16402       119155 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   16403       605607 :       && gfc_check_symbol_access (sym))
   16404              :     {
   16405       479141 :       gfc_interface *iface;
   16406              : 
   16407      1018634 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   16408              :         {
   16409       539494 :           if (arg->sym
   16410       539354 :               && arg->sym->ts.type == BT_DERIVED
   16411        44599 :               && arg->sym->ts.u.derived
   16412        44599 :               && !arg->sym->ts.u.derived->attr.use_assoc
   16413         4675 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16414       539503 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   16415              :                                   "and cannot be a dummy argument"
   16416              :                                   " of %qs, which is PUBLIC at %L",
   16417            9 :                                   arg->sym->name, sym->name,
   16418              :                                   &sym->declared_at))
   16419              :             {
   16420              :               /* Stop this message from recurring.  */
   16421            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16422            1 :               return false;
   16423              :             }
   16424              :         }
   16425              : 
   16426              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   16427              :          PRIVATE to the containing module.  */
   16428       678162 :       for (iface = sym->generic; iface; iface = iface->next)
   16429              :         {
   16430       466740 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   16431              :             {
   16432       267718 :               if (arg->sym
   16433       267686 :                   && arg->sym->ts.type == BT_DERIVED
   16434         8055 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   16435          234 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16436       267722 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   16437              :                                       "PUBLIC interface %qs at %L "
   16438              :                                       "takes dummy arguments of %qs which "
   16439              :                                       "is PRIVATE", iface->sym->name,
   16440            4 :                                       sym->name, &iface->sym->declared_at,
   16441            4 :                                       gfc_typename(&arg->sym->ts)))
   16442              :                 {
   16443              :                   /* Stop this message from recurring.  */
   16444            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16445            1 :                   return false;
   16446              :                 }
   16447              :              }
   16448              :         }
   16449              :     }
   16450              : 
   16451       514947 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   16452           89 :       && !sym->attr.proc_pointer)
   16453              :     {
   16454            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   16455              :                  sym->name, &sym->declared_at);
   16456              : 
   16457              :       /* Make sure no second error is issued for this.  */
   16458            2 :       sym->value->error = 1;
   16459            2 :       return false;
   16460              :     }
   16461              : 
   16462              :   /* An external symbol may not have an initializer because it is taken to be
   16463              :      a procedure. Exception: Procedure Pointers.  */
   16464       514945 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   16465              :     {
   16466            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   16467              :                  sym->name, &sym->declared_at);
   16468            0 :       return false;
   16469              :     }
   16470              : 
   16471              :   /* An elemental function is required to return a scalar 12.7.1  */
   16472       514945 :   if (sym->attr.elemental && sym->attr.function
   16473        86717 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   16474            2 :                       && CLASS_DATA (sym)->as)))
   16475              :     {
   16476            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   16477              :                  "result", sym->name, &sym->declared_at);
   16478              :       /* Reset so that the error only occurs once.  */
   16479            3 :       sym->attr.elemental = 0;
   16480            3 :       return false;
   16481              :     }
   16482              : 
   16483       514942 :   if (sym->attr.proc == PROC_ST_FUNCTION
   16484          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   16485              :     {
   16486            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   16487              :                  "allocatable attribute", sym->name, &sym->declared_at);
   16488            2 :       return false;
   16489              :     }
   16490              : 
   16491              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   16492              :      char-len-param shall not be array-valued, pointer-valued, recursive
   16493              :      or pure.  ....snip... A character value of * may only be used in the
   16494              :      following ways: (i) Dummy arg of procedure - dummy associates with
   16495              :      actual length; (ii) To declare a named constant; or (iii) External
   16496              :      function - but length must be declared in calling scoping unit.  */
   16497       514940 :   if (sym->attr.function
   16498       342382 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   16499         6951 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   16500              :     {
   16501          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   16502          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   16503              :         {
   16504            4 :           if (sym->as && sym->as->rank)
   16505            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16506              :                        "array-valued", sym->name, &sym->declared_at);
   16507              : 
   16508            4 :           if (sym->attr.pointer)
   16509            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16510              :                        "pointer-valued", sym->name, &sym->declared_at);
   16511              : 
   16512            4 :           if (sym->attr.pure)
   16513            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16514              :                        "pure", sym->name, &sym->declared_at);
   16515              : 
   16516            4 :           if (sym->attr.recursive)
   16517            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16518              :                        "recursive", sym->name, &sym->declared_at);
   16519              : 
   16520              :           return false;
   16521              :         }
   16522              : 
   16523              :       /* Appendix B.2 of the standard.  Contained functions give an
   16524              :          error anyway.  Deferred character length is an F2003 feature.
   16525              :          Don't warn on intrinsic conversion functions, which start
   16526              :          with two underscores.  */
   16527          176 :       if (!sym->attr.contained && !sym->ts.deferred
   16528          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   16529          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   16530              :                         "CHARACTER(*) function %qs at %L",
   16531              :                         sym->name, &sym->declared_at);
   16532              :     }
   16533              : 
   16534              :   /* F2008, C1218.  */
   16535       514936 :   if (sym->attr.elemental)
   16536              :     {
   16537        90031 :       if (sym->attr.proc_pointer)
   16538              :         {
   16539            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   16540              :                                                : sym->name);
   16541            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   16542              :                      name, &sym->declared_at);
   16543            7 :           return false;
   16544              :         }
   16545        90024 :       if (sym->attr.dummy)
   16546              :         {
   16547            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   16548              :                      sym->name, &sym->declared_at);
   16549            3 :           return false;
   16550              :         }
   16551              :     }
   16552              : 
   16553              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   16554              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   16555              :      pointer is tested and caught elsewhere.  */
   16556       514926 :   if (sym->result)
   16557       288171 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   16558       288171 :                              && CLASS_DATA (sym->result) ?
   16559         1745 :                              (CLASS_DATA (sym->result)->attr.allocatable
   16560         1745 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   16561       286426 :                              (sym->result->attr.allocatable
   16562       286426 :                               || sym->result->attr.pointer);
   16563              : 
   16564       514926 :   if (sym->attr.elemental && sym->result
   16565        86322 :       && allocatable_or_pointer)
   16566              :     {
   16567            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   16568              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   16569              :                  "attribute", sym->result->name,
   16570              :                  &sym->result->declared_at, sym->name);
   16571            4 :       return false;
   16572              :     }
   16573              : 
   16574              :   /* F2018:C1585: "The function result of a pure function shall not be both
   16575              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   16576              :      component."  */
   16577       514922 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   16578              :     {
   16579         2550 :       if (sym->ts.type == BT_CLASS
   16580            5 :           && sym->attr.class_ok
   16581            4 :           && CLASS_DATA (sym->result)
   16582            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   16583              :         {
   16584            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   16585              :                      "polymorphic allocatable",
   16586              :                      sym->result->name, &sym->result->declared_at);
   16587            4 :           return false;
   16588              :         }
   16589              : 
   16590         2546 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   16591              :         {
   16592              :           gfc_component *c = sym->ts.u.derived->components;
   16593         4820 :           for (; c; c = c->next)
   16594         2583 :             if (c->ts.type == BT_CLASS
   16595            2 :                 && CLASS_DATA (c)
   16596            2 :                 && CLASS_DATA (c)->attr.allocatable)
   16597              :               {
   16598            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   16599              :                            "polymorphic allocatable component %qs",
   16600              :                            sym->result->name, &sym->result->declared_at,
   16601              :                            c->name);
   16602            2 :                 return false;
   16603              :               }
   16604              :         }
   16605              :     }
   16606              : 
   16607       514916 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   16608              :     {
   16609         7474 :       gfc_formal_arglist *curr_arg;
   16610         7474 :       int has_non_interop_arg = 0;
   16611              : 
   16612         7474 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   16613         7474 :                               sym->common_block))
   16614              :         {
   16615              :           /* Clear these to prevent looking at them again if there was an
   16616              :              error.  */
   16617            2 :           sym->attr.is_bind_c = 0;
   16618            2 :           sym->attr.is_c_interop = 0;
   16619            2 :           sym->ts.is_c_interop = 0;
   16620              :         }
   16621              :       else
   16622              :         {
   16623              :           /* So far, no errors have been found.  */
   16624              :           sym->attr.is_c_interop = 1;
   16625              :           sym->ts.is_c_interop = 1;
   16626              :         }
   16627              : 
   16628         7474 :       curr_arg = gfc_sym_get_dummy_args (sym);
   16629        32673 :       while (curr_arg != NULL)
   16630              :         {
   16631              :           /* Skip implicitly typed dummy args here.  */
   16632        17725 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   16633        17668 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   16634              :               /* If something is found to fail, record the fact so we
   16635              :                  can mark the symbol for the procedure as not being
   16636              :                  BIND(C) to try and prevent multiple errors being
   16637              :                  reported.  */
   16638        17725 :               has_non_interop_arg = 1;
   16639              : 
   16640        17725 :           curr_arg = curr_arg->next;
   16641              :         }
   16642              : 
   16643              :       /* See if any of the arguments were not interoperable and if so, clear
   16644              :          the procedure symbol to prevent duplicate error messages.  */
   16645         7474 :       if (has_non_interop_arg != 0)
   16646              :         {
   16647          128 :           sym->attr.is_c_interop = 0;
   16648          128 :           sym->ts.is_c_interop = 0;
   16649          128 :           sym->attr.is_bind_c = 0;
   16650              :         }
   16651              :     }
   16652              : 
   16653       514916 :   if (!sym->attr.proc_pointer)
   16654              :     {
   16655       513797 :       if (sym->attr.save == SAVE_EXPLICIT)
   16656              :         {
   16657            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   16658              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16659            5 :           return false;
   16660              :         }
   16661       513792 :       if (sym->attr.intent)
   16662              :         {
   16663            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   16664              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16665            1 :           return false;
   16666              :         }
   16667       513791 :       if (sym->attr.subroutine && sym->attr.result)
   16668              :         {
   16669            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   16670            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   16671            2 :           return false;
   16672              :         }
   16673       513789 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   16674       148154 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   16675       148151 :               || sym->attr.contained))
   16676              :         {
   16677            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   16678              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16679            3 :           return false;
   16680              :         }
   16681       513786 :       if (strcmp ("ppr@", sym->name) == 0)
   16682              :         {
   16683            0 :           gfc_error ("Procedure pointer result %qs at %L "
   16684              :                      "is missing the pointer attribute",
   16685            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   16686            0 :           return false;
   16687              :         }
   16688              :     }
   16689              : 
   16690              :   /* Assume that a procedure whose body is not known has references
   16691              :      to external arrays.  */
   16692       514905 :   if (sym->attr.if_source != IFSRC_DECL)
   16693       353760 :     sym->attr.array_outer_dependency = 1;
   16694              : 
   16695              :   /* Compare the characteristics of a module procedure with the
   16696              :      interface declaration. Ideally this would be done with
   16697              :      gfc_compare_interfaces but, at present, the formal interface
   16698              :      cannot be copied to the ts.interface.  */
   16699       514905 :   if (sym->attr.module_procedure
   16700         1731 :       && sym->attr.if_source == IFSRC_DECL)
   16701              :     {
   16702          698 :       gfc_symbol *iface;
   16703          698 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   16704          698 :       char *module_name;
   16705          698 :       char *submodule_name;
   16706          698 :       strcpy (name, sym->ns->proc_name->name);
   16707          698 :       module_name = strtok (name, ".");
   16708          698 :       submodule_name = strtok (NULL, ".");
   16709              : 
   16710          698 :       iface = sym->tlink;
   16711          698 :       sym->tlink = NULL;
   16712              : 
   16713              :       /* Make sure that the result uses the correct charlen for deferred
   16714              :          length results.  */
   16715          698 :       if (iface && sym->result
   16716          230 :           && iface->ts.type == BT_CHARACTER
   16717           19 :           && iface->ts.deferred)
   16718            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16719              : 
   16720            6 :       if (iface == NULL)
   16721          196 :         goto check_formal;
   16722              : 
   16723              :       /* Check the procedure characteristics.  */
   16724          502 :       if (sym->attr.elemental != iface->attr.elemental)
   16725              :         {
   16726            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16727              :                      "PROCEDURE at %L and its interface in %s",
   16728              :                      &sym->declared_at, module_name);
   16729           10 :           return false;
   16730              :         }
   16731              : 
   16732          501 :       if (sym->attr.pure != iface->attr.pure)
   16733              :         {
   16734            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16735              :                      "PROCEDURE at %L and its interface in %s",
   16736              :                      &sym->declared_at, module_name);
   16737            2 :           return false;
   16738              :         }
   16739              : 
   16740          499 :       if (sym->attr.recursive != iface->attr.recursive)
   16741              :         {
   16742            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16743              :                      "PROCEDURE at %L and its interface in %s",
   16744              :                      &sym->declared_at, module_name);
   16745            2 :           return false;
   16746              :         }
   16747              : 
   16748              :       /* Check the result characteristics.  */
   16749          497 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16750              :         {
   16751            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16752              :                      "in MODULE %qs and the declaration at %L in "
   16753              :                      "(SUB)MODULE %qs",
   16754              :                      errmsg, module_name, &sym->declared_at,
   16755              :                      submodule_name ? submodule_name : module_name);
   16756            5 :           return false;
   16757              :         }
   16758              : 
   16759          492 : check_formal:
   16760              :       /* Check the characteristics of the formal arguments.  */
   16761          688 :       if (sym->formal && sym->formal_ns)
   16762              :         {
   16763         1336 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16764              :             {
   16765          760 :               new_formal = arg;
   16766          760 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16767              :             }
   16768              :         }
   16769              :     }
   16770              : 
   16771              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16772              :      BIND(C) attribute.  */
   16773       514895 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16774              :     {
   16775            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16776              :                  sym->name, &sym->declared_at);
   16777            1 :       return false;
   16778              :     }
   16779              : 
   16780              :   return true;
   16781              : }
   16782              : 
   16783              : 
   16784              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16785              :    been defined and we now know their defined arguments, check that they fulfill
   16786              :    the requirements of the standard for procedures used as finalizers.  */
   16787              : 
   16788              : static bool
   16789       122805 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16790              : {
   16791       122805 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16792       122805 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16793       122805 :   bool result = true;
   16794       122805 :   bool seen_scalar = false;
   16795       122805 :   gfc_symbol *vtab;
   16796       122805 :   gfc_component *c;
   16797       122805 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16798              : 
   16799       122805 :   if (parent)
   16800        17561 :     gfc_resolve_finalizers (parent, finalizable);
   16801              : 
   16802              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16803       122805 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16804       390773 :   for (c = derived->components; c; c = c->next)
   16805       267968 :     if (c->ts.type == BT_DERIVED
   16806        74698 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16807              :       {
   16808         9802 :         bool has_final2 = false;
   16809         9802 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16810            0 :           return false;  /* Error.  */
   16811         9802 :         has_final = has_final || has_final2;
   16812              :       }
   16813              :   /* Return early if not finalizable.  */
   16814       122805 :   if (!has_final)
   16815              :     {
   16816       119856 :       if (finalizable)
   16817         9782 :         *finalizable = false;
   16818              :       return true;
   16819              :     }
   16820              : 
   16821              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16822              :      the template. If the finalizers field has the same value, it needs to be
   16823              :      supplied with finalizers of the same pdt_type.  */
   16824         2949 :   if (derived->attr.pdt_type
   16825          126 :       && derived->template_sym
   16826           54 :       && derived->template_sym->f2k_derived
   16827           54 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16828         3003 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16829              :     {
   16830           42 :       gfc_finalizer *tmp = NULL;
   16831           42 :       derived->f2k_derived->finalizers = NULL;
   16832           42 :       prev_link = &derived->f2k_derived->finalizers;
   16833          144 :       for (list = pdt_finalizers; list; list = list->next)
   16834              :         {
   16835          102 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16836          102 :           if (args->sym
   16837          102 :               && args->sym->ts.type == BT_DERIVED
   16838          102 :               && args->sym->ts.u.derived
   16839          102 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16840              :             {
   16841           60 :               tmp = gfc_get_finalizer ();
   16842           60 :               *tmp = *list;
   16843           60 :               tmp->next = NULL;
   16844           60 :               *prev_link = tmp;
   16845           60 :               prev_link = &(tmp->next);
   16846           60 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16847              :             }
   16848              :         }
   16849              :     }
   16850              : 
   16851              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16852              :      does not fit in with the standard's definition, print an error and remove
   16853              :      it from the list.  */
   16854         2949 :   prev_link = &derived->f2k_derived->finalizers;
   16855         6046 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16856              :     {
   16857         3097 :       gfc_formal_arglist *dummy_args;
   16858         3097 :       gfc_symbol* arg;
   16859         3097 :       gfc_finalizer* i;
   16860         3097 :       int my_rank;
   16861              : 
   16862              :       /* Skip this finalizer if we already resolved it.  */
   16863         3097 :       if (list->proc_tree)
   16864              :         {
   16865         2486 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16866          620 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16867         1866 :             seen_scalar = true;
   16868         2486 :           prev_link = &(list->next);
   16869         2486 :           continue;
   16870              :         }
   16871              : 
   16872              :       /* Check this exists and is a SUBROUTINE.  */
   16873          611 :       if (!list->proc_sym->attr.subroutine)
   16874              :         {
   16875            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16876              :                      list->proc_sym->name, &list->where);
   16877            3 :           goto error;
   16878              :         }
   16879              : 
   16880              :       /* We should have exactly one argument.  */
   16881          608 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16882          608 :       if (!dummy_args || dummy_args->next)
   16883              :         {
   16884            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16885              :                      &list->where);
   16886            2 :           goto error;
   16887              :         }
   16888          606 :       arg = dummy_args->sym;
   16889              : 
   16890          606 :       if (!arg)
   16891              :         {
   16892            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16893            1 :                      &list->proc_sym->declared_at, derived->name);
   16894            1 :           goto error;
   16895              :         }
   16896              : 
   16897          605 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16898            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16899              :         {
   16900            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16901              :                      "be the only finalizer with the same kind/type "
   16902              :                      "(F2018: C790)", &list->where);
   16903            0 :           goto error;
   16904              :         }
   16905              : 
   16906              :       /* This argument must be of our type.  */
   16907          605 :       if (!derived->attr.pdt_template
   16908          587 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16909              :         {
   16910            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16911              :                      &arg->declared_at, derived->name);
   16912            2 :           goto error;
   16913              :         }
   16914              : 
   16915              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16916          603 :       if (arg->attr.pointer)
   16917              :         {
   16918            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16919              :                      &arg->declared_at);
   16920            1 :           goto error;
   16921              :         }
   16922          602 :       if (arg->attr.allocatable)
   16923              :         {
   16924            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16925              :                      " ALLOCATABLE", &arg->declared_at);
   16926            1 :           goto error;
   16927              :         }
   16928          601 :       if (arg->attr.optional)
   16929              :         {
   16930            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16931              :                      &arg->declared_at);
   16932            1 :           goto error;
   16933              :         }
   16934              : 
   16935              :       /* It must not be INTENT(OUT).  */
   16936          600 :       if (arg->attr.intent == INTENT_OUT)
   16937              :         {
   16938            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16939              :                      " INTENT(OUT)", &arg->declared_at);
   16940            1 :           goto error;
   16941              :         }
   16942              : 
   16943              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16944          599 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16945            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16946            2 :         gfc_warning (OPT_Wsurprising,
   16947              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16948              :                      " shape argument", &arg->declared_at);
   16949              : 
   16950              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16951              :          defined earlier.  To really loop over the *earlier* declarations,
   16952              :          we need to walk the tail of the list as new ones were pushed at the
   16953              :          front.  */
   16954              :       /* TODO: Handle kind parameters once they are implemented.  */
   16955          599 :       my_rank = (arg->as ? arg->as->rank : 0);
   16956          712 :       for (i = list->next; i; i = i->next)
   16957              :         {
   16958          115 :           gfc_formal_arglist *dummy_args;
   16959              : 
   16960              :           /* Argument list might be empty; that is an error signalled earlier,
   16961              :              but we nevertheless continued resolving.  */
   16962          115 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16963          115 :           if (dummy_args && !derived->attr.pdt_template)
   16964              :             {
   16965          113 :               gfc_symbol* i_arg = dummy_args->sym;
   16966          113 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16967          113 :               if (i_rank == my_rank)
   16968              :                 {
   16969            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16970              :                              " rank (%d) as %qs",
   16971            2 :                              list->proc_sym->name, &list->where, my_rank,
   16972            2 :                              i->proc_sym->name);
   16973            2 :                   list->proc_sym->refs++;
   16974            2 :                   goto error;
   16975              :                 }
   16976              :             }
   16977              :         }
   16978              : 
   16979              :         /* Is this the/a scalar finalizer procedure?  */
   16980          597 :         if (my_rank == 0)
   16981          459 :           seen_scalar = true;
   16982              : 
   16983              :         /* Find the symtree for this procedure.  */
   16984          597 :         gcc_assert (!list->proc_tree);
   16985          597 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16986              : 
   16987          597 :         prev_link = &list->next;
   16988          597 :         continue;
   16989              : 
   16990              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16991              :            troubles in the future when they might fail later expectations.  */
   16992           14 : error:
   16993           14 :         i = list;
   16994           14 :         *prev_link = list->next;
   16995           14 :         gfc_free_finalizer (i);
   16996           14 :         result = false;
   16997          597 :     }
   16998              : 
   16999         2949 :   if (result == false)
   17000              :     return false;
   17001              : 
   17002              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   17003              :      were nodes in the list, must have been for arrays.  It is surely a good
   17004              :      idea to have a scalar version there if there's something to finalize.  */
   17005         2945 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   17006            1 :     gfc_warning (OPT_Wsurprising,
   17007              :                  "Only array FINAL procedures declared for derived type %qs"
   17008              :                  " defined at %L, suggest also scalar one unless an assumed"
   17009              :                  " rank finalizer has been declared",
   17010              :                  derived->name, &derived->declared_at);
   17011              : 
   17012         2945 :   if (!derived->attr.pdt_template)
   17013              :     {
   17014         2837 :       vtab = gfc_find_derived_vtab (derived);
   17015         2837 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   17016         2837 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   17017         2837 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   17018              :     }
   17019              : 
   17020         2945 :   if (finalizable)
   17021          712 :     *finalizable = true;
   17022              : 
   17023              :   return true;
   17024              : }
   17025              : 
   17026              : 
   17027              : static gfc_symbol * containing_dt;
   17028              : 
   17029              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   17030              :    arguments whose declared types are PDT instances only transmit the PASS arg
   17031              :    if they match the enclosing derived type.  */
   17032              : 
   17033              : static bool
   17034         1544 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   17035              : {
   17036         1544 :   gfc_formal_arglist *dummy_args;
   17037         1544 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   17038              :     {
   17039          556 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   17040         1238 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   17041          126 :         dummy_args = dummy_args->next;
   17042          556 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   17043          556 :       if (dummy_args->sym->ts.type == BT_CLASS
   17044          556 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   17045              :                      containing_dt->name))
   17046          368 :         return true;
   17047              :     }
   17048              :   return false;
   17049              : }
   17050              : 
   17051              : 
   17052              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   17053              : 
   17054              : static bool
   17055          774 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   17056              :                              const char* generic_name, locus where)
   17057              : {
   17058          774 :   gfc_symbol *sym1, *sym2;
   17059          774 :   const char *pass1, *pass2;
   17060          774 :   gfc_formal_arglist *dummy_args;
   17061              : 
   17062          774 :   gcc_assert (t1->specific && t2->specific);
   17063          774 :   gcc_assert (!t1->specific->is_generic);
   17064          774 :   gcc_assert (!t2->specific->is_generic);
   17065          774 :   gcc_assert (t1->is_operator == t2->is_operator);
   17066              : 
   17067          774 :   sym1 = t1->specific->u.specific->n.sym;
   17068          774 :   sym2 = t2->specific->u.specific->n.sym;
   17069              : 
   17070          774 :   if (sym1 == sym2)
   17071              :     return true;
   17072              : 
   17073              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   17074          774 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   17075          772 :       || sym1->attr.function != sym2->attr.function)
   17076              :     {
   17077            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   17078              :                  " GENERIC %qs at %L",
   17079              :                  sym1->name, sym2->name, generic_name, &where);
   17080            2 :       return false;
   17081              :     }
   17082              : 
   17083              :   /* Determine PASS arguments.  */
   17084          772 :   if (t1->specific->nopass)
   17085              :     pass1 = NULL;
   17086          721 :   else if (t1->specific->pass_arg)
   17087              :     pass1 = t1->specific->pass_arg;
   17088              :   else
   17089              :     {
   17090          462 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   17091          462 :       if (dummy_args)
   17092          461 :         pass1 = dummy_args->sym->name;
   17093              :       else
   17094              :         pass1 = NULL;
   17095              :     }
   17096          772 :   if (t2->specific->nopass)
   17097              :     pass2 = NULL;
   17098          720 :   else if (t2->specific->pass_arg)
   17099              :     pass2 = t2->specific->pass_arg;
   17100              :   else
   17101              :     {
   17102          583 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   17103          583 :       if (dummy_args)
   17104          582 :         pass2 = dummy_args->sym->name;
   17105              :       else
   17106              :         pass2 = NULL;
   17107              :     }
   17108              : 
   17109              :   /* Care must be taken with pdt types and templates because the declared type
   17110              :      of the argument that is not 'no_pass' need not be the same as the
   17111              :      containing derived type.  If this is the case, subject the argument to
   17112              :      the full interface check, even though it cannot be used in the type
   17113              :      bound context.  */
   17114          772 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   17115          772 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   17116              : 
   17117          772 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   17118          772 :     pass1 = pass2 = NULL;
   17119              : 
   17120              :   /* Compare the interfaces.  */
   17121          772 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   17122              :                               NULL, 0, pass1, pass2))
   17123              :     {
   17124            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   17125              :                  sym1->name, sym2->name, generic_name, &where);
   17126            8 :       return false;
   17127              :     }
   17128              : 
   17129              :   return true;
   17130              : }
   17131              : 
   17132              : 
   17133              : /* Worker function for resolving a generic procedure binding; this is used to
   17134              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   17135              : 
   17136              :    The difference between those cases is finding possible inherited bindings
   17137              :    that are overridden, as one has to look for them in tb_sym_root,
   17138              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   17139              :    the super-type and set p->overridden correctly.  */
   17140              : 
   17141              : static bool
   17142         2493 : resolve_tb_generic_targets (gfc_symbol* super_type,
   17143              :                             gfc_typebound_proc* p, const char* name)
   17144              : {
   17145         2493 :   gfc_tbp_generic* target;
   17146         2493 :   gfc_symtree* first_target;
   17147         2493 :   gfc_symtree* inherited;
   17148              : 
   17149         2493 :   gcc_assert (p && p->is_generic);
   17150              : 
   17151              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   17152         2493 :   gcc_assert (p->u.generic);
   17153         5638 :   for (target = p->u.generic; target; target = target->next)
   17154         3162 :     if (!target->specific)
   17155              :       {
   17156         2699 :         gfc_typebound_proc* overridden_tbp;
   17157         2699 :         gfc_tbp_generic* g;
   17158         2699 :         const char* target_name;
   17159              : 
   17160         2699 :         target_name = target->specific_st->name;
   17161              : 
   17162              :         /* Defined for this type directly.  */
   17163         2699 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   17164              :           {
   17165         2690 :             target->specific = target->specific_st->n.tb;
   17166         2690 :             goto specific_found;
   17167              :           }
   17168              : 
   17169              :         /* Look for an inherited specific binding.  */
   17170            9 :         if (super_type)
   17171              :           {
   17172            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   17173              :                                                  true, NULL);
   17174              : 
   17175            5 :             if (inherited)
   17176              :               {
   17177            5 :                 gcc_assert (inherited->n.tb);
   17178            5 :                 target->specific = inherited->n.tb;
   17179            5 :                 goto specific_found;
   17180              :               }
   17181              :           }
   17182              : 
   17183            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   17184              :                    " at %L", target_name, name, &p->where);
   17185            4 :         return false;
   17186              : 
   17187              :         /* Once we've found the specific binding, check it is not ambiguous with
   17188              :            other specifics already found or inherited for the same GENERIC.  */
   17189         2695 : specific_found:
   17190         2695 :         gcc_assert (target->specific);
   17191              : 
   17192              :         /* This must really be a specific binding!  */
   17193         2695 :         if (target->specific->is_generic)
   17194              :           {
   17195            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   17196              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   17197            3 :             return false;
   17198              :           }
   17199              : 
   17200              :         /* Check those already resolved on this type directly.  */
   17201         6882 :         for (g = p->u.generic; g; g = g->next)
   17202         1512 :           if (g != target && g->specific
   17203         4953 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   17204              :             return false;
   17205              : 
   17206              :         /* Check for ambiguity with inherited specific targets.  */
   17207         2701 :         for (overridden_tbp = p->overridden; overridden_tbp;
   17208           16 :              overridden_tbp = overridden_tbp->overridden)
   17209           19 :           if (overridden_tbp->is_generic)
   17210              :             {
   17211           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   17212              :                 {
   17213           18 :                   gcc_assert (g->specific);
   17214           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   17215              :                     return false;
   17216              :                 }
   17217              :             }
   17218              :       }
   17219              : 
   17220              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   17221         2476 :   if (p->overridden && !p->overridden->is_generic)
   17222              :     {
   17223            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   17224              :                  " the same name", name, &p->where);
   17225            1 :       return false;
   17226              :     }
   17227              : 
   17228              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   17229              :      all must have the same attributes here.  */
   17230         2475 :   first_target = p->u.generic->specific->u.specific;
   17231         2475 :   gcc_assert (first_target);
   17232         2475 :   p->subroutine = first_target->n.sym->attr.subroutine;
   17233         2475 :   p->function = first_target->n.sym->attr.function;
   17234              : 
   17235         2475 :   return true;
   17236              : }
   17237              : 
   17238              : 
   17239              : /* Resolve a GENERIC procedure binding for a derived type.  */
   17240              : 
   17241              : static bool
   17242         1297 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   17243              : {
   17244         1297 :   gfc_symbol* super_type;
   17245              : 
   17246              :   /* Find the overridden binding if any.  */
   17247         1297 :   st->n.tb->overridden = NULL;
   17248         1297 :   super_type = gfc_get_derived_super_type (derived);
   17249         1297 :   if (super_type)
   17250              :     {
   17251           40 :       gfc_symtree* overridden;
   17252           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   17253              :                                             true, NULL);
   17254              : 
   17255           40 :       if (overridden && overridden->n.tb)
   17256           21 :         st->n.tb->overridden = overridden->n.tb;
   17257              :     }
   17258              : 
   17259              :   /* Resolve using worker function.  */
   17260         1297 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   17261              : }
   17262              : 
   17263              : 
   17264              : /* Retrieve the target-procedure of an operator binding and do some checks in
   17265              :    common for intrinsic and user-defined type-bound operators.  */
   17266              : 
   17267              : static gfc_symbol*
   17268         1268 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   17269              : {
   17270         1268 :   gfc_symbol* target_proc;
   17271              : 
   17272         1268 :   gcc_assert (target->specific && !target->specific->is_generic);
   17273         1268 :   target_proc = target->specific->u.specific->n.sym;
   17274         1268 :   gcc_assert (target_proc);
   17275              : 
   17276              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   17277         1268 :   if (target->specific->nopass)
   17278              :     {
   17279            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   17280            2 :       return NULL;
   17281              :     }
   17282              : 
   17283              :   return target_proc;
   17284              : }
   17285              : 
   17286              : 
   17287              : /* Resolve a type-bound intrinsic operator.  */
   17288              : 
   17289              : static bool
   17290         1083 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   17291              :                                 gfc_typebound_proc* p)
   17292              : {
   17293         1083 :   gfc_symbol* super_type;
   17294         1083 :   gfc_tbp_generic* target;
   17295              : 
   17296              :   /* If there's already an error here, do nothing (but don't fail again).  */
   17297         1083 :   if (p->error)
   17298              :     return true;
   17299              : 
   17300              :   /* Operators should always be GENERIC bindings.  */
   17301         1083 :   gcc_assert (p->is_generic);
   17302              : 
   17303              :   /* Look for an overridden binding.  */
   17304         1083 :   super_type = gfc_get_derived_super_type (derived);
   17305         1083 :   if (super_type && super_type->f2k_derived)
   17306            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   17307              :                                                      op, true, NULL);
   17308              :   else
   17309         1082 :     p->overridden = NULL;
   17310              : 
   17311              :   /* Resolve general GENERIC properties using worker function.  */
   17312         1083 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   17313            1 :     goto error;
   17314              : 
   17315              :   /* Check the targets to be procedures of correct interface.  */
   17316         2211 :   for (target = p->u.generic; target; target = target->next)
   17317              :     {
   17318         1154 :       gfc_symbol* target_proc;
   17319              : 
   17320         1154 :       target_proc = get_checked_tb_operator_target (target, p->where);
   17321         1154 :       if (!target_proc)
   17322            1 :         goto error;
   17323              : 
   17324         1153 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   17325            3 :         goto error;
   17326              : 
   17327              :       /* Add target to non-typebound operator list.  */
   17328         1150 :       if (!target->specific->deferred && !derived->attr.use_assoc
   17329          409 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   17330              :         {
   17331          407 :           gfc_interface *head, *intr;
   17332              : 
   17333              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   17334              :              mechanism for handling module procedures winds up resolving
   17335              :              operator interfaces twice and would otherwise cause an error.
   17336              :              Likewise, new instances of PDTs can cause the operator inter-
   17337              :              faces to be resolved multiple times.  */
   17338          479 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   17339           91 :             if (intr->sym == target_proc
   17340           21 :                 && (target_proc->attr.used_in_submodule
   17341            4 :                     || derived->attr.pdt_type
   17342            2 :                     || derived->attr.pdt_template))
   17343              :               return true;
   17344              : 
   17345          388 :           if (!gfc_check_new_interface (derived->ns->op[op],
   17346              :                                         target_proc, p->where))
   17347              :             return false;
   17348          386 :           head = derived->ns->op[op];
   17349          386 :           intr = gfc_get_interface ();
   17350          386 :           intr->sym = target_proc;
   17351          386 :           intr->where = p->where;
   17352          386 :           intr->next = head;
   17353          386 :           derived->ns->op[op] = intr;
   17354              :         }
   17355              :     }
   17356              : 
   17357              :   return true;
   17358              : 
   17359            5 : error:
   17360            5 :   p->error = 1;
   17361            5 :   return false;
   17362              : }
   17363              : 
   17364              : 
   17365              : /* Resolve a type-bound user operator (tree-walker callback).  */
   17366              : 
   17367              : static gfc_symbol* resolve_bindings_derived;
   17368              : static bool resolve_bindings_result;
   17369              : 
   17370              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   17371              : 
   17372              : static void
   17373          113 : resolve_typebound_user_op (gfc_symtree* stree)
   17374              : {
   17375          113 :   gfc_symbol* super_type;
   17376          113 :   gfc_tbp_generic* target;
   17377              : 
   17378          113 :   gcc_assert (stree && stree->n.tb);
   17379              : 
   17380          113 :   if (stree->n.tb->error)
   17381              :     return;
   17382              : 
   17383              :   /* Operators should always be GENERIC bindings.  */
   17384          113 :   gcc_assert (stree->n.tb->is_generic);
   17385              : 
   17386              :   /* Find overridden procedure, if any.  */
   17387          113 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17388          113 :   if (super_type && super_type->f2k_derived)
   17389              :     {
   17390           18 :       gfc_symtree* overridden;
   17391           18 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   17392              :                                                stree->name, true, NULL);
   17393              : 
   17394           18 :       if (overridden && overridden->n.tb)
   17395            0 :         stree->n.tb->overridden = overridden->n.tb;
   17396              :     }
   17397              :   else
   17398           95 :     stree->n.tb->overridden = NULL;
   17399              : 
   17400              :   /* Resolve basically using worker function.  */
   17401          113 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   17402            0 :     goto error;
   17403              : 
   17404              :   /* Check the targets to be functions of correct interface.  */
   17405          224 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   17406              :     {
   17407          114 :       gfc_symbol* target_proc;
   17408              : 
   17409          114 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   17410          114 :       if (!target_proc)
   17411            1 :         goto error;
   17412              : 
   17413          113 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   17414            2 :         goto error;
   17415              :     }
   17416              : 
   17417              :   return;
   17418              : 
   17419            3 : error:
   17420            3 :   resolve_bindings_result = false;
   17421            3 :   stree->n.tb->error = 1;
   17422              : }
   17423              : 
   17424              : 
   17425              : /* Resolve the type-bound procedures for a derived type.  */
   17426              : 
   17427              : static void
   17428        10507 : resolve_typebound_procedure (gfc_symtree* stree)
   17429              : {
   17430        10507 :   gfc_symbol* proc;
   17431        10507 :   locus where;
   17432        10507 :   gfc_symbol* me_arg;
   17433        10507 :   gfc_symbol* super_type;
   17434        10507 :   gfc_component* comp;
   17435              : 
   17436        10507 :   gcc_assert (stree);
   17437              : 
   17438              :   /* Undefined specific symbol from GENERIC target definition.  */
   17439        10507 :   if (!stree->n.tb)
   17440        10425 :     return;
   17441              : 
   17442        10501 :   if (stree->n.tb->error)
   17443              :     return;
   17444              : 
   17445              :   /* If this is a GENERIC binding, use that routine.  */
   17446        10485 :   if (stree->n.tb->is_generic)
   17447              :     {
   17448         1297 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   17449           17 :         goto error;
   17450              :       return;
   17451              :     }
   17452              : 
   17453              :   /* Get the target-procedure to check it.  */
   17454         9188 :   gcc_assert (!stree->n.tb->is_generic);
   17455         9188 :   gcc_assert (stree->n.tb->u.specific);
   17456         9188 :   proc = stree->n.tb->u.specific->n.sym;
   17457         9188 :   where = stree->n.tb->where;
   17458              : 
   17459              :   /* Default access should already be resolved from the parser.  */
   17460         9188 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   17461              : 
   17462         9188 :   if (stree->n.tb->deferred)
   17463              :     {
   17464          676 :       if (!check_proc_interface (proc, &where))
   17465            5 :         goto error;
   17466              :     }
   17467              :   else
   17468              :     {
   17469              :       /* If proc has not been resolved at this point, proc->name may
   17470              :          actually be a USE associated entity. See PR fortran/89647. */
   17471         8512 :       if (!proc->resolve_symbol_called
   17472         5938 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   17473              :         {
   17474           11 :           gfc_symbol *tmp;
   17475           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   17476           11 :           if (tmp && tmp->attr.use_assoc)
   17477              :             {
   17478            1 :               proc->module = tmp->module;
   17479            1 :               proc->attr.proc = tmp->attr.proc;
   17480            1 :               proc->attr.function = tmp->attr.function;
   17481            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   17482            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   17483            1 :               proc->ts = tmp->ts;
   17484            1 :               proc->result = tmp->result;
   17485              :             }
   17486              :         }
   17487              : 
   17488              :       /* Check for F08:C465.  */
   17489         8512 :       if ((!proc->attr.subroutine && !proc->attr.function)
   17490         8502 :           || (proc->attr.proc != PROC_MODULE
   17491           70 :               && proc->attr.if_source != IFSRC_IFBODY
   17492            7 :               && !proc->attr.module_procedure)
   17493         8501 :           || proc->attr.abstract)
   17494              :         {
   17495           12 :           gfc_error ("%qs must be a module procedure or an external "
   17496              :                      "procedure with an explicit interface at %L",
   17497              :                      proc->name, &where);
   17498           12 :           goto error;
   17499              :         }
   17500              :     }
   17501              : 
   17502         9171 :   stree->n.tb->subroutine = proc->attr.subroutine;
   17503         9171 :   stree->n.tb->function = proc->attr.function;
   17504              : 
   17505              :   /* Find the super-type of the current derived type.  We could do this once and
   17506              :      store in a global if speed is needed, but as long as not I believe this is
   17507              :      more readable and clearer.  */
   17508         9171 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17509              : 
   17510              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   17511              :      from a .mod file.  */
   17512         9171 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   17513              :     {
   17514         2922 :       gfc_formal_arglist *dummy_args;
   17515              : 
   17516         2922 :       dummy_args = gfc_sym_get_dummy_args (proc);
   17517         2922 :       if (stree->n.tb->pass_arg)
   17518              :         {
   17519          468 :           gfc_formal_arglist *i;
   17520              : 
   17521              :           /* If an explicit passing argument name is given, walk the arg-list
   17522              :              and look for it.  */
   17523              : 
   17524          468 :           me_arg = NULL;
   17525          468 :           stree->n.tb->pass_arg_num = 1;
   17526          601 :           for (i = dummy_args; i; i = i->next)
   17527              :             {
   17528          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   17529              :                 {
   17530              :                   me_arg = i->sym;
   17531              :                   break;
   17532              :                 }
   17533          133 :               ++stree->n.tb->pass_arg_num;
   17534              :             }
   17535              : 
   17536          468 :           if (!me_arg)
   17537              :             {
   17538            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   17539              :                          " argument %qs",
   17540              :                          proc->name, stree->n.tb->pass_arg, &where,
   17541              :                          stree->n.tb->pass_arg);
   17542            2 :               goto error;
   17543              :             }
   17544              :         }
   17545              :       else
   17546              :         {
   17547              :           /* Otherwise, take the first one; there should in fact be at least
   17548              :              one.  */
   17549         2454 :           stree->n.tb->pass_arg_num = 1;
   17550         2454 :           if (!dummy_args)
   17551              :             {
   17552            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   17553              :                          " least one argument", proc->name, &where);
   17554            2 :               goto error;
   17555              :             }
   17556         2452 :           me_arg = dummy_args->sym;
   17557              :         }
   17558              : 
   17559              :       /* Now check that the argument-type matches and the passed-object
   17560              :          dummy argument is generally fine.  */
   17561              : 
   17562         2452 :       gcc_assert (me_arg);
   17563              : 
   17564         2918 :       if (me_arg->ts.type != BT_CLASS)
   17565              :         {
   17566            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17567              :                      " at %L", proc->name, &where);
   17568            5 :           goto error;
   17569              :         }
   17570              : 
   17571              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   17572         2913 :       if (!resolve_bindings_derived->attr.pdt_template
   17573         2892 :           && !(containing_dt && containing_dt->attr.pdt_type
   17574           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   17575         2872 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   17576              :         {
   17577            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17578              :                      "the derived-type %qs", me_arg->name, proc->name,
   17579              :                      me_arg->name, &where, resolve_bindings_derived->name);
   17580            0 :           goto error;
   17581              :         }
   17582              : 
   17583         2913 :       if (resolve_bindings_derived->attr.pdt_template
   17584         2934 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   17585           21 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   17586              :         {
   17587            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17588              :                      "the parametric derived-type %qs", me_arg->name,
   17589              :                      proc->name, me_arg->name, &where,
   17590              :                      resolve_bindings_derived->name);
   17591            0 :           goto error;
   17592              :         }
   17593              : 
   17594         2913 :       if (((resolve_bindings_derived->attr.pdt_template
   17595           21 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   17596           21 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   17597         2892 :            || resolve_bindings_derived->attr.pdt_type)
   17598           81 :           && (me_arg->param_list != NULL)
   17599         2994 :           && (gfc_spec_list_type (me_arg->param_list,
   17600           81 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   17601              :                                   != SPEC_ASSUMED))
   17602              :         {
   17603              : 
   17604              :           /* Add a check to verify if there are any LEN parameters in the
   17605              :              first place.  If there are LEN parameters, throw this error.
   17606              :              If there are only KIND parameters, then don't trigger
   17607              :              this error.  */
   17608            6 :           gfc_component *c;
   17609            6 :           bool seen_len_param = false;
   17610            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   17611              : 
   17612            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   17613              :             {
   17614            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   17615              :                                      me_arg_param->name, true, true, NULL);
   17616              : 
   17617            6 :               gcc_assert (c != NULL);
   17618              : 
   17619            6 :               if (c->attr.pdt_kind)
   17620            0 :                 continue;
   17621              : 
   17622              :               /* Getting here implies that there is a pdt_len parameter
   17623              :                  in the list.  */
   17624              :               seen_len_param = true;
   17625              :               break;
   17626              :             }
   17627              : 
   17628            6 :             if (seen_len_param)
   17629              :               {
   17630            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   17631              :                            "argument %qs of %qs at %L must be ASSUMED.",
   17632              :                            me_arg->name, proc->name, &where);
   17633            6 :                 goto error;
   17634              :               }
   17635              :         }
   17636              : 
   17637         2907 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   17638         2907 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   17639              :         {
   17640            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   17641              :                      " scalar", proc->name, &where);
   17642            1 :           goto error;
   17643              :         }
   17644         2906 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17645              :         {
   17646            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17647              :                      " be ALLOCATABLE", proc->name, &where);
   17648            2 :           goto error;
   17649              :         }
   17650         2904 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17651              :         {
   17652            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17653              :                      " be POINTER", proc->name, &where);
   17654            2 :           goto error;
   17655              :         }
   17656              :     }
   17657              : 
   17658              :   /* If we are extending some type, check that we don't override a procedure
   17659              :      flagged NON_OVERRIDABLE.  */
   17660         9151 :   stree->n.tb->overridden = NULL;
   17661         9151 :   if (super_type)
   17662              :     {
   17663         1537 :       gfc_symtree* overridden;
   17664         1537 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   17665              :                                             stree->name, true, NULL);
   17666              : 
   17667         1537 :       if (overridden)
   17668              :         {
   17669         1242 :           if (overridden->n.tb)
   17670         1242 :             stree->n.tb->overridden = overridden->n.tb;
   17671              : 
   17672         1242 :           if (!gfc_check_typebound_override (stree, overridden))
   17673           26 :             goto error;
   17674              :         }
   17675              :     }
   17676              : 
   17677              :   /* See if there's a name collision with a component directly in this type.  */
   17678        21921 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   17679        12797 :     if (!strcmp (comp->name, stree->name))
   17680              :       {
   17681            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   17682              :                    " %qs",
   17683              :                    stree->name, &where, resolve_bindings_derived->name);
   17684            1 :         goto error;
   17685              :       }
   17686              : 
   17687              :   /* Try to find a name collision with an inherited component.  */
   17688         9124 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   17689              :                                         NULL))
   17690              :     {
   17691            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   17692              :                  " component of %qs",
   17693              :                  stree->name, &where, resolve_bindings_derived->name);
   17694            1 :       goto error;
   17695              :     }
   17696              : 
   17697         9123 :   stree->n.tb->error = 0;
   17698         9123 :   return;
   17699              : 
   17700           82 : error:
   17701           82 :   resolve_bindings_result = false;
   17702           82 :   stree->n.tb->error = 1;
   17703              : }
   17704              : 
   17705              : 
   17706              : static bool
   17707        93089 : resolve_typebound_procedures (gfc_symbol* derived)
   17708              : {
   17709        93089 :   int op;
   17710        93089 :   gfc_symbol* super_type;
   17711              : 
   17712              :   /* Resolve the super-type first so that inherited bindings (including
   17713              :      user operators) are fully resolved before we look them up via
   17714              :      gfc_find_typebound_user_op.  This must happen even when 'derived'
   17715              :      has no direct type-bound bindings of its own.  */
   17716        93089 :   super_type = gfc_get_derived_super_type (derived);
   17717        93089 :   if (super_type)
   17718        14945 :     resolve_symbol (super_type);
   17719              : 
   17720        93089 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17721              :     return true;
   17722              : 
   17723         5044 :   resolve_bindings_derived = derived;
   17724         5044 :   resolve_bindings_result = true;
   17725              : 
   17726         5044 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17727         5044 :   if (derived->f2k_derived->tb_sym_root)
   17728         5044 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17729              :                           &resolve_typebound_procedure);
   17730              : 
   17731         5044 :   if (derived->f2k_derived->tb_uop_root)
   17732           91 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17733              :                           &resolve_typebound_user_op);
   17734         5044 :   containing_dt = NULL;
   17735              : 
   17736       146276 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17737              :     {
   17738       141232 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17739       141232 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17740              :                                                 (gfc_intrinsic_op)op, p))
   17741            7 :         resolve_bindings_result = false;
   17742              :     }
   17743              : 
   17744         5044 :   return resolve_bindings_result;
   17745              : }
   17746              : 
   17747              : 
   17748              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17749              :    to give all identical derived types the same backend_decl.  */
   17750              : static void
   17751       192290 : add_dt_to_dt_list (gfc_symbol *derived)
   17752              : {
   17753       192290 :   if (!derived->dt_next)
   17754              :     {
   17755        90201 :       if (gfc_derived_types)
   17756              :         {
   17757        73831 :           derived->dt_next = gfc_derived_types->dt_next;
   17758        73831 :           gfc_derived_types->dt_next = derived;
   17759              :         }
   17760              :       else
   17761              :         {
   17762        16370 :           derived->dt_next = derived;
   17763              :         }
   17764        90201 :       gfc_derived_types = derived;
   17765              :     }
   17766       192290 : }
   17767              : 
   17768              : 
   17769              : /* Ensure that a derived-type is really not abstract, meaning that every
   17770              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17771              : 
   17772              : static bool
   17773         7212 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17774              : {
   17775         7212 :   if (!st)
   17776              :     return true;
   17777              : 
   17778         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17779              :     return false;
   17780         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17781              :     return false;
   17782              : 
   17783         2771 :   if (st->n.tb && st->n.tb->deferred)
   17784              :     {
   17785         2019 :       gfc_symtree* overriding;
   17786         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17787         2019 :       if (!overriding)
   17788              :         return false;
   17789         2018 :       gcc_assert (overriding->n.tb);
   17790         2018 :       if (overriding->n.tb->deferred)
   17791              :         {
   17792            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17793              :                      " %qs is DEFERRED and not overridden",
   17794              :                      sub->name, &sub->declared_at, st->name);
   17795            5 :           return false;
   17796              :         }
   17797              :     }
   17798              : 
   17799              :   return true;
   17800              : }
   17801              : 
   17802              : static bool
   17803         1520 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17804              : {
   17805              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17806              :      and for each ancestor-type, check all bindings.  If any of them is
   17807              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17808              :      binding is not DEFERRED.
   17809              :      This is not the most efficient way to do this, but it should be ok and is
   17810              :      clearer than something sophisticated.  */
   17811              : 
   17812         1669 :   gcc_assert (ancestor && !sub->attr.abstract);
   17813              : 
   17814         1669 :   if (!ancestor->attr.abstract)
   17815              :     return true;
   17816              : 
   17817              :   /* Walk bindings of this ancestor.  */
   17818         1668 :   if (ancestor->f2k_derived)
   17819              :     {
   17820         1668 :       bool t;
   17821         1668 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17822         1668 :       if (!t)
   17823              :         return false;
   17824              :     }
   17825              : 
   17826              :   /* Find next ancestor type and recurse on it.  */
   17827         1662 :   ancestor = gfc_get_derived_super_type (ancestor);
   17828         1662 :   if (ancestor)
   17829              :     return ensure_not_abstract (sub, ancestor);
   17830              : 
   17831              :   return true;
   17832              : }
   17833              : 
   17834              : 
   17835              : /* This check for typebound defined assignments is done recursively
   17836              :    since the order in which derived types are resolved is not always in
   17837              :    order of the declarations.  */
   17838              : 
   17839              : static void
   17840       198725 : check_defined_assignments (gfc_symbol *derived)
   17841              : {
   17842       198725 :   gfc_component *c;
   17843              : 
   17844       674984 :   for (c = derived->components; c; c = c->next)
   17845              :     {
   17846       478132 :       if (!gfc_bt_struct (c->ts.type)
   17847       115236 :           || c->attr.pointer
   17848        24079 :           || c->attr.proc_pointer_comp
   17849        24079 :           || c->attr.class_pointer
   17850        24073 :           || c->attr.proc_pointer)
   17851       454593 :         continue;
   17852              : 
   17853        23539 :       if (c->ts.u.derived->attr.defined_assign_comp
   17854        23304 :           || (c->ts.u.derived->f2k_derived
   17855        22722 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17856              :         {
   17857         1849 :           derived->attr.defined_assign_comp = 1;
   17858         1849 :           return;
   17859              :         }
   17860              : 
   17861        21690 :       if (c->attr.allocatable)
   17862         7560 :         continue;
   17863              : 
   17864        14130 :       check_defined_assignments (c->ts.u.derived);
   17865        14130 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17866              :         {
   17867           24 :           derived->attr.defined_assign_comp = 1;
   17868           24 :           return;
   17869              :         }
   17870              :     }
   17871              : }
   17872              : 
   17873              : 
   17874              : /* Resolve a single component of a derived type or structure.  */
   17875              : 
   17876              : static bool
   17877       453361 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17878              : {
   17879       453361 :   gfc_symbol *super_type;
   17880       453361 :   symbol_attribute *attr;
   17881              : 
   17882       453361 :   if (c->attr.artificial)
   17883              :     return true;
   17884              : 
   17885              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17886              :      such as block data because the procedure pointers will cause ICEs
   17887              :      and vtables are not needed in these contexts.  */
   17888       310641 :   if (sym->attr.vtype && sym->attr.use_assoc
   17889        52358 :       && sym->ns->proc_name == NULL)
   17890              :     return true;
   17891              : 
   17892              :   /* F2008, C442.  */
   17893       310632 :   if ((!sym->attr.is_class || c != sym->components)
   17894       310632 :       && c->attr.codimension
   17895          230 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17896              :     {
   17897            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17898              :                  "deferred shape", c->name, &c->loc);
   17899            4 :       return false;
   17900              :     }
   17901              : 
   17902              :   /* F2008, C443.  */
   17903       310628 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17904           85 :       && c->ts.u.derived->ts.is_iso_c)
   17905              :     {
   17906            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17907              :                  "shall not be a coarray", c->name, &c->loc);
   17908            1 :       return false;
   17909              :     }
   17910              : 
   17911              :   /* F2008, C444.  */
   17912       310627 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17913           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17914           26 :           || c->attr.allocatable))
   17915              :     {
   17916            3 :       gfc_error ("Component %qs at %L with coarray component "
   17917              :                  "shall be a nonpointer, nonallocatable scalar",
   17918              :                  c->name, &c->loc);
   17919            3 :       return false;
   17920              :     }
   17921              : 
   17922              :   /* F2008, C448.  */
   17923       310624 :   if (c->ts.type == BT_CLASS)
   17924              :     {
   17925         7760 :       if (c->attr.class_ok && CLASS_DATA (c))
   17926              :         {
   17927         7752 :           attr = &(CLASS_DATA (c)->attr);
   17928              : 
   17929              :           /* Fix up contiguous attribute.  */
   17930         7752 :           if (c->attr.contiguous)
   17931           11 :             attr->contiguous = 1;
   17932              :         }
   17933              :       else
   17934              :         attr = NULL;
   17935              :     }
   17936              :   else
   17937       302864 :     attr = &c->attr;
   17938              : 
   17939       310616 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17940              :     {
   17941            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17942              :                  "is not an array pointer", c->name, &c->loc);
   17943            5 :       return false;
   17944              :     }
   17945              : 
   17946              :   /* F2003, 15.2.1 - length has to be one.  */
   17947        41969 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17948       310644 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17949           25 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17950           25 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17951              :     {
   17952            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17953              :                  c->name, &c->loc);
   17954            1 :       return false;
   17955              :     }
   17956              : 
   17957        57869 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17958          583 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17959       310632 :       && !(gfc_get_derived_super_type (sym)
   17960            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17961            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17962              :     {
   17963           14 :       gfc_actual_arglist *type_spec_list;
   17964           14 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17965              :                                 &type_spec_list)
   17966              :           != MATCH_YES)
   17967            0 :         return false;
   17968           14 :       gfc_free_actual_arglist (c->param_list);
   17969           14 :       c->param_list = type_spec_list;
   17970           14 :       if (!sym->attr.pdt_type)
   17971           14 :         sym->attr.pdt_comp = 1;
   17972              :     }
   17973       310604 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17974           72 :     sym->attr.pdt_comp = 1;
   17975              : 
   17976       310618 :   if (c->attr.proc_pointer && c->ts.interface)
   17977              :     {
   17978        15570 :       gfc_symbol *ifc = c->ts.interface;
   17979              : 
   17980        15570 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17981              :         {
   17982            6 :           c->tb->error = 1;
   17983            6 :           return false;
   17984              :         }
   17985              : 
   17986        15564 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17987              :         {
   17988              :           /* Resolve interface and copy attributes.  */
   17989        15513 :           if (ifc->formal && !ifc->formal_ns)
   17990         2689 :             resolve_symbol (ifc);
   17991        15513 :           if (ifc->attr.intrinsic)
   17992            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17993              : 
   17994        15513 :           if (ifc->result)
   17995              :             {
   17996         8289 :               c->ts = ifc->result->ts;
   17997         8289 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17998         8289 :               c->attr.pointer = ifc->result->attr.pointer;
   17999         8289 :               c->attr.dimension = ifc->result->attr.dimension;
   18000         8289 :               c->as = gfc_copy_array_spec (ifc->result->as);
   18001         8289 :               c->attr.class_ok = ifc->result->attr.class_ok;
   18002              :             }
   18003              :           else
   18004              :             {
   18005         7224 :               c->ts = ifc->ts;
   18006         7224 :               c->attr.allocatable = ifc->attr.allocatable;
   18007         7224 :               c->attr.pointer = ifc->attr.pointer;
   18008         7224 :               c->attr.dimension = ifc->attr.dimension;
   18009         7224 :               c->as = gfc_copy_array_spec (ifc->as);
   18010         7224 :               c->attr.class_ok = ifc->attr.class_ok;
   18011              :             }
   18012        15513 :           c->ts.interface = ifc;
   18013        15513 :           c->attr.function = ifc->attr.function;
   18014        15513 :           c->attr.subroutine = ifc->attr.subroutine;
   18015              : 
   18016        15513 :           c->attr.pure = ifc->attr.pure;
   18017        15513 :           c->attr.elemental = ifc->attr.elemental;
   18018        15513 :           c->attr.recursive = ifc->attr.recursive;
   18019        15513 :           c->attr.always_explicit = ifc->attr.always_explicit;
   18020        15513 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   18021              :           /* Copy char length.  */
   18022        15513 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   18023              :             {
   18024          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   18025          454 :               if (cl->length && !cl->resolved
   18026          601 :                   && !gfc_resolve_expr (cl->length))
   18027              :                 {
   18028            0 :                   c->tb->error = 1;
   18029            0 :                   return false;
   18030              :                 }
   18031          491 :               c->ts.u.cl = cl;
   18032              :             }
   18033              :         }
   18034              :     }
   18035       295048 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   18036              :     {
   18037              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   18038              :          interface must be a subroutine.  */
   18039          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   18040              :     }
   18041              : 
   18042              :   /* Procedure pointer components: Check PASS arg.  */
   18043       310612 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   18044          596 :       && !sym->attr.vtype)
   18045              :     {
   18046           95 :       gfc_symbol* me_arg;
   18047              : 
   18048           95 :       if (c->tb->pass_arg)
   18049              :         {
   18050           20 :           gfc_formal_arglist* i;
   18051              : 
   18052              :           /* If an explicit passing argument name is given, walk the arg-list
   18053              :             and look for it.  */
   18054              : 
   18055           20 :           me_arg = NULL;
   18056           20 :           c->tb->pass_arg_num = 1;
   18057           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   18058              :             {
   18059           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   18060              :                 {
   18061              :                   me_arg = i->sym;
   18062              :                   break;
   18063              :                 }
   18064           14 :               c->tb->pass_arg_num++;
   18065              :             }
   18066              : 
   18067           20 :           if (!me_arg)
   18068              :             {
   18069            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   18070              :                          "at %L has no argument %qs", c->name,
   18071              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   18072            1 :               c->tb->error = 1;
   18073            1 :               return false;
   18074              :             }
   18075              :         }
   18076              :       else
   18077              :         {
   18078              :           /* Otherwise, take the first one; there should in fact be at least
   18079              :             one.  */
   18080           75 :           c->tb->pass_arg_num = 1;
   18081           75 :           if (!c->ts.interface->formal)
   18082              :             {
   18083            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   18084              :                          "must have at least one argument",
   18085              :                          c->name, &c->loc);
   18086            3 :               c->tb->error = 1;
   18087            3 :               return false;
   18088              :             }
   18089           72 :           me_arg = c->ts.interface->formal->sym;
   18090              :         }
   18091              : 
   18092              :       /* Now check that the argument-type matches.  */
   18093           72 :       gcc_assert (me_arg);
   18094           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   18095           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   18096           90 :           || (me_arg->ts.type == BT_CLASS
   18097           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   18098              :         {
   18099            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   18100              :                      " the derived type %qs", me_arg->name, c->name,
   18101              :                      me_arg->name, &c->loc, sym->name);
   18102            1 :           c->tb->error = 1;
   18103            1 :           return false;
   18104              :         }
   18105              : 
   18106              :       /* Check for F03:C453.  */
   18107           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   18108              :         {
   18109            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   18110              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   18111              :                      &c->loc);
   18112            1 :           c->tb->error = 1;
   18113            1 :           return false;
   18114              :         }
   18115              : 
   18116           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   18117              :         {
   18118            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   18119              :                      "may not have the POINTER attribute", me_arg->name,
   18120              :                      c->name, me_arg->name, &c->loc);
   18121            1 :           c->tb->error = 1;
   18122            1 :           return false;
   18123              :         }
   18124              : 
   18125           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   18126              :         {
   18127            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   18128              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   18129              :                      me_arg->name, &c->loc);
   18130            1 :           c->tb->error = 1;
   18131            1 :           return false;
   18132              :         }
   18133              : 
   18134           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   18135              :         {
   18136            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   18137              :                      " at %L", c->name, &c->loc);
   18138            2 :           return false;
   18139              :         }
   18140              : 
   18141              :     }
   18142              : 
   18143              :   /* Check type-spec if this is not the parent-type component.  */
   18144       310602 :   if (((sym->attr.is_class
   18145        14203 :         && (!sym->components->ts.u.derived->attr.extension
   18146         2451 :             || c != CLASS_DATA (sym->components)))
   18147       297782 :        || (!sym->attr.is_class
   18148       296399 :            && (!sym->attr.extension || c != sym->components)))
   18149       301411 :       && !sym->attr.vtype
   18150       493388 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   18151              :     return false;
   18152              : 
   18153       310601 :   super_type = gfc_get_derived_super_type (sym);
   18154              : 
   18155              :   /* If this type is an extension, set the accessibility of the parent
   18156              :      component.  */
   18157       310601 :   if (super_type
   18158        30624 :       && ((sym->attr.is_class
   18159        14203 :            && c == CLASS_DATA (sym->components))
   18160        20900 :           || (!sym->attr.is_class && c == sym->components))
   18161        17532 :       && strcmp (super_type->name, c->name) == 0)
   18162         7340 :     c->attr.access = super_type->attr.access;
   18163              : 
   18164              :   /* If this type is an extension, see if this component has the same name
   18165              :      as an inherited type-bound procedure.  */
   18166        30624 :   if (super_type && !sym->attr.is_class
   18167        16421 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   18168              :     {
   18169            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   18170              :                  " inherited type-bound procedure",
   18171              :                  c->name, sym->name, &c->loc);
   18172            1 :       return false;
   18173              :     }
   18174              : 
   18175       310600 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   18176        10934 :       && !c->ts.deferred)
   18177              :     {
   18178         8615 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   18179          711 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   18180              : 
   18181         8615 :       if (c->ts.u.cl->length == NULL
   18182         8609 :           || !resolve_charlen(c->ts.u.cl)
   18183        17223 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   18184              :         {
   18185            9 :           gfc_error ("Character length of component %qs needs to "
   18186              :                      "be a constant specification expression at %L",
   18187              :                      c->name,
   18188            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   18189            9 :           return false;
   18190              :         }
   18191              : 
   18192         8606 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   18193              :         {
   18194            2 :          if (!c->ts.u.cl->length->error)
   18195              :            {
   18196            1 :              gfc_error ("Character length expression of component %qs at %L "
   18197              :                         "must be of INTEGER type, found %s",
   18198            1 :                         c->name, &c->ts.u.cl->length->where,
   18199              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   18200            1 :              c->ts.u.cl->length->error = 1;
   18201              :            }
   18202              :          return false;
   18203              :        }
   18204              :     }
   18205              : 
   18206       310589 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   18207         2355 :       && !c->attr.pointer && !c->attr.allocatable)
   18208              :     {
   18209            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   18210              :                  "length must be a POINTER or ALLOCATABLE",
   18211              :                  c->name, sym->name, &c->loc);
   18212            1 :       return false;
   18213              :     }
   18214              : 
   18215              :   /* Add the hidden deferred length field.  */
   18216       310588 :   if (c->ts.type == BT_CHARACTER
   18217        11434 :       && (c->ts.deferred || c->attr.pdt_string)
   18218         2728 :       && !c->attr.function
   18219         2692 :       && !sym->attr.is_class)
   18220              :     {
   18221         2545 :       char name[GFC_MAX_SYMBOL_LEN+9];
   18222         2545 :       gfc_component *strlen;
   18223         2545 :       sprintf (name, "_%s_length", c->name);
   18224         2545 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   18225         2545 :       if (strlen == NULL)
   18226              :         {
   18227          599 :           if (!gfc_add_component (sym, name, &strlen))
   18228            0 :             return false;
   18229          599 :           strlen->ts.type = BT_INTEGER;
   18230          599 :           strlen->ts.kind = gfc_charlen_int_kind;
   18231          599 :           strlen->attr.access = ACCESS_PRIVATE;
   18232          599 :           strlen->attr.artificial = 1;
   18233              :         }
   18234              :     }
   18235              : 
   18236       310588 :   if (c->ts.type == BT_DERIVED
   18237        58079 :       && sym->component_access != ACCESS_PRIVATE
   18238        57059 :       && gfc_check_symbol_access (sym)
   18239       112082 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   18240        55982 :       && !c->ts.u.derived->attr.use_assoc
   18241        30481 :       && !gfc_check_symbol_access (c->ts.u.derived)
   18242       310785 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   18243              :                           "PRIVATE type and cannot be a component of "
   18244              :                           "%qs, which is PUBLIC at %L", c->name,
   18245              :                           sym->name, &sym->declared_at))
   18246              :     return false;
   18247              : 
   18248       310587 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   18249              :     {
   18250            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   18251              :                  "type %s", c->name, &c->loc, sym->name);
   18252            2 :       return false;
   18253              :     }
   18254              : 
   18255       310585 :   if (sym->attr.sequence)
   18256              :     {
   18257         2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   18258              :         {
   18259            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   18260              :                      "not have the SEQUENCE attribute",
   18261              :                      c->ts.u.derived->name, &sym->declared_at);
   18262            0 :           return false;
   18263              :         }
   18264              :     }
   18265              : 
   18266       310585 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   18267            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   18268       310585 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   18269         8094 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   18270            0 :     CLASS_DATA (c)->ts.u.derived
   18271            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   18272              : 
   18273              :   /* If an allocatable component derived type is of the same type as
   18274              :      the enclosing derived type, we need a vtable generating so that
   18275              :      the __deallocate procedure is created.  */
   18276       310585 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   18277        66183 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   18278          561 :     gfc_find_vtab (&c->ts);
   18279              : 
   18280              :   /* Ensure that all the derived type components are put on the
   18281              :      derived type list; even in formal namespaces, where derived type
   18282              :      pointer components might not have been declared.  */
   18283       310585 :   if (c->ts.type == BT_DERIVED
   18284        58078 :       && c->ts.u.derived
   18285        58078 :       && c->ts.u.derived->components
   18286        54748 :       && c->attr.pointer
   18287        36602 :       && sym != c->ts.u.derived)
   18288         4573 :     add_dt_to_dt_list (c->ts.u.derived);
   18289              : 
   18290       310585 :   if (c->as && c->as->type != AS_DEFERRED
   18291         9762 :       && (c->attr.pointer || c->attr.allocatable))
   18292              :     return false;
   18293              : 
   18294       310571 :   if (!gfc_resolve_array_spec (c->as,
   18295       310571 :                                !(c->attr.pointer || c->attr.proc_pointer
   18296       254289 :                                  || c->attr.allocatable)))
   18297              :     return false;
   18298              : 
   18299       122867 :   if (c->initializer && !sym->attr.vtype
   18300        41985 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   18301        35564 :       && !(sym->attr.pdt_template && c->ts.type == BT_REAL && c->ts.kind == 0)
   18302       346064 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   18303              :     return false;
   18304              : 
   18305              :   return true;
   18306              : }
   18307              : 
   18308              : 
   18309              : /* Be nice about the locus for a structure expression - show the locus of the
   18310              :    first non-null sub-expression if we can.  */
   18311              : 
   18312              : static locus *
   18313            4 : cons_where (gfc_expr *struct_expr)
   18314              : {
   18315            4 :   gfc_constructor *cons;
   18316              : 
   18317            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   18318              : 
   18319            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   18320           12 :   for (; cons; cons = gfc_constructor_next (cons))
   18321              :     {
   18322            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   18323            4 :         return &cons->expr->where;
   18324              :     }
   18325              : 
   18326            0 :   return &struct_expr->where;
   18327              : }
   18328              : 
   18329              : /* Resolve the components of a structure type. Much less work than derived
   18330              :    types.  */
   18331              : 
   18332              : static bool
   18333          913 : resolve_fl_struct (gfc_symbol *sym)
   18334              : {
   18335          913 :   gfc_component *c;
   18336          913 :   gfc_expr *init = NULL;
   18337          913 :   bool success;
   18338              : 
   18339              :   /* Make sure UNIONs do not have overlapping initializers.  */
   18340          913 :   if (sym->attr.flavor == FL_UNION)
   18341              :     {
   18342          498 :       for (c = sym->components; c; c = c->next)
   18343              :         {
   18344          331 :           if (init && c->initializer)
   18345              :             {
   18346            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   18347              :                          cons_where (init), cons_where (c->initializer));
   18348            2 :               gfc_free_expr (c->initializer);
   18349            2 :               c->initializer = NULL;
   18350              :             }
   18351              :           if (init == NULL)
   18352          291 :             init = c->initializer;
   18353              :         }
   18354              :     }
   18355              : 
   18356          913 :   success = true;
   18357         2830 :   for (c = sym->components; c; c = c->next)
   18358         1917 :     if (!resolve_component (c, sym))
   18359            0 :       success = false;
   18360              : 
   18361          913 :   if (!success)
   18362              :     return false;
   18363              : 
   18364          913 :   if (sym->components)
   18365          862 :     add_dt_to_dt_list (sym);
   18366              : 
   18367              :   return true;
   18368              : }
   18369              : 
   18370              : /* Figure if the derived type is using itself directly in one of its components
   18371              :    or through referencing other derived types.  The information is required to
   18372              :    generate the __deallocate and __final type bound procedures to ensure
   18373              :    freeing larger hierarchies of derived types with allocatable objects.  */
   18374              : 
   18375              : static void
   18376       149415 : resolve_cyclic_derived_type (gfc_symbol *derived)
   18377              : {
   18378       149415 :   hash_set<gfc_symbol *> seen, to_examin;
   18379       149415 :   gfc_component *c;
   18380       149415 :   seen.add (derived);
   18381       149415 :   to_examin.add (derived);
   18382       502731 :   while (!to_examin.is_empty ())
   18383              :     {
   18384       206255 :       gfc_symbol *cand = *to_examin.begin ();
   18385       206255 :       to_examin.remove (cand);
   18386       564146 :       for (c = cand->components; c; c = c->next)
   18387       360245 :         if (c->ts.type == BT_DERIVED)
   18388              :           {
   18389        79021 :             if (c->ts.u.derived == derived)
   18390              :               {
   18391         1282 :                 derived->attr.recursive = 1;
   18392         2354 :                 return;
   18393              :               }
   18394        77739 :             else if (!seen.contains (c->ts.u.derived))
   18395              :               {
   18396        51635 :                 seen.add (c->ts.u.derived);
   18397        51635 :                 to_examin.add (c->ts.u.derived);
   18398              :               }
   18399              :           }
   18400       281224 :         else if (c->ts.type == BT_CLASS)
   18401              :           {
   18402        10525 :             if (!c->attr.class_ok)
   18403            7 :               continue;
   18404        10518 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   18405              :               {
   18406         1072 :                 derived->attr.recursive = 1;
   18407         1072 :                 return;
   18408              :               }
   18409         9446 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   18410              :               {
   18411         5527 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   18412         5527 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   18413              :               }
   18414              :           }
   18415              :     }
   18416       149415 : }
   18417              : 
   18418              : /* Resolve the components of a derived type. This does not have to wait until
   18419              :    resolution stage, but can be done as soon as the dt declaration has been
   18420              :    parsed.  */
   18421              : 
   18422              : static bool
   18423       184691 : resolve_fl_derived0 (gfc_symbol *sym)
   18424              : {
   18425       184691 :   gfc_symbol* super_type;
   18426       184691 :   gfc_component *c;
   18427       184691 :   gfc_formal_arglist *f;
   18428       184691 :   bool success;
   18429              : 
   18430       184691 :   if (sym->attr.unlimited_polymorphic)
   18431              :     return true;
   18432              : 
   18433       184691 :   super_type = gfc_get_derived_super_type (sym);
   18434              : 
   18435              :   /* F2008, C432.  */
   18436       184691 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   18437              :     {
   18438            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   18439              :                  "parent type %qs shall also have one", sym->name,
   18440              :                  &sym->declared_at, super_type->name);
   18441            2 :       return false;
   18442              :     }
   18443              : 
   18444              :   /* Ensure the extended type gets resolved before we do.  */
   18445        19610 :   if (super_type && !resolve_fl_derived0 (super_type))
   18446              :     return false;
   18447              : 
   18448              :   /* An ABSTRACT type must be extensible.  */
   18449       184683 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   18450              :     {
   18451            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   18452              :                  sym->name, &sym->declared_at);
   18453            2 :       return false;
   18454              :     }
   18455              : 
   18456              :   /* Resolving components below, may create vtabs for which the cyclic type
   18457              :      information needs to be present.  */
   18458       184681 :   if (!sym->attr.vtype)
   18459       149415 :     resolve_cyclic_derived_type (sym);
   18460              : 
   18461       184681 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   18462              :                            : sym->components;
   18463              : 
   18464       184681 :   success = true;
   18465       636125 :   for ( ; c != NULL; c = c->next)
   18466       451444 :     if (!resolve_component (c, sym))
   18467           96 :       success = false;
   18468              : 
   18469       184681 :   if (!success)
   18470              :     return false;
   18471              : 
   18472              :   /* Now add the caf token field, where needed.  */
   18473       184595 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   18474         1065 :       && !sym->attr.vtype)
   18475              :     {
   18476         2335 :       for (c = sym->components; c; c = c->next)
   18477         1483 :         if (!c->attr.dimension && !c->attr.codimension
   18478          809 :             && (c->attr.allocatable || c->attr.pointer))
   18479              :           {
   18480          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   18481          146 :             gfc_component *token;
   18482          146 :             sprintf (name, "_caf_%s", c->name);
   18483          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   18484          146 :             if (token == NULL)
   18485              :               {
   18486           82 :                 if (!gfc_add_component (sym, name, &token))
   18487            0 :                   return false;
   18488           82 :                 token->ts.type = BT_VOID;
   18489           82 :                 token->ts.kind = gfc_default_integer_kind;
   18490           82 :                 token->attr.access = ACCESS_PRIVATE;
   18491           82 :                 token->attr.artificial = 1;
   18492           82 :                 token->attr.caf_token = 1;
   18493              :               }
   18494          146 :             c->caf_token = token;
   18495              :           }
   18496              :     }
   18497              : 
   18498       184595 :   check_defined_assignments (sym);
   18499              : 
   18500       184595 :   if (!sym->attr.defined_assign_comp && super_type)
   18501        18603 :     sym->attr.defined_assign_comp
   18502        18603 :                         = super_type->attr.defined_assign_comp;
   18503              : 
   18504              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   18505              :      all DEFERRED bindings are overridden.  */
   18506        19603 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   18507         1523 :       && !sym->attr.is_class
   18508         3369 :       && !ensure_not_abstract (sym, super_type))
   18509              :     return false;
   18510              : 
   18511              :   /* Check that there is a component for every PDT parameter.  */
   18512       184589 :   if (sym->attr.pdt_template)
   18513              :     {
   18514         5142 :       for (f = sym->formal; f; f = f->next)
   18515              :         {
   18516         3103 :           if (!f->sym)
   18517            1 :             continue;
   18518         3102 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   18519         3102 :           if (c == NULL)
   18520              :             {
   18521            9 :               gfc_error ("Parameterized type %qs does not have a component "
   18522              :                          "corresponding to parameter %qs at %L", sym->name,
   18523            9 :                          f->sym->name, &sym->declared_at);
   18524            9 :               break;
   18525              :             }
   18526              :         }
   18527              :     }
   18528              : 
   18529              :   /* Add derived type to the derived type list.  */
   18530       184589 :   add_dt_to_dt_list (sym);
   18531              : 
   18532       184589 :   return true;
   18533              : }
   18534              : 
   18535              : /* The following procedure does the full resolution of a derived type,
   18536              :    including resolution of all type-bound procedures (if present). In contrast
   18537              :    to 'resolve_fl_derived0' this can only be done after the module has been
   18538              :    parsed completely.  */
   18539              : 
   18540              : static bool
   18541        95459 : resolve_fl_derived (gfc_symbol *sym)
   18542              : {
   18543        95459 :   gfc_symbol *gen_dt = NULL;
   18544              : 
   18545        95459 :   if (sym->attr.unlimited_polymorphic)
   18546              :     return true;
   18547              : 
   18548        95459 :   if (!sym->attr.is_class)
   18549        81392 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   18550        61409 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   18551         2315 :       && (!gen_dt->generic->sym->attr.use_assoc
   18552         2166 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   18553        95641 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   18554              :                           "%qs at %L being the same name as derived "
   18555              :                           "type at %L", sym->name,
   18556              :                           gen_dt->generic->sym == sym
   18557           11 :                           ? gen_dt->generic->next->sym->name
   18558              :                           : gen_dt->generic->sym->name,
   18559              :                           gen_dt->generic->sym == sym
   18560           11 :                           ? &gen_dt->generic->next->sym->declared_at
   18561              :                           : &gen_dt->generic->sym->declared_at,
   18562              :                           &sym->declared_at))
   18563              :     return false;
   18564              : 
   18565        95455 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   18566              :     {
   18567           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   18568              :                   sym->name, &sym->declared_at);
   18569           13 :       return false;
   18570              :     }
   18571              : 
   18572              :   /* Resolve the finalizer procedures.  */
   18573        95442 :   if (!gfc_resolve_finalizers (sym, NULL))
   18574              :     return false;
   18575              : 
   18576        95439 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   18577              :     {
   18578              :       /* Fix up incomplete CLASS symbols.  */
   18579        14067 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   18580        14067 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18581              : 
   18582        14067 :       if (data->ts.u.derived->attr.pdt_template)
   18583              :         {
   18584            0 :           match m;
   18585            0 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   18586              :                                     &data->param_list);
   18587            0 :           if (m != MATCH_YES
   18588            0 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   18589              :             {
   18590            0 :               gfc_error ("Failed to build PDT class component at %L",
   18591              :                          &sym->declared_at);
   18592            0 :               return false;
   18593              :             }
   18594            0 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   18595            0 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18596              :         }
   18597              : 
   18598              :       /* Nothing more to do for unlimited polymorphic entities.  */
   18599        14067 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   18600              :         {
   18601         2266 :           add_dt_to_dt_list (sym);
   18602         2266 :           return true;
   18603              :         }
   18604        11801 :       else if (vptr->ts.u.derived == NULL)
   18605              :         {
   18606         7031 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   18607         7031 :           gcc_assert (vtab);
   18608         7031 :           vptr->ts.u.derived = vtab->ts.u.derived;
   18609         7031 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   18610              :             return false;
   18611              :         }
   18612              :     }
   18613              : 
   18614        93173 :   if (!resolve_fl_derived0 (sym))
   18615              :     return false;
   18616              : 
   18617              :   /* Resolve the type-bound procedures.  */
   18618        93089 :   if (!resolve_typebound_procedures (sym))
   18619              :     return false;
   18620              : 
   18621              :   /* Generate module vtables subject to their accessibility and their not
   18622              :      being vtables or pdt templates. If this is not done class declarations
   18623              :      in external procedures wind up with their own version and so SELECT TYPE
   18624              :      fails because the vptrs do not have the same address.  */
   18625        93048 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   18626        92987 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   18627        69523 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   18628        23654 :       && sym->attr.access != ACCESS_PRIVATE
   18629        23621 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   18630              :     {
   18631        20957 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   18632        20957 :       gfc_set_sym_referenced (vtab);
   18633              :     }
   18634              : 
   18635              :   return true;
   18636              : }
   18637              : 
   18638              : 
   18639              : static bool
   18640          881 : resolve_fl_namelist (gfc_symbol *sym)
   18641              : {
   18642          881 :   gfc_namelist *nl;
   18643          881 :   gfc_symbol *nlsym;
   18644              : 
   18645         3082 :   for (nl = sym->namelist; nl; nl = nl->next)
   18646              :     {
   18647              :       /* Check again, the check in match only works if NAMELIST comes
   18648              :          after the decl.  */
   18649         2206 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   18650              :         {
   18651            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   18652              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   18653            1 :           return false;
   18654              :         }
   18655              : 
   18656          678 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   18657         2213 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18658              :                               "with assumed shape in namelist %qs at %L",
   18659              :                               nl->sym->name, sym->name, &sym->declared_at))
   18660              :         return false;
   18661              : 
   18662         2204 :       if (is_non_constant_shape_array (nl->sym)
   18663         2254 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18664              :                               "with nonconstant shape in namelist %qs at %L",
   18665           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   18666              :         return false;
   18667              : 
   18668         2203 :       if (nl->sym->ts.type == BT_CHARACTER
   18669          605 :           && (nl->sym->ts.u.cl->length == NULL
   18670          566 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   18671         2285 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   18672              :                               "nonconstant character length in "
   18673           82 :                               "namelist %qs at %L", nl->sym->name,
   18674              :                               sym->name, &sym->declared_at))
   18675              :         return false;
   18676              : 
   18677              :     }
   18678              : 
   18679              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   18680          876 :   if (gfc_check_symbol_access (sym))
   18681              :     {
   18682         3063 :       for (nl = sym->namelist; nl; nl = nl->next)
   18683              :         {
   18684         2200 :           if (!nl->sym->attr.use_assoc
   18685         4104 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   18686         4230 :               && !gfc_check_symbol_access (nl->sym))
   18687              :             {
   18688            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   18689              :                          "cannot be member of PUBLIC namelist %qs at %L",
   18690            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   18691            2 :               return false;
   18692              :             }
   18693              : 
   18694         2198 :           if (nl->sym->ts.type == BT_DERIVED
   18695          478 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   18696          476 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   18697              :            {
   18698            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   18699              :                                   "namelist %qs at %L with ALLOCATABLE "
   18700              :                                   "or POINTER components", nl->sym->name,
   18701              :                                   sym->name, &sym->declared_at))
   18702              :                return false;
   18703              :              return true;
   18704              :            }
   18705              : 
   18706              :           /* Types with private components that came here by USE-association.  */
   18707         2193 :           if (nl->sym->ts.type == BT_DERIVED
   18708         2193 :               && derived_inaccessible (nl->sym->ts.u.derived))
   18709              :             {
   18710            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   18711              :                          "components and cannot be member of namelist %qs at %L",
   18712              :                          nl->sym->name, sym->name, &sym->declared_at);
   18713            6 :               return false;
   18714              :             }
   18715              : 
   18716              :           /* Types with private components that are defined in the same module.  */
   18717         2187 :           if (nl->sym->ts.type == BT_DERIVED
   18718          934 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18719         2477 :               && nl->sym->ts.u.derived->attr.private_comp)
   18720              :             {
   18721            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18722              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18723              :                          nl->sym->name, sym->name, &sym->declared_at);
   18724            0 :               return false;
   18725              :             }
   18726              :         }
   18727              :     }
   18728              : 
   18729              : 
   18730              :   /* 14.1.2 A module or internal procedure represent local entities
   18731              :      of the same type as a namelist member and so are not allowed.  */
   18732         3047 :   for (nl = sym->namelist; nl; nl = nl->next)
   18733              :     {
   18734         2187 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18735         1616 :         continue;
   18736              : 
   18737          571 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18738            7 :         if ((nl->sym == sym->ns->proc_name)
   18739            1 :                ||
   18740            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18741            6 :           continue;
   18742              : 
   18743          565 :       nlsym = NULL;
   18744          565 :       if (nl->sym->name)
   18745          565 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18746          565 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18747              :         {
   18748            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18749              :                      "attribute in %qs at %L", nlsym->name,
   18750              :                      &sym->declared_at);
   18751            3 :           return false;
   18752              :         }
   18753              :     }
   18754              : 
   18755              :   return true;
   18756              : }
   18757              : 
   18758              : 
   18759              : static bool
   18760       420216 : resolve_fl_parameter (gfc_symbol *sym)
   18761              : {
   18762              :   /* A parameter array's shape needs to be constant.  */
   18763       420216 :   if (sym->as != NULL
   18764       420216 :       && (sym->as->type == AS_DEFERRED
   18765         6437 :           || is_non_constant_shape_array (sym)))
   18766              :     {
   18767           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18768              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18769           17 :       return false;
   18770              :     }
   18771              : 
   18772              :   /* Constraints on deferred type parameter.  */
   18773       420199 :   if (!deferred_requirements (sym))
   18774              :     return false;
   18775              : 
   18776              :   /* Make sure a parameter that has been implicitly typed still
   18777              :      matches the implicit type, since PARAMETER statements can precede
   18778              :      IMPLICIT statements.  */
   18779       420198 :   if (sym->attr.implicit_type
   18780       420911 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18781          713 :                                                              sym->ns)))
   18782              :     {
   18783            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18784              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18785            0 :       return false;
   18786              :     }
   18787              : 
   18788              :   /* Make sure the types of derived parameters are consistent.  This
   18789              :      type checking is deferred until resolution because the type may
   18790              :      refer to a derived type from the host.  */
   18791       420198 :   if (sym->ts.type == BT_DERIVED
   18792       420198 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18793              :     {
   18794            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18795            0 :                  &sym->value->where);
   18796            0 :       return false;
   18797              :     }
   18798              : 
   18799              :   /* F03:C509,C514.  */
   18800       420198 :   if (sym->ts.type == BT_CLASS)
   18801              :     {
   18802            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18803              :                  sym->name, &sym->declared_at);
   18804            0 :       return false;
   18805              :     }
   18806              : 
   18807              :   /* Some programmers can have a typo when using an implied-do loop to
   18808              :      initialize an array constant.  For example,
   18809              :        INTEGER I,J
   18810              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18811              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18812              :      This check catches the typo.  */
   18813       420198 :   if (sym->attr.dimension
   18814         6430 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18815       426622 :       && !gfc_is_constant_expr (sym->value))
   18816              :     {
   18817              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18818              :          the array constructor of a parameter.  This seems inconsistent with
   18819              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18820           20 :       if (sym->value->ts.type == BT_DERIVED
   18821           18 :           && sym->value->ts.u.derived
   18822           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18823              :         return true;
   18824            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18825            2 :       return false;
   18826              :     }
   18827              : 
   18828              :   return true;
   18829              : }
   18830              : 
   18831              : 
   18832              : /* Called by resolve_symbol to check PDTs.  */
   18833              : 
   18834              : static void
   18835         2581 : resolve_pdt (gfc_symbol* sym)
   18836              : {
   18837         2581 :   gfc_symbol *derived = NULL;
   18838         2581 :   gfc_actual_arglist *param;
   18839         2581 :   gfc_component *c;
   18840         2581 :   bool const_len_exprs = true;
   18841         2581 :   bool assumed_len_exprs = false;
   18842         2581 :   symbol_attribute *attr;
   18843              : 
   18844         2581 :   if (sym->ts.type == BT_DERIVED)
   18845              :     {
   18846         2161 :       derived = sym->ts.u.derived;
   18847         2161 :       attr = &(sym->attr);
   18848              :     }
   18849          420 :   else if (sym->ts.type == BT_CLASS)
   18850              :     {
   18851          420 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18852          420 :       attr = &(CLASS_DATA (sym)->attr);
   18853              :     }
   18854              :   else
   18855            0 :     gcc_unreachable ();
   18856              : 
   18857         2581 :   gcc_assert (derived->attr.pdt_type);
   18858              : 
   18859              :   /* Now that the result symbol has been corrected for these module
   18860              :      procedures, we can transfer the param_list from the proc sym.  */
   18861         2581 :   if (sym->abr_modproc_decl && sym->result && sym != sym->result)
   18862              :     {
   18863           18 :       sym->result->param_list = sym->param_list;
   18864           18 :       sym->param_list = NULL;
   18865              :     }
   18866              : 
   18867         6063 :   for (param = sym->param_list; param; param = param->next)
   18868              :     {
   18869         3482 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18870         3482 :       gcc_assert (c);
   18871         3482 :       if (c->attr.pdt_kind)
   18872         1565 :         continue;
   18873              : 
   18874         1234 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18875         2151 :           && c->attr.pdt_len)
   18876              :         const_len_exprs = false;
   18877         1683 :       else if (param->spec_type == SPEC_ASSUMED)
   18878          639 :         assumed_len_exprs = true;
   18879              : 
   18880         1917 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18881           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18882           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18883            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18884              :                    "parameter %qs and requires either the POINTER "
   18885              :                    "or ALLOCATABLE attribute",
   18886              :                    sym->name, &sym->declared_at,
   18887              :                    param->name);
   18888              :     }
   18889              : 
   18890         2581 :   if (!const_len_exprs
   18891          228 :       && (sym->ns->proc_name->attr.is_main_program
   18892          227 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18893          226 :           || sym->attr.save != SAVE_NONE))
   18894            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18895              :                "SAVE attribute or be a variable declared in the "
   18896              :                "main program, a module or a submodule(F08/C513)",
   18897              :                sym->name, &sym->declared_at);
   18898              : 
   18899         2581 :   if (assumed_len_exprs && !(sym->attr.dummy
   18900            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18901            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18902              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18903              :                sym->name, &sym->declared_at);
   18904         2581 : }
   18905              : 
   18906              : 
   18907              : /* Resolve the symbol's array spec.  */
   18908              : 
   18909              : static bool
   18910      1829735 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18911              : {
   18912      1829735 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18913      1829735 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18914              : 
   18915      1829735 :   bool saved_specification_expr = specification_expr;
   18916      1829735 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18917      1829735 :   specification_expr = true;
   18918      1829735 :   specification_expr_symbol = sym;
   18919              : 
   18920      1829735 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18921              : 
   18922      1829735 :   specification_expr = saved_specification_expr;
   18923      1829735 :   specification_expr_symbol = saved_specification_expr_symbol;
   18924      1829735 :   gfc_current_ns = orig_current_ns;
   18925              : 
   18926      1829735 :   return result;
   18927              : }
   18928              : 
   18929              : 
   18930              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18931              :    assume that an otherwise unknown symbol is a variable.  This sort
   18932              :    of thing commonly happens for symbols in module.  */
   18933              : 
   18934              : static void
   18935      2007692 : resolve_symbol (gfc_symbol *sym)
   18936              : {
   18937      2007692 :   int check_constant, mp_flag;
   18938      2007692 :   gfc_symtree *symtree;
   18939      2007692 :   gfc_symtree *this_symtree;
   18940      2007692 :   gfc_namespace *ns;
   18941      2007692 :   gfc_component *c;
   18942      2007692 :   symbol_attribute class_attr;
   18943      2007692 :   gfc_array_spec *as;
   18944      2007692 :   bool declared_has_coarray_comp = false;
   18945              : 
   18946      2007692 :   if (sym->resolve_symbol_called >= 1)
   18947       209663 :     return;
   18948      1914370 :   sym->resolve_symbol_called = 1;
   18949              : 
   18950              :   /* No symbol will ever have union type; only components can be unions.
   18951              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18952              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18953      1914370 :   gcc_assert (sym->ts.type != BT_UNION);
   18954              : 
   18955              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18956              :      yet unsupported for -fcoarray=lib.  */
   18957      1914370 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18958          116 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18959          116 :       && CLASS_DATA (sym)->attr.codimension
   18960           98 :       && CLASS_DATA (sym)->ts.u.derived
   18961           97 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18962           94 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18963              :     {
   18964            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18965              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18966            6 :       return;
   18967              :     }
   18968              : 
   18969      1914364 :   if (sym->attr.artificial)
   18970              :     return;
   18971              : 
   18972      1800959 :   if (sym->attr.unlimited_polymorphic)
   18973              :     return;
   18974              : 
   18975      1799311 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18976              :     {
   18977            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18978              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18979            4 :       return;
   18980              :     }
   18981              : 
   18982      1799307 :   if (sym->attr.flavor == FL_UNKNOWN
   18983      1777319 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18984       477020 :           && !sym->attr.generic && !sym->attr.external
   18985       189034 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18986        85649 :           && sym->ts.type == BT_UNKNOWN))
   18987              :     {
   18988              :       /* A symbol in a common block might not have been resolved yet properly.
   18989              :          Do not try to find an interface with the same name.  */
   18990        98891 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18991        21984 :           && !sym->attr.generic && !sym->attr.external
   18992        21933 :           && sym->attr.in_common)
   18993         2595 :         goto skip_interfaces;
   18994              : 
   18995              :     /* If we find that a flavorless symbol is an interface in one of the
   18996              :        parent namespaces, find its symtree in this namespace, free the
   18997              :        symbol and set the symtree to point to the interface symbol.  */
   18998       137987 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18999              :         {
   19000        42429 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   19001        42429 :           if (symtree && (symtree->n.sym->generic ||
   19002          843 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   19003          729 :                            && sym->ns->construct_entities)))
   19004              :             {
   19005          746 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   19006              :                                                sym->name);
   19007          746 :               if (this_symtree->n.sym == sym)
   19008              :                 {
   19009          738 :                   symtree->n.sym->refs++;
   19010          738 :                   gfc_release_symbol (sym);
   19011          738 :                   this_symtree->n.sym = symtree->n.sym;
   19012          738 :                   return;
   19013              :                 }
   19014              :             }
   19015              :         }
   19016              : 
   19017        95558 : skip_interfaces:
   19018              :       /* Otherwise give it a flavor according to such attributes as
   19019              :          it has.  */
   19020        98153 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   19021        21803 :           && sym->attr.intrinsic == 0)
   19022        21799 :         sym->attr.flavor = FL_VARIABLE;
   19023        76354 :       else if (sym->attr.flavor == FL_UNKNOWN)
   19024              :         {
   19025           55 :           sym->attr.flavor = FL_PROCEDURE;
   19026           55 :           if (sym->attr.dimension)
   19027            0 :             sym->attr.function = 1;
   19028              :         }
   19029              :     }
   19030              : 
   19031      1798569 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   19032         2385 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   19033              : 
   19034         1542 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   19035      1800111 :       && !resolve_procedure_interface (sym))
   19036              :     return;
   19037              : 
   19038      1798558 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   19039          130 :       && (sym->attr.procedure || sym->attr.external))
   19040              :     {
   19041            0 :       if (sym->attr.external)
   19042            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   19043              :                    "at %L", &sym->declared_at);
   19044              :       else
   19045            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   19046              :                    "at %L", &sym->declared_at);
   19047              : 
   19048              :       return;
   19049              :     }
   19050              : 
   19051              :   /* Ensure that variables of derived or class type having a finalizer are
   19052              :      marked used even when the variable is not used anything else in the scope.
   19053              :      This fixes PR118730.  */
   19054       694203 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   19055       478709 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   19056      1850290 :       && gfc_may_be_finalized (sym->ts))
   19057         9262 :     gfc_set_sym_referenced (sym);
   19058              : 
   19059      1798558 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   19060              :     return;
   19061              : 
   19062      1798413 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   19063      1798413 :            && !resolve_fl_struct (sym))
   19064              :     return;
   19065              : 
   19066              :   /* Symbols that are module procedures with results (functions) have
   19067              :      the types and array specification copied for type checking in
   19068              :      procedures that call them, as well as for saving to a module
   19069              :      file.  These symbols can't stand the scrutiny that their results
   19070              :      can.  */
   19071      1798413 :   mp_flag = (sym->result != NULL && sym->result != sym);
   19072              : 
   19073              :   /* Make sure that the intrinsic is consistent with its internal
   19074              :      representation. This needs to be done before assigning a default
   19075              :      type to avoid spurious warnings.  */
   19076      1761582 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   19077      1836969 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   19078              :     return;
   19079              : 
   19080              :   /* Resolve associate names.  */
   19081      1798377 :   if (sym->assoc)
   19082         7524 :     resolve_assoc_var (sym, true);
   19083              : 
   19084              :   /* Assign default type to symbols that need one and don't have one.  */
   19085      1798377 :   if (sym->ts.type == BT_UNKNOWN)
   19086              :     {
   19087       435405 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   19088              :         {
   19089        11855 :           gfc_set_default_type (sym, 1, NULL);
   19090              :         }
   19091              : 
   19092       282062 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   19093        67839 :           && !sym->attr.function && !sym->attr.subroutine
   19094       437078 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   19095          622 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   19096              : 
   19097       435405 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   19098              :         {
   19099              :           /* The specific case of an external procedure should emit an error
   19100              :              in the case that there is no implicit type.  */
   19101       109504 :           if (!mp_flag)
   19102              :             {
   19103       102984 :               if (!sym->attr.mixed_entry_master)
   19104       102876 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   19105              :             }
   19106              :           else
   19107              :             {
   19108              :               /* Result may be in another namespace.  */
   19109         6520 :               resolve_symbol (sym->result);
   19110              : 
   19111         6520 :               if (!sym->result->attr.proc_pointer)
   19112              :                 {
   19113         6334 :                   sym->ts = sym->result->ts;
   19114         6334 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   19115         6334 :                   sym->attr.dimension = sym->result->attr.dimension;
   19116         6334 :                   sym->attr.codimension = sym->result->attr.codimension;
   19117         6334 :                   sym->attr.pointer = sym->result->attr.pointer;
   19118         6334 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   19119         6334 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   19120              :                 }
   19121              :             }
   19122              :         }
   19123              :     }
   19124      1362972 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   19125        31703 :     resolve_symbol_array_spec (sym->result, false);
   19126              : 
   19127              :   /* For a CLASS-valued function with a result variable, affirm that it has
   19128              :      been resolved also when looking at the symbol 'sym'.  */
   19129       467108 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   19130          794 :     sym->attr.class_ok = sym->result->attr.class_ok;
   19131              : 
   19132      1798377 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   19133        21164 :       && CLASS_DATA (sym))
   19134              :     {
   19135        21164 :       as = CLASS_DATA (sym)->as;
   19136        21164 :       class_attr = CLASS_DATA (sym)->attr;
   19137        21164 :       class_attr.pointer = class_attr.class_pointer;
   19138        21164 :       declared_has_coarray_comp = CLASS_DATA (sym)->ts.u.derived
   19139        21164 :                                   && CLASS_DATA (sym)->ts.u.derived->attr.coarray_comp;
   19140              :     }
   19141              :   else
   19142              :     {
   19143      1777213 :       class_attr = sym->attr;
   19144      1777213 :       as = sym->as;
   19145              :     }
   19146              : 
   19147              :   /* F2008, C530.  */
   19148      1798377 :   if (sym->attr.contiguous
   19149         8568 :       && !sym->attr.associate_var
   19150         8566 :       && (!class_attr.dimension
   19151         8563 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   19152          144 :               && !class_attr.pointer)))
   19153              :     {
   19154            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   19155              :                  "array pointer or an assumed-shape or assumed-rank array",
   19156              :                  sym->name, &sym->declared_at);
   19157            7 :       return;
   19158              :     }
   19159              : 
   19160              :   /* Assumed size arrays and assumed shape arrays must be dummy
   19161              :      arguments.  Array-spec's of implied-shape should have been resolved to
   19162              :      AS_EXPLICIT already.  */
   19163              : 
   19164      1789951 :   if (as)
   19165              :     {
   19166              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   19167              :          specification expression.  */
   19168       155075 :       if (as->type == AS_IMPLIED_SHAPE)
   19169              :         {
   19170              :           int i;
   19171            1 :           for (i=0; i<as->rank; i++)
   19172              :             {
   19173            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   19174              :                 {
   19175            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   19176              :                              &as->lower[i]->where);
   19177            1 :                   return;
   19178              :                 }
   19179              :             }
   19180            0 :           gcc_unreachable();
   19181              :         }
   19182              : 
   19183       155074 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   19184       119734 :            || as->type == AS_ASSUMED_SHAPE)
   19185        48114 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   19186            9 :           && !sym->attr.associate_var)
   19187              :         {
   19188            7 :           if (as->type == AS_ASSUMED_SIZE)
   19189            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   19190              :                        &sym->declared_at);
   19191              :           else
   19192            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   19193              :                        &sym->declared_at);
   19194              :           return;
   19195              :         }
   19196              :       /* TS 29113, C535a.  */
   19197       155067 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   19198           60 :           && !sym->attr.select_type_temporary
   19199           60 :           && !(cs_base && cs_base->current
   19200           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   19201            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   19202            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   19203              :         {
   19204           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   19205              :                      &sym->declared_at);
   19206           18 :           return;
   19207              :         }
   19208       155049 :       if (as->type == AS_ASSUMED_RANK
   19209        27453 :           && (sym->attr.codimension || sym->attr.value))
   19210              :         {
   19211            5 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   19212              :                      "CODIMENSION attribute", &sym->declared_at);
   19213            5 :           return;
   19214              :         }
   19215              : 
   19216              :       /* F2008, C557 (F2018, C862; F2023, C867).  Assumed-shape and
   19217              :          explicit-shape array dummies may have the VALUE attribute, but
   19218              :          assumed-size arrays may not.  */
   19219       155044 :       if (as->type == AS_ASSUMED_SIZE && sym->attr.value)
   19220              :         {
   19221            1 :           gfc_error ("Assumed-size array %qs at %L may not have the VALUE "
   19222              :                      "attribute", sym->name, &sym->declared_at);
   19223            1 :           return;
   19224              :         }
   19225       155043 :       else if (sym->attr.value && sym->attr.dummy
   19226          144 :                && (as->type == AS_EXPLICIT || as->type == AS_ASSUMED_SHAPE))
   19227              :         {
   19228          144 :           if (!gfc_notify_std (GFC_STD_F2008, "Array dummy argument %qs at "
   19229              :                                "%L with VALUE attribute", sym->name,
   19230              :                                &sym->declared_at))
   19231              :             return;
   19232              : 
   19233              :           /* F2023, 18.3.6 (4): only a scalar VALUE dummy is interoperable
   19234              :              with a formal parameter of the C prototype.  */
   19235          143 :           if (sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   19236              :             {
   19237            2 :               gfc_error ("Array dummy argument %qs at %L with VALUE attribute "
   19238              :                          "not allowed in BIND(C) procedure %qs", sym->name,
   19239              :                          &sym->declared_at, sym->ns->proc_name->name);
   19240            2 :               return;
   19241              :             }
   19242              : 
   19243          141 :           if (sym->ts.type == BT_CLASS)
   19244              :             {
   19245            1 :               gfc_error ("Sorry, polymorphic array dummy argument %qs at %L "
   19246              :                          "with VALUE attribute is not yet implemented",
   19247              :                          sym->name, &sym->declared_at);
   19248            1 :               return;
   19249              :             }
   19250              :         }
   19251              :     }
   19252              : 
   19253              :   /* Make sure symbols with known intent or optional are really dummy
   19254              :      variable.  Because of ENTRY statement, this has to be deferred
   19255              :      until resolution time.  */
   19256              : 
   19257      1798334 :   if (!sym->attr.dummy
   19258      1293970 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   19259              :     {
   19260            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   19261            2 :       return;
   19262              :     }
   19263              : 
   19264      1798332 :   if (sym->attr.value && !sym->attr.dummy)
   19265              :     {
   19266            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   19267              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   19268            2 :       return;
   19269              :     }
   19270              : 
   19271      1798330 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   19272              :     {
   19273          707 :       gfc_charlen *cl = sym->ts.u.cl;
   19274          707 :       if (!cl)
   19275              :         {
   19276            0 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   19277              :                      "attribute must have a length specification",
   19278              :                      sym->name, &sym->declared_at);
   19279            0 :           return;
   19280              :         }
   19281              : 
   19282              :       /* C interoperable character dummies must have length one.  */
   19283          707 :       if (sym->ts.is_c_interop
   19284          394 :           && (!cl->length
   19285          393 :               || cl->length->expr_type != EXPR_CONSTANT
   19286          393 :               || mpz_cmp_si (cl->length->value.integer, 1) != 0))
   19287              :         {
   19288            2 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   19289              :                      "with VALUE attribute must have length one",
   19290              :                      sym->name, &sym->declared_at);
   19291            2 :           return;
   19292              :         }
   19293              : 
   19294              :       /* Assumed-length character dummy with VALUE, valid since F2008.  */
   19295          705 :       if (!cl->length
   19296          705 :           && !gfc_notify_std (GFC_STD_F2008, "Assumed-length character "
   19297              :                               "dummy variable %qs at %L with VALUE attribute",
   19298              :                               sym->name, &sym->declared_at))
   19299              :         return;
   19300              : 
   19301              :       /* Likewise for a specified but non-constant length.  */
   19302          655 :       if (cl->length && cl->length->expr_type != EXPR_CONSTANT
   19303          727 :           && !gfc_notify_std (GFC_STD_F2008, "Character dummy variable "
   19304              :                               "%qs at %L with VALUE attribute and "
   19305              :                               "non-constant length",
   19306           24 :                               sym->name, &sym->declared_at))
   19307              :         return;
   19308              :     }
   19309              : 
   19310      1798326 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   19311       129538 :       && sym->ts.u.derived->attr.generic)
   19312              :     {
   19313           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   19314           20 :       if (!sym->ts.u.derived)
   19315              :         {
   19316            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   19317              :                      "which has not been defined", sym->name,
   19318              :                      &sym->declared_at, sym->ts.u.derived->name);
   19319            0 :           sym->ts.type = BT_UNKNOWN;
   19320            0 :           return;
   19321              :         }
   19322              :     }
   19323              : 
   19324              :     /* Use the same constraints as TYPE(*), except for the type check
   19325              :        and that only scalars and assumed-size arrays are permitted.  */
   19326      1798326 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   19327              :       {
   19328        14556 :         if (!sym->attr.dummy)
   19329              :           {
   19330            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   19331              :                        "a dummy argument", sym->name, &sym->declared_at);
   19332            1 :             return;
   19333              :           }
   19334              : 
   19335        14555 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   19336            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   19337            0 :             && sym->ts.type != BT_COMPLEX)
   19338              :           {
   19339            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   19340              :                        "of type TYPE(*) or of an numeric intrinsic type",
   19341              :                        sym->name, &sym->declared_at);
   19342            0 :             return;
   19343              :           }
   19344              : 
   19345        14555 :       if (sym->attr.allocatable || sym->attr.codimension
   19346        14553 :           || sym->attr.pointer || sym->attr.value)
   19347              :         {
   19348            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   19349              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   19350              :                      "attribute", sym->name, &sym->declared_at);
   19351            4 :           return;
   19352              :         }
   19353              : 
   19354        14551 :       if (sym->attr.intent == INTENT_OUT)
   19355              :         {
   19356            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   19357              :                      "have the INTENT(OUT) attribute",
   19358              :                      sym->name, &sym->declared_at);
   19359            0 :           return;
   19360              :         }
   19361        14551 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   19362              :         {
   19363            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   19364              :                      "either be a scalar or an assumed-size array",
   19365              :                      sym->name, &sym->declared_at);
   19366            1 :           return;
   19367              :         }
   19368              : 
   19369              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   19370              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   19371              :          packing.  */
   19372        14550 :       sym->ts.type = BT_ASSUMED;
   19373        14550 :       sym->as = gfc_get_array_spec ();
   19374        14550 :       sym->as->type = AS_ASSUMED_SIZE;
   19375        14550 :       sym->as->rank = 1;
   19376        14550 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   19377              :     }
   19378      1783770 :   else if (sym->ts.type == BT_ASSUMED)
   19379              :     {
   19380              :       /* TS 29113, C407a.  */
   19381        12368 :       if (!sym->attr.dummy)
   19382              :         {
   19383            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   19384              :                      "for dummy variables", sym->name, &sym->declared_at);
   19385            7 :           return;
   19386              :         }
   19387        12361 :       if (sym->attr.allocatable || sym->attr.codimension
   19388        12357 :           || sym->attr.pointer || sym->attr.value)
   19389              :         {
   19390            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   19391              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   19392              :                      sym->name, &sym->declared_at);
   19393            8 :           return;
   19394              :         }
   19395        12353 :       if (sym->attr.intent == INTENT_OUT)
   19396              :         {
   19397            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   19398              :                      "INTENT(OUT) attribute",
   19399              :                      sym->name, &sym->declared_at);
   19400            2 :           return;
   19401              :         }
   19402        12351 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   19403              :         {
   19404            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   19405              :                      "explicit-shape array", sym->name, &sym->declared_at);
   19406            3 :           return;
   19407              :         }
   19408              :     }
   19409              : 
   19410              :   /* If the symbol is marked as bind(c), that it is declared at module level
   19411              :      scope and verify its type and kind.  Do not do the latter for symbols
   19412              :      that are implicitly typed because that is handled in
   19413              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   19414              :      separately.  Also, anything that is use associated is not handled here
   19415              :      but instead is handled in the module it is declared in.  Finally, derived
   19416              :      type definitions are allowed to be BIND(C) since that only implies that
   19417              :      they're interoperable, and they are checked fully for interoperability
   19418              :      when a variable is declared of that type.  */
   19419      1798300 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   19420         8056 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   19421          572 :       && sym->attr.flavor != FL_DERIVED)
   19422              :     {
   19423          168 :       bool t = true;
   19424              : 
   19425              :       /* First, make sure the variable is declared at the
   19426              :          module-level scope (J3/04-007, Section 15.3).  */
   19427          168 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   19428            7 :           && !sym->attr.in_common)
   19429              :         {
   19430            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   19431              :                      "is neither a COMMON block nor declared at the "
   19432              :                      "module level scope", sym->name, &(sym->declared_at));
   19433            6 :           t = false;
   19434              :         }
   19435          162 :       else if (sym->ts.type == BT_CHARACTER
   19436          162 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   19437            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   19438            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   19439              :         {
   19440            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   19441            1 :                      sym->name, &sym->declared_at);
   19442            1 :           t = false;
   19443              :         }
   19444          161 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   19445              :         {
   19446            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   19447              :         }
   19448          160 :       else if (sym->attr.implicit_type == 0)
   19449              :         {
   19450              :           /* If type() declaration, we need to verify that the components
   19451              :              of the given type are all C interoperable, etc.  */
   19452          158 :           if (sym->ts.type == BT_DERIVED &&
   19453           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   19454              :             {
   19455              :               /* Make sure the user marked the derived type as BIND(C).  If
   19456              :                  not, call the verify routine.  This could print an error
   19457              :                  for the derived type more than once if multiple variables
   19458              :                  of that type are declared.  */
   19459           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   19460            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   19461          158 :               t = false;
   19462              :             }
   19463              : 
   19464              :           /* Verify the variable itself as C interoperable if it
   19465              :              is BIND(C).  It is not possible for this to succeed if
   19466              :              the verify_bind_c_derived_type failed, so don't have to handle
   19467              :              any error returned by verify_bind_c_derived_type.  */
   19468          158 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   19469          158 :                                  sym->common_block);
   19470              :         }
   19471              : 
   19472          166 :       if (!t)
   19473              :         {
   19474              :           /* clear the is_bind_c flag to prevent reporting errors more than
   19475              :              once if something failed.  */
   19476           10 :           sym->attr.is_bind_c = 0;
   19477           10 :           return;
   19478              :         }
   19479              :     }
   19480              : 
   19481              :   /* If a derived type symbol has reached this point, without its
   19482              :      type being declared, we have an error.  Notice that most
   19483              :      conditions that produce undefined derived types have already
   19484              :      been dealt with.  However, the likes of:
   19485              :      implicit type(t) (t) ..... call foo (t) will get us here if
   19486              :      the type is not declared in the scope of the implicit
   19487              :      statement. Change the type to BT_UNKNOWN, both because it is so
   19488              :      and to prevent an ICE.  */
   19489      1798290 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   19490       129536 :       && sym->ts.u.derived->components == NULL
   19491         1195 :       && !sym->ts.u.derived->attr.zero_comp)
   19492              :     {
   19493            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   19494              :                  "which has not been defined", sym->name,
   19495              :                   &sym->declared_at, sym->ts.u.derived->name);
   19496            3 :       sym->ts.type = BT_UNKNOWN;
   19497            3 :       return;
   19498              :     }
   19499              : 
   19500              :   /* Make sure that the derived type has been resolved and that the
   19501              :      derived type is visible in the symbol's namespace, if it is a
   19502              :      module function and is not PRIVATE.  */
   19503      1798287 :   if (sym->ts.type == BT_DERIVED
   19504       136703 :         && sym->ts.u.derived->attr.use_assoc
   19505       117292 :         && sym->ns->proc_name
   19506       117284 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   19507      1804270 :         && !resolve_fl_derived (sym->ts.u.derived))
   19508              :     return;
   19509              : 
   19510              :   /* Unless the derived-type declaration is use associated, Fortran 95
   19511              :      does not allow public entries of private derived types.
   19512              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   19513              :      161 in 95-006r3.  */
   19514      1798287 :   if (sym->ts.type == BT_DERIVED
   19515       136703 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   19516         8412 :       && !sym->ts.u.derived->attr.use_assoc
   19517         2429 :       && gfc_check_symbol_access (sym)
   19518         2216 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   19519      1798301 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   19520              :                           "derived type %qs",
   19521           14 :                           (sym->attr.flavor == FL_PARAMETER)
   19522              :                           ? "parameter" : "variable",
   19523              :                           sym->name, &sym->declared_at,
   19524           14 :                           sym->ts.u.derived->name))
   19525              :     return;
   19526              : 
   19527              :   /* F2008, C1302.  */
   19528      1798280 :   if (sym->ts.type == BT_DERIVED
   19529       136696 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19530          198 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   19531       136665 :           || sym->ts.u.derived->attr.lock_comp)
   19532           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19533              :     {
   19534            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   19535              :                  "type LOCK_TYPE must be a coarray", sym->name,
   19536              :                  &sym->declared_at);
   19537            4 :       return;
   19538              :     }
   19539              : 
   19540              :   /* TS18508, C702/C703.  */
   19541      1798276 :   if (sym->ts.type == BT_DERIVED
   19542       136692 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19543          197 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   19544       136675 :           || sym->ts.u.derived->attr.event_comp)
   19545           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19546              :     {
   19547            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   19548              :                  "type EVENT_TYPE must be a coarray", sym->name,
   19549              :                  &sym->declared_at);
   19550            1 :       return;
   19551              :     }
   19552              : 
   19553              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   19554              :      default initialization is defined (5.1.2.4.4).  */
   19555      1798275 :   if (sym->ts.type == BT_DERIVED
   19556       136691 :       && sym->attr.dummy
   19557        46771 :       && sym->attr.intent == INTENT_OUT
   19558         2465 :       && sym->as
   19559          418 :       && sym->as->type == AS_ASSUMED_SIZE)
   19560              :     {
   19561            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   19562              :         {
   19563            1 :           if (c->initializer)
   19564              :             {
   19565            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   19566              :                          "ASSUMED SIZE and so cannot have a default initializer",
   19567              :                          sym->name, &sym->declared_at);
   19568            1 :               return;
   19569              :             }
   19570              :         }
   19571              :     }
   19572              : 
   19573              :   /* F2008, C542.  */
   19574      1798274 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19575        46770 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   19576              :     {
   19577            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   19578              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19579            0 :       return;
   19580              :     }
   19581              : 
   19582              :   /* TS18508.  */
   19583      1798274 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19584        46770 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   19585              :     {
   19586            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   19587              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19588            0 :       return;
   19589              :     }
   19590              : 
   19591              :   /* F2008, C525.  */
   19592      1798274 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19593      1798161 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19594        21166 :              && sym->ts.u.derived && CLASS_DATA (sym)
   19595        21161 :              && CLASS_DATA (sym)->attr.coarray_comp))
   19596      1798161 :        || class_attr.codimension)
   19597         2034 :       && (sym->attr.result || sym->result == sym))
   19598              :     {
   19599            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   19600              :                  "a coarray component", sym->name, &sym->declared_at);
   19601            8 :       return;
   19602              :     }
   19603              : 
   19604              :   /* F2008, C524.  */
   19605      1798266 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   19606          439 :       && sym->ts.u.derived->ts.is_iso_c)
   19607              :     {
   19608            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   19609              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   19610            3 :       return;
   19611              :     }
   19612              : 
   19613              :   /* F2008, C525.  */
   19614      1798263 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19615      1798153 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19616        21165 :             && sym->ts.u.derived && CLASS_DATA (sym)
   19617        21160 :             && CLASS_DATA (sym)->attr.coarray_comp))
   19618          110 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   19619          106 :           || class_attr.allocatable))
   19620              :     {
   19621            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   19622              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   19623              :                  sym->name, &sym->declared_at);
   19624            4 :       return;
   19625              :     }
   19626              : 
   19627              :   /* F2008, C526.  The function-result case was handled above.  */
   19628      1798259 :   if (class_attr.codimension
   19629         1913 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   19630          382 :            || sym->attr.select_type_temporary
   19631          306 :            || sym->attr.associate_var
   19632          288 :            || (sym->ns->save_all && !sym->attr.automatic)
   19633          288 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   19634          288 :            || sym->ns->proc_name->attr.is_main_program
   19635            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   19636              :     {
   19637            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   19638              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   19639            4 :       return;
   19640              :     }
   19641              :   /* F2008, C528.  */
   19642      1798255 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   19643         1833 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   19644              :     {
   19645            7 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   19646              :                  "deferred shape without allocatable", sym->name,
   19647              :                  &sym->declared_at);
   19648            7 :       return;
   19649              :     }
   19650      1798248 :   else if (class_attr.codimension && class_attr.allocatable && as
   19651          800 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   19652              :     {
   19653            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   19654              :                  "deferred shape", sym->name, &sym->declared_at);
   19655            9 :       return;
   19656              :     }
   19657              : 
   19658              :   /* F2008, C541.  */
   19659      1798239 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19660      1798133 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19661        21160 :             && declared_has_coarray_comp))
   19662      1798126 :         || (class_attr.codimension && class_attr.allocatable))
   19663          904 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   19664              :     {
   19665            4 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   19666              :                  "allocatable coarray or have coarray components",
   19667              :                  sym->name, &sym->declared_at);
   19668            4 :       return;
   19669              :     }
   19670              : 
   19671      1798235 :   if (class_attr.codimension && sym->attr.dummy
   19672          482 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   19673              :     {
   19674            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   19675              :                  "procedure %qs", sym->name, &sym->declared_at,
   19676              :                  sym->ns->proc_name->name);
   19677            2 :       return;
   19678              :     }
   19679              : 
   19680      1798233 :   if (sym->ts.type == BT_LOGICAL
   19681       115259 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   19682       115256 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   19683        32904 :               && sym->ns->proc_name->attr.is_bind_c)))
   19684              :     {
   19685              :       int i;
   19686          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   19687          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   19688              :           break;
   19689           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   19690          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   19691              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   19692              :                               "%qs", sym->name, &sym->declared_at,
   19693           13 :                               sym->ns->proc_name->name))
   19694              :         return;
   19695          167 :       else if (!gfc_logical_kinds[i].c_bool
   19696          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   19697              :                                    "%qs at %L with non-C_Bool kind in "
   19698              :                                    "BIND(C) procedure %qs", sym->name,
   19699              :                                    &sym->declared_at,
   19700           15 :                                    sym->attr.function ? sym->name
   19701           13 :                                    : sym->ns->proc_name->name))
   19702              :         return;
   19703              :     }
   19704              : 
   19705      1798230 :   switch (sym->attr.flavor)
   19706              :     {
   19707       694075 :     case FL_VARIABLE:
   19708       694075 :       if (!resolve_fl_variable (sym, mp_flag))
   19709              :         return;
   19710              :       break;
   19711              : 
   19712       514962 :     case FL_PROCEDURE:
   19713       514962 :       if (sym->formal && !sym->formal_ns)
   19714              :         {
   19715              :           /* Check that none of the arguments are a namelist.  */
   19716              :           gfc_formal_arglist *formal = sym->formal;
   19717              : 
   19718       110456 :           for (; formal; formal = formal->next)
   19719        74610 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   19720              :               {
   19721            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   19722              :                            "subroutine or function at %L",
   19723              :                            formal->sym->name, &sym->declared_at);
   19724            1 :                 return;
   19725              :               }
   19726              :         }
   19727              : 
   19728       514961 :       if (!resolve_fl_procedure (sym, mp_flag))
   19729              :         return;
   19730              :       break;
   19731              : 
   19732          881 :     case FL_NAMELIST:
   19733          881 :       if (!resolve_fl_namelist (sym))
   19734              :         return;
   19735              :       break;
   19736              : 
   19737       420216 :     case FL_PARAMETER:
   19738       420216 :       if (!resolve_fl_parameter (sym))
   19739              :         return;
   19740              :       break;
   19741              : 
   19742              :     default:
   19743              :       break;
   19744              :     }
   19745              : 
   19746              :   /* Resolve array specifier. Check as well some constraints
   19747              :      on COMMON blocks.  */
   19748              : 
   19749      1798032 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   19750              : 
   19751      1798032 :   resolve_symbol_array_spec (sym, check_constant);
   19752              : 
   19753              :   /* Resolve formal namespaces.  */
   19754      1798032 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   19755       286967 :       && !sym->attr.contained && !sym->attr.intrinsic)
   19756       255896 :     gfc_resolve (sym->formal_ns);
   19757              : 
   19758              :   /* Make sure the formal namespace is present.  */
   19759      1798032 :   if (sym->formal && !sym->formal_ns)
   19760              :     {
   19761              :       gfc_formal_arglist *formal = sym->formal;
   19762        36647 :       while (formal && !formal->sym)
   19763           11 :         formal = formal->next;
   19764              : 
   19765        36636 :       if (formal)
   19766              :         {
   19767        36625 :           sym->formal_ns = formal->sym->ns;
   19768        36625 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   19769        28124 :             sym->formal_ns->refs++;
   19770              :         }
   19771              :     }
   19772              : 
   19773              :   /* Check threadprivate restrictions.  */
   19774      1798032 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   19775          387 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   19776           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19777           32 :       && sym->module == NULL
   19778           17 :       && (sym->ns->proc_name == NULL
   19779           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19780            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19781              :     {
   19782            2 :       if (sym->attr.threadprivate)
   19783            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19784              :       else
   19785            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19786              :                    "attribute", sym->name, &sym->declared_at);
   19787              :     }
   19788              : 
   19789      1798032 :   if (sym->attr.omp_groupprivate && sym->value)
   19790            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19791              :                "initializer", sym->name, &sym->declared_at);
   19792              : 
   19793              :   /* Check omp declare target restrictions.  */
   19794      1798032 :   if ((sym->attr.omp_declare_target
   19795      1796524 :        || sym->attr.omp_declare_target_link
   19796      1796475 :        || sym->attr.omp_declare_target_local)
   19797         1605 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19798         1555 :       && sym->attr.flavor == FL_VARIABLE
   19799          694 :       && !sym->attr.save
   19800          206 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19801          206 :       && (!sym->attr.in_common
   19802          192 :           && sym->module == NULL
   19803          102 :           && (sym->ns->proc_name == NULL
   19804          102 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19805           12 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19806            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19807              :                sym->name, &sym->declared_at);
   19808              : 
   19809              :   /* If we have come this far we can apply default-initializers, as
   19810              :      described in 14.7.5, to those variables that have not already
   19811              :      been assigned one.  */
   19812      1798032 :   if (sym->ts.type == BT_DERIVED
   19813       136661 :       && !sym->value
   19814       110569 :       && !sym->attr.allocatable
   19815       107304 :       && !sym->attr.alloc_comp)
   19816              :     {
   19817       107191 :       symbol_attribute *a = &sym->attr;
   19818              : 
   19819       107191 :       if ((!a->save && !a->dummy && !a->pointer
   19820        59119 :            && !a->in_common && !a->use_assoc
   19821        11470 :            && a->referenced
   19822         9014 :            && !((a->function || a->result)
   19823         1874 :                 && (!a->dimension
   19824          178 :                     || sym->ts.u.derived->attr.alloc_comp
   19825          101 :                     || sym->ts.u.derived->attr.pointer_comp))
   19826         7227 :            && !(a->function && sym != sym->result))
   19827        99990 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19828         1552 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19829         8654 :         apply_default_init (sym);
   19830        98537 :       else if (a->function && !a->pointer && !a->allocatable
   19831        21402 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19832              :         /* Default initialization for function results.  */
   19833         2886 :         apply_default_init (sym->result);
   19834        95651 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19835        12263 :                && (sym->ts.u.derived->attr.alloc_comp
   19836        11670 :                    || sym->ts.u.derived->attr.pointer_comp))
   19837              :         /* Mark the result symbol to be referenced, when it has allocatable
   19838              :            components.  */
   19839          652 :         sym->result->attr.referenced = 1;
   19840              :     }
   19841              : 
   19842      1798032 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19843        20610 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19844         1406 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19845         1331 :       && !CLASS_DATA (sym)->attr.class_pointer
   19846         1305 :       && !CLASS_DATA (sym)->attr.allocatable)
   19847          949 :     apply_default_init (sym);
   19848              : 
   19849              :   /* If this symbol has a type-spec, check it.  */
   19850      1798032 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19851       683852 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19852      1456523 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19853              :       return;
   19854              : 
   19855      1798029 :   if (sym->param_list)
   19856         2581 :     resolve_pdt (sym);
   19857              : }
   19858              : 
   19859              : 
   19860         4225 : void gfc_resolve_symbol (gfc_symbol *sym)
   19861              : {
   19862         4225 :   resolve_symbol (sym);
   19863         4225 :   return;
   19864              : }
   19865              : 
   19866              : 
   19867              : /************* Resolve DATA statements *************/
   19868              : 
   19869              : static struct
   19870              : {
   19871              :   gfc_data_value *vnode;
   19872              :   mpz_t left;
   19873              : }
   19874              : values;
   19875              : 
   19876              : 
   19877              : /* Advance the values structure to point to the next value in the data list.  */
   19878              : 
   19879              : static bool
   19880        10928 : next_data_value (void)
   19881              : {
   19882        16696 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19883              :     {
   19884              : 
   19885         8216 :       if (values.vnode->next == NULL)
   19886              :         return false;
   19887              : 
   19888         5768 :       values.vnode = values.vnode->next;
   19889         5768 :       mpz_set (values.left, values.vnode->repeat);
   19890              :     }
   19891              : 
   19892              :   return true;
   19893              : }
   19894              : 
   19895              : 
   19896              : static bool
   19897         3575 : check_data_variable (gfc_data_variable *var, locus *where)
   19898              : {
   19899         3575 :   gfc_expr *e;
   19900         3575 :   mpz_t size;
   19901         3575 :   mpz_t offset;
   19902         3575 :   bool t;
   19903         3575 :   ar_type mark = AR_UNKNOWN;
   19904         3575 :   int i;
   19905         3575 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19906         3575 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19907         3575 :   gfc_ref *ref;
   19908         3575 :   gfc_array_ref *ar;
   19909         3575 :   gfc_symbol *sym;
   19910         3575 :   int has_pointer;
   19911              : 
   19912         3575 :   if (!gfc_resolve_expr (var->expr))
   19913              :     return false;
   19914              : 
   19915         3575 :   ar = NULL;
   19916         3575 :   e = var->expr;
   19917              : 
   19918         3575 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19919            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19920            0 :     e = e->value.function.actual->expr;
   19921              : 
   19922         3575 :   if (e->expr_type != EXPR_VARIABLE)
   19923              :     {
   19924            0 :       gfc_error ("Expecting definable entity near %L", where);
   19925            0 :       return false;
   19926              :     }
   19927              : 
   19928         3575 :   sym = e->symtree->n.sym;
   19929              : 
   19930         3575 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19931              :     {
   19932            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19933              :                  sym->name, &sym->declared_at);
   19934            2 :       return false;
   19935              :     }
   19936              : 
   19937         3573 :   if (e->ref == NULL && sym->as)
   19938              :     {
   19939            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19940              :                  " declaration", sym->name, where);
   19941            1 :       return false;
   19942              :     }
   19943              : 
   19944         3572 :   if (gfc_is_coindexed (e))
   19945              :     {
   19946            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19947              :                  where);
   19948            7 :       return false;
   19949              :     }
   19950              : 
   19951         3565 :   has_pointer = sym->attr.pointer;
   19952              : 
   19953         6006 :   for (ref = e->ref; ref; ref = ref->next)
   19954              :     {
   19955         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19956              :         has_pointer = 1;
   19957              : 
   19958         2419 :       if (has_pointer)
   19959              :         {
   19960           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19961              :             {
   19962            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19963              :                          "be a full array", sym->name, where);
   19964            1 :               return false;
   19965              :             }
   19966              : 
   19967           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19968              :             {
   19969            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19970              :                          "and the corresponding DATA value is not a valid "
   19971              :                          "initial-data-target", where);
   19972            1 :               return false;
   19973              :             }
   19974              :         }
   19975              : 
   19976         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19977              :         {
   19978            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19979              :                      "attribute", ref->u.c.component->name, &e->where);
   19980            1 :           return false;
   19981              :         }
   19982              : 
   19983              :       /* Reject substrings of strings of non-constant length.  */
   19984         2442 :       if (ref->type == REF_SUBSTRING
   19985           73 :           && ref->u.ss.length
   19986           73 :           && ref->u.ss.length->length
   19987         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19988            1 :         goto bad_charlen;
   19989              :     }
   19990              : 
   19991              :   /* Reject strings with deferred length or non-constant length.  */
   19992         3561 :   if (e->ts.type == BT_CHARACTER
   19993         3561 :       && (e->ts.deferred
   19994          380 :           || (e->ts.u.cl->length
   19995          379 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19996            5 :     goto bad_charlen;
   19997              : 
   19998         3556 :   mpz_init_set_si (offset, 0);
   19999              : 
   20000         3556 :   if (e->rank == 0 || has_pointer)
   20001              :     {
   20002         2709 :       mpz_init_set_ui (size, 1);
   20003         2709 :       ref = NULL;
   20004              :     }
   20005              :   else
   20006              :     {
   20007          847 :       ref = e->ref;
   20008              : 
   20009              :       /* Find the array section reference.  */
   20010         1030 :       for (ref = e->ref; ref; ref = ref->next)
   20011              :         {
   20012         1030 :           if (ref->type != REF_ARRAY)
   20013           92 :             continue;
   20014          938 :           if (ref->u.ar.type == AR_ELEMENT)
   20015           91 :             continue;
   20016              :           break;
   20017              :         }
   20018          847 :       gcc_assert (ref);
   20019              : 
   20020              :       /* Set marks according to the reference pattern.  */
   20021          847 :       switch (ref->u.ar.type)
   20022              :         {
   20023              :         case AR_FULL:
   20024              :           mark = AR_FULL;
   20025              :           break;
   20026              : 
   20027          151 :         case AR_SECTION:
   20028          151 :           ar = &ref->u.ar;
   20029              :           /* Get the start position of array section.  */
   20030          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   20031          151 :           mark = AR_SECTION;
   20032          151 :           break;
   20033              : 
   20034            0 :         default:
   20035            0 :           gcc_unreachable ();
   20036              :         }
   20037              : 
   20038          847 :       if (!gfc_array_size (e, &size))
   20039              :         {
   20040            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   20041              :                      where);
   20042            1 :           mpz_clear (offset);
   20043            1 :           return false;
   20044              :         }
   20045              :     }
   20046              : 
   20047         3555 :   t = true;
   20048              : 
   20049        11973 :   while (mpz_cmp_ui (size, 0) > 0)
   20050              :     {
   20051         8481 :       if (!next_data_value ())
   20052              :         {
   20053            1 :           gfc_error ("DATA statement at %L has more variables than values",
   20054              :                      where);
   20055            1 :           t = false;
   20056            1 :           break;
   20057              :         }
   20058              : 
   20059         8480 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   20060         8480 :       if (!t)
   20061              :         break;
   20062              : 
   20063              :       /* If we have more than one element left in the repeat count,
   20064              :          and we have more than one element left in the target variable,
   20065              :          then create a range assignment.  */
   20066              :       /* FIXME: Only done for full arrays for now, since array sections
   20067              :          seem tricky.  */
   20068         8461 :       if (mark == AR_FULL && ref && ref->next == NULL
   20069         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   20070              :         {
   20071          137 :           mpz_t range;
   20072              : 
   20073          137 :           if (mpz_cmp (size, values.left) >= 0)
   20074              :             {
   20075          126 :               mpz_init_set (range, values.left);
   20076          126 :               mpz_sub (size, size, values.left);
   20077          126 :               mpz_set_ui (values.left, 0);
   20078              :             }
   20079              :           else
   20080              :             {
   20081           11 :               mpz_init_set (range, size);
   20082           11 :               mpz_sub (values.left, values.left, size);
   20083           11 :               mpz_set_ui (size, 0);
   20084              :             }
   20085              : 
   20086          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   20087              :                                      offset, &range);
   20088              : 
   20089          137 :           mpz_add (offset, offset, range);
   20090          137 :           mpz_clear (range);
   20091              : 
   20092          137 :           if (!t)
   20093              :             break;
   20094          129 :         }
   20095              : 
   20096              :       /* Assign initial value to symbol.  */
   20097              :       else
   20098              :         {
   20099         8324 :           mpz_sub_ui (values.left, values.left, 1);
   20100         8324 :           mpz_sub_ui (size, size, 1);
   20101              : 
   20102         8324 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   20103              :                                      offset, NULL);
   20104         8324 :           if (!t)
   20105              :             break;
   20106              : 
   20107         8289 :           if (mark == AR_FULL)
   20108         5259 :             mpz_add_ui (offset, offset, 1);
   20109              : 
   20110              :           /* Modify the array section indexes and recalculate the offset
   20111              :              for next element.  */
   20112         3030 :           else if (mark == AR_SECTION)
   20113          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   20114              :         }
   20115              :     }
   20116              : 
   20117         3555 :   if (mark == AR_SECTION)
   20118              :     {
   20119          344 :       for (i = 0; i < ar->dimen; i++)
   20120          194 :         mpz_clear (section_index[i]);
   20121              :     }
   20122              : 
   20123         3555 :   mpz_clear (size);
   20124         3555 :   mpz_clear (offset);
   20125              : 
   20126         3555 :   return t;
   20127              : 
   20128            6 : bad_charlen:
   20129            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   20130              :              &e->where);
   20131            6 :   return false;
   20132              : }
   20133              : 
   20134              : 
   20135              : static bool traverse_data_var (gfc_data_variable *, locus *);
   20136              : 
   20137              : /* Iterate over a list of elements in a DATA statement.  */
   20138              : 
   20139              : static bool
   20140          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   20141              : {
   20142          237 :   mpz_t trip;
   20143          237 :   iterator_stack frame;
   20144          237 :   gfc_expr *e, *start, *end, *step;
   20145          237 :   bool retval = true;
   20146              : 
   20147          237 :   mpz_init (frame.value);
   20148          237 :   mpz_init (trip);
   20149              : 
   20150          237 :   start = gfc_copy_expr (var->iter.start);
   20151          237 :   end = gfc_copy_expr (var->iter.end);
   20152          237 :   step = gfc_copy_expr (var->iter.step);
   20153              : 
   20154          237 :   if (!gfc_simplify_expr (start, 1)
   20155          237 :       || start->expr_type != EXPR_CONSTANT)
   20156              :     {
   20157            0 :       gfc_error ("start of implied-do loop at %L could not be "
   20158              :                  "simplified to a constant value", &start->where);
   20159            0 :       retval = false;
   20160            0 :       goto cleanup;
   20161              :     }
   20162          237 :   if (!gfc_simplify_expr (end, 1)
   20163          237 :       || end->expr_type != EXPR_CONSTANT)
   20164              :     {
   20165            0 :       gfc_error ("end of implied-do loop at %L could not be "
   20166              :                  "simplified to a constant value", &end->where);
   20167            0 :       retval = false;
   20168            0 :       goto cleanup;
   20169              :     }
   20170          237 :   if (!gfc_simplify_expr (step, 1)
   20171          237 :       || step->expr_type != EXPR_CONSTANT)
   20172              :     {
   20173            0 :       gfc_error ("step of implied-do loop at %L could not be "
   20174              :                  "simplified to a constant value", &step->where);
   20175            0 :       retval = false;
   20176            0 :       goto cleanup;
   20177              :     }
   20178          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   20179              :     {
   20180            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   20181              :                  &step->where);
   20182            1 :       retval = false;
   20183            1 :       goto cleanup;
   20184              :     }
   20185              : 
   20186          236 :   mpz_set (trip, end->value.integer);
   20187          236 :   mpz_sub (trip, trip, start->value.integer);
   20188          236 :   mpz_add (trip, trip, step->value.integer);
   20189              : 
   20190          236 :   mpz_div (trip, trip, step->value.integer);
   20191              : 
   20192          236 :   mpz_set (frame.value, start->value.integer);
   20193              : 
   20194          236 :   frame.prev = iter_stack;
   20195          236 :   frame.variable = var->iter.var->symtree;
   20196          236 :   iter_stack = &frame;
   20197              : 
   20198         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   20199              :     {
   20200          905 :       if (!traverse_data_var (var->list, where))
   20201              :         {
   20202           14 :           retval = false;
   20203           14 :           goto cleanup;
   20204              :         }
   20205              : 
   20206          891 :       e = gfc_copy_expr (var->expr);
   20207          891 :       if (!gfc_simplify_expr (e, 1))
   20208              :         {
   20209            0 :           gfc_free_expr (e);
   20210            0 :           retval = false;
   20211            0 :           goto cleanup;
   20212              :         }
   20213              : 
   20214          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   20215              : 
   20216          891 :       mpz_sub_ui (trip, trip, 1);
   20217              :     }
   20218              : 
   20219          222 : cleanup:
   20220          237 :   mpz_clear (frame.value);
   20221          237 :   mpz_clear (trip);
   20222              : 
   20223          237 :   gfc_free_expr (start);
   20224          237 :   gfc_free_expr (end);
   20225          237 :   gfc_free_expr (step);
   20226              : 
   20227          237 :   iter_stack = frame.prev;
   20228          237 :   return retval;
   20229              : }
   20230              : 
   20231              : 
   20232              : /* Type resolve variables in the variable list of a DATA statement.  */
   20233              : 
   20234              : static bool
   20235         3436 : traverse_data_var (gfc_data_variable *var, locus *where)
   20236              : {
   20237         3436 :   bool t;
   20238              : 
   20239         7150 :   for (; var; var = var->next)
   20240              :     {
   20241         3812 :       if (var->expr == NULL)
   20242          237 :         t = traverse_data_list (var, where);
   20243              :       else
   20244         3575 :         t = check_data_variable (var, where);
   20245              : 
   20246         3812 :       if (!t)
   20247              :         return false;
   20248              :     }
   20249              : 
   20250              :   return true;
   20251              : }
   20252              : 
   20253              : 
   20254              : /* Resolve the expressions and iterators associated with a data statement.
   20255              :    This is separate from the assignment checking because data lists should
   20256              :    only be resolved once.  */
   20257              : 
   20258              : static bool
   20259         2686 : resolve_data_variables (gfc_data_variable *d)
   20260              : {
   20261         5743 :   for (; d; d = d->next)
   20262              :     {
   20263         3062 :       if (d->list == NULL)
   20264              :         {
   20265         2909 :           if (!gfc_resolve_expr (d->expr))
   20266              :             return false;
   20267              :         }
   20268              :       else
   20269              :         {
   20270          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   20271              :             return false;
   20272              : 
   20273          150 :           if (!resolve_data_variables (d->list))
   20274              :             return false;
   20275              :         }
   20276              :     }
   20277              : 
   20278              :   return true;
   20279              : }
   20280              : 
   20281              : 
   20282              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   20283              :    the value list into static variables, and then recursively traversing the
   20284              :    variables list, expanding iterators and such.  */
   20285              : 
   20286              : static void
   20287         2536 : resolve_data (gfc_data *d)
   20288              : {
   20289         2536 :   gfc_data_value *v;
   20290              : 
   20291         2536 :   if (!resolve_data_variables (d->var))
   20292              :     return;
   20293              : 
   20294        10851 :   for (v = d->value; v; v = v->next)
   20295         8320 :     if (v->expr && !gfc_resolve_expr (v->expr))
   20296              :       return;
   20297              : 
   20298         2531 :   values.vnode = d->value;
   20299         2531 :   if (d->value == NULL)
   20300            0 :     mpz_set_ui (values.left, 0);
   20301              :   else
   20302         2531 :     mpz_set (values.left, d->value->repeat);
   20303              : 
   20304         2531 :   if (!traverse_data_var (d->var, &d->where))
   20305              :     return;
   20306              : 
   20307              :   /* At this point, we better not have any values left.  */
   20308              : 
   20309         2447 :   if (next_data_value ())
   20310            0 :     gfc_error ("DATA statement at %L has more values than variables",
   20311              :                &d->where);
   20312              : }
   20313              : 
   20314              : 
   20315              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   20316              :    accessed by host or use association, is a dummy argument to a pure function,
   20317              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   20318              :    is storage associated with any such variable, shall not be used in the
   20319              :    following contexts: (clients of this function).  */
   20320              : 
   20321              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   20322              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   20323              :    problem.  */
   20324              : bool
   20325        59998 : gfc_impure_variable (gfc_symbol *sym)
   20326              : {
   20327        59998 :   gfc_symbol *proc;
   20328        59998 :   gfc_namespace *ns;
   20329              : 
   20330        59998 :   if (sym->attr.use_assoc || sym->attr.in_common)
   20331              :     return 1;
   20332              : 
   20333              :   /* The namespace of a module procedure interface holds the arguments and
   20334              :      symbols, and so the symbol namespace can be different to that of the
   20335              :      procedure.  */
   20336        59367 :   if (sym->ns != gfc_current_ns
   20337         6439 :       && gfc_current_ns->proc_name->abr_modproc_decl
   20338           48 :       && sym->ns->proc_name->attr.function
   20339           12 :       && sym->attr.result
   20340           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   20341              :     return 0;
   20342              : 
   20343              :   /* Check if the symbol's ns is inside the pure procedure.  */
   20344        64335 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   20345              :     {
   20346        64051 :       if (ns == sym->ns)
   20347              :         break;
   20348         6761 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   20349         5579 :           && !(sym->attr.function || sym->attr.result))
   20350              :         return 1;
   20351              :     }
   20352              : 
   20353        57574 :   proc = sym->ns->proc_name;
   20354        57574 :   if (sym->attr.dummy
   20355         6452 :       && !sym->attr.value
   20356         6318 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   20357         6111 :           || proc->attr.function))
   20358          725 :     return 1;
   20359              : 
   20360              :   /* TODO: Sort out what can be storage associated, if anything, and include
   20361              :      it here.  In principle equivalences should be scanned but it does not
   20362              :      seem to be possible to storage associate an impure variable this way.  */
   20363              :   return 0;
   20364              : }
   20365              : 
   20366              : 
   20367              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   20368              :    current namespace is inside a pure procedure.  */
   20369              : 
   20370              : bool
   20371      2440406 : gfc_pure (gfc_symbol *sym)
   20372              : {
   20373      2440406 :   symbol_attribute attr;
   20374      2440406 :   gfc_namespace *ns;
   20375              : 
   20376      2440406 :   if (sym == NULL)
   20377              :     {
   20378              :       /* Check if the current namespace or one of its parents
   20379              :         belongs to a pure procedure.  */
   20380      3283118 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   20381              :         {
   20382      1939871 :           sym = ns->proc_name;
   20383      1939871 :           if (sym == NULL)
   20384              :             return 0;
   20385      1938730 :           attr = sym->attr;
   20386      1938730 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   20387              :             return 1;
   20388              :         }
   20389              :       return 0;
   20390              :     }
   20391              : 
   20392      1088345 :   attr = sym->attr;
   20393              : 
   20394      1088345 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   20395              : }
   20396              : 
   20397              : 
   20398              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   20399              :    checks if the current namespace is implicitly pure.  Note that this
   20400              :    function returns false for a PURE procedure.  */
   20401              : 
   20402              : bool
   20403       746458 : gfc_implicit_pure (gfc_symbol *sym)
   20404              : {
   20405       746458 :   gfc_namespace *ns;
   20406              : 
   20407       746458 :   if (sym == NULL)
   20408              :     {
   20409              :       /* Check if the current procedure is implicit_pure.  Walk up
   20410              :          the procedure list until we find a procedure.  */
   20411      1030796 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   20412              :         {
   20413       734423 :           sym = ns->proc_name;
   20414       734423 :           if (sym == NULL)
   20415              :             return 0;
   20416              : 
   20417       734350 :           if (sym->attr.flavor == FL_PROCEDURE)
   20418              :             break;
   20419              :         }
   20420              :     }
   20421              : 
   20422       450009 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   20423       776346 :     && !sym->attr.pure;
   20424              : }
   20425              : 
   20426              : 
   20427              : void
   20428       439171 : gfc_unset_implicit_pure (gfc_symbol *sym)
   20429              : {
   20430       439171 :   gfc_namespace *ns;
   20431              : 
   20432       439171 :   if (sym == NULL)
   20433              :     {
   20434              :       /* Check if the current procedure is implicit_pure.  Walk up
   20435              :          the procedure list until we find a procedure.  */
   20436       718577 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   20437              :         {
   20438       444460 :           sym = ns->proc_name;
   20439       444460 :           if (sym == NULL)
   20440              :             return;
   20441              : 
   20442       443627 :           if (sym->attr.flavor == FL_PROCEDURE)
   20443              :             break;
   20444              :         }
   20445              :     }
   20446              : 
   20447       438338 :   if (sym->attr.flavor == FL_PROCEDURE)
   20448       155407 :     sym->attr.implicit_pure = 0;
   20449              :   else
   20450       282931 :     sym->attr.pure = 0;
   20451              : }
   20452              : 
   20453              : 
   20454              : /* Test whether the current procedure is elemental or not.  */
   20455              : 
   20456              : bool
   20457      1469274 : gfc_elemental (gfc_symbol *sym)
   20458              : {
   20459      1469274 :   symbol_attribute attr;
   20460              : 
   20461      1469274 :   if (sym == NULL)
   20462            0 :     sym = gfc_current_ns->proc_name;
   20463            0 :   if (sym == NULL)
   20464              :     return 0;
   20465      1469274 :   attr = sym->attr;
   20466              : 
   20467      1469274 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   20468              : }
   20469              : 
   20470              : 
   20471              : /* Warn about unused labels.  */
   20472              : 
   20473              : static void
   20474         4890 : warn_unused_fortran_label (gfc_st_label *label)
   20475              : {
   20476         4924 :   if (label == NULL)
   20477              :     return;
   20478              : 
   20479           35 :   warn_unused_fortran_label (label->left);
   20480              : 
   20481           35 :   if (label->defined == ST_LABEL_UNKNOWN)
   20482              :     return;
   20483              : 
   20484           34 :   switch (label->referenced)
   20485              :     {
   20486            2 :     case ST_LABEL_UNKNOWN:
   20487            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   20488              :                    label->value, &label->where);
   20489            2 :       break;
   20490              : 
   20491            4 :     case ST_LABEL_BAD_TARGET:
   20492            4 :       gfc_warning (OPT_Wunused_label,
   20493              :                    "Label %d at %L defined but cannot be used",
   20494              :                    label->value, &label->where);
   20495            4 :       break;
   20496              : 
   20497              :     default:
   20498              :       break;
   20499              :     }
   20500              : 
   20501           34 :   warn_unused_fortran_label (label->right);
   20502              : }
   20503              : 
   20504              : 
   20505              : /* Returns the sequence type of a symbol or sequence.  */
   20506              : 
   20507              : static seq_type
   20508         1076 : sequence_type (gfc_typespec ts)
   20509              : {
   20510         1076 :   seq_type result;
   20511         1076 :   gfc_component *c;
   20512              : 
   20513         1076 :   switch (ts.type)
   20514              :   {
   20515           49 :     case BT_DERIVED:
   20516              : 
   20517           49 :       if (ts.u.derived->components == NULL)
   20518              :         return SEQ_NONDEFAULT;
   20519              : 
   20520           49 :       result = sequence_type (ts.u.derived->components->ts);
   20521          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   20522           67 :         if (sequence_type (c->ts) != result)
   20523              :           return SEQ_MIXED;
   20524              : 
   20525              :       return result;
   20526              : 
   20527          129 :     case BT_CHARACTER:
   20528          129 :       if (ts.kind != gfc_default_character_kind)
   20529            0 :           return SEQ_NONDEFAULT;
   20530              : 
   20531              :       return SEQ_CHARACTER;
   20532              : 
   20533          240 :     case BT_INTEGER:
   20534          240 :       if (ts.kind != gfc_default_integer_kind)
   20535           25 :           return SEQ_NONDEFAULT;
   20536              : 
   20537              :       return SEQ_NUMERIC;
   20538              : 
   20539          559 :     case BT_REAL:
   20540          559 :       if (!(ts.kind == gfc_default_real_kind
   20541          269 :             || ts.kind == gfc_default_double_kind))
   20542            0 :           return SEQ_NONDEFAULT;
   20543              : 
   20544              :       return SEQ_NUMERIC;
   20545              : 
   20546           81 :     case BT_COMPLEX:
   20547           81 :       if (ts.kind != gfc_default_complex_kind)
   20548           48 :           return SEQ_NONDEFAULT;
   20549              : 
   20550              :       return SEQ_NUMERIC;
   20551              : 
   20552           17 :     case BT_LOGICAL:
   20553           17 :       if (ts.kind != gfc_default_logical_kind)
   20554            0 :           return SEQ_NONDEFAULT;
   20555              : 
   20556              :       return SEQ_NUMERIC;
   20557              : 
   20558              :     default:
   20559              :       return SEQ_NONDEFAULT;
   20560              :   }
   20561              : }
   20562              : 
   20563              : 
   20564              : /* Resolve derived type EQUIVALENCE object.  */
   20565              : 
   20566              : static bool
   20567           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   20568              : {
   20569           80 :   gfc_component *c = derived->components;
   20570              : 
   20571           80 :   if (!derived)
   20572              :     return true;
   20573              : 
   20574              :   /* Shall not be an object of nonsequence derived type.  */
   20575           80 :   if (!derived->attr.sequence)
   20576              :     {
   20577            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   20578              :                  "attribute to be an EQUIVALENCE object", sym->name,
   20579              :                  &e->where);
   20580            0 :       return false;
   20581              :     }
   20582              : 
   20583              :   /* Shall not have allocatable components.  */
   20584           80 :   if (derived->attr.alloc_comp)
   20585              :     {
   20586            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   20587              :                  "components to be an EQUIVALENCE object",sym->name,
   20588              :                  &e->where);
   20589            1 :       return false;
   20590              :     }
   20591              : 
   20592           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   20593              :     {
   20594            1 :       gfc_error ("Derived type variable %qs at %L with default "
   20595              :                  "initialization cannot be in EQUIVALENCE with a variable "
   20596              :                  "in COMMON", sym->name, &e->where);
   20597            1 :       return false;
   20598              :     }
   20599              : 
   20600          245 :   for (; c ; c = c->next)
   20601              :     {
   20602          167 :       if (gfc_bt_struct (c->ts.type)
   20603          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   20604              :         return false;
   20605              : 
   20606              :       /* Shall not be an object of sequence derived type containing a pointer
   20607              :          in the structure.  */
   20608          167 :       if (c->attr.pointer)
   20609              :         {
   20610            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   20611              :                      "component(s) cannot be an EQUIVALENCE object",
   20612              :                      sym->name, &e->where);
   20613            0 :           return false;
   20614              :         }
   20615              :     }
   20616              :   return true;
   20617              : }
   20618              : 
   20619              : 
   20620              : /* Resolve equivalence object.
   20621              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   20622              :    an allocatable array, an object of nonsequence derived type, an object of
   20623              :    sequence derived type containing a pointer at any level of component
   20624              :    selection, an automatic object, a function name, an entry name, a result
   20625              :    name, a named constant, a structure component, or a subobject of any of
   20626              :    the preceding objects.  A substring shall not have length zero.  A
   20627              :    derived type shall not have components with default initialization nor
   20628              :    shall two objects of an equivalence group be initialized.
   20629              :    Either all or none of the objects shall have an protected attribute.
   20630              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   20631              :    are implemented here.  */
   20632              : 
   20633              : static void
   20634         1565 : resolve_equivalence (gfc_equiv *eq)
   20635              : {
   20636         1565 :   gfc_symbol *sym;
   20637         1565 :   gfc_symbol *first_sym;
   20638         1565 :   gfc_expr *e;
   20639         1565 :   gfc_ref *r;
   20640         1565 :   locus *last_where = NULL;
   20641         1565 :   seq_type eq_type, last_eq_type;
   20642         1565 :   gfc_typespec *last_ts;
   20643         1565 :   int object, cnt_protected;
   20644         1565 :   const char *msg;
   20645              : 
   20646         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   20647              : 
   20648         1565 :   first_sym = eq->expr->symtree->n.sym;
   20649              : 
   20650         1565 :   cnt_protected = 0;
   20651              : 
   20652         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   20653              :     {
   20654         3171 :       e = eq->expr;
   20655              : 
   20656         3171 :       e->ts = e->symtree->n.sym->ts;
   20657              :       /* match_varspec might not know yet if it is seeing
   20658              :          array reference or substring reference, as it doesn't
   20659              :          know the types.  */
   20660         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   20661              :         {
   20662         2152 :           gfc_ref *ref = e->ref;
   20663         2152 :           sym = e->symtree->n.sym;
   20664              : 
   20665         2152 :           if (sym->attr.dimension)
   20666              :             {
   20667         1855 :               ref->u.ar.as = sym->as;
   20668         1855 :               ref = ref->next;
   20669              :             }
   20670              : 
   20671              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   20672         2152 :           if (e->ts.type == BT_CHARACTER
   20673          592 :               && ref
   20674          371 :               && ref->type == REF_ARRAY
   20675          371 :               && ref->u.ar.dimen == 1
   20676          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   20677          371 :               && ref->u.ar.stride[0] == NULL)
   20678              :             {
   20679          370 :               gfc_expr *start = ref->u.ar.start[0];
   20680          370 :               gfc_expr *end = ref->u.ar.end[0];
   20681          370 :               void *mem = NULL;
   20682              : 
   20683              :               /* Optimize away the (:) reference.  */
   20684          370 :               if (start == NULL && end == NULL)
   20685              :                 {
   20686            9 :                   if (e->ref == ref)
   20687            0 :                     e->ref = ref->next;
   20688              :                   else
   20689            9 :                     e->ref->next = ref->next;
   20690              :                   mem = ref;
   20691              :                 }
   20692              :               else
   20693              :                 {
   20694          361 :                   ref->type = REF_SUBSTRING;
   20695          361 :                   if (start == NULL)
   20696            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   20697              :                                               NULL, 1);
   20698          361 :                   ref->u.ss.start = start;
   20699          361 :                   if (end == NULL && e->ts.u.cl)
   20700           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   20701          361 :                   ref->u.ss.end = end;
   20702          361 :                   ref->u.ss.length = e->ts.u.cl;
   20703          361 :                   e->ts.u.cl = NULL;
   20704              :                 }
   20705          370 :               ref = ref->next;
   20706          370 :               free (mem);
   20707              :             }
   20708              : 
   20709              :           /* Any further ref is an error.  */
   20710         1930 :           if (ref)
   20711              :             {
   20712            1 :               gcc_assert (ref->type == REF_ARRAY);
   20713            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   20714              :                          &ref->u.ar.where);
   20715            1 :               continue;
   20716              :             }
   20717              :         }
   20718              : 
   20719         3170 :       if (!gfc_resolve_expr (e))
   20720            2 :         continue;
   20721              : 
   20722         3168 :       sym = e->symtree->n.sym;
   20723              : 
   20724         3168 :       if (sym->attr.is_protected)
   20725            2 :         cnt_protected++;
   20726         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   20727              :         {
   20728            2 :               gfc_error ("Either all or none of the objects in the "
   20729              :                          "EQUIVALENCE set at %L shall have the "
   20730              :                          "PROTECTED attribute",
   20731              :                          &e->where);
   20732            2 :               break;
   20733              :         }
   20734              : 
   20735              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   20736         3166 :       if (sym->ns->proc_name
   20737         3150 :           && sym->ns->proc_name->attr.pure
   20738            7 :           && sym->attr.in_common)
   20739              :         {
   20740              :           /* Need to check for symbols that may have entered the pure
   20741              :              procedure via a USE statement.  */
   20742            7 :           bool saw_sym = false;
   20743            7 :           if (sym->ns->use_stmts)
   20744              :             {
   20745            6 :               gfc_use_rename *r;
   20746           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   20747            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   20748              :             }
   20749              :           else
   20750              :             saw_sym = true;
   20751              : 
   20752            6 :           if (saw_sym)
   20753            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   20754              :                        "EQUIVALENCE object in the pure procedure %qs",
   20755              :                        sym->name, &e->where, sym->ns->proc_name->name);
   20756              :           break;
   20757              :         }
   20758              : 
   20759              :       /* Shall not be a named constant.  */
   20760         3159 :       if (e->expr_type == EXPR_CONSTANT)
   20761              :         {
   20762            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   20763              :                      "object", sym->name, &e->where);
   20764            0 :           continue;
   20765              :         }
   20766              : 
   20767         3161 :       if (e->ts.type == BT_DERIVED
   20768         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   20769            2 :         continue;
   20770              : 
   20771              :       /* Check that the types correspond correctly:
   20772              :          Note 5.28:
   20773              :          A numeric sequence structure may be equivalenced to another sequence
   20774              :          structure, an object of default integer type, default real type, double
   20775              :          precision real type, default logical type such that components of the
   20776              :          structure ultimately only become associated to objects of the same
   20777              :          kind. A character sequence structure may be equivalenced to an object
   20778              :          of default character kind or another character sequence structure.
   20779              :          Other objects may be equivalenced only to objects of the same type and
   20780              :          kind parameters.  */
   20781              : 
   20782              :       /* Identical types are unconditionally OK.  */
   20783         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   20784         2677 :         goto identical_types;
   20785              : 
   20786          480 :       last_eq_type = sequence_type (*last_ts);
   20787          480 :       eq_type = sequence_type (sym->ts);
   20788              : 
   20789              :       /* Since the pair of objects is not of the same type, mixed or
   20790              :          non-default sequences can be rejected.  */
   20791              : 
   20792          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20793              :                "statement at %L with different type objects");
   20794          481 :       if ((object ==2
   20795          480 :            && last_eq_type == SEQ_MIXED
   20796            7 :            && last_where
   20797            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20798          486 :           || (eq_type == SEQ_MIXED
   20799            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20800            1 :         continue;
   20801              : 
   20802          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20803              :                "statement at %L with objects of different type");
   20804          483 :       if ((object ==2
   20805          479 :            && last_eq_type == SEQ_NONDEFAULT
   20806           50 :            && last_where
   20807           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20808          525 :           || (eq_type == SEQ_NONDEFAULT
   20809           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20810            4 :         continue;
   20811              : 
   20812          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20813              :                "EQUIVALENCE statement at %L");
   20814          479 :       if (last_eq_type == SEQ_CHARACTER
   20815          475 :           && eq_type != SEQ_CHARACTER
   20816          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20817            4 :                 continue;
   20818              : 
   20819          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20820              :                "EQUIVALENCE statement at %L");
   20821          473 :       if (last_eq_type == SEQ_NUMERIC
   20822          471 :           && eq_type != SEQ_NUMERIC
   20823          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20824            2 :                 continue;
   20825              : 
   20826         3146 : identical_types:
   20827              : 
   20828         3146 :       last_ts =&sym->ts;
   20829         3146 :       last_where = &e->where;
   20830              : 
   20831         3146 :       if (!e->ref)
   20832         1003 :         continue;
   20833              : 
   20834              :       /* Shall not be an automatic array.  */
   20835         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20836              :         {
   20837            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20838              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20839            3 :           continue;
   20840              :         }
   20841              : 
   20842         2140 :       r = e->ref;
   20843         4326 :       while (r)
   20844              :         {
   20845              :           /* Shall not be a structure component.  */
   20846         2187 :           if (r->type == REF_COMPONENT)
   20847              :             {
   20848            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20849              :                          "EQUIVALENCE object",
   20850            0 :                          r->u.c.component->name, &e->where);
   20851            0 :               break;
   20852              :             }
   20853              : 
   20854              :           /* A substring shall not have length zero.  */
   20855         2187 :           if (r->type == REF_SUBSTRING)
   20856              :             {
   20857          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20858              :                 {
   20859            1 :                   gfc_error ("Substring at %L has length zero",
   20860              :                              &r->u.ss.start->where);
   20861            1 :                   break;
   20862              :                 }
   20863              :             }
   20864         2186 :           r = r->next;
   20865              :         }
   20866              :     }
   20867         1565 : }
   20868              : 
   20869              : 
   20870              : /* Function called by resolve_fntype to flag other symbols used in the
   20871              :    length type parameter specification of function results.  */
   20872              : 
   20873              : static bool
   20874         4334 : flag_fn_result_spec (gfc_expr *expr,
   20875              :                      gfc_symbol *sym,
   20876              :                      int *f ATTRIBUTE_UNUSED)
   20877              : {
   20878         4334 :   gfc_namespace *ns;
   20879         4334 :   gfc_symbol *s;
   20880              : 
   20881         4334 :   if (expr->expr_type == EXPR_VARIABLE)
   20882              :     {
   20883         1419 :       s = expr->symtree->n.sym;
   20884         2231 :       for (ns = s->ns; ns; ns = ns->parent)
   20885         2231 :         if (!ns->parent)
   20886              :           break;
   20887              : 
   20888         1419 :       if (sym == s)
   20889              :         {
   20890            1 :           gfc_error ("Self reference in character length expression "
   20891              :                      "for %qs at %L", sym->name, &expr->where);
   20892            1 :           return true;
   20893              :         }
   20894              : 
   20895         1418 :       if (!s->fn_result_spec
   20896         1418 :           && s->attr.flavor == FL_PARAMETER)
   20897              :         {
   20898              :           /* Function contained in a module.... */
   20899           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20900              :             {
   20901           32 :               gfc_symtree *st;
   20902           32 :               s->fn_result_spec = 1;
   20903              :               /* Make sure that this symbol is translated as a module
   20904              :                  variable.  */
   20905           32 :               st = gfc_get_unique_symtree (ns);
   20906           32 :               st->n.sym = s;
   20907           32 :               s->refs++;
   20908           32 :             }
   20909              :           /* ... which is use associated and called.  */
   20910           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20911            0 :                         ||
   20912              :                   /* External function matched with an interface.  */
   20913            0 :                   (s->ns->proc_name
   20914            0 :                    && ((s->ns == ns
   20915            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20916            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20917            0 :                    && s->ns->proc_name->attr.function))
   20918           31 :             s->fn_result_spec = 1;
   20919              :         }
   20920              :     }
   20921              :   return false;
   20922              : }
   20923              : 
   20924              : 
   20925              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20926              : 
   20927              : static void
   20928       372899 : resolve_fntype (gfc_namespace *ns)
   20929              : {
   20930       372899 :   gfc_entry_list *el;
   20931       372899 :   gfc_symbol *sym;
   20932              : 
   20933       372899 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20934              :     return;
   20935              : 
   20936              :   /* If there are any entries, ns->proc_name is the entry master
   20937              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20938       193918 :   if (ns->entries)
   20939          596 :     sym = ns->entries->sym;
   20940              :   else
   20941              :     sym = ns->proc_name;
   20942       193918 :   if (sym->result == sym
   20943       157356 :       && sym->ts.type == BT_UNKNOWN
   20944            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20945       193922 :       && !sym->attr.untyped)
   20946              :     {
   20947            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20948              :                  sym->name, &sym->declared_at);
   20949            3 :       sym->attr.untyped = 1;
   20950              :     }
   20951              : 
   20952        14373 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20953         1989 :       && !sym->attr.contained
   20954          337 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20955       193918 :       && gfc_check_symbol_access (sym))
   20956              :     {
   20957            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20958              :                       "%L of PRIVATE type %qs", sym->name,
   20959            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20960              :     }
   20961              : 
   20962       193918 :     if (ns->entries)
   20963         1253 :     for (el = ns->entries->next; el; el = el->next)
   20964              :       {
   20965          657 :         if (el->sym->result == el->sym
   20966          445 :             && el->sym->ts.type == BT_UNKNOWN
   20967            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20968          659 :             && !el->sym->attr.untyped)
   20969              :           {
   20970            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20971              :                        el->sym->name, &el->sym->declared_at);
   20972            2 :             el->sym->attr.untyped = 1;
   20973              :           }
   20974              :       }
   20975              : 
   20976       193918 :   if (sym->ts.type == BT_CHARACTER
   20977         7231 :       && sym->ts.u.cl->length
   20978         1962 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20979         1957 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20980              : }
   20981              : 
   20982              : 
   20983              : /* 12.3.2.1.1 Defined operators.  */
   20984              : 
   20985              : static bool
   20986          508 : check_uop_procedure (gfc_symbol *sym, locus where)
   20987              : {
   20988          508 :   gfc_formal_arglist *formal;
   20989              : 
   20990          508 :   if (!sym->attr.function)
   20991              :     {
   20992            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20993              :                  sym->name, &where);
   20994            4 :       return false;
   20995              :     }
   20996              : 
   20997          504 :   if (sym->ts.type == BT_CHARACTER
   20998           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20999            2 :       && !(sym->result && ((sym->result->ts.u.cl
   21000            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   21001              :     {
   21002            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   21003              :                  "character length", sym->name, &where);
   21004            2 :       return false;
   21005              :     }
   21006              : 
   21007          502 :   formal = gfc_sym_get_dummy_args (sym);
   21008          502 :   if (!formal || !formal->sym)
   21009              :     {
   21010            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   21011              :                  "one argument", sym->name, &where);
   21012            1 :       return false;
   21013              :     }
   21014              : 
   21015          501 :   if (formal->sym->attr.intent != INTENT_IN)
   21016              :     {
   21017            0 :       gfc_error ("First argument of operator interface at %L must be "
   21018              :                  "INTENT(IN)", &where);
   21019            0 :       return false;
   21020              :     }
   21021              : 
   21022          501 :   if (formal->sym->attr.optional)
   21023              :     {
   21024            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   21025              :                  "optional", &where);
   21026            0 :       return false;
   21027              :     }
   21028              : 
   21029          501 :   formal = formal->next;
   21030          501 :   if (!formal || !formal->sym)
   21031              :     return true;
   21032              : 
   21033          297 :   if (formal->sym->attr.intent != INTENT_IN)
   21034              :     {
   21035            0 :       gfc_error ("Second argument of operator interface at %L must be "
   21036              :                  "INTENT(IN)", &where);
   21037            0 :       return false;
   21038              :     }
   21039              : 
   21040          297 :   if (formal->sym->attr.optional)
   21041              :     {
   21042            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   21043              :                  "optional", &where);
   21044            1 :       return false;
   21045              :     }
   21046              : 
   21047          296 :   if (formal->next)
   21048              :     {
   21049            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   21050              :                  "arguments", &where);
   21051            2 :       return false;
   21052              :     }
   21053              : 
   21054              :   return true;
   21055              : }
   21056              : 
   21057              : static void
   21058       373705 : gfc_resolve_uops (gfc_symtree *symtree)
   21059              : {
   21060       373705 :   gfc_interface *itr;
   21061              : 
   21062       373705 :   if (symtree == NULL)
   21063              :     return;
   21064              : 
   21065          403 :   gfc_resolve_uops (symtree->left);
   21066          403 :   gfc_resolve_uops (symtree->right);
   21067              : 
   21068          798 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   21069          395 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   21070              : }
   21071              : 
   21072              : /* Mark all lhs in assignment statement as used.  It is better to put this into
   21073              :    its own function rather than into the different switch cases in
   21074              :    gfc_resolve_code.  */
   21075              : 
   21076              : static void
   21077       716562 : mark_lhs_assignments_set (gfc_code *code)
   21078              : {
   21079              : 
   21080      1891513 :   for (; code; code = code->next)
   21081              :     {
   21082      1174951 :       gfc_expr *lvalue = code->expr1, *rvalue = code->expr2;
   21083              : 
   21084      1174951 :       if (lvalue == NULL || lvalue->symtree == NULL || rvalue == NULL)
   21085       869911 :         continue;
   21086              : 
   21087       305040 :       switch (code->op)
   21088              :         {
   21089       292926 :         case EXEC_ASSIGN:
   21090       292926 :           if (gfc_is_reallocatable_lhs (lvalue) && lvalue->rank == rvalue->rank)
   21091         8666 :             gfc_lvalue_allocated_at (lvalue->symtree->n.sym, &lvalue->where);
   21092              : 
   21093       303442 :           gcc_fallthrough();
   21094       303442 :         case EXEC_POINTER_ASSIGN:
   21095       303442 :           gfc_expr_set_at (lvalue, &rvalue->where, VALUE_VARDEF);
   21096              :         default:
   21097              :           break;
   21098              :         }
   21099              :     }
   21100       716562 : }
   21101              : 
   21102              : /* Examine all of the expressions associated with a program unit,
   21103              :    assign types to all intermediate expressions, make sure that all
   21104              :    assignments are to compatible types and figure out which names
   21105              :    refer to which functions or subroutines.  It doesn't check code
   21106              :    block, which is handled by gfc_resolve_code.  */
   21107              : 
   21108              : static void
   21109       375621 : resolve_types (gfc_namespace *ns)
   21110              : {
   21111       375621 :   gfc_namespace *n;
   21112       375621 :   gfc_charlen *cl;
   21113       375621 :   gfc_data *d;
   21114       375621 :   gfc_equiv *eq;
   21115       375621 :   gfc_namespace* old_ns = gfc_current_ns;
   21116       375621 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   21117              : 
   21118       375621 :   if (ns->types_resolved)
   21119              :     return;
   21120              : 
   21121              :   /* Check that all IMPLICIT types are ok.  */
   21122       372900 :   if (!ns->seen_implicit_none)
   21123              :     {
   21124              :       unsigned letter;
   21125      9393031 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   21126      9045141 :         if (ns->set_flag[letter]
   21127      9045141 :             && !resolve_typespec_used (&ns->default_type[letter],
   21128              :                                        &ns->implicit_loc[letter], NULL))
   21129              :           return;
   21130              :     }
   21131              : 
   21132       372899 :   gfc_current_ns = ns;
   21133              : 
   21134       372899 :   resolve_entries (ns);
   21135              : 
   21136       372899 :   resolve_common_vars (&ns->blank_common, false);
   21137       372899 :   resolve_common_blocks (ns->common_root);
   21138              : 
   21139       372899 :   resolve_contained_functions (ns);
   21140              : 
   21141       372899 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   21142       319267 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   21143       211653 :     gfc_resolve_formal_arglist (ns->proc_name);
   21144              : 
   21145       372899 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   21146              : 
   21147       470613 :   for (cl = ns->cl_list; cl; cl = cl->next)
   21148        97714 :     resolve_charlen (cl);
   21149              : 
   21150       372899 :   gfc_traverse_ns (ns, resolve_symbol);
   21151              : 
   21152       372899 :   resolve_fntype (ns);
   21153              : 
   21154       425159 :   for (n = ns->contained; n; n = n->sibling)
   21155              :     {
   21156              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   21157        52260 :       if (gfc_pure (ns->proc_name)
   21158            5 :           && !gfc_pure (n->proc_name)
   21159        52260 :           && !n->proc_name->attr.artificial)
   21160            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   21161              :                    "also be PURE", n->proc_name->name,
   21162              :                    &n->proc_name->declared_at);
   21163              : 
   21164        52260 :       resolve_types (n);
   21165              :     }
   21166              : 
   21167       372899 :   forall_flag = 0;
   21168       372899 :   gfc_do_concurrent_flag = 0;
   21169       372899 :   gfc_check_interfaces (ns);
   21170              : 
   21171       372899 :   gfc_traverse_ns (ns, resolve_values);
   21172              : 
   21173       372899 :   if (ns->save_all || (!flag_automatic && !recursive))
   21174          315 :     gfc_save_all (ns);
   21175              : 
   21176       372899 :   iter_stack = NULL;
   21177       375435 :   for (d = ns->data; d; d = d->next)
   21178         2536 :     resolve_data (d);
   21179              : 
   21180       372899 :   iter_stack = NULL;
   21181       372899 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   21182              : 
   21183       372899 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   21184              : 
   21185       374464 :   for (eq = ns->equiv; eq; eq = eq->next)
   21186         1565 :     resolve_equivalence (eq);
   21187              : 
   21188              :   /* Warn about unused labels.  */
   21189       372899 :   if (warn_unused_label)
   21190         4855 :     warn_unused_fortran_label (ns->st_labels);
   21191              : 
   21192       372899 :   gfc_resolve_uops (ns->uop_root);
   21193              : 
   21194       372899 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   21195              : 
   21196       372899 :   gfc_resolve_omp_declare (ns);
   21197              : 
   21198       372899 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   21199              : 
   21200       372899 :   gfc_resolve_omp_udms (ns->omp_udm_root);
   21201              : 
   21202       372899 :   ns->types_resolved = 1;
   21203              : 
   21204       372899 :   gfc_current_ns = old_ns;
   21205              : }
   21206              : 
   21207              : 
   21208              : /* Call gfc_resolve_code recursively.  */
   21209              : 
   21210              : static void
   21211       375683 : resolve_codes (gfc_namespace *ns)
   21212              : {
   21213       375683 :   gfc_namespace *n;
   21214       375683 :   bitmap_obstack old_obstack;
   21215              : 
   21216       375683 :   if (ns->resolved == 1)
   21217        15974 :     return;
   21218              : 
   21219       412031 :   for (n = ns->contained; n; n = n->sibling)
   21220        52322 :     resolve_codes (n);
   21221              : 
   21222       359709 :   gfc_current_ns = ns;
   21223              : 
   21224              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   21225       359709 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   21226       346304 :     cs_base = NULL;
   21227              : 
   21228              :   /* Set to an out of range value.  */
   21229       359709 :   current_entry_id = -1;
   21230              : 
   21231       359709 :   old_obstack = labels_obstack;
   21232       359709 :   bitmap_obstack_initialize (&labels_obstack);
   21233              : 
   21234       359709 :   gfc_resolve_oacc_declare (ns);
   21235       359709 :   gfc_resolve_oacc_routines (ns);
   21236       359709 :   gfc_resolve_omp_local_vars (ns);
   21237       359709 :   if (ns->omp_allocate)
   21238           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   21239       359709 :   gfc_resolve_code (ns->code, ns);
   21240              : 
   21241       359708 :   bitmap_obstack_release (&labels_obstack);
   21242       359708 :   labels_obstack = old_obstack;
   21243              : }
   21244              : 
   21245              : /* Return true if the value of a variable can be considered used, either
   21246              :    through the value_used flag or because it is a suitable dummy argument.  */
   21247              : 
   21248              : static bool
   21249          454 : var_value_is_used (gfc_symbol *sym)
   21250              : {
   21251          454 :   if (sym->attr.value_used != VALUE_UNUSED)
   21252              :     return true;
   21253              : 
   21254          107 :   if (!sym->attr.dummy)
   21255              :     return false;
   21256              : 
   21257           90 :   if (sym->attr.value)
   21258              :     return false;
   21259              : 
   21260           90 :   switch (sym->attr.intent)
   21261              :     {
   21262              :     case INTENT_UNKNOWN:
   21263              :     case INTENT_INOUT:
   21264              :     case INTENT_OUT:
   21265              :       return true;
   21266              : 
   21267              :     case INTENT_IN:
   21268              :     default:
   21269              :       return false;
   21270              :     }
   21271              : }
   21272              : 
   21273              : /* Similar, see if the variable could have gotten its value from somewhere.  */
   21274              : 
   21275              : static bool
   21276         2383 : var_value_is_set (gfc_symbol *sym)
   21277              : {
   21278         2383 :   if (sym->attr.value_set != VALUE_UNSET)
   21279              :     return true;
   21280              : 
   21281         1684 :   if (sym->value)
   21282              :     return true;
   21283              : 
   21284         1669 :   if (sym->ts.type == BT_DERIVED
   21285         1669 :       && gfc_has_default_initializer (sym->ts.u.derived))
   21286              :     return true;
   21287              : 
   21288         1669 :   if (!sym->attr.dummy)
   21289              :     return false;
   21290              : 
   21291         1624 :   if (sym->attr.value)
   21292              :     return true;
   21293              : 
   21294         1591 :   if (sym->attr.intent == INTENT_OUT)
   21295            3 :     return false;
   21296              : 
   21297              :   return true;
   21298              : }
   21299              : 
   21300              : /* Callback function to catch set but never used variables.  */
   21301              : 
   21302              : static void
   21303        34751 : find_unused_vs_set (gfc_symbol *sym)
   21304              : {
   21305        34751 :   symbol_attribute *attr = &sym->attr;
   21306              : 
   21307        34751 :   if (attr->flavor != FL_VARIABLE)
   21308              :     return;
   21309              : 
   21310              :   /* Do not warn about anything too far out of the ordinary.  This might be
   21311              :      tightened later.  */
   21312         8655 :   if (attr->in_common || attr->in_equivalence || attr->artificial
   21313         8249 :       || attr->cray_pointer || attr->cray_pointee || attr->associate_var
   21314         8246 :       || attr->target || attr->fe_temp || attr->omp_declare_target
   21315         8243 :       || attr->omp_declare_target_link || attr->omp_declare_target_local
   21316         8234 :       || attr->omp_declare_target_indirect || attr->oacc_declare_create
   21317         8234 :       || attr->oacc_declare_copyin || attr->oacc_declare_deviceptr
   21318         8234 :       || attr->oacc_declare_device_resident || attr->oacc_declare_link
   21319         8234 :       || attr->result || attr->warning_emitted || attr->use_assoc
   21320         5671 :       || attr->volatile_ || attr->asynchronous || !attr->referenced)
   21321              :     return;
   21322              : 
   21323         2451 :   if (attr->host_assoc && attr->access != ACCESS_PRIVATE)
   21324              :     return;
   21325              : 
   21326              :   /* There is no allocation in sight, but the variable is used anyway.  This
   21327              :      might be hidden behind PRESENT, but issue a warning nonetheless.  If
   21328              :      people complain, we might want to make this to an extra option to be
   21329              :      included with -Wextra.  */
   21330              : 
   21331         2385 :   if (warn_undefined_vars && attr->allocatable && !attr->allocated
   21332         2437 :       && var_value_is_used (sym))
   21333              :     {
   21334            3 :       if (attr->dummy && attr->intent == INTENT_OUT)
   21335              :         {
   21336            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated INTENT(OUT) variable "
   21337              :                        "%qs referenced at %L", sym->name, &sym->other_loc);
   21338            0 :           attr->warning_emitted = 1;
   21339            0 :           return;
   21340              :         }
   21341              : 
   21342            3 :       if (!attr->dummy)
   21343              :         {
   21344            2 :           gfc_warning (OPT_Wundefined_vars, "Unallocated variable %qs "
   21345              :                        "referenced at %L", sym->name, &sym->other_loc);
   21346            2 :           attr->warning_emitted = 1;
   21347            2 :           return;
   21348              :         }
   21349              :     }
   21350              : 
   21351         2426 :   if (warn_undefined_vars && !var_value_is_set (sym))
   21352              :     {
   21353              :       /* Warn about variables which have been allocated and used, but never
   21354              :          set.  */
   21355           48 :       if (attr->allocated && sym->attr.value_used > VALUE_MAYBE_USED)
   21356              :         {
   21357            3 :           switch (sym->attr.value_used)
   21358              :             {
   21359            1 :             case VALUE_INTENT_IN:
   21360            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   21361              :                            "undefined to INTENT(IN) argument at %L", sym->name,
   21362              :                            &sym->other_loc);
   21363            1 :               break;
   21364              : 
   21365            1 :             case VALUE_VALUE_ARG:
   21366            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   21367              :                            "undefined to VALUE argument at %L", sym->name,
   21368              :                            &sym->other_loc);
   21369            1 :               break;
   21370            1 :             case VALUE_USED:
   21371            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated undefined variable "
   21372              :                            "%qs used at %L", sym->name, &sym->other_loc);
   21373            1 :               break;
   21374            0 :             default:
   21375            0 :               gfc_internal_error ("Wrong value_set");
   21376            3 :               break;
   21377              :             }
   21378            3 :           attr->warning_emitted = 1;
   21379            3 :           return;
   21380              :         }
   21381              : 
   21382              :       /* Similar, when undefined variables are passed to INTENT(IN), VALUE
   21383              :          arguments or are used in general.  */
   21384              : 
   21385           45 :       if (attr->value_used == VALUE_INTENT_IN)
   21386              :         {
   21387            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   21388              :                        "to INTENT(IN) argument at %L", sym->name, &sym->other_loc);
   21389            1 :           attr->warning_emitted = 1;
   21390            1 :           return;
   21391              :         }
   21392           44 :       else if (attr->value_used == VALUE_VALUE_ARG)
   21393              :         {
   21394            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   21395              :                        "to VALUE argument at %L", sym->name, &sym->other_loc);
   21396            1 :           attr->warning_emitted = 1;
   21397            1 :           return;
   21398              :         }
   21399           43 :       else if (attr->value_used == VALUE_USED)
   21400              :         {
   21401            9 :           if (attr->dummy && attr->intent == INTENT_OUT)
   21402            1 :             gfc_warning (OPT_Wundefined_vars, "Undefined INTENT(OUT) variable %qs "
   21403              :                          "used at %L", sym->name, &sym->other_loc);
   21404              :           else
   21405            8 :             gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs used at "
   21406              :                          "%L", sym->name, &sym->other_loc);
   21407              : 
   21408            9 :           attr->warning_emitted = 1;
   21409            9 :           return;
   21410              :         }
   21411              : 
   21412              :       /* PR 28004 - warn about INTENT(OUT) variables that are never set.  If
   21413              :          the variable or a component are allocatable, do not warn since this is
   21414              :          a frequent shortcut for deallocation.  */
   21415              : 
   21416           34 :       if (sym->attr.dummy && sym->attr.intent == INTENT_OUT
   21417            2 :           && !(attr->allocatable || attr->alloc_comp))
   21418              :         {
   21419            0 :           gfc_warning (OPT_Wundefined_vars, "INTENT(OUT) variable %qs  "
   21420              :                        "declared at %L is not assigned a value", sym->name,
   21421              :                        &sym->declared_at);
   21422            0 :           attr->warning_emitted = 1;
   21423            0 :           return;
   21424              :         }
   21425              :     }
   21426              : 
   21427              :   /* Warn for unused but defined variables.  */
   21428              : 
   21429         2412 :   if (warn_unused_but_set_variable)
   21430              :     {
   21431         2304 :       if (attr->value_set == VALUE_VARDEF && !var_value_is_used (sym))
   21432              :         {
   21433            7 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs defined at "
   21434              :                        "%L but never used", sym->name, &sym->other_loc);
   21435            7 :           attr->warning_emitted = 1;
   21436            7 :           return;
   21437              :         }
   21438         2297 :       if (attr->allocatable && !var_value_is_used (sym))
   21439              :         {
   21440            2 :           if (attr->allocated == ALLOCATED_ALLOCATE_STMT)
   21441              :             {
   21442            1 :               gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs "
   21443              :                            "allocated at %L but never used", sym->name,
   21444              :                            &sym->extra_loc);
   21445            1 :               attr->warning_emitted = 1;
   21446            1 :               return;
   21447              :             }
   21448            1 :           else if (attr->allocated == ALLOCATED_ARG)
   21449              :             {
   21450            1 :               gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs maybe "
   21451              :                            "allocated as argument at %L but never used",
   21452              :                            sym->name, &sym->extra_loc);
   21453            1 :               attr->warning_emitted = 1;
   21454            1 :               return;
   21455              :             }
   21456              :         }
   21457              :     }
   21458              : 
   21459              :   /* -Wunused-intent-out and -Wunused-read are enabled with -Wextra, so
   21460              :      check for these conditions at the end.  If one of the warnings
   21461              :      with -Wall triggered, we do not want to issue a different warrning
   21462              :      for the same variable if the user supplies -Wall -Wextra instead
   21463              :      of only -Wall.  */
   21464              : 
   21465           39 :   if (warn_unused_intent_out && attr->value_set == VALUE_INTENT_OUT
   21466         2408 :       && !var_value_is_used (sym))
   21467              :     {
   21468            1 :       gfc_warning (OPT_Wunused_intent_out, "Variable %qs passed to "
   21469              :                    "INTENT(OUT) argument at %L but value never used",
   21470              :                    sym->name, &sym->other_loc);
   21471            1 :       attr->warning_emitted = 1;
   21472            1 :       return;
   21473              :     }
   21474              : 
   21475         2402 :   if (warn_unused_read && attr->value_set == VALUE_READ && !var_value_is_used (sym))
   21476              :     {
   21477            1 :       gfc_warning (OPT_Wunused_read, "Variable %qs read at %L but never "
   21478              :                    "used", sym->name, &sym->other_loc);
   21479            1 :       attr->warning_emitted = 1;
   21480            1 :       return;
   21481              :     }
   21482              : }
   21483              : 
   21484              : /* Run warn_unused_vs_set over a namespace recursively.  */
   21485              : 
   21486              : static void
   21487         4876 : warn_unused_vs_set (gfc_namespace *ns)
   21488              : {
   21489         4876 :   gfc_traverse_ns (ns, find_unused_vs_set);
   21490              : 
   21491         5399 :   for (gfc_namespace *n = ns->contained; n; n = n->sibling)
   21492          523 :     warn_unused_vs_set (n);
   21493         4876 : }
   21494              : 
   21495              : /* This function is called after a complete program unit has been compiled.
   21496              :    Its purpose is to examine all of the expressions associated with a program
   21497              :    unit, assign types to all intermediate expressions, make sure that all
   21498              :    assignments are to compatible types and figure out which names refer to
   21499              :    which functions or subroutines.  */
   21500              : 
   21501              : void
   21502       328458 : gfc_resolve (gfc_namespace *ns, gfc_association_list *a)
   21503              : {
   21504       328458 :   gfc_namespace *old_ns;
   21505       328458 :   code_stack *old_cs_base;
   21506       328458 :   struct gfc_omp_saved_state old_omp_state;
   21507              : 
   21508       328458 :   if (ns->resolved)
   21509         5097 :     return;
   21510              : 
   21511       323361 :   ns->resolved = -1;
   21512       323361 :   old_ns = gfc_current_ns;
   21513       323361 :   old_cs_base = cs_base;
   21514              : 
   21515              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   21516              :      body, we should clear any state associated to it, so that say NS's
   21517              :      DO loops are not interpreted as OpenMP loops.  */
   21518       323361 :   if (!ns->construct_entities)
   21519       309956 :     gfc_omp_save_and_clear_state (&old_omp_state);
   21520              : 
   21521       323361 :   resolve_types (ns);
   21522       323361 :   component_assignment_level = 0;
   21523       323361 :   resolve_codes (ns);
   21524       323360 :   mark_assoc_used (a);
   21525              : 
   21526       323360 :   if (warn_unused_but_set_variable || warn_unused_intent_out
   21527       319065 :       || warn_unused_read || warn_undefined_vars)
   21528              :     {
   21529         4377 :       int error_count;
   21530         4377 :       gfc_get_errors (NULL, &error_count);
   21531         4377 :       if (error_count == 0)
   21532         4353 :         warn_unused_vs_set (ns);
   21533              :     }
   21534              : 
   21535       323360 :   if (ns->omp_assumes)
   21536           16 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   21537              : 
   21538       323360 :   gfc_current_ns = old_ns;
   21539       323360 :   cs_base = old_cs_base;
   21540       323360 :   ns->resolved = 1;
   21541              : 
   21542       323360 :   gfc_run_passes (ns);
   21543              : 
   21544       323360 :   if (!ns->construct_entities)
   21545       309955 :     gfc_omp_restore_state (&old_omp_state);
   21546              : }
        

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