LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.4 % 9920 9270
Test Date: 2026-07-11 15:47:05 Functions: 99.6 % 254 253
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          464 : entry_dummy_seen_p (gfc_symbol *sym)
     100              : {
     101          464 :   gfc_entry_list *entry;
     102          464 :   gfc_formal_arglist *formal;
     103              : 
     104          464 :   gcc_checking_assert (sym->attr.dummy && sym->ns == gfc_current_ns);
     105              : 
     106          464 :   for (entry = gfc_current_ns->entries;
     107          471 :        entry && entry->id <= current_entry_id;
     108            7 :        entry = entry->next)
     109          765 :     for (formal = entry->sym->formal; formal; formal = formal->next)
     110          758 :       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        52862 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        57271 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         4667 :       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      1569079 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1569079 :   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            5 :       return false;
     150              :     }
     151              : 
     152              :   return true;
     153              : }
     154              : 
     155              : 
     156              : static bool
     157         5644 : check_proc_interface (gfc_symbol *ifc, locus *where)
     158              : {
     159              :   /* Several checks for F08:C1216.  */
     160         5644 :   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         5642 :   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         5638 :   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         5634 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     187         5634 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     188           17 :     ifc->attr.intrinsic = 1;
     189         5634 :   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         5631 :   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         2116 : resolve_procedure_interface (gfc_symbol *sym)
     211              : {
     212         2116 :   gfc_symbol *ifc = sym->ts.interface;
     213              : 
     214         2116 :   if (!ifc)
     215              :     return true;
     216              : 
     217         1956 :   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         1954 :   if (!check_proc_interface (ifc, &sym->declared_at))
     224              :     return false;
     225              : 
     226         1945 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     227              :     {
     228              :       /* Resolve interface and copy attributes.  */
     229         1666 :       resolve_symbol (ifc);
     230         1666 :       if (ifc->attr.intrinsic)
     231           14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     232              : 
     233         1666 :       if (ifc->result)
     234              :         {
     235          779 :           sym->ts = ifc->result->ts;
     236          779 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     237          779 :           sym->attr.pointer = ifc->result->attr.pointer;
     238          779 :           sym->attr.dimension = ifc->result->attr.dimension;
     239          779 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     240          779 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     241          779 :           sym->result = sym;
     242              :         }
     243              :       else
     244              :         {
     245          887 :           sym->ts = ifc->ts;
     246          887 :           sym->attr.allocatable = ifc->attr.allocatable;
     247          887 :           sym->attr.pointer = ifc->attr.pointer;
     248          887 :           sym->attr.dimension = ifc->attr.dimension;
     249          887 :           sym->attr.class_ok = ifc->attr.class_ok;
     250          887 :           sym->as = gfc_copy_array_spec (ifc->as);
     251              :         }
     252         1666 :       sym->ts.interface = ifc;
     253         1666 :       sym->attr.function = ifc->attr.function;
     254         1666 :       sym->attr.subroutine = ifc->attr.subroutine;
     255              : 
     256         1666 :       sym->attr.pure = ifc->attr.pure;
     257         1666 :       sym->attr.elemental = ifc->attr.elemental;
     258         1666 :       sym->attr.contiguous = ifc->attr.contiguous;
     259         1666 :       sym->attr.recursive = ifc->attr.recursive;
     260         1666 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     261         1666 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     262         1666 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     263              :       /* Copy char length.  */
     264         1666 :       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       537939 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       537939 :   gfc_formal_arglist *f;
     290       537939 :   gfc_symbol *sym;
     291       537939 :   bool saved_specification_expr;
     292       537939 :   int i;
     293              : 
     294       537939 :   if (proc->result != NULL)
     295       339446 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       537939 :   if (gfc_elemental (proc)
     300       375435 :       || sym->attr.pointer || sym->attr.allocatable
     301       901276 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       176932 :       proc->attr.always_explicit = 1;
     304       176932 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       537939 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       537939 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1391757 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       853820 :       gfc_array_spec *as;
     313       853820 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       853820 :       sym = f->sym;
     316              : 
     317       853820 :       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          599 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     332       854248 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       853649 :       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       853647 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          891 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       853647 :       if (sym->attr.subroutine || sym->attr.external)
     347              :         {
     348          901 :           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       852746 :           if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
     354         3663 :               && (!sym->attr.function || sym->result == sym))
     355         3625 :             gfc_set_default_type (sym, 1, sym->ns);
     356              :         }
     357              : 
     358       853647 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       867722 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       853647 :       saved_specification_expr = specification_expr;
     362       853647 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       853647 :       specification_expr = true;
     364       853647 :       specification_expr_symbol = sym;
     365       853647 :       gfc_resolve_array_spec (as, 0);
     366       853647 :       specification_expr = saved_specification_expr;
     367       853647 :       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       853647 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        12426 :           && ((sym->ts.type != BT_CLASS
     374        11300 :                && !(sym->attr.pointer || sym->attr.allocatable))
     375         5382 :               || (sym->ts.type == BT_CLASS
     376         1126 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     377          926 :                        || CLASS_DATA (sym)->attr.allocatable)))
     378         7555 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         7554 :           as->type = AS_ASSUMED_SHAPE;
     381        17553 :           for (i = 0; i < as->rank; i++)
     382         9999 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       131434 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       117562 :           || (as && as->type == AS_ASSUMED_RANK)
     387       802148 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       791977 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        11860 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11377 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10473 :                   || CLASS_DATA (sym)->attr.target))
     392       790590 :           || sym->attr.optional)
     393              :         {
     394        77997 :           proc->attr.always_explicit = 1;
     395        77997 :           if (proc->result)
     396        36167 :             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       853647 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        52065 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       853647 :       if (gfc_pure (proc))
     406              :         {
     407       327805 :           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       327776 :           else if (!sym->attr.pointer)
     418              :             {
     419       327762 :               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       327762 :               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       327804 :           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       853645 :       if (proc->attr.implicit_pure)
     458              :         {
     459        25135 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          331 :               if (!gfc_pure (sym))
     462          299 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        24804 :           else if (!sym->attr.pointer)
     465              :             {
     466        24023 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2747 :                   && !sym->value)
     468         2747 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        24023 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4209 :                   && !sym->value)
     472         4209 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       853645 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       302204 :           if (sym->attr.codimension
     480       302203 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     481          965 :                   && 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       302201 :           if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     489          963 :                           && 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       302199 :           if (sym->attr.allocatable
     497       302198 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     498          962 :                   && 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       302197 :           if (sym->attr.pointer
     507       302196 :               || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
     508          961 :                   && 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       302195 :           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       302193 :           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       853632 :       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       537939 :   if (sym)
     562       537847 :     sym->formal_resolved = 1;
     563       537939 :   gfc_current_ns = orig_current_ns;
     564       537939 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1883042 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1883042 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       339241 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       336910 :   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       353613 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       353613 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        37549 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        37549 :   bool t;
     598              : 
     599        37549 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        37549 :       && sym->ns->parent
     601         1444 :       && sym->ns->parent->proc_name
     602         1444 :       && 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        37549 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        11021 :       || sym->attr.entry_master)
     611        26717 :     return;
     612              : 
     613        10832 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        10832 :   if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
     618              :     {
     619           57 :       t = gfc_set_default_type (sym->result, 0, ns);
     620              : 
     621           57 :       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        10832 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1204 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1204 :       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         1491 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     668              : {
     669         1491 :   gfc_formal_arglist *f, *new_arglist;
     670         1491 :   gfc_symbol *new_sym;
     671              : 
     672         2644 :   for (; new_args != NULL; new_args = new_args->next)
     673              :     {
     674         1153 :       new_sym = new_args->sym;
     675              :       /* See if this arg is already in the formal argument list.  */
     676         2186 :       for (f = proc->formal; f; f = f->next)
     677              :         {
     678         1481 :           if (new_sym == f->sym)
     679              :             break;
     680              :         }
     681              : 
     682         1153 :       if (f)
     683          448 :         continue;
     684              : 
     685              :       /* Add a new argument.  Argument order is not important.  */
     686          705 :       new_arglist = gfc_get_formal_arglist ();
     687          705 :       new_arglist->sym = new_sym;
     688          705 :       new_arglist->next = proc->formal;
     689          705 :       proc->formal  = new_arglist;
     690              :     }
     691         1491 : }
     692              : 
     693              : 
     694              : /* Flag the arguments that are not present in all entries.  */
     695              : 
     696              : static void
     697         1491 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
     698              : {
     699         1491 :   gfc_formal_arglist *f, *head;
     700         1491 :   head = new_args;
     701              : 
     702         3086 :   for (f = proc->formal; f; f = f->next)
     703              :     {
     704         1595 :       if (f->sym == NULL)
     705           36 :         continue;
     706              : 
     707         2738 :       for (new_args = head; new_args; new_args = new_args->next)
     708              :         {
     709         2287 :           if (new_args->sym == f->sym)
     710              :             break;
     711              :         }
     712              : 
     713         1559 :       if (new_args)
     714         1108 :         continue;
     715              : 
     716          451 :       f->sym->attr.not_always_present = 1;
     717              :     }
     718         1491 : }
     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       390655 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       390655 :   gfc_namespace *old_ns;
     729       390655 :   gfc_code *c;
     730       390655 :   gfc_symbol *proc;
     731       390655 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       390655 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       390655 :   static int master_count = 0;
     735              : 
     736       390655 :   if (ns->proc_name == NULL)
     737       389952 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       390606 :   if (!ns->entries)
     742              :     return;
     743              : 
     744              :   /* We may already have resolved alternate entry points.  */
     745          954 :   if (ns->proc_name->attr.entry_master)
     746              :     return;
     747              : 
     748              :   /* If this isn't a procedure something has gone horribly wrong.  */
     749          703 :   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
     750              : 
     751              :   /* Remember the current namespace.  */
     752          703 :   old_ns = gfc_current_ns;
     753              : 
     754          703 :   gfc_current_ns = ns;
     755              : 
     756              :   /* Add the main entry point to the list of entry points.  */
     757          703 :   el = gfc_get_entry_list ();
     758          703 :   el->sym = ns->proc_name;
     759          703 :   el->id = 0;
     760          703 :   el->next = ns->entries;
     761          703 :   ns->entries = el;
     762          703 :   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          703 :   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         1491 :   for (el = el->next; el; el = el->next)
     777          788 :     if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
     778            0 :           && el->sym->attr.mod_proc)
     779            0 :       el->sym->ns = ns;
     780          703 :   el = ns->entries;
     781              : 
     782              :   /* Add an entry statement for it.  */
     783          703 :   c = gfc_get_code (EXEC_ENTRY);
     784          703 :   c->ext.entry = el;
     785          703 :   c->next = ns->code;
     786          703 :   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          703 :   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
     793          703 :             master_count++, ns->proc_name->name);
     794          703 :   gfc_get_ha_symbol (name, &proc);
     795          703 :   gcc_assert (proc != NULL);
     796              : 
     797          703 :   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
     798          703 :   if (ns->proc_name->attr.subroutine)
     799          107 :     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          703 :   proc->attr.access = ACCESS_PRIVATE;
     967          703 :   proc->attr.entry_master = 1;
     968              : 
     969              :   /* Merge all the entry point arguments.  */
     970         2194 :   for (el = ns->entries; el; el = el->next)
     971         1491 :     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         2194 :   for (el = ns->entries; el; el = el->next)
     976         1491 :     check_argument_lists (proc, el->sym->formal);
     977              : 
     978              :   /* Use the master function for the function body.  */
     979          703 :   ns->proc_name = proc;
     980              : 
     981              :   /* Finalize the new symbols.  */
     982          703 :   gfc_commit_symbols ();
     983              : 
     984              :   /* Restore the original namespace.  */
     985          703 :   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       355590 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       355590 :   gfc_symbol *csym = common_block->head;
     998       355590 :   gfc_gsymbol *gsym;
     999              : 
    1000       361641 :   for (; csym; csym = csym->common_next)
    1001              :     {
    1002         6051 :       gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
    1003         6051 :       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         6051 :       if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
    1018              :         {
    1019         5778 :           gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
    1020         5778 :           gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
    1021              :                           &common_block->where);
    1022              :         }
    1023              : 
    1024         6051 :       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         6051 :       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         6051 :       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         6051 :       if (csym->ts.type != BT_DERIVED)
    1051         6004 :         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       355590 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       354143 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       354143 :   gfc_symbol *sym = NULL;
    1077       354143 :   gfc_gsymbol * gsym;
    1078              : 
    1079       354143 :   if (common_root == NULL)
    1080       354021 :     return;
    1081              : 
    1082         1977 :   if (common_root->left)
    1083          257 :     resolve_common_blocks (common_root->left);
    1084         1977 :   if (common_root->right)
    1085          273 :     resolve_common_blocks (common_root->right);
    1086              : 
    1087         1977 :   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         1977 :   if (!common_root->n.common->binding_label
    1093         1977 :       || gfc_notification_std (GFC_STD_F2008))
    1094              :     {
    1095         3810 :       gsym = gfc_find_gsymbol (gfc_gsym_root,
    1096         1905 :                                common_root->n.common->name);
    1097              : 
    1098          820 :       if (gsym && gfc_notification_std (GFC_STD_F2008)
    1099           14 :           && gsym->type == GSYM_COMMON
    1100         1918 :           && ((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         1899 :       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         1085 :       if (!gsym)
    1136              :         {
    1137         1085 :           gsym = gfc_get_gsymbol (common_root->n.common->name, false);
    1138         1085 :           gsym->type = GSYM_COMMON;
    1139         1085 :           gsym->where = common_root->n.common->where;
    1140         1085 :           gsym->defined = 1;
    1141              :         }
    1142         1898 :       gsym->used = 1;
    1143              :     }
    1144              : 
    1145         1970 :   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         1969 :   gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
    1168         1969 :   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       353613 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       353613 :   gfc_namespace *child;
    1208       353613 :   gfc_entry_list *el;
    1209              : 
    1210       353613 :   resolve_formal_arglists (ns);
    1211              : 
    1212       390655 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        37042 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        37042 :       resolve_contained_fntype (child->proc_name, child);
    1219        37549 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       353613 : }
    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          308 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
    1236              : {
    1237          308 :   param = gfc_get_actual_arglist ();
    1238          308 :   if (!param_list)
    1239          252 :     param_list = param_tail = param;
    1240              :   else
    1241              :     {
    1242           56 :       param_tail->next = param;
    1243           56 :       param_tail = param_tail->next;
    1244              :     }
    1245              : 
    1246          308 :   param_tail->name = c->name;
    1247          308 :   if (expr)
    1248          308 :     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          288 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
    1267              :                      gfc_symbol *derived)
    1268              : {
    1269          288 :   gfc_constructor *cons = NULL;
    1270          288 :   gfc_component *comp;
    1271          288 :   bool t = true;
    1272              : 
    1273          288 :   if (expr && expr->expr_type == EXPR_STRUCTURE)
    1274          252 :     cons = gfc_constructor_first (expr->value.constructor);
    1275           36 :   else if (constr)
    1276           36 :     cons = *constr;
    1277          288 :   gcc_assert (cons);
    1278              : 
    1279          288 :   comp = derived->components;
    1280              : 
    1281          880 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1282              :     {
    1283          592 :       if (cons->expr
    1284          592 :           && cons->expr->expr_type == EXPR_STRUCTURE
    1285            0 :           && comp->ts.type == BT_DERIVED)
    1286              :         {
    1287            0 :           t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
    1288            0 :           if (!t)
    1289              :             return t;
    1290              :         }
    1291          592 :       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          556 :      else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
    1298          308 :                && derived->attr.pdt_template)
    1299              :         {
    1300          308 :           t = get_pdt_spec_expr (comp, cons->expr);
    1301          308 :           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        62984 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        62984 :   gfc_constructor *cons;
    1321        62984 :   gfc_component *comp;
    1322        62984 :   bool t;
    1323        62984 :   symbol_attribute a;
    1324              : 
    1325        62984 :   t = true;
    1326              : 
    1327        62984 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        60086 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        59936 :         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        60086 :       if (expr->ts.u.derived->attr.pdt_template)
    1337              :         {
    1338          252 :           param_list = NULL;
    1339          252 :           t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
    1340          252 :           if (!t)
    1341              :             return t;
    1342          252 :           gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
    1343              : 
    1344          252 :           expr->param_list = gfc_copy_actual_arglist (param_list);
    1345              : 
    1346          252 :           if (param_list)
    1347          252 :             gfc_free_actual_arglist (param_list);
    1348              : 
    1349          252 :           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        62984 :   if (expr->ref)
    1358          160 :     comp = expr->ref->u.c.sym->components;
    1359        62824 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        62822 :            && expr->ts.u.derived)
    1362        62822 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        62982 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       210913 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       147933 :       int rank;
    1371              : 
    1372       147933 :       if (!cons->expr)
    1373         9830 :         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       138103 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       138088 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       138088 :       rank = comp->as ? comp->as->rank : 0;
    1388       138088 :       if (comp->ts.type == BT_CLASS
    1389         1789 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1390         1788 :           && CLASS_DATA (comp)->as)
    1391          537 :         rank = CLASS_DATA (comp)->as->rank;
    1392              : 
    1393       138088 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          228 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       138088 :       if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
    1397          527 :           && (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       241105 :       if (!comp->attr.proc_pointer &&
    1409       103017 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        12688 :           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         9309 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3379 :           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         3377 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3314 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3314 :               if (t)
    1431       138088 :                 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.  */
    1439       138088 :       if (cons->expr->ts.type == BT_CHARACTER
    1440         3914 :           && comp->ts.type == BT_CHARACTER
    1441         3888 :           && comp->ts.u.cl && comp->ts.u.cl->length
    1442         2504 :           && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1443         2469 :           && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
    1444          926 :           && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1445          926 :           && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
    1446          926 :           && comp->ts.u.cl->length->ts.type == BT_INTEGER
    1447          926 :           && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
    1448          926 :                       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       138088 :       if (cons->expr->expr_type == EXPR_NULL
    1494        41680 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        20751 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1496         1136 :                || (comp->ts.type == BT_CLASS
    1497         1134 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1498          917 :                        || 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       138088 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        15783 :           gfc_symbol *s2 = NULL;
    1511        15783 :           gfc_component *c2;
    1512        15783 :           const char *name;
    1513        15783 :           char err[200];
    1514              : 
    1515        15783 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        15783 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        15771 :           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        15771 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        15351 :               s2 = cons->expr->symtree->n.sym;
    1529        15351 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        15363 :           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       138086 :       if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
    1544         2270 :           && comp->as && !comp->attr.allocatable && !comp->attr.pointer
    1545         1526 :           && !comp->attr.pdt_array)
    1546              :         {
    1547         1279 :           mpz_t len;
    1548         1279 :           mpz_init (len);
    1549         2651 :           for (int n = 0; n < rank; n++)
    1550              :             {
    1551         1377 :               if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
    1552         1372 :                   || 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         1372 :                 };
    1560         1372 :               if (cons->expr->shape == NULL)
    1561           12 :                 continue;
    1562         1360 :               mpz_set_ui (len, 1);
    1563         1360 :               mpz_add (len, len, comp->as->upper[n]->value.integer);
    1564         1360 :               mpz_sub (len, len, comp->as->lower[n]->value.integer);
    1565         1360 :               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         1279 :           mpz_clear (len);
    1577              :         }
    1578              : 
    1579       138086 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        22387 :           || cons->expr->expr_type == EXPR_NULL)
    1581       127792 :         continue;
    1582              : 
    1583        10294 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        10294 :       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        10294 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596        10226 :           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        10226 :           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        10294 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        20587 :           && 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        10294 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        10294 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        10257 :                         || 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        10294 :       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       748297 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       748297 :   symbol_attribute a;
    1649              : 
    1650       748297 :   a = sym->attr;
    1651              : 
    1652       748297 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       633887 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       625111 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       625109 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       625055 :       || a.asynchronous || a.codimension || a.subroutine)
    1659        95433 :     return 1;
    1660              : 
    1661              :   return 0;
    1662              : }
    1663              : 
    1664              : 
    1665              : /* Determine if a symbol is generic or not.  */
    1666              : 
    1667              : static int
    1668       415462 : generic_sym (gfc_symbol *sym)
    1669              : {
    1670       415462 :   gfc_symbol *s;
    1671              : 
    1672       415462 :   if (sym->attr.generic ||
    1673       386107 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1674        30425 :     return 1;
    1675              : 
    1676       385037 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1677              :     return 0;
    1678              : 
    1679        77939 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1680              : 
    1681        77939 :   if (s != NULL)
    1682              :     {
    1683          135 :       if (s == sym)
    1684              :         return 0;
    1685              :       else
    1686          134 :         return generic_sym (s);
    1687              :     }
    1688              : 
    1689              :   return 0;
    1690              : }
    1691              : 
    1692              : 
    1693              : /* Determine if a symbol is specific or not.  */
    1694              : 
    1695              : static int
    1696       384949 : specific_sym (gfc_symbol *sym)
    1697              : {
    1698       384949 :   gfc_symbol *s;
    1699              : 
    1700       384949 :   if (sym->attr.if_source == IFSRC_IFBODY
    1701       373558 :       || sym->attr.proc == PROC_MODULE
    1702              :       || sym->attr.proc == PROC_INTERNAL
    1703              :       || sym->attr.proc == PROC_ST_FUNCTION
    1704       296712 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1705       680930 :       || sym->attr.external)
    1706        91377 :     return 1;
    1707              : 
    1708       293572 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1709              :     return 0;
    1710              : 
    1711        77837 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1712              : 
    1713        77837 :   return (s == NULL) ? 0 : specific_sym (s);
    1714              : }
    1715              : 
    1716              : 
    1717              : /* Figure out if the procedure is specific, generic or unknown.  */
    1718              : 
    1719              : enum proc_type
    1720              : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
    1721              : 
    1722              : static proc_type
    1723       415179 : procedure_kind (gfc_symbol *sym)
    1724              : {
    1725       415179 :   if (generic_sym (sym))
    1726              :     return PTYPE_GENERIC;
    1727              : 
    1728       384900 :   if (specific_sym (sym))
    1729        91377 :     return PTYPE_SPECIFIC;
    1730              : 
    1731              :   return PTYPE_UNKNOWN;
    1732              : }
    1733              : 
    1734              : /* Check references to assumed size arrays.  The flag need_full_assumed_size
    1735              :    is nonzero when matching actual arguments.  */
    1736              : 
    1737              : static int need_full_assumed_size = 0;
    1738              : 
    1739              : static bool
    1740      1433037 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1741              : {
    1742      1433037 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1743              :       return false;
    1744              : 
    1745              :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1746              :      What should it be?  */
    1747         3800 :   if (e->ref
    1748         3798 :       && e->ref->u.ar.as
    1749         3797 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1750         3302 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1751         3302 :       && (e->ref->u.ar.type == AR_FULL))
    1752              :     {
    1753           25 :       gfc_error ("The upper bound in the last dimension must "
    1754              :                  "appear in the reference to the assumed size "
    1755              :                  "array %qs at %L", sym->name, &e->where);
    1756           25 :       return true;
    1757              :     }
    1758              :   return false;
    1759              : }
    1760              : 
    1761              : 
    1762              : /* Look for bad assumed size array references in argument expressions
    1763              :   of elemental and array valued intrinsic procedures.  Since this is
    1764              :   called from procedure resolution functions, it only recurses at
    1765              :   operators.  */
    1766              : 
    1767              : static bool
    1768       230761 : resolve_assumed_size_actual (gfc_expr *e)
    1769              : {
    1770       230761 :   if (e == NULL)
    1771              :    return false;
    1772              : 
    1773       230194 :   switch (e->expr_type)
    1774              :     {
    1775       110973 :     case EXPR_VARIABLE:
    1776       110973 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1777              :         return true;
    1778              :       break;
    1779              : 
    1780        48891 :     case EXPR_OP:
    1781        48891 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1782        48891 :           || resolve_assumed_size_actual (e->value.op.op2))
    1783            0 :         return true;
    1784              :       break;
    1785              : 
    1786              :     default:
    1787              :       break;
    1788              :     }
    1789              :   return false;
    1790              : }
    1791              : 
    1792              : 
    1793              : /* Check a generic procedure, passed as an actual argument, to see if
    1794              :    there is a matching specific name.  If none, it is an error, and if
    1795              :    more than one, the reference is ambiguous.  */
    1796              : static int
    1797            8 : count_specific_procs (gfc_expr *e)
    1798              : {
    1799            8 :   int n;
    1800            8 :   gfc_interface *p;
    1801            8 :   gfc_symbol *sym;
    1802              : 
    1803            8 :   n = 0;
    1804            8 :   sym = e->symtree->n.sym;
    1805              : 
    1806           22 :   for (p = sym->generic; p; p = p->next)
    1807           14 :     if (strcmp (sym->name, p->sym->name) == 0)
    1808              :       {
    1809            8 :         e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
    1810              :                                        sym->name);
    1811            8 :         n++;
    1812              :       }
    1813              : 
    1814            8 :   if (n > 1)
    1815            1 :     gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
    1816              :                &e->where);
    1817              : 
    1818            8 :   if (n == 0)
    1819            1 :     gfc_error ("GENERIC procedure %qs is not allowed as an actual "
    1820              :                "argument at %L", sym->name, &e->where);
    1821              : 
    1822            8 :   return n;
    1823              : }
    1824              : 
    1825              : 
    1826              : /* See if a call to sym could possibly be a not allowed RECURSION because of
    1827              :    a missing RECURSIVE declaration.  This means that either sym is the current
    1828              :    context itself, or sym is the parent of a contained procedure calling its
    1829              :    non-RECURSIVE containing procedure.
    1830              :    This also works if sym is an ENTRY.  */
    1831              : 
    1832              : static bool
    1833       152806 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1834              : {
    1835       152806 :   gfc_symbol* proc_sym;
    1836       152806 :   gfc_symbol* context_proc;
    1837       152806 :   gfc_namespace* real_context;
    1838              : 
    1839       152806 :   if (sym->attr.flavor == FL_PROGRAM
    1840              :       || gfc_fl_struct (sym->attr.flavor))
    1841              :     return false;
    1842              : 
    1843              :   /* If we've got an ENTRY, find real procedure.  */
    1844       152805 :   if (sym->attr.entry && sym->ns->entries)
    1845           45 :     proc_sym = sym->ns->entries->sym;
    1846              :   else
    1847              :     proc_sym = sym;
    1848              : 
    1849              :   /* If sym is RECURSIVE, all is well of course.  */
    1850       152805 :   if (proc_sym->attr.recursive || flag_recursive)
    1851              :     return false;
    1852              : 
    1853              :   /* Find the context procedure's "real" symbol if it has entries.
    1854              :      We look for a procedure symbol, so recurse on the parents if we don't
    1855              :      find one (like in case of a BLOCK construct).  */
    1856         1965 :   for (real_context = context; ; real_context = real_context->parent)
    1857              :     {
    1858              :       /* We should find something, eventually!  */
    1859       129684 :       gcc_assert (real_context);
    1860              : 
    1861       129684 :       context_proc = (real_context->entries ? real_context->entries->sym
    1862              :                                             : real_context->proc_name);
    1863              : 
    1864              :       /* In some special cases, there may not be a proc_name, like for this
    1865              :          invalid code:
    1866              :          real(bad_kind()) function foo () ...
    1867              :          when checking the call to bad_kind ().
    1868              :          In these cases, we simply return here and assume that the
    1869              :          call is ok.  */
    1870       129684 :       if (!context_proc)
    1871              :         return false;
    1872              : 
    1873       129420 :       if (context_proc->attr.flavor != FL_LABEL)
    1874              :         break;
    1875              :     }
    1876              : 
    1877              :   /* A call from sym's body to itself is recursion, of course.  */
    1878       127455 :   if (context_proc == proc_sym)
    1879              :     return true;
    1880              : 
    1881              :   /* The same is true if context is a contained procedure and sym the
    1882              :      containing one.  */
    1883       127440 :   if (context_proc->attr.contained)
    1884              :     {
    1885        21489 :       gfc_symbol* parent_proc;
    1886              : 
    1887        21489 :       gcc_assert (context->parent);
    1888        21489 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1889              :                                               : context->parent->proc_name);
    1890              : 
    1891        21489 :       if (parent_proc == proc_sym)
    1892            9 :         return true;
    1893              :     }
    1894              : 
    1895              :   return false;
    1896              : }
    1897              : 
    1898              : 
    1899              : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
    1900              :    its typespec and formal argument list.  */
    1901              : 
    1902              : bool
    1903        47082 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1904              : {
    1905        47082 :   gfc_intrinsic_sym* isym = NULL;
    1906        47082 :   const char* symstd;
    1907              : 
    1908        47082 :   if (sym->resolve_symbol_called >= 2)
    1909              :     return true;
    1910              : 
    1911        37128 :   sym->resolve_symbol_called = 2;
    1912              : 
    1913              :   /* Already resolved.  */
    1914        37128 :   if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
    1915              :     return true;
    1916              : 
    1917              :   /* We already know this one is an intrinsic, so we don't call
    1918              :      gfc_is_intrinsic for full checking but rather use gfc_find_function and
    1919              :      gfc_find_subroutine directly to check whether it is a function or
    1920              :      subroutine.  */
    1921              : 
    1922        29092 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1923              :     {
    1924        12679 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1925        12679 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1926        12679 :     }
    1927        16413 :   else if (sym->intmod_sym_id)
    1928              :     {
    1929        12628 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1930        12628 :       isym = gfc_intrinsic_function_by_id (id);
    1931              :     }
    1932         3785 :   else if (!sym->attr.subroutine)
    1933         3698 :     isym = gfc_find_function (sym->name);
    1934              : 
    1935        29005 :   if (isym && !sym->attr.subroutine)
    1936              :     {
    1937        16281 :       if (sym->ts.type != BT_UNKNOWN && warn_surprising
    1938           24 :           && !sym->attr.implicit_type)
    1939           10 :         gfc_warning (OPT_Wsurprising,
    1940              :                      "Type specified for intrinsic function %qs at %L is"
    1941              :                       " ignored", sym->name, &sym->declared_at);
    1942              : 
    1943        20664 :       if (!sym->attr.function &&
    1944         4383 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1945              :         return false;
    1946              : 
    1947        16281 :       sym->ts = isym->ts;
    1948              :     }
    1949        12811 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1950              :     {
    1951        12808 :       if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
    1952              :         {
    1953            1 :           gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
    1954              :                       " specifier", sym->name, &sym->declared_at);
    1955            1 :           return false;
    1956              :         }
    1957              : 
    1958        12848 :       if (!sym->attr.subroutine &&
    1959           41 :           !gfc_add_subroutine(&sym->attr, sym->name, loc))
    1960              :         return false;
    1961              :     }
    1962              :   else
    1963              :     {
    1964            3 :       gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
    1965              :                  &sym->declared_at);
    1966            3 :       return false;
    1967              :     }
    1968              : 
    1969        29087 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1970              : 
    1971        29087 :   sym->attr.pure = isym->pure;
    1972        29087 :   sym->attr.elemental = isym->elemental;
    1973              : 
    1974              :   /* Check it is actually available in the standard settings.  */
    1975        29087 :   if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
    1976              :     {
    1977           31 :       gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
    1978              :                  "available in the current standard settings but %s. Use "
    1979              :                  "an appropriate %<-std=*%> option or enable "
    1980              :                  "%<-fall-intrinsics%> in order to use it.",
    1981              :                  sym->name, &sym->declared_at, symstd);
    1982           31 :       return false;
    1983              :     }
    1984              : 
    1985              :   return true;
    1986              : }
    1987              : 
    1988              : 
    1989              : /* Resolve a procedure expression, like passing it to a called procedure or as
    1990              :    RHS for a procedure pointer assignment.  */
    1991              : 
    1992              : static bool
    1993      1335359 : resolve_procedure_expression (gfc_expr* expr)
    1994              : {
    1995      1335359 :   gfc_symbol* sym;
    1996              : 
    1997      1335359 :   if (expr->expr_type != EXPR_VARIABLE)
    1998              :     return true;
    1999      1335342 :   gcc_assert (expr->symtree);
    2000              : 
    2001      1335342 :   sym = expr->symtree->n.sym;
    2002              : 
    2003      1335342 :   if (sym->attr.intrinsic)
    2004         1360 :     gfc_resolve_intrinsic (sym, &expr->where);
    2005              : 
    2006      1335342 :   if (sym->attr.flavor != FL_PROCEDURE
    2007        32250 :       || (sym->attr.function && sym->result == sym))
    2008              :     return true;
    2009              : 
    2010              :    /* A non-RECURSIVE procedure that is used as procedure expression within its
    2011              :      own body is in danger of being called recursively.  */
    2012        17510 :   if (is_illegal_recursion (sym, gfc_current_ns))
    2013              :     {
    2014           10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    2015            0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    2016              :                      " possibly calling itself recursively in procedure %qs. "
    2017              :                      " Declare it RECURSIVE or use %<-frecursive%>",
    2018            0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    2019              :       else
    2020           10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    2021              :                      " itself recursively.  Declare it RECURSIVE or use"
    2022              :                      " %<-frecursive%>", sym->name, &expr->where);
    2023              :     }
    2024              : 
    2025              :   return true;
    2026              : }
    2027              : 
    2028              : 
    2029              : /* Check that name is not a derived type.  */
    2030              : 
    2031              : static bool
    2032         3416 : is_dt_name (const char *name)
    2033              : {
    2034         3416 :   gfc_symbol *dt_list, *dt_first;
    2035              : 
    2036         3416 :   dt_list = dt_first = gfc_derived_types;
    2037         5870 :   for (; dt_list; dt_list = dt_list->dt_next)
    2038              :     {
    2039         3577 :       if (strcmp(dt_list->name, name) == 0)
    2040              :         return true;
    2041         3574 :       if (dt_first == dt_list->dt_next)
    2042              :         break;
    2043              :     }
    2044              :   return false;
    2045              : }
    2046              : 
    2047              : 
    2048              : /* Resolve an actual argument list.  Most of the time, this is just
    2049              :    resolving the expressions in the list.
    2050              :    The exception is that we sometimes have to decide whether arguments
    2051              :    that look like procedure arguments are really simple variable
    2052              :    references.  */
    2053              : 
    2054              : static bool
    2055       429423 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2056              :                         bool no_formal_args)
    2057              : {
    2058       429423 :   gfc_symbol *sym = NULL;
    2059       429423 :   gfc_symtree *parent_st;
    2060       429423 :   gfc_expr *e;
    2061       429423 :   gfc_component *comp;
    2062       429423 :   int save_need_full_assumed_size;
    2063       429423 :   bool return_value = false;
    2064       429423 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2065              : 
    2066       429423 :   actual_arg = true;
    2067       429423 :   first_actual_arg = true;
    2068              : 
    2069      1101916 :   for (; arg; arg = arg->next)
    2070              :     {
    2071       672594 :       e = arg->expr;
    2072       672594 :       if (e == NULL)
    2073              :         {
    2074              :           /* Check the label is a valid branching target.  */
    2075         2437 :           if (arg->label)
    2076              :             {
    2077          236 :               if (arg->label->defined == ST_LABEL_UNKNOWN)
    2078              :                 {
    2079            0 :                   gfc_error ("Label %d referenced at %L is never defined",
    2080              :                              arg->label->value, &arg->label->where);
    2081            0 :                   goto cleanup;
    2082              :                 }
    2083              :             }
    2084         2437 :           first_actual_arg = false;
    2085         2437 :           continue;
    2086              :         }
    2087              : 
    2088       670157 :       if (e->expr_type == EXPR_VARIABLE
    2089       295770 :             && e->symtree->n.sym->attr.generic
    2090            8 :             && no_formal_args
    2091       670162 :             && count_specific_procs (e) != 1)
    2092            2 :         goto cleanup;
    2093              : 
    2094       670155 :       if (e->ts.type != BT_PROCEDURE)
    2095              :         {
    2096       597051 :           save_need_full_assumed_size = need_full_assumed_size;
    2097       597051 :           if (e->expr_type != EXPR_VARIABLE)
    2098       374387 :             need_full_assumed_size = 0;
    2099       597051 :           if (!gfc_resolve_expr (e))
    2100           60 :             goto cleanup;
    2101       596991 :           need_full_assumed_size = save_need_full_assumed_size;
    2102       596991 :           goto argument_list;
    2103              :         }
    2104              : 
    2105              :       /* See if the expression node should really be a variable reference.  */
    2106              : 
    2107        73104 :       sym = e->symtree->n.sym;
    2108              : 
    2109        73104 :       if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
    2110              :         {
    2111            3 :           gfc_error ("Derived type %qs is used as an actual "
    2112              :                      "argument at %L", sym->name, &e->where);
    2113            3 :           goto cleanup;
    2114              :         }
    2115              : 
    2116        73101 :       if (sym->attr.flavor == FL_PROCEDURE
    2117        69688 :           || sym->attr.intrinsic
    2118        69688 :           || sym->attr.external)
    2119              :         {
    2120         3413 :           int actual_ok;
    2121              : 
    2122              :           /* If a procedure is not already determined to be something else
    2123              :              check if it is intrinsic.  */
    2124         3413 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2125         1254 :             sym->attr.intrinsic = 1;
    2126              : 
    2127         3413 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2128              :             {
    2129            2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2130              :                          "actual argument", sym->name, &e->where);
    2131              :             }
    2132              : 
    2133         6826 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2134         3413 :                                                sym->attr.subroutine);
    2135         3413 :           if (sym->attr.intrinsic && actual_ok == 0)
    2136              :             {
    2137            0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2138              :                          "actual argument", sym->name, &e->where);
    2139              :             }
    2140              : 
    2141         3413 :           if (sym->attr.contained && !sym->attr.use_assoc
    2142          438 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2143              :             {
    2144          250 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2145              :                                    " used as actual argument at %L",
    2146              :                                    sym->name, &e->where))
    2147            3 :                 goto cleanup;
    2148              :             }
    2149              : 
    2150         3410 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2151              :             {
    2152            2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2153              :                          "allowed as an actual argument at %L", sym->name,
    2154              :                          &e->where);
    2155              :             }
    2156              : 
    2157              :           /* Check if a generic interface has a specific procedure
    2158              :             with the same name before emitting an error.  */
    2159         3410 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2160            0 :             goto cleanup;
    2161              : 
    2162              :           /* Just in case a specific was found for the expression.  */
    2163         3410 :           sym = e->symtree->n.sym;
    2164              : 
    2165              :           /* If the symbol is the function that names the current (or
    2166              :              parent) scope, then we really have a variable reference.  */
    2167              : 
    2168         3410 :           if (gfc_is_function_return_value (sym, sym->ns))
    2169            0 :             goto got_variable;
    2170              : 
    2171              :           /* If all else fails, see if we have a specific intrinsic.  */
    2172         3410 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2173              :             {
    2174            0 :               gfc_intrinsic_sym *isym;
    2175              : 
    2176            0 :               isym = gfc_find_function (sym->name);
    2177            0 :               if (isym == NULL || !isym->specific)
    2178              :                 {
    2179            0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2180              :                              "for the reference %qs at %L", sym->name,
    2181              :                              &e->where);
    2182            0 :                   goto cleanup;
    2183              :                 }
    2184            0 :               sym->ts = isym->ts;
    2185            0 :               sym->attr.intrinsic = 1;
    2186            0 :               sym->attr.function = 1;
    2187              :             }
    2188              : 
    2189         3410 :           if (!gfc_resolve_expr (e))
    2190            0 :             goto cleanup;
    2191         3410 :           goto argument_list;
    2192              :         }
    2193              : 
    2194              :       /* See if the name is a module procedure in a parent unit.  */
    2195              : 
    2196        69688 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2197        69595 :         goto got_variable;
    2198              : 
    2199           93 :       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
    2200              :         {
    2201            0 :           gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
    2202            0 :           goto cleanup;
    2203              :         }
    2204              : 
    2205           93 :       if (parent_st == NULL)
    2206           93 :         goto got_variable;
    2207              : 
    2208            0 :       sym = parent_st->n.sym;
    2209            0 :       e->symtree = parent_st;                /* Point to the right thing.  */
    2210              : 
    2211            0 :       if (sym->attr.flavor == FL_PROCEDURE
    2212            0 :           || sym->attr.intrinsic
    2213            0 :           || sym->attr.external)
    2214              :         {
    2215            0 :           if (!gfc_resolve_expr (e))
    2216            0 :             goto cleanup;
    2217            0 :           goto argument_list;
    2218              :         }
    2219              : 
    2220            0 :     got_variable:
    2221        69688 :       e->expr_type = EXPR_VARIABLE;
    2222        69688 :       e->ts = sym->ts;
    2223        69688 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2224        36213 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2225         3894 :               && CLASS_DATA (sym)->as))
    2226              :         {
    2227        39123 :           gfc_array_spec *as
    2228        36299 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2229        36299 :           e->rank = as->rank;
    2230        36299 :           e->corank = as->corank;
    2231        36299 :           e->ref = gfc_get_ref ();
    2232        36299 :           e->ref->type = REF_ARRAY;
    2233        36299 :           e->ref->u.ar.type = AR_FULL;
    2234        36299 :           e->ref->u.ar.as = as;
    2235              :         }
    2236              : 
    2237              :       /* These symbols are set untyped by calls to gfc_set_default_type
    2238              :          with 'error_flag' = false.  Reset the untyped attribute so that
    2239              :          the error will be generated in gfc_resolve_expr.  */
    2240        69688 :       if (e->expr_type == EXPR_VARIABLE
    2241        69688 :           && sym->ts.type == BT_UNKNOWN
    2242           36 :           && sym->attr.untyped)
    2243            5 :         sym->attr.untyped = 0;
    2244              : 
    2245              :       /* Expressions are assigned a default ts.type of BT_PROCEDURE in
    2246              :          primary.cc (match_actual_arg). If above code determines that it
    2247              :          is a  variable instead, it needs to be resolved as it was not
    2248              :          done at the beginning of this function.  */
    2249        69688 :       save_need_full_assumed_size = need_full_assumed_size;
    2250        69688 :       if (e->expr_type != EXPR_VARIABLE)
    2251            0 :         need_full_assumed_size = 0;
    2252        69688 :       if (!gfc_resolve_expr (e))
    2253           22 :         goto cleanup;
    2254        69666 :       need_full_assumed_size = save_need_full_assumed_size;
    2255              : 
    2256       670067 :     argument_list:
    2257              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2258              :          nothing to do for %REF.  */
    2259       670067 :       if (arg->name && arg->name[0] == '%')
    2260              :         {
    2261           42 :           if (strcmp ("%VAL", arg->name) == 0)
    2262              :             {
    2263           28 :               if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
    2264              :                 {
    2265            2 :                   gfc_error ("By-value argument at %L is not of numeric "
    2266              :                              "type", &e->where);
    2267            2 :                   goto cleanup;
    2268              :                 }
    2269              : 
    2270           26 :               if (e->rank)
    2271              :                 {
    2272            1 :                   gfc_error ("By-value argument at %L cannot be an array or "
    2273              :                              "an array section", &e->where);
    2274            1 :                   goto cleanup;
    2275              :                 }
    2276              : 
    2277              :               /* Intrinsics are still PROC_UNKNOWN here.  However,
    2278              :                  since same file external procedures are not resolvable
    2279              :                  in gfortran, it is a good deal easier to leave them to
    2280              :                  intrinsic.cc.  */
    2281           25 :               if (ptype != PROC_UNKNOWN
    2282           25 :                   && ptype != PROC_DUMMY
    2283            9 :                   && ptype != PROC_EXTERNAL
    2284            9 :                   && ptype != PROC_MODULE)
    2285              :                 {
    2286            3 :                   gfc_error ("By-value argument at %L is not allowed "
    2287              :                              "in this context", &e->where);
    2288            3 :                   goto cleanup;
    2289              :                 }
    2290              :             }
    2291              : 
    2292              :           /* Statement functions have already been excluded above.  */
    2293           14 :           else if (strcmp ("%LOC", arg->name) == 0
    2294            8 :                    && e->ts.type == BT_PROCEDURE)
    2295              :             {
    2296            0 :               if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
    2297              :                 {
    2298            0 :                   gfc_error ("Passing internal procedure at %L by location "
    2299              :                              "not allowed", &e->where);
    2300            0 :                   goto cleanup;
    2301              :                 }
    2302              :             }
    2303              :         }
    2304              : 
    2305       670061 :       comp = gfc_get_proc_ptr_comp(e);
    2306       670061 :       if (e->expr_type == EXPR_VARIABLE
    2307       294392 :           && comp && comp->attr.elemental)
    2308              :         {
    2309            1 :             gfc_error ("ELEMENTAL procedure pointer component %qs is not "
    2310              :                        "allowed as an actual argument at %L", comp->name,
    2311              :                        &e->where);
    2312              :         }
    2313              : 
    2314              :       /* Fortran 2008, C1237.  */
    2315       294392 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2316       670506 :           && gfc_has_ultimate_pointer (e))
    2317              :         {
    2318            3 :           gfc_error ("Coindexed actual argument at %L with ultimate pointer "
    2319              :                      "component", &e->where);
    2320            3 :           goto cleanup;
    2321              :         }
    2322              : 
    2323       670058 :       if (e->expr_type == EXPR_VARIABLE
    2324       294389 :           && e->ts.type == BT_PROCEDURE
    2325         3410 :           && no_formal_args
    2326         1505 :           && sym->attr.flavor == FL_PROCEDURE
    2327         1505 :           && sym->attr.if_source == IFSRC_UNKNOWN
    2328          142 :           && !sym->attr.external
    2329            2 :           && !sym->attr.intrinsic
    2330            2 :           && !sym->attr.artificial
    2331            2 :           && !sym->ts.interface)
    2332              :         {
    2333              :           /* Emit a warning for -std=legacy and an error otherwise. */
    2334            2 :           if (gfc_option.warn_std == 0)
    2335            0 :             gfc_warning (0, "Procedure %qs at %L used as actual argument but "
    2336              :                          "does neither have an explicit interface nor the "
    2337              :                          "EXTERNAL attribute", sym->name, &e->where);
    2338              :           else
    2339              :             {
    2340            2 :               gfc_error ("Procedure %qs at %L used as actual argument but "
    2341              :                          "does neither have an explicit interface nor the "
    2342              :                          "EXTERNAL attribute", sym->name, &e->where);
    2343            2 :               goto cleanup;
    2344              :             }
    2345              :         }
    2346              : 
    2347       670056 :       first_actual_arg = false;
    2348              :     }
    2349              : 
    2350              :   return_value = true;
    2351              : 
    2352       429423 : cleanup:
    2353       429423 :   actual_arg = actual_arg_sav;
    2354       429423 :   first_actual_arg = first_actual_arg_sav;
    2355              : 
    2356       429423 :   return return_value;
    2357              : }
    2358              : 
    2359              : 
    2360              : /* Do the checks of the actual argument list that are specific to elemental
    2361              :    procedures.  If called with c == NULL, we have a function, otherwise if
    2362              :    expr == NULL, we have a subroutine.  */
    2363              : 
    2364              : static bool
    2365       326925 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2366              : {
    2367       326925 :   gfc_actual_arglist *arg0;
    2368       326925 :   gfc_actual_arglist *arg;
    2369       326925 :   gfc_symbol *esym = NULL;
    2370       326925 :   gfc_intrinsic_sym *isym = NULL;
    2371       326925 :   gfc_expr *e = NULL;
    2372       326925 :   gfc_intrinsic_arg *iformal = NULL;
    2373       326925 :   gfc_formal_arglist *eformal = NULL;
    2374       326925 :   bool formal_optional = false;
    2375       326925 :   bool set_by_optional = false;
    2376       326925 :   int i;
    2377       326925 :   int rank = 0;
    2378              : 
    2379              :   /* Is this an elemental procedure?  */
    2380       326925 :   if (expr && expr->value.function.actual != NULL)
    2381              :     {
    2382       236945 :       if (expr->value.function.esym != NULL
    2383        44074 :           && expr->value.function.esym->attr.elemental)
    2384              :         {
    2385              :           arg0 = expr->value.function.actual;
    2386              :           esym = expr->value.function.esym;
    2387              :         }
    2388       220637 :       else if (expr->value.function.isym != NULL
    2389       191816 :                && expr->value.function.isym->elemental)
    2390              :         {
    2391              :           arg0 = expr->value.function.actual;
    2392              :           isym = expr->value.function.isym;
    2393              :         }
    2394              :       else
    2395              :         return true;
    2396              :     }
    2397        89980 :   else if (c && c->ext.actual != NULL)
    2398              :     {
    2399        71198 :       arg0 = c->ext.actual;
    2400              : 
    2401        71198 :       if (c->resolved_sym)
    2402              :         esym = c->resolved_sym;
    2403              :       else
    2404          323 :         esym = c->symtree->n.sym;
    2405        71198 :       gcc_assert (esym);
    2406              : 
    2407        71198 :       if (!esym->attr.elemental)
    2408              :         return true;
    2409              :     }
    2410              :   else
    2411              :     return true;
    2412              : 
    2413              :   /* The rank of an elemental is the rank of its array argument(s).  */
    2414       174596 :   for (arg = arg0; arg; arg = arg->next)
    2415              :     {
    2416       113127 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2417              :         {
    2418        10746 :           rank = arg->expr->rank;
    2419        10746 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2420         5490 :               && arg->expr->symtree->n.sym->attr.optional)
    2421        10746 :             set_by_optional = true;
    2422              : 
    2423              :           /* Function specific; set the result rank and shape.  */
    2424        10746 :           if (expr)
    2425              :             {
    2426         8338 :               expr->rank = rank;
    2427         8338 :               expr->corank = arg->expr->corank;
    2428         8338 :               if (!expr->shape && arg->expr->shape)
    2429              :                 {
    2430         3968 :                   expr->shape = gfc_get_shape (rank);
    2431         8731 :                   for (i = 0; i < rank; i++)
    2432         4763 :                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
    2433              :                 }
    2434              :             }
    2435              :           break;
    2436              :         }
    2437              :     }
    2438              : 
    2439              :   /* If it is an array, it shall not be supplied as an actual argument
    2440              :      to an elemental procedure unless an array of the same rank is supplied
    2441              :      as an actual argument corresponding to a nonoptional dummy argument of
    2442              :      that elemental procedure(12.4.1.5).  */
    2443        72215 :   formal_optional = false;
    2444        72215 :   if (isym)
    2445        49694 :     iformal = isym->formal;
    2446              :   else
    2447        22521 :     eformal = esym->formal;
    2448              : 
    2449       190858 :   for (arg = arg0; arg; arg = arg->next)
    2450              :     {
    2451       118643 :       if (eformal)
    2452              :         {
    2453        40411 :           if (eformal->sym && eformal->sym->attr.optional)
    2454        40411 :             formal_optional = true;
    2455        40411 :           eformal = eformal->next;
    2456              :         }
    2457        78232 :       else if (isym && iformal)
    2458              :         {
    2459        67949 :           if (iformal->optional)
    2460        13456 :             formal_optional = true;
    2461        67949 :           iformal = iformal->next;
    2462              :         }
    2463        10283 :       else if (isym)
    2464        10275 :         formal_optional = true;
    2465              : 
    2466       118643 :       if (pedantic && arg->expr != NULL
    2467        67699 :           && arg->expr->expr_type == EXPR_VARIABLE
    2468        31920 :           && arg->expr->symtree->n.sym->attr.optional
    2469          572 :           && formal_optional
    2470          479 :           && arg->expr->rank
    2471          153 :           && (set_by_optional || arg->expr->rank != rank)
    2472           42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2473              :         {
    2474          114 :           bool t = false;
    2475              :           gfc_actual_arglist *a;
    2476              : 
    2477              :           /* Scan the argument list for a non-optional argument with the
    2478              :              same rank as arg.  */
    2479          114 :           for (a = arg0; a; a = a->next)
    2480           87 :             if (a != arg
    2481           45 :                 && a->expr->rank == arg->expr->rank
    2482           39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2483           37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2484           37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2485              :               {
    2486              :                 t = true;
    2487              :                 break;
    2488              :               }
    2489              : 
    2490           42 :           if (!t)
    2491           27 :             gfc_warning (OPT_Wpedantic,
    2492              :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2493              :                          "present, then it cannot be the actual argument of "
    2494              :                          "an ELEMENTAL procedure unless there is a non-optional"
    2495              :                          " argument with the same rank "
    2496              :                          "(Fortran 2018, 15.5.2.12)",
    2497              :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2498              :         }
    2499              :     }
    2500              : 
    2501       190847 :   for (arg = arg0; arg; arg = arg->next)
    2502              :     {
    2503       118641 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2504       105007 :         continue;
    2505              : 
    2506              :       /* Being elemental, the last upper bound of an assumed size array
    2507              :          argument must be present.  */
    2508        13634 :       if (resolve_assumed_size_actual (arg->expr))
    2509              :         return false;
    2510              : 
    2511              :       /* Elemental procedure's array actual arguments must conform.  */
    2512        13631 :       if (e != NULL)
    2513              :         {
    2514         2888 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2515              :             return false;
    2516              :         }
    2517              :       else
    2518        10743 :         e = arg->expr;
    2519              :     }
    2520              : 
    2521              :   /* INTENT(OUT) is only allowed for subroutines; if any actual argument
    2522              :      is an array, the intent inout/out variable needs to be also an array.  */
    2523        72206 :   if (rank > 0 && esym && expr == NULL)
    2524         7333 :     for (eformal = esym->formal, arg = arg0; arg && eformal;
    2525         4931 :          arg = arg->next, eformal = eformal->next)
    2526         4933 :       if (eformal->sym
    2527         4932 :           && (eformal->sym->attr.intent == INTENT_OUT
    2528         3850 :               || eformal->sym->attr.intent == INTENT_INOUT)
    2529         1716 :           && arg->expr && arg->expr->rank == 0)
    2530              :         {
    2531            2 :           gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
    2532              :                      "ELEMENTAL subroutine %qs is a scalar, but another "
    2533              :                      "actual argument is an array", &arg->expr->where,
    2534              :                      (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
    2535              :                      : "INOUT", eformal->sym->name, esym->name);
    2536            2 :           return false;
    2537              :         }
    2538              :   return true;
    2539              : }
    2540              : 
    2541              : 
    2542              : /* This function does the checking of references to global procedures
    2543              :    as defined in sections 18.1 and 14.1, respectively, of the Fortran
    2544              :    77 and 95 standards.  It checks for a gsymbol for the name, making
    2545              :    one if it does not already exist.  If it already exists, then the
    2546              :    reference being resolved must correspond to the type of gsymbol.
    2547              :    Otherwise, the new symbol is equipped with the attributes of the
    2548              :    reference.  The corresponding code that is called in creating
    2549              :    global entities is parse.cc.
    2550              : 
    2551              :    In addition, for all but -std=legacy, the gsymbols are used to
    2552              :    check the interfaces of external procedures from the same file.
    2553              :    The namespace of the gsymbol is resolved and then, once this is
    2554              :    done the interface is checked.  */
    2555              : 
    2556              : 
    2557              : static bool
    2558        14955 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2559              : {
    2560        14955 :   if (!gsym_ns->proc_name->attr.recursive)
    2561              :     return true;
    2562              : 
    2563          151 :   if (sym->ns == gsym_ns)
    2564              :     return false;
    2565              : 
    2566          151 :   if (sym->ns->parent && sym->ns->parent == gsym_ns)
    2567            0 :     return false;
    2568              : 
    2569              :   return true;
    2570              : }
    2571              : 
    2572              : static bool
    2573        14955 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2574              : {
    2575        14955 :   if (gsym_ns->entries)
    2576              :     {
    2577              :       gfc_entry_list *entry = gsym_ns->entries;
    2578              : 
    2579         3312 :       for (; entry; entry = entry->next)
    2580              :         {
    2581         2333 :           if (strcmp (sym->name, entry->sym->name) == 0)
    2582              :             {
    2583          971 :               if (strcmp (gsym_ns->proc_name->name,
    2584          971 :                           sym->ns->proc_name->name) == 0)
    2585              :                 return false;
    2586              : 
    2587          971 :               if (sym->ns->parent
    2588            0 :                   && strcmp (gsym_ns->proc_name->name,
    2589            0 :                              sym->ns->parent->proc_name->name) == 0)
    2590              :                 return false;
    2591              :             }
    2592              :         }
    2593              :     }
    2594              :   return true;
    2595              : }
    2596              : 
    2597              : 
    2598              : /* Check for the requirement of an explicit interface. F08:12.4.2.2.  */
    2599              : 
    2600              : bool
    2601        15795 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2602              : {
    2603        15795 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2604              : 
    2605        59004 :   for ( ; arg; arg = arg->next)
    2606              :     {
    2607        27822 :       if (!arg->sym)
    2608          157 :         continue;
    2609              : 
    2610        27665 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2611              :         {
    2612            0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2613            0 :           return true;
    2614              :         }
    2615        27665 :       else if (arg->sym->attr.asynchronous)
    2616              :         {
    2617            0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2618            0 :           return true;
    2619              :         }
    2620        27665 :       else if (arg->sym->attr.optional)
    2621              :         {
    2622           75 :           strncpy (errmsg, _("optional argument"), err_len);
    2623           75 :           return true;
    2624              :         }
    2625        27590 :       else if (arg->sym->attr.pointer)
    2626              :         {
    2627           12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2628           12 :           return true;
    2629              :         }
    2630        27578 :       else if (arg->sym->attr.target)
    2631              :         {
    2632           72 :           strncpy (errmsg, _("target argument"), err_len);
    2633           72 :           return true;
    2634              :         }
    2635        27506 :       else if (arg->sym->attr.value)
    2636              :         {
    2637           12 :           strncpy (errmsg, _("value argument"), err_len);
    2638           12 :           return true;
    2639              :         }
    2640        27494 :       else if (arg->sym->attr.volatile_)
    2641              :         {
    2642            1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2643            1 :           return true;
    2644              :         }
    2645        27493 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2646              :         {
    2647           69 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2648           69 :           return true;
    2649              :         }
    2650        27424 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK)  /* TS 29113, 6.2.  */
    2651              :         {
    2652            1 :           strncpy (errmsg, _("assumed-rank argument"), err_len);
    2653            1 :           return true;
    2654              :         }
    2655        27423 :       else if (arg->sym->attr.codimension)  /* (2c)  */
    2656              :         {
    2657            1 :           strncpy (errmsg, _("coarray argument"), err_len);
    2658            1 :           return true;
    2659              :         }
    2660        27422 :       else if (false)  /* (2d) TODO: parametrized derived type  */
    2661              :         {
    2662              :           strncpy (errmsg, _("parametrized derived type argument"), err_len);
    2663              :           return true;
    2664              :         }
    2665        27422 :       else if (arg->sym->ts.type == BT_CLASS)  /* (2e)  */
    2666              :         {
    2667          164 :           strncpy (errmsg, _("polymorphic argument"), err_len);
    2668          164 :           return true;
    2669              :         }
    2670        27258 :       else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2671              :         {
    2672            0 :           strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
    2673            0 :           return true;
    2674              :         }
    2675        27258 :       else if (arg->sym->ts.type == BT_ASSUMED)
    2676              :         {
    2677              :           /* As assumed-type is unlimited polymorphic (cf. above).
    2678              :              See also TS 29113, Note 6.1.  */
    2679            1 :           strncpy (errmsg, _("assumed-type argument"), err_len);
    2680            1 :           return true;
    2681              :         }
    2682              :     }
    2683              : 
    2684        15387 :   if (sym->attr.function)
    2685              :     {
    2686         3485 :       gfc_symbol *res = sym->result ? sym->result : sym;
    2687              : 
    2688         3485 :       if (res->attr.dimension)  /* (3a)  */
    2689              :         {
    2690           93 :           strncpy (errmsg, _("array result"), err_len);
    2691           93 :           return true;
    2692              :         }
    2693         3392 :       else if (res->attr.pointer || res->attr.allocatable)  /* (3b)  */
    2694              :         {
    2695           38 :           strncpy (errmsg, _("pointer or allocatable result"), err_len);
    2696           38 :           return true;
    2697              :         }
    2698         3354 :       else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
    2699          347 :                && res->ts.u.cl->length
    2700          166 :                && res->ts.u.cl->length->expr_type != EXPR_CONSTANT)  /* (3c)  */
    2701              :         {
    2702           12 :           strncpy (errmsg, _("result with non-constant character length"), err_len);
    2703           12 :           return true;
    2704              :         }
    2705              :     }
    2706              : 
    2707        15244 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2708              :     {
    2709            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2710            7 :       return true;
    2711              :     }
    2712        15237 :   else if (sym->attr.is_bind_c)  /* (5)  */
    2713              :     {
    2714            0 :       strncpy (errmsg, _("bind(c) procedure"), err_len);
    2715            0 :       return true;
    2716              :     }
    2717              : 
    2718              :   return false;
    2719              : }
    2720              : 
    2721              : 
    2722              : static void
    2723        29567 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2724              : {
    2725        29567 :   gfc_gsymbol * gsym;
    2726        29567 :   gfc_namespace *ns;
    2727        29567 :   enum gfc_symbol_type type;
    2728        29567 :   char reason[200];
    2729              : 
    2730        29567 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2731              : 
    2732        29567 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2733        29567 :                           sym->binding_label != NULL);
    2734              : 
    2735        29567 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2736            9 :     gfc_global_used (gsym, where);
    2737              : 
    2738        29567 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2739         9378 :        || sym->attr.if_source == IFSRC_IFBODY)
    2740        25114 :       && gsym->type != GSYM_UNKNOWN
    2741        22934 :       && !gsym->binding_label
    2742        20617 :       && gsym->ns
    2743        14955 :       && gsym->ns->proc_name
    2744        14955 :       && not_in_recursive (sym, gsym->ns)
    2745        44522 :       && not_entry_self_reference (sym, gsym->ns))
    2746              :     {
    2747        14955 :       gfc_symbol *def_sym;
    2748        14955 :       def_sym = gsym->ns->proc_name;
    2749              : 
    2750        14955 :       if (gsym->ns->resolved != -1)
    2751              :         {
    2752              : 
    2753              :           /* Resolve the gsymbol namespace if needed.  */
    2754        14933 :           if (!gsym->ns->resolved)
    2755              :             {
    2756         2775 :               gfc_symbol *old_dt_list;
    2757              : 
    2758              :               /* Stash away derived types so that the backend_decls
    2759              :                  do not get mixed up.  */
    2760         2775 :               old_dt_list = gfc_derived_types;
    2761         2775 :               gfc_derived_types = NULL;
    2762              : 
    2763         2775 :               gfc_resolve (gsym->ns);
    2764              : 
    2765              :               /* Store the new derived types with the global namespace.  */
    2766         2775 :               if (gfc_derived_types)
    2767          306 :                 gsym->ns->derived_types = gfc_derived_types;
    2768              : 
    2769              :               /* Restore the derived types of this namespace.  */
    2770         2775 :               gfc_derived_types = old_dt_list;
    2771              :             }
    2772              : 
    2773              :           /* Make sure that translation for the gsymbol occurs before
    2774              :              the procedure currently being resolved.  */
    2775        14933 :           ns = gfc_global_ns_list;
    2776        25328 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2777              :             {
    2778        16924 :               if (ns->sibling == gsym->ns)
    2779              :                 {
    2780         6529 :                   ns->sibling = gsym->ns->sibling;
    2781         6529 :                   gsym->ns->sibling = gfc_global_ns_list;
    2782         6529 :                   gfc_global_ns_list = gsym->ns;
    2783         6529 :                   break;
    2784              :                 }
    2785              :             }
    2786              : 
    2787              :           /* This can happen if a binding name has been specified.  */
    2788        14933 :           if (gsym->binding_label && gsym->sym_name != def_sym->name)
    2789            0 :             gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
    2790              :         }
    2791              : 
    2792              :       /* Look up the specific entry symbol so that interface checks use
    2793              :          the entry's own formal argument list, not the entry master's.
    2794              :          This must run even when resolved == -1 (recursive resolution in
    2795              :          progress), because def_sym starts as the namespace proc_name
    2796              :          which is the entry master with the combined formals.  */
    2797        14955 :       if (def_sym->attr.entry_master || def_sym->attr.entry)
    2798              :         {
    2799          979 :           gfc_entry_list *entry;
    2800         1699 :           for (entry = gsym->ns->entries; entry; entry = entry->next)
    2801         1699 :             if (strcmp (entry->sym->name, sym->name) == 0)
    2802              :               {
    2803          979 :                 def_sym = entry->sym;
    2804          979 :                 break;
    2805              :               }
    2806              :         }
    2807              : 
    2808        14955 :       if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
    2809              :         {
    2810            6 :           gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
    2811              :                      sym->name, &sym->declared_at, gfc_typename (&sym->ts),
    2812            6 :                      gfc_typename (&def_sym->ts));
    2813           28 :           goto done;
    2814              :         }
    2815              : 
    2816        14949 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2817        14949 :           && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
    2818              :         {
    2819            8 :           gfc_error ("Explicit interface required for %qs at %L: %s",
    2820              :                      sym->name, &sym->declared_at, reason);
    2821            8 :           goto done;
    2822              :         }
    2823              : 
    2824        14941 :       bool bad_result_characteristics;
    2825        14941 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2826              :                                    reason, sizeof(reason), NULL, NULL,
    2827              :                                    &bad_result_characteristics))
    2828              :         {
    2829              :           /* Turn errors into warnings with -std=gnu and -std=legacy,
    2830              :              unless a function returns a wrong type, which can lead
    2831              :              to all kinds of ICEs and wrong code.  */
    2832              : 
    2833           14 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2834            2 :               && !bad_result_characteristics)
    2835            2 :             gfc_errors_to_warnings (true);
    2836              : 
    2837           14 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2838              :                      sym->name, &sym->declared_at, reason);
    2839           14 :           sym->error = 1;
    2840           14 :           gfc_errors_to_warnings (false);
    2841           14 :           goto done;
    2842              :         }
    2843              :     }
    2844              : 
    2845        29567 : done:
    2846              : 
    2847        29567 :   if (gsym->type == GSYM_UNKNOWN)
    2848              :     {
    2849         4025 :       gsym->type = type;
    2850         4025 :       gsym->where = *where;
    2851              :     }
    2852              : 
    2853        29567 :   gsym->used = 1;
    2854        29567 : }
    2855              : 
    2856              : 
    2857              : /************* Function resolution *************/
    2858              : 
    2859              : /* Resolve a function call known to be generic.
    2860              :    Section 14.1.2.4.1.  */
    2861              : 
    2862              : static match
    2863        27614 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2864              : {
    2865        27614 :   gfc_symbol *s;
    2866              : 
    2867        27614 :   if (sym->attr.generic)
    2868              :     {
    2869        26502 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2870        26502 :       if (s != NULL)
    2871              :         {
    2872        19830 :           expr->value.function.name = s->name;
    2873        19830 :           expr->value.function.esym = s;
    2874              : 
    2875        19830 :           if (s->ts.type != BT_UNKNOWN)
    2876        19813 :             expr->ts = s->ts;
    2877           17 :           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
    2878           15 :             expr->ts = s->result->ts;
    2879              : 
    2880        19830 :           if (s->as != NULL)
    2881              :             {
    2882           55 :               expr->rank = s->as->rank;
    2883           55 :               expr->corank = s->as->corank;
    2884              :             }
    2885        19775 :           else if (s->result != NULL && s->result->as != NULL)
    2886              :             {
    2887            0 :               expr->rank = s->result->as->rank;
    2888            0 :               expr->corank = s->result->as->corank;
    2889              :             }
    2890              : 
    2891        19830 :           gfc_set_sym_referenced (expr->value.function.esym);
    2892              : 
    2893        19830 :           return MATCH_YES;
    2894              :         }
    2895              : 
    2896              :       /* TODO: Need to search for elemental references in generic
    2897              :          interface.  */
    2898              :     }
    2899              : 
    2900         7784 :   if (sym->attr.intrinsic)
    2901         1069 :     return gfc_intrinsic_func_interface (expr, 0);
    2902              : 
    2903              :   return MATCH_NO;
    2904              : }
    2905              : 
    2906              : 
    2907              : static bool
    2908        27470 : resolve_generic_f (gfc_expr *expr)
    2909              : {
    2910        27470 :   gfc_symbol *sym;
    2911        27470 :   match m;
    2912        27470 :   gfc_interface *intr = NULL;
    2913              : 
    2914        27470 :   sym = expr->symtree->n.sym;
    2915              : 
    2916        27614 :   for (;;)
    2917              :     {
    2918        27614 :       m = resolve_generic_f0 (expr, sym);
    2919        27614 :       if (m == MATCH_YES)
    2920              :         return true;
    2921         6717 :       else if (m == MATCH_ERROR)
    2922              :         return false;
    2923              : 
    2924         6717 : generic:
    2925         6720 :       if (!intr)
    2926         6688 :         for (intr = sym->generic; intr; intr = intr->next)
    2927         6604 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2928              :             break;
    2929              : 
    2930         6720 :       if (sym->ns->parent == NULL)
    2931              :         break;
    2932          298 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2933              : 
    2934          298 :       if (sym == NULL)
    2935              :         break;
    2936          147 :       if (!generic_sym (sym))
    2937            3 :         goto generic;
    2938              :     }
    2939              : 
    2940              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2941              :      that possesses a matching interface.  14.1.2.4  */
    2942         6573 :   if (sym  && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
    2943              :     {
    2944            5 :       if (gfc_init_expr_flag)
    2945            1 :         gfc_error ("Function %qs in initialization expression at %L "
    2946              :                    "must be an intrinsic function",
    2947            1 :                    expr->symtree->n.sym->name, &expr->where);
    2948              :       else
    2949            4 :         gfc_error ("There is no specific function for the generic %qs "
    2950            4 :                    "at %L", expr->symtree->n.sym->name, &expr->where);
    2951            5 :       return false;
    2952              :     }
    2953              : 
    2954         6568 :   if (intr)
    2955              :     {
    2956         6533 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2957              :                                                  NULL, false))
    2958              :         return false;
    2959         6506 :       if (!gfc_use_derived (expr->ts.u.derived))
    2960              :         return false;
    2961         6506 :       return resolve_structure_cons (expr, 0);
    2962              :     }
    2963              : 
    2964           35 :   m = gfc_intrinsic_func_interface (expr, 0);
    2965           35 :   if (m == MATCH_YES)
    2966              :     return true;
    2967              : 
    2968            3 :   if (m == MATCH_NO)
    2969            3 :     gfc_error ("Generic function %qs at %L is not consistent with a "
    2970            3 :                "specific intrinsic interface", expr->symtree->n.sym->name,
    2971              :                &expr->where);
    2972              : 
    2973              :   return false;
    2974              : }
    2975              : 
    2976              : 
    2977              : /* Resolve a function call known to be specific.  */
    2978              : 
    2979              : static match
    2980        28303 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2981              : {
    2982        28303 :   match m;
    2983              : 
    2984        28303 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2985              :     {
    2986         8193 :       if (sym->attr.dummy)
    2987              :         {
    2988          282 :           sym->attr.proc = PROC_DUMMY;
    2989          282 :           goto found;
    2990              :         }
    2991              : 
    2992         7911 :       sym->attr.proc = PROC_EXTERNAL;
    2993         7911 :       goto found;
    2994              :     }
    2995              : 
    2996        20110 :   if (sym->attr.proc == PROC_MODULE
    2997              :       || sym->attr.proc == PROC_ST_FUNCTION
    2998              :       || sym->attr.proc == PROC_INTERNAL)
    2999        19372 :     goto found;
    3000              : 
    3001          738 :   if (sym->attr.intrinsic)
    3002              :     {
    3003          731 :       m = gfc_intrinsic_func_interface (expr, 1);
    3004          731 :       if (m == MATCH_YES)
    3005              :         return MATCH_YES;
    3006            0 :       if (m == MATCH_NO)
    3007            0 :         gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
    3008              :                    "with an intrinsic", sym->name, &expr->where);
    3009              : 
    3010            0 :       return MATCH_ERROR;
    3011              :     }
    3012              : 
    3013              :   return MATCH_NO;
    3014              : 
    3015        27565 : found:
    3016        27565 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3017              : 
    3018        27565 :   if (sym->result)
    3019        27565 :     expr->ts = sym->result->ts;
    3020              :   else
    3021            0 :     expr->ts = sym->ts;
    3022        27565 :   expr->value.function.name = sym->name;
    3023        27565 :   expr->value.function.esym = sym;
    3024              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3025              :      error(s).  */
    3026        27565 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3027              :     return MATCH_ERROR;
    3028        27564 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
    3029              :     {
    3030          322 :       expr->rank = CLASS_DATA (sym)->as->rank;
    3031          322 :       expr->corank = CLASS_DATA (sym)->as->corank;
    3032              :     }
    3033        27242 :   else if (sym->as != NULL)
    3034              :     {
    3035         2335 :       expr->rank = sym->as->rank;
    3036         2335 :       expr->corank = sym->as->corank;
    3037              :     }
    3038              : 
    3039              :   return MATCH_YES;
    3040              : }
    3041              : 
    3042              : 
    3043              : static bool
    3044        28296 : resolve_specific_f (gfc_expr *expr)
    3045              : {
    3046        28296 :   gfc_symbol *sym;
    3047        28296 :   match m;
    3048              : 
    3049        28296 :   sym = expr->symtree->n.sym;
    3050              : 
    3051        28303 :   for (;;)
    3052              :     {
    3053        28303 :       m = resolve_specific_f0 (sym, expr);
    3054        28303 :       if (m == MATCH_YES)
    3055              :         return true;
    3056            8 :       if (m == MATCH_ERROR)
    3057              :         return false;
    3058              : 
    3059            7 :       if (sym->ns->parent == NULL)
    3060              :         break;
    3061              : 
    3062            7 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3063              : 
    3064            7 :       if (sym == NULL)
    3065              :         break;
    3066              :     }
    3067              : 
    3068            0 :   gfc_error ("Unable to resolve the specific function %qs at %L",
    3069            0 :              expr->symtree->n.sym->name, &expr->where);
    3070              : 
    3071            0 :   return true;
    3072              : }
    3073              : 
    3074              : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
    3075              :    candidates in CANDIDATES_LEN.  */
    3076              : 
    3077              : static void
    3078          212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
    3079              :                                        char **&candidates,
    3080              :                                        size_t &candidates_len)
    3081              : {
    3082          388 :   gfc_symtree *p;
    3083              : 
    3084          388 :   if (sym == NULL)
    3085              :     return;
    3086          388 :   if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
    3087          126 :       && sym->n.sym->attr.flavor == FL_PROCEDURE)
    3088           51 :     vec_push (candidates, candidates_len, sym->name);
    3089              : 
    3090          388 :   p = sym->left;
    3091          388 :   if (p)
    3092          155 :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3093              : 
    3094          388 :   p = sym->right;
    3095          388 :   if (p)
    3096              :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3097              : }
    3098              : 
    3099              : 
    3100              : /* Lookup function FN fuzzily, taking names in SYMROOT into account.  */
    3101              : 
    3102              : const char*
    3103           57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
    3104              : {
    3105           57 :   char **candidates = NULL;
    3106           57 :   size_t candidates_len = 0;
    3107           57 :   lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
    3108           57 :   return gfc_closest_fuzzy_match (fn, candidates);
    3109              : }
    3110              : 
    3111              : 
    3112              : /* Resolve a procedure call not known to be generic nor specific.  */
    3113              : 
    3114              : static bool
    3115       277629 : resolve_unknown_f (gfc_expr *expr)
    3116              : {
    3117       277629 :   gfc_symbol *sym;
    3118       277629 :   gfc_typespec *ts;
    3119              : 
    3120       277629 :   sym = expr->symtree->n.sym;
    3121              : 
    3122       277629 :   if (sym->attr.dummy)
    3123              :     {
    3124          289 :       sym->attr.proc = PROC_DUMMY;
    3125          289 :       expr->value.function.name = sym->name;
    3126          289 :       goto set_type;
    3127              :     }
    3128              : 
    3129              :   /* See if we have an intrinsic function reference.  */
    3130              : 
    3131       277340 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3132              :     {
    3133       275083 :       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
    3134              :         return true;
    3135              :       return false;
    3136              :     }
    3137              : 
    3138              :   /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
    3139              :   /* Intrinsics were handled above, only non-intrinsics left here.  */
    3140         2257 :   if (sym->attr.flavor == FL_PROCEDURE
    3141         2254 :       && sym->attr.implicit_type
    3142          371 :       && sym->ns
    3143          371 :       && sym->ns->has_implicit_none_export)
    3144              :     {
    3145            3 :           gfc_error ("Missing explicit declaration with EXTERNAL attribute "
    3146              :               "for symbol %qs at %L", sym->name, &sym->declared_at);
    3147            3 :           sym->error = 1;
    3148            3 :           return false;
    3149              :     }
    3150              : 
    3151              :   /* The reference is to an external name.  */
    3152              : 
    3153         2254 :   sym->attr.proc = PROC_EXTERNAL;
    3154         2254 :   expr->value.function.name = sym->name;
    3155         2254 :   expr->value.function.esym = expr->symtree->n.sym;
    3156              : 
    3157         2254 :   if (sym->as != NULL)
    3158              :     {
    3159            1 :       expr->rank = sym->as->rank;
    3160            1 :       expr->corank = sym->as->corank;
    3161              :     }
    3162              : 
    3163              :   /* Type of the expression is either the type of the symbol or the
    3164              :      default type of the symbol.  */
    3165              : 
    3166         2253 : set_type:
    3167         2543 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3168              : 
    3169         2543 :   if (sym->ts.type != BT_UNKNOWN)
    3170         2492 :     expr->ts = sym->ts;
    3171              :   else
    3172              :     {
    3173           51 :       ts = gfc_get_default_type (sym->name, sym->ns);
    3174              : 
    3175           51 :       if (ts->type == BT_UNKNOWN)
    3176              :         {
    3177           41 :           const char *guessed
    3178           41 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    3179           41 :           if (guessed)
    3180            3 :             gfc_error ("Function %qs at %L has no IMPLICIT type"
    3181              :                        "; did you mean %qs?",
    3182              :                        sym->name, &expr->where, guessed);
    3183              :           else
    3184           38 :             gfc_error ("Function %qs at %L has no IMPLICIT type",
    3185              :                        sym->name, &expr->where);
    3186           41 :           return false;
    3187              :         }
    3188              :       else
    3189           10 :         expr->ts = *ts;
    3190              :     }
    3191              : 
    3192              :   return true;
    3193              : }
    3194              : 
    3195              : 
    3196              : /* Return true, if the symbol is an external procedure.  */
    3197              : static bool
    3198       856191 : is_external_proc (gfc_symbol *sym)
    3199              : {
    3200       854476 :   if (!sym->attr.dummy && !sym->attr.contained
    3201       745644 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3202       162408 :         && sym->attr.proc != PROC_ST_FUNCTION
    3203       161813 :         && !sym->attr.proc_pointer
    3204       160607 :         && !sym->attr.use_assoc
    3205       915356 :         && sym->name)
    3206              :     return true;
    3207              : 
    3208              :   return false;
    3209              : }
    3210              : 
    3211              : 
    3212              : /* Figure out if a function reference is pure or not.  Also set the name
    3213              :    of the function for a potential error message.  Return nonzero if the
    3214              :    function is PURE, zero if not.  */
    3215              : static bool
    3216              : pure_stmt_function (gfc_expr *, gfc_symbol *);
    3217              : 
    3218              : bool
    3219       257435 : gfc_pure_function (gfc_expr *e, const char **name)
    3220              : {
    3221       257435 :   bool pure;
    3222       257435 :   gfc_component *comp;
    3223              : 
    3224       257435 :   *name = NULL;
    3225              : 
    3226       257435 :   if (e->symtree != NULL
    3227       257079 :         && e->symtree->n.sym != NULL
    3228       257079 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3229          305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3230              : 
    3231       257130 :   comp = gfc_get_proc_ptr_comp (e);
    3232       257130 :   if (comp)
    3233              :     {
    3234          479 :       pure = gfc_pure (comp->ts.interface);
    3235          479 :       *name = comp->name;
    3236              :     }
    3237       256651 :   else if (e->value.function.esym)
    3238              :     {
    3239        52988 :       pure = gfc_pure (e->value.function.esym);
    3240        52988 :       *name = e->value.function.esym->name;
    3241              :     }
    3242       203663 :   else if (e->value.function.isym)
    3243              :     {
    3244       405186 :       pure = e->value.function.isym->pure
    3245       202593 :              || e->value.function.isym->elemental;
    3246       202593 :       *name = e->value.function.isym->name;
    3247              :     }
    3248         1070 :   else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
    3249              :     {
    3250              :       /* The function has been resolved, but esym is not yet set.
    3251              :          This can happen with functions as dummy argument.  */
    3252          287 :       pure = e->symtree->n.sym->attr.pure;
    3253          287 :       *name = e->symtree->n.sym->name;
    3254              :     }
    3255              :   else
    3256              :     {
    3257              :       /* Implicit functions are not pure.  */
    3258          783 :       pure = 0;
    3259          783 :       *name = e->value.function.name;
    3260              :     }
    3261              : 
    3262              :   return pure;
    3263              : }
    3264              : 
    3265              : 
    3266              : /* Check if the expression is a reference to an implicitly pure function.  */
    3267              : 
    3268              : bool
    3269        38298 : gfc_implicit_pure_function (gfc_expr *e)
    3270              : {
    3271        38298 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3272        38298 :   if (comp)
    3273          463 :     return gfc_implicit_pure (comp->ts.interface);
    3274        37835 :   else if (e->value.function.esym)
    3275        32430 :     return gfc_implicit_pure (e->value.function.esym);
    3276              :   else
    3277              :     return 0;
    3278              : }
    3279              : 
    3280              : 
    3281              : static bool
    3282          981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
    3283              :                  int *f ATTRIBUTE_UNUSED)
    3284              : {
    3285          981 :   const char *name;
    3286              : 
    3287              :   /* Don't bother recursing into other statement functions
    3288              :      since they will be checked individually for purity.  */
    3289          981 :   if (e->expr_type != EXPR_FUNCTION
    3290          343 :         || !e->symtree
    3291          343 :         || e->symtree->n.sym == sym
    3292           20 :         || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3293              :     return false;
    3294              : 
    3295           19 :   return gfc_pure_function (e, &name) ? false : true;
    3296              : }
    3297              : 
    3298              : 
    3299              : static bool
    3300          305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
    3301              : {
    3302          305 :   return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
    3303              : }
    3304              : 
    3305              : 
    3306              : /* Check if an impure function is allowed in the current context. */
    3307              : 
    3308       245493 : static bool check_pure_function (gfc_expr *e)
    3309              : {
    3310       245493 :   const char *name = NULL;
    3311       245493 :   code_stack *stack;
    3312       245493 :   bool saw_block = false;
    3313              : 
    3314              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3315              :      gfc_do_concurrent_flag = 0 when the check for an impure function
    3316              :      occurs.  Check the stack to see if the source code has a nested
    3317              :      BLOCK construct.  */
    3318              : 
    3319       568135 :   for (stack = cs_base; stack; stack = stack->prev)
    3320              :     {
    3321       322644 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3322              :         {
    3323         7462 :           saw_block = true;
    3324         7462 :           continue;
    3325              :         }
    3326              : 
    3327         5304 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3328              :         {
    3329           10 :           bool is_pure;
    3330       322642 :           is_pure = (e->value.function.isym
    3331            9 :                      && (e->value.function.isym->pure
    3332            1 :                          || e->value.function.isym->elemental))
    3333           11 :                     || (e->value.function.esym
    3334            1 :                         && (e->value.function.esym->attr.pure
    3335            1 :                             || e->value.function.esym->attr.elemental));
    3336            2 :           if (!is_pure)
    3337              :             {
    3338            2 :               gfc_error ("Reference to impure function at %L inside a "
    3339              :                          "DO CONCURRENT", &e->where);
    3340            2 :               return false;
    3341              :             }
    3342              :         }
    3343              :     }
    3344              : 
    3345       245491 :   if (!gfc_pure_function (e, &name) && name)
    3346              :     {
    3347        37001 :       if (forall_flag)
    3348              :         {
    3349            4 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3350              :                      "FORALL %s", name, &e->where,
    3351              :                      forall_flag == 2 ? "mask" : "block");
    3352            4 :           return false;
    3353              :         }
    3354        36997 :       else if (gfc_do_concurrent_flag)
    3355              :         {
    3356            2 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3357              :                      "DO CONCURRENT %s", name, &e->where,
    3358              :                      gfc_do_concurrent_flag == 2 ? "mask" : "block");
    3359            2 :           return false;
    3360              :         }
    3361        36995 :       else if (gfc_pure (NULL))
    3362              :         {
    3363            5 :           gfc_error ("Reference to impure function %qs at %L "
    3364              :                      "within a PURE procedure", name, &e->where);
    3365            5 :           return false;
    3366              :         }
    3367        36990 :       if (!gfc_implicit_pure_function (e))
    3368        30476 :         gfc_unset_implicit_pure (NULL);
    3369              :     }
    3370              :   return true;
    3371              : }
    3372              : 
    3373              : 
    3374              : /* Update current procedure's array_outer_dependency flag, considering
    3375              :    a call to procedure SYM.  */
    3376              : 
    3377              : static void
    3378       133234 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
    3379              : {
    3380              :   /* Check to see if this is a sibling function that has not yet
    3381              :      been resolved.  */
    3382       133234 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3383       251126 :   for (; sibling; sibling = sibling->sibling)
    3384              :     {
    3385       125064 :       if (sibling->proc_name == sym)
    3386              :         {
    3387         7172 :           gfc_resolve (sibling);
    3388         7172 :           break;
    3389              :         }
    3390              :     }
    3391              : 
    3392              :   /* If SYM has references to outer arrays, so has the procedure calling
    3393              :      SYM.  If SYM is a procedure pointer, we can assume the worst.  */
    3394       133234 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3395        68176 :       && gfc_current_ns->proc_name)
    3396        68132 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3397       133234 : }
    3398              : 
    3399              : 
    3400              : /* Resolve a function call, which means resolving the arguments, then figuring
    3401              :    out which entity the name refers to.  */
    3402              : 
    3403              : static bool
    3404       346662 : resolve_function (gfc_expr *expr)
    3405              : {
    3406       346662 :   gfc_actual_arglist *arg;
    3407       346662 :   gfc_symbol *sym;
    3408       346662 :   bool t;
    3409       346662 :   int temp;
    3410       346662 :   procedure_type p = PROC_INTRINSIC;
    3411       346662 :   bool no_formal_args;
    3412              : 
    3413       346662 :   sym = NULL;
    3414       346662 :   if (expr->symtree)
    3415       346306 :     sym = expr->symtree->n.sym;
    3416              : 
    3417              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3418       346662 :   if (gfc_is_proc_ptr_comp (expr))
    3419              :     return true;
    3420              : 
    3421              :   /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
    3422              :      another caf_get.  */
    3423       346252 :   if (sym && sym->attr.intrinsic
    3424         8667 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3425         8667 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3426              :     return true;
    3427              : 
    3428       346252 :   if (expr->ref)
    3429              :     {
    3430            1 :       gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
    3431              :                  &expr->where);
    3432            1 :       return false;
    3433              :     }
    3434              : 
    3435       345895 :   if (sym && sym->attr.intrinsic
    3436       354918 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3437              :     return false;
    3438              : 
    3439       346251 :   if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
    3440              :     {
    3441            4 :       gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
    3442            4 :       return false;
    3443              :     }
    3444              : 
    3445              :   /* If this is a deferred TBP with an abstract interface (which may
    3446              :      of course be referenced), expr->value.function.esym will be set.  */
    3447       345891 :   if (sym && sym->attr.abstract && !expr->value.function.esym)
    3448              :     {
    3449            1 :       gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3450              :                  sym->name, &expr->where);
    3451            1 :       return false;
    3452              :     }
    3453              : 
    3454              :   /* If this is a deferred TBP with an abstract interface, its result
    3455              :      cannot be an assumed length character (F2003: C418).  */
    3456       345890 :   if (sym && sym->attr.abstract && sym->attr.function
    3457          192 :       && sym->result->ts.u.cl
    3458          158 :       && sym->result->ts.u.cl->length == NULL
    3459            2 :       && !sym->result->ts.deferred)
    3460              :     {
    3461            1 :       gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
    3462              :                  "character length result (F2008: C418)", sym->name,
    3463              :                  &sym->declared_at);
    3464            1 :       return false;
    3465              :     }
    3466              : 
    3467              :   /* Switch off assumed size checking and do this again for certain kinds
    3468              :      of procedure, once the procedure itself is resolved.  */
    3469       346245 :   need_full_assumed_size++;
    3470              : 
    3471       346245 :   if (expr->symtree && expr->symtree->n.sym)
    3472       345889 :     p = expr->symtree->n.sym->attr.proc;
    3473              : 
    3474       346245 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3475         1105 :     inquiry_argument = true;
    3476       345889 :   no_formal_args = sym && is_external_proc (sym)
    3477       360156 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3478              : 
    3479       346245 :   if (!resolve_actual_arglist (expr->value.function.actual,
    3480              :                                p, no_formal_args))
    3481              :     {
    3482           67 :       inquiry_argument = false;
    3483           67 :       return false;
    3484              :     }
    3485              : 
    3486       346178 :   inquiry_argument = false;
    3487              : 
    3488              :   /* Resume assumed_size checking.  */
    3489       346178 :   need_full_assumed_size--;
    3490              : 
    3491              :   /* If the procedure is external, check for usage.  */
    3492       346178 :   if (sym && is_external_proc (sym))
    3493        13891 :     resolve_global_procedure (sym, &expr->where, 0);
    3494              : 
    3495       346178 :   if (sym && sym->ts.type == BT_CHARACTER
    3496         3347 :       && sym->ts.u.cl
    3497         3253 :       && sym->ts.u.cl->length == NULL
    3498          677 :       && !sym->attr.dummy
    3499          670 :       && !sym->ts.deferred
    3500            2 :       && expr->value.function.esym == NULL
    3501            2 :       && !sym->attr.contained)
    3502              :     {
    3503              :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3504            1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3505              :                  "be used at %L since it is not a dummy argument",
    3506              :                  sym->name, &expr->where);
    3507            1 :       return false;
    3508              :     }
    3509              : 
    3510              :   /* Add and check formal interface when -fc-prototypes-external is in
    3511              :      force, see comment in resolve_call().  */
    3512              : 
    3513       346177 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3514           18 :       && sym->attr.external)
    3515              :     {
    3516           18 :       if (sym->formal)
    3517              :         {
    3518            6 :           bool conflict;
    3519            6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3520              :                                                  sym->formal, 0, 0, 0, NULL);
    3521            6 :           if (conflict)
    3522              :             {
    3523            6 :               sym->ext_dummy_arglist_mismatch = 1;
    3524            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3525              :                            "Different argument lists in external dummy "
    3526              :                            "function %s at %L and %L", sym->name,
    3527              :                            &expr->where, &sym->other_loc);
    3528              :             }
    3529              :         }
    3530           12 :       else if (!sym->formal_resolved)
    3531              :         {
    3532            6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3533            6 :           sym->other_loc = expr->where;
    3534              :         }
    3535              :     }
    3536              :   /* See if function is already resolved.  */
    3537              : 
    3538       346177 :   if (expr->value.function.name != NULL
    3539       334193 :       || expr->value.function.isym != NULL)
    3540              :     {
    3541        12782 :       if (expr->ts.type == BT_UNKNOWN)
    3542            3 :         expr->ts = sym->ts;
    3543              :       t = true;
    3544              :     }
    3545              :   else
    3546              :     {
    3547              :       /* Apply the rules of section 14.1.2.  */
    3548              : 
    3549       333395 :       switch (procedure_kind (sym))
    3550              :         {
    3551        27470 :         case PTYPE_GENERIC:
    3552        27470 :           t = resolve_generic_f (expr);
    3553        27470 :           break;
    3554              : 
    3555        28296 :         case PTYPE_SPECIFIC:
    3556        28296 :           t = resolve_specific_f (expr);
    3557        28296 :           break;
    3558              : 
    3559       277629 :         case PTYPE_UNKNOWN:
    3560       277629 :           t = resolve_unknown_f (expr);
    3561       277629 :           break;
    3562              : 
    3563              :         default:
    3564              :           gfc_internal_error ("resolve_function(): bad function type");
    3565              :         }
    3566              :     }
    3567              : 
    3568              :   /* If the expression is still a function (it might have simplified),
    3569              :      then we check to see if we are calling an elemental function.  */
    3570              : 
    3571       346177 :   if (expr->expr_type != EXPR_FUNCTION)
    3572              :     return t;
    3573              : 
    3574              :   /* Walk the argument list looking for invalid BOZ.  */
    3575       742929 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3576       497892 :     if (arg->expr && arg->expr->ts.type == BT_BOZ)
    3577              :       {
    3578            5 :         gfc_error ("A BOZ literal constant at %L cannot appear as an "
    3579              :                    "actual argument in a function reference",
    3580              :                    &arg->expr->where);
    3581            5 :         return false;
    3582              :       }
    3583              : 
    3584       245037 :   temp = need_full_assumed_size;
    3585       245037 :   need_full_assumed_size = 0;
    3586              : 
    3587       245037 :   if (!resolve_elemental_actual (expr, NULL))
    3588              :     return false;
    3589              : 
    3590       245034 :   if (omp_workshare_flag
    3591           32 :       && expr->value.function.esym
    3592       245039 :       && ! gfc_elemental (expr->value.function.esym))
    3593              :     {
    3594            4 :       gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
    3595            4 :                  "in WORKSHARE construct", expr->value.function.esym->name,
    3596              :                  &expr->where);
    3597            4 :       t = false;
    3598              :     }
    3599              : 
    3600              : #define GENERIC_ID expr->value.function.isym->id
    3601       245030 :   else if (expr->value.function.actual != NULL
    3602       236942 :            && expr->value.function.isym != NULL
    3603       191815 :            && GENERIC_ID != GFC_ISYM_LBOUND
    3604              :            && GENERIC_ID != GFC_ISYM_LCOBOUND
    3605              :            && GENERIC_ID != GFC_ISYM_UCOBOUND
    3606              :            && GENERIC_ID != GFC_ISYM_LEN
    3607              :            && GENERIC_ID != GFC_ISYM_LOC
    3608              :            && GENERIC_ID != GFC_ISYM_C_LOC
    3609              :            && GENERIC_ID != GFC_ISYM_PRESENT)
    3610              :     {
    3611              :       /* Array intrinsics must also have the last upper bound of an
    3612              :          assumed size array argument.  UBOUND and SIZE have to be
    3613              :          excluded from the check if the second argument is anything
    3614              :          than a constant.  */
    3615              : 
    3616       539677 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3617              :         {
    3618       373847 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3619        45953 :               && arg == expr->value.function.actual
    3620        16929 :               && arg->next != NULL && arg->next->expr)
    3621              :             {
    3622         8339 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3623              :                 break;
    3624              : 
    3625         8115 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3626              :                 break;
    3627              : 
    3628         8115 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3629         8115 :                         < arg->expr->rank)
    3630              :                 break;
    3631              :             }
    3632              : 
    3633       371432 :           if (arg->expr != NULL
    3634       247693 :               && arg->expr->rank > 0
    3635       490777 :               && resolve_assumed_size_actual (arg->expr))
    3636              :             return false;
    3637              :         }
    3638              :     }
    3639              : #undef GENERIC_ID
    3640              : 
    3641       245031 :   need_full_assumed_size = temp;
    3642              : 
    3643       245031 :   if (!check_pure_function(expr))
    3644           12 :     t = false;
    3645              : 
    3646              :   /* Functions without the RECURSIVE attribution are not allowed to
    3647              :    * call themselves.  */
    3648       245031 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3649              :     {
    3650        51713 :       gfc_symbol *esym;
    3651        51713 :       esym = expr->value.function.esym;
    3652              : 
    3653        51713 :       if (is_illegal_recursion (esym, gfc_current_ns))
    3654              :       {
    3655            5 :         if (esym->attr.entry && esym->ns->entries)
    3656            3 :           gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
    3657              :                      " function %qs is not RECURSIVE",
    3658            3 :                      esym->name, &expr->where, esym->ns->entries->sym->name);
    3659              :         else
    3660            2 :           gfc_error ("Function %qs at %L cannot be called recursively, as it"
    3661              :                      " is not RECURSIVE", esym->name, &expr->where);
    3662              : 
    3663              :         t = false;
    3664              :       }
    3665              :     }
    3666              : 
    3667              :   /* Character lengths of use associated functions may contains references to
    3668              :      symbols not referenced from the current program unit otherwise.  Make sure
    3669              :      those symbols are marked as referenced.  */
    3670              : 
    3671       245031 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3672         3451 :       && expr->value.function.esym->attr.use_assoc)
    3673              :     {
    3674         1256 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3675              :     }
    3676              : 
    3677              :   /* Make sure that the expression has a typespec that works.  */
    3678       245031 :   if (expr->ts.type == BT_UNKNOWN)
    3679              :     {
    3680          922 :       if (expr->symtree->n.sym->result
    3681          913 :             && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
    3682          561 :             && !expr->symtree->n.sym->result->attr.proc_pointer)
    3683          561 :         expr->ts = expr->symtree->n.sym->result->ts;
    3684              :     }
    3685              : 
    3686              :   /* These derived types with an incomplete namespace, arising from use
    3687              :      association, cause gfc_get_derived_vtab to segfault. If the function
    3688              :      namespace does not suffice, something is badly wrong.  */
    3689       245031 :   if (expr->ts.type == BT_DERIVED
    3690         9588 :       && !expr->ts.u.derived->ns->proc_name)
    3691              :     {
    3692            3 :       gfc_symbol *der;
    3693            3 :       gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
    3694            3 :       if (der)
    3695              :         {
    3696            3 :           expr->ts.u.derived->refs--;
    3697            3 :           expr->ts.u.derived = der;
    3698            3 :           der->refs++;
    3699              :         }
    3700              :       else
    3701            0 :         expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
    3702              :     }
    3703              : 
    3704       245031 :   if (!expr->ref && !expr->value.function.isym)
    3705              :     {
    3706        53095 :       if (expr->value.function.esym)
    3707        52025 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3708              :       else
    3709         1070 :         update_current_proc_array_outer_dependency (sym);
    3710              :     }
    3711       191936 :   else if (expr->ref)
    3712              :     /* typebound procedure: Assume the worst.  */
    3713            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3714              : 
    3715       245031 :   if (expr->value.function.esym
    3716        52025 :       && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    3717           26 :     gfc_warning (OPT_Wdeprecated_declarations,
    3718              :                  "Using function %qs at %L is deprecated",
    3719              :                  sym->name, &expr->where);
    3720              : 
    3721              :   /* Check an external function supplied as a dummy argument has an external
    3722              :      attribute when a program unit uses 'implicit none (external)'.  */
    3723       245031 :   if (expr->expr_type == EXPR_FUNCTION
    3724       245031 :       && expr->symtree
    3725       244675 :       && expr->symtree->n.sym->attr.dummy
    3726          570 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3727       245032 :       && !gfc_is_intrinsic(expr->symtree->n.sym, 0, expr->where))
    3728              :     {
    3729            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    3730              :                  sym->name, &expr->where);
    3731            1 :       return false;
    3732              :     }
    3733              : 
    3734              :   return t;
    3735              : }
    3736              : 
    3737              : 
    3738              : /************* Subroutine resolution *************/
    3739              : 
    3740              : static bool
    3741        77596 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3742              : {
    3743        77596 :   code_stack *stack;
    3744        77596 :   bool saw_block = false;
    3745              : 
    3746        77596 :   if (gfc_pure (sym))
    3747              :     return true;
    3748              : 
    3749              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3750              :      gfc_do_concurrent_flag = 0 when the check for an impure subroutine
    3751              :      occurs.  Walk up the stack to see if the source code has a nested
    3752              :      construct.  */
    3753              : 
    3754       160039 :   for (stack = cs_base; stack; stack = stack->prev)
    3755              :     {
    3756        88094 :       if (stack->current->op == EXEC_BLOCK)
    3757              :         {
    3758         1920 :           saw_block = true;
    3759         1920 :           continue;
    3760              :         }
    3761              : 
    3762        86174 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3763              :         {
    3764              : 
    3765            2 :           bool is_pure = true;
    3766        88094 :           is_pure = sym->attr.pure || sym->attr.elemental;
    3767              : 
    3768            2 :           if (!is_pure)
    3769              :             {
    3770            2 :               gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
    3771              :                          "is not PURE", loc);
    3772            2 :               return false;
    3773              :             }
    3774              :         }
    3775              :     }
    3776              : 
    3777        71945 :   if (forall_flag)
    3778              :     {
    3779            0 :       gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
    3780              :                  name, loc);
    3781            0 :       return false;
    3782              :     }
    3783        71945 :   else if (gfc_do_concurrent_flag)
    3784              :     {
    3785            6 :       gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
    3786              :                  "PURE", name, loc);
    3787            6 :       return false;
    3788              :     }
    3789        71939 :   else if (gfc_pure (NULL))
    3790              :     {
    3791            4 :       gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
    3792            4 :       return false;
    3793              :     }
    3794              : 
    3795        71935 :   gfc_unset_implicit_pure (NULL);
    3796        71935 :   return true;
    3797              : }
    3798              : 
    3799              : 
    3800              : static match
    3801         2811 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3802              : {
    3803         2811 :   gfc_symbol *s;
    3804              : 
    3805         2811 :   if (sym->attr.generic)
    3806              :     {
    3807         2810 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3808         2810 :       if (s != NULL)
    3809              :         {
    3810         2801 :           c->resolved_sym = s;
    3811         2801 :           if (!pure_subroutine (s, s->name, &c->loc))
    3812              :             return MATCH_ERROR;
    3813         2801 :           return MATCH_YES;
    3814              :         }
    3815              : 
    3816              :       /* TODO: Need to search for elemental references in generic interface.  */
    3817              :     }
    3818              : 
    3819           10 :   if (sym->attr.intrinsic)
    3820            1 :     return gfc_intrinsic_sub_interface (c, 0);
    3821              : 
    3822              :   return MATCH_NO;
    3823              : }
    3824              : 
    3825              : 
    3826              : static bool
    3827         2809 : resolve_generic_s (gfc_code *c)
    3828              : {
    3829         2809 :   gfc_symbol *sym;
    3830         2809 :   match m;
    3831              : 
    3832         2809 :   sym = c->symtree->n.sym;
    3833              : 
    3834         2811 :   for (;;)
    3835              :     {
    3836         2811 :       m = resolve_generic_s0 (c, sym);
    3837         2811 :       if (m == MATCH_YES)
    3838              :         return true;
    3839            9 :       else if (m == MATCH_ERROR)
    3840              :         return false;
    3841              : 
    3842            9 : generic:
    3843            9 :       if (sym->ns->parent == NULL)
    3844              :         break;
    3845            3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3846              : 
    3847            3 :       if (sym == NULL)
    3848              :         break;
    3849            2 :       if (!generic_sym (sym))
    3850            0 :         goto generic;
    3851              :     }
    3852              : 
    3853              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3854              :      that possesses a matching interface.  14.1.2.4  */
    3855            7 :   sym = c->symtree->n.sym;
    3856              : 
    3857            7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3858              :     {
    3859            4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3860              :                  sym->name, &c->loc);
    3861            4 :       return false;
    3862              :     }
    3863              : 
    3864            3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3865            3 :   if (m == MATCH_YES)
    3866              :     return true;
    3867            1 :   if (m == MATCH_NO)
    3868            1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3869              :                "intrinsic subroutine interface", sym->name, &c->loc);
    3870              : 
    3871              :   return false;
    3872              : }
    3873              : 
    3874              : 
    3875              : /* Resolve a subroutine call known to be specific.  */
    3876              : 
    3877              : static match
    3878        63081 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3879              : {
    3880        63081 :   match m;
    3881              : 
    3882        63081 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3883              :     {
    3884         5693 :       if (sym->attr.dummy)
    3885              :         {
    3886          257 :           sym->attr.proc = PROC_DUMMY;
    3887          257 :           goto found;
    3888              :         }
    3889              : 
    3890         5436 :       sym->attr.proc = PROC_EXTERNAL;
    3891         5436 :       goto found;
    3892              :     }
    3893              : 
    3894        57388 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3895        57388 :     goto found;
    3896              : 
    3897            0 :   if (sym->attr.intrinsic)
    3898              :     {
    3899            0 :       m = gfc_intrinsic_sub_interface (c, 1);
    3900            0 :       if (m == MATCH_YES)
    3901              :         return MATCH_YES;
    3902            0 :       if (m == MATCH_NO)
    3903            0 :         gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
    3904              :                    "with an intrinsic", sym->name, &c->loc);
    3905              : 
    3906            0 :       return MATCH_ERROR;
    3907              :     }
    3908              : 
    3909              :   return MATCH_NO;
    3910              : 
    3911        63081 : found:
    3912        63081 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3913              : 
    3914        63081 :   c->resolved_sym = sym;
    3915        63081 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3916              :     return MATCH_ERROR;
    3917              : 
    3918              :   return MATCH_YES;
    3919              : }
    3920              : 
    3921              : 
    3922              : static bool
    3923        63081 : resolve_specific_s (gfc_code *c)
    3924              : {
    3925        63081 :   gfc_symbol *sym;
    3926        63081 :   match m;
    3927              : 
    3928        63081 :   sym = c->symtree->n.sym;
    3929              : 
    3930        63081 :   for (;;)
    3931              :     {
    3932        63081 :       m = resolve_specific_s0 (c, sym);
    3933        63081 :       if (m == MATCH_YES)
    3934              :         return true;
    3935            7 :       if (m == MATCH_ERROR)
    3936              :         return false;
    3937              : 
    3938            0 :       if (sym->ns->parent == NULL)
    3939              :         break;
    3940              : 
    3941            0 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3942              : 
    3943            0 :       if (sym == NULL)
    3944              :         break;
    3945              :     }
    3946              : 
    3947            0 :   sym = c->symtree->n.sym;
    3948            0 :   gfc_error ("Unable to resolve the specific subroutine %qs at %L",
    3949              :              sym->name, &c->loc);
    3950              : 
    3951            0 :   return false;
    3952              : }
    3953              : 
    3954              : 
    3955              : /* Resolve a subroutine call not known to be generic nor specific.  */
    3956              : 
    3957              : static bool
    3958        15894 : resolve_unknown_s (gfc_code *c)
    3959              : {
    3960        15894 :   gfc_symbol *sym;
    3961              : 
    3962        15894 :   sym = c->symtree->n.sym;
    3963              : 
    3964        15894 :   if (sym->attr.dummy)
    3965              :     {
    3966           26 :       sym->attr.proc = PROC_DUMMY;
    3967           26 :       goto found;
    3968              :     }
    3969              : 
    3970              :   /* See if we have an intrinsic function reference.  */
    3971              : 
    3972        15868 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3973              :     {
    3974         4303 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3975              :         return true;
    3976          319 :       return false;
    3977              :     }
    3978              : 
    3979              :   /* The reference is to an external name.  */
    3980              : 
    3981        11565 : found:
    3982        11591 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3983              : 
    3984        11591 :   c->resolved_sym = sym;
    3985              : 
    3986        11591 :   return pure_subroutine (sym, sym->name, &c->loc);
    3987              : }
    3988              : 
    3989              : 
    3990              : 
    3991              : static bool
    3992          805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
    3993              :                          gfc_code *c, gfc_namespace *ns)
    3994              : {
    3995          805 :   locus *here;
    3996              : 
    3997              :   /* If the type has been imported then its vtype functions are OK.  */
    3998          805 :   if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
    3999              :     return true;
    4000              : 
    4001              :   if (e)
    4002          791 :     here = &e->where;
    4003              :   else
    4004            7 :     here = &c->loc;
    4005              : 
    4006          798 :   if (s && !s->import_only)
    4007          705 :     s = gfc_find_symtree (ns->sym_root, sym->name);
    4008              : 
    4009          798 :   if (ns->import_state == IMPORT_ONLY
    4010           75 :       && sym->ns != ns
    4011           58 :       && (!s || !s->import_only))
    4012              :     {
    4013           21 :       gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
    4014              :                  "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
    4015           21 :       return false;
    4016              :     }
    4017          777 :   else if (ns->import_state == IMPORT_NONE
    4018           27 :            && sym->ns != ns)
    4019              :     {
    4020           12 :       gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
    4021              :                  "has IMPORT, NONE", sym->name, here);
    4022           12 :       return false;
    4023              :     }
    4024              :   return true;
    4025              : }
    4026              : 
    4027              : 
    4028              : static bool
    4029         7354 : check_import_status (gfc_expr *e)
    4030              : {
    4031         7354 :   gfc_symtree *st;
    4032         7354 :   gfc_ref *ref;
    4033         7354 :   gfc_symbol *sym, *der;
    4034         7354 :   gfc_namespace *ns = gfc_current_ns;
    4035              : 
    4036         7354 :   switch (e->expr_type)
    4037              :     {
    4038          727 :       case EXPR_VARIABLE:
    4039          727 :       case EXPR_FUNCTION:
    4040          727 :       case EXPR_SUBSTRING:
    4041          727 :         sym = e->symtree ? e->symtree->n.sym : NULL;
    4042              : 
    4043              :         /* Check the symbol itself.  */
    4044          727 :         if (sym
    4045          727 :             && !(ns->proc_name
    4046              :                  && (sym == ns->proc_name))
    4047         1450 :             && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
    4048              :           return false;
    4049              : 
    4050              :         /* Check the declared derived type.  */
    4051          717 :         if (sym->ts.type == BT_DERIVED)
    4052              :           {
    4053           16 :             der = sym->ts.u.derived;
    4054           16 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4055              : 
    4056           16 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4057              :               return false;
    4058              :           }
    4059          701 :         else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
    4060              :           {
    4061           44 :             der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
    4062              :                                    : sym->ts.u.derived;
    4063           44 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4064              : 
    4065           44 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4066              :               return false;
    4067              :           }
    4068              : 
    4069              :         /* Check the declared derived types of component references.  */
    4070          724 :         for (ref = e->ref; ref; ref = ref->next)
    4071           20 :           if (ref->type == REF_COMPONENT)
    4072              :             {
    4073           19 :               gfc_component *c = ref->u.c.component;
    4074           19 :               if (c->ts.type == BT_DERIVED)
    4075              :                 {
    4076            7 :                   der = c->ts.u.derived;
    4077            7 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4078            7 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4079              :                     return false;
    4080              :                 }
    4081           12 :               else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
    4082              :                 {
    4083            0 :                   der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
    4084              :                                        : c->ts.u.derived;
    4085            0 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4086            0 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4087              :                     return false;
    4088              :                 }
    4089              :             }
    4090              : 
    4091              :         break;
    4092              : 
    4093            8 :       case EXPR_ARRAY:
    4094            8 :       case EXPR_STRUCTURE:
    4095              :         /* Check the declared derived type.  */
    4096            8 :         if (e->ts.type == BT_DERIVED)
    4097              :           {
    4098            8 :             der = e->ts.u.derived;
    4099            8 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4100              : 
    4101            8 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4102              :               return false;
    4103              :           }
    4104            0 :         else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
    4105              :           {
    4106            0 :             der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
    4107              :                                    : e->ts.u.derived;
    4108            0 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4109              : 
    4110            0 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4111              :               return false;
    4112              :           }
    4113              : 
    4114              :         break;
    4115              : 
    4116              : /* Either not applicable or resolved away
    4117              :       case EXPR_OP:
    4118              :       case EXPR_UNKNOWN:
    4119              :       case EXPR_CONSTANT:
    4120              :       case EXPR_NULL:
    4121              :       case EXPR_COMPCALL:
    4122              :       case EXPR_PPC: */
    4123              : 
    4124              :       default:
    4125              :         break;
    4126              :     }
    4127              : 
    4128              :   return true;
    4129              : }
    4130              : 
    4131              : 
    4132              : /* If an elemental call has an INTENT_IN argument that has a dependency on an
    4133              :    argument which is not INTENT_IN and requires a temporary, build a temporary
    4134              :    for the INTENT_IN actual argument as well.  */
    4135              : 
    4136              : static void
    4137              : add_temp_assign_before_call (gfc_code *, gfc_namespace *, gfc_expr **);
    4138              : 
    4139              : static void
    4140         5257 : resolve_elemental_dependencies (gfc_code *c)
    4141              : {
    4142         5257 :   gfc_actual_arglist *arg1 = c->ext.actual;
    4143         5257 :   gfc_actual_arglist *arg2 = NULL;
    4144         5257 :   gfc_formal_arglist *formal1 = c->resolved_sym->formal;
    4145         5257 :   gfc_formal_arglist *formal2 = NULL;
    4146         5257 :   gfc_expr *expr1;
    4147         5257 :   gfc_expr **expr2;
    4148              : 
    4149        16645 :   for (; arg1 && formal1; arg1 = arg1->next, formal1 = formal1->next)
    4150              :     {
    4151        11388 :       if (formal1->sym
    4152        11388 :           && (formal1->sym->attr.intent == INTENT_IN
    4153         3536 :               || formal1->sym->attr.value))
    4154         8110 :         continue;
    4155              : 
    4156         3278 :       if (!arg1->expr || arg1->expr->expr_type != EXPR_VARIABLE)
    4157            0 :         continue;
    4158              : 
    4159         3278 :       arg2 = c->ext.actual;
    4160         3278 :       formal2 = c->resolved_sym->formal;
    4161        10696 :       for (; arg2 && formal2; arg2 = arg2->next, formal2 = formal2->next)
    4162              :         {
    4163         7418 :           if (arg2 == arg1 || !arg2->expr
    4164         4128 :               || !(formal2->sym && formal2->sym->attr.intent == INTENT_IN))
    4165         3304 :             continue;
    4166              : 
    4167         4114 :           expr1 = arg1->expr;
    4168         4114 :           expr2 = &arg2->expr;
    4169              : 
    4170              :           /* If the arg1 has something horrible like a vector index and
    4171              :              there is a dependency between arg1 and arg2, build a
    4172              :              temporary from arg2, assign the arg2 to it and use the
    4173              :              temporary in the call expression.  */
    4174         2009 :           if (expr1->rank && gfc_ref_needs_temporary_p (expr1->ref)
    4175         4234 :               && gfc_check_dependency (expr1, *expr2, false))
    4176           36 :             add_temp_assign_before_call (c, gfc_current_ns, expr2);
    4177              :         }
    4178              :     }
    4179         5257 : }
    4180              : 
    4181              : /* Resolve a subroutine call.  Although it was tempting to use the same code
    4182              :    for functions, subroutines and functions are stored differently and this
    4183              :    makes things awkward.  */
    4184              : 
    4185              : 
    4186              : static bool
    4187        81929 : resolve_call (gfc_code *c)
    4188              : {
    4189        81929 :   bool t;
    4190        81929 :   procedure_type ptype = PROC_INTRINSIC;
    4191        81929 :   gfc_symbol *csym, *sym;
    4192        81929 :   bool no_formal_args;
    4193              : 
    4194        81929 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4195              : 
    4196        81929 :   if (csym && csym->ts.type != BT_UNKNOWN)
    4197              :     {
    4198            4 :       gfc_error ("%qs at %L has a type, which is not consistent with "
    4199              :                  "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
    4200            4 :       return false;
    4201              :     }
    4202              : 
    4203        81925 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    4204              :     {
    4205        18602 :       gfc_symtree *st;
    4206        18602 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    4207        18602 :       sym = st ? st->n.sym : NULL;
    4208        18602 :       if (sym && csym != sym
    4209            3 :               && sym->ns == gfc_current_ns
    4210            3 :               && sym->attr.flavor == FL_PROCEDURE
    4211            3 :               && sym->attr.contained)
    4212              :         {
    4213            3 :           sym->refs++;
    4214            3 :           if (csym->attr.generic)
    4215            2 :             c->symtree->n.sym = sym;
    4216              :           else
    4217            1 :             c->symtree = st;
    4218            3 :           csym = c->symtree->n.sym;
    4219              :         }
    4220              :     }
    4221              : 
    4222              :   /* If this ia a deferred TBP, c->expr1 will be set.  */
    4223        81925 :   if (!c->expr1 && csym)
    4224              :     {
    4225        80184 :       if (csym->attr.abstract)
    4226              :         {
    4227            1 :           gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    4228              :                     csym->name, &c->loc);
    4229            1 :           return false;
    4230              :         }
    4231              : 
    4232              :       /* Subroutines without the RECURSIVE attribution are not allowed to
    4233              :          call themselves.  */
    4234        80183 :       if (is_illegal_recursion (csym, gfc_current_ns))
    4235              :         {
    4236            4 :           if (csym->attr.entry && csym->ns->entries)
    4237            2 :             gfc_error ("ENTRY %qs at %L cannot be called recursively, "
    4238              :                        "as subroutine %qs is not RECURSIVE",
    4239            2 :                        csym->name, &c->loc, csym->ns->entries->sym->name);
    4240              :           else
    4241            2 :             gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
    4242              :                        "as it is not RECURSIVE", csym->name, &c->loc);
    4243              : 
    4244        81924 :           t = false;
    4245              :         }
    4246              :     }
    4247              : 
    4248              :   /* Switch off assumed size checking and do this again for certain kinds
    4249              :      of procedure, once the procedure itself is resolved.  */
    4250        81924 :   need_full_assumed_size++;
    4251              : 
    4252        81924 :   if (csym)
    4253        81924 :     ptype = csym->attr.proc;
    4254              : 
    4255        81924 :   no_formal_args = csym && is_external_proc (csym)
    4256        15682 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4257        81924 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4258              :     return false;
    4259              : 
    4260              :   /* Resume assumed_size checking.  */
    4261        81890 :   need_full_assumed_size--;
    4262              : 
    4263              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4264              :      check for an 'external' attribute.  */
    4265        81890 :   if (csym->ns->has_implicit_none_export
    4266         4433 :       && csym->attr.external == 0 && csym->attr.dummy == 1)
    4267              :     {
    4268            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    4269              :                  csym->name, &c->loc);
    4270            1 :       return false;
    4271              :     }
    4272              : 
    4273              :   /* If external, check for usage.  */
    4274        81889 :   if (csym && is_external_proc (csym))
    4275        15676 :     resolve_global_procedure (csym, &c->loc, 1);
    4276              : 
    4277              :   /* If we have an external dummy argument, we want to write out its arguments
    4278              :      with -fc-prototypes-external.  Code like
    4279              : 
    4280              :      subroutine foo(a,n)
    4281              :        external a
    4282              :        if (n == 1) call a(1)
    4283              :        if (n == 2) call a(2,3)
    4284              :      end subroutine foo
    4285              : 
    4286              :      is actually legal Fortran, but it is not possible to generate a C23-
    4287              :      compliant prototype for this, so we just record the fact here and
    4288              :      handle that during -fc-prototypes-external processing.  */
    4289              : 
    4290        81889 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4291           14 :       && csym->attr.external)
    4292              :     {
    4293           14 :       if (csym->formal)
    4294              :         {
    4295            6 :           bool conflict;
    4296            6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4297              :                                                  0, 0, 0, NULL);
    4298            6 :           if (conflict)
    4299              :             {
    4300            6 :               csym->ext_dummy_arglist_mismatch = 1;
    4301            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4302              :                            "Different argument lists in external dummy "
    4303              :                            "subroutine %s at %L and %L", csym->name,
    4304              :                            &c->loc, &csym->other_loc);
    4305              :             }
    4306              :         }
    4307            8 :       else if (!csym->formal_resolved)
    4308              :         {
    4309            7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4310            7 :           csym->other_loc = c->loc;
    4311              :         }
    4312              :     }
    4313              : 
    4314        81889 :   t = true;
    4315        81889 :   if (c->resolved_sym == NULL)
    4316              :     {
    4317        81784 :       c->resolved_isym = NULL;
    4318        81784 :       switch (procedure_kind (csym))
    4319              :         {
    4320         2809 :         case PTYPE_GENERIC:
    4321         2809 :           t = resolve_generic_s (c);
    4322         2809 :           break;
    4323              : 
    4324        63081 :         case PTYPE_SPECIFIC:
    4325        63081 :           t = resolve_specific_s (c);
    4326        63081 :           break;
    4327              : 
    4328        15894 :         case PTYPE_UNKNOWN:
    4329        15894 :           t = resolve_unknown_s (c);
    4330        15894 :           break;
    4331              : 
    4332              :         default:
    4333              :           gfc_internal_error ("resolve_subroutine(): bad function type");
    4334              :         }
    4335              :     }
    4336              : 
    4337              :   /* Some checks of elemental subroutine actual arguments.  */
    4338        81888 :   if (!resolve_elemental_actual (NULL, c))
    4339              :     return false;
    4340              : 
    4341              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4342        81880 :   if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args
    4343         6206 :       && c->ext.actual && c->ext.actual->next)
    4344         5257 :     resolve_elemental_dependencies (c);
    4345              : 
    4346        81880 :   if (!c->expr1)
    4347        80139 :     update_current_proc_array_outer_dependency (csym);
    4348              :   else
    4349              :     /* Typebound procedure: Assume the worst.  */
    4350         1741 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4351              : 
    4352        81880 :   if (c->resolved_sym
    4353        81557 :       && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    4354           34 :     gfc_warning (OPT_Wdeprecated_declarations,
    4355              :                  "Using subroutine %qs at %L is deprecated",
    4356              :                  c->resolved_sym->name, &c->loc);
    4357              : 
    4358        81880 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4359        81880 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
    4360            2 :       && csym != gfc_current_ns->proc_name)
    4361            1 :     return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
    4362              : 
    4363              :   return t;
    4364              : }
    4365              : 
    4366              : 
    4367              : /* Compare the shapes of two arrays that have non-NULL shapes.  If both
    4368              :    op1->shape and op2->shape are non-NULL return true if their shapes
    4369              :    match.  If both op1->shape and op2->shape are non-NULL return false
    4370              :    if their shapes do not match.  If either op1->shape or op2->shape is
    4371              :    NULL, return true.  */
    4372              : 
    4373              : static bool
    4374        32707 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4375              : {
    4376        32707 :   bool t;
    4377        32707 :   int i;
    4378              : 
    4379        32707 :   t = true;
    4380              : 
    4381        32707 :   if (op1->shape != NULL && op2->shape != NULL)
    4382              :     {
    4383        43158 :       for (i = 0; i < op1->rank; i++)
    4384              :         {
    4385        23016 :           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
    4386              :            {
    4387            3 :              gfc_error ("Shapes for operands at %L and %L are not conformable",
    4388              :                         &op1->where, &op2->where);
    4389            3 :              t = false;
    4390            3 :              break;
    4391              :            }
    4392              :         }
    4393              :     }
    4394              : 
    4395        32707 :   return t;
    4396              : }
    4397              : 
    4398              : /* Convert a logical operator to the corresponding bitwise intrinsic call.
    4399              :    For example A .AND. B becomes IAND(A, B).  */
    4400              : static gfc_expr *
    4401          668 : logical_to_bitwise (gfc_expr *e)
    4402              : {
    4403          668 :   gfc_expr *tmp, *op1, *op2;
    4404          668 :   gfc_isym_id isym;
    4405          668 :   gfc_actual_arglist *args = NULL;
    4406              : 
    4407          668 :   gcc_assert (e->expr_type == EXPR_OP);
    4408              : 
    4409          668 :   isym = GFC_ISYM_NONE;
    4410          668 :   op1 = e->value.op.op1;
    4411          668 :   op2 = e->value.op.op2;
    4412              : 
    4413          668 :   switch (e->value.op.op)
    4414              :     {
    4415              :     case INTRINSIC_NOT:
    4416              :       isym = GFC_ISYM_NOT;
    4417              :       break;
    4418          126 :     case INTRINSIC_AND:
    4419          126 :       isym = GFC_ISYM_IAND;
    4420          126 :       break;
    4421          127 :     case INTRINSIC_OR:
    4422          127 :       isym = GFC_ISYM_IOR;
    4423          127 :       break;
    4424          270 :     case INTRINSIC_NEQV:
    4425          270 :       isym = GFC_ISYM_IEOR;
    4426          270 :       break;
    4427          126 :     case INTRINSIC_EQV:
    4428              :       /* "Bitwise eqv" is just the complement of NEQV === IEOR.
    4429              :          Change the old expression to NEQV, which will get replaced by IEOR,
    4430              :          and wrap it in NOT.  */
    4431          126 :       tmp = gfc_copy_expr (e);
    4432          126 :       tmp->value.op.op = INTRINSIC_NEQV;
    4433          126 :       tmp = logical_to_bitwise (tmp);
    4434          126 :       isym = GFC_ISYM_NOT;
    4435          126 :       op1 = tmp;
    4436          126 :       op2 = NULL;
    4437          126 :       break;
    4438            0 :     default:
    4439            0 :       gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
    4440              :     }
    4441              : 
    4442              :   /* Inherit the original operation's operands as arguments.  */
    4443          668 :   args = gfc_get_actual_arglist ();
    4444          668 :   args->expr = op1;
    4445          668 :   if (op2)
    4446              :     {
    4447          523 :       args->next = gfc_get_actual_arglist ();
    4448          523 :       args->next->expr = op2;
    4449              :     }
    4450              : 
    4451              :   /* Convert the expression to a function call.  */
    4452          668 :   e->expr_type = EXPR_FUNCTION;
    4453          668 :   e->value.function.actual = args;
    4454          668 :   e->value.function.isym = gfc_intrinsic_function_by_id (isym);
    4455          668 :   e->value.function.name = e->value.function.isym->name;
    4456          668 :   e->value.function.esym = NULL;
    4457              : 
    4458              :   /* Make up a pre-resolved function call symtree if we need to.  */
    4459          668 :   if (!e->symtree || !e->symtree->n.sym)
    4460              :     {
    4461          668 :       gfc_symbol *sym;
    4462          668 :       gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
    4463          668 :       sym = e->symtree->n.sym;
    4464          668 :       sym->result = sym;
    4465          668 :       sym->attr.flavor = FL_PROCEDURE;
    4466          668 :       sym->attr.function = 1;
    4467          668 :       sym->attr.elemental = 1;
    4468          668 :       sym->attr.pure = 1;
    4469          668 :       sym->attr.referenced = 1;
    4470          668 :       gfc_intrinsic_symbol (sym);
    4471          668 :       gfc_commit_symbol (sym);
    4472              :     }
    4473              : 
    4474          668 :   args->name = e->value.function.isym->formal->name;
    4475          668 :   if (e->value.function.isym->formal->next)
    4476          523 :     args->next->name = e->value.function.isym->formal->next->name;
    4477              : 
    4478          668 :   return e;
    4479              : }
    4480              : 
    4481              : /* Recursively append candidate UOP to CANDIDATES.  Store the number of
    4482              :    candidates in CANDIDATES_LEN.  */
    4483              : static void
    4484          114 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4485              :                                   char **&candidates,
    4486              :                                   size_t &candidates_len)
    4487              : {
    4488          116 :   gfc_symtree *p;
    4489              : 
    4490          116 :   if (uop == NULL)
    4491              :     return;
    4492              : 
    4493              :   /* Not sure how to properly filter here.  Use all for a start.
    4494              :      n.uop.op is NULL for empty interface operators (is that legal?) disregard
    4495              :      these as i suppose they don't make terribly sense.  */
    4496              : 
    4497          116 :   if (uop->n.uop->op != NULL)
    4498            2 :     vec_push (candidates, candidates_len, uop->name);
    4499              : 
    4500          116 :   p = uop->left;
    4501          116 :   if (p)
    4502           36 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4503              : 
    4504          116 :   p = uop->right;
    4505          116 :   if (p)
    4506              :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4507              : }
    4508              : 
    4509              : /* Lookup user-operator OP fuzzily, taking names in UOP into account.  */
    4510              : 
    4511              : static const char*
    4512           78 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4513              : {
    4514           78 :   char **candidates = NULL;
    4515           78 :   size_t candidates_len = 0;
    4516           78 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4517           78 :   return gfc_closest_fuzzy_match (op, candidates);
    4518              : }
    4519              : 
    4520              : 
    4521              : /* Callback finding an impure function as an operand to an .and. or
    4522              :    .or.  expression.  Remember the last function warned about to
    4523              :    avoid double warnings when recursing.  */
    4524              : 
    4525              : static int
    4526       193480 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4527              :                           void *data)
    4528              : {
    4529       193480 :   gfc_expr *f = *e;
    4530       193480 :   const char *name;
    4531       193480 :   static gfc_expr *last = NULL;
    4532       193480 :   bool *found = (bool *) data;
    4533              : 
    4534       193480 :   if (f->expr_type == EXPR_FUNCTION)
    4535              :     {
    4536        11913 :       *found = 1;
    4537        11913 :       if (f != last && !gfc_pure_function (f, &name)
    4538        13216 :           && !gfc_implicit_pure_function (f))
    4539              :         {
    4540         1164 :           if (name)
    4541         1164 :             gfc_warning (OPT_Wfunction_elimination,
    4542              :                          "Impure function %qs at %L might not be evaluated",
    4543              :                          name, &f->where);
    4544              :           else
    4545            0 :             gfc_warning (OPT_Wfunction_elimination,
    4546              :                          "Impure function at %L might not be evaluated",
    4547              :                          &f->where);
    4548              :         }
    4549        11913 :       last = f;
    4550              :     }
    4551              : 
    4552       193480 :   return 0;
    4553              : }
    4554              : 
    4555              : /* Return true if TYPE is character based, false otherwise.  */
    4556              : 
    4557              : static int
    4558         1373 : is_character_based (bt type)
    4559              : {
    4560         1373 :   return type == BT_CHARACTER || type == BT_HOLLERITH;
    4561              : }
    4562              : 
    4563              : 
    4564              : /* If expression is a hollerith, convert it to character and issue a warning
    4565              :    for the conversion.  */
    4566              : 
    4567              : static void
    4568          408 : convert_hollerith_to_character (gfc_expr *e)
    4569              : {
    4570          408 :   if (e->ts.type == BT_HOLLERITH)
    4571              :     {
    4572          108 :       gfc_typespec t;
    4573          108 :       gfc_clear_ts (&t);
    4574          108 :       t.type = BT_CHARACTER;
    4575          108 :       t.kind = e->ts.kind;
    4576          108 :       gfc_convert_type_warn (e, &t, 2, 1);
    4577              :     }
    4578          408 : }
    4579              : 
    4580              : /* Convert to numeric and issue a warning for the conversion.  */
    4581              : 
    4582              : static void
    4583          240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
    4584              : {
    4585          240 :   gfc_typespec t;
    4586          240 :   gfc_clear_ts (&t);
    4587          240 :   t.type = b->ts.type;
    4588          240 :   t.kind = b->ts.kind;
    4589          240 :   gfc_convert_type_warn (a, &t, 2, 1);
    4590          240 : }
    4591              : 
    4592              : /* Resolve an operator expression node.  This can involve replacing the
    4593              :    operation with a user defined function call.  CHECK_INTERFACES is a
    4594              :    helper macro.  */
    4595              : 
    4596              : #define CHECK_INTERFACES \
    4597              :   { \
    4598              :     match m = gfc_extend_expr (e); \
    4599              :     if (m == MATCH_YES) \
    4600              :       return true; \
    4601              :     if (m == MATCH_ERROR) \
    4602              :       return false; \
    4603              :   }
    4604              : 
    4605              : static bool
    4606       534529 : resolve_operator (gfc_expr *e)
    4607              : {
    4608       534529 :   gfc_expr *op1, *op2;
    4609              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4610       534529 :   bool t = true;
    4611              : 
    4612              :   /* Reduce stacked parentheses to single pair  */
    4613       534529 :   while (e->expr_type == EXPR_OP
    4614       534687 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4615        23594 :          && e->value.op.op1->expr_type == EXPR_OP
    4616       551532 :          && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
    4617              :     {
    4618          158 :       gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
    4619          158 :       gfc_replace_expr (e, tmp);
    4620              :     }
    4621              : 
    4622              :   /* Resolve all subnodes-- give them types.  */
    4623              : 
    4624       534529 :   switch (e->value.op.op)
    4625              :     {
    4626       482360 :     default:
    4627       482360 :       if (!gfc_resolve_expr (e->value.op.op2))
    4628       534529 :         t = false;
    4629              : 
    4630              :     /* Fall through.  */
    4631              : 
    4632       534529 :     case INTRINSIC_NOT:
    4633       534529 :     case INTRINSIC_UPLUS:
    4634       534529 :     case INTRINSIC_UMINUS:
    4635       534529 :     case INTRINSIC_PARENTHESES:
    4636       534529 :       if (!gfc_resolve_expr (e->value.op.op1))
    4637              :         return false;
    4638       534368 :       if (e->value.op.op1
    4639       534359 :           && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
    4640              :         {
    4641            0 :           gfc_error ("BOZ literal constant at %L cannot be an operand of "
    4642            0 :                      "unary operator %qs", &e->value.op.op1->where,
    4643              :                      gfc_op2string (e->value.op.op));
    4644            0 :           return false;
    4645              :         }
    4646       534368 :       if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
    4647            6 :           && e->value.op.op == INTRINSIC_UMINUS)
    4648              :         {
    4649            2 :           gfc_error ("Negation of unsigned expression at %L not permitted ",
    4650              :                      &e->value.op.op1->where);
    4651            2 :           return false;
    4652              :         }
    4653       534366 :       break;
    4654              :     }
    4655              : 
    4656              :   /* Typecheck the new node.  */
    4657              : 
    4658       534366 :   op1 = e->value.op.op1;
    4659       534366 :   op2 = e->value.op.op2;
    4660       534366 :   if (op1 == NULL && op2 == NULL)
    4661              :     return false;
    4662              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4663       534357 :   if (t == false)
    4664              :     return false;
    4665              : 
    4666              :   /* op1 and op2 cannot both be BOZ.  */
    4667       534291 :   if (op1 && op1->ts.type == BT_BOZ
    4668            0 :       && op2 && op2->ts.type == BT_BOZ)
    4669              :     {
    4670            0 :       gfc_error ("Operands at %L and %L cannot appear as operands of "
    4671            0 :                  "binary operator %qs", &op1->where, &op2->where,
    4672              :                  gfc_op2string (e->value.op.op));
    4673            0 :       return false;
    4674              :     }
    4675              : 
    4676       534291 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4677       534289 :       || (op2 && op2->expr_type == EXPR_NULL))
    4678              :     {
    4679            3 :       CHECK_INTERFACES
    4680            3 :       gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
    4681            3 :       return false;
    4682              :     }
    4683              : 
    4684       534288 :   switch (e->value.op.op)
    4685              :     {
    4686         8178 :     case INTRINSIC_UPLUS:
    4687         8178 :     case INTRINSIC_UMINUS:
    4688         8178 :       if (op1->ts.type == BT_INTEGER
    4689              :           || op1->ts.type == BT_REAL
    4690              :           || op1->ts.type == BT_COMPLEX
    4691              :           || op1->ts.type == BT_UNSIGNED)
    4692              :         {
    4693         8109 :           e->ts = op1->ts;
    4694         8109 :           break;
    4695              :         }
    4696              : 
    4697           69 :       CHECK_INTERFACES
    4698           43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4699              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4700           43 :       return false;
    4701              : 
    4702       155776 :     case INTRINSIC_POWER:
    4703       155776 :     case INTRINSIC_PLUS:
    4704       155776 :     case INTRINSIC_MINUS:
    4705       155776 :     case INTRINSIC_TIMES:
    4706       155776 :     case INTRINSIC_DIVIDE:
    4707              : 
    4708              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4709              :              conversion.  */
    4710       155776 :       if (flag_unsigned &&  gfc_invalid_unsigned_ops (op1, op2))
    4711              :         {
    4712            3 :           CHECK_INTERFACES
    4713            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are "
    4714              :                      "%s/%s", gfc_op2string (e->value.op.op), &e->where,
    4715              :                      gfc_typename (op1), gfc_typename (op2));
    4716            3 :           return false;
    4717              :         }
    4718              : 
    4719       155773 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4720              :         {
    4721              :           /* Do not perform conversions if operands are not conformable as
    4722              :              required for the binary intrinsic operators (F2018:10.1.5).
    4723              :              Defer to a possibly overloading user-defined operator.  */
    4724       155319 :           if (!gfc_op_rank_conformable (op1, op2))
    4725              :             {
    4726           36 :               CHECK_INTERFACES
    4727            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4728            0 :                          &op1->where, &op2->where);
    4729            0 :               return false;
    4730              :             }
    4731              : 
    4732       155283 :           gfc_type_convert_binary (e, 1);
    4733       155283 :           break;
    4734              :         }
    4735              : 
    4736          454 :       if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
    4737              :         {
    4738          225 :           CHECK_INTERFACES
    4739            2 :           gfc_error ("Unexpected derived-type entities in binary intrinsic "
    4740              :                      "numeric operator %qs at %L",
    4741              :                      gfc_op2string (e->value.op.op), &e->where);
    4742            2 :           return false;
    4743              :         }
    4744              :       else
    4745              :         {
    4746          229 :           CHECK_INTERFACES
    4747            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
    4748              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4749              :                      gfc_typename (op2));
    4750            3 :           return false;
    4751              :         }
    4752              : 
    4753         2279 :     case INTRINSIC_CONCAT:
    4754         2279 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4755         2254 :           && op1->ts.kind == op2->ts.kind)
    4756              :         {
    4757         2245 :           e->ts.type = BT_CHARACTER;
    4758         2245 :           e->ts.kind = op1->ts.kind;
    4759         2245 :           break;
    4760              :         }
    4761              : 
    4762           34 :       CHECK_INTERFACES
    4763           10 :       gfc_error ("Operands of string concatenation operator at %L are %s/%s",
    4764              :                  &e->where, gfc_typename (op1), gfc_typename (op2));
    4765           10 :       return false;
    4766              : 
    4767        69740 :     case INTRINSIC_AND:
    4768        69740 :     case INTRINSIC_OR:
    4769        69740 :     case INTRINSIC_EQV:
    4770        69740 :     case INTRINSIC_NEQV:
    4771        69740 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4772              :         {
    4773        69189 :           e->ts.type = BT_LOGICAL;
    4774        69189 :           e->ts.kind = gfc_kind_max (op1, op2);
    4775        69189 :           if (op1->ts.kind < e->ts.kind)
    4776          140 :             gfc_convert_type (op1, &e->ts, 2);
    4777        69049 :           else if (op2->ts.kind < e->ts.kind)
    4778          117 :             gfc_convert_type (op2, &e->ts, 2);
    4779              : 
    4780        69189 :           if (flag_frontend_optimize &&
    4781        58123 :             (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
    4782              :             {
    4783              :               /* Warn about short-circuiting
    4784              :                  with impure function as second operand.  */
    4785        52118 :               bool op2_f = false;
    4786        52118 :               gfc_expr_walker (&op2, impure_function_callback, &op2_f);
    4787              :             }
    4788              :           break;
    4789              :         }
    4790              : 
    4791              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4792          551 :       else if (flag_dec
    4793          523 :                && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
    4794              :         {
    4795          523 :           e->ts.type = BT_INTEGER;
    4796          523 :           e->ts.kind = gfc_kind_max (op1, op2);
    4797          523 :           if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
    4798          289 :             gfc_convert_type (op1, &e->ts, 1);
    4799          523 :           if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
    4800          144 :             gfc_convert_type (op2, &e->ts, 1);
    4801          523 :           e = logical_to_bitwise (e);
    4802          523 :           goto simplify_op;
    4803              :         }
    4804              : 
    4805           28 :       CHECK_INTERFACES
    4806           16 :       gfc_error ("Operands of logical operator %qs at %L are %s/%s",
    4807              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4808              :                  gfc_typename (op2));
    4809           16 :       return false;
    4810              : 
    4811        20539 :     case INTRINSIC_NOT:
    4812              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4813        20539 :       if (flag_dec && op1->ts.type == BT_INTEGER)
    4814              :         {
    4815           19 :           e->ts.type = BT_INTEGER;
    4816           19 :           e->ts.kind = op1->ts.kind;
    4817           19 :           e = logical_to_bitwise (e);
    4818           19 :           goto simplify_op;
    4819              :         }
    4820              : 
    4821        20520 :       if (op1->ts.type == BT_LOGICAL)
    4822              :         {
    4823        20514 :           e->ts.type = BT_LOGICAL;
    4824        20514 :           e->ts.kind = op1->ts.kind;
    4825        20514 :           break;
    4826              :         }
    4827              : 
    4828            6 :       CHECK_INTERFACES
    4829            3 :       gfc_error ("Operand of .not. operator at %L is %s", &e->where,
    4830              :                  gfc_typename (op1));
    4831            3 :       return false;
    4832              : 
    4833        21487 :     case INTRINSIC_GT:
    4834        21487 :     case INTRINSIC_GT_OS:
    4835        21487 :     case INTRINSIC_GE:
    4836        21487 :     case INTRINSIC_GE_OS:
    4837        21487 :     case INTRINSIC_LT:
    4838        21487 :     case INTRINSIC_LT_OS:
    4839        21487 :     case INTRINSIC_LE:
    4840        21487 :     case INTRINSIC_LE_OS:
    4841        21487 :       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
    4842              :         {
    4843           18 :           CHECK_INTERFACES
    4844            0 :           gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
    4845            0 :           return false;
    4846              :         }
    4847              : 
    4848              :       /* Fall through.  */
    4849              : 
    4850       254058 :     case INTRINSIC_EQ:
    4851       254058 :     case INTRINSIC_EQ_OS:
    4852       254058 :     case INTRINSIC_NE:
    4853       254058 :     case INTRINSIC_NE_OS:
    4854              : 
    4855       254058 :       if (flag_dec
    4856         1038 :           && is_character_based (op1->ts.type)
    4857       254393 :           && is_character_based (op2->ts.type))
    4858              :         {
    4859          204 :           convert_hollerith_to_character (op1);
    4860          204 :           convert_hollerith_to_character (op2);
    4861              :         }
    4862              : 
    4863       254058 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4864        38553 :           && op1->ts.kind == op2->ts.kind)
    4865              :         {
    4866        38516 :           e->ts.type = BT_LOGICAL;
    4867        38516 :           e->ts.kind = gfc_default_logical_kind;
    4868        38516 :           break;
    4869              :         }
    4870              : 
    4871              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4872       215542 :       if (op1->ts.type == BT_BOZ)
    4873              :         {
    4874            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
    4875              :                                "as an operand of a relational operator"),
    4876              :                                &op1->where))
    4877              :             return false;
    4878              : 
    4879            0 :           if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
    4880              :             return false;
    4881              : 
    4882            0 :           if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
    4883              :             return false;
    4884              :         }
    4885              : 
    4886              :       /* If op2 is BOZ, then op1 is not!.  Try to convert to type of op2. */
    4887       215542 :       if (op2->ts.type == BT_BOZ)
    4888              :         {
    4889            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
    4890              :                                " as an operand of a relational operator"),
    4891              :                                 &op2->where))
    4892              :             return false;
    4893              : 
    4894            0 :           if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
    4895              :             return false;
    4896              : 
    4897            0 :           if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
    4898              :             return false;
    4899              :         }
    4900       215542 :       if (flag_dec
    4901       215542 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4902          120 :         convert_to_numeric (op1, op2);
    4903              : 
    4904       215542 :       if (flag_dec
    4905       215542 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4906          120 :         convert_to_numeric (op2, op1);
    4907              : 
    4908       215542 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4909              :         {
    4910              :           /* Do not perform conversions if operands are not conformable as
    4911              :              required for the binary intrinsic operators (F2018:10.1.5).
    4912              :              Defer to a possibly overloading user-defined operator.  */
    4913       214413 :           if (!gfc_op_rank_conformable (op1, op2))
    4914              :             {
    4915           70 :               CHECK_INTERFACES
    4916            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4917            0 :                          &op1->where, &op2->where);
    4918            0 :               return false;
    4919              :             }
    4920              : 
    4921       214343 :           if (flag_unsigned  && gfc_invalid_unsigned_ops (op1, op2))
    4922              :             {
    4923            1 :               CHECK_INTERFACES
    4924            1 :               gfc_error ("Inconsistent types for operator at %L and %L: "
    4925            1 :                          "%s and %s", &op1->where, &op2->where,
    4926              :                          gfc_typename (op1), gfc_typename (op2));
    4927            1 :               return false;
    4928              :             }
    4929              : 
    4930       214342 :           gfc_type_convert_binary (e, 1);
    4931              : 
    4932       214342 :           e->ts.type = BT_LOGICAL;
    4933       214342 :           e->ts.kind = gfc_default_logical_kind;
    4934              : 
    4935       214342 :           if (warn_compare_reals)
    4936              :             {
    4937           70 :               gfc_intrinsic_op op = e->value.op.op;
    4938              : 
    4939              :               /* Type conversion has made sure that the types of op1 and op2
    4940              :                  agree, so it is only necessary to check the first one.   */
    4941           70 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4942           13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4943            6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4944              :                 {
    4945           13 :                   const char *msg;
    4946              : 
    4947           13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4948              :                     msg = G_("Equality comparison for %s at %L");
    4949              :                   else
    4950            6 :                     msg = G_("Inequality comparison for %s at %L");
    4951              : 
    4952           13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4953              :                                gfc_typename (op1), &op1->where);
    4954              :                 }
    4955              :             }
    4956              : 
    4957              :           break;
    4958              :         }
    4959              : 
    4960         1129 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4961              :         {
    4962            2 :           CHECK_INTERFACES
    4963            4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4964              :                      &e->where,
    4965            2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4966              :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4967            2 :         }
    4968              :       else
    4969              :         {
    4970         1127 :           CHECK_INTERFACES
    4971          113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4972              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4973              :                      gfc_typename (op2));
    4974              :         }
    4975              : 
    4976              :       return false;
    4977              : 
    4978          303 :     case INTRINSIC_USER:
    4979          303 :       if (e->value.op.uop->op == NULL)
    4980              :         {
    4981           78 :           const char *name = e->value.op.uop->name;
    4982           78 :           const char *guessed;
    4983           78 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4984           78 :           CHECK_INTERFACES
    4985            5 :           if (guessed)
    4986            1 :             gfc_error ("Unknown operator %qs at %L; did you mean "
    4987              :                         "%qs?", name, &e->where, guessed);
    4988              :           else
    4989            4 :             gfc_error ("Unknown operator %qs at %L", name, &e->where);
    4990              :         }
    4991          225 :       else if (op2 == NULL)
    4992              :         {
    4993           48 :           CHECK_INTERFACES
    4994            0 :           gfc_error ("Operand of user operator %qs at %L is %s",
    4995            0 :                   e->value.op.uop->name, &e->where, gfc_typename (op1));
    4996              :         }
    4997              :       else
    4998              :         {
    4999          177 :           e->value.op.uop->op->sym->attr.referenced = 1;
    5000          177 :           CHECK_INTERFACES
    5001            5 :           gfc_error ("Operands of user operator %qs at %L are %s/%s",
    5002            5 :                     e->value.op.uop->name, &e->where, gfc_typename (op1),
    5003              :                     gfc_typename (op2));
    5004              :         }
    5005              : 
    5006              :       return false;
    5007              : 
    5008        23397 :     case INTRINSIC_PARENTHESES:
    5009        23397 :       e->ts = op1->ts;
    5010        23397 :       if (e->ts.type == BT_CHARACTER)
    5011          321 :         e->ts.u.cl = op1->ts.u.cl;
    5012              :       break;
    5013              : 
    5014            0 :     default:
    5015            0 :       gfc_internal_error ("resolve_operator(): Bad intrinsic");
    5016              :     }
    5017              : 
    5018              :   /* Deal with arrayness of an operand through an operator.  */
    5019              : 
    5020       531595 :   switch (e->value.op.op)
    5021              :     {
    5022       479575 :     case INTRINSIC_PLUS:
    5023       479575 :     case INTRINSIC_MINUS:
    5024       479575 :     case INTRINSIC_TIMES:
    5025       479575 :     case INTRINSIC_DIVIDE:
    5026       479575 :     case INTRINSIC_POWER:
    5027       479575 :     case INTRINSIC_CONCAT:
    5028       479575 :     case INTRINSIC_AND:
    5029       479575 :     case INTRINSIC_OR:
    5030       479575 :     case INTRINSIC_EQV:
    5031       479575 :     case INTRINSIC_NEQV:
    5032       479575 :     case INTRINSIC_EQ:
    5033       479575 :     case INTRINSIC_EQ_OS:
    5034       479575 :     case INTRINSIC_NE:
    5035       479575 :     case INTRINSIC_NE_OS:
    5036       479575 :     case INTRINSIC_GT:
    5037       479575 :     case INTRINSIC_GT_OS:
    5038       479575 :     case INTRINSIC_GE:
    5039       479575 :     case INTRINSIC_GE_OS:
    5040       479575 :     case INTRINSIC_LT:
    5041       479575 :     case INTRINSIC_LT_OS:
    5042       479575 :     case INTRINSIC_LE:
    5043       479575 :     case INTRINSIC_LE_OS:
    5044              : 
    5045       479575 :       if (op1->rank == 0 && op2->rank == 0)
    5046       426983 :         e->rank = 0;
    5047              : 
    5048       479575 :       if (op1->rank == 0 && op2->rank != 0)
    5049              :         {
    5050         2613 :           e->rank = op2->rank;
    5051              : 
    5052         2613 :           if (e->shape == NULL)
    5053         2583 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    5054              :         }
    5055              : 
    5056       479575 :       if (op1->rank != 0 && op2->rank == 0)
    5057              :         {
    5058        17211 :           e->rank = op1->rank;
    5059              : 
    5060        17211 :           if (e->shape == NULL)
    5061        17187 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5062              :         }
    5063              : 
    5064       479575 :       if (op1->rank != 0 && op2->rank != 0)
    5065              :         {
    5066        32768 :           if (op1->rank == op2->rank)
    5067              :             {
    5068        32768 :               e->rank = op1->rank;
    5069        32768 :               if (e->shape == NULL)
    5070              :                 {
    5071        32707 :                   t = compare_shapes (op1, op2);
    5072        32707 :                   if (!t)
    5073            3 :                     e->shape = NULL;
    5074              :                   else
    5075        32704 :                     e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5076              :                 }
    5077              :             }
    5078              :           else
    5079              :             {
    5080              :               /* Allow higher level expressions to work.  */
    5081            0 :               e->rank = 0;
    5082              : 
    5083              :               /* Try user-defined operators, and otherwise throw an error.  */
    5084            0 :               CHECK_INTERFACES
    5085            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    5086            0 :                          &op1->where, &op2->where);
    5087            0 :               return false;
    5088              :             }
    5089              :         }
    5090              :       break;
    5091              : 
    5092        52020 :     case INTRINSIC_PARENTHESES:
    5093        52020 :     case INTRINSIC_NOT:
    5094        52020 :     case INTRINSIC_UPLUS:
    5095        52020 :     case INTRINSIC_UMINUS:
    5096              :       /* Simply copy arrayness attribute */
    5097        52020 :       e->rank = op1->rank;
    5098        52020 :       e->corank = op1->corank;
    5099              : 
    5100        52020 :       if (e->shape == NULL)
    5101        52012 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5102              : 
    5103              :       break;
    5104              : 
    5105              :     default:
    5106              :       break;
    5107              :     }
    5108              : 
    5109       532137 : simplify_op:
    5110              : 
    5111              :   /* Attempt to simplify the expression.  */
    5112            3 :   if (t)
    5113              :     {
    5114       532134 :       t = gfc_simplify_expr (e, 0);
    5115              :       /* Some calls do not succeed in simplification and return false
    5116              :          even though there is no error; e.g. variable references to
    5117              :          PARAMETER arrays.  */
    5118       532134 :       if (!gfc_is_constant_expr (e))
    5119       486166 :         t = true;
    5120              :     }
    5121              :   return t;
    5122              : }
    5123              : 
    5124              : static bool
    5125          170 : resolve_conditional (gfc_expr *expr)
    5126              : {
    5127          170 :   gfc_expr *condition, *true_expr, *false_expr;
    5128              : 
    5129          170 :   condition = expr->value.conditional.condition;
    5130          170 :   true_expr = expr->value.conditional.true_expr;
    5131          170 :   false_expr = expr->value.conditional.false_expr;
    5132              : 
    5133          340 :   if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
    5134          340 :       || !gfc_resolve_expr (false_expr))
    5135            0 :     return false;
    5136              : 
    5137          170 :   if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
    5138              :     {
    5139            2 :       gfc_error (
    5140              :         "Condition in conditional expression must be a scalar logical at %L",
    5141              :         &condition->where);
    5142            2 :       return false;
    5143              :     }
    5144              : 
    5145          168 :   if (true_expr->ts.type != false_expr->ts.type)
    5146              :     {
    5147            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5148              :                  "must have the same declared type",
    5149              :                  &true_expr->where, &false_expr->where);
    5150            1 :       return false;
    5151              :     }
    5152              : 
    5153          167 :   if (true_expr->ts.kind != false_expr->ts.kind)
    5154              :     {
    5155            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5156              :                  "must have the same kind parameter",
    5157              :                  &true_expr->where, &false_expr->where);
    5158            1 :       return false;
    5159              :     }
    5160              : 
    5161          166 :   if (true_expr->rank != false_expr->rank)
    5162              :     {
    5163            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5164              :                  "must have the same rank",
    5165              :                  &true_expr->where, &false_expr->where);
    5166            1 :       return false;
    5167              :     }
    5168              : 
    5169              :   /* TODO: support more data types for conditional expressions  */
    5170          165 :   if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
    5171          165 :       && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
    5172           67 :       && true_expr->ts.type != BT_CHARACTER)
    5173              :     {
    5174            1 :       gfc_error (
    5175              :         "Sorry, only integer, logical, real, complex and character types are "
    5176              :         "currently supported for conditional expressions at %L",
    5177              :         &expr->where);
    5178            1 :       return false;
    5179              :     }
    5180              : 
    5181              :   /* TODO: support arrays in conditional expressions  */
    5182          164 :   if (true_expr->rank > 0)
    5183              :     {
    5184            1 :       gfc_error ("Sorry, array is currently unsupported for conditional "
    5185              :                  "expressions at %L",
    5186              :                  &expr->where);
    5187            1 :       return false;
    5188              :     }
    5189              : 
    5190          163 :   expr->ts = true_expr->ts;
    5191          163 :   expr->rank = true_expr->rank;
    5192          163 :   return true;
    5193              : }
    5194              : 
    5195              : /************** Array resolution subroutines **************/
    5196              : 
    5197              : enum compare_result
    5198              : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
    5199              : 
    5200              : /* Compare two integer expressions.  */
    5201              : 
    5202              : static compare_result
    5203       469879 : compare_bound (gfc_expr *a, gfc_expr *b)
    5204              : {
    5205       469879 :   int i;
    5206              : 
    5207       469879 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5208       309026 :       || b == NULL || b->expr_type != EXPR_CONSTANT)
    5209              :     return CMP_UNKNOWN;
    5210              : 
    5211              :   /* If either of the types isn't INTEGER, we must have
    5212              :      raised an error earlier.  */
    5213              : 
    5214       213456 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5215              :     return CMP_UNKNOWN;
    5216              : 
    5217       213452 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5218              : 
    5219       213452 :   if (i < 0)
    5220              :     return CMP_LT;
    5221       100191 :   if (i > 0)
    5222        39801 :     return CMP_GT;
    5223              :   return CMP_EQ;
    5224              : }
    5225              : 
    5226              : 
    5227              : /* Compare an integer expression with an integer.  */
    5228              : 
    5229              : static compare_result
    5230        75454 : compare_bound_int (gfc_expr *a, int b)
    5231              : {
    5232        75454 :   int i;
    5233              : 
    5234        75454 :   if (a == NULL
    5235        32560 :       || a->expr_type != EXPR_CONSTANT
    5236        29612 :       || a->ts.type != BT_INTEGER)
    5237              :     return CMP_UNKNOWN;
    5238              : 
    5239        29612 :   i = mpz_cmp_si (a->value.integer, b);
    5240              : 
    5241        29612 :   if (i < 0)
    5242              :     return CMP_LT;
    5243        25138 :   if (i > 0)
    5244        21603 :     return CMP_GT;
    5245              :   return CMP_EQ;
    5246              : }
    5247              : 
    5248              : 
    5249              : /* Compare an integer expression with a mpz_t.  */
    5250              : 
    5251              : static compare_result
    5252        70188 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5253              : {
    5254        70188 :   int i;
    5255              : 
    5256        70188 :   if (a == NULL
    5257        57319 :       || a->expr_type != EXPR_CONSTANT
    5258        55196 :       || a->ts.type != BT_INTEGER)
    5259              :     return CMP_UNKNOWN;
    5260              : 
    5261        55193 :   i = mpz_cmp (a->value.integer, b);
    5262              : 
    5263        55193 :   if (i < 0)
    5264              :     return CMP_LT;
    5265        25130 :   if (i > 0)
    5266        10734 :     return CMP_GT;
    5267              :   return CMP_EQ;
    5268              : }
    5269              : 
    5270              : 
    5271              : /* Compute the last value of a sequence given by a triplet.
    5272              :    Return 0 if it wasn't able to compute the last value, or if the
    5273              :    sequence if empty, and 1 otherwise.  */
    5274              : 
    5275              : static int
    5276        52382 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5277              :                                 gfc_expr *stride, mpz_t last)
    5278              : {
    5279        52382 :   mpz_t rem;
    5280              : 
    5281        52382 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5282        37217 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5283        32507 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5284              :     return 0;
    5285              : 
    5286        32188 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5287        32187 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    5288              :     return 0;
    5289              : 
    5290         6701 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    5291              :     {
    5292        25612 :       if (compare_bound (start, end) == CMP_GT)
    5293              :         return 0;
    5294        24223 :       mpz_set (last, end->value.integer);
    5295        24223 :       return 1;
    5296              :     }
    5297              : 
    5298         6575 :   if (compare_bound_int (stride, 0) == CMP_GT)
    5299              :     {
    5300              :       /* Stride is positive */
    5301         5210 :       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
    5302              :         return 0;
    5303              :     }
    5304              :   else
    5305              :     {
    5306              :       /* Stride is negative */
    5307         1365 :       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
    5308              :         return 0;
    5309              :     }
    5310              : 
    5311         6555 :   mpz_init (rem);
    5312         6555 :   mpz_sub (rem, end->value.integer, start->value.integer);
    5313         6555 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    5314         6555 :   mpz_sub (last, end->value.integer, rem);
    5315         6555 :   mpz_clear (rem);
    5316              : 
    5317         6555 :   return 1;
    5318              : }
    5319              : 
    5320              : 
    5321              : /* Compare a single dimension of an array reference to the array
    5322              :    specification.  */
    5323              : 
    5324              : static bool
    5325       217695 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5326              : {
    5327       217695 :   mpz_t last_value;
    5328              : 
    5329       217695 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5330              :     {
    5331          498 :       gcc_assert (ar->stride[i] == NULL);
    5332              :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5333          498 :       if (ar->start[i] == NULL)
    5334              :         {
    5335          406 :           gcc_assert (ar->end[i] == NULL);
    5336              :           return true;
    5337              :         }
    5338              :     }
    5339              : 
    5340              : /* Given start, end and stride values, calculate the minimum and
    5341              :    maximum referenced indexes.  */
    5342              : 
    5343       217289 :   switch (ar->dimen_type[i])
    5344              :     {
    5345              :     case DIMEN_VECTOR:
    5346              :     case DIMEN_THIS_IMAGE:
    5347              :       break;
    5348              : 
    5349       156613 :     case DIMEN_STAR:
    5350       156613 :     case DIMEN_ELEMENT:
    5351       156613 :       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
    5352              :         {
    5353            2 :           if (i < as->rank)
    5354            2 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5355              :                          "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5356            2 :                          mpz_get_si (ar->start[i]->value.integer),
    5357            2 :                          mpz_get_si (as->lower[i]->value.integer), i+1);
    5358              :           else
    5359            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5360              :                          "(%ld < %ld) in codimension %d", &ar->c_where[i],
    5361            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5362            0 :                          mpz_get_si (as->lower[i]->value.integer),
    5363            0 :                          i + 1 - as->rank);
    5364            2 :           return true;
    5365              :         }
    5366       156611 :       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
    5367              :         {
    5368           39 :           if (i < as->rank)
    5369           39 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5370              :                          "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5371           39 :                          mpz_get_si (ar->start[i]->value.integer),
    5372           39 :                          mpz_get_si (as->upper[i]->value.integer), i+1);
    5373              :           else
    5374            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5375              :                          "(%ld > %ld) in codimension %d", &ar->c_where[i],
    5376            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5377            0 :                          mpz_get_si (as->upper[i]->value.integer),
    5378            0 :                          i + 1 - as->rank);
    5379           39 :           return true;
    5380              :         }
    5381              : 
    5382              :       break;
    5383              : 
    5384        52427 :     case DIMEN_RANGE:
    5385        52427 :       {
    5386              : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
    5387              : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
    5388              : 
    5389        52427 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5390        52427 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5391              : 
    5392              :         /* Check for zero stride, which is not allowed.  */
    5393        52427 :         if (comp_stride_zero == CMP_EQ)
    5394              :           {
    5395            1 :             gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
    5396            1 :             return false;
    5397              :           }
    5398              : 
    5399              :         /* if start == end || (stride > 0 && start < end)
    5400              :                            || (stride < 0 && start > end),
    5401              :            then the array section contains at least one element.  In this
    5402              :            case, there is an out-of-bounds access if
    5403              :            (start < lower || start > upper).  */
    5404        52426 :         if (comp_start_end == CMP_EQ
    5405        51664 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5406        48875 :                 && comp_start_end == CMP_LT)
    5407        22947 :             || (comp_stride_zero == CMP_LT
    5408        22947 :                 && comp_start_end == CMP_GT))
    5409              :           {
    5410        30824 :             if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
    5411              :               {
    5412           27 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5413              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5414           27 :                        mpz_get_si (AR_START->value.integer),
    5415           27 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5416           27 :                 return true;
    5417              :               }
    5418        30797 :             if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
    5419              :               {
    5420           17 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5421              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5422           17 :                        mpz_get_si (AR_START->value.integer),
    5423           17 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5424           17 :                 return true;
    5425              :               }
    5426              :           }
    5427              : 
    5428              :         /* If we can compute the highest index of the array section,
    5429              :            then it also has to be between lower and upper.  */
    5430        52382 :         mpz_init (last_value);
    5431        52382 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5432              :                                             last_value))
    5433              :           {
    5434        30778 :             if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
    5435              :               {
    5436            3 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5437              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5438              :                        mpz_get_si (last_value),
    5439            3 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5440            3 :                 mpz_clear (last_value);
    5441            3 :                 return true;
    5442              :               }
    5443        30775 :             if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
    5444              :               {
    5445            7 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5446              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5447              :                        mpz_get_si (last_value),
    5448            7 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5449            7 :                 mpz_clear (last_value);
    5450            7 :                 return true;
    5451              :               }
    5452              :           }
    5453        52372 :         mpz_clear (last_value);
    5454              : 
    5455              : #undef AR_START
    5456              : #undef AR_END
    5457              :       }
    5458        52372 :       break;
    5459              : 
    5460            0 :     default:
    5461            0 :       gfc_internal_error ("check_dimension(): Bad array reference");
    5462              :     }
    5463              : 
    5464              :   return true;
    5465              : }
    5466              : 
    5467              : 
    5468              : /* Compare an array reference with an array specification.  */
    5469              : 
    5470              : static bool
    5471       428745 : compare_spec_to_ref (gfc_array_ref *ar)
    5472              : {
    5473       428745 :   gfc_array_spec *as;
    5474       428745 :   int i;
    5475              : 
    5476       428745 :   as = ar->as;
    5477       428745 :   i = as->rank - 1;
    5478              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5479       428745 :   if (as->type == AS_ASSUMED_SIZE
    5480         5804 :       && (/*ar->type == AR_FULL
    5481         5804 :           ||*/ (ar->type == AR_SECTION
    5482          517 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5483              :     {
    5484            5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5485              :                  "not specified at %L", &ar->where);
    5486            5 :       return false;
    5487              :     }
    5488              : 
    5489       428740 :   if (ar->type == AR_FULL)
    5490              :     return true;
    5491              : 
    5492       165406 :   if (as->rank != ar->dimen)
    5493              :     {
    5494           28 :       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
    5495              :                  &ar->where, ar->dimen, as->rank);
    5496           28 :       return false;
    5497              :     }
    5498              : 
    5499              :   /* ar->codimen == 0 is a local array.  */
    5500       165378 :   if (as->corank != ar->codimen && ar->codimen != 0)
    5501              :     {
    5502            0 :       gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
    5503              :                  &ar->where, ar->codimen, as->corank);
    5504            0 :       return false;
    5505              :     }
    5506              : 
    5507       373258 :   for (i = 0; i < as->rank; i++)
    5508       207881 :     if (!check_dimension (i, ar, as))
    5509              :       return false;
    5510              : 
    5511              :   /* Local access has no coarray spec.  */
    5512       165377 :   if (ar->codimen != 0)
    5513        18870 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5514              :       {
    5515         9816 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5516         6831 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5517              :           {
    5518            2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5519            2 :                        i + 1 - as->rank, &ar->where);
    5520            2 :             return false;
    5521              :           }
    5522         9814 :         if (!check_dimension (i, ar, as))
    5523              :           return false;
    5524              :       }
    5525              : 
    5526              :   return true;
    5527              : }
    5528              : 
    5529              : 
    5530              : /* Resolve one part of an array index.  */
    5531              : 
    5532              : static bool
    5533       738097 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5534              :                      int force_index_integer_kind)
    5535              : {
    5536       738097 :   gfc_typespec ts;
    5537              : 
    5538       738097 :   if (index == NULL)
    5539              :     return true;
    5540              : 
    5541       219299 :   if (!gfc_resolve_expr (index))
    5542              :     return false;
    5543              : 
    5544       219288 :   if (check_scalar && index->rank != 0)
    5545              :     {
    5546            2 :       gfc_error ("Array index at %L must be scalar", &index->where);
    5547            2 :       return false;
    5548              :     }
    5549              : 
    5550       219286 :   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
    5551              :     {
    5552            4 :       gfc_error ("Array index at %L must be of INTEGER type, found %s",
    5553              :                  &index->where, gfc_basic_typename (index->ts.type));
    5554            4 :       return false;
    5555              :     }
    5556              : 
    5557       219282 :   if (index->ts.type == BT_REAL)
    5558          339 :     if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
    5559              :                          &index->where))
    5560              :       return false;
    5561              : 
    5562       219282 :   if ((index->ts.kind != gfc_index_integer_kind
    5563       214233 :        && force_index_integer_kind)
    5564       187731 :       || (index->ts.type != BT_INTEGER
    5565              :           && index->ts.type != BT_UNKNOWN))
    5566              :     {
    5567        31889 :       gfc_clear_ts (&ts);
    5568        31889 :       ts.type = BT_INTEGER;
    5569        31889 :       ts.kind = gfc_index_integer_kind;
    5570              : 
    5571        31889 :       gfc_convert_type_warn (index, &ts, 2, 0);
    5572              :     }
    5573              : 
    5574              :   return true;
    5575              : }
    5576              : 
    5577              : /* Resolve one part of an array index.  */
    5578              : 
    5579              : bool
    5580       492323 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5581              : {
    5582       492323 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5583              : }
    5584              : 
    5585              : /* Resolve a dim argument to an intrinsic function.  */
    5586              : 
    5587              : bool
    5588        23915 : gfc_resolve_dim_arg (gfc_expr *dim)
    5589              : {
    5590        23915 :   if (dim == NULL)
    5591              :     return true;
    5592              : 
    5593        23915 :   if (!gfc_resolve_expr (dim))
    5594              :     return false;
    5595              : 
    5596        23915 :   if (dim->rank != 0)
    5597              :     {
    5598            0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5599            0 :       return false;
    5600              : 
    5601              :     }
    5602              : 
    5603        23915 :   if (dim->ts.type != BT_INTEGER)
    5604              :     {
    5605            0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5606            0 :       return false;
    5607              :     }
    5608              : 
    5609        23915 :   if (dim->ts.kind != gfc_index_integer_kind)
    5610              :     {
    5611        15306 :       gfc_typespec ts;
    5612              : 
    5613        15306 :       gfc_clear_ts (&ts);
    5614        15306 :       ts.type = BT_INTEGER;
    5615        15306 :       ts.kind = gfc_index_integer_kind;
    5616              : 
    5617        15306 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5618              :     }
    5619              : 
    5620              :   return true;
    5621              : }
    5622              : 
    5623              : /* Given an expression that contains array references, update those array
    5624              :    references to point to the right array specifications.  While this is
    5625              :    filled in during matching, this information is difficult to save and load
    5626              :    in a module, so we take care of it here.
    5627              : 
    5628              :    The idea here is that the original array reference comes from the
    5629              :    base symbol.  We traverse the list of reference structures, setting
    5630              :    the stored reference to references.  Component references can
    5631              :    provide an additional array specification.  */
    5632              : static void
    5633              : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5634              : 
    5635              : static bool
    5636          914 : find_array_spec (gfc_expr *e)
    5637              : {
    5638          914 :   gfc_array_spec *as;
    5639          914 :   gfc_component *c;
    5640          914 :   gfc_ref *ref;
    5641          914 :   bool class_as = false;
    5642              : 
    5643          914 :   if (e->symtree->n.sym->assoc)
    5644              :     {
    5645          217 :       if (e->symtree->n.sym->assoc->target)
    5646          217 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5647          217 :       resolve_assoc_var (e->symtree->n.sym, false);
    5648              :     }
    5649              : 
    5650          914 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5651              :     {
    5652          124 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5653          124 :       class_as = true;
    5654              :     }
    5655              :   else
    5656          790 :     as = e->symtree->n.sym->as;
    5657              : 
    5658         2085 :   for (ref = e->ref; ref; ref = ref->next)
    5659         1178 :     switch (ref->type)
    5660              :       {
    5661          916 :       case REF_ARRAY:
    5662          916 :         if (as == NULL)
    5663              :           {
    5664            7 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5665           14 :                          ? ref->u.ar.where : e->where);
    5666            7 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5667              :                        &loc);
    5668            7 :             return false;
    5669              :           }
    5670              : 
    5671          909 :         ref->u.ar.as = as;
    5672          909 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5673              :         as = NULL;
    5674              :         break;
    5675              : 
    5676          238 :       case REF_COMPONENT:
    5677          238 :         c = ref->u.c.component;
    5678          238 :         if (c->attr.dimension)
    5679              :           {
    5680          107 :             if (as != NULL && !(class_as && as == c->as))
    5681            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5682          107 :             as = c->as;
    5683              :           }
    5684              : 
    5685              :         break;
    5686              : 
    5687              :       case REF_SUBSTRING:
    5688              :       case REF_INQUIRY:
    5689              :         break;
    5690              :       }
    5691              : 
    5692          907 :   if (as != NULL)
    5693            0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5694              : 
    5695              :   return true;
    5696              : }
    5697              : 
    5698              : 
    5699              : /* Resolve an array reference.  */
    5700              : 
    5701              : static bool
    5702       429459 : resolve_array_ref (gfc_array_ref *ar)
    5703              : {
    5704       429459 :   int i, check_scalar;
    5705       429459 :   gfc_expr *e;
    5706              : 
    5707       675216 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5708              :     {
    5709       245774 :       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
    5710              : 
    5711              :       /* Do not force gfc_index_integer_kind for the start.  We can
    5712              :          do fine with any integer kind.  This avoids temporary arrays
    5713              :          created for indexing with a vector.  */
    5714       245774 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5715              :         return false;
    5716       245759 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5717              :         return false;
    5718       245757 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5719              :         return false;
    5720              : 
    5721       245757 :       e = ar->start[i];
    5722              : 
    5723       245757 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5724       146681 :         switch (e->rank)
    5725              :           {
    5726       145589 :           case 0:
    5727       145589 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5728       145589 :             break;
    5729              : 
    5730         1092 :           case 1:
    5731         1092 :             ar->dimen_type[i] = DIMEN_VECTOR;
    5732         1092 :             if (e->expr_type == EXPR_VARIABLE
    5733          470 :                 && e->symtree->n.sym->ts.type == BT_DERIVED)
    5734           13 :               ar->start[i] = gfc_get_parentheses (e);
    5735              :             break;
    5736              : 
    5737            0 :           default:
    5738            0 :             gfc_error ("Array index at %L is an array of rank %d",
    5739              :                        &ar->c_where[i], e->rank);
    5740            0 :             return false;
    5741              :           }
    5742              : 
    5743              :       /* Fill in the upper bound, which may be lower than the
    5744              :          specified one for something like a(2:10:5), which is
    5745              :          identical to a(2:7:5).  Only relevant for strides not equal
    5746              :          to one.  Don't try a division by zero.  */
    5747       245757 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5748        72136 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5749         8439 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5750         8292 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5751              :         {
    5752         8291 :           mpz_t size, end;
    5753              : 
    5754         8291 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5755              :             {
    5756         6585 :               if (ar->end[i] == NULL)
    5757              :                 {
    5758         8022 :                   ar->end[i] =
    5759         4011 :                     gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
    5760              :                                            &ar->where);
    5761         4011 :                   mpz_set (ar->end[i]->value.integer, end);
    5762              :                 }
    5763         2574 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5764         2574 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5765              :                 {
    5766         2574 :                   mpz_set (ar->end[i]->value.integer, end);
    5767              :                 }
    5768              :               else
    5769            0 :                 gcc_unreachable ();
    5770              : 
    5771         6585 :               mpz_clear (size);
    5772         6585 :               mpz_clear (end);
    5773              :             }
    5774              :         }
    5775              :     }
    5776              : 
    5777       429442 :   if (ar->type == AR_FULL)
    5778              :     {
    5779       266780 :       if (ar->as->rank == 0)
    5780         3412 :         ar->type = AR_ELEMENT;
    5781              : 
    5782              :       /* Make sure array is the same as array(:,:), this way
    5783              :          we don't need to special case all the time.  */
    5784       266780 :       ar->dimen = ar->as->rank;
    5785       635887 :       for (i = 0; i < ar->dimen; i++)
    5786              :         {
    5787       369107 :           ar->dimen_type[i] = DIMEN_RANGE;
    5788              : 
    5789       369107 :           gcc_assert (ar->start[i] == NULL);
    5790       369107 :           gcc_assert (ar->end[i] == NULL);
    5791       369107 :           gcc_assert (ar->stride[i] == NULL);
    5792              :         }
    5793              :     }
    5794              : 
    5795              :   /* If the reference type is unknown, figure out what kind it is.  */
    5796              : 
    5797       429442 :   if (ar->type == AR_UNKNOWN)
    5798              :     {
    5799       149530 :       ar->type = AR_ELEMENT;
    5800       289418 :       for (i = 0; i < ar->dimen; i++)
    5801       178199 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5802       178199 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5803              :           {
    5804        38311 :             ar->type = AR_SECTION;
    5805        38311 :             break;
    5806              :           }
    5807              :     }
    5808              : 
    5809       429442 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5810              :     return false;
    5811              : 
    5812       429406 :   if (ar->as->corank && ar->codimen == 0)
    5813              :     {
    5814         2075 :       int n;
    5815         2075 :       ar->codimen = ar->as->corank;
    5816         5916 :       for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
    5817         3841 :         ar->dimen_type[n] = DIMEN_THIS_IMAGE;
    5818              :     }
    5819              : 
    5820       429406 :   if (ar->codimen)
    5821              :     {
    5822        13631 :       if (ar->team_type == TEAM_NUMBER)
    5823              :         {
    5824           60 :           if (!gfc_resolve_expr (ar->team))
    5825              :             return false;
    5826              : 
    5827           60 :           if (ar->team->rank != 0)
    5828              :             {
    5829            0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5830              :                          &ar->team->where);
    5831            0 :               return false;
    5832              :             }
    5833              : 
    5834           60 :           if (ar->team->ts.type != BT_INTEGER)
    5835              :             {
    5836            6 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5837              :                          "type, found %s",
    5838            6 :                          &ar->team->where,
    5839              :                          gfc_basic_typename (ar->team->ts.type));
    5840            6 :               return false;
    5841              :             }
    5842              :         }
    5843        13571 :       else if (ar->team_type == TEAM_TEAM)
    5844              :         {
    5845           42 :           if (!gfc_resolve_expr (ar->team))
    5846              :             return false;
    5847              : 
    5848           42 :           if (ar->team->rank != 0)
    5849              :             {
    5850            3 :               gfc_error ("TEAM argument at %L must be scalar",
    5851              :                          &ar->team->where);
    5852            3 :               return false;
    5853              :             }
    5854              : 
    5855           39 :           if (ar->team->ts.type != BT_DERIVED
    5856           36 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5857           36 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5858              :             {
    5859            3 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5860              :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5861            3 :                          &ar->team->where,
    5862              :                          gfc_basic_typename (ar->team->ts.type));
    5863            3 :               return false;
    5864              :             }
    5865              :         }
    5866        13619 :       if (ar->stat)
    5867              :         {
    5868           62 :           if (!gfc_resolve_expr (ar->stat))
    5869              :             return false;
    5870              : 
    5871           62 :           if (ar->stat->rank != 0)
    5872              :             {
    5873            3 :               gfc_error ("STAT argument at %L must be scalar",
    5874              :                          &ar->stat->where);
    5875            3 :               return false;
    5876              :             }
    5877              : 
    5878           59 :           if (ar->stat->ts.type != BT_INTEGER)
    5879              :             {
    5880            3 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5881              :                          "type, found %s",
    5882            3 :                          &ar->stat->where,
    5883              :                          gfc_basic_typename (ar->stat->ts.type));
    5884            3 :               return false;
    5885              :             }
    5886              : 
    5887           56 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5888              :             {
    5889            0 :               gfc_error ("STAT's expression at %L must be a variable",
    5890              :                          &ar->stat->where);
    5891            0 :               return false;
    5892              :             }
    5893              :         }
    5894              :     }
    5895              :   return true;
    5896              : }
    5897              : 
    5898              : 
    5899              : bool
    5900         8836 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5901              : {
    5902         8836 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5903              : 
    5904         8836 :   if (ref->u.ss.start != NULL)
    5905              :     {
    5906         8836 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5907              :         return false;
    5908              : 
    5909         8836 :       if (ref->u.ss.start->ts.type != BT_INTEGER)
    5910              :         {
    5911            1 :           gfc_error ("Substring start index at %L must be of type INTEGER",
    5912              :                      &ref->u.ss.start->where);
    5913            1 :           return false;
    5914              :         }
    5915              : 
    5916         8835 :       if (ref->u.ss.start->rank != 0)
    5917              :         {
    5918            0 :           gfc_error ("Substring start index at %L must be scalar",
    5919              :                      &ref->u.ss.start->where);
    5920            0 :           return false;
    5921              :         }
    5922              : 
    5923         8835 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5924         8835 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5925           37 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5926              :         {
    5927            1 :           gfc_error ("Substring start index at %L is less than one",
    5928              :                      &ref->u.ss.start->where);
    5929            1 :           return false;
    5930              :         }
    5931              :     }
    5932              : 
    5933         8834 :   if (ref->u.ss.end != NULL)
    5934              :     {
    5935         8640 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5936              :         return false;
    5937              : 
    5938         8640 :       if (ref->u.ss.end->ts.type != BT_INTEGER)
    5939              :         {
    5940            1 :           gfc_error ("Substring end index at %L must be of type INTEGER",
    5941              :                      &ref->u.ss.end->where);
    5942            1 :           return false;
    5943              :         }
    5944              : 
    5945         8639 :       if (ref->u.ss.end->rank != 0)
    5946              :         {
    5947            0 :           gfc_error ("Substring end index at %L must be scalar",
    5948              :                      &ref->u.ss.end->where);
    5949            0 :           return false;
    5950              :         }
    5951              : 
    5952         8639 :       if (ref->u.ss.length != NULL
    5953         8303 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5954         8651 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5955           12 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5956              :         {
    5957            4 :           gfc_error ("Substring end index at %L exceeds the string length",
    5958              :                      &ref->u.ss.start->where);
    5959            4 :           return false;
    5960              :         }
    5961              : 
    5962         8635 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5963         8635 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5964         8635 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5965            7 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5966              :         {
    5967            4 :           gfc_error ("Substring end index at %L is too large",
    5968              :                      &ref->u.ss.end->where);
    5969            4 :           return false;
    5970              :         }
    5971              :       /*  If the substring has the same length as the original
    5972              :           variable, the reference itself can be deleted.  */
    5973              : 
    5974         8631 :       if (ref->u.ss.length != NULL
    5975         8295 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5976         9547 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5977          230 :         *equal_length = true;
    5978              :     }
    5979              : 
    5980              :   return true;
    5981              : }
    5982              : 
    5983              : 
    5984              : /* This function supplies missing substring charlens.  */
    5985              : 
    5986              : void
    5987         4564 : gfc_resolve_substring_charlen (gfc_expr *e)
    5988              : {
    5989         4564 :   gfc_ref *char_ref;
    5990         4564 :   gfc_expr *start, *end;
    5991         4564 :   gfc_typespec *ts = NULL;
    5992         4564 :   mpz_t diff;
    5993              : 
    5994         8889 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    5995              :     {
    5996         7042 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    5997              :         break;
    5998         4325 :       if (char_ref->type == REF_COMPONENT)
    5999          328 :         ts = &char_ref->u.c.component->ts;
    6000              :     }
    6001              : 
    6002         4564 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    6003         1909 :     return;
    6004              : 
    6005         2717 :   gcc_assert (char_ref->next == NULL);
    6006              : 
    6007         2717 :   if (e->ts.u.cl)
    6008              :     {
    6009          120 :       if (e->ts.u.cl->length)
    6010          108 :         gfc_free_expr (e->ts.u.cl->length);
    6011           12 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    6012              :         return;
    6013              :     }
    6014              : 
    6015         2705 :   if (!e->ts.u.cl)
    6016         2597 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6017              : 
    6018         2705 :   if (char_ref->u.ss.start)
    6019         2705 :     start = gfc_copy_expr (char_ref->u.ss.start);
    6020              :   else
    6021            0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    6022              : 
    6023         2705 :   if (char_ref->u.ss.end)
    6024         2655 :     end = gfc_copy_expr (char_ref->u.ss.end);
    6025           50 :   else if (e->expr_type == EXPR_VARIABLE)
    6026              :     {
    6027           50 :       if (!ts)
    6028           32 :         ts = &e->symtree->n.sym->ts;
    6029           50 :       end = gfc_copy_expr (ts->u.cl->length);
    6030              :     }
    6031              :   else
    6032              :     end = NULL;
    6033              : 
    6034         2705 :   if (!start || !end)
    6035              :     {
    6036           50 :       gfc_free_expr (start);
    6037           50 :       gfc_free_expr (end);
    6038           50 :       return;
    6039              :     }
    6040              : 
    6041              :   /* Length = (end - start + 1).
    6042              :      Check first whether it has a constant length.  */
    6043         2655 :   if (gfc_dep_difference (end, start, &diff))
    6044              :     {
    6045         2539 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    6046              :                                              &e->where);
    6047              : 
    6048         2539 :       mpz_add_ui (len->value.integer, diff, 1);
    6049         2539 :       mpz_clear (diff);
    6050         2539 :       e->ts.u.cl->length = len;
    6051              :       /* The check for length < 0 is handled below */
    6052              :     }
    6053              :   else
    6054              :     {
    6055          116 :       e->ts.u.cl->length = gfc_subtract (end, start);
    6056          116 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    6057              :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    6058              :                                                       NULL, 1));
    6059              :     }
    6060              : 
    6061              :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    6062              :      be within the range 1, 2, ..., n unless the starting point exceeds
    6063              :      the ending point, in which case the substring has length zero.  */
    6064              : 
    6065         2655 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    6066           15 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    6067              : 
    6068         2655 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6069         2655 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6070              : 
    6071              :   /* Make sure that the length is simplified.  */
    6072         2655 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    6073         2655 :   gfc_resolve_expr (e->ts.u.cl->length);
    6074              : }
    6075              : 
    6076              : 
    6077              : /* Convert an array reference to an array element so that PDT KIND and LEN
    6078              :    or inquiry references are always scalar.  */
    6079              : 
    6080              : static void
    6081           21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
    6082              : {
    6083           21 :   gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    6084           21 :   int dim;
    6085              : 
    6086           21 :   array_ref->u.ar.type = AR_ELEMENT;
    6087           21 :   expr->rank = 0;
    6088              :   /* Suppress the runtime bounds check.  */
    6089           21 :   expr->no_bounds_check = 1;
    6090           42 :   for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    6091              :     {
    6092           21 :       array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    6093           21 :       if (array_ref->u.ar.start[dim])
    6094            0 :         gfc_free_expr (array_ref->u.ar.start[dim]);
    6095              : 
    6096           21 :       if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
    6097            9 :         array_ref->u.ar.start[dim]
    6098            9 :                         = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
    6099              :       else
    6100           12 :         array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
    6101              : 
    6102           21 :       if (array_ref->u.ar.end[dim])
    6103            0 :         gfc_free_expr (array_ref->u.ar.end[dim]);
    6104           21 :       if (array_ref->u.ar.stride[dim])
    6105            0 :         gfc_free_expr (array_ref->u.ar.stride[dim]);
    6106              :     }
    6107           21 :   gfc_free_expr (unity);
    6108           21 : }
    6109              : 
    6110              : 
    6111              : /* Resolve subtype references.  */
    6112              : 
    6113              : bool
    6114       546942 : gfc_resolve_ref (gfc_expr *expr)
    6115              : {
    6116       546942 :   int current_part_dimension, n_components, seen_part_dimension;
    6117       546942 :   gfc_ref *ref, **prev, *array_ref;
    6118       546942 :   bool equal_length;
    6119       546942 :   gfc_symbol *last_pdt = NULL;
    6120              : 
    6121      1074586 :   for (ref = expr->ref; ref; ref = ref->next)
    6122       528558 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    6123              :       {
    6124          914 :         if (!find_array_spec (expr))
    6125              :           return false;
    6126              :         break;
    6127              :       }
    6128              : 
    6129      1603978 :   for (prev = &expr->ref; *prev != NULL;
    6130       528624 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6131       528703 :     switch ((*prev)->type)
    6132              :       {
    6133       429459 :       case REF_ARRAY:
    6134       429459 :         if (!resolve_array_ref (&(*prev)->u.ar))
    6135              :             return false;
    6136              :         break;
    6137              : 
    6138              :       case REF_COMPONENT:
    6139              :       case REF_INQUIRY:
    6140              :         break;
    6141              : 
    6142         8555 :       case REF_SUBSTRING:
    6143         8555 :         equal_length = false;
    6144         8555 :         if (!gfc_resolve_substring (*prev, &equal_length))
    6145              :             return false;
    6146              : 
    6147         8547 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    6148              :           {
    6149              :             /* Remove the reference and move the charlen, if any.  */
    6150          205 :             ref = *prev;
    6151          205 :             *prev = ref->next;
    6152          205 :             ref->next = NULL;
    6153          205 :             expr->ts.u.cl = ref->u.ss.length;
    6154          205 :             ref->u.ss.length = NULL;
    6155          205 :             gfc_free_ref_list (ref);
    6156              :           }
    6157              :         break;
    6158              :       }
    6159              : 
    6160              :   /* Check constraints on part references.  */
    6161              : 
    6162       546856 :   current_part_dimension = 0;
    6163       546856 :   seen_part_dimension = 0;
    6164       546856 :   n_components = 0;
    6165       546856 :   array_ref = NULL;
    6166              : 
    6167              :   /* Use the declared type of the base symbol to initialize last_pdt when the
    6168              :      expression is not itself a PDT. This matters for ASSOCIATE variables whose
    6169              :      component reference may still point to a PDT template.  */
    6170       546856 :   if (expr->expr_type == EXPR_VARIABLE
    6171       453693 :       && (IS_PDT (expr)
    6172       453123 :           || (expr->ref && expr->symtree && IS_PDT (expr->symtree->n.sym))))
    6173         2267 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6174              : 
    6175      1075250 :   for (ref = expr->ref; ref; ref = ref->next)
    6176              :     {
    6177       528405 :       switch (ref->type)
    6178              :         {
    6179       429381 :         case REF_ARRAY:
    6180       429381 :           array_ref = ref;
    6181       429381 :           switch (ref->u.ar.type)
    6182              :             {
    6183       263366 :             case AR_FULL:
    6184              :               /* Coarray scalar.  */
    6185       263366 :               if (ref->u.ar.as->rank == 0)
    6186              :                 {
    6187              :                   current_part_dimension = 0;
    6188              :                   break;
    6189              :                 }
    6190              :               /* Fall through.  */
    6191       304753 :             case AR_SECTION:
    6192       304753 :               current_part_dimension = 1;
    6193       304753 :               break;
    6194              : 
    6195       124628 :             case AR_ELEMENT:
    6196       124628 :               array_ref = NULL;
    6197       124628 :               current_part_dimension = 0;
    6198       124628 :               break;
    6199              : 
    6200            0 :             case AR_UNKNOWN:
    6201            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6202              :             }
    6203              : 
    6204              :           break;
    6205              : 
    6206        89861 :         case REF_COMPONENT:
    6207        89861 :           if (current_part_dimension || seen_part_dimension)
    6208              :             {
    6209              :               /* F03:C614.  */
    6210         6863 :               if (ref->u.c.component->attr.pointer
    6211         6860 :                   || ref->u.c.component->attr.proc_pointer
    6212         6859 :                   || (ref->u.c.component->ts.type == BT_CLASS
    6213            1 :                         && CLASS_DATA (ref->u.c.component)->attr.pointer))
    6214              :                 {
    6215            4 :                   gfc_error ("Component to the right of a part reference "
    6216              :                              "with nonzero rank must not have the POINTER "
    6217              :                              "attribute at %L", &expr->where);
    6218            4 :                   return false;
    6219              :                 }
    6220         6859 :               else if (ref->u.c.component->attr.allocatable
    6221         6853 :                         || (ref->u.c.component->ts.type == BT_CLASS
    6222            1 :                             && CLASS_DATA (ref->u.c.component)->attr.allocatable))
    6223              : 
    6224              :                 {
    6225            7 :                   gfc_error ("Component to the right of a part reference "
    6226              :                              "with nonzero rank must not have the ALLOCATABLE "
    6227              :                              "attribute at %L", &expr->where);
    6228            7 :                   return false;
    6229              :                 }
    6230              :             }
    6231              : 
    6232              :           /* Sometimes the component in a component reference is that of the
    6233              :              pdt_template. Point to the component of pdt_type instead. This
    6234              :              ensures that the component gets a backend_decl in translation.  */
    6235        89850 :           if (last_pdt)
    6236              :             {
    6237         2198 :               gfc_component *cmp = last_pdt->components;
    6238         5397 :               for (; cmp; cmp = cmp->next)
    6239         5272 :                 if (!strcmp (cmp->name, ref->u.c.component->name))
    6240              :                   {
    6241         2073 :                     ref->u.c.component = cmp;
    6242         2073 :                     break;
    6243              :                   }
    6244         2198 :               ref->u.c.sym = last_pdt;
    6245              :             }
    6246              : 
    6247              :           /* Convert pdt_templates, if necessary, and update 'last_pdt'.  */
    6248        89850 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6249              :             {
    6250        20852 :               if (ref->u.c.component->ts.u.derived->attr.pdt_template)
    6251              :                 {
    6252            0 :                   if (gfc_get_pdt_instance (ref->u.c.component->param_list,
    6253              :                                             &ref->u.c.component->ts.u.derived,
    6254              :                                             NULL) != MATCH_YES)
    6255              :                     return false;
    6256            0 :                   last_pdt = ref->u.c.component->ts.u.derived;
    6257              :                 }
    6258        20852 :               else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
    6259          521 :                 last_pdt = ref->u.c.component->ts.u.derived;
    6260              :               else
    6261              :                 last_pdt = NULL;
    6262              :             }
    6263              : 
    6264              :           /* The F08 standard requires(See R425, R431, R435, and in particular
    6265              :              Note 6.7) that a PDT parameter reference be a scalar even if
    6266              :              the designator is an array."  */
    6267        89850 :           if (array_ref && last_pdt && last_pdt->attr.pdt_type
    6268          149 :               && (ref->u.c.component->attr.pdt_kind
    6269          149 :                   || ref->u.c.component->attr.pdt_len))
    6270            7 :             reset_array_ref_to_scalar (expr, array_ref);
    6271              : 
    6272        89850 :           n_components++;
    6273        89850 :           break;
    6274              : 
    6275              :         case REF_SUBSTRING:
    6276              :           break;
    6277              : 
    6278          821 :         case REF_INQUIRY:
    6279              :           /* Implement requirement in note 9.7 of F2018 that the result of the
    6280              :              LEN inquiry be a scalar.  */
    6281          821 :           if (ref->u.i == INQUIRY_LEN && array_ref
    6282           40 :               && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
    6283           40 :                   || expr->ts.type == BT_INTEGER))
    6284           14 :             reset_array_ref_to_scalar (expr, array_ref);
    6285              :           break;
    6286              :         }
    6287              : 
    6288       528394 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6289       515077 :            || ref->next == NULL)
    6290              :           && current_part_dimension
    6291       462945 :           && seen_part_dimension)
    6292              :         {
    6293            0 :           gfc_error ("Two or more part references with nonzero rank must "
    6294              :                      "not be specified at %L", &expr->where);
    6295            0 :           return false;
    6296              :         }
    6297              : 
    6298       528394 :       if (ref->type == REF_COMPONENT)
    6299              :         {
    6300        89850 :           if (current_part_dimension)
    6301         6665 :             seen_part_dimension = 1;
    6302              : 
    6303              :           /* reset to make sure */
    6304              :           current_part_dimension = 0;
    6305              :         }
    6306              :     }
    6307              : 
    6308              :   return true;
    6309              : }
    6310              : 
    6311              : 
    6312              : /* Given an expression, determine its shape.  This is easier than it sounds.
    6313              :    Leaves the shape array NULL if it is not possible to determine the shape.  */
    6314              : 
    6315              : static void
    6316      2613595 : expression_shape (gfc_expr *e)
    6317              : {
    6318      2613595 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6319      2613595 :   int i;
    6320              : 
    6321      2613595 :   if (e->rank <= 0 || e->shape != NULL)
    6322      2435645 :     return;
    6323              : 
    6324       710487 :   for (i = 0; i < e->rank; i++)
    6325       479932 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6326       177950 :       goto fail;
    6327              : 
    6328       230555 :   e->shape = gfc_get_shape (e->rank);
    6329              : 
    6330       230555 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6331              : 
    6332       230555 :   return;
    6333              : 
    6334       177950 : fail:
    6335       179621 :   for (i--; i >= 0; i--)
    6336         1671 :     mpz_clear (array[i]);
    6337              : }
    6338              : 
    6339              : 
    6340              : /* Given a variable expression node, compute the rank of the expression by
    6341              :    examining the base symbol and any reference structures it may have.  */
    6342              : 
    6343              : void
    6344      2613595 : gfc_expression_rank (gfc_expr *e)
    6345              : {
    6346      2613595 :   gfc_ref *ref, *last_arr_ref = nullptr;
    6347      2613595 :   int i, rank, corank;
    6348              : 
    6349              :   /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
    6350              :      could lead to serious confusion...  */
    6351      2613595 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6352              : 
    6353      2613595 :   if (e->ref == NULL)
    6354              :     {
    6355      1926403 :       if (e->expr_type == EXPR_ARRAY)
    6356        72779 :         goto done;
    6357              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6358              : 
    6359      1853624 :       if (e->symtree != NULL)
    6360              :         {
    6361              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6362      1853612 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    6363        13911 :                                 && e->symtree->n.sym->ts.u.derived
    6364        13906 :                                 && CLASS_DATA (e->symtree->n.sym))
    6365      1853612 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6366              :                                  : e->symtree->n.sym->as;
    6367      1853612 :           if (as)
    6368              :             {
    6369          626 :               e->rank = as->rank;
    6370          626 :               e->corank = as->corank;
    6371          626 :               goto done;
    6372              :             }
    6373              :         }
    6374      1852998 :       e->rank = 0;
    6375      1852998 :       e->corank = 0;
    6376      1852998 :       goto done;
    6377              :     }
    6378              : 
    6379              :   rank = 0;
    6380              :   corank = 0;
    6381              : 
    6382      1087202 :   for (ref = e->ref; ref; ref = ref->next)
    6383              :     {
    6384       795921 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    6385          568 :           && ref->u.c.component->attr.function && !ref->next)
    6386              :         {
    6387          372 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6388          372 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6389              :         }
    6390              : 
    6391       795921 :       if (ref->type != REF_ARRAY)
    6392       159355 :         continue;
    6393              : 
    6394       636566 :       last_arr_ref = ref;
    6395       636566 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6396              :         {
    6397       349826 :           rank = ref->u.ar.as->rank;
    6398       349826 :           break;
    6399              :         }
    6400              : 
    6401       286740 :       if (ref->u.ar.type == AR_SECTION)
    6402              :         {
    6403              :           /* Figure out the rank of the section.  */
    6404        46085 :           if (rank != 0)
    6405            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6406              : 
    6407       114818 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6408        68733 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6409        68733 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6410        59901 :               rank++;
    6411              : 
    6412              :           break;
    6413              :         }
    6414              :     }
    6415       687192 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6416       616372 :       && last_arr_ref->u.ar.as->rank != -1)
    6417              :     {
    6418        19272 :       for (i = last_arr_ref->u.ar.as->rank;
    6419       627391 :            i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
    6420              :         {
    6421              :           /* For unknown dimen in non-resolved as assume full corank.  */
    6422        20162 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6423        19595 :               || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
    6424          323 :                   && !last_arr_ref->u.ar.as->resolved))
    6425              :             {
    6426              :               corank = last_arr_ref->u.ar.as->corank;
    6427              :               break;
    6428              :             }
    6429        19272 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6430        19272 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6431        19174 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6432        16683 :             corank++;
    6433         2589 :           else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
    6434            0 :             gfc_internal_error ("Illegal coarray index");
    6435              :         }
    6436              :     }
    6437              : 
    6438       687192 :   e->rank = rank;
    6439       687192 :   e->corank = corank;
    6440              : 
    6441      2613595 : done:
    6442      2613595 :   expression_shape (e);
    6443      2613595 : }
    6444              : 
    6445              : 
    6446              : /* Given two expressions, check that their rank is conformable, i.e. either
    6447              :    both have the same rank or at least one is a scalar.  */
    6448              : 
    6449              : bool
    6450     12243376 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6451              : {
    6452     12243376 :   if (op1->expr_type == EXPR_VARIABLE)
    6453       738419 :     gfc_expression_rank (op1);
    6454     12243376 :   if (op2->expr_type == EXPR_VARIABLE)
    6455       447401 :     gfc_expression_rank (op2);
    6456              : 
    6457        77532 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6458     12320582 :          && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
    6459           30 :              || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
    6460              : }
    6461              : 
    6462              : /* Resolve a variable expression.  */
    6463              : 
    6464              : static bool
    6465      1336245 : resolve_variable (gfc_expr *e)
    6466              : {
    6467      1336245 :   gfc_symbol *sym;
    6468      1336245 :   bool t;
    6469              : 
    6470      1336245 :   t = true;
    6471              : 
    6472      1336245 :   if (e->symtree == NULL)
    6473              :     return false;
    6474      1335800 :   sym = e->symtree->n.sym;
    6475              : 
    6476              :   /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
    6477              :      as ts.type is set to BT_ASSUMED in resolve_symbol.  */
    6478      1335800 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    6479              :     {
    6480          183 :       if (!actual_arg || inquiry_argument)
    6481              :         {
    6482            2 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
    6483              :                      "be used as actual argument", sym->name, &e->where);
    6484            2 :           return false;
    6485              :         }
    6486              :     }
    6487              :   /* TS 29113, 407b.  */
    6488      1335617 :   else if (e->ts.type == BT_ASSUMED)
    6489              :     {
    6490          571 :       if (!actual_arg)
    6491              :         {
    6492           20 :           gfc_error ("Assumed-type variable %s at %L may only be used "
    6493              :                      "as actual argument", sym->name, &e->where);
    6494           20 :           return false;
    6495              :         }
    6496          551 :       else if (inquiry_argument && !first_actual_arg)
    6497              :         {
    6498              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6499              :              for all inquiry functions in resolve_function; the reason is
    6500              :              that the function-name resolution happens too late in that
    6501              :              function.  */
    6502            0 :           gfc_error ("Assumed-type variable %s at %L as actual argument to "
    6503              :                      "an inquiry function shall be the first argument",
    6504              :                      sym->name, &e->where);
    6505            0 :           return false;
    6506              :         }
    6507              :     }
    6508              :   /* TS 29113, C535b.  */
    6509      1335046 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6510        37574 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6511        37569 :              && CLASS_DATA (sym)->as
    6512        14692 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6513      1334082 :             || (sym->ts.type != BT_CLASS && sym->as
    6514       365333 :                 && sym->as->type == AS_ASSUMED_RANK))
    6515         8045 :            && !sym->attr.select_rank_temporary
    6516         8045 :            && !(sym->assoc && sym->assoc->ar))
    6517              :     {
    6518         8045 :       if (!actual_arg
    6519         1265 :           && !(cs_base && cs_base->current
    6520         1264 :                && (cs_base->current->op == EXEC_SELECT_RANK
    6521          188 :                    || sym->attr.target)))
    6522              :         {
    6523          144 :           gfc_error ("Assumed-rank variable %s at %L may only be used as "
    6524              :                      "actual argument", sym->name, &e->where);
    6525          144 :           return false;
    6526              :         }
    6527         7901 :       else if (inquiry_argument && !first_actual_arg)
    6528              :         {
    6529              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6530              :              for all inquiry functions in resolve_function; the reason is
    6531              :              that the function-name resolution happens too late in that
    6532              :              function.  */
    6533            0 :           gfc_error ("Assumed-rank variable %s at %L as actual argument "
    6534              :                      "to an inquiry function shall be the first argument",
    6535              :                      sym->name, &e->where);
    6536            0 :           return false;
    6537              :         }
    6538              :     }
    6539              : 
    6540      1335634 :   if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
    6541          181 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6542          180 :            && e->ref->next == NULL))
    6543              :     {
    6544            1 :       gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
    6545              :                  "a subobject reference", sym->name, &e->ref->u.ar.where);
    6546            1 :       return false;
    6547              :     }
    6548              :   /* TS 29113, 407b.  */
    6549      1335633 :   else if (e->ts.type == BT_ASSUMED && e->ref
    6550          687 :            && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6551          680 :                 && e->ref->next == NULL))
    6552              :     {
    6553            7 :       gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
    6554              :                  "reference", sym->name, &e->ref->u.ar.where);
    6555            7 :       return false;
    6556              :     }
    6557              : 
    6558              :   /* TS 29113, C535b.  */
    6559      1335626 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6560        37574 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6561        37569 :         && CLASS_DATA (sym)->as
    6562        14692 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6563      1334662 :        || (sym->ts.type != BT_CLASS && sym->as
    6564       365869 :            && sym->as->type == AS_ASSUMED_RANK))
    6565         8185 :       && !(sym->assoc && sym->assoc->ar)
    6566         8185 :       && e->ref
    6567         8185 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6568         8181 :            && e->ref->next == NULL))
    6569              :     {
    6570            4 :       gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
    6571              :                  "reference", sym->name, &e->ref->u.ar.where);
    6572            4 :       return false;
    6573              :     }
    6574              : 
    6575              :   /* Guessed type variables are associate_names whose selector had not been
    6576              :      parsed at the time that the construct was parsed. Now the namespace is
    6577              :      being resolved, the TKR of the selector will be available for fixup of
    6578              :      the associate_name.  */
    6579      1335622 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6580              :     {
    6581          410 :       gfc_fixup_inferred_type_refs (e);
    6582              :       /* KIND inquiry ref returns the kind of the target.  */
    6583          410 :       if (e->expr_type == EXPR_CONSTANT)
    6584              :         return true;
    6585              :     }
    6586      1335212 :   else if (IS_INFERRED_TYPE (e)
    6587          489 :            && sym->ts.type != BT_UNKNOWN
    6588          489 :            && (sym->ts.type != e->ts.type || sym->ts.kind != e->ts.kind))
    6589              :     /* No subobject ref, but the expression's typespec was set at parse
    6590              :        time before the target's actual type/kind was known.  Refresh from
    6591              :        the now-resolved associate-name symbol.  */
    6592          192 :     e->ts = sym->ts;
    6593      1335020 :   else if (sym->attr.select_type_temporary
    6594         9050 :            && sym->ns->assoc_name_inferred)
    6595           92 :     gfc_fixup_inferred_type_refs (e);
    6596              : 
    6597              :   /* For variables that are used in an associate (target => object) where
    6598              :      the object's basetype is array valued while the target is scalar,
    6599              :      the ts' type of the component refs is still array valued, which
    6600              :      can't be translated that way.  */
    6601      1335610 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6602          605 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6603          605 :       && sym->assoc->target->ts.u.derived
    6604          605 :       && CLASS_DATA (sym->assoc->target)
    6605          605 :       && CLASS_DATA (sym->assoc->target)->as)
    6606              :     {
    6607              :       gfc_ref *ref = e->ref;
    6608          701 :       while (ref)
    6609              :         {
    6610          542 :           switch (ref->type)
    6611              :             {
    6612          237 :             case REF_COMPONENT:
    6613          237 :               ref->u.c.sym = sym->ts.u.derived;
    6614              :               /* Stop the loop.  */
    6615          237 :               ref = NULL;
    6616          237 :               break;
    6617          305 :             default:
    6618          305 :               ref = ref->next;
    6619          305 :               break;
    6620              :             }
    6621              :         }
    6622              :     }
    6623              : 
    6624              :   /* If this is an associate-name, it may be parsed with an array reference
    6625              :      in error even though the target is scalar.  Fail directly in this case.
    6626              :      TODO Understand why class scalar expressions must be excluded.  */
    6627      1335610 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6628              :     {
    6629        11920 :       if (sym->ts.type == BT_CLASS)
    6630          245 :         gfc_fix_class_refs (e);
    6631        11920 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6632         2204 :           && e->ref->type == REF_ARRAY)
    6633              :         {
    6634              :           /* Unambiguously scalar!  */
    6635            3 :           if (sym->assoc->target
    6636            3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6637            1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6638            2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6639              :                        sym->name, &e->where);
    6640            3 :           return false;
    6641              :         }
    6642        11917 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6643         7038 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6644              :         {
    6645              :           /* This can happen because the parser did not detect that the
    6646              :              associate name is an array and the expression had no array
    6647              :              part_ref.  */
    6648          141 :           gfc_ref *ref = gfc_get_ref ();
    6649          141 :           ref->type = REF_ARRAY;
    6650          141 :           ref->u.ar.type = AR_FULL;
    6651          141 :           if (sym->as)
    6652              :             {
    6653          140 :               ref->u.ar.as = sym->as;
    6654          140 :               ref->u.ar.dimen = sym->as->rank;
    6655              :             }
    6656          141 :           ref->next = e->ref;
    6657          141 :           e->ref = ref;
    6658              :         }
    6659              :     }
    6660              : 
    6661      1335607 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6662            0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6663              : 
    6664              :   /* On the other hand, the parser may not have known this is an array;
    6665              :      in this case, we have to add a FULL reference.  */
    6666      1335607 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6667              :     {
    6668            0 :       e->ref = gfc_get_ref ();
    6669            0 :       e->ref->type = REF_ARRAY;
    6670            0 :       e->ref->u.ar.type = AR_FULL;
    6671            0 :       e->ref->u.ar.dimen = 0;
    6672              :     }
    6673              : 
    6674              :   /* Like above, but for class types, where the checking whether an array
    6675              :      ref is present is more complicated.  Furthermore make sure not to add
    6676              :      the full array ref to _vptr or _len refs.  */
    6677      1335607 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6678         1023 :       && CLASS_DATA (sym)
    6679         1023 :       && (CLASS_DATA (sym)->attr.dimension
    6680          449 :           || CLASS_DATA (sym)->attr.codimension)
    6681          580 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6682              :     {
    6683          555 :       gfc_ref *ref, *newref;
    6684              : 
    6685          555 :       newref = gfc_get_ref ();
    6686          555 :       newref->type = REF_ARRAY;
    6687          555 :       newref->u.ar.type = AR_FULL;
    6688          555 :       newref->u.ar.dimen = 0;
    6689              : 
    6690              :       /* Because this is an associate var and the first ref either is a ref to
    6691              :          the _data component or not, no traversal of the ref chain is
    6692              :          needed.  The array ref needs to be inserted after the _data ref,
    6693              :          or when that is not present, which may happened for polymorphic
    6694              :          types, then at the first position.  */
    6695          555 :       ref = e->ref;
    6696          555 :       if (!ref)
    6697           18 :         e->ref = newref;
    6698          537 :       else if (ref->type == REF_COMPONENT
    6699          232 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6700              :         {
    6701          232 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6702              :             {
    6703           12 :               newref->next = ref->next;
    6704           12 :               ref->next = newref;
    6705              :             }
    6706              :           else
    6707              :             /* Array ref present already.  */
    6708          220 :             gfc_free_ref_list (newref);
    6709              :         }
    6710          305 :       else if (ref->type == REF_ARRAY)
    6711              :         /* Array ref present already.  */
    6712          305 :         gfc_free_ref_list (newref);
    6713              :       else
    6714              :         {
    6715            0 :           newref->next = ref;
    6716            0 :           e->ref = newref;
    6717              :         }
    6718              :     }
    6719      1335052 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6720              :     {
    6721          498 :       gfc_ref *ref;
    6722          922 :       for (ref = e->ref; ref; ref = ref->next)
    6723          454 :         if (ref->type == REF_SUBSTRING)
    6724              :           break;
    6725          498 :       if (ref == NULL)
    6726          468 :         e->ts = sym->ts;
    6727              :     }
    6728              : 
    6729      1335607 :   if (e->ref && !gfc_resolve_ref (e))
    6730              :     return false;
    6731              : 
    6732      1335514 :   if (sym->attr.flavor == FL_PROCEDURE
    6733        32268 :       && (!sym->attr.function
    6734        18947 :           || (sym->attr.function && sym->result
    6735        18492 :               && sym->result->attr.proc_pointer
    6736          726 :               && !sym->result->attr.function)))
    6737              :     {
    6738        13321 :       e->ts.type = BT_PROCEDURE;
    6739        13321 :       goto resolve_procedure;
    6740              :     }
    6741              : 
    6742      1322193 :   if (sym->ts.type != BT_UNKNOWN)
    6743      1321427 :     gfc_variable_attr (e, &e->ts);
    6744          766 :   else if (sym->attr.flavor == FL_PROCEDURE
    6745           12 :            && sym->attr.function && sym->result
    6746           12 :            && sym->result->ts.type != BT_UNKNOWN
    6747           10 :            && sym->result->attr.proc_pointer)
    6748           10 :     e->ts = sym->result->ts;
    6749              :   else
    6750              :     {
    6751              :       /* Must be a simple variable reference.  */
    6752          756 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6753              :         return false;
    6754          627 :       e->ts = sym->ts;
    6755              :     }
    6756              : 
    6757      1322064 :   if (check_assumed_size_reference (sym, e))
    6758              :     return false;
    6759              : 
    6760              :   /* Deal with forward references to entries during gfc_resolve_code, to
    6761              :      satisfy, at least partially, 12.5.2.5.  */
    6762      1322045 :   if (gfc_current_ns->entries
    6763         3229 :       && current_entry_id == sym->entry_id
    6764         1050 :       && cs_base
    6765          964 :       && cs_base->current
    6766          964 :       && cs_base->current->op != EXEC_ENTRY)
    6767              :     {
    6768          964 :       int n;
    6769          964 :       bool saved_specification_expr;
    6770          964 :       gfc_symbol *saved_specification_expr_symbol;
    6771              : 
    6772              :       /* If the symbol is a dummy...  */
    6773          964 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6774              :         {
    6775              :           /*  If it has not been seen as a dummy, this is an error.  */
    6776          462 :           if (!entry_dummy_seen_p (sym))
    6777              :             {
    6778            5 :               if (specification_expr
    6779            4 :                   && specification_expr_symbol
    6780            4 :                   && specification_expr_symbol->attr.dummy
    6781            2 :                   && specification_expr_symbol->ns == gfc_current_ns
    6782            7 :                   && !entry_dummy_seen_p (specification_expr_symbol))
    6783              :                 ;
    6784            3 :               else if (specification_expr)
    6785            2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6786              :                            ", is referenced at %L before the ENTRY statement "
    6787              :                            "in which it is a parameter",
    6788              :                            sym->name, &cs_base->current->loc);
    6789              :               else
    6790            1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6791              :                            "statement in which it is a parameter",
    6792              :                            sym->name, &cs_base->current->loc);
    6793              :               t = false;
    6794              :             }
    6795              :         }
    6796              : 
    6797              :       /* Now do the same check on the specification expressions.  */
    6798          964 :       saved_specification_expr = specification_expr;
    6799          964 :       saved_specification_expr_symbol = specification_expr_symbol;
    6800          964 :       specification_expr = true;
    6801          964 :       specification_expr_symbol = sym;
    6802          964 :       if (sym->ts.type == BT_CHARACTER
    6803          964 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6804              :         t = false;
    6805              : 
    6806          964 :       if (sym->as)
    6807              :         {
    6808          279 :           for (n = 0; n < sym->as->rank; n++)
    6809              :             {
    6810          164 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6811            0 :                 t = false;
    6812          164 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6813            1 :                 t = false;
    6814              :             }
    6815              :         }
    6816          964 :       specification_expr = saved_specification_expr;
    6817          964 :       specification_expr_symbol = saved_specification_expr_symbol;
    6818              : 
    6819          964 :       if (t)
    6820              :         /* Update the symbol's entry level.  */
    6821          957 :         sym->entry_id = current_entry_id + 1;
    6822              :     }
    6823              : 
    6824              :   /* If a symbol has been host_associated mark it.  This is used latter,
    6825              :      to identify if aliasing is possible via host association.  */
    6826      1322045 :   if (sym->attr.flavor == FL_VARIABLE
    6827      1283528 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6828         6224 :           || !sym->ns->code->ext.block.assoc)
    6829      1281430 :       && gfc_current_ns->parent
    6830       611394 :       && (gfc_current_ns->parent == sym->ns
    6831       572905 :           || (gfc_current_ns->parent->parent
    6832        12210 :               && gfc_current_ns->parent->parent == sym->ns)))
    6833        45103 :     sym->attr.host_assoc = 1;
    6834              : 
    6835      1322045 :   if (gfc_current_ns->proc_name
    6836      1317875 :       && sym->attr.dimension
    6837       359325 :       && (sym->ns != gfc_current_ns
    6838       335443 :           || sym->attr.use_assoc
    6839       331468 :           || sym->attr.in_common))
    6840        32645 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6841              : 
    6842      1335366 : resolve_procedure:
    6843      1335366 :   if (t && !resolve_procedure_expression (e))
    6844              :     t = false;
    6845              : 
    6846              :   /* F2008, C617 and C1229.  */
    6847      1334326 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6848      1434735 :       && gfc_is_coindexed (e))
    6849              :     {
    6850          359 :       gfc_ref *ref, *ref2 = NULL;
    6851              : 
    6852          442 :       for (ref = e->ref; ref; ref = ref->next)
    6853              :         {
    6854          442 :           if (ref->type == REF_COMPONENT)
    6855           83 :             ref2 = ref;
    6856          442 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6857              :             break;
    6858              :         }
    6859              : 
    6860          718 :       for ( ; ref; ref = ref->next)
    6861          371 :         if (ref->type == REF_COMPONENT)
    6862              :           break;
    6863              : 
    6864              :       /* Expression itself is not coindexed object.  */
    6865          359 :       if (ref && e->ts.type == BT_CLASS)
    6866              :         {
    6867            3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6868              :                      &e->where);
    6869            3 :           t = false;
    6870              :         }
    6871              : 
    6872              :       /* Expression itself is coindexed object.  */
    6873          347 :       if (ref == NULL)
    6874              :         {
    6875          347 :           gfc_component *c;
    6876          347 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6877          467 :           for ( ; c; c = c->next)
    6878          120 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6879              :               {
    6880            0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6881              :                          "subcomponent at %L", &e->where);
    6882            0 :                 t = false;
    6883            0 :                 break;
    6884              :               }
    6885              :         }
    6886              :     }
    6887              : 
    6888      1335366 :   if (t)
    6889      1335356 :     gfc_expression_rank (e);
    6890              : 
    6891      1335366 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6892            3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6893              :                  "Using variable %qs at %L is deprecated",
    6894              :                  sym->name, &e->where);
    6895              :   /* Simplify cases where access to a parameter array results in a
    6896              :      single constant.  Suppress errors since those will have been
    6897              :      issued before, as warnings.  */
    6898      1335366 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    6899              :     {
    6900         2731 :       gfc_push_suppress_errors ();
    6901         2731 :       gfc_simplify_expr (e, 1);
    6902         2731 :       gfc_pop_suppress_errors ();
    6903              :     }
    6904              : 
    6905              :   return t;
    6906              : }
    6907              : 
    6908              : 
    6909              : /* 'sym' was initially guessed to be derived type but has been corrected
    6910              :    in resolve_assoc_var to be a class entity or the derived type correcting.
    6911              :    If a class entity it will certainly need the _data reference or the
    6912              :    reference derived type symbol correcting in the first component ref if
    6913              :    a derived type.  */
    6914              : 
    6915              : void
    6916          920 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6917              : {
    6918          920 :   gfc_ref *ref, *new_ref;
    6919          920 :   gfc_symbol *sym, *derived;
    6920          920 :   gfc_expr *target;
    6921          920 :   sym = e->symtree->n.sym;
    6922              : 
    6923              :   /* An associate_name whose selector is (i) a component ref of a selector
    6924              :      that is a inferred type associate_name; or (ii) an intrinsic type that
    6925              :      has been inferred from an inquiry ref.  */
    6926          920 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    6927              :     {
    6928          318 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    6929          318 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    6930          318 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    6931              :         {
    6932           60 :           ref = e->ref;
    6933              :           /* A substring misidentified as an array section.  */
    6934           60 :           if (sym->ts.type == BT_CHARACTER
    6935           30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    6936            6 :               && !ref->u.ar.stride[0])
    6937              :             {
    6938            6 :               new_ref = gfc_get_ref ();
    6939            6 :               new_ref->type = REF_SUBSTRING;
    6940            6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    6941            6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    6942            6 :               new_ref->u.ss.length = sym->ts.u.cl;
    6943            6 :               *ref = *new_ref;
    6944            6 :               free (new_ref);
    6945              :             }
    6946              :           else
    6947              :             {
    6948           54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    6949           24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    6950           54 :               e->ref = ref->next;
    6951           54 :               free (ref);
    6952              :             }
    6953              :         }
    6954              : 
    6955              :       /* It is possible for an inquiry reference to be mistaken for a
    6956              :          component reference. Correct this now.  */
    6957          318 :       ref = e->ref;
    6958          318 :       if (ref && ref->type == REF_ARRAY)
    6959          138 :         ref = ref->next;
    6960          186 :       if (ref && ref->type == REF_COMPONENT
    6961          150 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    6962              :         {
    6963           12 :           e->symtree->n.sym = sym;
    6964           12 :           *ref = *new_ref;
    6965           12 :           gfc_free_ref_list (new_ref);
    6966              :         }
    6967              : 
    6968              :       /* The kind of the associate name is best evaluated directly from the
    6969              :          selector because of the guesses made in primary.cc, when the type
    6970              :          is still unknown.  */
    6971          318 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    6972              :         {
    6973           24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    6974           12 :                                            sym->assoc->target->ts.kind);
    6975           12 :           gfc_replace_expr (e, ne);
    6976           12 :         }
    6977          174 :       else if (ref && ref->type == REF_INQUIRY
    6978          150 :                && (ref->u.i == INQUIRY_RE || ref->u.i == INQUIRY_IM)
    6979          114 :                && sym->ts.type == BT_COMPLEX
    6980          114 :                && e->ts.type == BT_REAL
    6981          114 :                && e->ts.kind != sym->ts.kind)
    6982              :         /* primary.cc set the inquiry-result kind to the default real kind
    6983              :            when the associate-name's type was inferred from %re/%im before
    6984              :            the target was resolved.  Now use the (resolved) selector kind.  */
    6985           24 :         e->ts.kind = sym->ts.kind;
    6986              : 
    6987              :       /* Now that the references are all sorted out, set the expression rank
    6988              :          and return.  */
    6989          318 :       gfc_expression_rank (e);
    6990          318 :       return;
    6991              :     }
    6992              : 
    6993          602 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    6994              :                                      : sym->ts.u.derived;
    6995              : 
    6996              :   /* Ensure that class symbols have an array spec and ensure that there
    6997              :      is a _data field reference following class type references.  */
    6998          602 :   if (sym->ts.type == BT_CLASS
    6999          196 :       && sym->assoc->target->ts.type == BT_CLASS)
    7000              :     {
    7001          196 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    7002          196 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    7003          196 :       sym->attr.dimension = 0;
    7004          196 :       sym->attr.codimension = 0;
    7005          196 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    7006          196 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    7007          196 :       if (e->ref && (e->ref->type != REF_COMPONENT
    7008          160 :                      || e->ref->u.c.component->name[0] != '_'))
    7009              :         {
    7010           82 :           ref = gfc_get_ref ();
    7011           82 :           ref->type = REF_COMPONENT;
    7012           82 :           ref->next = e->ref;
    7013           82 :           e->ref = ref;
    7014           82 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    7015              :                                                    true, true, NULL);
    7016           82 :           ref->u.c.sym = sym->ts.u.derived;
    7017              :         }
    7018              :     }
    7019              : 
    7020              :   /* Proceed as far as the first component reference and ensure that the
    7021              :      correct derived type is being used.  */
    7022          865 :   for (ref = e->ref; ref; ref = ref->next)
    7023          829 :     if (ref->type == REF_COMPONENT)
    7024              :       {
    7025          566 :         if (ref->u.c.component->name[0] != '_')
    7026          370 :           ref->u.c.sym = derived;
    7027              :         else
    7028          196 :           ref->u.c.sym = sym->ts.u.derived;
    7029              :         break;
    7030              :       }
    7031              : 
    7032              :   /* Verify that the type inference mechanism has not introduced a spurious
    7033              :      array reference.  This can happen with an associate name, whose selector
    7034              :      is an element of another inferred type.  */
    7035          602 :   target = e->symtree->n.sym->assoc->target;
    7036          602 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7037          190 :       && e != target && !target->rank)
    7038              :     {
    7039              :       /* First case: array ref after the scalar class or derived
    7040              :          associate_name.  */
    7041          190 :       if (e->ref && e->ref->type == REF_ARRAY
    7042            7 :           && e->ref->u.ar.type != AR_ELEMENT)
    7043              :         {
    7044            7 :           ref = e->ref;
    7045            7 :           if (ref->u.ar.type == AR_UNKNOWN)
    7046            1 :             gfc_error ("Invalid array reference at %L", &e->where);
    7047            7 :           e->ref = ref->next;
    7048            7 :           free (ref);
    7049              : 
    7050              :           /* If it hasn't a ref to the '_data' field supply one.  */
    7051            7 :           if (sym->ts.type == BT_CLASS
    7052            0 :               && !(e->ref->type == REF_COMPONENT
    7053            0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    7054              :             {
    7055            0 :               gfc_ref *new_ref;
    7056            0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    7057              :                                   "_data", true, true, &new_ref);
    7058            0 :               new_ref->next = e->ref;
    7059            0 :               e->ref = new_ref;
    7060              :             }
    7061              :         }
    7062              :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    7063          183 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7064          183 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    7065           64 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    7066            0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    7067              :         {
    7068            0 :           ref = e->ref->next;
    7069            0 :           if (ref->u.ar.type == AR_UNKNOWN)
    7070            0 :             gfc_error ("Invalid array reference at %L", &e->where);
    7071            0 :           e->ref->next = e->ref->next->next;
    7072            0 :           free (ref);
    7073              :         }
    7074              :     }
    7075              : 
    7076              :   /* Now that all the references are OK, get the expression rank.  */
    7077          602 :   gfc_expression_rank (e);
    7078              : }
    7079              : 
    7080              : 
    7081              : /* Checks to see that the correct symbol has been host associated.
    7082              :    The only situations where this arises are:
    7083              :         (i)  That in which a twice contained function is parsed after
    7084              :              the host association is made. On detecting this, change
    7085              :              the symbol in the expression and convert the array reference
    7086              :              into an actual arglist if the old symbol is a variable; or
    7087              :         (ii) That in which an external function is typed but not declared
    7088              :              explicitly to be external. Here, the old symbol is changed
    7089              :              from a variable to an external function.  */
    7090              : static bool
    7091      1682907 : check_host_association (gfc_expr *e)
    7092              : {
    7093      1682907 :   gfc_symbol *sym, *old_sym;
    7094      1682907 :   gfc_symtree *st;
    7095      1682907 :   int n;
    7096      1682907 :   gfc_ref *ref;
    7097      1682907 :   gfc_actual_arglist *arg, *tail = NULL;
    7098      1682907 :   bool retval = e->expr_type == EXPR_FUNCTION;
    7099              : 
    7100              :   /*  If the expression is the result of substitution in
    7101              :       interface.cc(gfc_extend_expr) because there is no way in
    7102              :       which the host association can be wrong.  */
    7103      1682907 :   if (e->symtree == NULL
    7104      1682106 :         || e->symtree->n.sym == NULL
    7105      1682106 :         || e->user_operator)
    7106              :     return retval;
    7107              : 
    7108      1680326 :   old_sym = e->symtree->n.sym;
    7109              : 
    7110      1680326 :   if (gfc_current_ns->parent
    7111       738135 :         && old_sym->ns != gfc_current_ns)
    7112              :     {
    7113              :       /* Use the 'USE' name so that renamed module symbols are
    7114              :          correctly handled.  */
    7115        92207 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7116              : 
    7117        92207 :       if (sym && old_sym != sym
    7118          702 :               && sym->attr.flavor == FL_PROCEDURE
    7119          105 :               && sym->attr.contained)
    7120              :         {
    7121              :           /* Clear the shape, since it might not be valid.  */
    7122           83 :           gfc_free_shape (&e->shape, e->rank);
    7123              : 
    7124              :           /* Give the expression the right symtree!  */
    7125           83 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    7126           83 :           gcc_assert (st != NULL);
    7127              : 
    7128           83 :           if (old_sym->attr.flavor == FL_PROCEDURE
    7129           59 :                 || e->expr_type == EXPR_FUNCTION)
    7130              :             {
    7131              :               /* Original was function so point to the new symbol, since
    7132              :                  the actual argument list is already attached to the
    7133              :                  expression.  */
    7134           30 :               e->value.function.esym = NULL;
    7135           30 :               e->symtree = st;
    7136              :             }
    7137              :           else
    7138              :             {
    7139              :               /* Original was variable so convert array references into
    7140              :                  an actual arglist. This does not need any checking now
    7141              :                  since resolve_function will take care of it.  */
    7142           53 :               e->value.function.actual = NULL;
    7143           53 :               e->expr_type = EXPR_FUNCTION;
    7144           53 :               e->symtree = st;
    7145              : 
    7146              :               /* Ambiguity will not arise if the array reference is not
    7147              :                  the last reference.  */
    7148           55 :               for (ref = e->ref; ref; ref = ref->next)
    7149           38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    7150              :                   break;
    7151              : 
    7152           53 :               if ((ref == NULL || ref->type != REF_ARRAY)
    7153           17 :                   && sym->attr.proc == PROC_INTERNAL)
    7154              :                 {
    7155            4 :                   gfc_error ("%qs at %L is host associated at %L into "
    7156              :                              "a contained procedure with an internal "
    7157              :                              "procedure of the same name", sym->name,
    7158              :                               &old_sym->declared_at, &e->where);
    7159            4 :                   return false;
    7160              :                 }
    7161              : 
    7162           13 :               if (ref == NULL)
    7163              :                 return false;
    7164              : 
    7165           36 :               gcc_assert (ref->type == REF_ARRAY);
    7166              : 
    7167              :               /* Grab the start expressions from the array ref and
    7168              :                  copy them into actual arguments.  */
    7169           84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7170              :                 {
    7171           48 :                   arg = gfc_get_actual_arglist ();
    7172           48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    7173           48 :                   if (e->value.function.actual == NULL)
    7174           36 :                     tail = e->value.function.actual = arg;
    7175              :                   else
    7176              :                     {
    7177           12 :                       tail->next = arg;
    7178           12 :                       tail = arg;
    7179              :                     }
    7180              :                 }
    7181              : 
    7182              :               /* Dump the reference list and set the rank.  */
    7183           36 :               gfc_free_ref_list (e->ref);
    7184           36 :               e->ref = NULL;
    7185           36 :               e->rank = sym->as ? sym->as->rank : 0;
    7186           36 :               e->corank = sym->as ? sym->as->corank : 0;
    7187              :             }
    7188              : 
    7189           66 :           gfc_resolve_expr (e);
    7190           66 :           sym->refs++;
    7191              :         }
    7192              :       /* This case corresponds to a call, from a block or a contained
    7193              :          procedure, to an external function, which has not been declared
    7194              :          as being external in the main program but has been typed.  */
    7195        92124 :       else if (sym && old_sym != sym
    7196          619 :                && !e->ref
    7197          347 :                && sym->ts.type == BT_UNKNOWN
    7198           21 :                && old_sym->ts.type != BT_UNKNOWN
    7199           19 :                && sym->attr.flavor == FL_PROCEDURE
    7200           19 :                && old_sym->attr.flavor == FL_VARIABLE
    7201            7 :                && sym->ns->parent == old_sym->ns
    7202            7 :                && sym->ns->proc_name
    7203            7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    7204            6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    7205            6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    7206              :         {
    7207            6 :           old_sym->attr.flavor = FL_PROCEDURE;
    7208            6 :           old_sym->attr.external = 1;
    7209            6 :           old_sym->attr.function = 1;
    7210            6 :           old_sym->result = old_sym;
    7211            6 :           gfc_resolve_expr (e);
    7212              :         }
    7213              :     }
    7214              :   /* This might have changed!  */
    7215      1680309 :   return e->expr_type == EXPR_FUNCTION;
    7216              : }
    7217              : 
    7218              : 
    7219              : static void
    7220         1454 : gfc_resolve_character_operator (gfc_expr *e)
    7221              : {
    7222         1454 :   gfc_expr *op1 = e->value.op.op1;
    7223         1454 :   gfc_expr *op2 = e->value.op.op2;
    7224         1454 :   gfc_expr *e1 = NULL;
    7225         1454 :   gfc_expr *e2 = NULL;
    7226              : 
    7227         1454 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7228              : 
    7229         1454 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7230          767 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7231          687 :   else if (op1->expr_type == EXPR_CONSTANT)
    7232          268 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7233          268 :                            op1->value.character.length);
    7234              : 
    7235         1454 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7236          755 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7237          699 :   else if (op2->expr_type == EXPR_CONSTANT)
    7238          468 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7239          468 :                            op2->value.character.length);
    7240              : 
    7241         1454 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7242              : 
    7243         1454 :   if (!e1 || !e2)
    7244              :     {
    7245          547 :       gfc_free_expr (e1);
    7246          547 :       gfc_free_expr (e2);
    7247              : 
    7248          547 :       return;
    7249              :     }
    7250              : 
    7251          907 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7252          907 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7253          907 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7254          907 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7255          907 :   gfc_resolve_expr (e->ts.u.cl->length);
    7256              : 
    7257          907 :   return;
    7258              : }
    7259              : 
    7260              : 
    7261              : /*  Ensure that an character expression has a charlen and, if possible, a
    7262              :     length expression.  */
    7263              : 
    7264              : static void
    7265       184682 : fixup_charlen (gfc_expr *e)
    7266              : {
    7267              :   /* The cases fall through so that changes in expression type and the need
    7268              :      for multiple fixes are picked up.  In all circumstances, a charlen should
    7269              :      be available for the middle end to hang a backend_decl on.  */
    7270       184682 :   switch (e->expr_type)
    7271              :     {
    7272         1454 :     case EXPR_OP:
    7273         1454 :       gfc_resolve_character_operator (e);
    7274              :       /* FALLTHRU */
    7275              : 
    7276         1521 :     case EXPR_ARRAY:
    7277         1521 :       if (e->expr_type == EXPR_ARRAY)
    7278           67 :         gfc_resolve_character_array_constructor (e);
    7279              :       /* FALLTHRU */
    7280              : 
    7281         1978 :     case EXPR_SUBSTRING:
    7282         1978 :       if (!e->ts.u.cl && e->ref)
    7283          453 :         gfc_resolve_substring_charlen (e);
    7284              :       /* FALLTHRU */
    7285              : 
    7286       184682 :     default:
    7287       184682 :       if (!e->ts.u.cl)
    7288       182708 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7289              : 
    7290       184682 :       break;
    7291              :     }
    7292       184682 : }
    7293              : 
    7294              : 
    7295              : /* Update an actual argument to include the passed-object for type-bound
    7296              :    procedures at the right position.  */
    7297              : 
    7298              : static gfc_actual_arglist*
    7299         3038 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7300              :                      const char *name)
    7301              : {
    7302         3062 :   gcc_assert (argpos > 0);
    7303              : 
    7304         3062 :   if (argpos == 1)
    7305              :     {
    7306         2913 :       gfc_actual_arglist* result;
    7307              : 
    7308         2913 :       result = gfc_get_actual_arglist ();
    7309         2913 :       result->expr = po;
    7310         2913 :       result->next = lst;
    7311         2913 :       if (name)
    7312          514 :         result->name = name;
    7313              : 
    7314         2913 :       return result;
    7315              :     }
    7316              : 
    7317          149 :   if (lst)
    7318          125 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    7319              :   else
    7320           24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    7321              :   return lst;
    7322              : }
    7323              : 
    7324              : 
    7325              : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    7326              : 
    7327              : static gfc_expr*
    7328         7395 : extract_compcall_passed_object (gfc_expr* e)
    7329              : {
    7330         7395 :   gfc_expr* po;
    7331              : 
    7332         7395 :   if (e->expr_type == EXPR_UNKNOWN)
    7333              :     {
    7334            0 :       gfc_error ("Error in typebound call at %L",
    7335              :                  &e->where);
    7336            0 :       return NULL;
    7337              :     }
    7338              : 
    7339         7395 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7340              : 
    7341         7395 :   if (e->value.compcall.base_object)
    7342         1656 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7343              :   else
    7344              :     {
    7345         5739 :       po = gfc_get_expr ();
    7346         5739 :       po->expr_type = EXPR_VARIABLE;
    7347         5739 :       po->symtree = e->symtree;
    7348         5739 :       po->ref = gfc_copy_ref (e->ref);
    7349         5739 :       po->where = e->where;
    7350              :     }
    7351              : 
    7352         7395 :   if (!gfc_resolve_expr (po))
    7353              :     return NULL;
    7354              : 
    7355              :   return po;
    7356              : }
    7357              : 
    7358              : 
    7359              : /* Update the arglist of an EXPR_COMPCALL expression to include the
    7360              :    passed-object.  */
    7361              : 
    7362              : static bool
    7363         3402 : update_compcall_arglist (gfc_expr* e)
    7364              : {
    7365         3402 :   gfc_expr* po;
    7366         3402 :   gfc_typebound_proc* tbp;
    7367              : 
    7368         3402 :   tbp = e->value.compcall.tbp;
    7369              : 
    7370         3402 :   if (tbp->error)
    7371              :     return false;
    7372              : 
    7373         3401 :   po = extract_compcall_passed_object (e);
    7374         3401 :   if (!po)
    7375              :     return false;
    7376              : 
    7377         3401 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7378              :     {
    7379         1152 :       gfc_free_expr (po);
    7380         1152 :       return true;
    7381              :     }
    7382              : 
    7383         2249 :   if (tbp->pass_arg_num <= 0)
    7384              :     return false;
    7385              : 
    7386         2248 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7387              :                                                   tbp->pass_arg_num,
    7388              :                                                   tbp->pass_arg);
    7389              : 
    7390         2248 :   return true;
    7391              : }
    7392              : 
    7393              : 
    7394              : /* Extract the passed object from a PPC call (a copy of it).  */
    7395              : 
    7396              : static gfc_expr*
    7397           85 : extract_ppc_passed_object (gfc_expr *e)
    7398              : {
    7399           85 :   gfc_expr *po;
    7400           85 :   gfc_ref **ref;
    7401              : 
    7402           85 :   po = gfc_get_expr ();
    7403           85 :   po->expr_type = EXPR_VARIABLE;
    7404           85 :   po->symtree = e->symtree;
    7405           85 :   po->ref = gfc_copy_ref (e->ref);
    7406           85 :   po->where = e->where;
    7407              : 
    7408              :   /* Remove PPC reference.  */
    7409           85 :   ref = &po->ref;
    7410           91 :   while ((*ref)->next)
    7411            6 :     ref = &(*ref)->next;
    7412           85 :   gfc_free_ref_list (*ref);
    7413           85 :   *ref = NULL;
    7414              : 
    7415           85 :   if (!gfc_resolve_expr (po))
    7416            0 :     return NULL;
    7417              : 
    7418              :   return po;
    7419              : }
    7420              : 
    7421              : 
    7422              : /* Update the actual arglist of a procedure pointer component to include the
    7423              :    passed-object.  */
    7424              : 
    7425              : static bool
    7426          588 : update_ppc_arglist (gfc_expr* e)
    7427              : {
    7428          588 :   gfc_expr* po;
    7429          588 :   gfc_component *ppc;
    7430          588 :   gfc_typebound_proc* tb;
    7431              : 
    7432          588 :   ppc = gfc_get_proc_ptr_comp (e);
    7433          588 :   if (!ppc)
    7434              :     return false;
    7435              : 
    7436          588 :   tb = ppc->tb;
    7437              : 
    7438          588 :   if (tb->error)
    7439              :     return false;
    7440          586 :   else if (tb->nopass)
    7441              :     return true;
    7442              : 
    7443           85 :   po = extract_ppc_passed_object (e);
    7444           85 :   if (!po)
    7445              :     return false;
    7446              : 
    7447              :   /* F08:R739.  */
    7448           85 :   if (po->rank != 0)
    7449              :     {
    7450            0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7451            0 :       return false;
    7452              :     }
    7453              : 
    7454              :   /* F08:C611.  */
    7455           85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7456              :     {
    7457            1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7458              :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7459            1 :       return false;
    7460              :     }
    7461              : 
    7462           84 :   gcc_assert (tb->pass_arg_num > 0);
    7463           84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7464              :                                                   tb->pass_arg_num,
    7465              :                                                   tb->pass_arg);
    7466              : 
    7467           84 :   return true;
    7468              : }
    7469              : 
    7470              : 
    7471              : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7472              :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7473              : 
    7474              : static bool
    7475         3413 : check_typebound_baseobject (gfc_expr* e)
    7476              : {
    7477         3413 :   gfc_expr* base;
    7478         3413 :   bool return_value = false;
    7479              : 
    7480         3413 :   base = extract_compcall_passed_object (e);
    7481         3413 :   if (!base)
    7482              :     return false;
    7483              : 
    7484         3410 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7485              :     {
    7486            1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7487            1 :       goto cleanup;
    7488              :     }
    7489              : 
    7490         3409 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7491            1 :     return false;
    7492              : 
    7493              :   /* F08:C611.  */
    7494         3408 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7495              :     {
    7496            3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7497              :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7498            3 :       goto cleanup;
    7499              :     }
    7500              : 
    7501              :   /* F08:C1230. If the procedure called is NOPASS,
    7502              :      the base object must be scalar.  */
    7503         3405 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7504              :     {
    7505            1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7506              :                  " be scalar", &e->where);
    7507            1 :       goto cleanup;
    7508              :     }
    7509              : 
    7510              :   return_value = true;
    7511              : 
    7512         3409 : cleanup:
    7513         3409 :   gfc_free_expr (base);
    7514         3409 :   return return_value;
    7515              : }
    7516              : 
    7517              : 
    7518              : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7519              :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7520              :    arglist and the target-procedure symtree are returned.  */
    7521              : 
    7522              : static bool
    7523         3402 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7524              :                           gfc_actual_arglist** actual)
    7525              : {
    7526         3402 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7527         3402 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7528              : 
    7529              :   /* Update the actual arglist for PASS.  */
    7530         3402 :   if (!update_compcall_arglist (e))
    7531              :     return false;
    7532              : 
    7533         3400 :   *actual = e->value.compcall.actual;
    7534         3400 :   *target = e->value.compcall.tbp->u.specific;
    7535              : 
    7536         3400 :   gfc_free_ref_list (e->ref);
    7537         3400 :   e->ref = NULL;
    7538         3400 :   e->value.compcall.actual = NULL;
    7539              : 
    7540              :   /* If we find a deferred typebound procedure, check for derived types
    7541              :      that an overriding typebound procedure has not been missed.  */
    7542         3400 :   if (e->value.compcall.name
    7543         3400 :       && !e->value.compcall.tbp->non_overridable
    7544         3382 :       && e->value.compcall.base_object
    7545          828 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7546              :     {
    7547          535 :       gfc_symtree *st;
    7548          535 :       gfc_symbol *derived;
    7549              : 
    7550              :       /* Use the derived type of the base_object.  */
    7551          535 :       derived = e->value.compcall.base_object->ts.u.derived;
    7552          535 :       st = NULL;
    7553              : 
    7554              :       /* If necessary, go through the inheritance chain.  */
    7555         1613 :       while (!st && derived)
    7556              :         {
    7557              :           /* Look for the typebound procedure 'name'.  */
    7558          543 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7559          535 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7560              :                                    e->value.compcall.name);
    7561          543 :           if (!st)
    7562            8 :             derived = gfc_get_derived_super_type (derived);
    7563              :         }
    7564              : 
    7565              :       /* Now find the specific name in the derived type namespace.  */
    7566          535 :       if (st && st->n.tb && st->n.tb->u.specific)
    7567          535 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7568          535 :                            derived->ns, 1, &st);
    7569          535 :       if (st)
    7570          535 :         *target = st;
    7571              :     }
    7572              : 
    7573         3400 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7574         3400 :       && !e->value.compcall.tbp->deferred)
    7575            1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7576              :                  " itself recursively.  Declare it RECURSIVE or use"
    7577              :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7578              : 
    7579              :   return true;
    7580              : }
    7581              : 
    7582              : 
    7583              : /* Get the ultimate declared type from an expression.  In addition,
    7584              :    return the last class/derived type reference and the copy of the
    7585              :    reference list.  If check_types is set true, derived types are
    7586              :    identified as well as class references.  */
    7587              : static gfc_symbol*
    7588         3321 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7589              :                         gfc_expr *e, bool check_types)
    7590              : {
    7591         3321 :   gfc_symbol *declared;
    7592         3321 :   gfc_ref *ref;
    7593              : 
    7594         3321 :   declared = NULL;
    7595         3321 :   if (class_ref)
    7596         2888 :     *class_ref = NULL;
    7597         3321 :   if (new_ref)
    7598         2595 :     *new_ref = gfc_copy_ref (e->ref);
    7599              : 
    7600         4116 :   for (ref = e->ref; ref; ref = ref->next)
    7601              :     {
    7602          795 :       if (ref->type != REF_COMPONENT)
    7603          292 :         continue;
    7604              : 
    7605          503 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7606          256 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7607          428 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7608              :         {
    7609          354 :           declared = ref->u.c.component->ts.u.derived;
    7610          354 :           if (class_ref)
    7611          332 :             *class_ref = ref;
    7612              :         }
    7613              :     }
    7614              : 
    7615         3321 :   if (declared == NULL)
    7616         2993 :     declared = e->symtree->n.sym->ts.u.derived;
    7617              : 
    7618         3321 :   return declared;
    7619              : }
    7620              : 
    7621              : 
    7622              : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7623              :    which of the specific bindings (if any) matches the arglist and transform
    7624              :    the expression into a call of that binding.  */
    7625              : 
    7626              : static bool
    7627         3404 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7628              : {
    7629         3404 :   gfc_typebound_proc* genproc;
    7630         3404 :   const char* genname;
    7631         3404 :   gfc_symtree *st;
    7632         3404 :   gfc_symbol *derived;
    7633              : 
    7634         3404 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7635         3404 :   genname = e->value.compcall.name;
    7636         3404 :   genproc = e->value.compcall.tbp;
    7637              : 
    7638         3404 :   if (!genproc->is_generic)
    7639              :     return true;
    7640              : 
    7641              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7642          445 :   for (; genproc; genproc = genproc->overridden)
    7643              :     {
    7644          443 :       gfc_tbp_generic* g;
    7645              : 
    7646          443 :       gcc_assert (genproc->is_generic);
    7647          677 :       for (g = genproc->u.generic; g; g = g->next)
    7648              :         {
    7649          667 :           gfc_symbol* target;
    7650          667 :           gfc_actual_arglist* args;
    7651          667 :           bool matches;
    7652              : 
    7653          667 :           gcc_assert (g->specific);
    7654              : 
    7655          667 :           if (g->specific->error)
    7656            0 :             continue;
    7657              : 
    7658          667 :           target = g->specific->u.specific->n.sym;
    7659              : 
    7660              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7661          667 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7662          667 :           if (!g->specific->nopass)
    7663              :             {
    7664          581 :               gfc_expr* po;
    7665          581 :               po = extract_compcall_passed_object (e);
    7666          581 :               if (!po)
    7667              :                 {
    7668            0 :                   gfc_free_actual_arglist (args);
    7669            0 :                   return false;
    7670              :                 }
    7671              : 
    7672          581 :               gcc_assert (g->specific->pass_arg_num > 0);
    7673          581 :               gcc_assert (!g->specific->error);
    7674          581 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7675              :                                           g->specific->pass_arg);
    7676              :             }
    7677          667 :           resolve_actual_arglist (args, target->attr.proc,
    7678          667 :                                   is_external_proc (target)
    7679          667 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7680              : 
    7681              :           /* Check if this arglist matches the formal.  */
    7682          667 :           matches = gfc_arglist_matches_symbol (&args, target);
    7683              : 
    7684              :           /* Clean up and break out of the loop if we've found it.  */
    7685          667 :           gfc_free_actual_arglist (args);
    7686          667 :           if (matches)
    7687              :             {
    7688          433 :               e->value.compcall.tbp = g->specific;
    7689          433 :               genname = g->specific_st->name;
    7690              :               /* Pass along the name for CLASS methods, where the vtab
    7691              :                  procedure pointer component has to be referenced.  */
    7692          433 :               if (name)
    7693          161 :                 *name = genname;
    7694          433 :               goto success;
    7695              :             }
    7696              :         }
    7697              :     }
    7698              : 
    7699              :   /* Nothing matching found!  */
    7700            2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7701              :              " %qs at %L", genname, &e->where);
    7702            2 :   return false;
    7703              : 
    7704          433 : success:
    7705              :   /* Make sure that we have the right specific instance for the name.  */
    7706          433 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7707              : 
    7708          433 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7709          433 :   if (st)
    7710          433 :     e->value.compcall.tbp = st->n.tb;
    7711              : 
    7712              :   return true;
    7713              : }
    7714              : 
    7715              : 
    7716              : /* Resolve a call to a type-bound subroutine.  */
    7717              : 
    7718              : static bool
    7719         1756 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7720              : {
    7721         1756 :   gfc_actual_arglist* newactual;
    7722         1756 :   gfc_symtree* target;
    7723              : 
    7724              :   /* Check that's really a SUBROUTINE.  */
    7725         1756 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7726              :     {
    7727           17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7728           15 :           && c->expr1->value.compcall.tbp->u.specific
    7729           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7730           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7731           12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7732              :       else
    7733              :         {
    7734            5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7735              :                      c->expr1->value.compcall.name, &c->loc);
    7736            5 :           return false;
    7737              :         }
    7738              :     }
    7739              : 
    7740         1751 :   if (!check_typebound_baseobject (c->expr1))
    7741              :     return false;
    7742              : 
    7743              :   /* Pass along the name for CLASS methods, where the vtab
    7744              :      procedure pointer component has to be referenced.  */
    7745         1744 :   if (name)
    7746          480 :     *name = c->expr1->value.compcall.name;
    7747              : 
    7748         1744 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7749              :     return false;
    7750              : 
    7751              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7752         1743 :   if (overridable)
    7753          371 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7754              : 
    7755              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7756              : 
    7757         1743 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7758              :     return false;
    7759              : 
    7760         1741 :   c->ext.actual = newactual;
    7761         1741 :   c->symtree = target;
    7762         1741 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7763              : 
    7764         1741 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7765              : 
    7766         1741 :   gfc_free_expr (c->expr1);
    7767         1741 :   c->expr1 = gfc_get_expr ();
    7768         1741 :   c->expr1->expr_type = EXPR_FUNCTION;
    7769         1741 :   c->expr1->symtree = target;
    7770         1741 :   c->expr1->where = c->loc;
    7771              : 
    7772         1741 :   return resolve_call (c);
    7773              : }
    7774              : 
    7775              : 
    7776              : /* Resolve a component-call expression.  */
    7777              : static bool
    7778         1669 : resolve_compcall (gfc_expr* e, const char **name)
    7779              : {
    7780         1669 :   gfc_actual_arglist* newactual;
    7781         1669 :   gfc_symtree* target;
    7782              : 
    7783              :   /* Check that's really a FUNCTION.  */
    7784         1669 :   if (!e->value.compcall.tbp->function)
    7785              :     {
    7786            7 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    7787            5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    7788              :                    &e->where);
    7789            7 :       return false;
    7790              :     }
    7791              : 
    7792              : 
    7793              :   /* These must not be assign-calls!  */
    7794         1662 :   gcc_assert (!e->value.compcall.assign);
    7795              : 
    7796         1662 :   if (!check_typebound_baseobject (e))
    7797              :     return false;
    7798              : 
    7799              :   /* Pass along the name for CLASS methods, where the vtab
    7800              :      procedure pointer component has to be referenced.  */
    7801         1660 :   if (name)
    7802          864 :     *name = e->value.compcall.name;
    7803              : 
    7804         1660 :   if (!resolve_typebound_generic_call (e, name))
    7805              :     return false;
    7806         1659 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7807              : 
    7808              :   /* Take the rank from the function's symbol.  */
    7809         1659 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    7810              :     {
    7811          155 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    7812          155 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    7813              :     }
    7814              : 
    7815              :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    7816              :      arglist to the TBP's binding target.  */
    7817              : 
    7818         1659 :   if (!resolve_typebound_static (e, &target, &newactual))
    7819              :     return false;
    7820              : 
    7821         1659 :   e->value.function.actual = newactual;
    7822         1659 :   e->value.function.name = NULL;
    7823         1659 :   e->value.function.esym = target->n.sym;
    7824         1659 :   e->value.function.isym = NULL;
    7825         1659 :   e->symtree = target;
    7826         1659 :   e->ts = target->n.sym->ts;
    7827         1659 :   e->expr_type = EXPR_FUNCTION;
    7828              : 
    7829              :   /* Resolution is not necessary if this is a class subroutine; this
    7830              :      function only has to identify the specific proc. Resolution of
    7831              :      the call will be done next in resolve_typebound_call.  */
    7832         1659 :   return gfc_resolve_expr (e);
    7833              : }
    7834              : 
    7835              : 
    7836              : static bool resolve_fl_derived (gfc_symbol *sym);
    7837              : 
    7838              : 
    7839              : /* Resolve a typebound function, or 'method'. First separate all
    7840              :    the non-CLASS references by calling resolve_compcall directly.  */
    7841              : 
    7842              : static bool
    7843         1669 : resolve_typebound_function (gfc_expr* e)
    7844              : {
    7845         1669 :   gfc_symbol *declared;
    7846         1669 :   gfc_component *c;
    7847         1669 :   gfc_ref *new_ref;
    7848         1669 :   gfc_ref *class_ref;
    7849         1669 :   gfc_symtree *st;
    7850         1669 :   const char *name;
    7851         1669 :   gfc_typespec ts;
    7852         1669 :   gfc_expr *expr;
    7853         1669 :   bool overridable;
    7854              : 
    7855         1669 :   st = e->symtree;
    7856              : 
    7857              :   /* Deal with typebound operators for CLASS objects.  */
    7858         1669 :   expr = e->value.compcall.base_object;
    7859         1669 :   overridable = !e->value.compcall.tbp->non_overridable;
    7860         1669 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    7861              :     {
    7862              :       /* Since the typebound operators are generic, we have to ensure
    7863              :          that any delays in resolution are corrected and that the vtab
    7864              :          is present.  */
    7865          184 :       ts = expr->ts;
    7866          184 :       declared = ts.u.derived;
    7867          184 :       if (!resolve_fl_derived (declared))
    7868              :         return false;
    7869              : 
    7870          184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7871          184 :       if (c->ts.u.derived == NULL)
    7872            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7873              : 
    7874          184 :       if (!resolve_compcall (e, &name))
    7875              :         return false;
    7876              : 
    7877              :       /* Use the generic name if it is there.  */
    7878          184 :       name = name ? name : e->value.function.esym->name;
    7879          184 :       e->symtree = expr->symtree;
    7880          184 :       e->ref = gfc_copy_ref (expr->ref);
    7881          184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    7882              : 
    7883              :       /* Trim away the extraneous references that emerge from nested
    7884              :          use of interface.cc (extend_expr).  */
    7885          184 :       if (class_ref && class_ref->next)
    7886              :         {
    7887            0 :           gfc_free_ref_list (class_ref->next);
    7888            0 :           class_ref->next = NULL;
    7889              :         }
    7890          184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    7891              :         {
    7892            0 :           gfc_free_ref_list (e->ref);
    7893            0 :           e->ref = NULL;
    7894              :         }
    7895              : 
    7896          184 :       gfc_add_vptr_component (e);
    7897          184 :       gfc_add_component_ref (e, name);
    7898          184 :       e->value.function.esym = NULL;
    7899          184 :       if (expr->expr_type != EXPR_VARIABLE)
    7900           80 :         e->base_expr = expr;
    7901          184 :       return true;
    7902              :     }
    7903              : 
    7904         1485 :   if (st == NULL)
    7905          195 :     return resolve_compcall (e, NULL);
    7906              : 
    7907         1290 :   if (!gfc_resolve_ref (e))
    7908              :     return false;
    7909              : 
    7910              :   /* It can happen that a generic, typebound procedure is marked as overridable
    7911              :      with all of the specific procedures being non-overridable. If this is the
    7912              :      case, it is safe to resolve the compcall.  */
    7913         1290 :   if (!expr && overridable
    7914         1282 :       && e->value.compcall.tbp->is_generic
    7915          198 :       && e->value.compcall.tbp->u.generic->specific
    7916          197 :       && e->value.compcall.tbp->u.generic->specific->non_overridable)
    7917              :     {
    7918              :       gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
    7919            6 :       for (; g; g = g->next)
    7920            4 :         if (!g->specific->non_overridable)
    7921              :           break;
    7922            2 :       if (g == NULL && resolve_compcall (e, &name))
    7923              :         return true;
    7924              :     }
    7925              : 
    7926              :   /* Get the CLASS declared type.  */
    7927         1288 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7928              : 
    7929         1288 :   if (!resolve_fl_derived (declared))
    7930              :     return false;
    7931              : 
    7932              :   /* Weed out cases of the ultimate component being a derived type.  */
    7933         1288 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7934         1194 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7935              :     {
    7936          608 :       gfc_free_ref_list (new_ref);
    7937          608 :       return resolve_compcall (e, NULL);
    7938              :     }
    7939              : 
    7940          680 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    7941              : 
    7942              :   /* Treat the call as if it is a typebound procedure, in order to roll
    7943              :      out the correct name for the specific function.  */
    7944          680 :   if (!resolve_compcall (e, &name))
    7945              :     {
    7946            3 :       gfc_free_ref_list (new_ref);
    7947            3 :       return false;
    7948              :     }
    7949          677 :   ts = e->ts;
    7950              : 
    7951          677 :   if (overridable)
    7952              :     {
    7953              :       /* Convert the expression to a procedure pointer component call.  */
    7954          675 :       e->value.function.esym = NULL;
    7955          675 :       e->symtree = st;
    7956              : 
    7957          675 :       if (new_ref)
    7958          125 :         e->ref = new_ref;
    7959              : 
    7960              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7961          675 :       gfc_add_vptr_component (e);
    7962          675 :       gfc_add_component_ref (e, name);
    7963              : 
    7964              :       /* Recover the typespec for the expression.  This is really only
    7965              :         necessary for generic procedures, where the additional call
    7966              :         to gfc_add_component_ref seems to throw the collection of the
    7967              :         correct typespec.  */
    7968          675 :       e->ts = ts;
    7969              :     }
    7970            2 :   else if (new_ref)
    7971            0 :     gfc_free_ref_list (new_ref);
    7972              : 
    7973              :   return true;
    7974              : }
    7975              : 
    7976              : /* Resolve a typebound subroutine, or 'method'. First separate all
    7977              :    the non-CLASS references by calling resolve_typebound_call
    7978              :    directly.  */
    7979              : 
    7980              : static bool
    7981         1756 : resolve_typebound_subroutine (gfc_code *code)
    7982              : {
    7983         1756 :   gfc_symbol *declared;
    7984         1756 :   gfc_component *c;
    7985         1756 :   gfc_ref *new_ref;
    7986         1756 :   gfc_ref *class_ref;
    7987         1756 :   gfc_symtree *st;
    7988         1756 :   const char *name;
    7989         1756 :   gfc_typespec ts;
    7990         1756 :   gfc_expr *expr;
    7991         1756 :   bool overridable;
    7992              : 
    7993         1756 :   st = code->expr1->symtree;
    7994              : 
    7995              :   /* Deal with typebound operators for CLASS objects.  */
    7996         1756 :   expr = code->expr1->value.compcall.base_object;
    7997         1756 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7998         1756 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    7999              :     {
    8000              :       /* If the base_object is not a variable, the corresponding actual
    8001              :          argument expression must be stored in e->base_expression so
    8002              :          that the corresponding tree temporary can be used as the base
    8003              :          object in gfc_conv_procedure_call.  */
    8004          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8005              :         {
    8006              :           gfc_actual_arglist *args;
    8007              : 
    8008              :           args= code->expr1->value.function.actual;
    8009              :           for (; args; args = args->next)
    8010              :             if (expr == args->expr)
    8011              :               expr = args->expr;
    8012              :         }
    8013              : 
    8014              :       /* Since the typebound operators are generic, we have to ensure
    8015              :          that any delays in resolution are corrected and that the vtab
    8016              :          is present.  */
    8017          109 :       declared = expr->ts.u.derived;
    8018          109 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    8019          109 :       if (c->ts.u.derived == NULL)
    8020            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8021              : 
    8022          109 :       if (!resolve_typebound_call (code, &name, NULL))
    8023              :         return false;
    8024              : 
    8025              :       /* Use the generic name if it is there.  */
    8026          109 :       name = name ? name : code->expr1->value.function.esym->name;
    8027          109 :       code->expr1->symtree = expr->symtree;
    8028          109 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    8029              : 
    8030              :       /* Trim away the extraneous references that emerge from nested
    8031              :          use of interface.cc (extend_expr).  */
    8032          109 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    8033          109 :       if (class_ref && class_ref->next)
    8034              :         {
    8035            0 :           gfc_free_ref_list (class_ref->next);
    8036            0 :           class_ref->next = NULL;
    8037              :         }
    8038          109 :       else if (code->expr1->ref && !class_ref)
    8039              :         {
    8040           18 :           gfc_free_ref_list (code->expr1->ref);
    8041           18 :           code->expr1->ref = NULL;
    8042              :         }
    8043              : 
    8044              :       /* Now use the procedure in the vtable.  */
    8045          109 :       gfc_add_vptr_component (code->expr1);
    8046          109 :       gfc_add_component_ref (code->expr1, name);
    8047          109 :       code->expr1->value.function.esym = NULL;
    8048          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8049            0 :         code->expr1->base_expr = expr;
    8050          109 :       return true;
    8051              :     }
    8052              : 
    8053         1647 :   if (st == NULL)
    8054          340 :     return resolve_typebound_call (code, NULL, NULL);
    8055              : 
    8056         1307 :   if (!gfc_resolve_ref (code->expr1))
    8057              :     return false;
    8058              : 
    8059              :   /* Get the CLASS declared type.  */
    8060         1307 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    8061              : 
    8062              :   /* Weed out cases of the ultimate component being a derived type.  */
    8063         1307 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8064         1242 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8065              :     {
    8066          931 :       gfc_free_ref_list (new_ref);
    8067          931 :       return resolve_typebound_call (code, NULL, NULL);
    8068              :     }
    8069              : 
    8070          376 :   if (!resolve_typebound_call (code, &name, &overridable))
    8071              :     {
    8072            5 :       gfc_free_ref_list (new_ref);
    8073            5 :       return false;
    8074              :     }
    8075          371 :   ts = code->expr1->ts;
    8076              : 
    8077          371 :   if (overridable)
    8078              :     {
    8079              :       /* Convert the expression to a procedure pointer component call.  */
    8080          369 :       code->expr1->value.function.esym = NULL;
    8081          369 :       code->expr1->symtree = st;
    8082              : 
    8083          369 :       if (new_ref)
    8084           93 :         code->expr1->ref = new_ref;
    8085              : 
    8086              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8087          369 :       gfc_add_vptr_component (code->expr1);
    8088          369 :       gfc_add_component_ref (code->expr1, name);
    8089              : 
    8090              :       /* Recover the typespec for the expression.  This is really only
    8091              :         necessary for generic procedures, where the additional call
    8092              :         to gfc_add_component_ref seems to throw the collection of the
    8093              :         correct typespec.  */
    8094          369 :       code->expr1->ts = ts;
    8095              :     }
    8096            2 :   else if (new_ref)
    8097            0 :     gfc_free_ref_list (new_ref);
    8098              : 
    8099              :   return true;
    8100              : }
    8101              : 
    8102              : 
    8103              : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    8104              : 
    8105              : static bool
    8106          124 : resolve_ppc_call (gfc_code* c)
    8107              : {
    8108          124 :   gfc_component *comp;
    8109              : 
    8110          124 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    8111          124 :   gcc_assert (comp != NULL);
    8112              : 
    8113          124 :   c->resolved_sym = c->expr1->symtree->n.sym;
    8114          124 :   c->expr1->expr_type = EXPR_VARIABLE;
    8115              : 
    8116          124 :   if (!comp->attr.subroutine)
    8117            1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    8118              : 
    8119          124 :   if (!gfc_resolve_ref (c->expr1))
    8120              :     return false;
    8121              : 
    8122          124 :   if (!update_ppc_arglist (c->expr1))
    8123              :     return false;
    8124              : 
    8125          123 :   c->ext.actual = c->expr1->value.compcall.actual;
    8126              : 
    8127          123 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    8128          123 :                                !(comp->ts.interface
    8129           93 :                                  && comp->ts.interface->formal)))
    8130              :     return false;
    8131              : 
    8132          123 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    8133              :     return false;
    8134              : 
    8135          122 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    8136              : 
    8137          122 :   return true;
    8138              : }
    8139              : 
    8140              : 
    8141              : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    8142              : 
    8143              : static bool
    8144          464 : resolve_expr_ppc (gfc_expr* e)
    8145              : {
    8146          464 :   gfc_component *comp;
    8147              : 
    8148          464 :   comp = gfc_get_proc_ptr_comp (e);
    8149          464 :   gcc_assert (comp != NULL);
    8150              : 
    8151              :   /* Convert to EXPR_FUNCTION.  */
    8152          464 :   e->expr_type = EXPR_FUNCTION;
    8153          464 :   e->value.function.isym = NULL;
    8154          464 :   e->value.function.actual = e->value.compcall.actual;
    8155          464 :   e->ts = comp->ts;
    8156          464 :   if (comp->as != NULL)
    8157              :     {
    8158           28 :       e->rank = comp->as->rank;
    8159           28 :       e->corank = comp->as->corank;
    8160              :     }
    8161              : 
    8162          464 :   if (!comp->attr.function)
    8163            3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    8164              : 
    8165          464 :   if (!gfc_resolve_ref (e))
    8166              :     return false;
    8167              : 
    8168          464 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    8169          464 :                                !(comp->ts.interface
    8170          463 :                                  && comp->ts.interface->formal)))
    8171              :     return false;
    8172              : 
    8173          464 :   if (!update_ppc_arglist (e))
    8174              :     return false;
    8175              : 
    8176          462 :   if (!check_pure_function(e))
    8177              :     return false;
    8178              : 
    8179          461 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    8180              : 
    8181          461 :   return true;
    8182              : }
    8183              : 
    8184              : 
    8185              : static bool
    8186        12115 : gfc_is_expandable_expr (gfc_expr *e)
    8187              : {
    8188        12115 :   gfc_constructor *con;
    8189              : 
    8190        12115 :   if (e->expr_type == EXPR_ARRAY)
    8191              :     {
    8192              :       /* Traverse the constructor looking for variables that are flavor
    8193              :          parameter.  Parameters must be expanded since they are fully used at
    8194              :          compile time.  */
    8195        12115 :       con = gfc_constructor_first (e->value.constructor);
    8196        32058 :       for (; con; con = gfc_constructor_next (con))
    8197              :         {
    8198        14038 :           if (con->expr->expr_type == EXPR_VARIABLE
    8199         5425 :               && con->expr->symtree
    8200         5425 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8201         5343 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8202              :             return true;
    8203         8613 :           if (con->expr->expr_type == EXPR_ARRAY
    8204         8613 :               && gfc_is_expandable_expr (con->expr))
    8205              :             return true;
    8206              :         }
    8207              :     }
    8208              : 
    8209              :   return false;
    8210              : }
    8211              : 
    8212              : 
    8213              : /* Sometimes variables in specification expressions of the result
    8214              :    of module procedures in submodules wind up not being the 'real'
    8215              :    dummy.  Find this, if possible, in the namespace of the first
    8216              :    formal argument.  */
    8217              : 
    8218              : static void
    8219         4845 : fixup_unique_dummy (gfc_expr *e)
    8220              : {
    8221         4845 :   gfc_symtree *st = NULL;
    8222         4845 :   gfc_symbol *s = NULL;
    8223              : 
    8224         4845 :   if (e->symtree->n.sym->ns->proc_name
    8225         4815 :       && e->symtree->n.sym->ns->proc_name->formal)
    8226         4815 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8227              : 
    8228         4815 :   if (s != NULL)
    8229         4815 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8230              : 
    8231         4845 :   if (st != NULL
    8232           14 :       && st->n.sym != NULL
    8233           14 :       && st->n.sym->attr.dummy)
    8234           14 :     e->symtree = st;
    8235         4845 : }
    8236              : 
    8237              : 
    8238              : /* Resolve an expression.  That is, make sure that types of operands agree
    8239              :    with their operators, intrinsic operators are converted to function calls
    8240              :    for overloaded types and unresolved function references are resolved.  */
    8241              : 
    8242              : bool
    8243      7018751 : gfc_resolve_expr (gfc_expr *e)
    8244              : {
    8245      7018751 :   bool t;
    8246      7018751 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8247              : 
    8248      7018751 :   if (e == NULL || e->do_not_resolve_again)
    8249              :     return true;
    8250              : 
    8251              :   /* inquiry_argument only applies to variables.  */
    8252      5083855 :   inquiry_save = inquiry_argument;
    8253      5083855 :   actual_arg_save = actual_arg;
    8254      5083855 :   first_actual_arg_save = first_actual_arg;
    8255              : 
    8256      5083855 :   if (e->expr_type != EXPR_VARIABLE)
    8257              :     {
    8258      3747574 :       inquiry_argument = false;
    8259      3747574 :       actual_arg = false;
    8260      3747574 :       first_actual_arg = false;
    8261              :     }
    8262      1336281 :   else if (e->symtree != NULL
    8263      1335836 :            && *e->symtree->name == '@'
    8264         5575 :            && e->symtree->n.sym->attr.dummy)
    8265              :     {
    8266              :       /* Deal with submodule specification expressions that are not
    8267              :          found to be referenced in module.cc(read_cleanup).  */
    8268         4845 :       fixup_unique_dummy (e);
    8269              :     }
    8270              : 
    8271      5083855 :   switch (e->expr_type)
    8272              :     {
    8273       534529 :     case EXPR_OP:
    8274       534529 :       t = resolve_operator (e);
    8275       534529 :       break;
    8276              : 
    8277          170 :     case EXPR_CONDITIONAL:
    8278          170 :       t = resolve_conditional (e);
    8279          170 :       break;
    8280              : 
    8281      1682907 :     case EXPR_FUNCTION:
    8282      1682907 :     case EXPR_VARIABLE:
    8283              : 
    8284      1682907 :       if (check_host_association (e))
    8285       346662 :         t = resolve_function (e);
    8286              :       else
    8287      1336245 :         t = resolve_variable (e);
    8288              : 
    8289      1682907 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    8290         7368 :           && e->ref->type != REF_SUBSTRING)
    8291         2162 :         gfc_resolve_substring_charlen (e);
    8292              : 
    8293              :       break;
    8294              : 
    8295         1669 :     case EXPR_COMPCALL:
    8296         1669 :       t = resolve_typebound_function (e);
    8297         1669 :       break;
    8298              : 
    8299          508 :     case EXPR_SUBSTRING:
    8300          508 :       t = gfc_resolve_ref (e);
    8301          508 :       break;
    8302              : 
    8303              :     case EXPR_CONSTANT:
    8304              :     case EXPR_NULL:
    8305              :       t = true;
    8306              :       break;
    8307              : 
    8308          464 :     case EXPR_PPC:
    8309          464 :       t = resolve_expr_ppc (e);
    8310          464 :       break;
    8311              : 
    8312        73008 :     case EXPR_ARRAY:
    8313        73008 :       t = false;
    8314        73008 :       if (!gfc_resolve_ref (e))
    8315              :         break;
    8316              : 
    8317        73008 :       t = gfc_resolve_array_constructor (e);
    8318              :       /* Also try to expand a constructor.  */
    8319        73008 :       if (t)
    8320              :         {
    8321        72906 :           gfc_expression_rank (e);
    8322        72906 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8323        68247 :             gfc_expand_constructor (e, false);
    8324              :         }
    8325              : 
    8326              :       /* This provides the opportunity for the length of constructors with
    8327              :          character valued function elements to propagate the string length
    8328              :          to the expression.  */
    8329        72906 :       if (t && e->ts.type == BT_CHARACTER)
    8330              :         {
    8331              :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    8332              :              here rather then add a duplicate test for it above.  */
    8333        10804 :           gfc_expand_constructor (e, false);
    8334        10804 :           t = gfc_resolve_character_array_constructor (e);
    8335              :         }
    8336              : 
    8337              :       break;
    8338              : 
    8339        16583 :     case EXPR_STRUCTURE:
    8340        16583 :       t = gfc_resolve_ref (e);
    8341        16583 :       if (!t)
    8342              :         break;
    8343              : 
    8344        16583 :       t = resolve_structure_cons (e, 0);
    8345        16583 :       if (!t)
    8346              :         break;
    8347              : 
    8348        16571 :       t = gfc_simplify_expr (e, 0);
    8349        16571 :       break;
    8350              : 
    8351            0 :     default:
    8352            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8353              :     }
    8354              : 
    8355      5083855 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8356       184682 :     fixup_charlen (e);
    8357              : 
    8358      5083855 :   inquiry_argument = inquiry_save;
    8359      5083855 :   actual_arg = actual_arg_save;
    8360      5083855 :   first_actual_arg = first_actual_arg_save;
    8361              : 
    8362              :   /* For some reason, resolving these expressions a second time mangles
    8363              :      the typespec of the expression itself.  */
    8364      5083855 :   if (t && e->expr_type == EXPR_VARIABLE
    8365      1333399 :       && e->symtree->n.sym->attr.select_rank_temporary
    8366         3452 :       && UNLIMITED_POLY (e->symtree->n.sym))
    8367           83 :     e->do_not_resolve_again = 1;
    8368              : 
    8369      5081323 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8370         7354 :     t = check_import_status (e);
    8371              : 
    8372              :   return t;
    8373              : }
    8374              : 
    8375              : 
    8376              : /* Resolve an expression from an iterator.  They must be scalar and have
    8377              :    INTEGER or (optionally) REAL type.  */
    8378              : 
    8379              : static bool
    8380       153957 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8381              :                            const char *name_msgid)
    8382              : {
    8383       153957 :   if (!gfc_resolve_expr (expr))
    8384              :     return false;
    8385              : 
    8386       153952 :   if (expr->rank != 0)
    8387              :     {
    8388            0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    8389            0 :       return false;
    8390              :     }
    8391              : 
    8392       153952 :   if (expr->ts.type != BT_INTEGER)
    8393              :     {
    8394          274 :       if (expr->ts.type == BT_REAL)
    8395              :         {
    8396          274 :           if (real_ok)
    8397          271 :             return gfc_notify_std (GFC_STD_F95_DEL,
    8398              :                                    "%s at %L must be integer",
    8399          271 :                                    _(name_msgid), &expr->where);
    8400              :           else
    8401              :             {
    8402            3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    8403              :                          &expr->where);
    8404            3 :               return false;
    8405              :             }
    8406              :         }
    8407              :       else
    8408              :         {
    8409            0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    8410            0 :           return false;
    8411              :         }
    8412              :     }
    8413              :   return true;
    8414              : }
    8415              : 
    8416              : 
    8417              : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    8418              :    false allow only INTEGER type iterators, otherwise allow REAL types.
    8419              :    Set own_scope to true for ac-implied-do and data-implied-do as those
    8420              :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    8421              : 
    8422              : bool
    8423        38498 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8424              : {
    8425        38498 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8426              :     return false;
    8427              : 
    8428        38494 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8429        38494 :                                  _("iterator variable")))
    8430              :     return false;
    8431              : 
    8432        38488 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8433              :                                   "Start expression in DO loop"))
    8434              :     return false;
    8435              : 
    8436        38487 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8437              :                                   "End expression in DO loop"))
    8438              :     return false;
    8439              : 
    8440        38484 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8441              :                                   "Step expression in DO loop"))
    8442              :     return false;
    8443              : 
    8444              :   /* Convert start, end, and step to the same type as var.  */
    8445        38483 :   if (iter->start->ts.kind != iter->var->ts.kind
    8446        38155 :       || iter->start->ts.type != iter->var->ts.type)
    8447          363 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8448              : 
    8449        38483 :   if (iter->end->ts.kind != iter->var->ts.kind
    8450        38182 :       || iter->end->ts.type != iter->var->ts.type)
    8451          326 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8452              : 
    8453        38483 :   if (iter->step->ts.kind != iter->var->ts.kind
    8454        38191 :       || iter->step->ts.type != iter->var->ts.type)
    8455          328 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8456              : 
    8457        38483 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8458              :     {
    8459        37360 :       if ((iter->step->ts.type == BT_INTEGER
    8460        37277 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8461        74635 :           || (iter->step->ts.type == BT_REAL
    8462           83 :               && mpfr_sgn (iter->step->value.real) == 0))
    8463              :         {
    8464            3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8465            3 :                      &iter->step->where);
    8466            3 :           return false;
    8467              :         }
    8468              :     }
    8469              : 
    8470        38480 :   if (iter->start->expr_type == EXPR_CONSTANT
    8471        35343 :       && iter->end->expr_type == EXPR_CONSTANT
    8472        27640 :       && iter->step->expr_type == EXPR_CONSTANT)
    8473              :     {
    8474        27373 :       int sgn, cmp;
    8475        27373 :       if (iter->start->ts.type == BT_INTEGER)
    8476              :         {
    8477        27319 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8478        27319 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8479              :         }
    8480              :       else
    8481              :         {
    8482           54 :           sgn = mpfr_sgn (iter->step->value.real);
    8483           54 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8484              :         }
    8485        27373 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8486          146 :         gfc_warning (OPT_Wzerotrip,
    8487              :                      "DO loop at %L will be executed zero times",
    8488          146 :                      &iter->step->where);
    8489              :     }
    8490              : 
    8491        38480 :   if (iter->end->expr_type == EXPR_CONSTANT
    8492        28008 :       && iter->end->ts.type == BT_INTEGER
    8493        27954 :       && iter->step->expr_type == EXPR_CONSTANT
    8494        27644 :       && iter->step->ts.type == BT_INTEGER
    8495        27644 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8496        27273 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8497              :     {
    8498        26487 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8499        26487 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8500              : 
    8501        26487 :       if (is_step_positive
    8502        26116 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8503            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8504              :                      "DO loop at %L is undefined as it overflows",
    8505            7 :                      &iter->step->where);
    8506              :       else if (!is_step_positive
    8507          371 :                && mpz_cmp (iter->end->value.integer,
    8508          371 :                            gfc_integer_kinds[k].min_int) == 0)
    8509            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8510              :                      "DO loop at %L is undefined as it underflows",
    8511            7 :                      &iter->step->where);
    8512              :     }
    8513              : 
    8514        38480 :   gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8515              :                           VALUE_USED);
    8516        38480 :   gfc_value_used_expr (iter->start, VALUE_USED);
    8517        38480 :   gfc_value_used_expr (iter->end, VALUE_USED);
    8518        38480 :   gfc_value_used_expr (iter->step, VALUE_USED);
    8519              : 
    8520        38480 :   return true;
    8521              : }
    8522              : 
    8523              : 
    8524              : /* Traversal function for find_forall_index.  f == 2 signals that
    8525              :    that variable itself is not to be checked - only the references.  */
    8526              : 
    8527              : static bool
    8528        42682 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8529              : {
    8530        42682 :   if (expr->expr_type != EXPR_VARIABLE)
    8531              :     return false;
    8532              : 
    8533              :   /* A scalar assignment  */
    8534        18195 :   if (!expr->ref || *f == 1)
    8535              :     {
    8536        12133 :       if (expr->symtree->n.sym == sym)
    8537              :         return true;
    8538              :       else
    8539              :         return false;
    8540              :     }
    8541              : 
    8542         6062 :   if (*f == 2)
    8543         1731 :     *f = 1;
    8544              :   return false;
    8545              : }
    8546              : 
    8547              : 
    8548              : /* Check whether the FORALL index appears in the expression or not.
    8549              :    Returns true if SYM is found in EXPR.  */
    8550              : 
    8551              : bool
    8552        27060 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8553              : {
    8554        27060 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8555              :     return true;
    8556              :   else
    8557              :     return false;
    8558              : }
    8559              : 
    8560              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8561              :    This constraint specifies rules for variables in locality-specs.  */
    8562              : 
    8563              : static int
    8564          765 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8565              : {
    8566          765 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8567              : 
    8568          765 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8569              :     {
    8570           22 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8571           22 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8572           24 :            list; list = list->next)
    8573              :         {
    8574            5 :           if (list->expr->symtree->n.sym == sym)
    8575              :             {
    8576            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8577              :                          "must not appear in LOCAL locality-spec at %L",
    8578              :                          sym->name, &(*expr)->where, &list->expr->where);
    8579            3 :               *walk_subtrees = 0;
    8580            3 :               return 1;
    8581              :             }
    8582              :         }
    8583              :     }
    8584              : 
    8585          762 :     *walk_subtrees = 1;
    8586          762 :     return 0;
    8587              : }
    8588              : 
    8589              : static int
    8590         4058 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8591              : {
    8592         4058 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8593              : 
    8594         4058 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8595              :     {
    8596         1866 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8597              : 
    8598         1866 :       if (d->sym_hash->contains (sym))
    8599         1275 :         sym->mark = 1;
    8600              : 
    8601          591 :       else if (d->default_none)
    8602              :         {
    8603            8 :           gfc_namespace *ns2 = d->ns;
    8604           13 :           while (ns2)
    8605              :             {
    8606            8 :               if (ns2 == sym->ns)
    8607              :                 break;
    8608            5 :               ns2 = ns2->parent;
    8609              :             }
    8610              : 
    8611              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.
    8612              :              Use d->code (the DO CONCURRENT node) rather than sym->ns->code,
    8613              :              which may be a different code type (e.g. EXEC_ASSOCIATE) whose
    8614              :              ext union would be read incorrectly.  */
    8615            8 :           for (gfc_forall_iterator *iter = d->code->ext.concur.forall_iterator;
    8616           17 :                iter; iter = iter->next)
    8617              :             {
    8618           10 :               if (!iter->var || !iter->var->symtree)
    8619            0 :                 continue;
    8620           10 :               const char *iter_name = iter->var->symtree->name;
    8621              :               /* Shadow iterators (from inline type-spec: integer :: i = ...)
    8622              :                  store the iterator with a leading underscore internally; the
    8623              :                  user-visible name does not have the underscore.  */
    8624           10 :               if (iter->shadow)
    8625            0 :                 iter_name++;
    8626           10 :               if (strcmp (sym->name, iter_name) == 0)
    8627            1 :                 return 0;
    8628              :             }
    8629              : 
    8630              :           /* A named constant is not a variable, so skip test.  */
    8631            7 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8632              :             {
    8633            2 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8634              :                         "of DO CONCURRENT at %L but required due to "
    8635              :                         "DEFAULT (NONE)",
    8636              :                         sym->name, &(*e)->where, &d->code->loc);
    8637            2 :               d->sym_hash->add (sym);
    8638              :             }
    8639              :         }
    8640              :     }
    8641              :   return 0;
    8642              : }
    8643              : 
    8644              : static void
    8645          224 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8646              : {
    8647          224 :   struct check_default_none_data data;
    8648          224 :   data.code = code;
    8649          224 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8650          224 :   data.ns = ns;
    8651          224 :   data.default_none = code->ext.concur.default_none;
    8652              : 
    8653         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8654              :     {
    8655          896 :       const char *name;
    8656          896 :       switch (locality)
    8657              :         {
    8658              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8659          224 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8660          224 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8661          224 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8662              :           default: gcc_unreachable ();
    8663              :         }
    8664              : 
    8665         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8666          391 :            list = list->next)
    8667              :         {
    8668          391 :           gfc_expr *expr = list->expr;
    8669              : 
    8670          391 :           if (locality == LOCALITY_REDUCE
    8671           72 :               && (expr->expr_type == EXPR_FUNCTION
    8672           48 :                   || expr->expr_type == EXPR_OP))
    8673           35 :             continue;
    8674              : 
    8675          367 :           if (!gfc_resolve_expr (expr))
    8676            3 :             continue;
    8677              : 
    8678          364 :           if (expr->expr_type != EXPR_VARIABLE
    8679          364 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8680          364 :               || (expr->ref
    8681          151 :                   && (expr->ref->type != REF_ARRAY
    8682          151 :                       || expr->ref->u.ar.type != AR_FULL
    8683          147 :                       || expr->ref->next)))
    8684              :             {
    8685            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8686              :                          name, &expr->where);
    8687            4 :                 continue;
    8688              :             }
    8689              : 
    8690          360 :           gfc_symbol *sym = expr->symtree->n.sym;
    8691              : 
    8692          360 :           if (data.sym_hash->contains (sym))
    8693              :             {
    8694            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8695              :                          "locality-spec", sym->name, &expr->where);
    8696            4 :               continue;
    8697              :             }
    8698              : 
    8699          356 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8700          716 :                iter; iter = iter->next)
    8701              :             {
    8702          360 :               if (iter->var->symtree->n.sym == sym)
    8703              :                 {
    8704            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8705              :                              "locality-spec", sym->name, &expr->where);
    8706            1 :                   continue;
    8707              :                 }
    8708              : 
    8709          359 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8710              :             }
    8711              : 
    8712          356 :           if (locality == LOCALITY_LOCAL
    8713          356 :               || locality == LOCALITY_LOCAL_INIT
    8714          356 :               || locality == LOCALITY_REDUCE)
    8715              :             {
    8716          198 :               if (sym->attr.optional)
    8717            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8718              :                            "locality-spec at %L",
    8719              :                            sym->name, name, &expr->where);
    8720              : 
    8721          198 :               if (sym->attr.dimension
    8722           66 :                   && sym->as
    8723           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8724            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8725              :                            "locality-spec at %L",
    8726              :                            sym->name, name, &expr->where);
    8727              : 
    8728          198 :               gfc_check_vardef_context (expr, false, false, false, name);
    8729              :             }
    8730              : 
    8731          198 :           if (locality == LOCALITY_LOCAL
    8732              :               || locality == LOCALITY_LOCAL_INIT)
    8733              :             {
    8734          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    8735              : 
    8736          181 :               if (attr.allocatable)
    8737            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8738              :                            "locality-spec at %L",
    8739              :                            sym->name, name, &expr->where);
    8740              : 
    8741          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8742            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8743              :                            " for %qs in %s locality-spec at %L",
    8744              :                            sym->name, name, &expr->where);
    8745              : 
    8746          177 :               else if (attr.codimension)
    8747            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8748              :                            "at %L",
    8749              :                            sym->name, name, &expr->where);
    8750              : 
    8751          177 :               else if (expr->ts.type == BT_DERIVED
    8752          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8753            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8754              :                            "locality-spec at %L",
    8755              :                            sym->name, name, &expr->where);
    8756              : 
    8757          177 :               else if (gfc_has_ultimate_allocatable (expr))
    8758            4 :                 gfc_error ("Type with ultimate allocatable component not "
    8759              :                            "permitted for %qs in %s locality-spec at %L",
    8760              :                            sym->name, name, &expr->where);
    8761              :             }
    8762              : 
    8763          175 :           else if (locality == LOCALITY_REDUCE)
    8764              :             {
    8765           17 :               if (sym->attr.asynchronous)
    8766            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8767              :                            "REDUCE locality-spec at %L",
    8768              :                            sym->name, &expr->where);
    8769           17 :               if (sym->attr.volatile_)
    8770            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8771              :                            "locality-spec at %L", sym->name, &expr->where);
    8772              :             }
    8773              : 
    8774          356 :           data.sym_hash->add (sym);
    8775              :         }
    8776              : 
    8777          896 :       if (locality == LOCALITY_LOCAL)
    8778              :         {
    8779          224 :           gcc_assert (locality == 0);
    8780              : 
    8781          224 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8782          467 :                iter; iter = iter->next)
    8783              :             {
    8784          243 :               gfc_expr_walker (&iter->start,
    8785              :                                do_concur_locality_specs_f2023,
    8786              :                                &data);
    8787              : 
    8788          243 :               gfc_expr_walker (&iter->end,
    8789              :                                do_concur_locality_specs_f2023,
    8790              :                                &data);
    8791              : 
    8792          243 :               gfc_expr_walker (&iter->stride,
    8793              :                                do_concur_locality_specs_f2023,
    8794              :                                &data);
    8795              :             }
    8796              : 
    8797          224 :           if (code->expr1)
    8798            7 :             gfc_expr_walker (&code->expr1,
    8799              :                              do_concur_locality_specs_f2023,
    8800              :                              &data);
    8801              :         }
    8802              :     }
    8803              : 
    8804          224 :   gfc_expr *reduce_op = NULL;
    8805              : 
    8806          224 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8807          272 :        list; list = list->next)
    8808              :     {
    8809           48 :       gfc_expr *expr = list->expr;
    8810              : 
    8811           48 :       if (expr->expr_type != EXPR_VARIABLE)
    8812              :         {
    8813           24 :           reduce_op = expr;
    8814           24 :           continue;
    8815              :         }
    8816              : 
    8817           24 :       if (reduce_op->expr_type == EXPR_OP)
    8818              :         {
    8819           17 :           switch (reduce_op->value.op.op)
    8820              :             {
    8821           17 :               case INTRINSIC_PLUS:
    8822           17 :               case INTRINSIC_TIMES:
    8823           17 :                 if (!gfc_numeric_ts (&expr->ts))
    8824            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8825            3 :                              "got %s", expr->symtree->n.sym->name,
    8826              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8827              :                 break;
    8828            0 :               case INTRINSIC_AND:
    8829            0 :               case INTRINSIC_OR:
    8830            0 :               case INTRINSIC_EQV:
    8831            0 :               case INTRINSIC_NEQV:
    8832            0 :                 if (expr->ts.type != BT_LOGICAL)
    8833            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8834            0 :                              "got %qs", expr->symtree->n.sym->name,
    8835              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8836              :                 break;
    8837            0 :               default:
    8838            0 :                 gcc_unreachable ();
    8839              :             }
    8840              :         }
    8841              : 
    8842            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8843              :         {
    8844            7 :           switch (reduce_op->value.function.isym->id)
    8845              :             {
    8846            6 :               case GFC_ISYM_MIN:
    8847            6 :               case GFC_ISYM_MAX:
    8848            6 :                 if (expr->ts.type != BT_INTEGER
    8849              :                     && expr->ts.type != BT_REAL
    8850              :                     && expr->ts.type != BT_CHARACTER)
    8851            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8852              :                              "in REDUCE with MIN/MAX at %L, got %s",
    8853            2 :                              expr->symtree->n.sym->name, &expr->where,
    8854              :                              gfc_basic_typename (expr->ts.type));
    8855              :                 break;
    8856            1 :               case GFC_ISYM_IAND:
    8857            1 :               case GFC_ISYM_IOR:
    8858            1 :               case GFC_ISYM_IEOR:
    8859            1 :                 if (expr->ts.type != BT_INTEGER)
    8860            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8861              :                              "IAND/IOR/IEOR at %L, got %s",
    8862            1 :                              expr->symtree->n.sym->name, &expr->where,
    8863              :                              gfc_basic_typename (expr->ts.type));
    8864              :                 break;
    8865            0 :               default:
    8866            0 :                 gcc_unreachable ();
    8867              :             }
    8868              :         }
    8869              : 
    8870              :       else
    8871            0 :         gcc_unreachable ();
    8872              :     }
    8873              : 
    8874         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8875              :     {
    8876         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8877          391 :            list = list->next)
    8878              :         {
    8879          391 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8880          367 :             list->expr->symtree->n.sym->mark = 0;
    8881              :         }
    8882              :     }
    8883              : 
    8884          224 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8885              :                    check_default_none_expr, &data);
    8886              : 
    8887         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8888              :     {
    8889          896 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8890         1287 :       while (*plist)
    8891              :         {
    8892          391 :           gfc_expr *expr = (*plist)->expr;
    8893          391 :           if (expr->expr_type == EXPR_VARIABLE)
    8894              :             {
    8895          367 :               gfc_symbol *sym = expr->symtree->n.sym;
    8896          367 :               if (sym->mark == 0)
    8897              :                 {
    8898           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8899              :                                "locality-spec at %L is not used",
    8900              :                                sym->name, &expr->where);
    8901           70 :                   gfc_expr_list *tmp = *plist;
    8902           70 :                   *plist = (*plist)->next;
    8903           70 :                   gfc_free_expr (tmp->expr);
    8904           70 :                   free (tmp);
    8905           70 :                   continue;
    8906           70 :                 }
    8907              :             }
    8908          321 :           plist = &((*plist)->next);
    8909              :         }
    8910              :     }
    8911              : 
    8912          448 :   delete data.sym_hash;
    8913          224 : }
    8914              : 
    8915              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8916              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8917              :    INTEGERs, and if stride is a constant it must be nonzero.
    8918              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8919              :    not contain a reference to any index-name in the
    8920              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8921              : 
    8922              : static void
    8923         2217 : resolve_forall_iterators (gfc_forall_iterator *it)
    8924              : {
    8925         2217 :   gfc_forall_iterator *iter, *iter2;
    8926              : 
    8927         6352 :   for (iter = it; iter; iter = iter->next)
    8928              :     {
    8929         4135 :       if (gfc_resolve_expr (iter->var)
    8930         4135 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8931            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8932              :                    &iter->var->where);
    8933              : 
    8934         4135 :       if (gfc_resolve_expr (iter->start)
    8935         4135 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8936            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8937              :                    &iter->start->where);
    8938         4135 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8939            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8940              : 
    8941         4135 :       if (gfc_resolve_expr (iter->end)
    8942         4135 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8943            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8944              :                    &iter->end->where);
    8945         4135 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8946            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8947              : 
    8948         4135 :       if (gfc_resolve_expr (iter->stride))
    8949              :         {
    8950         4135 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8951            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8952              :                        &iter->stride->where, "INTEGER");
    8953              : 
    8954         4135 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8955         4131 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8956            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8957              :                        &iter->stride->where);
    8958              :         }
    8959         4135 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8960            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8961              : 
    8962         4135 :       gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8963              :                               VALUE_USED);
    8964         4135 :       gfc_value_used_expr (iter->start, VALUE_USED);
    8965         4135 :       gfc_value_used_expr (iter->end, VALUE_USED);
    8966         4135 :       gfc_value_used_expr (iter->stride, VALUE_USED);
    8967              :     }
    8968              : 
    8969         6352 :   for (iter = it; iter; iter = iter->next)
    8970        11114 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8971              :       {
    8972         6979 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8973         6977 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8974        13954 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8975            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8976            6 :                      "specification at %L", iter->var->symtree->name,
    8977            6 :                      &iter2->start->where);
    8978              :       }
    8979         2217 : }
    8980              : 
    8981              : 
    8982              : /* Given a pointer to a symbol that is a derived type, see if it's
    8983              :    inaccessible, i.e. if it's defined in another module and the components are
    8984              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8985              :    inaccessible components are found, nonzero otherwise.  */
    8986              : 
    8987              : static bool
    8988         1358 : derived_inaccessible (gfc_symbol *sym)
    8989              : {
    8990         1358 :   gfc_component *c;
    8991              : 
    8992         1358 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8993              :     return 1;
    8994              : 
    8995         4013 :   for (c = sym->components; c; c = c->next)
    8996              :     {
    8997              :         /* Prevent an infinite loop through this function.  */
    8998         2668 :         if (c->ts.type == BT_DERIVED
    8999          289 :             && (c->attr.pointer || c->attr.allocatable)
    9000           72 :             && sym == c->ts.u.derived)
    9001           72 :           continue;
    9002              : 
    9003         2596 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    9004              :           return 1;
    9005              :     }
    9006              : 
    9007              :   return 0;
    9008              : }
    9009              : 
    9010              : 
    9011              : /* Resolve the argument of a deallocate expression.  The expression must be
    9012              :    a pointer or a full array.  */
    9013              : 
    9014              : static bool
    9015         8399 : resolve_deallocate_expr (gfc_expr *e)
    9016              : {
    9017         8399 :   symbol_attribute attr;
    9018         8399 :   int allocatable, pointer;
    9019         8399 :   gfc_ref *ref;
    9020         8399 :   gfc_symbol *sym;
    9021         8399 :   gfc_component *c;
    9022         8399 :   bool unlimited;
    9023              : 
    9024         8399 :   if (!gfc_resolve_expr (e))
    9025              :     return false;
    9026              : 
    9027         8399 :   if (e->expr_type != EXPR_VARIABLE)
    9028            0 :     goto bad;
    9029              : 
    9030         8399 :   sym = e->symtree->n.sym;
    9031         8399 :   unlimited = UNLIMITED_POLY(sym);
    9032              : 
    9033         8399 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    9034              :     {
    9035         1574 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    9036         1574 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    9037              :     }
    9038              :   else
    9039              :     {
    9040         6825 :       allocatable = sym->attr.allocatable;
    9041         6825 :       pointer = sym->attr.pointer;
    9042              :     }
    9043        16869 :   for (ref = e->ref; ref; ref = ref->next)
    9044              :     {
    9045         8470 :       switch (ref->type)
    9046              :         {
    9047         6320 :         case REF_ARRAY:
    9048         6320 :           if (ref->u.ar.type != AR_FULL
    9049         6528 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9050          208 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9051              :             allocatable = 0;
    9052              :           break;
    9053              : 
    9054         2150 :         case REF_COMPONENT:
    9055         2150 :           c = ref->u.c.component;
    9056         2150 :           if (c->ts.type == BT_CLASS)
    9057              :             {
    9058          303 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9059          303 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9060              :             }
    9061              :           else
    9062              :             {
    9063         1847 :               allocatable = c->attr.allocatable;
    9064         1847 :               pointer = c->attr.pointer;
    9065              :             }
    9066              :           break;
    9067              : 
    9068              :         case REF_SUBSTRING:
    9069              :         case REF_INQUIRY:
    9070          519 :           allocatable = 0;
    9071              :           break;
    9072              :         }
    9073              :     }
    9074              : 
    9075         8399 :   attr = gfc_expr_attr (e);
    9076              : 
    9077         8399 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9078              :     {
    9079            3 :     bad:
    9080            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9081              :                  &e->where);
    9082            3 :       return false;
    9083              :     }
    9084              : 
    9085              :   /* F2008, C644.  */
    9086         8396 :   if (gfc_is_coindexed (e))
    9087              :     {
    9088            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9089            1 :       return false;
    9090              :     }
    9091              : 
    9092         8395 :   if (pointer
    9093        10787 :       && !gfc_check_vardef_context (e, true, true, false,
    9094         2392 :                                     _("DEALLOCATE object")))
    9095              :     return false;
    9096         8393 :   if (!gfc_check_vardef_context (e, false, true, false,
    9097         8393 :                                  _("DEALLOCATE object")))
    9098              :     return false;
    9099              : 
    9100              :   return true;
    9101              : }
    9102              : 
    9103              : 
    9104              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9105              : static bool
    9106        47385 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9107              : {
    9108        47385 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9109         2081 :     return true;
    9110              : 
    9111              :   return false;
    9112              : }
    9113              : 
    9114              : bool
    9115        20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9116              : {
    9117        20080 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9118              : }
    9119              : 
    9120              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9121              :    of character expressions.  */
    9122              : static bool
    9123        20481 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9124              : {
    9125            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9126              : }
    9127              : 
    9128              : 
    9129              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9130              :    allocated, get the expression node to be initialized afterwards (needed for
    9131              :    derived types with default initializers, and derived types with allocatable
    9132              :    components that need nullification.)  */
    9133              : 
    9134              : gfc_expr *
    9135         5817 : gfc_expr_to_initialize (gfc_expr *e)
    9136              : {
    9137         5817 :   gfc_expr *result;
    9138         5817 :   gfc_ref *ref;
    9139         5817 :   int i;
    9140              : 
    9141         5817 :   result = gfc_copy_expr (e);
    9142              : 
    9143              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9144        11486 :   for (ref = result->ref; ref; ref = ref->next)
    9145         9038 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9146              :       {
    9147         3369 :         if (ref->u.ar.dimen == 0
    9148           77 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9149              :           return result;
    9150              : 
    9151         3292 :         ref->u.ar.type = AR_FULL;
    9152              : 
    9153         7436 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9154         4144 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9155              : 
    9156              :         break;
    9157              :       }
    9158              : 
    9159         5740 :   gfc_free_shape (&result->shape, result->rank);
    9160              : 
    9161              :   /* Recalculate rank, shape, etc.  */
    9162         5740 :   gfc_resolve_expr (result);
    9163         5740 :   return result;
    9164              : }
    9165              : 
    9166              : 
    9167              : /* If the last ref of an expression is an array ref, return a copy of the
    9168              :    expression with that one removed.  Otherwise, a copy of the original
    9169              :    expression.  This is used for allocate-expressions and pointer assignment
    9170              :    LHS, where there may be an array specification that needs to be stripped
    9171              :    off when using gfc_check_vardef_context.  */
    9172              : 
    9173              : static gfc_expr*
    9174        27865 : remove_last_array_ref (gfc_expr* e)
    9175              : {
    9176        27865 :   gfc_expr* e2;
    9177        27865 :   gfc_ref** r;
    9178              : 
    9179        27865 :   e2 = gfc_copy_expr (e);
    9180        36088 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9181        24692 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9182              :       {
    9183        16469 :         gfc_free_ref_list (*r);
    9184        16469 :         *r = NULL;
    9185        16469 :         break;
    9186              :       }
    9187              : 
    9188        27865 :   return e2;
    9189              : }
    9190              : 
    9191              : 
    9192              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9193              :    a source-expr are conformable.  This does not catch all possible
    9194              :    cases; in particular a runtime checking is needed.  */
    9195              : 
    9196              : static bool
    9197         1910 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9198              : {
    9199         1910 :   gfc_ref *tail;
    9200         1910 :   bool scalar;
    9201              : 
    9202         2642 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9203              : 
    9204              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9205              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9206              :      so let's emit a warning if -Wsurprising is given.  */
    9207         1910 :   scalar = !tail || tail->type == REF_COMPONENT;
    9208         1910 :   if (e1->mold && e1->rank > 0
    9209          166 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9210              :     {
    9211           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9212           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9213              :                      "but MOLD= expression at %L has rank %d",
    9214            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9215              :                      &e1->where, e1->rank);
    9216           30 :       return true;
    9217              :     }
    9218              : 
    9219              :   /* First compare rank.  */
    9220         1880 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9221            2 :       || (!tail && e1->rank != e2->rank))
    9222              :     {
    9223            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9224              :                  "same rank as the allocate-object at %L",
    9225              :                  &e1->where, &e2->where);
    9226            7 :       return false;
    9227              :     }
    9228              : 
    9229         1873 :   if (e1->shape)
    9230              :     {
    9231         1373 :       int i;
    9232         1373 :       mpz_t s;
    9233              : 
    9234         1373 :       mpz_init (s);
    9235              : 
    9236         3165 :       for (i = 0; i < e1->rank; i++)
    9237              :         {
    9238         1379 :           if (tail->u.ar.start[i] == NULL)
    9239              :             break;
    9240              : 
    9241          419 :           if (tail->u.ar.end[i])
    9242              :             {
    9243           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9244           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9245           54 :               mpz_add_ui (s, s, 1);
    9246              :             }
    9247              :           else
    9248              :             {
    9249          365 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9250              :             }
    9251              : 
    9252          419 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9253              :             {
    9254            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9255              :                          "have the same shape", &e1->where, &e2->where);
    9256            0 :               mpz_clear (s);
    9257            0 :               return false;
    9258              :             }
    9259              :         }
    9260              : 
    9261         1373 :       mpz_clear (s);
    9262              :     }
    9263              : 
    9264              :   return true;
    9265              : }
    9266              : 
    9267              : 
    9268              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9269              :    checks to see whether the expression is OK or not.  The expression must
    9270              :    have a trailing array reference that gives the size of the array.  */
    9271              : 
    9272              : static bool
    9273        17459 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9274              : {
    9275        17459 :   int i, pointer, allocatable, dimension, is_abstract;
    9276        17459 :   int codimension;
    9277        17459 :   bool coindexed;
    9278        17459 :   bool unlimited;
    9279        17459 :   symbol_attribute attr;
    9280        17459 :   gfc_ref *ref, *ref2;
    9281        17459 :   gfc_expr *e2;
    9282        17459 :   gfc_array_ref *ar;
    9283        17459 :   gfc_symbol *sym = NULL;
    9284        17459 :   gfc_alloc *a;
    9285        17459 :   gfc_component *c;
    9286        17459 :   bool t;
    9287              : 
    9288              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9289              :      checking of coarrays.  */
    9290        22323 :   for (ref = e->ref; ref; ref = ref->next)
    9291        18123 :     if (ref->next == NULL)
    9292              :       break;
    9293              : 
    9294        17459 :   if (ref && ref->type == REF_ARRAY)
    9295        12064 :     ref->u.ar.in_allocate = true;
    9296              : 
    9297        17459 :   if (!gfc_resolve_expr (e))
    9298            1 :     goto failure;
    9299              : 
    9300              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9301              :      pointer, the next-to-last reference must be a pointer.  */
    9302              : 
    9303        17458 :   ref2 = NULL;
    9304        17458 :   if (e->symtree)
    9305        17458 :     sym = e->symtree->n.sym;
    9306              : 
    9307              :   /* Check whether ultimate component is abstract and CLASS.  */
    9308        34916 :   is_abstract = 0;
    9309              : 
    9310              :   /* Is the allocate-object unlimited polymorphic?  */
    9311        17458 :   unlimited = UNLIMITED_POLY(e);
    9312              : 
    9313        17458 :   if (e->expr_type != EXPR_VARIABLE)
    9314              :     {
    9315            0 :       allocatable = 0;
    9316            0 :       attr = gfc_expr_attr (e);
    9317            0 :       pointer = attr.pointer;
    9318            0 :       dimension = attr.dimension;
    9319            0 :       codimension = attr.codimension;
    9320              :     }
    9321              :   else
    9322              :     {
    9323        17458 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9324              :         {
    9325         3432 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9326         3432 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9327         3432 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9328         3432 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9329         3432 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9330              :         }
    9331              :       else
    9332              :         {
    9333        14026 :           allocatable = sym->attr.allocatable;
    9334        14026 :           pointer = sym->attr.pointer;
    9335        14026 :           dimension = sym->attr.dimension;
    9336        14026 :           codimension = sym->attr.codimension;
    9337              :         }
    9338              : 
    9339        17458 :       coindexed = false;
    9340              : 
    9341        35575 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9342              :         {
    9343        18119 :           switch (ref->type)
    9344              :             {
    9345        13546 :               case REF_ARRAY:
    9346        13546 :                 if (ref->u.ar.codimen > 0)
    9347              :                   {
    9348          760 :                     int n;
    9349         1061 :                     for (n = ref->u.ar.dimen;
    9350         1061 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9351          801 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9352              :                         {
    9353              :                           coindexed = true;
    9354              :                           break;
    9355              :                         }
    9356              :                    }
    9357              : 
    9358        13546 :                 if (ref->next != NULL)
    9359         1484 :                   pointer = 0;
    9360              :                 break;
    9361              : 
    9362         4573 :               case REF_COMPONENT:
    9363              :                 /* F2008, C644.  */
    9364         4573 :                 if (coindexed)
    9365              :                   {
    9366            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9367              :                                &e->where);
    9368            2 :                     goto failure;
    9369              :                   }
    9370              : 
    9371         4571 :                 c = ref->u.c.component;
    9372         4571 :                 if (c->ts.type == BT_CLASS)
    9373              :                   {
    9374         1000 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9375         1000 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9376         1000 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9377         1000 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9378         1000 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9379              :                   }
    9380              :                 else
    9381              :                   {
    9382         3571 :                     allocatable = c->attr.allocatable;
    9383         3571 :                     pointer = c->attr.pointer;
    9384         3571 :                     dimension = c->attr.dimension;
    9385         3571 :                     codimension = c->attr.codimension;
    9386         3571 :                     is_abstract = c->attr.abstract;
    9387              :                   }
    9388              :                 break;
    9389              : 
    9390            0 :               case REF_SUBSTRING:
    9391            0 :               case REF_INQUIRY:
    9392            0 :                 allocatable = 0;
    9393            0 :                 pointer = 0;
    9394            0 :                 break;
    9395              :             }
    9396              :         }
    9397              :     }
    9398              : 
    9399              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9400              :      pointer or an allocatable variable.  */
    9401        17456 :   if (allocatable == 0 && pointer == 0)
    9402              :     {
    9403            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9404              :                  &e->where);
    9405            4 :       goto failure;
    9406              :     }
    9407              : 
    9408              :   /* Some checks for the SOURCE tag.  */
    9409        17452 :   if (code->expr3)
    9410              :     {
    9411              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9412              :          rank as allocate-object".  This would require the MOLD argument to
    9413              :          NULL() as source-expr for subsequent checking.  However, even the
    9414              :          resulting disassociated pointer or unallocated array has no shape that
    9415              :          could be used for SOURCE= or MOLD=.  */
    9416         3863 :       if (code->expr3->expr_type == EXPR_NULL)
    9417              :         {
    9418            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9419              :                      &code->expr3->where);
    9420            4 :           goto failure;
    9421              :         }
    9422              : 
    9423              :       /* Check F03:C631.  */
    9424         3859 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9425              :         {
    9426           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9427           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9428           10 :           goto failure;
    9429              :         }
    9430              : 
    9431              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9432         3849 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9433            7 :         goto failure;
    9434              : 
    9435              :       /* Check F03:C633.  */
    9436         3842 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9437              :         {
    9438            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9439              :                      "shall have the same kind type parameter",
    9440              :                      &e->where, &code->expr3->where);
    9441            1 :           goto failure;
    9442              :         }
    9443              : 
    9444              :       /* Check F2008, C642.  */
    9445         3841 :       if (code->expr3->ts.type == BT_DERIVED
    9446         3841 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9447         1198 :               || (code->expr3->ts.u.derived->from_intmod
    9448              :                      == INTMOD_ISO_FORTRAN_ENV
    9449            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9450              :                      == ISOFORTRAN_LOCK_TYPE)))
    9451              :         {
    9452            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9453              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9454              :                       "allocate-object at %L is a coarray",
    9455            0 :                       &code->expr3->where, &e->where);
    9456            0 :           goto failure;
    9457              :         }
    9458              : 
    9459              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9460              :          allocate-object and source-expr shall have the same values."  */
    9461         3841 :       if (e->ts.type == BT_CHARACTER
    9462          816 :           && !e->ts.deferred
    9463          162 :           && e->ts.u.cl->length
    9464          162 :           && code->expr3->ts.type == BT_CHARACTER
    9465         4003 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9466              :                                      "SOURCE= or MOLD= specifier"))
    9467           17 :             goto failure;
    9468              : 
    9469              :       /* Check TS18508, C702/C703.  */
    9470         3824 :       if (code->expr3->ts.type == BT_DERIVED
    9471         5022 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9472         1198 :               || (code->expr3->ts.u.derived->from_intmod
    9473              :                      == INTMOD_ISO_FORTRAN_ENV
    9474            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9475              :                      == ISOFORTRAN_EVENT_TYPE)))
    9476              :         {
    9477            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9478              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9479              :                       "allocate-object at %L is a coarray",
    9480            0 :                       &code->expr3->where, &e->where);
    9481            0 :           goto failure;
    9482              :         }
    9483              :     }
    9484              : 
    9485              :   /* Check F08:C629.  */
    9486        17413 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9487          153 :       && !code->expr3)
    9488              :     {
    9489            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9490            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9491              :                  "type-spec or source-expr", sym->name, &e->where);
    9492            2 :       goto failure;
    9493              :     }
    9494              : 
    9495              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9496              :      if and only if each allocate-object is a dummy argument for which the
    9497              :      corresponding type parameter is assumed.  */
    9498        17411 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9499          513 :       && code->ext.alloc.ts.u.cl->length != NULL
    9500          498 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9501           23 :       && e->ts.u.cl->length == NULL
    9502            2 :       && e->symtree->n.sym->attr.dummy)
    9503              :     {
    9504            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9505              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9506              :                  "dummy argument with assumed type parameter",
    9507              :                  sym->name, &e->where);
    9508            2 :       goto failure;
    9509              :     }
    9510              : 
    9511              :   /* Check F08:C632.  */
    9512        17409 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9513           60 :       && !UNLIMITED_POLY (e))
    9514              :     {
    9515           36 :       int cmp;
    9516              : 
    9517           36 :       if (!e->ts.u.cl->length)
    9518           15 :         goto failure;
    9519              : 
    9520           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9521           21 :                                   code->ext.alloc.ts.u.cl->length);
    9522           21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9523              :         {
    9524            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9525              :                      "character-length parameter as in the declaration",
    9526              :                      sym->name, &e->where);
    9527            2 :           goto failure;
    9528              :         }
    9529              :     }
    9530              : 
    9531              :   /* In the variable definition context checks, gfc_expr_attr is used
    9532              :      on the expression.  This is fooled by the array specification
    9533              :      present in e, thus we have to eliminate that one temporarily.  */
    9534        17392 :   e2 = remove_last_array_ref (e);
    9535        17392 :   t = true;
    9536        17392 :   if (t && pointer)
    9537         3921 :     t = gfc_check_vardef_context (e2, true, true, false,
    9538         3921 :                                   _("ALLOCATE object"));
    9539         3921 :   if (t)
    9540        17384 :     t = gfc_check_vardef_context (e2, false, true, false,
    9541        17384 :                                   _("ALLOCATE object"));
    9542        17392 :   gfc_free_expr (e2);
    9543        17392 :   if (!t)
    9544           11 :     goto failure;
    9545              : 
    9546        17381 :   code->ext.alloc.expr3_not_explicit = 0;
    9547        17381 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9548         1629 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9549              :     {
    9550              :       /* For class arrays, the initialization with SOURCE is done
    9551              :          using _copy and trans_call. It is convenient to exploit that
    9552              :          when the allocated type is different from the declared type but
    9553              :          no SOURCE exists by setting expr3.  */
    9554          305 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9555          305 :       code->ext.alloc.expr3_not_explicit = 1;
    9556              :     }
    9557        17076 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9558         2652 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9559            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9560              :     {
    9561              :       /* We have to zero initialize the integer variable.  */
    9562            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9563            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9564              :     }
    9565              : 
    9566        17381 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9567              :     {
    9568              :       /* Make sure the vtab symbol is present when
    9569              :          the module variables are generated.  */
    9570         3002 :       gfc_typespec ts = e->ts;
    9571         3002 :       if (code->expr3)
    9572         1337 :         ts = code->expr3->ts;
    9573         1665 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9574          732 :         ts = code->ext.alloc.ts;
    9575              : 
    9576              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9577              :          statement is necessary.  */
    9578         3002 :       gfc_find_derived_vtab (ts.u.derived);
    9579         3002 :     }
    9580        14379 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9581              :     {
    9582              :       /* Again, make sure the vtab symbol is present when
    9583              :          the module variables are generated.  */
    9584          440 :       gfc_typespec *ts = NULL;
    9585          440 :       if (code->expr3)
    9586          353 :         ts = &code->expr3->ts;
    9587              :       else
    9588           87 :         ts = &code->ext.alloc.ts;
    9589              : 
    9590          440 :       gcc_assert (ts);
    9591              : 
    9592              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9593              :          statement is necessary.  */
    9594          440 :       gfc_find_vtab (ts);
    9595              :     }
    9596              : 
    9597        17381 :   if (dimension == 0 && codimension == 0)
    9598         5348 :     goto success;
    9599              : 
    9600              :   /* Make sure the last reference node is an array specification.  */
    9601              : 
    9602        12033 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9603        10800 :       || (dimension && ref2->u.ar.dimen == 0))
    9604              :     {
    9605              :       /* F08:C633.  */
    9606         1233 :       if (code->expr3)
    9607              :         {
    9608         1232 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9609              :                                "in ALLOCATE statement at %L", &e->where))
    9610            0 :             goto failure;
    9611         1232 :           if (code->expr3->rank != 0)
    9612         1231 :             *array_alloc_wo_spec = true;
    9613              :           else
    9614              :             {
    9615            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9616              :                          "expression required in ALLOCATE statement at %L",
    9617              :                          &e->where);
    9618            1 :               goto failure;
    9619              :             }
    9620              :         }
    9621              :       else
    9622              :         {
    9623            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9624              :                      "at %L", &e->where);
    9625            1 :           goto failure;
    9626              :         }
    9627              :     }
    9628              : 
    9629              :   /* Make sure that the array section reference makes sense in the
    9630              :      context of an ALLOCATE specification.  */
    9631              : 
    9632        12031 :   ar = &ref2->u.ar;
    9633              : 
    9634        12031 :   if (codimension)
    9635         1179 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9636              :       {
    9637          692 :         switch (ar->dimen_type[i])
    9638              :           {
    9639            2 :           case DIMEN_THIS_IMAGE:
    9640            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9641              :                        "at %L", &e->where);
    9642            2 :             goto failure;
    9643              : 
    9644           98 :           case  DIMEN_RANGE:
    9645              :             /* F2018:R937:
    9646              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9647              :              */
    9648           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9649              :               {
    9650            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9651              :                            "at %L", &e->where);
    9652            8 :                 goto failure;
    9653              :               }
    9654           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9655              :               {
    9656            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9657            2 :                            &ar->start[i]->where);
    9658            2 :                 goto failure;
    9659              :               }
    9660              :             break;
    9661              : 
    9662          105 :           case DIMEN_ELEMENT:
    9663          105 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9664              :               {
    9665           97 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9666           97 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9667              :                   {
    9668            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9669              :                                "of 1 at %L", &ar->start[i]->where);
    9670            1 :                     goto failure;
    9671              :                   }
    9672              :               }
    9673              :             break;
    9674              : 
    9675              :           case DIMEN_STAR:
    9676              :             break;
    9677              : 
    9678            0 :           default:
    9679            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9680              :                        &e->where);
    9681            0 :             goto failure;
    9682              : 
    9683              :           }
    9684              :       }
    9685        29426 :   for (i = 0; i < ar->dimen; i++)
    9686              :     {
    9687        17412 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9688        14673 :         goto check_symbols;
    9689              : 
    9690         2739 :       switch (ar->dimen_type[i])
    9691              :         {
    9692              :         case DIMEN_ELEMENT:
    9693              :           break;
    9694              : 
    9695         2473 :         case DIMEN_RANGE:
    9696         2473 :           if (ar->start[i] != NULL
    9697         2473 :               && ar->end[i] != NULL
    9698         2472 :               && ar->stride[i] == NULL)
    9699              :             break;
    9700              : 
    9701              :           /* Fall through.  */
    9702              : 
    9703            1 :         case DIMEN_UNKNOWN:
    9704            1 :         case DIMEN_VECTOR:
    9705            1 :         case DIMEN_STAR:
    9706            1 :         case DIMEN_THIS_IMAGE:
    9707            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9708              :                      &e->where);
    9709            1 :           goto failure;
    9710              :         }
    9711              : 
    9712         2472 : check_symbols:
    9713        45259 :       for (a = code->ext.alloc.list; a; a = a->next)
    9714              :         {
    9715        27851 :           sym = a->expr->symtree->n.sym;
    9716              : 
    9717              :           /* TODO - check derived type components.  */
    9718        27851 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9719         9412 :             continue;
    9720              : 
    9721        18439 :           if ((ar->start[i] != NULL
    9722        17758 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9723        36194 :               || (ar->end[i] != NULL
    9724         2723 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9725              :             {
    9726            3 :               gfc_error ("%qs must not appear in the array specification at "
    9727              :                          "%L in the same ALLOCATE statement where it is "
    9728              :                          "itself allocated", sym->name, &ar->where);
    9729            3 :               goto failure;
    9730              :             }
    9731              :         }
    9732              :     }
    9733              : 
    9734        12205 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9735              :     {
    9736          868 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9737          677 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9738              :         {
    9739          191 :           if (i == (ar->dimen + ar->codimen - 1))
    9740              :             {
    9741            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9742              :                          "statement at %L", &e->where);
    9743            0 :               goto failure;
    9744              :             }
    9745          191 :           continue;
    9746              :         }
    9747              : 
    9748          486 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9749          486 :           && ar->stride[i] == NULL)
    9750              :         break;
    9751              : 
    9752            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9753              :                  &e->where);
    9754            0 :       goto failure;
    9755              :     }
    9756              : 
    9757        12014 : success:
    9758        17362 :   gfc_used_in_allocate_expr (e, &e->where);
    9759              : 
    9760        17362 :   if (code->expr3)
    9761         4019 :     gfc_value_set_at (e->symtree->n.sym, &code->expr3->where, VALUE_VARDEF);
    9762              : 
    9763              :   return true;
    9764              : 
    9765              : failure:
    9766              :   return false;
    9767              : }
    9768              : 
    9769              : 
    9770              : static void
    9771        20542 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9772              : {
    9773        20542 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9774        20542 :   gfc_alloc *a, *p, *q;
    9775              : 
    9776        20542 :   stat = code->expr1;
    9777        20542 :   errmsg = code->expr2;
    9778              : 
    9779              :   /* Check the stat variable.  */
    9780        20542 :   if (stat)
    9781              :     {
    9782          661 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9783          661 :                                      _("STAT variable")))
    9784            8 :           goto done_stat;
    9785              : 
    9786          653 :       if (stat->ts.type != BT_INTEGER
    9787          644 :           || stat->rank > 0)
    9788           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9789              :                    "variable", &stat->where);
    9790              : 
    9791          653 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9792            0 :         goto done_stat;
    9793              : 
    9794              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9795              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9796              :        */
    9797         1354 :       for (p = code->ext.alloc.list; p; p = p->next)
    9798          708 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9799              :           {
    9800            9 :             gfc_ref *ref1, *ref2;
    9801            9 :             bool found = true;
    9802              : 
    9803           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9804            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9805              :               {
    9806            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9807            5 :                   continue;
    9808            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9809              :                   {
    9810              :                     found = false;
    9811              :                     break;
    9812              :                   }
    9813              :               }
    9814              : 
    9815            9 :             if (found)
    9816              :               {
    9817            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9818              :                            "the same %s statement", &stat->where, fcn, fcn);
    9819            7 :                 break;
    9820              :               }
    9821              :           }
    9822              :     }
    9823              : 
    9824        19881 : done_stat:
    9825              : 
    9826              :   /* Check the errmsg variable.  */
    9827        20542 :   if (errmsg)
    9828              :     {
    9829          150 :       if (!stat)
    9830            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9831              :                      &errmsg->where);
    9832              : 
    9833          150 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9834          150 :                                      _("ERRMSG variable")))
    9835            6 :           goto done_errmsg;
    9836              : 
    9837              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9838              :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9839              :          F18:R906  default-char-variable is variable
    9840              :          F18:C906  default-char-variable shall be default character.  */
    9841          144 :       if (errmsg->ts.type != BT_CHARACTER
    9842          142 :           || errmsg->rank > 0
    9843          141 :           || errmsg->ts.kind != gfc_default_character_kind)
    9844            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9845              :                    "variable", &errmsg->where);
    9846              : 
    9847          144 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9848            0 :         goto done_errmsg;
    9849              : 
    9850              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9851              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9852              :        */
    9853          286 :       for (p = code->ext.alloc.list; p; p = p->next)
    9854          147 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9855              :           {
    9856            9 :             gfc_ref *ref1, *ref2;
    9857            9 :             bool found = true;
    9858              : 
    9859           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9860            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9861              :               {
    9862           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9863            4 :                   continue;
    9864            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9865              :                   {
    9866              :                     found = false;
    9867              :                     break;
    9868              :                   }
    9869              :               }
    9870              : 
    9871            9 :             if (found)
    9872              :               {
    9873            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9874              :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9875            5 :                 break;
    9876              :               }
    9877              :           }
    9878              :     }
    9879              : 
    9880        20392 : done_errmsg:
    9881              : 
    9882              :   /* Check that an allocate-object appears only once in the statement.  */
    9883              : 
    9884        46400 :   for (p = code->ext.alloc.list; p; p = p->next)
    9885              :     {
    9886        25858 :       pe = p->expr;
    9887        35152 :       for (q = p->next; q; q = q->next)
    9888              :         {
    9889         9294 :           qe = q->expr;
    9890         9294 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9891              :             {
    9892              :               /* This is a potential collision.  */
    9893         2094 :               gfc_ref *pr = pe->ref;
    9894         2094 :               gfc_ref *qr = qe->ref;
    9895              : 
    9896              :               /* Follow the references  until
    9897              :                  a) They start to differ, in which case there is no error;
    9898              :                  you can deallocate a%b and a%c in a single statement
    9899              :                  b) Both of them stop, which is an error
    9900              :                  c) One of them stops, which is also an error.  */
    9901         4518 :               while (1)
    9902              :                 {
    9903         3306 :                   if (pr == NULL && qr == NULL)
    9904              :                     {
    9905            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9906              :                                  &pe->where, &qe->where);
    9907            7 :                       break;
    9908              :                     }
    9909         3299 :                   else if (pr != NULL && qr == NULL)
    9910              :                     {
    9911            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9912              :                                  " object at %L", &pe->where, &qe->where);
    9913            2 :                       break;
    9914              :                     }
    9915         3297 :                   else if (pr == NULL && qr != NULL)
    9916              :                     {
    9917            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9918              :                                  " object at %L", &qe->where, &pe->where);
    9919            2 :                       break;
    9920              :                     }
    9921              :                   /* Here, pr != NULL && qr != NULL  */
    9922         3295 :                   gcc_assert(pr->type == qr->type);
    9923         3295 :                   if (pr->type == REF_ARRAY)
    9924              :                     {
    9925              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9926              :                          which are legal.  */
    9927         1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9928              : 
    9929         1065 :                       if (pr->next && qr->next)
    9930              :                         {
    9931              :                           int i;
    9932              :                           gfc_array_ref *par = &(pr->u.ar);
    9933              :                           gfc_array_ref *qar = &(qr->u.ar);
    9934              : 
    9935         1840 :                           for (i=0; i<par->dimen; i++)
    9936              :                             {
    9937          954 :                               if ((par->start[i] != NULL
    9938            0 :                                    || qar->start[i] != NULL)
    9939         1908 :                                   && gfc_dep_compare_expr (par->start[i],
    9940          954 :                                                            qar->start[i]) != 0)
    9941          168 :                                 goto break_label;
    9942              :                             }
    9943              :                         }
    9944              :                     }
    9945              :                   else
    9946              :                     {
    9947         2230 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9948              :                         break;
    9949              :                     }
    9950              : 
    9951         1212 :                   pr = pr->next;
    9952         1212 :                   qr = qr->next;
    9953         1212 :                 }
    9954         9294 :             break_label:
    9955              :               ;
    9956              :             }
    9957              :         }
    9958              :     }
    9959              : 
    9960        20542 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9961              :     {
    9962        14419 :       bool arr_alloc_wo_spec = false;
    9963              : 
    9964              :       /* Resolving the expr3 in the loop over all objects to allocate would
    9965              :          execute loop invariant code for each loop item.  Therefore do it just
    9966              :          once here.  */
    9967        14419 :       if (code->expr3 && code->expr3->mold
    9968          363 :           && code->expr3->ts.type == BT_DERIVED
    9969           30 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
    9970              :         {
    9971              :           /* Default initialization via MOLD (non-polymorphic).  */
    9972           28 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9973           28 :           if (rhs != NULL)
    9974              :             {
    9975            9 :               gfc_resolve_expr (rhs);
    9976            9 :               gfc_free_expr (code->expr3);
    9977            9 :               code->expr3 = rhs;
    9978              :             }
    9979              :         }
    9980        31878 :       for (a = code->ext.alloc.list; a; a = a->next)
    9981        17459 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9982              : 
    9983        14419 :       if (arr_alloc_wo_spec && code->expr3)
    9984              :         {
    9985              :           /* Mark the allocate to have to take the array specification
    9986              :              from the expr3.  */
    9987         1225 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9988              :         }
    9989              :     }
    9990              :   else
    9991              :     {
    9992        14522 :       for (a = code->ext.alloc.list; a; a = a->next)
    9993         8399 :         resolve_deallocate_expr (a->expr);
    9994              :     }
    9995        20542 : }
    9996              : 
    9997              : 
    9998              : /************ SELECT CASE resolution subroutines ************/
    9999              : 
   10000              : /* Callback function for our mergesort variant.  Determines interval
   10001              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
   10002              :    op1 > op2.  Assumes we're not dealing with the default case.
   10003              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
   10004              :    There are nine situations to check.  */
   10005              : 
   10006              : static int
   10007         1578 : compare_cases (const gfc_case *op1, const gfc_case *op2)
   10008              : {
   10009         1578 :   int retval;
   10010              : 
   10011         1578 :   if (op1->low == NULL) /* op1 = (:L)  */
   10012              :     {
   10013              :       /* op2 = (:N), so overlap.  */
   10014           52 :       retval = 0;
   10015              :       /* op2 = (M:) or (M:N),  L < M  */
   10016           52 :       if (op2->low != NULL
   10017           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10018              :         retval = -1;
   10019              :     }
   10020         1526 :   else if (op1->high == NULL) /* op1 = (K:)  */
   10021              :     {
   10022              :       /* op2 = (M:), so overlap.  */
   10023           10 :       retval = 0;
   10024              :       /* op2 = (:N) or (M:N), K > N  */
   10025           10 :       if (op2->high != NULL
   10026           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10027              :         retval = 1;
   10028              :     }
   10029              :   else /* op1 = (K:L)  */
   10030              :     {
   10031         1516 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
   10032           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10033           18 :                  ? 1 : 0;
   10034         1498 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
   10035           14 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10036           10 :                  ? -1 : 0;
   10037              :       else                      /* op2 = (M:N)  */
   10038              :         {
   10039         1488 :           retval =  0;
   10040              :           /* L < M  */
   10041         1488 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10042              :             retval =  -1;
   10043              :           /* K > N  */
   10044          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10045          438 :             retval =  1;
   10046              :         }
   10047              :     }
   10048              : 
   10049         1578 :   return retval;
   10050              : }
   10051              : 
   10052              : 
   10053              : /* Merge-sort a double linked case list, detecting overlap in the
   10054              :    process.  LIST is the head of the double linked case list before it
   10055              :    is sorted.  Returns the head of the sorted list if we don't see any
   10056              :    overlap, or NULL otherwise.  */
   10057              : 
   10058              : static gfc_case *
   10059          646 : check_case_overlap (gfc_case *list)
   10060              : {
   10061          646 :   gfc_case *p, *q, *e, *tail;
   10062          646 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10063              : 
   10064              :   /* If the passed list was empty, return immediately.  */
   10065          646 :   if (!list)
   10066              :     return NULL;
   10067              : 
   10068              :   overlap_seen = 0;
   10069              :   insize = 1;
   10070              : 
   10071              :   /* Loop unconditionally.  The only exit from this loop is a return
   10072              :      statement, when we've finished sorting the case list.  */
   10073         1350 :   for (;;)
   10074              :     {
   10075          998 :       p = list;
   10076          998 :       list = NULL;
   10077          998 :       tail = NULL;
   10078              : 
   10079              :       /* Count the number of merges we do in this pass.  */
   10080          998 :       nmerges = 0;
   10081              : 
   10082              :       /* Loop while there exists a merge to be done.  */
   10083         2523 :       while (p)
   10084              :         {
   10085         1525 :           int i;
   10086              : 
   10087              :           /* Count this merge.  */
   10088         1525 :           nmerges++;
   10089              : 
   10090              :           /* Cut the list in two pieces by stepping INSIZE places
   10091              :              forward in the list, starting from P.  */
   10092         1525 :           psize = 0;
   10093         1525 :           q = p;
   10094         3208 :           for (i = 0; i < insize; i++)
   10095              :             {
   10096         2243 :               psize++;
   10097         2243 :               q = q->right;
   10098         2243 :               if (!q)
   10099              :                 break;
   10100              :             }
   10101              :           qsize = insize;
   10102              : 
   10103              :           /* Now we have two lists.  Merge them!  */
   10104         5013 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10105              :             {
   10106              :               /* See from which the next case to merge comes from.  */
   10107          807 :               if (psize == 0)
   10108              :                 {
   10109              :                   /* P is empty so the next case must come from Q.  */
   10110          807 :                   e = q;
   10111          807 :                   q = q->right;
   10112          807 :                   qsize--;
   10113              :                 }
   10114         2681 :               else if (qsize == 0 || q == NULL)
   10115              :                 {
   10116              :                   /* Q is empty.  */
   10117         1103 :                   e = p;
   10118         1103 :                   p = p->right;
   10119         1103 :                   psize--;
   10120              :                 }
   10121              :               else
   10122              :                 {
   10123         1578 :                   cmp = compare_cases (p, q);
   10124         1578 :                   if (cmp < 0)
   10125              :                     {
   10126              :                       /* The whole case range for P is less than the
   10127              :                          one for Q.  */
   10128         1136 :                       e = p;
   10129         1136 :                       p = p->right;
   10130         1136 :                       psize--;
   10131              :                     }
   10132          442 :                   else if (cmp > 0)
   10133              :                     {
   10134              :                       /* The whole case range for Q is greater than
   10135              :                          the case range for P.  */
   10136          438 :                       e = q;
   10137          438 :                       q = q->right;
   10138          438 :                       qsize--;
   10139              :                     }
   10140              :                   else
   10141              :                     {
   10142              :                       /* The cases overlap, or they are the same
   10143              :                          element in the list.  Either way, we must
   10144              :                          issue an error and get the next case from P.  */
   10145              :                       /* FIXME: Sort P and Q by line number.  */
   10146            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10147              :                                  "label at %L", &p->where, &q->where);
   10148            4 :                       overlap_seen = 1;
   10149            4 :                       e = p;
   10150            4 :                       p = p->right;
   10151            4 :                       psize--;
   10152              :                     }
   10153              :                 }
   10154              : 
   10155              :                 /* Add the next element to the merged list.  */
   10156         3488 :               if (tail)
   10157         2490 :                 tail->right = e;
   10158              :               else
   10159              :                 list = e;
   10160         3488 :               e->left = tail;
   10161         3488 :               tail = e;
   10162              :             }
   10163              : 
   10164              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10165              :              they're the same.  */
   10166              :           p = q;
   10167              :         }
   10168          998 :       tail->right = NULL;
   10169              : 
   10170              :       /* If we have done only one merge or none at all, we've
   10171              :          finished sorting the cases.  */
   10172          998 :       if (nmerges <= 1)
   10173              :         {
   10174          646 :           if (!overlap_seen)
   10175              :             return list;
   10176              :           else
   10177              :             return NULL;
   10178              :         }
   10179              : 
   10180              :       /* Otherwise repeat, merging lists twice the size.  */
   10181          352 :       insize *= 2;
   10182          352 :     }
   10183              : }
   10184              : 
   10185              : 
   10186              : /* Check to see if an expression is suitable for use in a CASE statement.
   10187              :    Makes sure that all case expressions are scalar constants of the same
   10188              :    type.  Return false if anything is wrong.  */
   10189              : 
   10190              : static bool
   10191         3307 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10192              : {
   10193         3307 :   if (e == NULL) return true;
   10194              : 
   10195         3214 :   if (e->ts.type != case_expr->ts.type)
   10196              :     {
   10197            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10198              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10199            4 :       return false;
   10200              :     }
   10201              : 
   10202              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10203              :      the same type as case-expr.  For character type, length differences
   10204              :      are allowed, but the kind type parameters shall be the same.  */
   10205              : 
   10206         3210 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10207              :     {
   10208            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10209              :                  &e->where, case_expr->ts.kind);
   10210            4 :       return false;
   10211              :     }
   10212              : 
   10213              :   /* Convert the case value kind to that of case expression kind,
   10214              :      if needed */
   10215              : 
   10216         3206 :   if (e->ts.kind != case_expr->ts.kind)
   10217           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10218              : 
   10219         3206 :   if (e->rank != 0)
   10220              :     {
   10221            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10222              :                  &e->where);
   10223            0 :       return false;
   10224              :     }
   10225              : 
   10226              :   return true;
   10227              : }
   10228              : 
   10229              : 
   10230              : /* Given a completely parsed select statement, we:
   10231              : 
   10232              :      - Validate all expressions and code within the SELECT.
   10233              :      - Make sure that the selection expression is not of the wrong type.
   10234              :      - Make sure that no case ranges overlap.
   10235              :      - Eliminate unreachable cases and unreachable code resulting from
   10236              :        removing case labels.
   10237              : 
   10238              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10239              :    they are a hassle for code generation, and to prevent that, we just
   10240              :    cut them out here.  This is not necessary for overlapping cases
   10241              :    because they are illegal and we never even try to generate code.
   10242              : 
   10243              :    We have the additional caveat that a SELECT construct could have
   10244              :    been a computed GOTO in the source code. Fortunately we can fairly
   10245              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10246              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10247              :    we have to do is make sure that the case_expr is a scalar integer
   10248              :    expression.  */
   10249              : 
   10250              : static void
   10251          687 : resolve_select (gfc_code *code, bool select_type)
   10252              : {
   10253          687 :   gfc_code *body;
   10254          687 :   gfc_expr *case_expr;
   10255          687 :   gfc_case *cp, *default_case, *tail, *head;
   10256          687 :   int seen_unreachable;
   10257          687 :   int seen_logical;
   10258          687 :   int ncases;
   10259          687 :   bt type;
   10260          687 :   bool t;
   10261              : 
   10262          687 :   if (code->expr1 == NULL)
   10263              :     {
   10264              :       /* This was actually a computed GOTO statement.  */
   10265            5 :       case_expr = code->expr2;
   10266            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10267            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10268              :                    "at %L must be a scalar integer expression",
   10269              :                    &case_expr->where);
   10270              : 
   10271              :       /* Further checking is not necessary because this SELECT was built
   10272              :          by the compiler, so it should always be OK.  Just move the
   10273              :          case_expr from expr2 to expr so that we can handle computed
   10274              :          GOTOs as normal SELECTs from here on.  */
   10275            5 :       code->expr1 = code->expr2;
   10276            5 :       code->expr2 = NULL;
   10277            5 :       return;
   10278              :     }
   10279              : 
   10280          682 :   case_expr = code->expr1;
   10281          682 :   type = case_expr->ts.type;
   10282              : 
   10283              :   /* F08:C830.  */
   10284          682 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10285            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10286              : 
   10287              :     {
   10288            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10289              :                  &case_expr->where, gfc_typename (case_expr));
   10290              : 
   10291              :       /* Punt. Going on here just produce more garbage error messages.  */
   10292            0 :       return;
   10293              :     }
   10294              : 
   10295              :   /* F08:R842.  */
   10296          682 :   if (!select_type && case_expr->rank != 0)
   10297              :     {
   10298            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10299              :                  "expression", &case_expr->where);
   10300              : 
   10301              :       /* Punt.  */
   10302            1 :       return;
   10303              :     }
   10304              : 
   10305              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10306              :      the case-expr. Later, all expressions will be promoted to the
   10307              :      largest kind of all case-labels.  */
   10308              : 
   10309          681 :   if (type == BT_INTEGER)
   10310         1927 :     for (body = code->block; body; body = body->block)
   10311         2852 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10312              :         {
   10313         1462 :           if (cp->low
   10314         1462 :               && gfc_check_integer_range (cp->low->value.integer,
   10315              :                                           case_expr->ts.kind) != ARITH_OK)
   10316            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10317            6 :                          "not in the range of %s", &cp->low->where,
   10318              :                          gfc_typename (case_expr));
   10319              : 
   10320         1462 :           if (cp->high
   10321         1178 :               && cp->low != cp->high
   10322         1570 :               && gfc_check_integer_range (cp->high->value.integer,
   10323              :                                           case_expr->ts.kind) != ARITH_OK)
   10324            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10325            0 :                          "not in the range of %s", &cp->high->where,
   10326              :                          gfc_typename (case_expr));
   10327              :         }
   10328              : 
   10329              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10330              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10331              :      of case values, and if we find a mismatch, promote case_expr to
   10332              :      the appropriate kind.  */
   10333              : 
   10334          681 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10335              :     {
   10336         2113 :       for (body = code->block; body; body = body->block)
   10337              :         {
   10338              :           /* Walk the case label list.  */
   10339         3113 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10340              :             {
   10341              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10342         1597 :               if (cp->low == NULL && cp->high == NULL)
   10343          292 :                 continue;
   10344              : 
   10345              :               /* Unreachable case ranges are discarded, so ignore.  */
   10346         1260 :               if (cp->low != NULL && cp->high != NULL
   10347         1212 :                   && cp->low != cp->high
   10348         1370 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10349           33 :                 continue;
   10350              : 
   10351         1272 :               if (cp->low != NULL
   10352         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10353           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10354              : 
   10355         1272 :               if (cp->high != NULL
   10356         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10357            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10358              :             }
   10359              :          }
   10360              :     }
   10361              : 
   10362              :   /* Assume there is no DEFAULT case.  */
   10363          681 :   default_case = NULL;
   10364          681 :   head = tail = NULL;
   10365          681 :   ncases = 0;
   10366          681 :   seen_logical = 0;
   10367              : 
   10368         2502 :   for (body = code->block; body; body = body->block)
   10369              :     {
   10370              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10371         1821 :       t = true;
   10372         1821 :       seen_unreachable = 0;
   10373              : 
   10374              :       /* Walk the case label list, making sure that all case labels
   10375              :          are legal.  */
   10376         3829 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10377              :         {
   10378              :           /* Count the number of cases in the whole construct.  */
   10379         2019 :           ncases++;
   10380              : 
   10381              :           /* Intercept the DEFAULT case.  */
   10382         2019 :           if (cp->low == NULL && cp->high == NULL)
   10383              :             {
   10384          362 :               if (default_case != NULL)
   10385              :                 {
   10386            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10387              :                              "by a second DEFAULT CASE at %L",
   10388              :                              &default_case->where, &cp->where);
   10389            0 :                   t = false;
   10390            0 :                   break;
   10391              :                 }
   10392              :               else
   10393              :                 {
   10394          362 :                   default_case = cp;
   10395          362 :                   continue;
   10396              :                 }
   10397              :             }
   10398              : 
   10399              :           /* Deal with single value cases and case ranges.  Errors are
   10400              :              issued from the validation function.  */
   10401         1657 :           if (!validate_case_label_expr (cp->low, case_expr)
   10402         1657 :               || !validate_case_label_expr (cp->high, case_expr))
   10403              :             {
   10404              :               t = false;
   10405              :               break;
   10406              :             }
   10407              : 
   10408         1649 :           if (type == BT_LOGICAL
   10409           78 :               && ((cp->low == NULL || cp->high == NULL)
   10410           76 :                   || cp->low != cp->high))
   10411              :             {
   10412            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10413              :                          "allowed",
   10414            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10415            2 :               t = false;
   10416            2 :               break;
   10417              :             }
   10418              : 
   10419           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10420              :             {
   10421           76 :               int value;
   10422           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10423           76 :               if (value & seen_logical)
   10424              :                 {
   10425            1 :                   gfc_error ("Constant logical value in CASE statement "
   10426              :                              "is repeated at %L",
   10427              :                              &cp->low->where);
   10428            1 :                   t = false;
   10429            1 :                   break;
   10430              :                 }
   10431           75 :               seen_logical |= value;
   10432              :             }
   10433              : 
   10434         1602 :           if (cp->low != NULL && cp->high != NULL
   10435         1555 :               && cp->low != cp->high
   10436         1758 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10437              :             {
   10438           35 :               if (warn_surprising)
   10439            1 :                 gfc_warning (OPT_Wsurprising,
   10440              :                              "Range specification at %L can never be matched",
   10441              :                              &cp->where);
   10442              : 
   10443           35 :               cp->unreachable = 1;
   10444           35 :               seen_unreachable = 1;
   10445              :             }
   10446              :           else
   10447              :             {
   10448              :               /* If the case range can be matched, it can also overlap with
   10449              :                  other cases.  To make sure it does not, we put it in a
   10450              :                  double linked list here.  We sort that with a merge sort
   10451              :                  later on to detect any overlapping cases.  */
   10452         1611 :               if (!head)
   10453              :                 {
   10454          646 :                   head = tail = cp;
   10455          646 :                   head->right = head->left = NULL;
   10456              :                 }
   10457              :               else
   10458              :                 {
   10459          965 :                   tail->right = cp;
   10460          965 :                   tail->right->left = tail;
   10461          965 :                   tail = tail->right;
   10462          965 :                   tail->right = NULL;
   10463              :                 }
   10464              :             }
   10465              :         }
   10466              : 
   10467              :       /* It there was a failure in the previous case label, give up
   10468              :          for this case label list.  Continue with the next block.  */
   10469         1821 :       if (!t)
   10470           11 :         continue;
   10471              : 
   10472              :       /* See if any case labels that are unreachable have been seen.
   10473              :          If so, we eliminate them.  This is a bit of a kludge because
   10474              :          the case lists for a single case statement (label) is a
   10475              :          single forward linked lists.  */
   10476         1810 :       if (seen_unreachable)
   10477              :       {
   10478              :         /* Advance until the first case in the list is reachable.  */
   10479           69 :         while (body->ext.block.case_list != NULL
   10480           69 :                && body->ext.block.case_list->unreachable)
   10481              :           {
   10482           34 :             gfc_case *n = body->ext.block.case_list;
   10483           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10484           34 :             n->next = NULL;
   10485           34 :             gfc_free_case_list (n);
   10486              :           }
   10487              : 
   10488              :         /* Strip all other unreachable cases.  */
   10489           35 :         if (body->ext.block.case_list)
   10490              :           {
   10491            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10492              :               {
   10493            1 :                 if (cp->next->unreachable)
   10494              :                   {
   10495            1 :                     gfc_case *n = cp->next;
   10496            1 :                     cp->next = cp->next->next;
   10497            1 :                     n->next = NULL;
   10498            1 :                     gfc_free_case_list (n);
   10499              :                   }
   10500              :               }
   10501              :           }
   10502              :       }
   10503              :     }
   10504              : 
   10505              :   /* See if there were overlapping cases.  If the check returns NULL,
   10506              :      there was overlap.  In that case we don't do anything.  If head
   10507              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10508              :      then used during code generation for SELECT CASE constructs with
   10509              :      a case expression of a CHARACTER type.  */
   10510          681 :   if (head)
   10511              :     {
   10512          646 :       head = check_case_overlap (head);
   10513              : 
   10514              :       /* Prepend the default_case if it is there.  */
   10515          646 :       if (head != NULL && default_case)
   10516              :         {
   10517          345 :           default_case->left = NULL;
   10518          345 :           default_case->right = head;
   10519          345 :           head->left = default_case;
   10520              :         }
   10521              :     }
   10522              : 
   10523              :   /* Eliminate dead blocks that may be the result if we've seen
   10524              :      unreachable case labels for a block.  */
   10525         2468 :   for (body = code; body && body->block; body = body->block)
   10526              :     {
   10527         1787 :       if (body->block->ext.block.case_list == NULL)
   10528              :         {
   10529              :           /* Cut the unreachable block from the code chain.  */
   10530           34 :           gfc_code *c = body->block;
   10531           34 :           body->block = c->block;
   10532              : 
   10533              :           /* Kill the dead block, but not the blocks below it.  */
   10534           34 :           c->block = NULL;
   10535           34 :           gfc_free_statements (c);
   10536              :         }
   10537              :     }
   10538              : 
   10539              :   /* More than two cases is legal but insane for logical selects.
   10540              :      Issue a warning for it.  */
   10541          681 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10542            0 :     gfc_warning (OPT_Wsurprising,
   10543              :                  "Logical SELECT CASE block at %L has more that two cases",
   10544              :                  &code->loc);
   10545              : }
   10546              : 
   10547              : 
   10548              : /* Check if a derived type is extensible.  */
   10549              : 
   10550              : bool
   10551        24286 : gfc_type_is_extensible (gfc_symbol *sym)
   10552              : {
   10553        24286 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10554        24270 :            || (sym->attr.is_class
   10555         2196 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10556              : }
   10557              : 
   10558              : 
   10559              : static void
   10560              : resolve_types (gfc_namespace *ns);
   10561              : 
   10562              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10563              :    correct as well as possibly the array-spec.  */
   10564              : 
   10565              : static void
   10566        13051 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10567              : {
   10568        13051 :   gfc_expr* target;
   10569        13051 :   bool parentheses = false;
   10570              : 
   10571        13051 :   gcc_assert (sym->assoc);
   10572        13051 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10573              : 
   10574        13051 :   if (sym->assoc->target
   10575         7815 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10576          598 :       && sym->assoc->target->symtree
   10577          598 :       && sym->assoc->target->symtree->n.sym
   10578          598 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10579              :     {
   10580           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10581           33 :         sym->ts = sym->assoc->target->ts;
   10582              :       else
   10583              :         {
   10584            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10585            0 :                      sym->assoc->target->symtree->n.sym->name,
   10586            0 :                      &sym->assoc->target->where);
   10587            0 :           return;
   10588              :         }
   10589              :     }
   10590              : 
   10591              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10592              :      case, return.  Resolution will be called later manually again when
   10593              :      this is done.  */
   10594        13051 :   target = sym->assoc->target;
   10595        13051 :   if (!target)
   10596              :     return;
   10597         7815 :   gcc_assert (!sym->assoc->dangling);
   10598              : 
   10599         7815 :   if (target->expr_type == EXPR_OP
   10600          267 :       && target->value.op.op == INTRINSIC_PARENTHESES
   10601           42 :       && target->value.op.op1->expr_type == EXPR_VARIABLE)
   10602              :     {
   10603           23 :       sym->assoc->target = gfc_copy_expr (target->value.op.op1);
   10604           23 :       gfc_free_expr (target);
   10605           23 :       target = sym->assoc->target;
   10606           23 :       parentheses = true;
   10607              :     }
   10608              : 
   10609         7815 :   if (resolve_target && !gfc_resolve_expr (target))
   10610              :     return;
   10611              : 
   10612         7810 :   if (sym->assoc->ar)
   10613              :     {
   10614              :       int dim;
   10615              :       gfc_array_ref *ar = sym->assoc->ar;
   10616           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10617              :         {
   10618           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10619           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10620           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10621           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10622            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10623              :                        "remapping of associate name %s at %L",
   10624              :                        sym->name, &sym->declared_at);
   10625              :         }
   10626              :     }
   10627              : 
   10628              :   /* For variable targets, we get some attributes from the target.  */
   10629         7810 :   if (target->expr_type == EXPR_VARIABLE)
   10630              :     {
   10631         6761 :       gfc_symbol *tsym, *dsym;
   10632              : 
   10633         6761 :       gcc_assert (target->symtree);
   10634         6761 :       tsym = target->symtree->n.sym;
   10635              : 
   10636         6761 :       if (gfc_expr_attr (target).proc_pointer)
   10637              :         {
   10638            0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10639              :                      tsym->name, &target->where);
   10640            0 :           return;
   10641              :         }
   10642              : 
   10643           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10644            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10645         6762 :           && dsym->attr.flavor == FL_DERIVED)
   10646              :         {
   10647            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10648              :                      tsym->name, &target->where);
   10649            1 :           return;
   10650              :         }
   10651              : 
   10652         6760 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10653              :         {
   10654           73 :           bool is_error = true;
   10655           73 :           if (tsym->attr.function && tsym->result == tsym)
   10656          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10657          137 :               if (tsym == ns->proc_name)
   10658              :                 {
   10659              :                   is_error = false;
   10660              :                   break;
   10661              :                 }
   10662           64 :           if (is_error)
   10663              :             {
   10664           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10665              :                          tsym->name, &target->where);
   10666           13 :               return;
   10667              :             }
   10668              :         }
   10669              : 
   10670         6747 :       sym->attr.asynchronous = tsym->attr.asynchronous;
   10671         6747 :       sym->attr.volatile_ = tsym->attr.volatile_;
   10672              : 
   10673        13494 :       sym->attr.target = tsym->attr.target
   10674         6747 :                          || gfc_expr_attr (target).pointer;
   10675         6747 :       if (is_subref_array (target))
   10676          402 :         sym->attr.subref_array_pointer = 1;
   10677              :     }
   10678         1049 :   else if (target->ts.type == BT_PROCEDURE)
   10679              :     {
   10680            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10681              :                  &target->where);
   10682            0 :       return;
   10683              :     }
   10684              : 
   10685         7796 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10686              :     {
   10687              :       /* By now, the type of the target has been fixed up.  */
   10688          314 :       symbol_attribute attr;
   10689              : 
   10690          314 :       if (sym->ts.type == BT_DERIVED
   10691          181 :           && target->ts.type == BT_CLASS
   10692           31 :           && !UNLIMITED_POLY (target))
   10693              :         {
   10694              :           /* Inferred to be derived type but the target has type class.  */
   10695           31 :           sym->ts = CLASS_DATA (target)->ts;
   10696           31 :           if (!sym->as)
   10697           31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10698           31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10699           31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10700           31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10701              :                             target->corank);
   10702           31 :           sym->as = NULL;
   10703              :         }
   10704          283 :       else if (target->ts.type == BT_DERIVED
   10705          150 :                && target->symtree && target->symtree->n.sym
   10706          126 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10707            0 :                && IS_INFERRED_TYPE (target)
   10708            0 :                && target->ref && target->ref->next
   10709            0 :                && target->ref->next->type == REF_ARRAY
   10710            0 :                && !target->ref->next->next)
   10711              :         {
   10712              :           /* A inferred type selector whose symbol has been determined to be
   10713              :              a class array but which only has an array reference. Change the
   10714              :              associate name and the selector to class type.  */
   10715            0 :           sym->ts = target->ts;
   10716            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10717            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10718            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10719              :                             target->corank);
   10720            0 :           sym->as = NULL;
   10721            0 :           target->ts = sym->ts;
   10722              :         }
   10723          283 :       else if ((target->ts.type == BT_DERIVED)
   10724          133 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10725           61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10726              :         /* Confirmed to be either a derived type or misidentified to be a
   10727              :            scalar class object, when the selector is a class array.  */
   10728          156 :         sym->ts = target->ts;
   10729          127 :       else if (sym->assoc->inferred_type
   10730          120 :                && (sym->ts.type == BT_COMPLEX
   10731           78 :                    || sym->ts.type == BT_CHARACTER)
   10732           66 :                && target->ts.type == sym->ts.type
   10733           66 :                && sym->ts.kind != target->ts.kind)
   10734              :         /* The inferred type was set from a %re, %im or %len inquiry on
   10735              :            the associate name with the default kind, before the target's
   10736              :            actual type was known.  Now that the target has been resolved,
   10737              :            update the kind to match.  */
   10738            6 :         sym->ts = target->ts;
   10739              :     }
   10740              : 
   10741              : 
   10742         7796 :   if (target->expr_type == EXPR_NULL)
   10743              :     {
   10744            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10745            1 :       return;
   10746              :     }
   10747         7795 :   else if (target->ts.type == BT_UNKNOWN)
   10748              :     {
   10749            2 :       gfc_error ("Selector at %L has no type", &target->where);
   10750            2 :       return;
   10751              :     }
   10752              : 
   10753              :   /* Get type if this was not already set.  Note that it can be
   10754              :      some other type than the target in case this is a SELECT TYPE
   10755              :      selector!  So we must not update when the type is already there.  */
   10756         7793 :   if (sym->ts.type == BT_UNKNOWN)
   10757          259 :     sym->ts = target->ts;
   10758              : 
   10759         7793 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10760              : 
   10761              :   /* See if this is a valid association-to-variable.  */
   10762        15586 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10763         6747 :                            && !parentheses
   10764         6726 :                            && !gfc_has_vector_subscript (target))
   10765         7841 :                           || gfc_is_ptr_fcn (target));
   10766              : 
   10767              :   /* Finally resolve if this is an array or not.  */
   10768         7793 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10769          237 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10770              :     {
   10771          142 :       gfc_expression_rank (target);
   10772          142 :       if (target->ts.type == BT_DERIVED
   10773           95 :           && !sym->as
   10774           95 :           && target->symtree->n.sym->as)
   10775              :         {
   10776            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10777            0 :           sym->attr.dimension = 1;
   10778              :         }
   10779          142 :       else if (target->ts.type == BT_CLASS
   10780           47 :                && CLASS_DATA (target)->as)
   10781              :         {
   10782            0 :           target->rank = CLASS_DATA (target)->as->rank;
   10783            0 :           target->corank = CLASS_DATA (target)->as->corank;
   10784            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10785              :             {
   10786            0 :               sym->ts = target->ts;
   10787            0 :               sym->attr.dimension = 0;
   10788              :             }
   10789              :         }
   10790              :     }
   10791              : 
   10792              : 
   10793         7793 :   if (sym->attr.dimension && target->rank == 0)
   10794              :     {
   10795              :       /* primary.cc makes the assumption that a reference to an associate
   10796              :          name followed by a left parenthesis is an array reference.  */
   10797           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10798              :         {
   10799           12 :           gfc_expression_rank (sym->assoc->target);
   10800           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10801           12 :           if (!sym->attr.dimension && sym->as)
   10802            0 :             sym->as = NULL;
   10803              :         }
   10804              : 
   10805           17 :       if (sym->attr.dimension && target->rank == 0)
   10806              :         {
   10807            5 :           if (sym->ts.type != BT_CHARACTER)
   10808            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10809              :                        sym->name, &sym->declared_at);
   10810            5 :           sym->attr.dimension = 0;
   10811            5 :           return;
   10812              :         }
   10813              :     }
   10814              : 
   10815              :   /* We cannot deal with class selectors that need temporaries.  */
   10816         7788 :   if (target->ts.type == BT_CLASS
   10817         7788 :         && gfc_ref_needs_temporary_p (target->ref))
   10818              :     {
   10819            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10820              :                  "yet implemented", &target->where);
   10821            1 :       return;
   10822              :     }
   10823              : 
   10824         7787 :   if (target->ts.type == BT_CLASS)
   10825         2848 :     gfc_fix_class_refs (target);
   10826              : 
   10827         7787 :   if ((target->rank > 0 || target->corank > 0)
   10828         2766 :       && !sym->attr.select_rank_temporary)
   10829              :     {
   10830         2766 :       gfc_array_spec *as;
   10831              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10832              :          it is corrected now.  */
   10833         2766 :       if (sym->ts.type != BT_CLASS
   10834         2175 :           && (!sym->as || sym->as->corank != target->corank))
   10835              :         {
   10836          135 :           if (!sym->as)
   10837          128 :             sym->as = gfc_get_array_spec ();
   10838          135 :           as = sym->as;
   10839          135 :           as->rank = target->rank;
   10840          135 :           as->type = AS_DEFERRED;
   10841          135 :           as->corank = target->corank;
   10842          135 :           sym->attr.dimension = 1;
   10843          135 :           if (as->corank != 0)
   10844            7 :             sym->attr.codimension = 1;
   10845              :         }
   10846         2631 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10847          590 :                && (!CLASS_DATA (sym)->as
   10848          590 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10849              :         {
   10850            0 :           if (!CLASS_DATA (sym)->as)
   10851            0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10852            0 :           as = CLASS_DATA (sym)->as;
   10853            0 :           as->rank = target->rank;
   10854            0 :           as->type = AS_DEFERRED;
   10855            0 :           as->corank = target->corank;
   10856            0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10857            0 :           if (as->corank != 0)
   10858            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10859              :         }
   10860              :     }
   10861         5021 :   else if (!sym->attr.select_rank_temporary)
   10862              :     {
   10863              :       /* target's rank is 0, but the type of the sym is still array valued,
   10864              :          which has to be corrected.  */
   10865         3608 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10866          730 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10867              :         {
   10868           24 :           gfc_array_spec *as;
   10869           24 :           symbol_attribute attr;
   10870              :           /* The associated variable's type is still the array type
   10871              :              correct this now.  */
   10872           24 :           gfc_typespec *ts = &target->ts;
   10873           24 :           gfc_ref *ref;
   10874              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10875              :            */
   10876           24 :           bool internal_ref = true;
   10877              : 
   10878           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10879              :             {
   10880           48 :               switch (ref->type)
   10881              :                 {
   10882           24 :                 case REF_COMPONENT:
   10883           24 :                   ts = &ref->u.c.component->ts;
   10884           24 :                   internal_ref
   10885           24 :                     = target->ref == ref && ref->next
   10886           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10887              :                   break;
   10888           24 :                 case REF_ARRAY:
   10889           24 :                   if (ts->type == BT_CLASS)
   10890            0 :                     ts = &ts->u.derived->components->ts;
   10891           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10892            0 :                     for (int i = ref->u.ar.dimen;
   10893              :                          internal_ref
   10894            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10895              :                          ++i)
   10896            0 :                       internal_ref
   10897            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10898              :                   break;
   10899              :                 default:
   10900              :                   break;
   10901              :                 }
   10902              :             }
   10903              :           /* Only rewrite the type of this symbol, when the refs are not the
   10904              :              internal ones for class and co-array this-image.  */
   10905           24 :           if (!internal_ref)
   10906              :             {
   10907              :               /* Create a scalar instance of the current class type.  Because
   10908              :                  the rank of a class array goes into its name, the type has to
   10909              :                  be rebuilt.  The alternative of (re-)setting just the
   10910              :                  attributes and as in the current type, destroys the type also
   10911              :                  in other places.  */
   10912            0 :               as = NULL;
   10913            0 :               sym->ts = *ts;
   10914            0 :               sym->ts.type = BT_CLASS;
   10915            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10916            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10917            0 :               sym->as = NULL;
   10918              :             }
   10919              :         }
   10920              :     }
   10921              : 
   10922              :   /* Mark this as an associate variable.  */
   10923         7787 :   sym->attr.associate_var = 1;
   10924              : 
   10925              :   /* Fix up the type-spec for CHARACTER types.  */
   10926         7787 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10927              :     {
   10928          527 :       gfc_ref *ref;
   10929          812 :       for (ref = target->ref; ref; ref = ref->next)
   10930          311 :         if (ref->type == REF_SUBSTRING
   10931           74 :             && (ref->u.ss.start == NULL
   10932           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10933           74 :                 || ref->u.ss.end == NULL
   10934           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10935              :           break;
   10936              : 
   10937          527 :       if (!sym->ts.u.cl)
   10938          182 :         sym->ts.u.cl = target->ts.u.cl;
   10939              : 
   10940          527 :       if (sym->ts.deferred
   10941          195 :           && sym->ts.u.cl == target->ts.u.cl)
   10942              :         {
   10943          116 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10944          116 :           sym->ts.deferred = 1;
   10945              :         }
   10946              : 
   10947          527 :       if (!sym->ts.u.cl->length
   10948          333 :           && !sym->ts.deferred
   10949          138 :           && target->expr_type == EXPR_CONSTANT)
   10950              :         {
   10951           30 :           sym->ts.u.cl->length =
   10952           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10953           30 :                                   target->value.character.length);
   10954              :         }
   10955          497 :       else if (((!sym->ts.u.cl->length
   10956          194 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10957          309 :                 && target->expr_type != EXPR_VARIABLE)
   10958          368 :                || ref)
   10959              :         {
   10960          155 :           if (!sym->ts.deferred)
   10961              :             {
   10962           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10963           45 :               sym->ts.deferred = 1;
   10964              :             }
   10965              : 
   10966              :           /* This is reset in trans-stmt.cc after the assignment
   10967              :              of the target expression to the associate name.  */
   10968          155 :           if (ref && sym->as)
   10969           26 :             sym->attr.pointer = 1;
   10970              :           else
   10971          129 :             sym->attr.allocatable = 1;
   10972              :         }
   10973              :     }
   10974              : 
   10975         7787 :   if (sym->ts.type == BT_CLASS
   10976         1466 :       && IS_INFERRED_TYPE (target)
   10977           13 :       && target->ts.type == BT_DERIVED
   10978            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10979            0 :       && target->ref && target->ref->next && !target->ref->next->next
   10980            0 :       && target->ref->next->type == REF_ARRAY)
   10981            0 :     target->ts = target->symtree->n.sym->ts;
   10982              : 
   10983              :   /* If the target is a good class object, so is the associate variable.  */
   10984         7787 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10985          731 :     sym->attr.class_ok = 1;
   10986              : 
   10987              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   10988         7787 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   10989            3 :     sym->attr.contiguous = 1;
   10990              : }
   10991              : 
   10992              : 
   10993              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   10994              :    array reference, where necessary.  The symbols are artificial and so
   10995              :    the dimension attribute and arrayspec can also be set.  In addition,
   10996              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   10997              :    This is corrected here as well.*/
   10998              : 
   10999              : static void
   11000         1719 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   11001              :                  gfc_ref *ref)
   11002              : {
   11003         1719 :   gfc_ref *nref = (*expr1)->ref;
   11004         1719 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   11005         1719 :   gfc_symbol *sym2;
   11006         1719 :   gfc_expr *selector = gfc_copy_expr (expr2);
   11007              : 
   11008         1719 :   (*expr1)->rank = rank;
   11009         1719 :   (*expr1)->corank = corank;
   11010         1719 :   if (selector)
   11011              :     {
   11012          324 :       gfc_resolve_expr (selector);
   11013          324 :       if (selector->expr_type == EXPR_OP
   11014            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   11015            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   11016          322 :       else if (selector->expr_type == EXPR_VARIABLE
   11017            7 :                || selector->expr_type == EXPR_FUNCTION)
   11018          322 :         sym2 = selector->symtree->n.sym;
   11019              :       else
   11020            0 :         gcc_unreachable ();
   11021              :     }
   11022              :   else
   11023              :     sym2 = NULL;
   11024              : 
   11025         1719 :   if (sym1->ts.type == BT_CLASS)
   11026              :     {
   11027         1719 :       if ((*expr1)->ts.type != BT_CLASS)
   11028           13 :         (*expr1)->ts = sym1->ts;
   11029              : 
   11030         1719 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   11031         1719 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   11032         1719 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   11033            1 :         CLASS_DATA (sym1)->as
   11034            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   11035              :     }
   11036              :   else
   11037              :     {
   11038            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   11039            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   11040            0 :       if (sym1->as == NULL && sym2)
   11041            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   11042              :     }
   11043              : 
   11044         3108 :   for (; nref; nref = nref->next)
   11045         2784 :     if (nref->next == NULL)
   11046              :       break;
   11047              : 
   11048         1719 :   if (ref && nref && nref->type != REF_ARRAY)
   11049            6 :     nref->next = gfc_copy_ref (ref);
   11050         1713 :   else if (ref && !nref)
   11051          315 :     (*expr1)->ref = gfc_copy_ref (ref);
   11052         1398 :   else if (ref && nref->u.ar.codimen != corank)
   11053              :     {
   11054          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   11055          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11056           61 :       nref->u.ar.codimen = corank;
   11057              :     }
   11058         1719 : }
   11059              : 
   11060              : 
   11061              : static gfc_expr *
   11062         6856 : build_loc_call (gfc_expr *sym_expr)
   11063              : {
   11064         6856 :   gfc_expr *loc_call;
   11065         6856 :   loc_call = gfc_get_expr ();
   11066         6856 :   loc_call->expr_type = EXPR_FUNCTION;
   11067         6856 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11068         6856 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11069         6856 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11070         6856 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11071         6856 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11072         6856 :   loc_call->ts.type = BT_INTEGER;
   11073         6856 :   loc_call->ts.kind = gfc_index_integer_kind;
   11074         6856 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11075         6856 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11076         6856 :   loc_call->value.function.actual->expr = sym_expr;
   11077         6856 :   loc_call->where = sym_expr->where;
   11078         6856 :   return loc_call;
   11079              : }
   11080              : 
   11081              : /* Resolve a SELECT TYPE statement.  */
   11082              : 
   11083              : static void
   11084         3081 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11085              : {
   11086         3081 :   gfc_symbol *selector_type;
   11087         3081 :   gfc_code *body, *new_st, *if_st, *tail;
   11088         3081 :   gfc_code *class_is = NULL, *default_case = NULL;
   11089         3081 :   gfc_case *c;
   11090         3081 :   gfc_symtree *st;
   11091         3081 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11092         3081 :   gfc_namespace *ns;
   11093         3081 :   int error = 0;
   11094         3081 :   int rank = 0, corank = 0;
   11095         3081 :   gfc_ref* ref = NULL;
   11096         3081 :   gfc_expr *selector_expr = NULL;
   11097         3081 :   gfc_code *old_code = code;
   11098              : 
   11099         3081 :   ns = code->ext.block.ns;
   11100         3081 :   if (code->expr2)
   11101              :     {
   11102              :       /* Set this, or coarray checks in resolve will fail.  */
   11103          670 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11104              :     }
   11105         3081 :   gfc_resolve (ns);
   11106              : 
   11107              :   /* Check for F03:C813.  */
   11108         3081 :   if (code->expr1->ts.type != BT_CLASS
   11109           36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11110              :     {
   11111           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11112              :                  "at %L", &code->loc);
   11113           42 :       return;
   11114              :     }
   11115              : 
   11116              :   /* Prevent segfault, when class type is not initialized due to previous
   11117              :      error.  */
   11118         3068 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11119         3066 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11120              :     return;
   11121              : 
   11122         3061 :   if (code->expr2)
   11123              :     {
   11124          661 :       gfc_ref *ref2 = NULL;
   11125         1532 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11126          871 :          if (ref->type == REF_COMPONENT
   11127          447 :              && ref->u.c.component->ts.type == BT_CLASS)
   11128          871 :            ref2 = ref;
   11129              : 
   11130          661 :       if (ref2)
   11131              :         {
   11132          353 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11133            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11134          353 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11135              :         }
   11136              :       else
   11137              :         {
   11138          308 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11139           28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11140              :           /* Sometimes the selector expression is given the typespec of the
   11141              :              '_data' field, which is logical enough but inappropriate here. */
   11142          308 :           if (code->expr2->ts.type == BT_DERIVED
   11143           73 :               && code->expr2->symtree
   11144           73 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11145           73 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11146          308 :           selector_type = CLASS_DATA (code->expr2)
   11147              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11148              :         }
   11149              : 
   11150          661 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11151              :         {
   11152          310 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11153          310 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11154          310 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11155              :         }
   11156              : 
   11157              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11158          661 :       if (gfc_is_coindexed (code->expr2))
   11159              :         {
   11160            4 :           gfc_error ("Selector at %L must not be coindexed",
   11161            4 :                      &code->expr2->where);
   11162            4 :           return;
   11163              :         }
   11164              : 
   11165              :     }
   11166              :   else
   11167              :     {
   11168         2400 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11169              : 
   11170         2400 :       if (gfc_is_coindexed (code->expr1))
   11171              :         {
   11172            0 :           gfc_error ("Selector at %L must not be coindexed",
   11173            0 :                      &code->expr1->where);
   11174            0 :           return;
   11175              :         }
   11176              :     }
   11177              : 
   11178              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11179         8513 :   for (body = code->block; body; body = body->block)
   11180              :     {
   11181         5457 :       c = body->ext.block.case_list;
   11182              : 
   11183         5457 :       if (!error)
   11184              :         {
   11185              :           /* Check for repeated cases.  */
   11186         8452 :           for (tail = code->block; tail; tail = tail->block)
   11187              :             {
   11188         8452 :               gfc_case *d = tail->ext.block.case_list;
   11189         8452 :               if (tail == body)
   11190              :                 break;
   11191              : 
   11192         3004 :               if (c->ts.type == d->ts.type
   11193          516 :                   && ((c->ts.type == BT_DERIVED
   11194          418 :                        && c->ts.u.derived && d->ts.u.derived
   11195          418 :                        && !strcmp (c->ts.u.derived->name,
   11196              :                                    d->ts.u.derived->name))
   11197          515 :                       || c->ts.type == BT_UNKNOWN
   11198          515 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11199           55 :                           && c->ts.kind == d->ts.kind)))
   11200              :                 {
   11201            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11202              :                              &c->where, &d->where);
   11203            1 :                   return;
   11204              :                 }
   11205              :             }
   11206              :         }
   11207              : 
   11208              :       /* Check F03:C815.  */
   11209         3448 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11210         2358 :           && selector_type
   11211         2358 :           && !selector_type->attr.unlimited_polymorphic
   11212         7491 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11213              :         {
   11214            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11215            1 :                      c->ts.u.derived->name, &c->where);
   11216            1 :           error++;
   11217            1 :           continue;
   11218              :         }
   11219              : 
   11220              :       /* Check F03:C816.  */
   11221         5461 :       if (c->ts.type != BT_UNKNOWN
   11222         3815 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11223         7493 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11224         2034 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11225              :         {
   11226            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11227            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11228            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11229              :           else
   11230            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11231              :                        gfc_basic_typename (c->ts.type), &c->where);
   11232            6 :           error++;
   11233            6 :           continue;
   11234              :         }
   11235              : 
   11236              :       /* Check F03:C814.  */
   11237         5449 :       if (c->ts.type == BT_CHARACTER
   11238          742 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11239              :         {
   11240            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11241              :                      "type parameter is assumed", &c->where);
   11242            0 :           error++;
   11243            0 :           continue;
   11244              :         }
   11245              : 
   11246              :       /* Intercept the DEFAULT case.  */
   11247         5449 :       if (c->ts.type == BT_UNKNOWN)
   11248              :         {
   11249              :           /* Check F03:C818.  */
   11250         1640 :           if (default_case)
   11251              :             {
   11252            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11253              :                          "by a second DEFAULT CASE at %L",
   11254            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11255            1 :               error++;
   11256            1 :               continue;
   11257              :             }
   11258              : 
   11259              :           default_case = body;
   11260              :         }
   11261              :     }
   11262              : 
   11263         3056 :   if (error > 0)
   11264              :     return;
   11265              : 
   11266              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11267              :      target if present.  If there are any EXIT statements referring to the
   11268              :      SELECT TYPE construct, this is no problem because the gfc_code
   11269              :      reference stays the same and EXIT is equally possible from the BLOCK
   11270              :      it is changed to.  */
   11271         3053 :   code->op = EXEC_BLOCK;
   11272         3053 :   if (code->expr2)
   11273              :     {
   11274          657 :       gfc_association_list* assoc;
   11275              : 
   11276          657 :       assoc = gfc_get_association_list ();
   11277          657 :       assoc->st = code->expr1->symtree;
   11278          657 :       assoc->target = gfc_copy_expr (code->expr2);
   11279          657 :       assoc->target->where = code->expr2->where;
   11280              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11281              : 
   11282          657 :       code->ext.block.assoc = assoc;
   11283          657 :       code->expr1->symtree->n.sym->assoc = assoc;
   11284              : 
   11285          657 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11286              :     }
   11287              :   else
   11288         2396 :     code->ext.block.assoc = NULL;
   11289              : 
   11290              :   /* Ensure that the selector rank and arrayspec are available to
   11291              :      correct expressions in which they might be missing.  */
   11292         3053 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11293              :     {
   11294          324 :       rank = code->expr2->rank;
   11295          324 :       corank = code->expr2->corank;
   11296          608 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11297          599 :         if (ref->next == NULL)
   11298              :           break;
   11299          324 :       if (ref && ref->type == REF_ARRAY)
   11300          315 :         ref = gfc_copy_ref (ref);
   11301              : 
   11302              :       /* Fixup expr1 if necessary.  */
   11303          324 :       if (rank || corank)
   11304          324 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11305              :     }
   11306         2729 :   else if (code->expr1->rank || code->expr1->corank)
   11307              :     {
   11308          892 :       rank = code->expr1->rank;
   11309          892 :       corank = code->expr1->corank;
   11310          892 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11311          892 :         if (ref->next == NULL)
   11312              :           break;
   11313          892 :       if (ref && ref->type == REF_ARRAY)
   11314          892 :         ref = gfc_copy_ref (ref);
   11315              :     }
   11316              : 
   11317         3053 :   gfc_expr *orig_expr1 = code->expr1;
   11318              : 
   11319              :   /* Add EXEC_SELECT to switch on type.  */
   11320         3053 :   new_st = gfc_get_code (code->op);
   11321         3053 :   new_st->expr1 = code->expr1;
   11322         3053 :   new_st->expr2 = code->expr2;
   11323         3053 :   new_st->block = code->block;
   11324         3053 :   code->expr1 = code->expr2 =  NULL;
   11325         3053 :   code->block = NULL;
   11326         3053 :   if (!ns->code)
   11327         3053 :     ns->code = new_st;
   11328              :   else
   11329            0 :     ns->code->next = new_st;
   11330         3053 :   code = new_st;
   11331         3053 :   code->op = EXEC_SELECT_TYPE;
   11332              : 
   11333              :   /* Use the intrinsic LOC function to generate an integer expression
   11334              :      for the vtable of the selector.  Note that the rank of the selector
   11335              :      expression has to be set to zero.  */
   11336         3053 :   gfc_add_vptr_component (code->expr1);
   11337         3053 :   code->expr1->rank = 0;
   11338         3053 :   code->expr1->corank = 0;
   11339         3053 :   code->expr1 = build_loc_call (code->expr1);
   11340         3053 :   selector_expr = code->expr1->value.function.actual->expr;
   11341              : 
   11342              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11343         8494 :   for (body = code->block; body; body = body->block)
   11344              :     {
   11345         5441 :       gfc_symbol *vtab;
   11346         5441 :       c = body->ext.block.case_list;
   11347              : 
   11348              :       /* Generate an index integer expression for address of the
   11349              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11350              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11351         5441 :       if (c->ts.type != BT_UNKNOWN)
   11352              :         {
   11353         3803 :           gfc_expr *e;
   11354         3803 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11355              :             {
   11356         2349 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11357         2349 :               gcc_assert (vtab);
   11358         2349 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11359         2349 :                                           c->ts.u.derived->hash_value);
   11360              :             }
   11361              :           else
   11362              :             {
   11363         1454 :               vtab = gfc_find_vtab (&c->ts);
   11364         1454 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11365         1454 :               e = CLASS_DATA (vtab)->initializer;
   11366         1454 :               c->high = gfc_copy_expr (e);
   11367         1454 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11368              :                 {
   11369            1 :                   gfc_typespec ts;
   11370            1 :                   ts.kind = gfc_integer_4_kind;
   11371            1 :                   ts.type = BT_INTEGER;
   11372            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11373              :                 }
   11374              :             }
   11375              : 
   11376         3803 :           e = gfc_lval_expr_from_sym (vtab);
   11377         3803 :           c->low = build_loc_call (e);
   11378              :         }
   11379              :       else
   11380         1638 :         continue;
   11381              : 
   11382              :       /* Associate temporary to selector.  This should only be done
   11383              :          when this case is actually true, so build a new ASSOCIATE
   11384              :          that does precisely this here (instead of using the
   11385              :          'global' one).  */
   11386              : 
   11387              :       /* First check the derived type import status.  */
   11388         3803 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11389            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11390              :         {
   11391           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11392            6 :                                  c->ts.u.derived->name);
   11393            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11394              :                                         gfc_current_ns))
   11395            6 :             error++;
   11396              :         }
   11397              : 
   11398         3803 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11399         3803 :       if (c->ts.type == BT_CLASS)
   11400          348 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11401          348 :                   c->ts.u.derived->name, var_name);
   11402         3455 :       else if (c->ts.type == BT_DERIVED)
   11403         2001 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11404         2001 :                   c->ts.u.derived->name, var_name);
   11405         1454 :       else if (c->ts.type == BT_CHARACTER)
   11406              :         {
   11407          742 :           HOST_WIDE_INT charlen = 0;
   11408          742 :           if (c->ts.u.cl && c->ts.u.cl->length
   11409            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11410            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11411          742 :           snprintf (name, sizeof (name),
   11412              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11413              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11414              :                     var_name);
   11415              :         }
   11416              :       else
   11417          712 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11418              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11419              : 
   11420         3803 :       st = gfc_find_symtree (ns->sym_root, name);
   11421         3803 :       gcc_assert (st->n.sym->assoc);
   11422         3803 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11423         3803 :       st->n.sym->assoc->target->where = selector_expr->where;
   11424         3803 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11425              :         {
   11426         3455 :           gfc_add_data_component (st->n.sym->assoc->target);
   11427              :           /* Fixup the target expression if necessary.  */
   11428         3455 :           if (rank || corank)
   11429         1395 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11430              :                              ref);
   11431              :         }
   11432              : 
   11433         3803 :       new_st = gfc_get_code (EXEC_BLOCK);
   11434         3803 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11435         3803 :       new_st->ext.block.ns->code = body->next;
   11436         3803 :       body->next = new_st;
   11437              : 
   11438              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11439              :          there is a CASE label overlap and this is already used.  Just ignore,
   11440              :          the error is diagnosed elsewhere.  */
   11441         3803 :       if (st->n.sym->assoc->dangling)
   11442              :         {
   11443         3802 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11444         3802 :           st->n.sym->assoc->dangling = 0;
   11445              :         }
   11446              : 
   11447         3803 :       resolve_assoc_var (st->n.sym, false);
   11448              :     }
   11449              : 
   11450              :   /* Take out CLASS IS cases for separate treatment.  */
   11451              :   body = code;
   11452         8494 :   while (body && body->block)
   11453              :     {
   11454         5441 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11455              :         {
   11456              :           /* Add to class_is list.  */
   11457          348 :           if (class_is == NULL)
   11458              :             {
   11459          317 :               class_is = body->block;
   11460          317 :               tail = class_is;
   11461              :             }
   11462              :           else
   11463              :             {
   11464           43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11465           31 :               tail->block = body->block;
   11466           31 :               tail = tail->block;
   11467              :             }
   11468              :           /* Remove from EXEC_SELECT list.  */
   11469          348 :           body->block = body->block->block;
   11470          348 :           tail->block = NULL;
   11471              :         }
   11472              :       else
   11473              :         body = body->block;
   11474              :     }
   11475              : 
   11476         3053 :   if (class_is)
   11477              :     {
   11478          317 :       gfc_symbol *vtab;
   11479              : 
   11480          317 :       if (!default_case)
   11481              :         {
   11482              :           /* Add a default case to hold the CLASS IS cases.  */
   11483          315 :           for (tail = code; tail->block; tail = tail->block) ;
   11484          207 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11485          207 :           tail = tail->block;
   11486          207 :           tail->ext.block.case_list = gfc_get_case ();
   11487          207 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11488          207 :           tail->next = NULL;
   11489          207 :           default_case = tail;
   11490              :         }
   11491              : 
   11492              :       /* More than one CLASS IS block?  */
   11493          317 :       if (class_is->block)
   11494              :         {
   11495           37 :           gfc_code **c1,*c2;
   11496           37 :           bool swapped;
   11497              :           /* Sort CLASS IS blocks by extension level.  */
   11498           36 :           do
   11499              :             {
   11500           37 :               swapped = false;
   11501           97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11502              :                 {
   11503           61 :                   c2 = (*c1)->block;
   11504              :                   /* F03:C817 (check for doubles).  */
   11505           61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   11506           61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   11507              :                     {
   11508            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11509              :                                  "statement at %L",
   11510              :                                  &c2->ext.block.case_list->where);
   11511            1 :                       return;
   11512              :                     }
   11513           60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11514           60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11515              :                     {
   11516              :                       /* Swap.  */
   11517           24 :                       (*c1)->block = c2->block;
   11518           24 :                       c2->block = *c1;
   11519           24 :                       *c1 = c2;
   11520           24 :                       swapped = true;
   11521              :                     }
   11522              :                 }
   11523              :             }
   11524              :           while (swapped);
   11525              :         }
   11526              : 
   11527              :       /* Generate IF chain.  */
   11528          316 :       if_st = gfc_get_code (EXEC_IF);
   11529          316 :       new_st = if_st;
   11530          662 :       for (body = class_is; body; body = body->block)
   11531              :         {
   11532          346 :           new_st->block = gfc_get_code (EXEC_IF);
   11533          346 :           new_st = new_st->block;
   11534              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11535          346 :           new_st->expr1 = gfc_get_expr ();
   11536          346 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11537          346 :           new_st->expr1->ts.type = BT_LOGICAL;
   11538          346 :           new_st->expr1->ts.kind = 4;
   11539          346 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11540          346 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11541          346 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11542              :           /* Set up arguments.  */
   11543          346 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11544          346 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11545          346 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11546          346 :           new_st->expr1->where = code->loc;
   11547          346 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11548          346 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11549          346 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11550          346 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11551          346 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11552          346 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11553              :           /* Set up types in formal arg list.  */
   11554          346 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11555          346 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11556          346 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11557          346 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11558              : 
   11559          346 :           new_st->next = body->next;
   11560              :         }
   11561          316 :         if (default_case->next)
   11562              :           {
   11563          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11564          110 :             new_st = new_st->block;
   11565          110 :             new_st->next = default_case->next;
   11566              :           }
   11567              : 
   11568              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11569          316 :         default_case->next = if_st;
   11570              :     }
   11571              : 
   11572              :   /* Resolve the internal code.  This cannot be done earlier because
   11573              :      it requires that the sym->assoc of selectors is set already.  */
   11574         3052 :   gfc_current_ns = ns;
   11575         3052 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11576         3052 :   gfc_current_ns = old_ns;
   11577              : 
   11578         3052 :   free (ref);
   11579              : }
   11580              : 
   11581              : 
   11582              : /* Resolve a SELECT RANK statement.  */
   11583              : 
   11584              : static void
   11585         1036 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11586              : {
   11587         1036 :   gfc_namespace *ns;
   11588         1036 :   gfc_code *body, *new_st, *tail;
   11589         1036 :   gfc_case *c;
   11590         1036 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11591         1036 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11592         1036 :   gfc_symtree *st;
   11593         1036 :   gfc_expr *selector_expr = NULL;
   11594         1036 :   int case_value;
   11595         1036 :   HOST_WIDE_INT charlen = 0;
   11596              : 
   11597         1036 :   ns = code->ext.block.ns;
   11598         1036 :   gfc_resolve (ns);
   11599              : 
   11600         1036 :   code->op = EXEC_BLOCK;
   11601         1036 :   if (code->expr2)
   11602              :     {
   11603           42 :       gfc_association_list* assoc;
   11604              : 
   11605           42 :       assoc = gfc_get_association_list ();
   11606           42 :       assoc->st = code->expr1->symtree;
   11607           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11608           42 :       assoc->target->where = code->expr2->where;
   11609              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11610              : 
   11611           42 :       code->ext.block.assoc = assoc;
   11612           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11613              : 
   11614           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11615              :     }
   11616              :   else
   11617          994 :     code->ext.block.assoc = NULL;
   11618              : 
   11619              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11620              :      cascade of further errors because the case blocks do not compile
   11621              :      correctly.  */
   11622         3380 :   for (body = code->block; body; body = body->block)
   11623              :     {
   11624         2344 :       c = body->ext.block.case_list;
   11625         2344 :       if (c->low)
   11626         1413 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11627              :       else
   11628              :         case_value = -2;
   11629              : 
   11630              :       /* Check for repeated cases.  */
   11631         5914 :       for (tail = code->block; tail; tail = tail->block)
   11632              :         {
   11633         5914 :           gfc_case *d = tail->ext.block.case_list;
   11634         5914 :           int case_value2;
   11635              : 
   11636         5914 :           if (tail == body)
   11637              :             break;
   11638              : 
   11639              :           /* Check F2018: C1153.  */
   11640         3570 :           if (!c->low && !d->low)
   11641            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11642              :                        &c->where, &d->where);
   11643              : 
   11644         3570 :           if (!c->low || !d->low)
   11645         1277 :             continue;
   11646              : 
   11647              :           /* Check F2018: C1153.  */
   11648         2293 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11649         2293 :           if ((case_value == case_value2) && case_value == -1)
   11650            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11651              :                        &c->where, &d->where);
   11652         2292 :           else if (case_value == case_value2)
   11653            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11654              :                        case_value, &c->where, &d->where);
   11655              :         }
   11656              : 
   11657         2344 :       if (!c->low)
   11658          931 :         continue;
   11659              : 
   11660              :       /* Check F2018: C1155.  */
   11661         1413 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11662         1411 :                                || gfc_expr_attr (code->expr1).pointer))
   11663            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11664            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11665              :     }
   11666              : 
   11667              :   /* Add EXEC_SELECT to switch on rank.  */
   11668         1036 :   new_st = gfc_get_code (code->op);
   11669         1036 :   new_st->expr1 = code->expr1;
   11670         1036 :   new_st->expr2 = code->expr2;
   11671         1036 :   new_st->block = code->block;
   11672         1036 :   code->expr1 = code->expr2 =  NULL;
   11673         1036 :   code->block = NULL;
   11674         1036 :   if (!ns->code)
   11675         1036 :     ns->code = new_st;
   11676              :   else
   11677            0 :     ns->code->next = new_st;
   11678         1036 :   code = new_st;
   11679         1036 :   code->op = EXEC_SELECT_RANK;
   11680              : 
   11681         1036 :   selector_expr = code->expr1;
   11682              : 
   11683              :   /* Loop over SELECT RANK cases.  */
   11684         3380 :   for (body = code->block; body; body = body->block)
   11685              :     {
   11686         2344 :       c = body->ext.block.case_list;
   11687         2344 :       int case_value;
   11688              : 
   11689              :       /* Pass on the default case.  */
   11690         2344 :       if (c->low == NULL)
   11691          931 :         continue;
   11692              : 
   11693              :       /* Associate temporary to selector.  This should only be done
   11694              :          when this case is actually true, so build a new ASSOCIATE
   11695              :          that does precisely this here (instead of using the
   11696              :          'global' one).  */
   11697         1413 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11698          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11699          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11700              : 
   11701         1413 :       if (c->ts.type == BT_CLASS)
   11702          145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11703         1268 :       else if (c->ts.type == BT_DERIVED)
   11704          110 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11705         1158 :       else if (c->ts.type != BT_CHARACTER)
   11706          599 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11707              :       else
   11708          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11709              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11710              : 
   11711         1413 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11712         1413 :       if (case_value >= 0)
   11713         1380 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11714              :       else
   11715           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11716              : 
   11717         1413 :       st = gfc_find_symtree (ns->sym_root, name);
   11718         1413 :       gcc_assert (st->n.sym->assoc);
   11719              : 
   11720         1413 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11721         1413 :       st->n.sym->assoc->target->where = selector_expr->where;
   11722              : 
   11723         1413 :       new_st = gfc_get_code (EXEC_BLOCK);
   11724         1413 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11725         1413 :       new_st->ext.block.ns->code = body->next;
   11726         1413 :       body->next = new_st;
   11727              : 
   11728              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11729              :          there is a CASE label overlap and this is already used.  Just ignore,
   11730              :          the error is diagnosed elsewhere.  */
   11731         1413 :       if (st->n.sym->assoc->dangling)
   11732              :         {
   11733         1411 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11734         1411 :           st->n.sym->assoc->dangling = 0;
   11735              :         }
   11736              : 
   11737         1413 :       resolve_assoc_var (st->n.sym, false);
   11738              :     }
   11739              : 
   11740         1036 :   gfc_current_ns = ns;
   11741         1036 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11742         1036 :   gfc_current_ns = old_ns;
   11743         1036 : }
   11744              : 
   11745              : 
   11746              : /* Resolve a transfer statement. This is making sure that:
   11747              :    -- a derived type being transferred has only non-pointer components
   11748              :    -- a derived type being transferred doesn't have private components, unless
   11749              :       it's being transferred from the module where the type was defined
   11750              :    -- we're not trying to transfer a whole assumed size array.  */
   11751              : 
   11752              : static void
   11753        47475 : resolve_transfer (gfc_code *code)
   11754              : {
   11755        47475 :   gfc_symbol *sym, *derived;
   11756        47475 :   gfc_ref *ref;
   11757        47475 :   gfc_expr *exp;
   11758        47475 :   bool write = false;
   11759        47475 :   bool formatted = false;
   11760        47475 :   gfc_dt *dt = code->ext.dt;
   11761        47475 :   gfc_symbol *dtio_sub = NULL;
   11762              : 
   11763        47475 :   exp = code->expr1;
   11764              : 
   11765        94956 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11766        48409 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11767            6 :     exp = exp->value.op.op1;
   11768              : 
   11769        47475 :   if (exp && exp->expr_type == EXPR_NULL
   11770            2 :       && code->ext.dt)
   11771              :     {
   11772            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11773              :                  &exp->where);
   11774            2 :       return;
   11775              :     }
   11776              : 
   11777        47473 :   if (dt && (dt->dt_io_kind->value.iokind == M_WRITE
   11778        47321 :              || dt->dt_io_kind->value.iokind == M_PRINT))
   11779        39814 :     gfc_value_used_expr (exp, VALUE_USED);
   11780              : 
   11781        47473 :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11782              :                       && exp->expr_type != EXPR_FUNCTION
   11783              :                       && exp->expr_type != EXPR_ARRAY
   11784              :                       && exp->expr_type != EXPR_STRUCTURE))
   11785              :     return;
   11786              : 
   11787        26309 :   if (dt && dt->dt_io_kind->value.iokind == M_READ)
   11788              :     {
   11789              :       /* If we are reading, the variable will be changed.  Note that
   11790              :          code->ext.dt may be NULL if the TRANSFER is related to an INQUIRE
   11791              :          statement -- but in this case, we are not reading, either.  */
   11792         7507 :       if (!gfc_check_vardef_context (exp, false, false, false,
   11793         7507 :                                      _("item in READ")))
   11794              :         return;
   11795              : 
   11796         7503 :       gfc_expr_set_at (exp, &exp->where, VALUE_READ);
   11797              :     }
   11798              : 
   11799        26305 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11800        26305 :                         || exp->expr_type == EXPR_FUNCTION
   11801        21914 :                         || exp->expr_type == EXPR_ARRAY
   11802        48219 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11803              : 
   11804              :   /* Go to actual component transferred.  */
   11805        34119 :   for (ref = exp->ref; ref; ref = ref->next)
   11806         7814 :     if (ref->type == REF_COMPONENT)
   11807         2210 :       ts = &ref->u.c.component->ts;
   11808              : 
   11809        26305 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11810        26157 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11811              :     {
   11812          720 :       derived = ts->u.derived;
   11813              : 
   11814              :       /* Determine when to use the formatted DTIO procedure.  */
   11815          720 :       if (dt && (dt->format_expr || dt->format_label))
   11816          645 :         formatted = true;
   11817              : 
   11818          720 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11819          720 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11820          720 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11821              : 
   11822          720 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11823              :         {
   11824          450 :           dt->udtio = exp;
   11825          450 :           sym = exp->symtree->n.sym->ns->proc_name;
   11826              :           /* Check to see if this is a nested DTIO call, with the
   11827              :              dummy as the io-list object.  */
   11828          450 :           if (sym && sym == dtio_sub && sym->formal
   11829           30 :               && sym->formal->sym == exp->symtree->n.sym
   11830           30 :               && exp->ref == NULL)
   11831              :             {
   11832            0 :               if (!sym->attr.recursive)
   11833              :                 {
   11834            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11835              :                              sym->name, &sym->declared_at);
   11836            0 :                   return;
   11837              :                 }
   11838              :             }
   11839              :         }
   11840              :     }
   11841              : 
   11842        26305 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11843              :     {
   11844            3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11845              :                 "it is processed by a defined input/output procedure",
   11846              :                 &code->loc);
   11847            3 :       return;
   11848              :     }
   11849              : 
   11850        26302 :   if (ts->type == BT_DERIVED)
   11851              :     {
   11852              :       /* Check that transferred derived type doesn't contain POINTER
   11853              :          components unless it is processed by a defined input/output
   11854              :          procedure".  */
   11855          688 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11856              :         {
   11857            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11858              :                      "components unless it is processed by a defined "
   11859              :                      "input/output procedure", &code->loc);
   11860            2 :           return;
   11861              :         }
   11862              : 
   11863              :       /* F08:C935.  */
   11864          686 :       if (ts->u.derived->attr.proc_pointer_comp)
   11865              :         {
   11866            2 :           gfc_error ("Data transfer element at %L cannot have "
   11867              :                      "procedure pointer components", &code->loc);
   11868            2 :           return;
   11869              :         }
   11870              : 
   11871          684 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11872              :         {
   11873            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11874              :                      "components unless it is processed by a defined "
   11875              :                      "input/output procedure", &code->loc);
   11876            6 :           return;
   11877              :         }
   11878              : 
   11879              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11880              :          be printed.  However, if -std=gnu and not -pedantic, allow
   11881              :          the component to be printed to help debugging.  */
   11882          678 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11883              :         {
   11884            4 :           gfc_error ("Data transfer element at %L "
   11885              :                      "cannot have PRIVATE components", &code->loc);
   11886            4 :             return;
   11887              :         }
   11888          674 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11889              :         {
   11890            4 :           gfc_error ("Data transfer element at %L cannot have "
   11891              :                      "PRIVATE components unless it is processed by "
   11892              :                      "a defined input/output procedure", &code->loc);
   11893            4 :           return;
   11894              :         }
   11895              :     }
   11896              : 
   11897        26284 :   if (exp->expr_type == EXPR_STRUCTURE)
   11898              :     return;
   11899              : 
   11900        26239 :   if (exp->expr_type == EXPR_ARRAY)
   11901              :     return;
   11902              : 
   11903        25857 :   sym = exp->symtree->n.sym;
   11904              : 
   11905        25857 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11906           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11907              :     {
   11908            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11909              :                  "an assumed-size array", &code->loc);
   11910            1 :       return;
   11911              :     }
   11912              : 
   11913              : }
   11914              : 
   11915              : 
   11916              : /*********** Toplevel code resolution subroutines ***********/
   11917              : 
   11918              : /* Find the set of labels that are reachable from this block.  We also
   11919              :    record the last statement in each block.  */
   11920              : 
   11921              : static void
   11922       689152 : find_reachable_labels (gfc_code *block)
   11923              : {
   11924       689152 :   gfc_code *c;
   11925              : 
   11926       689152 :   if (!block)
   11927              :     return;
   11928              : 
   11929       428631 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11930              : 
   11931              :   /* Collect labels in this block.  We don't keep those corresponding
   11932              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11933              :      up through the code_stack.  */
   11934      1571648 :   for (c = block; c; c = c->next)
   11935              :     {
   11936      1143017 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11937         3661 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11938              :     }
   11939              : 
   11940              :   /* Merge with labels from parent block.  */
   11941       428631 :   if (cs_base->prev)
   11942              :     {
   11943       351780 :       gcc_assert (cs_base->prev->reachable_labels);
   11944       351780 :       bitmap_ior_into (cs_base->reachable_labels,
   11945              :                        cs_base->prev->reachable_labels);
   11946              :     }
   11947              : }
   11948              : 
   11949              : static void
   11950          197 : resolve_lock_unlock_event (gfc_code *code)
   11951              : {
   11952          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11953          197 :       && (code->expr1->ts.type != BT_DERIVED
   11954          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   11955          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11956          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11957          136 :           || code->expr1->rank != 0
   11958          181 :           || (!gfc_is_coarray (code->expr1) &&
   11959           46 :               !gfc_is_coindexed (code->expr1))))
   11960            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11961            4 :                &code->expr1->where);
   11962          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11963           58 :            && (code->expr1->ts.type != BT_DERIVED
   11964           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   11965           58 :                || code->expr1->ts.u.derived->from_intmod
   11966              :                   != INTMOD_ISO_FORTRAN_ENV
   11967           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   11968              :                   != ISOFORTRAN_EVENT_TYPE
   11969           58 :                || code->expr1->rank != 0))
   11970            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11971              :                &code->expr1->where);
   11972           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11973          209 :            && !gfc_is_coindexed (code->expr1))
   11974            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11975            0 :                &code->expr1->where);
   11976          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11977            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11978            0 :                "coindexed", &code->expr1->where);
   11979              : 
   11980              :   /* Check STAT.  */
   11981          197 :   if (code->expr2
   11982           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11983           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11984            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11985              :                &code->expr2->where);
   11986              : 
   11987          197 :   if (code->expr2
   11988          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11989           54 :                                     _("STAT variable")))
   11990              :     return;
   11991              : 
   11992              :   /* Check ERRMSG.  */
   11993          197 :   if (code->expr3
   11994            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   11995            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   11996            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11997              :                &code->expr3->where);
   11998              : 
   11999          197 :   if (code->expr3
   12000          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   12001            2 :                                     _("ERRMSG variable")))
   12002              :     return;
   12003              : 
   12004              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   12005          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12006           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   12007           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   12008            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   12009              :                "variable", &code->expr4->where);
   12010              : 
   12011          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12012          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   12013           22 :                                     _("ACQUIRED_LOCK variable")))
   12014              :     return;
   12015              : 
   12016              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   12017          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   12018              :     {
   12019           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   12020           36 :           || code->expr4->rank != 0)
   12021            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   12022            0 :                    "expression", &code->expr4->where);
   12023              :     }
   12024              : }
   12025              : 
   12026              : static void
   12027          246 : resolve_team_argument (gfc_expr *team)
   12028              : {
   12029          246 :   gfc_resolve_expr (team);
   12030          246 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   12031          239 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   12032          239 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   12033              :     {
   12034            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   12035              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   12036              :                  &team->where);
   12037              :     }
   12038          246 : }
   12039              : 
   12040              : static void
   12041         1358 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   12042              :                                 gfc_expr *e)
   12043              : {
   12044         1358 :   gfc_resolve_expr (e);
   12045         1358 :   if (e
   12046          139 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   12047          124 :           || e->expr_type != EXPR_VARIABLE))
   12048           15 :     gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   12049              :                "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   12050              :                exp_kind);
   12051         1358 : }
   12052              : 
   12053              : void
   12054          679 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   12055              : {
   12056          679 :   resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
   12057          679 :   resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   12058              :                                   gfc_default_character_kind,
   12059              :                                   sync_stat->errmsg);
   12060          679 : }
   12061              : 
   12062              : static void
   12063          260 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   12064              :                          gfc_expr *e)
   12065              : {
   12066          260 :   gfc_resolve_expr (e);
   12067          260 :   if (e
   12068          161 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12069            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12070              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12071          260 : }
   12072              : 
   12073              : static void
   12074          130 : resolve_form_team (gfc_code *code)
   12075              : {
   12076          130 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12077              :                            code->expr1);
   12078          130 :   resolve_team_argument (code->expr2);
   12079          130 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12080              :                            code->expr3);
   12081          130 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12082          130 : }
   12083              : 
   12084              : static void resolve_block_construct (gfc_code *);
   12085              : 
   12086              : static void
   12087           73 : resolve_change_team (gfc_code *code)
   12088              : {
   12089           73 :   resolve_team_argument (code->expr1);
   12090           73 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12091          146 :   resolve_block_construct (code);
   12092              :   /* Map the coarray bounds as selected.  */
   12093           76 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12094            3 :     if (a->ar)
   12095              :       {
   12096            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12097            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12098            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12099              :         else
   12100            3 :           dst = a->st->n.sym->as;
   12101            3 :         dst->corank = src->corank;
   12102            3 :         dst->cotype = src->cotype;
   12103            6 :         for (int i = 0; i < src->corank; ++i)
   12104              :           {
   12105            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12106            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12107            3 :             src->lower[i] = src->upper[i] = nullptr;
   12108              :           }
   12109            3 :         gfc_free_array_spec (src);
   12110            3 :         free (a->ar);
   12111            3 :         a->ar = nullptr;
   12112            3 :         dst->resolved = false;
   12113            3 :         gfc_resolve_array_spec (dst, 0);
   12114              :       }
   12115           73 : }
   12116              : 
   12117              : static void
   12118           43 : resolve_sync_team (gfc_code *code)
   12119              : {
   12120           43 :   resolve_team_argument (code->expr1);
   12121           43 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12122           43 : }
   12123              : 
   12124              : static void
   12125           71 : resolve_end_team (gfc_code *code)
   12126              : {
   12127           71 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12128           71 : }
   12129              : 
   12130              : static void
   12131           54 : resolve_critical (gfc_code *code)
   12132              : {
   12133           54 :   gfc_symtree *symtree;
   12134           54 :   gfc_symbol *lock_type;
   12135           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12136           54 :   static int serial = 0;
   12137              : 
   12138           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12139              : 
   12140           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12141           30 :     return;
   12142              : 
   12143           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12144              :                               GFC_PREFIX ("lock_type"));
   12145           24 :   if (symtree)
   12146           12 :     lock_type = symtree->n.sym;
   12147              :   else
   12148              :     {
   12149           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12150              :                             false) != 0)
   12151            0 :         gcc_unreachable ();
   12152           12 :       lock_type = symtree->n.sym;
   12153           12 :       lock_type->attr.flavor = FL_DERIVED;
   12154           12 :       lock_type->attr.zero_comp = 1;
   12155           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12156           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12157              :     }
   12158              : 
   12159           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12160           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12161            0 :     gcc_unreachable ();
   12162              : 
   12163           24 :   code->resolved_sym = symtree->n.sym;
   12164           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12165           24 :   symtree->n.sym->attr.referenced = 1;
   12166           24 :   symtree->n.sym->attr.artificial = 1;
   12167           24 :   symtree->n.sym->attr.codimension = 1;
   12168           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12169           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12170           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12171           24 :   symtree->n.sym->as->corank = 1;
   12172           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12173           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12174           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12175              :                                                    NULL, 1);
   12176           24 :   gfc_commit_symbols();
   12177              : }
   12178              : 
   12179              : 
   12180              : static void
   12181         1317 : resolve_sync (gfc_code *code)
   12182              : {
   12183              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12184         1317 :   if (code->expr1)
   12185              :     {
   12186           71 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12187            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12188              :                    "INTEGER expression", &code->expr1->where);
   12189           71 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12190           27 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12191            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12192              :                    &code->expr1->where);
   12193           70 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12194           70 :                && gfc_simplify_expr (code->expr1, 0))
   12195              :         {
   12196           20 :            gfc_constructor *cons;
   12197           20 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12198           60 :            for (; cons; cons = gfc_constructor_next (cons))
   12199           20 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12200           20 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12201            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12202              :                           "num_images()", &cons->expr->where);
   12203              :         }
   12204              :     }
   12205              : 
   12206              :   /* Check STAT.  */
   12207         1317 :   gfc_resolve_expr (code->expr2);
   12208         1317 :   if (code->expr2)
   12209              :     {
   12210          108 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12211            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12212              :                    &code->expr2->where);
   12213              :       else
   12214          107 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12215          107 :                                   _("STAT variable"));
   12216              :     }
   12217              : 
   12218              :   /* Check ERRMSG.  */
   12219         1317 :   gfc_resolve_expr (code->expr3);
   12220         1317 :   if (code->expr3)
   12221              :     {
   12222           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12223            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12224              :                    &code->expr3->where);
   12225              :       else
   12226           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12227           86 :                                   _("ERRMSG variable"));
   12228              :     }
   12229         1317 : }
   12230              : 
   12231              : 
   12232              : /* Given a branch to a label, see if the branch is conforming.
   12233              :    The code node describes where the branch is located.  */
   12234              : 
   12235              : static void
   12236       111637 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12237              : {
   12238       111637 :   code_stack *stack;
   12239              : 
   12240       111637 :   if (label == NULL)
   12241              :     return;
   12242              : 
   12243              :   /* Step one: is this a valid branching target?  */
   12244              : 
   12245         2460 :   if (label->defined == ST_LABEL_UNKNOWN)
   12246              :     {
   12247            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12248              :                  &code->loc);
   12249            4 :       return;
   12250              :     }
   12251              : 
   12252         2456 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12253              :     {
   12254            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12255              :                  "for the branch statement at %L", &label->where, &code->loc);
   12256            4 :       return;
   12257              :     }
   12258              : 
   12259              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12260              : 
   12261         2452 :   if (code->here == label)
   12262              :     {
   12263            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12264              :                    &code->loc);
   12265            0 :       return;
   12266              :     }
   12267              : 
   12268              :   /* Step three:  See if the label is in the same block as the
   12269              :      branching statement.  The hard work has been done by setting up
   12270              :      the bitmap reachable_labels.  */
   12271              : 
   12272         2452 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12273              :     {
   12274              :       /* Check now whether there is a CRITICAL construct; if so, check
   12275              :          whether the label is still visible outside of the CRITICAL block,
   12276              :          which is invalid.  */
   12277         6267 :       for (stack = cs_base; stack; stack = stack->prev)
   12278              :         {
   12279         3883 :           if (stack->current->op == EXEC_CRITICAL
   12280         3883 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12281            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12282              :                       "label at %L", &code->loc, &label->where);
   12283         3881 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12284         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12285            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12286              :                       "for label at %L", &code->loc, &label->where);
   12287         3881 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12288         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12289            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12290              :                       "for label at %L", &code->loc, &label->where);
   12291              :         }
   12292              : 
   12293              :       return;
   12294              :     }
   12295              : 
   12296              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12297              :     still be the label of the END of the enclosing block, in which
   12298              :     case we find it by going up the code_stack.  */
   12299              : 
   12300          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12301              :     {
   12302          131 :       if (stack->current->next && stack->current->next->here == label)
   12303              :         break;
   12304          101 :       if (stack->current->op == EXEC_CRITICAL)
   12305              :         {
   12306              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12307              :              construct as END CRITICAL is still part of it.  */
   12308            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12309              :                       " at %L", &code->loc, &label->where);
   12310            2 :           return;
   12311              :         }
   12312           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12313              :         {
   12314            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12315              :                      "label at %L", &code->loc, &label->where);
   12316            0 :           return;
   12317              :         }
   12318              :     }
   12319              : 
   12320           66 :   if (stack)
   12321              :     {
   12322           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12323              :       return;
   12324              :     }
   12325              : 
   12326              :   /* The label is not in an enclosing block, so illegal.  This was
   12327              :      allowed in Fortran 66, so we allow it as extension.  No
   12328              :      further checks are necessary in this case.  */
   12329           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12330              :                   "as the GOTO statement at %L", &label->where,
   12331              :                   &code->loc);
   12332           36 :   return;
   12333              : }
   12334              : 
   12335              : 
   12336              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12337              : 
   12338              : static bool
   12339         1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12340              : {
   12341         1467 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12342         1467 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12343         1467 :   bool result = false;
   12344         1467 :   int i;
   12345              : 
   12346              :   /* Compare the rank.  */
   12347         1467 :   if (expr1->rank != expr2->rank)
   12348              :     return result;
   12349              : 
   12350              :   /* Compare the size of each dimension.  */
   12351         2811 :   for (i=0; i<expr1->rank; i++)
   12352              :     {
   12353         1495 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12354          151 :         goto ignore;
   12355              : 
   12356         1344 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12357            0 :         goto ignore;
   12358              : 
   12359         1344 :       if (mpz_cmp (shape[i], shape2[i]))
   12360            0 :         goto over;
   12361              :     }
   12362              : 
   12363              :   /* When either of the two expression is an assumed size array, we
   12364              :      ignore the comparison of dimension sizes.  */
   12365         1316 : ignore:
   12366              :   result = true;
   12367              : 
   12368         1467 : over:
   12369         1467 :   gfc_clear_shape (shape, i);
   12370         1467 :   gfc_clear_shape (shape2, i);
   12371         1467 :   return result;
   12372              : }
   12373              : 
   12374              : 
   12375              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12376              :    has the same shape as the outermost WHERE mask expression.  */
   12377              : 
   12378              : static void
   12379          509 : resolve_where (gfc_code *code, gfc_expr *mask)
   12380              : {
   12381          509 :   gfc_code *cblock;
   12382          509 :   gfc_code *cnext;
   12383          509 :   gfc_expr *e = NULL;
   12384              : 
   12385          509 :   cblock = code->block;
   12386              : 
   12387              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12388              :      In case of nested WHERE, only the outermost one is stored.  */
   12389          509 :   if (mask == NULL) /* outermost WHERE */
   12390          453 :     e = cblock->expr1;
   12391              :   else /* inner WHERE */
   12392          509 :     e = mask;
   12393              : 
   12394         1387 :   while (cblock)
   12395              :     {
   12396          878 :       if (cblock->expr1)
   12397              :         {
   12398              :           /* Check if the mask-expr has a consistent shape with the
   12399              :              outermost WHERE mask-expr.  */
   12400          714 :           if (!resolve_where_shape (cblock->expr1, e))
   12401            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12402            0 :                        &cblock->expr1->where);
   12403              :          }
   12404              : 
   12405              :       /* the assignment statement of a WHERE statement, or the first
   12406              :          statement in where-body-construct of a WHERE construct */
   12407          878 :       cnext = cblock->next;
   12408         1733 :       while (cnext)
   12409              :         {
   12410          855 :           switch (cnext->op)
   12411              :             {
   12412              :             /* WHERE assignment statement */
   12413          753 :             case EXEC_ASSIGN:
   12414              : 
   12415              :               /* Check shape consistent for WHERE assignment target.  */
   12416          753 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12417            0 :                gfc_error ("WHERE assignment target at %L has "
   12418            0 :                           "inconsistent shape", &cnext->expr1->where);
   12419              : 
   12420          753 :               if (cnext->op == EXEC_ASSIGN
   12421          753 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12422            0 :                 cnext->expr1->must_finalize = 1;
   12423              : 
   12424              :               break;
   12425              : 
   12426              : 
   12427           46 :             case EXEC_ASSIGN_CALL:
   12428           46 :               resolve_call (cnext);
   12429           46 :               if (!cnext->resolved_sym->attr.elemental)
   12430            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12431            2 :                           &cnext->ext.actual->expr->where);
   12432              :               break;
   12433              : 
   12434              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12435           56 :             case EXEC_WHERE:
   12436           56 :               resolve_where (cnext, e);
   12437           56 :               break;
   12438              : 
   12439            0 :             default:
   12440            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12441              :                          &cnext->loc);
   12442              :             }
   12443              :          /* the next statement within the same where-body-construct */
   12444          855 :          cnext = cnext->next;
   12445              :        }
   12446              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12447          878 :     cblock = cblock->block;
   12448              :   }
   12449          509 : }
   12450              : 
   12451              : 
   12452              : /* Resolve assignment in FORALL construct.
   12453              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12454              :    FORALL index variables.  */
   12455              : 
   12456              : static void
   12457         2376 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12458              : {
   12459         2376 :   int n;
   12460         2376 :   gfc_symbol *forall_index;
   12461              : 
   12462         6774 :   for (n = 0; n < nvar; n++)
   12463              :     {
   12464         4398 :       forall_index = var_expr[n]->symtree->n.sym;
   12465              : 
   12466              :       /* Check whether the assignment target is one of the FORALL index
   12467              :          variable.  */
   12468         4398 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12469         4398 :           && (code->expr1->symtree->n.sym == forall_index))
   12470            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12471              :                    &code->expr1->where);
   12472              :       else
   12473              :         {
   12474              :           /* If one of the FORALL index variables doesn't appear in the
   12475              :              assignment variable, then there could be a many-to-one
   12476              :              assignment.  Emit a warning rather than an error because the
   12477              :              mask could be resolving this problem.
   12478              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12479              :              many-to-one assignments are semantically valid (formalized with
   12480              :              the REDUCE locality-spec in Fortran 2023).  */
   12481         4398 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12482         4398 :               && !gfc_do_concurrent_flag)
   12483            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12484              :                          "left side of the assignment at %L and so might "
   12485              :                          "cause multiple assignment to this object",
   12486            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12487              :         }
   12488              :     }
   12489         2376 : }
   12490              : 
   12491              : 
   12492              : /* Resolve WHERE statement in FORALL construct.  */
   12493              : 
   12494              : static void
   12495           47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12496              :                                   gfc_expr **var_expr)
   12497              : {
   12498           47 :   gfc_code *cblock;
   12499           47 :   gfc_code *cnext;
   12500              : 
   12501           47 :   cblock = code->block;
   12502          113 :   while (cblock)
   12503              :     {
   12504              :       /* the assignment statement of a WHERE statement, or the first
   12505              :          statement in where-body-construct of a WHERE construct */
   12506           66 :       cnext = cblock->next;
   12507          132 :       while (cnext)
   12508              :         {
   12509           66 :           switch (cnext->op)
   12510              :             {
   12511              :             /* WHERE assignment statement */
   12512           66 :             case EXEC_ASSIGN:
   12513           66 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12514              : 
   12515           66 :               if (cnext->op == EXEC_ASSIGN
   12516           66 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12517            0 :                 cnext->expr1->must_finalize = 1;
   12518              : 
   12519              :               break;
   12520              : 
   12521              :             /* WHERE operator assignment statement */
   12522            0 :             case EXEC_ASSIGN_CALL:
   12523            0 :               resolve_call (cnext);
   12524            0 :               if (!cnext->resolved_sym->attr.elemental)
   12525            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12526            0 :                           &cnext->ext.actual->expr->where);
   12527              :               break;
   12528              : 
   12529              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12530            0 :             case EXEC_WHERE:
   12531            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12532            0 :               break;
   12533              : 
   12534            0 :             default:
   12535            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12536              :                          &cnext->loc);
   12537              :             }
   12538              :           /* the next statement within the same where-body-construct */
   12539           66 :           cnext = cnext->next;
   12540              :         }
   12541              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12542           66 :       cblock = cblock->block;
   12543              :     }
   12544           47 : }
   12545              : 
   12546              : 
   12547              : /* Traverse the FORALL body to check whether the following errors exist:
   12548              :    1. For assignment, check if a many-to-one assignment happens.
   12549              :    2. For WHERE statement, check the WHERE body to see if there is any
   12550              :       many-to-one assignment.  */
   12551              : 
   12552              : static void
   12553         2217 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12554              : {
   12555         2217 :   gfc_code *c;
   12556              : 
   12557         2217 :   c = code->block->next;
   12558         4856 :   while (c)
   12559              :     {
   12560         2639 :       switch (c->op)
   12561              :         {
   12562         2310 :         case EXEC_ASSIGN:
   12563         2310 :         case EXEC_POINTER_ASSIGN:
   12564         2310 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12565              : 
   12566         2310 :           if (c->op == EXEC_ASSIGN
   12567         2310 :               && gfc_may_be_finalized (c->expr1->ts))
   12568            0 :             c->expr1->must_finalize = 1;
   12569              : 
   12570              :           break;
   12571              : 
   12572            0 :         case EXEC_ASSIGN_CALL:
   12573            0 :           resolve_call (c);
   12574            0 :           break;
   12575              : 
   12576              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12577              :            there is no need to handle it here.  */
   12578              :         case EXEC_FORALL:
   12579              :           break;
   12580           47 :         case EXEC_WHERE:
   12581           47 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12582           47 :           break;
   12583              :         default:
   12584              :           break;
   12585              :         }
   12586              :       /* The next statement in the FORALL body.  */
   12587         2639 :       c = c->next;
   12588              :     }
   12589         2217 : }
   12590              : 
   12591              : 
   12592              : /* Counts the number of iterators needed inside a forall construct, including
   12593              :    nested forall constructs. This is used to allocate the needed memory
   12594              :    in gfc_resolve_forall.  */
   12595              : 
   12596              : static int gfc_count_forall_iterators (gfc_code *code);
   12597              : 
   12598              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12599              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12600              : 
   12601              : static int
   12602         2415 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12603              : {
   12604         2415 :   int max_iters = 0;
   12605              : 
   12606         5281 :   for (gfc_code *c = code; c; c = c->next)
   12607              :     {
   12608         2866 :       int sub_iters = 0;
   12609              : 
   12610         2866 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12611           94 :         sub_iters = gfc_count_forall_iterators (c);
   12612         2772 :       else if (c->op == EXEC_BLOCK)
   12613              :         {
   12614              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12615              :              not in the generic c->block arm list used by IF/SELECT.  */
   12616           34 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12617           34 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12618              :         }
   12619         2738 :       else if (c->block)
   12620          307 :         for (gfc_code *b = c->block; b; b = b->block)
   12621              :           {
   12622          164 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12623          164 :             if (arm_iters > sub_iters)
   12624              :               sub_iters = arm_iters;
   12625              :           }
   12626              : 
   12627         2866 :       if (sub_iters > max_iters)
   12628              :         max_iters = sub_iters;
   12629              :     }
   12630              : 
   12631         2415 :   return max_iters;
   12632              : }
   12633              : 
   12634              : 
   12635              : static int
   12636         2217 : gfc_count_forall_iterators (gfc_code *code)
   12637              : {
   12638         2217 :   int current_iters = 0;
   12639         2217 :   gfc_forall_iterator *fa;
   12640              : 
   12641         2217 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12642              : 
   12643         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12644         4135 :     current_iters++;
   12645              : 
   12646         2217 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   12647              : }
   12648              : 
   12649              : 
   12650              : /* Given a FORALL construct.
   12651              :    1) Resolve the FORALL iterator.
   12652              :    2) Check for shadow index-name(s) and update code block.
   12653              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   12654              : 
   12655              : /* Custom recursive expression walker that replaces symbols.
   12656              :    Visits all expressions including array subscripts.  Also called from
   12657              :    replace_in_code_recursive to handle ASSOCIATE selector expressions.  */
   12658              : 
   12659              : static void
   12660          192 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
   12661              : {
   12662          228 :   if (!expr)
   12663              :     return;
   12664              : 
   12665              :   /* Check if this is a variable reference to replace */
   12666          144 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   12667              :     {
   12668           30 :       expr->symtree = new_st;
   12669           30 :       expr->ts = new_st->n.sym->ts;
   12670              :     }
   12671              : 
   12672              :   /* Walk through reference chain (array subscripts, substrings, etc.) */
   12673          150 :   for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
   12674              :     {
   12675            6 :       if (ref->type == REF_ARRAY)
   12676              :         {
   12677              :           gfc_array_ref *ar = &ref->u.ar;
   12678           12 :           for (int i = 0; i < ar->dimen; i++)
   12679              :             {
   12680            6 :               replace_in_expr_recursive (ar->start[i], old_sym, new_st);
   12681            6 :               replace_in_expr_recursive (ar->end[i], old_sym, new_st);
   12682            6 :               replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
   12683              :             }
   12684              :         }
   12685            0 :       else if (ref->type == REF_SUBSTRING)
   12686              :         {
   12687            0 :           replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
   12688            0 :           replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
   12689              :         }
   12690              :     }
   12691              : 
   12692              :   /* Walk through sub-expressions based on expression type */
   12693          144 :   switch (expr->expr_type)
   12694              :     {
   12695           36 :     case EXPR_OP:
   12696           36 :       replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
   12697           36 :       replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
   12698           36 :       break;
   12699              : 
   12700            6 :     case EXPR_FUNCTION:
   12701           18 :       for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
   12702           12 :         replace_in_expr_recursive (a->expr, old_sym, new_st);
   12703              :       break;
   12704              : 
   12705            0 :     case EXPR_ARRAY:
   12706            0 :     case EXPR_STRUCTURE:
   12707            0 :       for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   12708            0 :            c; c = gfc_constructor_next (c))
   12709              :         {
   12710            0 :           replace_in_expr_recursive (c->expr, old_sym, new_st);
   12711            0 :           if (c->iterator)
   12712              :             {
   12713            0 :               replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
   12714            0 :               replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
   12715            0 :               replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
   12716              :             }
   12717              :         }
   12718              :       break;
   12719              : 
   12720              :     default:
   12721              :       break;
   12722              :     }
   12723              : }
   12724              : 
   12725              : 
   12726              : /* Walk code tree and replace all variable references */
   12727              : 
   12728              : static void
   12729           30 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   12730              : {
   12731           30 :   if (!code)
   12732              :     return;
   12733              : 
   12734           60 :   for (gfc_code *c = code; c; c = c->next)
   12735              :     {
   12736              :       /* Replace in expressions associated with this code node */
   12737           30 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   12738           30 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   12739           30 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   12740           30 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   12741              : 
   12742              :       /* Handle special code types with additional expressions */
   12743           30 :       switch (c->op)
   12744              :         {
   12745            0 :         case EXEC_DO:
   12746            0 :           if (c->ext.iterator)
   12747              :             {
   12748            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   12749            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   12750            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   12751              :             }
   12752              :           break;
   12753              : 
   12754            0 :         case EXEC_CALL:
   12755            0 :         case EXEC_ASSIGN_CALL:
   12756            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   12757            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   12758              :           break;
   12759              : 
   12760            0 :         case EXEC_SELECT:
   12761            0 :           for (gfc_code *b = c->block; b; b = b->block)
   12762              :             {
   12763            0 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   12764              :                 {
   12765            0 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   12766            0 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   12767              :                 }
   12768            0 :               replace_in_code_recursive (b->next, old_sym, new_st);
   12769              :             }
   12770              :           break;
   12771              : 
   12772            0 :         case EXEC_FORALL:
   12773            0 :         case EXEC_DO_CONCURRENT:
   12774            0 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   12775              :             {
   12776            0 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   12777            0 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   12778            0 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   12779              :             }
   12780              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   12781              :              they'll be handled separately */
   12782              :           break;
   12783              : 
   12784            6 :         case EXEC_BLOCK:
   12785              :           /* Replace in ASSOCIATE selector expressions and the body.
   12786              :              The body of an EXEC_BLOCK lives in c->ext.block.ns->code, not
   12787              :              c->block->next, so without this case both selectors and body
   12788              :              are silently skipped, leaving shadow iterator references unreplaced
   12789              :              and producing wrong values at runtime.  */
   12790            6 :           for (gfc_association_list *alist = c->ext.block.assoc;
   12791           12 :                alist; alist = alist->next)
   12792            6 :             replace_in_expr_recursive (alist->target, old_sym, new_st);
   12793            6 :           if (c->ext.block.ns)
   12794            6 :             replace_in_code_recursive (c->ext.block.ns->code, old_sym, new_st);
   12795              :           break;
   12796              : 
   12797              :         default:
   12798              :           break;
   12799              :         }
   12800              : 
   12801              :       /* Recurse into blocks */
   12802           30 :       if (c->block)
   12803            0 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   12804              :     }
   12805              : }
   12806              : 
   12807              : 
   12808              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   12809              : 
   12810              : static void
   12811           24 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   12812              :                               gfc_symtree *shadow_st)
   12813              : {
   12814              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   12815            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   12816           24 : }
   12817              : 
   12818              : 
   12819              : static void
   12820         2217 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   12821              : {
   12822         2217 :   static gfc_expr **var_expr;
   12823         2217 :   static int total_var = 0;
   12824         2217 :   static int nvar = 0;
   12825         2217 :   int i, old_nvar, tmp;
   12826         2217 :   gfc_forall_iterator *fa;
   12827         2217 :   bool shadow = false;
   12828              : 
   12829         2217 :   old_nvar = nvar;
   12830              : 
   12831              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   12832         2217 :   if (code->op == EXEC_FORALL
   12833         2217 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   12834              :     return;
   12835              : 
   12836              :   /* Start to resolve a FORALL construct   */
   12837              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   12838              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   12839              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   12840              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   12841         2217 :   if (forall_save == 0 && nvar == 0)
   12842              :     {
   12843              :       /* Count the total number of FORALL indices in the nested FORALL
   12844              :          construct in order to allocate the VAR_EXPR with proper size.  */
   12845         2123 :       total_var = gfc_count_forall_iterators (code);
   12846              : 
   12847              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12848         2123 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12849              :     }
   12850              : 
   12851              :   /* The information about FORALL iterator, including FORALL indices start,
   12852              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   12853              :      stride.  Check for a shadow index-name.  */
   12854         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12855              :     {
   12856              :       /* Fortran 2008: C738 (R753).  */
   12857         4135 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12858              :         {
   12859            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12860              :                      "of type integer", &fa->var->where);
   12861            2 :           continue;
   12862              :         }
   12863              : 
   12864              :       /* Check if any outer FORALL index name is the same as the current
   12865              :          one.  Skip this check if the iterator is a shadow variable (from
   12866              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   12867         7144 :       for (i = 0; i < nvar; i++)
   12868              :         {
   12869         3011 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   12870         3011 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12871            0 :             gfc_error ("An outer FORALL construct already has an index "
   12872              :                         "with this name %L", &fa->var->where);
   12873              :         }
   12874              : 
   12875         4133 :       if (fa->shadow)
   12876           24 :         shadow = true;
   12877              : 
   12878              :       /* Record the current FORALL index.  */
   12879         4133 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12880              : 
   12881         4133 :       nvar++;
   12882              : 
   12883              :       /* No memory leak.  */
   12884         4133 :       gcc_assert (nvar <= total_var);
   12885              :     }
   12886              : 
   12887              :   /* Need to walk the code and replace references to the index-name with
   12888              :      references to the shadow index-name. This must be done BEFORE resolving
   12889              :      the body so that resolution uses the correct shadow variables.  */
   12890         2217 :   if (shadow)
   12891              :     {
   12892              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   12893           54 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12894              :         {
   12895           30 :           if (fa->shadow)
   12896              :             {
   12897           24 :               gfc_symtree *shadow_st;
   12898           24 :               const char *shadow_name_str;
   12899           24 :               char *outer_name;
   12900              : 
   12901              :               /* fa->var now points to the shadow variable "_name".  */
   12902           24 :               shadow_name_str = fa->var->symtree->name;
   12903           24 :               shadow_st = fa->var->symtree;
   12904              : 
   12905           24 :               if (shadow_name_str[0] != '_')
   12906            0 :                 gfc_internal_error ("Expected shadow variable name to start with _");
   12907              : 
   12908           24 :               outer_name = (char *) alloca (strlen (shadow_name_str));
   12909           24 :               strcpy (outer_name, shadow_name_str + 1);
   12910              : 
   12911              :               /* Find the ITERATOR symbol in the current namespace.
   12912              :                  This is the local DO CONCURRENT variable that body expressions reference.  */
   12913           24 :               gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
   12914              : 
   12915           24 :               if (!iter_st)
   12916              :                 /* No iterator variable found - this shouldn't happen */
   12917            0 :                 continue;
   12918              : 
   12919           24 :               gfc_symbol *iter_sym = iter_st->n.sym;
   12920              : 
   12921              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   12922           24 :               if (code->block && code->block->next)
   12923           24 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   12924              :             }
   12925              :         }
   12926              :     }
   12927              : 
   12928              :   /* Resolve the FORALL body.  */
   12929         2217 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12930              : 
   12931              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12932         2217 :   gfc_resolve_blocks (code->block, ns);
   12933              : 
   12934         2217 :   tmp = nvar;
   12935         2217 :   nvar = old_nvar;
   12936              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12937         6350 :   for (i = nvar; i < tmp; i++)
   12938         4133 :      gfc_free_expr (var_expr[i]);
   12939              : 
   12940         2217 :   if (nvar == 0)
   12941              :     {
   12942              :       /* We are in the outermost FORALL construct.  */
   12943         2123 :       gcc_assert (forall_save == 0);
   12944              : 
   12945              :       /* VAR_EXPR is not needed any more.  */
   12946         2123 :       free (var_expr);
   12947         2123 :       total_var = 0;
   12948              :     }
   12949              : }
   12950              : 
   12951              : 
   12952              : /* Resolve a BLOCK construct statement.  */
   12953              : 
   12954              : static void
   12955         8215 : resolve_block_construct (gfc_code* code)
   12956              : {
   12957         8215 :   gfc_namespace *ns = code->ext.block.ns;
   12958              : 
   12959              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12960              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12961              :      namespace.  */
   12962         8215 :   gfc_resolve (ns);
   12963            0 : }
   12964              : 
   12965              : 
   12966              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12967              :    DO code nodes.  */
   12968              : 
   12969              : void
   12970       334159 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12971              : {
   12972       334159 :   bool t;
   12973              : 
   12974       679850 :   for (; b; b = b->block)
   12975              :     {
   12976       345691 :       t = gfc_resolve_expr (b->expr1);
   12977       345691 :       if (!gfc_resolve_expr (b->expr2))
   12978            0 :         t = false;
   12979              : 
   12980       345691 :       switch (b->op)
   12981              :         {
   12982       238425 :         case EXEC_IF:
   12983       238425 :           if (t && b->expr1 != NULL
   12984       234102 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   12985            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   12986              :                        &b->expr1->where);
   12987              :           break;
   12988              : 
   12989          764 :         case EXEC_WHERE:
   12990          764 :           if (t
   12991          764 :               && b->expr1 != NULL
   12992          631 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   12993            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   12994              :                        &b->expr1->where);
   12995              :           break;
   12996              : 
   12997           76 :         case EXEC_GOTO:
   12998           76 :           resolve_branch (b->label1, b);
   12999           76 :           break;
   13000              : 
   13001            0 :         case EXEC_BLOCK:
   13002            0 :           resolve_block_construct (b);
   13003            0 :           break;
   13004              : 
   13005              :         case EXEC_SELECT:
   13006              :         case EXEC_SELECT_TYPE:
   13007              :         case EXEC_SELECT_RANK:
   13008              :         case EXEC_FORALL:
   13009              :         case EXEC_DO:
   13010              :         case EXEC_DO_WHILE:
   13011              :         case EXEC_DO_CONCURRENT:
   13012              :         case EXEC_CRITICAL:
   13013              :         case EXEC_READ:
   13014              :         case EXEC_WRITE:
   13015              :         case EXEC_IOLENGTH:
   13016              :         case EXEC_WAIT:
   13017              :           break;
   13018              : 
   13019         2697 :         case EXEC_OMP_ATOMIC:
   13020         2697 :         case EXEC_OACC_ATOMIC:
   13021         2697 :           {
   13022              :             /* Verify this before calling gfc_resolve_code, which might
   13023              :                change it.  */
   13024         2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   13025              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   13026              :           }
   13027              :           break;
   13028              : 
   13029              :         case EXEC_OACC_PARALLEL_LOOP:
   13030              :         case EXEC_OACC_PARALLEL:
   13031              :         case EXEC_OACC_KERNELS_LOOP:
   13032              :         case EXEC_OACC_KERNELS:
   13033              :         case EXEC_OACC_SERIAL_LOOP:
   13034              :         case EXEC_OACC_SERIAL:
   13035              :         case EXEC_OACC_DATA:
   13036              :         case EXEC_OACC_HOST_DATA:
   13037              :         case EXEC_OACC_LOOP:
   13038              :         case EXEC_OACC_UPDATE:
   13039              :         case EXEC_OACC_WAIT:
   13040              :         case EXEC_OACC_CACHE:
   13041              :         case EXEC_OACC_ENTER_DATA:
   13042              :         case EXEC_OACC_EXIT_DATA:
   13043              :         case EXEC_OACC_ROUTINE:
   13044              :         case EXEC_OMP_ALLOCATE:
   13045              :         case EXEC_OMP_ALLOCATORS:
   13046              :         case EXEC_OMP_ASSUME:
   13047              :         case EXEC_OMP_CRITICAL:
   13048              :         case EXEC_OMP_DISPATCH:
   13049              :         case EXEC_OMP_DISTRIBUTE:
   13050              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13051              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13052              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13053              :         case EXEC_OMP_DO:
   13054              :         case EXEC_OMP_DO_SIMD:
   13055              :         case EXEC_OMP_ERROR:
   13056              :         case EXEC_OMP_LOOP:
   13057              :         case EXEC_OMP_MASKED:
   13058              :         case EXEC_OMP_MASKED_TASKLOOP:
   13059              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13060              :         case EXEC_OMP_MASTER:
   13061              :         case EXEC_OMP_MASTER_TASKLOOP:
   13062              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13063              :         case EXEC_OMP_ORDERED:
   13064              :         case EXEC_OMP_PARALLEL:
   13065              :         case EXEC_OMP_PARALLEL_DO:
   13066              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13067              :         case EXEC_OMP_PARALLEL_LOOP:
   13068              :         case EXEC_OMP_PARALLEL_MASKED:
   13069              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13070              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13071              :         case EXEC_OMP_PARALLEL_MASTER:
   13072              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13073              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13074              :         case EXEC_OMP_PARALLEL_SECTIONS:
   13075              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13076              :         case EXEC_OMP_SECTIONS:
   13077              :         case EXEC_OMP_SIMD:
   13078              :         case EXEC_OMP_SCOPE:
   13079              :         case EXEC_OMP_SINGLE:
   13080              :         case EXEC_OMP_TARGET:
   13081              :         case EXEC_OMP_TARGET_DATA:
   13082              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13083              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13084              :         case EXEC_OMP_TARGET_PARALLEL:
   13085              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13086              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13087              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13088              :         case EXEC_OMP_TARGET_SIMD:
   13089              :         case EXEC_OMP_TARGET_TEAMS:
   13090              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13091              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13092              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13093              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13094              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13095              :         case EXEC_OMP_TARGET_UPDATE:
   13096              :         case EXEC_OMP_TASK:
   13097              :         case EXEC_OMP_TASKGROUP:
   13098              :         case EXEC_OMP_TASKLOOP:
   13099              :         case EXEC_OMP_TASKLOOP_SIMD:
   13100              :         case EXEC_OMP_TASKWAIT:
   13101              :         case EXEC_OMP_TASKYIELD:
   13102              :         case EXEC_OMP_TEAMS:
   13103              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13104              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13105              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13106              :         case EXEC_OMP_TEAMS_LOOP:
   13107              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13108              :         case EXEC_OMP_TILE:
   13109              :         case EXEC_OMP_UNROLL:
   13110              :         case EXEC_OMP_WORKSHARE:
   13111              :           break;
   13112              : 
   13113            0 :         default:
   13114            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13115              :         }
   13116       345691 :       gfc_value_used_expr (b->expr1, VALUE_USED);
   13117       345691 :       gfc_value_used_expr (b->expr2, VALUE_USED);
   13118       345691 :       gfc_resolve_code (b->next, ns);
   13119              :     }
   13120       334159 : }
   13121              : 
   13122              : bool
   13123            0 : caf_possible_reallocate (gfc_expr *e)
   13124              : {
   13125            0 :   symbol_attribute caf_attr;
   13126            0 :   gfc_ref *last_arr_ref = nullptr;
   13127              : 
   13128            0 :   caf_attr = gfc_caf_attr (e);
   13129            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13130              :     return false;
   13131              : 
   13132              :   /* Only full array refs can indicate a needed reallocation.  */
   13133            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13134            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13135            0 :       last_arr_ref = ref;
   13136              : 
   13137            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13138              : }
   13139              : 
   13140              : /* Does everything to resolve an ordinary assignment.  Returns true
   13141              :    if this is an interface assignment.  */
   13142              : static bool
   13143       287261 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13144              : {
   13145       287261 :   bool rval = false;
   13146       287261 :   gfc_expr *lhs;
   13147       287261 :   gfc_expr *rhs;
   13148       287261 :   int n;
   13149       287261 :   gfc_ref *ref;
   13150       287261 :   symbol_attribute attr;
   13151              : 
   13152       287261 :   if (gfc_extend_assign (code, ns))
   13153              :     {
   13154          918 :       gfc_expr** rhsptr;
   13155              : 
   13156          918 :       if (code->op == EXEC_ASSIGN_CALL)
   13157              :         {
   13158          469 :           lhs = code->ext.actual->expr;
   13159          469 :           rhsptr = &code->ext.actual->next->expr;
   13160              :         }
   13161              :       else
   13162              :         {
   13163          449 :           gfc_actual_arglist* args;
   13164          449 :           gfc_typebound_proc* tbp;
   13165              : 
   13166          449 :           gcc_assert (code->op == EXEC_COMPCALL);
   13167              : 
   13168          449 :           args = code->expr1->value.compcall.actual;
   13169          449 :           lhs = args->expr;
   13170          449 :           rhsptr = &args->next->expr;
   13171              : 
   13172          449 :           tbp = code->expr1->value.compcall.tbp;
   13173          449 :           gcc_assert (!tbp->is_generic);
   13174              :         }
   13175              : 
   13176              :       /* Make a temporary rhs when there is a default initializer
   13177              :          and rhs is the same symbol as the lhs.  */
   13178          918 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13179          507 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13180          436 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13181         1206 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13182           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13183              : 
   13184          918 :       return true;
   13185              :     }
   13186              : 
   13187       286343 :   lhs = code->expr1;
   13188       286343 :   rhs = code->expr2;
   13189              : 
   13190       286343 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13191       266021 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13192        22952 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13193       309295 :       && gfc_expr_attr (lhs).proc_pointer)
   13194              :     {
   13195            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13196              :                  "pointer component",
   13197              :                  &lhs->where);
   13198            1 :       return false;
   13199              :     }
   13200              : 
   13201       337459 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13202       250638 :       && rhs->ts.type == BT_CHARACTER
   13203       286735 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13204              :     {
   13205              :       /* Use of -fdec-char-conversions allows assignment of character data
   13206              :          to non-character variables.  This not permitted for nonconstant
   13207              :          strings.  */
   13208           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13209              :                  gfc_typename (lhs), &rhs->where);
   13210           29 :       return false;
   13211              :     }
   13212              : 
   13213       286313 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13214              :     {
   13215            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13216              :                    gfc_typename (lhs), &rhs->where);
   13217            0 :       return false;
   13218              :     }
   13219              : 
   13220              :   /* Handle the case of a BOZ literal on the RHS.  */
   13221       286313 :   if (rhs->ts.type == BT_BOZ)
   13222              :     {
   13223            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13224              :                            "statement value nor an actual argument of "
   13225              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13226              :                            &rhs->where))
   13227              :         return false;
   13228              : 
   13229            1 :       switch (lhs->ts.type)
   13230              :         {
   13231            0 :         case BT_INTEGER:
   13232            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13233              :             return false;
   13234              :           break;
   13235            1 :         case BT_REAL:
   13236            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13237              :             return false;
   13238              :           break;
   13239            0 :         default:
   13240            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13241            0 :           return false;
   13242              :         }
   13243              :     }
   13244              : 
   13245       286311 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13246              :     {
   13247           67 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13248           67 :       if (lhs->ts.u.cl != NULL
   13249           67 :             && lhs->ts.u.cl->length != NULL
   13250           56 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13251           56 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13252              : 
   13253           67 :       if (rhs->expr_type == EXPR_CONSTANT)
   13254           29 :         rlen = rhs->value.character.length;
   13255              : 
   13256           38 :       else if (rhs->ts.u.cl != NULL
   13257           38 :                  && rhs->ts.u.cl->length != NULL
   13258           35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13259           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13260              : 
   13261           67 :       if (rlen && llen && rlen > llen)
   13262           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13263              :                          "CHARACTER expression will be truncated "
   13264              :                          "in assignment (%wd/%wd) at %L",
   13265              :                          llen, rlen, &code->loc);
   13266              :     }
   13267              : 
   13268              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13269              :      to a temporary if the lvalue symbol is referenced in it.  */
   13270       286311 :   if (lhs->rank)
   13271              :     {
   13272       113375 :       for (ref = lhs->ref; ref; ref= ref->next)
   13273        60587 :         if (ref->type == REF_ARRAY)
   13274              :           {
   13275       133512 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13276        78916 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13277        79146 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13278          230 :                                            ref->u.ar.start[n]))
   13279           14 :                 ref->u.ar.start[n]
   13280           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13281              :           }
   13282              :     }
   13283              : 
   13284       286311 :   if (gfc_pure (NULL))
   13285              :     {
   13286         3543 :       if (lhs->ts.type == BT_DERIVED
   13287          136 :             && lhs->expr_type == EXPR_VARIABLE
   13288          136 :             && lhs->ts.u.derived->attr.pointer_comp
   13289            4 :             && rhs->expr_type == EXPR_VARIABLE
   13290         3546 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13291            2 :                 || gfc_is_coindexed (rhs)))
   13292              :         {
   13293              :           /* F2008, C1283.  */
   13294            2 :           if (gfc_is_coindexed (rhs))
   13295            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13296              :                         "a derived type variable with a POINTER "
   13297              :                         "component in a PURE procedure",
   13298              :                         &rhs->where);
   13299              :           else
   13300              :           /* F2008, C1283 (4).  */
   13301            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13302              :                         "shall not be used as the expr at %L of an intrinsic "
   13303              :                         "assignment statement in which the variable is of a "
   13304              :                         "derived type if the derived type has a pointer "
   13305              :                         "component at any level of component selection.",
   13306              :                         &rhs->where);
   13307            2 :           return rval;
   13308              :         }
   13309              : 
   13310              :       /* Fortran 2008, C1283.  */
   13311         3541 :       if (gfc_is_coindexed (lhs))
   13312              :         {
   13313            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13314              :                      "procedure", &rhs->where);
   13315            1 :           return rval;
   13316              :         }
   13317              :     }
   13318              : 
   13319       286308 :   if (gfc_implicit_pure (NULL))
   13320              :     {
   13321         7359 :       if (lhs->expr_type == EXPR_VARIABLE
   13322         7359 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13323         5244 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13324          256 :         gfc_unset_implicit_pure (NULL);
   13325              : 
   13326         7359 :       if (lhs->ts.type == BT_DERIVED
   13327          353 :             && lhs->expr_type == EXPR_VARIABLE
   13328          353 :             && lhs->ts.u.derived->attr.pointer_comp
   13329            7 :             && rhs->expr_type == EXPR_VARIABLE
   13330         7366 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13331            7 :                 || gfc_is_coindexed (rhs)))
   13332            0 :         gfc_unset_implicit_pure (NULL);
   13333              : 
   13334              :       /* Fortran 2008, C1283.  */
   13335         7359 :       if (gfc_is_coindexed (lhs))
   13336            0 :         gfc_unset_implicit_pure (NULL);
   13337              :     }
   13338              : 
   13339              :   /* F2008, 7.2.1.2.  */
   13340       286308 :   attr = gfc_expr_attr (lhs);
   13341       286308 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13342              :     {
   13343          987 :       if (attr.codimension)
   13344              :         {
   13345            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13346              :                      "permitted", &lhs->where);
   13347            1 :           return false;
   13348              :         }
   13349          986 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13350              :                            "polymorphic variable at %L", &lhs->where))
   13351              :         return false;
   13352          985 :       if (!flag_realloc_lhs)
   13353              :         {
   13354            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13355              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13356            1 :           return false;
   13357              :         }
   13358              :     }
   13359       285321 :   else if (lhs->ts.type == BT_CLASS)
   13360              :     {
   13361            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13362              :                  "assignment at %L - check that there is a matching specific "
   13363              :                  "subroutine for %<=%> operator", &lhs->where);
   13364            9 :       return false;
   13365              :     }
   13366              : 
   13367       286296 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13368              : 
   13369              :   /* F2008, Section 7.2.1.2.  */
   13370       286296 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13371              :     {
   13372            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13373              :                  "component in assignment at %L", &lhs->where);
   13374            1 :       return false;
   13375              :     }
   13376              : 
   13377              :   /* Assign the 'data' of a class object to a derived type.  */
   13378       286295 :   if (lhs->ts.type == BT_DERIVED
   13379         7335 :       && rhs->ts.type == BT_CLASS
   13380          168 :       && (rhs->expr_type != EXPR_ARRAY
   13381          162 :           && rhs->expr_type != EXPR_OP))
   13382          156 :     gfc_add_data_component (rhs);
   13383              : 
   13384              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13385              :      rhs type.  */
   13386       286295 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13387          615 :     gfc_find_vtab (&rhs->ts);
   13388              : 
   13389       286295 :   gfc_check_assign (lhs, rhs, 1);
   13390              : 
   13391       286295 :   return false;
   13392              : }
   13393              : 
   13394              : 
   13395              : /* Add a component reference onto an expression.  */
   13396              : 
   13397              : static void
   13398          665 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13399              : {
   13400          665 :   gfc_ref **ref;
   13401          665 :   ref = &(e->ref);
   13402          889 :   while (*ref)
   13403          224 :     ref = &((*ref)->next);
   13404          665 :   *ref = gfc_get_ref ();
   13405          665 :   (*ref)->type = REF_COMPONENT;
   13406          665 :   (*ref)->u.c.sym = e->ts.u.derived;
   13407          665 :   (*ref)->u.c.component = c;
   13408          665 :   e->ts = c->ts;
   13409              : 
   13410              :   /* Add a full array ref, as necessary.  */
   13411          665 :   if (c->as)
   13412              :     {
   13413           84 :       gfc_add_full_array_ref (e, c->as);
   13414           84 :       e->rank = c->as->rank;
   13415           84 :       e->corank = c->as->corank;
   13416              :     }
   13417          665 : }
   13418              : 
   13419              : 
   13420              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13421              :    pointer assignments can be made.  */
   13422              : 
   13423              : static gfc_code *
   13424          988 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13425              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13426              : {
   13427          988 :   gfc_code *this_code;
   13428              : 
   13429          988 :   this_code = gfc_get_code (op);
   13430          988 :   this_code->next = NULL;
   13431          988 :   this_code->expr1 = gfc_copy_expr (expr1);
   13432          988 :   this_code->expr2 = gfc_copy_expr (expr2);
   13433          988 :   this_code->loc = loc;
   13434          988 :   if (comp1 && comp2)
   13435              :     {
   13436          288 :       add_comp_ref (this_code->expr1, comp1);
   13437          288 :       add_comp_ref (this_code->expr2, comp2);
   13438              :     }
   13439              : 
   13440          988 :   return this_code;
   13441              : }
   13442              : 
   13443              : 
   13444              : /* Makes a temporary variable expression based on the characteristics of
   13445              :    a given variable expression.  If allocatable is set, the temporary is
   13446              :    unconditionally allocatable*/
   13447              : 
   13448              : static gfc_expr*
   13449          482 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13450              :                     bool allocatable = false)
   13451              : {
   13452          482 :   static int serial = 0;
   13453          482 :   char name[GFC_MAX_SYMBOL_LEN];
   13454          482 :   gfc_symtree *tmp;
   13455          482 :   gfc_array_spec *as;
   13456          482 :   gfc_array_ref *aref;
   13457          482 :   gfc_ref *ref;
   13458              : 
   13459          482 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13460          482 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13461          482 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13462              : 
   13463          482 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13464            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13465              :                                                     NULL,
   13466            0 :                                                     e->value.character.length);
   13467              : 
   13468          482 :   as = NULL;
   13469          482 :   ref = NULL;
   13470          482 :   aref = NULL;
   13471              : 
   13472              :   /* Obtain the arrayspec for the temporary.  */
   13473          482 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13474              :        && e->expr_type != EXPR_FUNCTION
   13475              :        && e->expr_type != EXPR_OP)
   13476              :     {
   13477           52 :       aref = gfc_find_array_ref (e);
   13478           52 :       if (e->expr_type == EXPR_VARIABLE
   13479           52 :           && e->symtree->n.sym->as == aref->as)
   13480              :         as = aref->as;
   13481              :       else
   13482              :         {
   13483            0 :           for (ref = e->ref; ref; ref = ref->next)
   13484            0 :             if (ref->type == REF_COMPONENT
   13485            0 :                 && ref->u.c.component->as == aref->as)
   13486              :               {
   13487              :                 as = aref->as;
   13488              :                 break;
   13489              :               }
   13490              :         }
   13491              :     }
   13492              : 
   13493              :   /* Add the attributes and the arrayspec to the temporary.  */
   13494          482 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13495          482 :   tmp->n.sym->attr.function = 0;
   13496          482 :   tmp->n.sym->attr.proc_pointer = 0;
   13497          482 :   tmp->n.sym->attr.result = 0;
   13498          482 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13499          482 :   tmp->n.sym->attr.dummy = 0;
   13500          482 :   tmp->n.sym->attr.use_assoc = 0;
   13501          482 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13502              : 
   13503              : 
   13504          482 :   if (as && !allocatable)
   13505              :     {
   13506           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13507           52 :       if (!ref)
   13508           52 :         ref = e->ref;
   13509           52 :       if (as->type == AS_DEFERRED)
   13510           46 :         tmp->n.sym->attr.allocatable = 1;
   13511              :     }
   13512          430 :   else if ((e->rank || e->corank)
   13513          130 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13514           24 :                || e->expr_type == EXPR_OP || allocatable))
   13515              :     {
   13516          130 :       tmp->n.sym->as = gfc_get_array_spec ();
   13517          130 :       tmp->n.sym->as->type = AS_DEFERRED;
   13518          130 :       tmp->n.sym->as->rank = e->rank;
   13519          130 :       tmp->n.sym->as->corank = e->corank;
   13520          130 :       tmp->n.sym->attr.allocatable = 1;
   13521          130 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13522          260 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13523              :     }
   13524              :   else
   13525          300 :     tmp->n.sym->attr.dimension = 0;
   13526              : 
   13527          482 :   gfc_set_sym_referenced (tmp->n.sym);
   13528          482 :   gfc_commit_symbol (tmp->n.sym);
   13529          482 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13530              : 
   13531              :   /* Should the lhs be a section, use its array ref for the
   13532              :      temporary expression.  */
   13533          482 :   if (aref && aref->type != AR_FULL && !allocatable)
   13534              :     {
   13535            6 :       gfc_free_ref_list (e->ref);
   13536            6 :       e->ref = gfc_copy_ref (ref);
   13537              :     }
   13538          482 :   return e;
   13539              : }
   13540              : 
   13541              : 
   13542              : /* Helper function to take an argument in a subroutine call with a dependency
   13543              :    on another argument, copy it to an allocatable temporary and use the
   13544              :    temporary in the call expression. The new code is embedded in a block to
   13545              :    ensure local, automatic deallocation.  */
   13546              : 
   13547              : static void
   13548           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13549              :                              gfc_expr **rhsptr)
   13550              : {
   13551           36 :   gfc_namespace *block_ns;
   13552           36 :   gfc_expr *tmp_var;
   13553              : 
   13554              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13555           36 :   block_ns = gfc_build_block_ns (ns);
   13556              : 
   13557              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13558              :      the assignment would be converted to a call and, in any case, the modified
   13559              :      call fails in gfc_check_conformance.  */
   13560           36 :   block_ns->resolved = 1;
   13561              : 
   13562              :   /* Assign the original expression to the temporary.  */
   13563           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13564           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13565           36 :                                      NULL, NULL, (*rhsptr)->where);
   13566              : 
   13567              :   /* Transfer the call to the block and terminate block code.  */
   13568           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13569           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13570           36 :   *(block_ns->code->next) = *code;
   13571           36 :   block_ns->code->next->next = NULL;
   13572              : 
   13573              :   /* Convert the original code to execute the block.  */
   13574           36 :   code->op = EXEC_BLOCK;
   13575           36 :   code->ext.block.ns = block_ns;
   13576           36 :   code->ext.block.assoc = NULL;
   13577           36 :   code->expr1 = code->expr2 = NULL;
   13578           36 : }
   13579              : 
   13580              : 
   13581              : /* Add one line of code to the code chain, making sure that 'head' and
   13582              :    'tail' are appropriately updated.  */
   13583              : 
   13584              : static void
   13585          656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13586              : {
   13587          656 :   gcc_assert (this_code);
   13588          656 :   if (*head == NULL)
   13589          308 :     *head = *tail = *this_code;
   13590              :   else
   13591          348 :     *tail = gfc_append_code (*tail, *this_code);
   13592          656 :   *this_code = NULL;
   13593          656 : }
   13594              : 
   13595              : 
   13596              : /* Generate a final call from a variable expression  */
   13597              : 
   13598              : static void
   13599           81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13600              : {
   13601           81 :   gfc_code *this_code;
   13602           81 :   gfc_expr *final_expr = NULL;
   13603           81 :   gfc_expr *size_expr;
   13604           81 :   gfc_expr *fini_coarray;
   13605              : 
   13606           81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13607           81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13608           75 :     return;
   13609              : 
   13610              :   /* Now generate the finalizer call.  */
   13611            6 :   this_code = gfc_get_code (EXEC_CALL);
   13612            6 :   this_code->symtree = final_expr->symtree;
   13613            6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13614              : 
   13615              :   //* Expression to be finalized  */
   13616            6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13617            6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13618              : 
   13619              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13620            6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13621            6 :   size_expr = gfc_get_expr ();
   13622            6 :   size_expr->where = gfc_current_locus;
   13623            6 :   size_expr->expr_type = EXPR_OP;
   13624            6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13625            6 :   size_expr->value.op.op1
   13626           12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13627              :                                     "storage_size", gfc_current_locus, 2,
   13628            6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13629              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13630              :                                                       NULL, 0));
   13631            6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   13632              :                                               gfc_character_storage_size);
   13633            6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   13634            6 :   size_expr->ts = size_expr->value.op.op1->ts;
   13635            6 :   this_code->ext.actual->next->expr = size_expr;
   13636              : 
   13637              :   /* fini_coarray  */
   13638            6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   13639            6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   13640              :                                         &tmp_expr->where);
   13641            6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   13642            6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   13643              : 
   13644            6 :   add_code_to_chain (&this_code, head, tail);
   13645              : 
   13646              : }
   13647              : 
   13648              : /* Counts the potential number of part array references that would
   13649              :    result from resolution of typebound defined assignments.  */
   13650              : 
   13651              : 
   13652              : static int
   13653          243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   13654              : {
   13655          243 :   gfc_component *c;
   13656          243 :   int c_depth = 0, t_depth;
   13657              : 
   13658          584 :   for (c= derived->components; c; c = c->next)
   13659              :     {
   13660          341 :       if ((!gfc_bt_struct (c->ts.type)
   13661          261 :             || c->attr.pointer
   13662          261 :             || c->attr.allocatable
   13663          260 :             || c->attr.proc_pointer_comp
   13664          260 :             || c->attr.class_pointer
   13665          260 :             || c->attr.proc_pointer)
   13666           81 :           && !c->attr.defined_assign_comp)
   13667           81 :         continue;
   13668              : 
   13669          260 :       if (c->as && c_depth == 0)
   13670          260 :         c_depth = 1;
   13671              : 
   13672          260 :       if (c->ts.u.derived->attr.defined_assign_comp)
   13673          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   13674              :                                               c->as ? 1 : 0);
   13675              :       else
   13676              :         t_depth = 0;
   13677              : 
   13678          260 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   13679              :     }
   13680          243 :   return depth + c_depth;
   13681              : }
   13682              : 
   13683              : 
   13684              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   13685              :    "An intrinsic assignment where the variable is of derived type is performed
   13686              :     as if each component of the variable were assigned from the corresponding
   13687              :     component of expr using pointer assignment (10.2.2) for each pointer
   13688              :     component, defined assignment for each nonpointer nonallocatable component
   13689              :     of a type that has a type-bound defined assignment consistent with the
   13690              :     component, intrinsic assignment for each other nonpointer nonallocatable
   13691              :     component, and intrinsic assignment for each allocated coarray component.
   13692              :     For unallocated coarray components, the corresponding component of the
   13693              :     variable shall be unallocated. For a noncoarray allocatable component the
   13694              :     following sequence of operations is applied.
   13695              :         (1) If the component of the variable is allocated, it is deallocated.
   13696              :         (2) If the component of the value of expr is allocated, the
   13697              :             corresponding component of the variable is allocated with the same
   13698              :             dynamic type and type parameters as the component of the value of
   13699              :             expr. If it is an array, it is allocated with the same bounds. The
   13700              :             value of the component of the value of expr is then assigned to the
   13701              :             corresponding component of the variable using defined assignment if
   13702              :             the declared type of the component has a type-bound defined
   13703              :             assignment consistent with the component, and intrinsic assignment
   13704              :             for the dynamic type of that component otherwise."
   13705              : 
   13706              :    The pointer assignments are taken care of by the intrinsic assignment of the
   13707              :    structure itself.  This function recursively adds defined assignments where
   13708              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   13709              : 
   13710              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   13711              :    and the component of 'var' is finalizable, we need a temporary for the
   13712              :    lhs.  In pseudo-code for an assignment var = expr:
   13713              : 
   13714              :    ! Confine finalization of temporaries, as far as possible.
   13715              :      Enclose the code for the assignment in a block
   13716              :    ! Only call function 'expr' once.
   13717              :       #if ('expr is not a constant or an variable)
   13718              :         temp_expr = expr
   13719              :         expr = temp_x
   13720              :    ! Do the intrinsic assignment
   13721              :       #if typeof ('var') has a typebound final subroutine
   13722              :         finalize (var)
   13723              :       var = expr
   13724              :    ! Now do the component assignments
   13725              :       #do over derived type components [%cmp]
   13726              :         #if (cmp is a pointer of any kind)
   13727              :           continue
   13728              :         build the assignment
   13729              :         resolve the code
   13730              :         #if the code is a typebound assignment
   13731              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   13732              :              t1 = var
   13733              :              arg1 = t1
   13734              :              deal with allocatation or not of var and this component
   13735              :         #elseif the code is an assignment by itself
   13736              :            #if this component does not need finalization
   13737              :              delete code and continue
   13738              :         #else
   13739              :            remove the leading assignment
   13740              :         #endif
   13741              :         commit the code
   13742              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   13743              :            var%cmp = t1%cmp
   13744              :       #enddo
   13745              :       put all code chunks involving t1 to the top of the generated code
   13746              :       insert the generated block in place of the original code
   13747              : */
   13748              : 
   13749              : static bool
   13750          381 : is_finalizable_type (gfc_typespec ts)
   13751              : {
   13752          381 :   gfc_component *c;
   13753              : 
   13754          381 :   if (ts.type != BT_DERIVED)
   13755              :     return false;
   13756              : 
   13757              :   /* (1) Check for FINAL subroutines.  */
   13758          381 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   13759              :     return true;
   13760              : 
   13761              :   /* (2) Check for components of finalizable type.  */
   13762          809 :   for (c = ts.u.derived->components; c; c = c->next)
   13763          470 :     if (c->ts.type == BT_DERIVED
   13764          243 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   13765          242 :         && c->ts.u.derived->f2k_derived
   13766          242 :         && c->ts.u.derived->f2k_derived->finalizers)
   13767              :       return true;
   13768              : 
   13769              :   return false;
   13770              : }
   13771              : 
   13772              : /* The temporary assignments have to be put on top of the additional
   13773              :    code to avoid the result being changed by the intrinsic assignment.
   13774              :    */
   13775              : static int component_assignment_level = 0;
   13776              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   13777              : static bool finalizable_comp;
   13778              : 
   13779              : static void
   13780          188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   13781              : {
   13782          188 :   gfc_component *comp1, *comp2;
   13783          188 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   13784          188 :   gfc_code *tmp_code = NULL;
   13785          188 :   gfc_expr *t1 = NULL;
   13786          188 :   gfc_expr *tmp_expr = NULL;
   13787          188 :   int error_count, depth;
   13788          188 :   bool finalizable_lhs;
   13789              : 
   13790          188 :   gfc_get_errors (NULL, &error_count);
   13791              : 
   13792              :   /* Filter out continuing processing after an error.  */
   13793          188 :   if (error_count
   13794          188 :       || (*code)->expr1->ts.type != BT_DERIVED
   13795          188 :       || (*code)->expr2->ts.type != BT_DERIVED)
   13796          140 :     return;
   13797              : 
   13798              :   /* TODO: Handle more than one part array reference in assignments.  */
   13799          188 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   13800          188 :                                       (*code)->expr1->rank ? 1 : 0);
   13801          188 :   if (depth > 1)
   13802              :     {
   13803            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   13804              :                    "done because multiple part array references would "
   13805              :                    "occur in intermediate expressions.", &(*code)->loc);
   13806            6 :       return;
   13807              :     }
   13808              : 
   13809          182 :   if (!component_assignment_level)
   13810          134 :     finalizable_comp = true;
   13811              : 
   13812              :   /* Build a block so that function result temporaries are finalized
   13813              :      locally on exiting the rather than enclosing scope.  */
   13814          182 :   if (!component_assignment_level)
   13815              :     {
   13816          134 :       ns = gfc_build_block_ns (ns);
   13817          134 :       tmp_code = gfc_get_code (EXEC_NOP);
   13818          134 :       *tmp_code = **code;
   13819          134 :       tmp_code->next = NULL;
   13820          134 :       (*code)->op = EXEC_BLOCK;
   13821          134 :       (*code)->ext.block.ns = ns;
   13822          134 :       (*code)->ext.block.assoc = NULL;
   13823          134 :       (*code)->expr1 = (*code)->expr2 = NULL;
   13824          134 :       ns->code = tmp_code;
   13825          134 :       code = &ns->code;
   13826              :     }
   13827              : 
   13828          182 :   component_assignment_level++;
   13829              : 
   13830          182 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   13831              : 
   13832              :   /* Create a temporary so that functions get called only once.  */
   13833          182 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   13834          182 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   13835              :     {
   13836              :       /* Assign the rhs to the temporary.  */
   13837           81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13838           81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   13839              :         {
   13840              :           /* Use allocate on assignment for the sake of simplicity. The
   13841              :              temporary must not take on the optional attribute. Assume
   13842              :              that the assignment is guarded by a PRESENT condition if the
   13843              :              lhs is optional.  */
   13844           25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   13845           25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   13846           25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   13847              :         }
   13848          162 :       this_code = build_assignment (EXEC_ASSIGN,
   13849              :                                     tmp_expr, (*code)->expr2,
   13850           81 :                                     NULL, NULL, (*code)->loc);
   13851           81 :       this_code->expr2->must_finalize = 1;
   13852              :       /* Add the code and substitute the rhs expression.  */
   13853           81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   13854           81 :       gfc_free_expr ((*code)->expr2);
   13855           81 :       (*code)->expr2 = tmp_expr;
   13856              :     }
   13857              : 
   13858              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   13859              :      of the temporaries generated here, since the intrinsic assignment
   13860              :      to the final result already does this.  */
   13861          182 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   13862              :     {
   13863          182 :       if (finalizable_lhs)
   13864           18 :         (*code)->expr1->must_finalize = 1;
   13865          182 :       this_code = build_assignment (EXEC_ASSIGN,
   13866              :                                     (*code)->expr1, (*code)->expr2,
   13867              :                                     NULL, NULL, (*code)->loc);
   13868          182 :       add_code_to_chain (&this_code, &head, &tail);
   13869              :     }
   13870              : 
   13871          182 :   comp1 = (*code)->expr1->ts.u.derived->components;
   13872          182 :   comp2 = (*code)->expr2->ts.u.derived->components;
   13873              : 
   13874          449 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   13875              :     {
   13876          267 :       bool inout = false;
   13877          267 :       bool finalizable_out = false;
   13878              : 
   13879              :       /* The intrinsic assignment does the right thing for pointers
   13880              :          of all kinds and allocatable components.  */
   13881          267 :       if (!gfc_bt_struct (comp1->ts.type)
   13882          200 :           || comp1->attr.pointer
   13883          200 :           || comp1->attr.allocatable
   13884          199 :           || comp1->attr.proc_pointer_comp
   13885          199 :           || comp1->attr.class_pointer
   13886          199 :           || comp1->attr.proc_pointer)
   13887           68 :         continue;
   13888              : 
   13889          398 :       finalizable_comp = is_finalizable_type (comp1->ts)
   13890          199 :                          && !finalizable_lhs;
   13891              : 
   13892              :       /* Make an assignment for this component.  */
   13893          398 :       this_code = build_assignment (EXEC_ASSIGN,
   13894              :                                     (*code)->expr1, (*code)->expr2,
   13895          199 :                                     comp1, comp2, (*code)->loc);
   13896              : 
   13897              :       /* Convert the assignment if there is a defined assignment for
   13898              :          this type.  Otherwise, using the call from gfc_resolve_code,
   13899              :          recurse into its components.  */
   13900          199 :       gfc_resolve_code (this_code, ns);
   13901              : 
   13902          199 :       if (this_code->op == EXEC_ASSIGN_CALL)
   13903              :         {
   13904          144 :           gfc_formal_arglist *dummy_args;
   13905          144 :           gfc_symbol *rsym;
   13906              :           /* Check that there is a typebound defined assignment.  If not,
   13907              :              then this must be a module defined assignment.  We cannot
   13908              :              use the defined_assign_comp attribute here because it must
   13909              :              be this derived type that has the defined assignment and not
   13910              :              a parent type.  */
   13911          144 :           if (!(comp1->ts.u.derived->f2k_derived
   13912              :                 && comp1->ts.u.derived->f2k_derived
   13913          144 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   13914              :             {
   13915            1 :               gfc_free_statements (this_code);
   13916            1 :               this_code = NULL;
   13917            1 :               continue;
   13918              :             }
   13919              : 
   13920              :           /* If the first argument of the subroutine has intent INOUT
   13921              :              a temporary must be generated and used instead.  */
   13922          143 :           rsym = this_code->resolved_sym;
   13923          143 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   13924          268 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   13925           18 :                             && dummy_args
   13926          161 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   13927          286 :           inout = dummy_args
   13928          268 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   13929           72 :           if ((inout || finalizable_out)
   13930           89 :               && !comp1->attr.allocatable)
   13931              :             {
   13932           89 :               gfc_code *temp_code;
   13933           89 :               inout = true;
   13934              : 
   13935              :               /* Build the temporary required for the assignment and put
   13936              :                  it at the head of the generated code.  */
   13937           89 :               if (!t1)
   13938              :                 {
   13939           89 :                   gfc_namespace *tmp_ns = ns;
   13940           89 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   13941           18 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   13942           89 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   13943           89 :                   t1->symtree->n.sym->attr.artificial = 1;
   13944          178 :                   temp_code = build_assignment (EXEC_ASSIGN,
   13945              :                                                 t1, (*code)->expr1,
   13946           89 :                                 NULL, NULL, (*code)->loc);
   13947              : 
   13948              :                   /* For allocatable LHS, check whether it is allocated.  Note
   13949              :                      that allocatable components with defined assignment are
   13950              :                      not yet support.  See PR 57696.  */
   13951           89 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   13952              :                     {
   13953           24 :                       gfc_code *block;
   13954           24 :                       gfc_expr *e =
   13955           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13956           24 :                       block = gfc_get_code (EXEC_IF);
   13957           24 :                       block->block = gfc_get_code (EXEC_IF);
   13958           24 :                       block->block->expr1
   13959           48 :                           = gfc_build_intrinsic_call (ns,
   13960              :                                     GFC_ISYM_ALLOCATED, "allocated",
   13961           24 :                                     (*code)->loc, 1, e);
   13962           24 :                       block->block->next = temp_code;
   13963           24 :                       temp_code = block;
   13964              :                     }
   13965           89 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   13966              :                 }
   13967              : 
   13968              :               /* Replace the first actual arg with the component of the
   13969              :                  temporary.  */
   13970           89 :               gfc_free_expr (this_code->ext.actual->expr);
   13971           89 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   13972           89 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   13973              : 
   13974              :               /* If the LHS variable is allocatable and wasn't allocated and
   13975              :                  the temporary is allocatable, pointer assign the address of
   13976              :                  the freshly allocated LHS to the temporary.  */
   13977           89 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   13978           89 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   13979              :                 {
   13980           18 :                   gfc_code *block;
   13981           18 :                   gfc_expr *cond;
   13982              : 
   13983           18 :                   cond = gfc_get_expr ();
   13984           18 :                   cond->ts.type = BT_LOGICAL;
   13985           18 :                   cond->ts.kind = gfc_default_logical_kind;
   13986           18 :                   cond->expr_type = EXPR_OP;
   13987           18 :                   cond->where = (*code)->loc;
   13988           18 :                   cond->value.op.op = INTRINSIC_NOT;
   13989           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   13990              :                                           GFC_ISYM_ALLOCATED, "allocated",
   13991           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   13992           18 :                   block = gfc_get_code (EXEC_IF);
   13993           18 :                   block->block = gfc_get_code (EXEC_IF);
   13994           18 :                   block->block->expr1 = cond;
   13995           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13996              :                                         t1, (*code)->expr1,
   13997           18 :                                         NULL, NULL, (*code)->loc);
   13998           18 :                   add_code_to_chain (&block, &head, &tail);
   13999              :                 }
   14000              :             }
   14001              :         }
   14002           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   14003              :         {
   14004              :           /* Don't add intrinsic assignments since they are already
   14005              :              effected by the intrinsic assignment of the structure, unless
   14006              :              finalization is required.  */
   14007            7 :           if (finalizable_comp)
   14008            0 :             this_code->expr1->must_finalize = 1;
   14009              :           else
   14010              :             {
   14011            7 :               gfc_free_statements (this_code);
   14012            7 :               this_code = NULL;
   14013            7 :               continue;
   14014              :             }
   14015              :         }
   14016              :       else
   14017              :         {
   14018              :           /* Resolution has expanded an assignment of a derived type with
   14019              :              defined assigned components.  Remove the redundant, leading
   14020              :              assignment.  */
   14021           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   14022           48 :           gfc_code *tmp = this_code;
   14023           48 :           this_code = this_code->next;
   14024           48 :           tmp->next = NULL;
   14025           48 :           gfc_free_statements (tmp);
   14026              :         }
   14027              : 
   14028          191 :       add_code_to_chain (&this_code, &head, &tail);
   14029              : 
   14030          191 :       if (t1 && (inout || finalizable_out))
   14031              :         {
   14032              :           /* Transfer the value to the final result.  */
   14033          178 :           this_code = build_assignment (EXEC_ASSIGN,
   14034              :                                         (*code)->expr1, t1,
   14035           89 :                                         comp1, comp2, (*code)->loc);
   14036           89 :           this_code->expr1->must_finalize = 0;
   14037           89 :           add_code_to_chain (&this_code, &head, &tail);
   14038              :         }
   14039              :     }
   14040              : 
   14041              :   /* Put the temporary assignments at the top of the generated code.  */
   14042          182 :   if (tmp_head && component_assignment_level == 1)
   14043              :     {
   14044          126 :       gfc_append_code (tmp_head, head);
   14045          126 :       head = tmp_head;
   14046          126 :       tmp_head = tmp_tail = NULL;
   14047              :     }
   14048              : 
   14049              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   14050              :      not accidentally deallocated. Hence, nullify t1.  */
   14051           89 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   14052          271 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   14053              :     {
   14054           18 :       gfc_code *block;
   14055           18 :       gfc_expr *cond;
   14056           18 :       gfc_expr *e;
   14057              : 
   14058           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14059           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   14060           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   14061           18 :       block = gfc_get_code (EXEC_IF);
   14062           18 :       block->block = gfc_get_code (EXEC_IF);
   14063           18 :       block->block->expr1 = cond;
   14064           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14065              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   14066           18 :                                         NULL, NULL, (*code)->loc);
   14067           18 :       gfc_append_code (tail, block);
   14068           18 :       tail = block;
   14069              :     }
   14070              : 
   14071          182 :   component_assignment_level--;
   14072              : 
   14073              :   /* Make an explicit final call for the function result.  */
   14074          182 :   if (tmp_expr)
   14075           81 :     generate_final_call (tmp_expr, &head, &tail);
   14076              : 
   14077          182 :   if (tmp_code)
   14078              :     {
   14079          134 :       ns->code = head;
   14080          134 :       return;
   14081              :     }
   14082              : 
   14083              :   /* Now attach the remaining code chain to the input code.  Step on
   14084              :      to the end of the new code since resolution is complete.  */
   14085           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14086           48 :   tail->next = (*code)->next;
   14087              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14088              :      before the temporary for the lhs is created.  */
   14089           48 :   gfc_free_expr ((*code)->expr1);
   14090           48 :   gfc_free_expr ((*code)->expr2);
   14091           48 :   **code = *head;
   14092           48 :   if (head != tail)
   14093           48 :     free (head);
   14094           48 :   *code = tail;
   14095              : }
   14096              : 
   14097              : 
   14098              : /* F2008: Pointer function assignments are of the form:
   14099              :         ptr_fcn (args) = expr
   14100              :    This function breaks these assignments into two statements:
   14101              :         temporary_pointer => ptr_fcn(args)
   14102              :         temporary_pointer = expr  */
   14103              : 
   14104              : static bool
   14105       287505 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14106              : {
   14107       287505 :   gfc_expr *tmp_ptr_expr;
   14108       287505 :   gfc_code *this_code;
   14109       287505 :   gfc_component *comp;
   14110       287505 :   gfc_symbol *s;
   14111              : 
   14112       287505 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14113              :     return false;
   14114              : 
   14115              :   /* Even if standard does not support this feature, continue to build
   14116              :      the two statements to avoid upsetting frontend_passes.c.  */
   14117          205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14118              :                   "%L", &(*code)->loc);
   14119              : 
   14120          205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14121              : 
   14122          205 :   if (comp)
   14123            6 :     s = comp->ts.interface;
   14124              :   else
   14125          199 :     s = (*code)->expr1->symtree->n.sym;
   14126              : 
   14127          205 :   if (s == NULL || !s->result->attr.pointer)
   14128              :     {
   14129            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14130              :                  "%L must have the pointer attribute.",
   14131            5 :                  &(*code)->expr1->where);
   14132            5 :       (*code)->op = EXEC_NOP;
   14133            5 :       return false;
   14134              :     }
   14135              : 
   14136          200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14137              : 
   14138              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14139              :      end, where array bounds are not known, arrays are made allocatable.
   14140              :      Change the temporary to a pointer here.  */
   14141          200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14142          200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14143          200 :   tmp_ptr_expr->where = (*code)->loc;
   14144              : 
   14145              :   /* A new charlen is required to ensure that the variable string length
   14146              :      is different to that of the original lhs for deferred results.  */
   14147          200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14148              :     {
   14149           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14150           60 :       tmp_ptr_expr->ts.deferred = 1;
   14151           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14152           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14153           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14154              :     }
   14155              : 
   14156          400 :   this_code = build_assignment (EXEC_ASSIGN,
   14157              :                                 tmp_ptr_expr, (*code)->expr2,
   14158          200 :                                 NULL, NULL, (*code)->loc);
   14159          200 :   this_code->next = (*code)->next;
   14160          200 :   (*code)->next = this_code;
   14161          200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14162          200 :   (*code)->expr2 = (*code)->expr1;
   14163          200 :   (*code)->expr1 = tmp_ptr_expr;
   14164              : 
   14165          200 :   return true;
   14166              : }
   14167              : 
   14168              : 
   14169              : /* Deferred character length assignments from an operator expression
   14170              :    require a temporary because the character length of the lhs can
   14171              :    change in the course of the assignment.  */
   14172              : 
   14173              : static bool
   14174       286343 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14175              : {
   14176       286343 :   gfc_expr *tmp_expr;
   14177       286343 :   gfc_code *this_code;
   14178              : 
   14179       286343 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14180        27371 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14181          836 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14182          835 :          && (*code)->expr2->expr_type == EXPR_OP))
   14183              :     return false;
   14184              : 
   14185           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14186              :     return false;
   14187              : 
   14188           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14189              :     return false;
   14190              : 
   14191           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14192           22 :   tmp_expr->where = (*code)->loc;
   14193              : 
   14194              :   /* A new charlen is required to ensure that the variable string
   14195              :      length is different to that of the original lhs.  */
   14196           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14197           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14198           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14199           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14200              : 
   14201           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14202              : 
   14203           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14204           22 :                                 (*code)->expr1,
   14205              :                                 gfc_copy_expr (tmp_expr),
   14206              :                                 NULL, NULL, (*code)->loc);
   14207              : 
   14208           22 :   (*code)->expr1 = tmp_expr;
   14209              : 
   14210           22 :   this_code->next = (*code)->next;
   14211           22 :   (*code)->next = this_code;
   14212              : 
   14213           22 :   return true;
   14214              : }
   14215              : 
   14216              : static void mark_lhs_assignments_set (gfc_code *code);
   14217              : 
   14218              : /* Given a block of code, recursively resolve everything pointed to by this
   14219              :    code block.  */
   14220              : 
   14221              : void
   14222       689152 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14223              : {
   14224       689152 :   int omp_workshare_save;
   14225       689152 :   int forall_save, do_concurrent_save;
   14226       689152 :   code_stack frame;
   14227       689152 :   bool t;
   14228       689152 :   gfc_code *orig_code = code;
   14229              : 
   14230       689152 :   frame.prev = cs_base;
   14231       689152 :   frame.head = code;
   14232       689152 :   cs_base = &frame;
   14233              : 
   14234       689152 :   find_reachable_labels (code);
   14235              : 
   14236      1832443 :   for (; code; code = code->next)
   14237              :     {
   14238      1143292 :       frame.current = code;
   14239      1143292 :       forall_save = forall_flag;
   14240      1143292 :       do_concurrent_save = gfc_do_concurrent_flag;
   14241              : 
   14242      1143292 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14243              :         {
   14244         2217 :           if (code->op == EXEC_FORALL)
   14245         1993 :             forall_flag = 1;
   14246          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14247          224 :             gfc_do_concurrent_flag = 1;
   14248         2217 :           gfc_resolve_forall (code, ns, forall_save);
   14249         2217 :           if (code->op == EXEC_FORALL)
   14250         1993 :             forall_flag = 2;
   14251          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14252          224 :             gfc_do_concurrent_flag = 2;
   14253              :         }
   14254      1141075 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14255          138 :         for (gfc_omp_variant *variant
   14256              :                = code->ext.omp_variants;
   14257          448 :              variant; variant = variant->next)
   14258          310 :           gfc_resolve_code (variant->code, ns);
   14259      1140937 :       else if (code->block)
   14260              :         {
   14261       331945 :           omp_workshare_save = -1;
   14262       331945 :           switch (code->op)
   14263              :             {
   14264        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14265        10119 :             case EXEC_OACC_PARALLEL:
   14266        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14267        10119 :             case EXEC_OACC_KERNELS:
   14268        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14269        10119 :             case EXEC_OACC_SERIAL:
   14270        10119 :             case EXEC_OACC_DATA:
   14271        10119 :             case EXEC_OACC_HOST_DATA:
   14272        10119 :             case EXEC_OACC_LOOP:
   14273        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14274        10119 :               break;
   14275           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14276           54 :               omp_workshare_save = omp_workshare_flag;
   14277           54 :               omp_workshare_flag = 1;
   14278           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14279           54 :               break;
   14280         6000 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14281         6000 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14282         6000 :             case EXEC_OMP_MASKED_TASKLOOP:
   14283         6000 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14284         6000 :             case EXEC_OMP_MASTER_TASKLOOP:
   14285         6000 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14286         6000 :             case EXEC_OMP_PARALLEL:
   14287         6000 :             case EXEC_OMP_PARALLEL_DO:
   14288         6000 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14289         6000 :             case EXEC_OMP_PARALLEL_LOOP:
   14290         6000 :             case EXEC_OMP_PARALLEL_MASKED:
   14291         6000 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14292         6000 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14293         6000 :             case EXEC_OMP_PARALLEL_MASTER:
   14294         6000 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14295         6000 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14296         6000 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14297         6000 :             case EXEC_OMP_TARGET_PARALLEL:
   14298         6000 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14299         6000 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14300         6000 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14301         6000 :             case EXEC_OMP_TARGET_TEAMS:
   14302         6000 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14303         6000 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14304         6000 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14305         6000 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14306         6000 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14307         6000 :             case EXEC_OMP_TASK:
   14308         6000 :             case EXEC_OMP_TASKLOOP:
   14309         6000 :             case EXEC_OMP_TASKLOOP_SIMD:
   14310         6000 :             case EXEC_OMP_TEAMS:
   14311         6000 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14312         6000 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14313         6000 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14314         6000 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14315         6000 :             case EXEC_OMP_TEAMS_LOOP:
   14316         6000 :               omp_workshare_save = omp_workshare_flag;
   14317         6000 :               omp_workshare_flag = 0;
   14318         6000 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14319         6000 :               break;
   14320         3064 :             case EXEC_OMP_DISTRIBUTE:
   14321         3064 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14322         3064 :             case EXEC_OMP_DO:
   14323         3064 :             case EXEC_OMP_DO_SIMD:
   14324         3064 :             case EXEC_OMP_LOOP:
   14325         3064 :             case EXEC_OMP_SIMD:
   14326         3064 :             case EXEC_OMP_TARGET_SIMD:
   14327         3064 :             case EXEC_OMP_TILE:
   14328         3064 :             case EXEC_OMP_UNROLL:
   14329         3064 :               gfc_resolve_omp_do_blocks (code, ns);
   14330         3064 :               break;
   14331              :             case EXEC_SELECT_TYPE:
   14332              :             case EXEC_SELECT_RANK:
   14333              :               /* Blocks are handled in resolve_select_type/rank because we
   14334              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14335              :               break;
   14336              :             case EXEC_DO_CONCURRENT:
   14337              :               gfc_do_concurrent_flag = 1;
   14338              :               gfc_resolve_blocks (code->block, ns);
   14339              :               gfc_do_concurrent_flag = 2;
   14340              :               break;
   14341           39 :             case EXEC_OMP_WORKSHARE:
   14342           39 :               omp_workshare_save = omp_workshare_flag;
   14343           39 :               omp_workshare_flag = 1;
   14344              :               /* FALL THROUGH */
   14345       308617 :             default:
   14346       308617 :               gfc_resolve_blocks (code->block, ns);
   14347       308617 :               break;
   14348              :             }
   14349              : 
   14350       327854 :           if (omp_workshare_save != -1)
   14351         6093 :             omp_workshare_flag = omp_workshare_save;
   14352              :         }
   14353       808992 : start:
   14354      1143497 :       t = true;
   14355      1143497 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14356      1142066 :           t = gfc_resolve_expr (code->expr1);
   14357              : 
   14358      1143497 :       forall_flag = forall_save;
   14359      1143497 :       gfc_do_concurrent_flag = do_concurrent_save;
   14360              : 
   14361      1143497 :       if (!gfc_resolve_expr (code->expr2))
   14362          638 :         t = false;
   14363              : 
   14364      1143497 :       if (code->op == EXEC_ALLOCATE
   14365      1143497 :           && !gfc_resolve_expr (code->expr3))
   14366              :         t = false;
   14367              : 
   14368      1143497 :       switch (code->op)
   14369              :         {
   14370              :         case EXEC_NOP:
   14371              :         case EXEC_END_BLOCK:
   14372              :         case EXEC_END_NESTED_BLOCK:
   14373              :         case EXEC_CYCLE:
   14374              :           break;
   14375              : 
   14376       218739 :         case EXEC_STOP:
   14377       218739 :         case EXEC_ERROR_STOP:
   14378       218739 :           if (code->expr1 != NULL && t)
   14379              :             {
   14380       198287 :               if (!(code->expr1->ts.type == BT_CHARACTER
   14381              :                     || code->expr1->ts.type == BT_INTEGER))
   14382            1 :                 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER "
   14383              :                            "type", &code->expr1->where);
   14384       198286 :               else if (code->expr1->rank != 0)
   14385            0 :                 gfc_error ("STOP code at %L must be scalar",
   14386              :                            &code->expr1->where);
   14387       198286 :               else if (code->expr1->ts.type == BT_CHARACTER
   14388          478 :                        && code->expr1->ts.kind != gfc_default_character_kind)
   14389            0 :                 gfc_error ("STOP code at %L must be default character KIND=%d",
   14390              :                            &code->expr1->where, (int) gfc_default_character_kind);
   14391       198286 :               else if (code->expr1->ts.type == BT_INTEGER
   14392       197808 :                        && code->expr1->ts.kind != gfc_default_integer_kind)
   14393            8 :                 gfc_notify_std (GFC_STD_F2018, "STOP code at %L must be default "
   14394              :                                 "integer KIND=%d", &code->expr1->where,
   14395              :                                 (int) gfc_default_integer_kind);
   14396              :             }
   14397       218739 :           if (code->expr2 != NULL
   14398           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14399           37 :                   || code->expr2->rank != 0))
   14400            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14401              :                        &code->expr2->where);
   14402              : 
   14403              :           /* Fall through.  */
   14404       218769 :         case EXEC_PAUSE:
   14405       218769 :           gfc_value_used_expr (code->expr1, VALUE_USED);
   14406       218769 :           break;
   14407              : 
   14408              :         case EXEC_EXIT:
   14409              :         case EXEC_CONTINUE:
   14410              :         case EXEC_DT_END:
   14411              :         case EXEC_ASSIGN_CALL:
   14412              :           break;
   14413              : 
   14414           54 :         case EXEC_CRITICAL:
   14415           54 :           resolve_critical (code);
   14416           54 :           break;
   14417              : 
   14418         1317 :         case EXEC_SYNC_ALL:
   14419         1317 :         case EXEC_SYNC_IMAGES:
   14420         1317 :         case EXEC_SYNC_MEMORY:
   14421         1317 :           resolve_sync (code);
   14422         1317 :           break;
   14423              : 
   14424          197 :         case EXEC_LOCK:
   14425          197 :         case EXEC_UNLOCK:
   14426          197 :         case EXEC_EVENT_POST:
   14427          197 :         case EXEC_EVENT_WAIT:
   14428          197 :           resolve_lock_unlock_event (code);
   14429          197 :           break;
   14430              : 
   14431              :         case EXEC_FAIL_IMAGE:
   14432              :           break;
   14433              : 
   14434          130 :         case EXEC_FORM_TEAM:
   14435          130 :           resolve_form_team (code);
   14436          130 :           break;
   14437              : 
   14438           73 :         case EXEC_CHANGE_TEAM:
   14439           73 :           resolve_change_team (code);
   14440           73 :           break;
   14441              : 
   14442           71 :         case EXEC_END_TEAM:
   14443           71 :           resolve_end_team (code);
   14444           71 :           break;
   14445              : 
   14446           43 :         case EXEC_SYNC_TEAM:
   14447           43 :           resolve_sync_team (code);
   14448           43 :           break;
   14449              : 
   14450         1491 :         case EXEC_ENTRY:
   14451              :           /* Keep track of which entry we are up to.  */
   14452         1491 :           current_entry_id = code->ext.entry->id;
   14453         1491 :           break;
   14454              : 
   14455          453 :         case EXEC_WHERE:
   14456          453 :           resolve_where (code, NULL);
   14457          453 :           break;
   14458              : 
   14459         1250 :         case EXEC_GOTO:
   14460         1250 :           if (code->expr1 != NULL)
   14461              :             {
   14462           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14463           76 :                   || code->expr1->ts.type != BT_INTEGER
   14464           76 :                   || (code->expr1->ref
   14465            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14466           75 :                   || code->expr1->symtree == NULL
   14467           75 :                   || (code->expr1->symtree->n.sym
   14468           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14469           75 :                           == FL_PARAMETER)))
   14470            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14471              :                            "scalar INTEGER variable", &code->expr1->where);
   14472           74 :               else if (code->expr1->symtree->n.sym
   14473           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14474            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14475              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14476              :                            &code->expr1->where);
   14477              :             }
   14478              :           else
   14479         1172 :             resolve_branch (code->label1, code);
   14480              :           break;
   14481              : 
   14482         3224 :         case EXEC_RETURN:
   14483         3224 :           if (code->expr1 != NULL
   14484           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14485            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14486              :                        "INTEGER return specifier", &code->expr1->where);
   14487              :           break;
   14488              : 
   14489              :         case EXEC_INIT_ASSIGN:
   14490              :         case EXEC_END_PROCEDURE:
   14491              :           break;
   14492              : 
   14493       288681 :         case EXEC_ASSIGN:
   14494       288681 :           if (!t)
   14495              :             break;
   14496              : 
   14497       288005 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14498       288005 :               && gfc_is_coindexed (code->expr1))
   14499              :             {
   14500              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14501              :                  coindexed variable.  */
   14502          500 :               code->op = EXEC_CALL;
   14503          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14504              :                                 true);
   14505          500 :               code->resolved_sym = code->symtree->n.sym;
   14506          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14507          500 :               code->resolved_sym->attr.intrinsic = 1;
   14508          500 :               code->resolved_sym->attr.subroutine = 1;
   14509          500 :               code->resolved_isym
   14510          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14511          500 :               gfc_commit_symbol (code->resolved_sym);
   14512          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14513          500 :               code->ext.actual->expr = code->expr1;
   14514          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14515          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14516          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14517              :                 {
   14518              :                   /* Convert assignments of expr1[...] = expr2 into
   14519              :                         tvar = expr2
   14520              :                         expr1[...] = tvar
   14521              :                      when expr2 is not trivial.  */
   14522           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14523           54 :                   gfc_code next_code = *code;
   14524           54 :                   gfc_code *rhs_code
   14525          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14526           54 :                                         NULL, code->expr2->where);
   14527           54 :                   *code = *rhs_code;
   14528           54 :                   code->next = rhs_code;
   14529           54 :                   *rhs_code = next_code;
   14530              : 
   14531           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14532           54 :                   rhs_code->expr1 = NULL;
   14533           54 :                   rhs_code->expr2 = NULL;
   14534              :                 }
   14535              :               else
   14536              :                 {
   14537          446 :                   code->ext.actual->next->expr = code->expr2;
   14538              : 
   14539          446 :                   code->expr1 = NULL;
   14540          446 :                   code->expr2 = NULL;
   14541              :                 }
   14542              :               break;
   14543              :             }
   14544              : 
   14545       287505 :           if (code->expr1->ts.type == BT_CLASS)
   14546         1114 :             gfc_find_vtab (&code->expr2->ts);
   14547              : 
   14548              :           /* If this is a pointer function in an lvalue variable context,
   14549              :              the new code will have to be resolved afresh. This is also the
   14550              :              case with an error, where the code is transformed into NOP to
   14551              :              prevent ICEs downstream.  */
   14552       287505 :           if (resolve_ptr_fcn_assign (&code, ns)
   14553       287505 :               || code->op == EXEC_NOP)
   14554          205 :             goto start;
   14555              : 
   14556       287300 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14557       287300 :                                          _("assignment")))
   14558              :             break;
   14559              : 
   14560       287261 :           if (resolve_ordinary_assign (code, ns))
   14561              :             {
   14562          918 :               if (omp_workshare_flag)
   14563              :                 {
   14564            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14565            1 :                              "at %L", &code->loc);
   14566            1 :                   break;
   14567              :                 }
   14568          917 :               if (code->op == EXEC_COMPCALL)
   14569          449 :                 goto compcall;
   14570              :               else
   14571          468 :                 goto call;
   14572              :             }
   14573              : 
   14574              :           /* Check for dependencies in deferred character length array
   14575              :              assignments and generate a temporary, if necessary.  */
   14576       286343 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14577              :             break;
   14578              : 
   14579              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14580       286321 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14581         7338 :               && code->expr1->ts.u.derived
   14582         7338 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14583          188 :             generate_component_assignments (&code, ns);
   14584       286133 :           else if (code->op == EXEC_ASSIGN)
   14585              :             {
   14586       286133 :               if (gfc_may_be_finalized (code->expr1->ts))
   14587         1295 :                 code->expr1->must_finalize = 1;
   14588       286133 :               if (code->expr2->expr_type == EXPR_ARRAY
   14589       286133 :                   && gfc_may_be_finalized (code->expr2->ts))
   14590           73 :                 code->expr2->must_finalize = 1;
   14591              :             }
   14592              : 
   14593              :           break;
   14594              : 
   14595          126 :         case EXEC_LABEL_ASSIGN:
   14596          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   14597            0 :             gfc_error ("Label %d referenced at %L is never defined",
   14598              :                        code->label1->value, &code->label1->where);
   14599          126 :           if (t
   14600          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   14601          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   14602          126 :                   || code->expr1->symtree->n.sym->ts.kind
   14603          126 :                      != gfc_default_integer_kind
   14604          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   14605          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   14606            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   14607              :                        "default INTEGER variable", &code->expr1->where);
   14608              :           break;
   14609              : 
   14610        10478 :         case EXEC_POINTER_ASSIGN:
   14611        10478 :           {
   14612        10478 :             gfc_expr* e;
   14613              : 
   14614        10478 :             if (!t)
   14615              :               break;
   14616              : 
   14617              :             /* This is both a variable definition and pointer assignment
   14618              :                context, so check both of them.  For rank remapping, a final
   14619              :                array ref may be present on the LHS and fool gfc_expr_attr
   14620              :                used in gfc_check_vardef_context.  Remove it.  */
   14621        10473 :             e = remove_last_array_ref (code->expr1);
   14622        20946 :             t = gfc_check_vardef_context (e, true, false, false,
   14623        10473 :                                           _("pointer assignment"));
   14624        10473 :             if (t)
   14625        10444 :               t = gfc_check_vardef_context (e, false, false, false,
   14626        10444 :                                             _("pointer assignment"));
   14627        10473 :             gfc_free_expr (e);
   14628              : 
   14629        10473 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   14630              : 
   14631        10331 :             if (!t)
   14632              :               break;
   14633              : 
   14634              :             /* Assigning a class object always is a regular assign.  */
   14635        10331 :             if (code->expr2->ts.type == BT_CLASS
   14636          582 :                 && code->expr1->ts.type == BT_CLASS
   14637          491 :                 && CLASS_DATA (code->expr2)
   14638          490 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   14639        10968 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   14640           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   14641           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   14642           43 :                         == FL_PROCEDURE))
   14643          340 :               code->op = EXEC_ASSIGN;
   14644              :             break;
   14645              :           }
   14646              : 
   14647           72 :         case EXEC_ARITHMETIC_IF:
   14648           72 :           {
   14649           72 :             gfc_expr *e = code->expr1;
   14650              : 
   14651           72 :             gfc_resolve_expr (e);
   14652           72 :             if (e->expr_type == EXPR_NULL)
   14653            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   14654              : 
   14655           72 :             if (t && (e->rank > 0
   14656           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   14657            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   14658              :                          "REAL or INTEGER expression", &e->where);
   14659              : 
   14660           72 :             resolve_branch (code->label1, code);
   14661           72 :             resolve_branch (code->label2, code);
   14662           72 :             resolve_branch (code->label3, code);
   14663              :           }
   14664           72 :           break;
   14665              : 
   14666       232224 :         case EXEC_IF:
   14667       232224 :           if (t && code->expr1 != NULL
   14668            0 :               && (code->expr1->ts.type != BT_LOGICAL
   14669            0 :                   || code->expr1->rank != 0))
   14670            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   14671              :                        &code->expr1->where);
   14672              :           break;
   14673              : 
   14674        80142 :         case EXEC_CALL:
   14675        80142 :         call:
   14676        80142 :           resolve_call (code);
   14677        80142 :           break;
   14678              : 
   14679         1756 :         case EXEC_COMPCALL:
   14680         1756 :         compcall:
   14681         1756 :           resolve_typebound_subroutine (code);
   14682         1756 :           break;
   14683              : 
   14684          124 :         case EXEC_CALL_PPC:
   14685          124 :           resolve_ppc_call (code);
   14686          124 :           break;
   14687              : 
   14688          687 :         case EXEC_SELECT:
   14689              :           /* Select is complicated. Also, a SELECT construct could be
   14690              :              a transformed computed GOTO.  */
   14691          687 :           resolve_select (code, false);
   14692          687 :           break;
   14693              : 
   14694         3081 :         case EXEC_SELECT_TYPE:
   14695         3081 :           resolve_select_type (code, ns);
   14696         3081 :           break;
   14697              : 
   14698         1036 :         case EXEC_SELECT_RANK:
   14699         1036 :           resolve_select_rank (code, ns);
   14700         1036 :           break;
   14701              : 
   14702         8142 :         case EXEC_BLOCK:
   14703         8142 :           resolve_block_construct (code);
   14704         8142 :           break;
   14705              : 
   14706        33103 :         case EXEC_DO:
   14707        33103 :           if (code->ext.iterator != NULL)
   14708              :             {
   14709        33103 :               gfc_iterator *iter = code->ext.iterator;
   14710        33103 :               if (gfc_resolve_iterator (iter, true, false))
   14711        33089 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   14712              :                                          true);
   14713              :             }
   14714              :           break;
   14715              : 
   14716          531 :         case EXEC_DO_WHILE:
   14717          531 :           if (code->expr1 == NULL)
   14718            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   14719              :                                 "DO WHILE");
   14720          531 :           if (t
   14721          531 :               && (code->expr1->rank != 0
   14722          531 :                   || code->expr1->ts.type != BT_LOGICAL))
   14723            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   14724              :                        "a scalar LOGICAL expression", &code->expr1->where);
   14725              :           break;
   14726              : 
   14727        14421 :         case EXEC_ALLOCATE:
   14728        14421 :           if (t)
   14729        14419 :             resolve_allocate_deallocate (code, "ALLOCATE");
   14730              : 
   14731              :           break;
   14732              : 
   14733         6123 :         case EXEC_DEALLOCATE:
   14734         6123 :           if (t)
   14735         6123 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   14736              : 
   14737              :           break;
   14738              : 
   14739         3955 :         case EXEC_OPEN:
   14740         3955 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   14741              :             break;
   14742              : 
   14743         3728 :           resolve_branch (code->ext.open->err, code);
   14744         3728 :           break;
   14745              : 
   14746         3148 :         case EXEC_CLOSE:
   14747         3148 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   14748              :             break;
   14749              : 
   14750         3114 :           resolve_branch (code->ext.close->err, code);
   14751         3114 :           break;
   14752              : 
   14753         2851 :         case EXEC_BACKSPACE:
   14754         2851 :         case EXEC_ENDFILE:
   14755         2851 :         case EXEC_REWIND:
   14756         2851 :         case EXEC_FLUSH:
   14757         2851 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   14758              :             break;
   14759              : 
   14760         2785 :           resolve_branch (code->ext.filepos->err, code);
   14761         2785 :           break;
   14762              : 
   14763          838 :         case EXEC_INQUIRE:
   14764          838 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14765              :               break;
   14766              : 
   14767          790 :           resolve_branch (code->ext.inquire->err, code);
   14768          790 :           break;
   14769              : 
   14770           92 :         case EXEC_IOLENGTH:
   14771           92 :           gcc_assert (code->ext.inquire != NULL);
   14772           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14773              :             break;
   14774              : 
   14775           90 :           resolve_branch (code->ext.inquire->err, code);
   14776           90 :           break;
   14777              : 
   14778           89 :         case EXEC_WAIT:
   14779           89 :           if (!gfc_resolve_wait (code->ext.wait))
   14780              :             break;
   14781              : 
   14782           74 :           resolve_branch (code->ext.wait->err, code);
   14783           74 :           resolve_branch (code->ext.wait->end, code);
   14784           74 :           resolve_branch (code->ext.wait->eor, code);
   14785           74 :           break;
   14786              : 
   14787        33456 :         case EXEC_READ:
   14788        33456 :         case EXEC_WRITE:
   14789        33456 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   14790              :             break;
   14791              : 
   14792        33148 :           resolve_branch (code->ext.dt->err, code);
   14793        33148 :           resolve_branch (code->ext.dt->end, code);
   14794        33148 :           resolve_branch (code->ext.dt->eor, code);
   14795        33148 :           break;
   14796              : 
   14797        47475 :         case EXEC_TRANSFER:
   14798        47475 :           resolve_transfer (code);
   14799        47475 :           break;
   14800              : 
   14801         2217 :         case EXEC_DO_CONCURRENT:
   14802         2217 :         case EXEC_FORALL:
   14803         2217 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   14804              : 
   14805         2217 :           if (code->expr1 != NULL
   14806          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   14807            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   14808              :                        "expression", &code->expr1->where);
   14809              : 
   14810         2217 :     if (code->op == EXEC_DO_CONCURRENT)
   14811          224 :       resolve_locality_spec (code, ns);
   14812              :           break;
   14813              : 
   14814        13168 :         case EXEC_OACC_PARALLEL_LOOP:
   14815        13168 :         case EXEC_OACC_PARALLEL:
   14816        13168 :         case EXEC_OACC_KERNELS_LOOP:
   14817        13168 :         case EXEC_OACC_KERNELS:
   14818        13168 :         case EXEC_OACC_SERIAL_LOOP:
   14819        13168 :         case EXEC_OACC_SERIAL:
   14820        13168 :         case EXEC_OACC_DATA:
   14821        13168 :         case EXEC_OACC_HOST_DATA:
   14822        13168 :         case EXEC_OACC_LOOP:
   14823        13168 :         case EXEC_OACC_UPDATE:
   14824        13168 :         case EXEC_OACC_WAIT:
   14825        13168 :         case EXEC_OACC_CACHE:
   14826        13168 :         case EXEC_OACC_ENTER_DATA:
   14827        13168 :         case EXEC_OACC_EXIT_DATA:
   14828        13168 :         case EXEC_OACC_ATOMIC:
   14829        13168 :         case EXEC_OACC_DECLARE:
   14830        13168 :           gfc_resolve_oacc_directive (code, ns);
   14831        13168 :           break;
   14832              : 
   14833        17287 :         case EXEC_OMP_ALLOCATE:
   14834        17287 :         case EXEC_OMP_ALLOCATORS:
   14835        17287 :         case EXEC_OMP_ASSUME:
   14836        17287 :         case EXEC_OMP_ATOMIC:
   14837        17287 :         case EXEC_OMP_BARRIER:
   14838        17287 :         case EXEC_OMP_CANCEL:
   14839        17287 :         case EXEC_OMP_CANCELLATION_POINT:
   14840        17287 :         case EXEC_OMP_CRITICAL:
   14841        17287 :         case EXEC_OMP_FLUSH:
   14842        17287 :         case EXEC_OMP_DEPOBJ:
   14843        17287 :         case EXEC_OMP_DISPATCH:
   14844        17287 :         case EXEC_OMP_DISTRIBUTE:
   14845        17287 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14846        17287 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14847        17287 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   14848        17287 :         case EXEC_OMP_DO:
   14849        17287 :         case EXEC_OMP_DO_SIMD:
   14850        17287 :         case EXEC_OMP_ERROR:
   14851        17287 :         case EXEC_OMP_INTEROP:
   14852        17287 :         case EXEC_OMP_LOOP:
   14853        17287 :         case EXEC_OMP_MASTER:
   14854        17287 :         case EXEC_OMP_MASTER_TASKLOOP:
   14855        17287 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14856        17287 :         case EXEC_OMP_MASKED:
   14857        17287 :         case EXEC_OMP_MASKED_TASKLOOP:
   14858        17287 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14859        17287 :         case EXEC_OMP_METADIRECTIVE:
   14860        17287 :         case EXEC_OMP_ORDERED:
   14861        17287 :         case EXEC_OMP_SCAN:
   14862        17287 :         case EXEC_OMP_SCOPE:
   14863        17287 :         case EXEC_OMP_SECTIONS:
   14864        17287 :         case EXEC_OMP_SIMD:
   14865        17287 :         case EXEC_OMP_SINGLE:
   14866        17287 :         case EXEC_OMP_TARGET:
   14867        17287 :         case EXEC_OMP_TARGET_DATA:
   14868        17287 :         case EXEC_OMP_TARGET_ENTER_DATA:
   14869        17287 :         case EXEC_OMP_TARGET_EXIT_DATA:
   14870        17287 :         case EXEC_OMP_TARGET_PARALLEL:
   14871        17287 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   14872        17287 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14873        17287 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14874        17287 :         case EXEC_OMP_TARGET_SIMD:
   14875        17287 :         case EXEC_OMP_TARGET_TEAMS:
   14876        17287 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14877        17287 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14878        17287 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14879        17287 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14880        17287 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   14881        17287 :         case EXEC_OMP_TARGET_UPDATE:
   14882        17287 :         case EXEC_OMP_TASK:
   14883        17287 :         case EXEC_OMP_TASKGROUP:
   14884        17287 :         case EXEC_OMP_TASKLOOP:
   14885        17287 :         case EXEC_OMP_TASKLOOP_SIMD:
   14886        17287 :         case EXEC_OMP_TASKWAIT:
   14887        17287 :         case EXEC_OMP_TASKYIELD:
   14888        17287 :         case EXEC_OMP_TEAMS:
   14889        17287 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   14890        17287 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14891        17287 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14892        17287 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14893        17287 :         case EXEC_OMP_TEAMS_LOOP:
   14894        17287 :         case EXEC_OMP_TILE:
   14895        17287 :         case EXEC_OMP_UNROLL:
   14896        17287 :         case EXEC_OMP_WORKSHARE:
   14897        17287 :           gfc_resolve_omp_directive (code, ns);
   14898        17287 :           break;
   14899              : 
   14900         3907 :         case EXEC_OMP_PARALLEL:
   14901         3907 :         case EXEC_OMP_PARALLEL_DO:
   14902         3907 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   14903         3907 :         case EXEC_OMP_PARALLEL_LOOP:
   14904         3907 :         case EXEC_OMP_PARALLEL_MASKED:
   14905         3907 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14906         3907 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14907         3907 :         case EXEC_OMP_PARALLEL_MASTER:
   14908         3907 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14909         3907 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14910         3907 :         case EXEC_OMP_PARALLEL_SECTIONS:
   14911         3907 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   14912         3907 :           omp_workshare_save = omp_workshare_flag;
   14913         3907 :           omp_workshare_flag = 0;
   14914         3907 :           gfc_resolve_omp_directive (code, ns);
   14915         3907 :           omp_workshare_flag = omp_workshare_save;
   14916         3907 :           break;
   14917              : 
   14918            0 :         default:
   14919            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   14920              :         }
   14921      1143291 :       gfc_value_used_expr (code->expr2, VALUE_USED);
   14922      1143291 :       gfc_value_used_expr (code->expr3, VALUE_USED);
   14923      1143291 :       gfc_value_used_expr (code->expr4, VALUE_USED);
   14924              :     }
   14925              : 
   14926       689151 :   mark_lhs_assignments_set (orig_code);
   14927              : 
   14928       689151 :   cs_base = frame.prev;
   14929       689151 : }
   14930              : 
   14931              : 
   14932              : /* Resolve initial values and make sure they are compatible with
   14933              :    the variable.  */
   14934              : 
   14935              : static void
   14936      1913226 : resolve_values (gfc_symbol *sym)
   14937              : {
   14938      1913226 :   bool t;
   14939              : 
   14940      1913226 :   if (sym->value == NULL)
   14941              :     return;
   14942              : 
   14943       443342 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   14944           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   14945              :                  "Using parameter %qs declared at %L is deprecated",
   14946              :                  sym->name, &sym->declared_at);
   14947              : 
   14948       443342 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   14949        39895 :     t= resolve_structure_cons (sym->value, 1);
   14950              :   else
   14951       403447 :     t = gfc_resolve_expr (sym->value);
   14952              : 
   14953       443342 :   if (!t)
   14954              :     return;
   14955              : 
   14956       443340 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   14957              : }
   14958              : 
   14959              : 
   14960              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   14961              :    for them once, rather than for each variable declared of that type.  */
   14962              : 
   14963              : static void
   14964      1883186 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   14965              : {
   14966      1883186 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   14967        83843 :       && derived_sym->attr.is_bind_c == 1)
   14968        26909 :     verify_bind_c_derived_type (derived_sym);
   14969              : 
   14970      1883186 :   return;
   14971              : }
   14972              : 
   14973              : 
   14974              : /* Check the interfaces of DTIO procedures associated with derived
   14975              :    type 'sym'.  These procedures can either have typebound bindings or
   14976              :    can appear in DTIO generic interfaces.  */
   14977              : 
   14978              : static void
   14979      1914196 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   14980              : {
   14981      1914196 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   14982              :     return;
   14983              : 
   14984        93362 :   gfc_check_dtio_interfaces (sym);
   14985              : 
   14986        93362 :   return;
   14987              : }
   14988              : 
   14989              : /* Auxiliary function, checks if an argument decays to a pointer.  */
   14990              : 
   14991              : static bool
   14992        65146 : decays_to_pointer (gfc_symbol *sym)
   14993              : {
   14994        65146 :   if (!sym->as)
   14995              :     return true;
   14996              : 
   14997        18849 :   if (sym->as->type == AS_ASSUMED_SHAPE)
   14998              :     return false;
   14999              : 
   15000        15094 :   if (sym->as->type == AS_ASSUMED_RANK)
   15001              :     return false;
   15002              : 
   15003         9996 :   if (sym->as->type == AS_DEFERRED && sym->attr.dummy)
   15004          968 :     return false;
   15005              : 
   15006              :   return true;
   15007              : }
   15008              : 
   15009              : /* Helper function, returns true if the types conform according to the C
   15010              :    standard, when they are not equal on the Fortran side.  If we decide to
   15011              :    include or exclude any types from this, this is the place to change.  */
   15012              : 
   15013              : static bool
   15014          390 : c_types_conform (gfc_typespec *ts1, gfc_typespec *ts2)
   15015              : {
   15016          390 :   if (ts1->type == BT_ASSUMED || ts2->type == BT_ASSUMED)
   15017              :     return true;
   15018              : 
   15019          384 :   if (ts1->kind == ts2->kind
   15020              :       && (ts1->type == BT_CHARACTER || ts1->type == BT_INTEGER
   15021              :           || ts1->type == BT_UNSIGNED)
   15022              :       && (ts2->type == BT_CHARACTER || ts2->type == BT_INTEGER
   15023              :           || ts2->type == BT_UNSIGNED))
   15024          384 :     return true;
   15025              : 
   15026              :   return false;
   15027              : 
   15028              : }
   15029              : 
   15030              : /* Check argument lists of BIND(C) procedures against each other, return
   15031              :    false if they do not. */
   15032              : 
   15033              : static bool
   15034        12113 : compare_c_binding_arglists (gfc_symbol *osym, gfc_symbol *nsym)
   15035              : {
   15036        12113 :   gfc_formal_arglist *oarg, *narg;
   15037        12113 :   bool ret = true;
   15038        12113 :   locus *oloc, *nloc;
   15039              : 
   15040        12113 :   oarg = osym->formal;
   15041        12113 :   narg = nsym->formal;
   15042        12113 :   oloc = &osym->declared_at;
   15043        12113 :   nloc = &nsym->declared_at;
   15044        44696 :   for ( ; oarg && narg ; oarg = oarg->next, narg = narg->next)
   15045              :     {
   15046        32583 :       oloc = &oarg->sym->declared_at;
   15047        32583 :       nloc = &narg->sym->declared_at;
   15048              : 
   15049        32583 :       if (!gfc_compare_types (&oarg->sym->ts, &narg->sym->ts)
   15050        32583 :           && (pedantic || !c_types_conform (&oarg->sym->ts, &narg->sym->ts)))
   15051              :         {
   15052           24 :           gfc_error ("Type mismatch in argument %qs at %L (%s/%s) "
   15053            8 :                      "originally declared at %L", narg->sym->name,
   15054            8 :                      nloc, gfc_typename (&narg->sym->ts),
   15055            8 :                      gfc_typename (&oarg->sym->ts), oloc);
   15056            8 :                      ret = false;
   15057            8 :                      continue;
   15058              :         }
   15059        32575 :       if (oarg->sym->attr.value != narg->sym->attr.value)
   15060              :         {
   15061            1 :           gfc_error ("VALUE attribute mismatch in argument %qs at %L "
   15062              :                      "originally declared at %L",narg->sym->name,
   15063              :                      nloc, oloc);
   15064            1 :           ret = false;
   15065            1 :           continue;
   15066              :         }
   15067              : 
   15068              :       /* According to the Fortran standard, ranks have to match for arguments.
   15069              :          In this case, this makes little sense because both decay to C
   15070              :          pointers.  Only issue an error if -pedantic or if the argument does
   15071              :          not decay to a pointer.  Same thing for CFI_desc arrays, which include
   15072              :          assumed rank.  */
   15073              : 
   15074        32574 :       int orank = gfc_symbol_rank (oarg->sym);
   15075        32574 :       int nrank = gfc_symbol_rank (narg->sym);
   15076        32574 :       if (orank != nrank && pedantic)
   15077              :         {
   15078            1 :           gfc_error ("Rank mismatch in argument %qs (%d/%d) at %L originally "
   15079            1 :                      "declared at %L", narg->sym->name, nrank, orank,  nloc,
   15080              :                      oloc);
   15081            1 :           ret = false;
   15082            1 :           continue;
   15083              :         }
   15084              : 
   15085              :       /* Confusion between CFI_desc and "normal" arrays.  */
   15086              : 
   15087        32573 :       if (decays_to_pointer (oarg->sym) != decays_to_pointer (narg->sym))
   15088              :         {
   15089            1 :           gfc_error ("Array specification mismatch in argument %qs at %L "
   15090              :                      "originally declared at %L", narg->sym->name,
   15091              :                      nloc, oloc);
   15092            1 :           ret = false;
   15093            1 :           continue;
   15094              :         }
   15095              :     }
   15096              : 
   15097        12113 :   if (oarg && !narg)
   15098              :     {
   15099            0 :       gfc_error ("Not enough arguments for procedure %qs with binding label "
   15100              :                  "%qs after %L, originally declared at %L", nsym->name,
   15101            0 :                  nsym->binding_label, nloc, &oarg->sym->declared_at);
   15102            0 :       ret = false;
   15103              :     }
   15104              : 
   15105        12113 :   if (!oarg && narg)
   15106              :     {
   15107            2 :       gfc_error ("Too many arguments for procedure %qs with binding label "
   15108              :                  "%qs at %L, originally declared at %L", nsym->name,
   15109            2 :                  nsym->binding_label, &narg->sym->declared_at, oloc);
   15110            2 :       ret = false;
   15111              :     }
   15112              : 
   15113        12113 :   return ret;
   15114              : }
   15115              : 
   15116              : 
   15117              : /* Verify that any binding labels used in a given namespace do not collide
   15118              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   15119              :    for the same procedure are permitted.  Abstract interfaces and dummy
   15120              :    arguments are not checked.  */
   15121              : 
   15122              : static void
   15123      1914196 : gfc_verify_binding_labels (gfc_symbol *sym)
   15124              : {
   15125      1914196 :   gfc_gsymbol *gsym;
   15126      1914196 :   const char *module;
   15127              : 
   15128      1914196 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   15129        68209 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   15130        40359 :       || sym->attr.abstract || sym->attr.dummy)
   15131              :     return;
   15132              : 
   15133              :   /* Avoid double error reporting.  */
   15134        40223 :   if (sym->error)
   15135              :     return;
   15136              : 
   15137              :   /* TODO: Check the names of reserved external C identifiers here, see
   15138              :      PR 125251.  */
   15139              : 
   15140              :   /* According to the Fortran standard, global identifiers are case
   15141              :      insensitive, which also holds for C identifiers.  This was probably done
   15142              :      for systems which had case-insensitive linkers.  Such systems could not
   15143              :      accommodate the C standards referenced, so this restriction makes little
   15144              :      sense for modern systems. Therefore, check case-sensitive labels unless
   15145              :      -pedantic is in force.  */
   15146              : 
   15147        40223 :   if (pedantic)
   15148         4650 :     gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   15149              :   else
   15150        35573 :     gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
   15151              : 
   15152        40223 :   if (sym->module)
   15153              :     module = sym->module;
   15154        13122 :   else if (sym->ns && sym->ns->proc_name
   15155        13122 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   15156         4580 :     module = sym->ns->proc_name->name;
   15157         8542 :   else if (sym->ns && sym->ns->parent
   15158          358 :            && sym->ns && sym->ns->parent->proc_name
   15159          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15160          272 :     module = sym->ns->parent->proc_name->name;
   15161              :   else
   15162              :     module = NULL;
   15163              : 
   15164        40223 :   if (gsym)
   15165              :     {
   15166        12195 :       if (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)
   15167              :         {
   15168        12154 :           gfc_symbol *global_sym;
   15169        12154 :           gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &global_sym);
   15170              : 
   15171              :           /* For when the symtree does not match the symbol name, which can happen
   15172              :              in modules with PRIVATE.  */
   15173              : 
   15174        12154 :           if (global_sym == NULL)
   15175            1 :             gfc_find_symbol_by_name (gsym->sym_name, gsym->ns, &global_sym);
   15176              : 
   15177        12154 :           gcc_assert (global_sym);
   15178              : 
   15179              :           /* If subroutines and functions are conflated, there is little point
   15180              :              in continuing checks.  */
   15181        12154 :           if ((sym->attr.function && gsym->type == GSYM_SUBROUTINE)
   15182        12154 :               || (sym->attr.subroutine && gsym->type == GSYM_FUNCTION))
   15183              :             {
   15184            1 :               gfc_global_used (gsym, &sym->declared_at);
   15185            1 :               sym->binding_label = NULL;
   15186            1 :               sym->error = 1;
   15187           51 :               return;
   15188              :             }
   15189              : 
   15190         6596 :           if (gsym->type == GSYM_FUNCTION && sym->attr.function
   15191        18749 :               && !gfc_compare_types (&sym->ts, &global_sym->ts))
   15192              :             {
   15193           40 :               gfc_error ("Return type mismatch of function %qs with binding "
   15194              :                          "label %qs at %L (%s/%s), originally declared at %L",
   15195              :                          sym->name, sym->binding_label,
   15196              :                          &sym->declared_at,
   15197              :                          gfc_typename (&sym->ts),
   15198           40 :                          gfc_typename (&global_sym->ts),
   15199              :                          &gsym->where);
   15200           40 :               sym->binding_label = NULL;
   15201           40 :               sym->error = 1;
   15202           40 :               return;
   15203              :             }
   15204        12113 :           if (!compare_c_binding_arglists (global_sym, sym))
   15205              :             {
   15206           10 :               sym->binding_label = NULL;
   15207           10 :               sym->error = 1;
   15208           10 :               return;
   15209              :             }
   15210              :         }
   15211              :     }
   15212              : 
   15213        12103 :   if (!gsym
   15214        12144 :       || (!gsym->defined
   15215         9196 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   15216              :     {
   15217        28028 :       if (!gsym)
   15218        28028 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   15219        37224 :       gsym->where = sym->declared_at;
   15220        37224 :       gsym->sym_name = sym->name;
   15221        37224 :       gsym->binding_label = sym->binding_label;
   15222        37224 :       gsym->ns = sym->ns;
   15223        37224 :       gsym->mod_name = module;
   15224        37224 :       if (sym->attr.function)
   15225        25511 :         gsym->type = GSYM_FUNCTION;
   15226        11713 :       else if (sym->attr.subroutine)
   15227        11574 :         gsym->type = GSYM_SUBROUTINE;
   15228              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   15229        37224 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   15230        37224 :       return;
   15231              :     }
   15232              : 
   15233         2948 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   15234              :     {
   15235            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   15236              :                  "identifier as entity at %L", sym->name,
   15237              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15238              :       /* Clear the binding label to prevent checking multiple times.  */
   15239            1 :       sym->binding_label = NULL;
   15240            1 :       return;
   15241              :     }
   15242              : 
   15243         2947 :   if (sym->attr.flavor == FL_VARIABLE && module
   15244           37 :       && (strcmp (module, gsym->mod_name) != 0
   15245           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   15246              :     {
   15247              :       /* This can only happen if the variable is defined in a module - if it
   15248              :          isn't the same module, reject it.  */
   15249            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   15250              :                  "uses the same global identifier as entity at %L from module %qs",
   15251              :                  sym->name, module, sym->binding_label,
   15252              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   15253            3 :       sym->binding_label = NULL;
   15254            3 :       return;
   15255              :     }
   15256              : 
   15257         2944 :   if ((sym->attr.function || sym->attr.subroutine)
   15258         2908 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   15259         2906 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   15260         2523 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   15261         2091 :       && (module != gsym->mod_name
   15262         2087 :           || strcmp (gsym->sym_name, sym->name) != 0
   15263         2087 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   15264              :     {
   15265              :       /* Print an error if the procedure is defined multiple times; we have to
   15266              :          exclude references to the same procedure via module association or
   15267              :          multiple checks for the same procedure.  */
   15268            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   15269              :                  "global identifier as entity at %L", sym->name,
   15270              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15271            4 :       sym->binding_label = NULL;
   15272            4 :       return;
   15273              :     }
   15274              : }
   15275              : 
   15276              : 
   15277              : /* Resolve an index expression.  */
   15278              : 
   15279              : static bool
   15280       266998 : resolve_index_expr (gfc_expr *e)
   15281              : {
   15282       266998 :   if (!gfc_resolve_expr (e))
   15283              :     return false;
   15284              : 
   15285       266988 :   if (!gfc_simplify_expr (e, 0))
   15286              :     return false;
   15287              : 
   15288       266986 :   if (!gfc_specification_expr (e))
   15289              :     return false;
   15290              : 
   15291              :   return true;
   15292              : }
   15293              : 
   15294              : 
   15295              : /* Resolve a charlen structure.  */
   15296              : 
   15297              : static bool
   15298       104299 : resolve_charlen (gfc_charlen *cl)
   15299              : {
   15300       104299 :   int k;
   15301       104299 :   bool saved_specification_expr;
   15302              : 
   15303       104299 :   if (cl->resolved)
   15304              :     return true;
   15305              : 
   15306        95774 :   cl->resolved = 1;
   15307        95774 :   saved_specification_expr = specification_expr;
   15308        95774 :   specification_expr = true;
   15309              : 
   15310        95774 :   if (cl->length_from_typespec)
   15311              :     {
   15312         2136 :       if (!gfc_resolve_expr (cl->length))
   15313              :         {
   15314            1 :           specification_expr = saved_specification_expr;
   15315            1 :           return false;
   15316              :         }
   15317              : 
   15318         2135 :       if (!gfc_simplify_expr (cl->length, 0))
   15319              :         {
   15320            0 :           specification_expr = saved_specification_expr;
   15321            0 :           return false;
   15322              :         }
   15323              : 
   15324              :       /* cl->length has been resolved.  It should have an integer type.  */
   15325         2135 :       if (cl->length
   15326         2134 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15327              :         {
   15328            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15329              :                      &cl->length->where);
   15330            4 :           return false;
   15331              :         }
   15332              :     }
   15333              :   else
   15334              :     {
   15335        93638 :       if (!resolve_index_expr (cl->length))
   15336              :         {
   15337           19 :           specification_expr = saved_specification_expr;
   15338           19 :           return false;
   15339              :         }
   15340              :     }
   15341              : 
   15342              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15343              :      a negative value, the length of character entities declared is zero.  */
   15344        95750 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15345        57628 :       && mpz_sgn (cl->length->value.integer) < 0)
   15346            0 :     gfc_replace_expr (cl->length,
   15347              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15348              : 
   15349              :   /* Check that the character length is not too large.  */
   15350        95750 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15351        95750 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15352        57628 :       && cl->length->ts.type == BT_INTEGER
   15353        57628 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15354              :     {
   15355            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15356            4 :       specification_expr = saved_specification_expr;
   15357            4 :       return false;
   15358              :     }
   15359              : 
   15360        95746 :   specification_expr = saved_specification_expr;
   15361        95746 :   return true;
   15362              : }
   15363              : 
   15364              : 
   15365              : /* Test for non-constant shape arrays.  */
   15366              : 
   15367              : static bool
   15368       118556 : is_non_constant_shape_array (gfc_symbol *sym)
   15369              : {
   15370       118556 :   gfc_expr *e;
   15371       118556 :   int i;
   15372       118556 :   bool not_constant;
   15373              : 
   15374       118556 :   not_constant = false;
   15375       118556 :   if (sym->as != NULL)
   15376              :     {
   15377              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15378              :          has not been simplified; parameter array references.  Do the
   15379              :          simplification now.  */
   15380       156125 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15381              :         {
   15382        90123 :           if (i == GFC_MAX_DIMENSIONS)
   15383              :             break;
   15384              : 
   15385        90121 :           e = sym->as->lower[i];
   15386        90121 :           if (e && (!resolve_index_expr(e)
   15387        87267 :                     || !gfc_is_constant_expr (e)))
   15388              :             not_constant = true;
   15389        90121 :           e = sym->as->upper[i];
   15390        90121 :           if (e && (!resolve_index_expr(e)
   15391        86065 :                     || !gfc_is_constant_expr (e)))
   15392              :             not_constant = true;
   15393              :         }
   15394              :     }
   15395       118556 :   return not_constant;
   15396              : }
   15397              : 
   15398              : /* Given a symbol and an initialization expression, add code to initialize
   15399              :    the symbol to the function entry.  */
   15400              : static void
   15401         2144 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15402              : {
   15403         2144 :   gfc_expr *lval;
   15404         2144 :   gfc_code *init_st;
   15405         2144 :   gfc_namespace *ns = sym->ns;
   15406              : 
   15407         2144 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15408              :     {
   15409           46 :       gfc_free_expr (init);
   15410           46 :       return;
   15411              :     }
   15412              : 
   15413              :   /* Search for the function namespace if this is a contained
   15414              :      function without an explicit result.  */
   15415         2098 :   if (sym->attr.function && sym == sym->result
   15416          299 :       && sym->name != sym->ns->proc_name->name)
   15417              :     {
   15418          298 :       ns = ns->contained;
   15419         1376 :       for (;ns; ns = ns->sibling)
   15420         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15421              :           break;
   15422              :     }
   15423              : 
   15424         2098 :   if (ns == NULL)
   15425              :     {
   15426           61 :       gfc_free_expr (init);
   15427           61 :       return;
   15428              :     }
   15429              : 
   15430              :   /* Build an l-value expression for the result.  */
   15431         2037 :   lval = gfc_lval_expr_from_sym (sym);
   15432              : 
   15433              :   /* Add the code at scope entry.  */
   15434         2037 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15435         2037 :   init_st->next = ns->code;
   15436         2037 :   ns->code = init_st;
   15437              : 
   15438              :   /* Assign the default initializer to the l-value.  */
   15439         2037 :   init_st->loc = sym->declared_at;
   15440         2037 :   init_st->expr1 = lval;
   15441         2037 :   init_st->expr2 = init;
   15442              : }
   15443              : 
   15444              : 
   15445              : /* Whether or not we can generate a default initializer for a symbol.  */
   15446              : 
   15447              : static bool
   15448        30505 : can_generate_init (gfc_symbol *sym)
   15449              : {
   15450        30505 :   symbol_attribute *a;
   15451        30505 :   if (!sym)
   15452              :     return false;
   15453        30505 :   a = &sym->attr;
   15454              : 
   15455              :   /* These symbols should never have a default initialization.  */
   15456        50255 :   return !(
   15457        30505 :        a->allocatable
   15458        30505 :     || a->external
   15459        29326 :     || a->pointer
   15460        29326 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15461         5781 :         && (CLASS_DATA (sym)->attr.class_pointer
   15462         3787 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15463        27332 :     || a->in_equivalence
   15464        27211 :     || a->in_common
   15465        27164 :     || a->data
   15466        26986 :     || sym->module
   15467        23250 :     || a->cray_pointee
   15468        23188 :     || a->cray_pointer
   15469        23188 :     || sym->assoc
   15470        20431 :     || (!a->referenced && !a->result)
   15471        19750 :     || (a->dummy && (a->intent != INTENT_OUT
   15472         1081 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15473        19750 :     || (a->function && sym != sym->result)
   15474              :   );
   15475              : }
   15476              : 
   15477              : 
   15478              : /* Assign the default initializer to a derived type variable or result.  */
   15479              : 
   15480              : static void
   15481        11653 : apply_default_init (gfc_symbol *sym)
   15482              : {
   15483        11653 :   gfc_expr *init = NULL;
   15484              : 
   15485        11653 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15486              :     return;
   15487              : 
   15488        11408 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15489        10555 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15490              : 
   15491        11408 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15492              :     return;
   15493              : 
   15494         1762 :   build_init_assign (sym, init);
   15495         1762 :   sym->attr.referenced = 1;
   15496              : }
   15497              : 
   15498              : 
   15499              : /* Build an initializer for a local. Returns null if the symbol should not have
   15500              :    a default initialization.  */
   15501              : 
   15502              : static gfc_expr *
   15503       206685 : build_default_init_expr (gfc_symbol *sym)
   15504              : {
   15505              :   /* These symbols should never have a default initialization.  */
   15506       206685 :   if (sym->attr.allocatable
   15507       192854 :       || sym->attr.external
   15508       192854 :       || sym->attr.dummy
   15509       126537 :       || sym->attr.pointer
   15510       118333 :       || sym->attr.in_equivalence
   15511       115957 :       || sym->attr.in_common
   15512       112856 :       || sym->attr.data
   15513       110558 :       || sym->module
   15514       108015 :       || sym->attr.cray_pointee
   15515       107714 :       || sym->attr.cray_pointer
   15516       107412 :       || sym->assoc)
   15517              :     return NULL;
   15518              : 
   15519              :   /* Get the appropriate init expression.  */
   15520       102557 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15521              : }
   15522              : 
   15523              : /* Add an initialization expression to a local variable.  */
   15524              : static void
   15525       206685 : apply_default_init_local (gfc_symbol *sym)
   15526              : {
   15527       206685 :   gfc_expr *init = NULL;
   15528              : 
   15529              :   /* The symbol should be a variable or a function return value.  */
   15530       206685 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15531       206685 :       || (sym->attr.function && sym->result != sym))
   15532              :     return;
   15533              : 
   15534              :   /* Try to build the initializer expression.  If we can't initialize
   15535              :      this symbol, then init will be NULL.  */
   15536       206685 :   init = build_default_init_expr (sym);
   15537       206685 :   if (init == NULL)
   15538              :     return;
   15539              : 
   15540              :   /* For saved variables, we don't want to add an initializer at function
   15541              :      entry, so we just add a static initializer. Note that automatic variables
   15542              :      are stack allocated even with -fno-automatic; we have also to exclude
   15543              :      result variable, which are also nonstatic.  */
   15544          419 :   if (!sym->attr.automatic
   15545          419 :       && (sym->attr.save || sym->ns->save_all
   15546          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15547           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15548           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15549              :     {
   15550              :       /* Don't clobber an existing initializer!  */
   15551           37 :       gcc_assert (sym->value == NULL);
   15552           37 :       sym->value = init;
   15553           37 :       return;
   15554              :     }
   15555              : 
   15556          382 :   build_init_assign (sym, init);
   15557              : }
   15558              : 
   15559              : 
   15560              : /* Resolution of common features of flavors variable and procedure.  */
   15561              : 
   15562              : static bool
   15563       992503 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15564              : {
   15565       992503 :   gfc_array_spec *as;
   15566              : 
   15567       992503 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15568        19686 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15569        19680 :     as = CLASS_DATA (sym)->as;
   15570              :   else
   15571       972823 :     as = sym->as;
   15572              : 
   15573              :   /* Constraints on deferred shape variable.  */
   15574       992503 :   if (as == NULL || as->type != AS_DEFERRED)
   15575              :     {
   15576       967938 :       bool pointer, allocatable, dimension;
   15577              : 
   15578       967938 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15579        16445 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15580              :         {
   15581        16439 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15582        16439 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15583        16439 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15584              :         }
   15585              :       else
   15586              :         {
   15587       951499 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   15588       951499 :           allocatable = sym->attr.allocatable;
   15589       951499 :           dimension = sym->attr.dimension;
   15590              :         }
   15591              : 
   15592       967938 :       if (allocatable)
   15593              :         {
   15594         8116 :           if (dimension
   15595         8116 :               && as
   15596          524 :               && as->type != AS_ASSUMED_RANK
   15597            5 :               && !sym->attr.select_rank_temporary)
   15598              :             {
   15599            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   15600              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   15601            3 :               return false;
   15602              :             }
   15603         8113 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   15604              :                                     "%qs at %L may not be ALLOCATABLE",
   15605              :                                     sym->name, &sym->declared_at))
   15606              :             return false;
   15607              :         }
   15608              : 
   15609       967934 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   15610              :         {
   15611            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   15612              :                      "assumed rank", sym->name, &sym->declared_at);
   15613            4 :           sym->error = 1;
   15614            4 :           return false;
   15615              :         }
   15616              :     }
   15617              :   else
   15618              :     {
   15619        24565 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   15620         4731 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   15621              :         {
   15622            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   15623              :                      sym->name, &sym->declared_at);
   15624            3 :           return false;
   15625              :          }
   15626              :     }
   15627              : 
   15628              :   /* Constraints on polymorphic variables.  */
   15629       992492 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   15630              :     {
   15631              :       /* F03:C502.  */
   15632        19019 :       if (sym->attr.class_ok
   15633        18963 :           && sym->ts.u.derived
   15634        18958 :           && !sym->attr.select_type_temporary
   15635        17824 :           && !UNLIMITED_POLY (sym)
   15636        15277 :           && CLASS_DATA (sym)
   15637        15276 :           && CLASS_DATA (sym)->ts.u.derived
   15638        34294 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   15639              :         {
   15640            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   15641            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   15642              :                      &sym->declared_at);
   15643            5 :           return false;
   15644              :         }
   15645              : 
   15646              :       /* F03:C509.  */
   15647              :       /* Assume that use associated symbols were checked in the module ns.
   15648              :          Class-variables that are associate-names are also something special
   15649              :          and excepted from the test.  */
   15650        19014 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   15651           54 :           && !sym->attr.select_type_temporary
   15652           54 :           && !sym->attr.select_rank_temporary)
   15653              :         {
   15654           54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   15655              :                      "or pointer", sym->name, &sym->declared_at);
   15656           54 :           return false;
   15657              :         }
   15658              :     }
   15659              : 
   15660              :   return true;
   15661              : }
   15662              : 
   15663              : 
   15664              : /* Additional checks for symbols with flavor variable and derived
   15665              :    type.  To be called from resolve_fl_variable.  */
   15666              : 
   15667              : static bool
   15668        82650 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   15669              : {
   15670        82650 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   15671              : 
   15672              :   /* Check to see if a derived type is blocked from being host
   15673              :      associated by the presence of another class I symbol in the same
   15674              :      namespace.  14.6.1.3 of the standard and the discussion on
   15675              :      comp.lang.fortran.  */
   15676        82650 :   if (sym->ts.u.derived
   15677        82645 :       && sym->ns != sym->ts.u.derived->ns
   15678        47720 :       && !sym->ts.u.derived->attr.use_assoc
   15679        17817 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   15680              :     {
   15681        16828 :       gfc_symbol *s;
   15682        16828 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   15683        16828 :       if (s && s->attr.generic)
   15684            2 :         s = gfc_find_dt_in_generic (s);
   15685        16828 :       if (s && !gfc_fl_struct (s->attr.flavor))
   15686              :         {
   15687            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   15688              :                      "because it is blocked by an incompatible object "
   15689              :                      "of the same name declared at %L",
   15690            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   15691              :                      &s->declared_at);
   15692            2 :           return false;
   15693              :         }
   15694              :     }
   15695              : 
   15696              :   /* 4th constraint in section 11.3: "If an object of a type for which
   15697              :      component-initialization is specified (R429) appears in the
   15698              :      specification-part of a module and does not have the ALLOCATABLE
   15699              :      or POINTER attribute, the object shall have the SAVE attribute."
   15700              : 
   15701              :      The check for initializers is performed with
   15702              :      gfc_has_default_initializer because gfc_default_initializer generates
   15703              :      a hidden default for allocatable components.  */
   15704        81971 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   15705        18785 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   15706          423 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   15707           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   15708           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   15709        82657 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   15710              :                           "%qs at %L, needed due to the default "
   15711              :                           "initialization", sym->name, &sym->declared_at))
   15712              :     return false;
   15713              : 
   15714              :   /* Assign default initializer.  */
   15715        82646 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   15716        76333 :       && (!no_init_flag
   15717        59386 :           || (sym->attr.intent == INTENT_OUT
   15718         3177 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   15719        19950 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15720              : 
   15721              :   return true;
   15722              : }
   15723              : 
   15724              : 
   15725              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   15726              :    except in the declaration of an entity or component that has the POINTER
   15727              :    or ALLOCATABLE attribute.  */
   15728              : 
   15729              : static bool
   15730      1562881 : deferred_requirements (gfc_symbol *sym)
   15731              : {
   15732      1562881 :   if (sym->ts.deferred
   15733         7972 :       && !(sym->attr.pointer
   15734         2422 :            || sym->attr.allocatable
   15735           92 :            || sym->attr.associate_var
   15736            7 :            || sym->attr.omp_udr_artificial_var))
   15737              :     {
   15738              :       /* If a function has a result variable, only check the variable.  */
   15739            7 :       if (sym->result && sym->name != sym->result->name)
   15740              :         return true;
   15741              : 
   15742            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   15743              :                  "requires either the POINTER or ALLOCATABLE attribute",
   15744              :                  sym->name, &sym->declared_at);
   15745            6 :       return false;
   15746              :     }
   15747              :   return true;
   15748              : }
   15749              : 
   15750              : 
   15751              : /* Resolve symbols with flavor variable.  */
   15752              : 
   15753              : static bool
   15754       663878 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   15755              : {
   15756       663878 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   15757              :                                  "SAVE attribute");
   15758              : 
   15759       663878 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   15760              :     return false;
   15761              : 
   15762              :   /* Set this flag to check that variables are parameters of all entries.
   15763              :      This check is effected by the call to gfc_resolve_expr through
   15764              :      is_non_constant_shape_array.  */
   15765       663818 :   bool saved_specification_expr = specification_expr;
   15766       663818 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   15767       663818 :   specification_expr = true;
   15768       663818 :   specification_expr_symbol = sym;
   15769              : 
   15770       663818 :   if (sym->ns->proc_name
   15771       663723 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15772       658664 :           || sym->ns->proc_name->attr.is_main_program)
   15773        83344 :       && !sym->attr.use_assoc
   15774        80143 :       && !sym->attr.allocatable
   15775        74333 :       && !sym->attr.pointer
   15776       734483 :       && is_non_constant_shape_array (sym))
   15777              :     {
   15778              :       /* F08:C541. The shape of an array defined in a main program or module
   15779              :        * needs to be constant.  */
   15780            3 :       gfc_error ("The module or main program array %qs at %L must "
   15781              :                  "have constant shape", sym->name, &sym->declared_at);
   15782            3 :       specification_expr = saved_specification_expr;
   15783            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   15784            3 :       return false;
   15785              :     }
   15786              : 
   15787              :   /* Constraints on deferred type parameter.  */
   15788       663815 :   if (!deferred_requirements (sym))
   15789              :     return false;
   15790              : 
   15791       663811 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   15792              :     {
   15793              :       /* Make sure that character string variables with assumed length are
   15794              :          dummy arguments.  */
   15795        36157 :       gfc_expr *e = NULL;
   15796              : 
   15797        36157 :       if (sym->ts.u.cl)
   15798        36157 :         e = sym->ts.u.cl->length;
   15799              :       else
   15800              :         return false;
   15801              : 
   15802        36157 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   15803         2644 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   15804            2 :           && !sym->attr.omp_udr_artificial_var)
   15805              :         {
   15806            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   15807              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   15808            2 :           specification_expr = saved_specification_expr;
   15809            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15810            2 :           return false;
   15811              :         }
   15812              : 
   15813        20992 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   15814              :         {
   15815            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15816            1 :           specification_expr = saved_specification_expr;
   15817            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   15818            1 :           return false;
   15819              :         }
   15820              : 
   15821        36154 :       if (!gfc_is_constant_expr (e)
   15822        36154 :           && !(e->expr_type == EXPR_VARIABLE
   15823         1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   15824              :         {
   15825         2184 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   15826         1680 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15827         1679 :                   || sym->ns->proc_name->attr.is_main_program))
   15828              :             {
   15829            3 :               gfc_error ("%qs at %L must have constant character length "
   15830              :                         "in this context", sym->name, &sym->declared_at);
   15831            3 :               specification_expr = saved_specification_expr;
   15832            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   15833            3 :               return false;
   15834              :             }
   15835         2181 :           if (sym->attr.in_common)
   15836              :             {
   15837            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   15838              :                          "character length", sym->name, &sym->declared_at);
   15839            1 :               specification_expr = saved_specification_expr;
   15840            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   15841            1 :               return false;
   15842              :             }
   15843              :         }
   15844              :     }
   15845              : 
   15846       663804 :   if (sym->value == NULL && sym->attr.referenced
   15847       208619 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   15848       206685 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   15849              : 
   15850              :   /* Determine if the symbol may not have an initializer.  */
   15851       663804 :   int no_init_flag = 0, automatic_flag = 0;
   15852       663804 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   15853       172100 :       || sym->attr.intrinsic || sym->attr.result)
   15854              :     no_init_flag = 1;
   15855       139685 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   15856       174549 :            && is_non_constant_shape_array (sym))
   15857              :     {
   15858         1351 :       no_init_flag = automatic_flag = 1;
   15859              : 
   15860              :       /* Also, they must not have the SAVE attribute.
   15861              :          SAVE_IMPLICIT is checked below.  */
   15862         1351 :       if (sym->as && sym->attr.codimension)
   15863              :         {
   15864            7 :           int corank = sym->as->corank;
   15865            7 :           sym->as->corank = 0;
   15866            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   15867            7 :           sym->as->corank = corank;
   15868              :         }
   15869         1351 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   15870              :         {
   15871            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15872            2 :           specification_expr = saved_specification_expr;
   15873            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15874            2 :           return false;
   15875              :         }
   15876              :     }
   15877              : 
   15878              :   /* Ensure that any initializer is simplified.  */
   15879       663802 :   if (sym->value)
   15880         8246 :     gfc_simplify_expr (sym->value, 1);
   15881              : 
   15882              :   /* Reject illegal initializers.  */
   15883       663802 :   if (!sym->mark && sym->value)
   15884              :     {
   15885         8246 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   15886           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   15887            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   15888              :                    sym->name, &sym->declared_at);
   15889         8245 :       else if (sym->attr.external)
   15890            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   15891              :                    sym->name, &sym->declared_at);
   15892         8245 :       else if (sym->attr.dummy)
   15893            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   15894              :                    sym->name, &sym->declared_at);
   15895         8242 :       else if (sym->attr.intrinsic)
   15896            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   15897              :                    sym->name, &sym->declared_at);
   15898         8242 :       else if (sym->attr.result)
   15899            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   15900              :                    sym->name, &sym->declared_at);
   15901         8241 :       else if (automatic_flag)
   15902            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   15903              :                    sym->name, &sym->declared_at);
   15904              :       else
   15905         8236 :         goto no_init_error;
   15906           10 :       specification_expr = saved_specification_expr;
   15907           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   15908           10 :       return false;
   15909              :     }
   15910              : 
   15911       655556 : no_init_error:
   15912       663792 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   15913              :     {
   15914        82650 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   15915        82650 :       specification_expr = saved_specification_expr;
   15916        82650 :       specification_expr_symbol = saved_specification_expr_symbol;
   15917        82650 :       return res;
   15918              :     }
   15919              : 
   15920       581142 :   specification_expr = saved_specification_expr;
   15921       581142 :   specification_expr_symbol = saved_specification_expr_symbol;
   15922       581142 :   return true;
   15923              : }
   15924              : 
   15925              : 
   15926              : /* Compare the dummy characteristics of a module procedure interface
   15927              :    declaration with the corresponding declaration in a submodule.  */
   15928              : static gfc_formal_arglist *new_formal;
   15929              : static char errmsg[200];
   15930              : 
   15931              : static void
   15932         1351 : compare_fsyms (gfc_symbol *sym)
   15933              : {
   15934         1351 :   gfc_symbol *fsym;
   15935              : 
   15936         1351 :   if (sym == NULL || new_formal == NULL)
   15937              :     return;
   15938              : 
   15939         1351 :   fsym = new_formal->sym;
   15940              : 
   15941         1351 :   if (sym == fsym)
   15942              :     return;
   15943              : 
   15944         1327 :   if (strcmp (sym->name, fsym->name) == 0)
   15945              :     {
   15946          522 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   15947            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   15948              :     }
   15949              : }
   15950              : 
   15951              : 
   15952              : /* Resolve a procedure.  */
   15953              : 
   15954              : static bool
   15955       490624 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   15956              : {
   15957       490624 :   gfc_formal_arglist *arg;
   15958       490624 :   bool allocatable_or_pointer = false;
   15959              : 
   15960       490624 :   if (sym->attr.function
   15961       490624 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   15962              :     return false;
   15963              : 
   15964              :   /* Constraints on deferred type parameter.  */
   15965       490614 :   if (!deferred_requirements (sym))
   15966              :     return false;
   15967              : 
   15968       490613 :   if (sym->ts.type == BT_CHARACTER)
   15969              :     {
   15970        11820 :       gfc_charlen *cl = sym->ts.u.cl;
   15971              : 
   15972         7603 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   15973        13126 :              && !resolve_charlen (cl))
   15974              :         return false;
   15975              : 
   15976        11819 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   15977        10514 :           && sym->attr.proc == PROC_ST_FUNCTION)
   15978              :         {
   15979            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   15980              :                      "have constant length", sym->name, &sym->declared_at);
   15981            0 :           return false;
   15982              :         }
   15983              :     }
   15984              : 
   15985              :   /* Ensure that derived type for are not of a private type.  Internal
   15986              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   15987              :      externally accessible and can access all the objects accessible in
   15988              :      the host.  */
   15989       113202 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   15990       113202 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15991       578055 :       && gfc_check_symbol_access (sym))
   15992              :     {
   15993       457573 :       gfc_interface *iface;
   15994              : 
   15995       973401 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   15996              :         {
   15997       515829 :           if (arg->sym
   15998       515689 :               && arg->sym->ts.type == BT_DERIVED
   15999        42508 :               && arg->sym->ts.u.derived
   16000        42508 :               && !arg->sym->ts.u.derived->attr.use_assoc
   16001         4253 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16002       515838 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   16003              :                                   "and cannot be a dummy argument"
   16004              :                                   " of %qs, which is PUBLIC at %L",
   16005            9 :                                   arg->sym->name, sym->name,
   16006              :                                   &sym->declared_at))
   16007              :             {
   16008              :               /* Stop this message from recurring.  */
   16009            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16010            1 :               return false;
   16011              :             }
   16012              :         }
   16013              : 
   16014              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   16015              :          PRIVATE to the containing module.  */
   16016       650106 :       for (iface = sym->generic; iface; iface = iface->next)
   16017              :         {
   16018       451550 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   16019              :             {
   16020       259016 :               if (arg->sym
   16021       258984 :                   && arg->sym->ts.type == BT_DERIVED
   16022         8009 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   16023          232 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16024       259020 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   16025              :                                       "PUBLIC interface %qs at %L "
   16026              :                                       "takes dummy arguments of %qs which "
   16027              :                                       "is PRIVATE", iface->sym->name,
   16028            4 :                                       sym->name, &iface->sym->declared_at,
   16029            4 :                                       gfc_typename(&arg->sym->ts)))
   16030              :                 {
   16031              :                   /* Stop this message from recurring.  */
   16032            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16033            1 :                   return false;
   16034              :                 }
   16035              :              }
   16036              :         }
   16037              :     }
   16038              : 
   16039       490610 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   16040           86 :       && !sym->attr.proc_pointer)
   16041              :     {
   16042            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   16043              :                  sym->name, &sym->declared_at);
   16044              : 
   16045              :       /* Make sure no second error is issued for this.  */
   16046            2 :       sym->value->error = 1;
   16047            2 :       return false;
   16048              :     }
   16049              : 
   16050              :   /* An external symbol may not have an initializer because it is taken to be
   16051              :      a procedure. Exception: Procedure Pointers.  */
   16052       490608 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   16053              :     {
   16054            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   16055              :                  sym->name, &sym->declared_at);
   16056            0 :       return false;
   16057              :     }
   16058              : 
   16059              :   /* An elemental function is required to return a scalar 12.7.1  */
   16060       490608 :   if (sym->attr.elemental && sym->attr.function
   16061        86524 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   16062            2 :                       && CLASS_DATA (sym)->as)))
   16063              :     {
   16064            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   16065              :                  "result", sym->name, &sym->declared_at);
   16066              :       /* Reset so that the error only occurs once.  */
   16067            3 :       sym->attr.elemental = 0;
   16068            3 :       return false;
   16069              :     }
   16070              : 
   16071       490605 :   if (sym->attr.proc == PROC_ST_FUNCTION
   16072          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   16073              :     {
   16074            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   16075              :                  "allocatable attribute", sym->name, &sym->declared_at);
   16076            2 :       return false;
   16077              :     }
   16078              : 
   16079              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   16080              :      char-len-param shall not be array-valued, pointer-valued, recursive
   16081              :      or pure.  ....snip... A character value of * may only be used in the
   16082              :      following ways: (i) Dummy arg of procedure - dummy associates with
   16083              :      actual length; (ii) To declare a named constant; or (iii) External
   16084              :      function - but length must be declared in calling scoping unit.  */
   16085       490603 :   if (sym->attr.function
   16086       328606 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   16087         6815 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   16088              :     {
   16089          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   16090          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   16091              :         {
   16092            4 :           if (sym->as && sym->as->rank)
   16093            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16094              :                        "array-valued", sym->name, &sym->declared_at);
   16095              : 
   16096            4 :           if (sym->attr.pointer)
   16097            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16098              :                        "pointer-valued", sym->name, &sym->declared_at);
   16099              : 
   16100            4 :           if (sym->attr.pure)
   16101            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16102              :                        "pure", sym->name, &sym->declared_at);
   16103              : 
   16104            4 :           if (sym->attr.recursive)
   16105            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16106              :                        "recursive", sym->name, &sym->declared_at);
   16107              : 
   16108            4 :           return false;
   16109              :         }
   16110              : 
   16111              :       /* Appendix B.2 of the standard.  Contained functions give an
   16112              :          error anyway.  Deferred character length is an F2003 feature.
   16113              :          Don't warn on intrinsic conversion functions, which start
   16114              :          with two underscores.  */
   16115          176 :       if (!sym->attr.contained && !sym->ts.deferred
   16116          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   16117          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   16118              :                         "CHARACTER(*) function %qs at %L",
   16119              :                         sym->name, &sym->declared_at);
   16120              :     }
   16121              : 
   16122              :   /* F2008, C1218.  */
   16123       490599 :   if (sym->attr.elemental)
   16124              :     {
   16125        89778 :       if (sym->attr.proc_pointer)
   16126              :         {
   16127            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   16128              :                                                : sym->name);
   16129            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   16130              :                      name, &sym->declared_at);
   16131            7 :           return false;
   16132              :         }
   16133        89771 :       if (sym->attr.dummy)
   16134              :         {
   16135            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   16136              :                      sym->name, &sym->declared_at);
   16137            3 :           return false;
   16138              :         }
   16139              :     }
   16140              : 
   16141              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   16142              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   16143              :      pointer is tested and caught elsewhere.  */
   16144       490589 :   if (sym->result)
   16145       276301 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   16146       276301 :                              && CLASS_DATA (sym->result) ?
   16147         1669 :                              (CLASS_DATA (sym->result)->attr.allocatable
   16148         1669 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   16149       274632 :                              (sym->result->attr.allocatable
   16150       274632 :                               || sym->result->attr.pointer);
   16151              : 
   16152       490589 :   if (sym->attr.elemental && sym->result
   16153        86141 :       && allocatable_or_pointer)
   16154              :     {
   16155            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   16156              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   16157              :                  "attribute", sym->result->name,
   16158              :                  &sym->result->declared_at, sym->name);
   16159            4 :       return false;
   16160              :     }
   16161              : 
   16162              :   /* F2018:C1585: "The function result of a pure function shall not be both
   16163              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   16164              :      component."  */
   16165       490585 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   16166              :     {
   16167         2520 :       if (sym->ts.type == BT_CLASS
   16168            5 :           && sym->attr.class_ok
   16169            4 :           && CLASS_DATA (sym->result)
   16170            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   16171              :         {
   16172            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   16173              :                      "polymorphic allocatable",
   16174              :                      sym->result->name, &sym->result->declared_at);
   16175            4 :           return false;
   16176              :         }
   16177              : 
   16178         2516 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   16179              :         {
   16180              :           gfc_component *c = sym->ts.u.derived->components;
   16181         4613 :           for (; c; c = c->next)
   16182         2406 :             if (c->ts.type == BT_CLASS
   16183            2 :                 && CLASS_DATA (c)
   16184            2 :                 && CLASS_DATA (c)->attr.allocatable)
   16185              :               {
   16186            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   16187              :                            "polymorphic allocatable component %qs",
   16188              :                            sym->result->name, &sym->result->declared_at,
   16189              :                            c->name);
   16190            2 :                 return false;
   16191              :               }
   16192              :         }
   16193              :     }
   16194              : 
   16195       490579 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   16196              :     {
   16197         7233 :       gfc_formal_arglist *curr_arg;
   16198         7233 :       int has_non_interop_arg = 0;
   16199              : 
   16200         7233 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   16201         7233 :                               sym->common_block))
   16202              :         {
   16203              :           /* Clear these to prevent looking at them again if there was an
   16204              :              error.  */
   16205            2 :           sym->attr.is_bind_c = 0;
   16206            2 :           sym->attr.is_c_interop = 0;
   16207            2 :           sym->ts.is_c_interop = 0;
   16208              :         }
   16209              :       else
   16210              :         {
   16211              :           /* So far, no errors have been found.  */
   16212         7231 :           sym->attr.is_c_interop = 1;
   16213         7231 :           sym->ts.is_c_interop = 1;
   16214              :         }
   16215              : 
   16216         7233 :       curr_arg = gfc_sym_get_dummy_args (sym);
   16217        31838 :       while (curr_arg != NULL)
   16218              :         {
   16219              :           /* Skip implicitly typed dummy args here.  */
   16220        17372 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   16221        17315 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   16222              :               /* If something is found to fail, record the fact so we
   16223              :                  can mark the symbol for the procedure as not being
   16224              :                  BIND(C) to try and prevent multiple errors being
   16225              :                  reported.  */
   16226        17372 :               has_non_interop_arg = 1;
   16227              : 
   16228        17372 :           curr_arg = curr_arg->next;
   16229              :         }
   16230              : 
   16231              :       /* See if any of the arguments were not interoperable and if so, clear
   16232              :          the procedure symbol to prevent duplicate error messages.  */
   16233         7233 :       if (has_non_interop_arg != 0)
   16234              :         {
   16235          128 :           sym->attr.is_c_interop = 0;
   16236          128 :           sym->ts.is_c_interop = 0;
   16237          128 :           sym->attr.is_bind_c = 0;
   16238              :         }
   16239              :     }
   16240              : 
   16241       490579 :   if (!sym->attr.proc_pointer)
   16242              :     {
   16243       489473 :       if (sym->attr.save == SAVE_EXPLICIT)
   16244              :         {
   16245            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   16246              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16247            5 :           return false;
   16248              :         }
   16249       489468 :       if (sym->attr.intent)
   16250              :         {
   16251            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   16252              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16253            1 :           return false;
   16254              :         }
   16255       489467 :       if (sym->attr.subroutine && sym->attr.result)
   16256              :         {
   16257            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   16258            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   16259            2 :           return false;
   16260              :         }
   16261       489465 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   16262       142641 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   16263       142638 :               || sym->attr.contained))
   16264              :         {
   16265            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   16266              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16267            3 :           return false;
   16268              :         }
   16269       489462 :       if (strcmp ("ppr@", sym->name) == 0)
   16270              :         {
   16271            0 :           gfc_error ("Procedure pointer result %qs at %L "
   16272              :                      "is missing the pointer attribute",
   16273            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   16274            0 :           return false;
   16275              :         }
   16276              :     }
   16277              : 
   16278              :   /* Assume that a procedure whose body is not known has references
   16279              :      to external arrays.  */
   16280       490568 :   if (sym->attr.if_source != IFSRC_DECL)
   16281       337155 :     sym->attr.array_outer_dependency = 1;
   16282              : 
   16283              :   /* Compare the characteristics of a module procedure with the
   16284              :      interface declaration. Ideally this would be done with
   16285              :      gfc_compare_interfaces but, at present, the formal interface
   16286              :      cannot be copied to the ts.interface.  */
   16287       490568 :   if (sym->attr.module_procedure
   16288         1612 :       && sym->attr.if_source == IFSRC_DECL)
   16289              :     {
   16290          657 :       gfc_symbol *iface;
   16291          657 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   16292          657 :       char *module_name;
   16293          657 :       char *submodule_name;
   16294          657 :       strcpy (name, sym->ns->proc_name->name);
   16295          657 :       module_name = strtok (name, ".");
   16296          657 :       submodule_name = strtok (NULL, ".");
   16297              : 
   16298          657 :       iface = sym->tlink;
   16299          657 :       sym->tlink = NULL;
   16300              : 
   16301              :       /* Make sure that the result uses the correct charlen for deferred
   16302              :          length results.  */
   16303          657 :       if (iface && sym->result
   16304          192 :           && iface->ts.type == BT_CHARACTER
   16305           19 :           && iface->ts.deferred)
   16306            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16307              : 
   16308            6 :       if (iface == NULL)
   16309          195 :         goto check_formal;
   16310              : 
   16311              :       /* Check the procedure characteristics.  */
   16312          462 :       if (sym->attr.elemental != iface->attr.elemental)
   16313              :         {
   16314            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16315              :                      "PROCEDURE at %L and its interface in %s",
   16316              :                      &sym->declared_at, module_name);
   16317           10 :           return false;
   16318              :         }
   16319              : 
   16320          461 :       if (sym->attr.pure != iface->attr.pure)
   16321              :         {
   16322            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16323              :                      "PROCEDURE at %L and its interface in %s",
   16324              :                      &sym->declared_at, module_name);
   16325            2 :           return false;
   16326              :         }
   16327              : 
   16328          459 :       if (sym->attr.recursive != iface->attr.recursive)
   16329              :         {
   16330            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16331              :                      "PROCEDURE at %L and its interface in %s",
   16332              :                      &sym->declared_at, module_name);
   16333            2 :           return false;
   16334              :         }
   16335              : 
   16336              :       /* Check the result characteristics.  */
   16337          457 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16338              :         {
   16339            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16340              :                      "in MODULE %qs and the declaration at %L in "
   16341              :                      "(SUB)MODULE %qs",
   16342              :                      errmsg, module_name, &sym->declared_at,
   16343              :                      submodule_name ? submodule_name : module_name);
   16344            5 :           return false;
   16345              :         }
   16346              : 
   16347          452 : check_formal:
   16348              :       /* Check the characteristics of the formal arguments.  */
   16349          647 :       if (sym->formal && sym->formal_ns)
   16350              :         {
   16351         1256 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16352              :             {
   16353          720 :               new_formal = arg;
   16354          720 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16355              :             }
   16356              :         }
   16357              :     }
   16358              : 
   16359              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16360              :      BIND(C) attribute.  */
   16361       490558 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16362              :     {
   16363            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16364              :                  sym->name, &sym->declared_at);
   16365            1 :       return false;
   16366              :     }
   16367              : 
   16368              :   return true;
   16369              : }
   16370              : 
   16371              : 
   16372              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16373              :    been defined and we now know their defined arguments, check that they fulfill
   16374              :    the requirements of the standard for procedures used as finalizers.  */
   16375              : 
   16376              : static bool
   16377       112899 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16378              : {
   16379       112899 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16380       112899 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16381       112899 :   bool result = true;
   16382       112899 :   bool seen_scalar = false;
   16383       112899 :   gfc_symbol *vtab;
   16384       112899 :   gfc_component *c;
   16385       112899 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16386              : 
   16387       112899 :   if (parent)
   16388        15822 :     gfc_resolve_finalizers (parent, finalizable);
   16389              : 
   16390              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16391       112899 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16392       353765 :   for (c = derived->components; c; c = c->next)
   16393       240866 :     if (c->ts.type == BT_DERIVED
   16394        67871 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16395              :       {
   16396         8394 :         bool has_final2 = false;
   16397         8394 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16398            0 :           return false;  /* Error.  */
   16399         8394 :         has_final = has_final || has_final2;
   16400              :       }
   16401              :   /* Return early if not finalizable.  */
   16402       112899 :   if (!has_final)
   16403              :     {
   16404       110202 :       if (finalizable)
   16405         8284 :         *finalizable = false;
   16406       110202 :       return true;
   16407              :     }
   16408              : 
   16409              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16410              :      the template. If the finalizers field has the same value, it needs to be
   16411              :      supplied with finalizers of the same pdt_type.  */
   16412         2697 :   if (derived->attr.pdt_type
   16413           54 :       && derived->template_sym
   16414           24 :       && derived->template_sym->f2k_derived
   16415           24 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16416         2721 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16417              :     {
   16418           24 :       gfc_finalizer *tmp = NULL;
   16419           24 :       derived->f2k_derived->finalizers = NULL;
   16420           24 :       prev_link = &derived->f2k_derived->finalizers;
   16421           84 :       for (list = pdt_finalizers; list; list = list->next)
   16422              :         {
   16423           60 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16424           60 :           if (args->sym
   16425           60 :               && args->sym->ts.type == BT_DERIVED
   16426           60 :               && args->sym->ts.u.derived
   16427           60 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16428              :             {
   16429           36 :               tmp = gfc_get_finalizer ();
   16430           36 :               *tmp = *list;
   16431           36 :               tmp->next = NULL;
   16432           36 :               *prev_link = tmp;
   16433           36 :               prev_link = &(tmp->next);
   16434           36 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16435              :             }
   16436              :         }
   16437              :     }
   16438              : 
   16439              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16440              :      does not fit in with the standard's definition, print an error and remove
   16441              :      it from the list.  */
   16442         2697 :   prev_link = &derived->f2k_derived->finalizers;
   16443         5554 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16444              :     {
   16445         2857 :       gfc_formal_arglist *dummy_args;
   16446         2857 :       gfc_symbol* arg;
   16447         2857 :       gfc_finalizer* i;
   16448         2857 :       int my_rank;
   16449              : 
   16450              :       /* Skip this finalizer if we already resolved it.  */
   16451         2857 :       if (list->proc_tree)
   16452              :         {
   16453         2294 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16454          602 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16455         1692 :             seen_scalar = true;
   16456         2294 :           prev_link = &(list->next);
   16457         2294 :           continue;
   16458              :         }
   16459              : 
   16460              :       /* Check this exists and is a SUBROUTINE.  */
   16461          563 :       if (!list->proc_sym->attr.subroutine)
   16462              :         {
   16463            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16464              :                      list->proc_sym->name, &list->where);
   16465            3 :           goto error;
   16466              :         }
   16467              : 
   16468              :       /* We should have exactly one argument.  */
   16469          560 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16470          560 :       if (!dummy_args || dummy_args->next)
   16471              :         {
   16472            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16473              :                      &list->where);
   16474            2 :           goto error;
   16475              :         }
   16476          558 :       arg = dummy_args->sym;
   16477              : 
   16478          558 :       if (!arg)
   16479              :         {
   16480            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16481            1 :                      &list->proc_sym->declared_at, derived->name);
   16482            1 :           goto error;
   16483              :         }
   16484              : 
   16485          557 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16486            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16487              :         {
   16488            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16489              :                      "be the only finalizer with the same kind/type "
   16490              :                      "(F2018: C790)", &list->where);
   16491            0 :           goto error;
   16492              :         }
   16493              : 
   16494              :       /* This argument must be of our type.  */
   16495          557 :       if (!derived->attr.pdt_template
   16496          545 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16497              :         {
   16498            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16499              :                      &arg->declared_at, derived->name);
   16500            2 :           goto error;
   16501              :         }
   16502              : 
   16503              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16504          555 :       if (arg->attr.pointer)
   16505              :         {
   16506            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16507              :                      &arg->declared_at);
   16508            1 :           goto error;
   16509              :         }
   16510          554 :       if (arg->attr.allocatable)
   16511              :         {
   16512            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16513              :                      " ALLOCATABLE", &arg->declared_at);
   16514            1 :           goto error;
   16515              :         }
   16516          553 :       if (arg->attr.optional)
   16517              :         {
   16518            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16519              :                      &arg->declared_at);
   16520            1 :           goto error;
   16521              :         }
   16522              : 
   16523              :       /* It must not be INTENT(OUT).  */
   16524          552 :       if (arg->attr.intent == INTENT_OUT)
   16525              :         {
   16526            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16527              :                      " INTENT(OUT)", &arg->declared_at);
   16528            1 :           goto error;
   16529              :         }
   16530              : 
   16531              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16532          551 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16533            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16534            2 :         gfc_warning (OPT_Wsurprising,
   16535              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16536              :                      " shape argument", &arg->declared_at);
   16537              : 
   16538              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16539              :          defined earlier.  To really loop over the *earlier* declarations,
   16540              :          we need to walk the tail of the list as new ones were pushed at the
   16541              :          front.  */
   16542              :       /* TODO: Handle kind parameters once they are implemented.  */
   16543          551 :       my_rank = (arg->as ? arg->as->rank : 0);
   16544          658 :       for (i = list->next; i; i = i->next)
   16545              :         {
   16546          109 :           gfc_formal_arglist *dummy_args;
   16547              : 
   16548              :           /* Argument list might be empty; that is an error signalled earlier,
   16549              :              but we nevertheless continued resolving.  */
   16550          109 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16551          109 :           if (dummy_args && !derived->attr.pdt_template)
   16552              :             {
   16553          107 :               gfc_symbol* i_arg = dummy_args->sym;
   16554          107 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16555          107 :               if (i_rank == my_rank)
   16556              :                 {
   16557            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16558              :                              " rank (%d) as %qs",
   16559            2 :                              list->proc_sym->name, &list->where, my_rank,
   16560            2 :                              i->proc_sym->name);
   16561            2 :                   goto error;
   16562              :                 }
   16563              :             }
   16564              :         }
   16565              : 
   16566              :         /* Is this the/a scalar finalizer procedure?  */
   16567          549 :         if (my_rank == 0)
   16568          417 :           seen_scalar = true;
   16569              : 
   16570              :         /* Find the symtree for this procedure.  */
   16571          549 :         gcc_assert (!list->proc_tree);
   16572          549 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16573              : 
   16574          549 :         prev_link = &list->next;
   16575          549 :         continue;
   16576              : 
   16577              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16578              :            troubles in the future when they might fail later expectations.  */
   16579           14 : error:
   16580           14 :         i = list;
   16581           14 :         *prev_link = list->next;
   16582           14 :         gfc_free_finalizer (i);
   16583           14 :         result = false;
   16584          549 :     }
   16585              : 
   16586         2697 :   if (result == false)
   16587              :     return false;
   16588              : 
   16589              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   16590              :      were nodes in the list, must have been for arrays.  It is surely a good
   16591              :      idea to have a scalar version there if there's something to finalize.  */
   16592         2693 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   16593            1 :     gfc_warning (OPT_Wsurprising,
   16594              :                  "Only array FINAL procedures declared for derived type %qs"
   16595              :                  " defined at %L, suggest also scalar one unless an assumed"
   16596              :                  " rank finalizer has been declared",
   16597              :                  derived->name, &derived->declared_at);
   16598              : 
   16599         2693 :   if (!derived->attr.pdt_template)
   16600              :     {
   16601         2645 :       vtab = gfc_find_derived_vtab (derived);
   16602         2645 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   16603         2645 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   16604         2645 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   16605              :     }
   16606              : 
   16607         2693 :   if (finalizable)
   16608          664 :     *finalizable = true;
   16609              : 
   16610              :   return true;
   16611              : }
   16612              : 
   16613              : 
   16614              : static gfc_symbol * containing_dt;
   16615              : 
   16616              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   16617              :    arguments whose declared types are PDT instances only transmit the PASS arg
   16618              :    if they match the enclosing derived type.  */
   16619              : 
   16620              : static bool
   16621         1496 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   16622              : {
   16623         1496 :   gfc_formal_arglist *dummy_args;
   16624         1496 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   16625              :     {
   16626          532 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   16627         1190 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   16628          126 :         dummy_args = dummy_args->next;
   16629          532 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   16630          532 :       if (dummy_args->sym->ts.type == BT_CLASS
   16631          532 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   16632              :                      containing_dt->name))
   16633              :         return true;
   16634              :     }
   16635              :   return false;
   16636              : }
   16637              : 
   16638              : 
   16639              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   16640              : 
   16641              : static bool
   16642          750 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   16643              :                              const char* generic_name, locus where)
   16644              : {
   16645          750 :   gfc_symbol *sym1, *sym2;
   16646          750 :   const char *pass1, *pass2;
   16647          750 :   gfc_formal_arglist *dummy_args;
   16648              : 
   16649          750 :   gcc_assert (t1->specific && t2->specific);
   16650          750 :   gcc_assert (!t1->specific->is_generic);
   16651          750 :   gcc_assert (!t2->specific->is_generic);
   16652          750 :   gcc_assert (t1->is_operator == t2->is_operator);
   16653              : 
   16654          750 :   sym1 = t1->specific->u.specific->n.sym;
   16655          750 :   sym2 = t2->specific->u.specific->n.sym;
   16656              : 
   16657          750 :   if (sym1 == sym2)
   16658              :     return true;
   16659              : 
   16660              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   16661          750 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   16662          748 :       || sym1->attr.function != sym2->attr.function)
   16663              :     {
   16664            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   16665              :                  " GENERIC %qs at %L",
   16666              :                  sym1->name, sym2->name, generic_name, &where);
   16667            2 :       return false;
   16668              :     }
   16669              : 
   16670              :   /* Determine PASS arguments.  */
   16671          748 :   if (t1->specific->nopass)
   16672              :     pass1 = NULL;
   16673          697 :   else if (t1->specific->pass_arg)
   16674              :     pass1 = t1->specific->pass_arg;
   16675              :   else
   16676              :     {
   16677          438 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   16678          438 :       if (dummy_args)
   16679          437 :         pass1 = dummy_args->sym->name;
   16680              :       else
   16681              :         pass1 = NULL;
   16682              :     }
   16683          748 :   if (t2->specific->nopass)
   16684              :     pass2 = NULL;
   16685          696 :   else if (t2->specific->pass_arg)
   16686              :     pass2 = t2->specific->pass_arg;
   16687              :   else
   16688              :     {
   16689          559 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   16690          559 :       if (dummy_args)
   16691          558 :         pass2 = dummy_args->sym->name;
   16692              :       else
   16693              :         pass2 = NULL;
   16694              :     }
   16695              : 
   16696              :   /* Care must be taken with pdt types and templates because the declared type
   16697              :      of the argument that is not 'no_pass' need not be the same as the
   16698              :      containing derived type.  If this is the case, subject the argument to
   16699              :      the full interface check, even though it cannot be used in the type
   16700              :      bound context.  */
   16701          748 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   16702          748 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   16703              : 
   16704          748 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   16705          748 :     pass1 = pass2 = NULL;
   16706              : 
   16707              :   /* Compare the interfaces.  */
   16708          748 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   16709              :                               NULL, 0, pass1, pass2))
   16710              :     {
   16711            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   16712              :                  sym1->name, sym2->name, generic_name, &where);
   16713            8 :       return false;
   16714              :     }
   16715              : 
   16716              :   return true;
   16717              : }
   16718              : 
   16719              : 
   16720              : /* Worker function for resolving a generic procedure binding; this is used to
   16721              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   16722              : 
   16723              :    The difference between those cases is finding possible inherited bindings
   16724              :    that are overridden, as one has to look for them in tb_sym_root,
   16725              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   16726              :    the super-type and set p->overridden correctly.  */
   16727              : 
   16728              : static bool
   16729         2409 : resolve_tb_generic_targets (gfc_symbol* super_type,
   16730              :                             gfc_typebound_proc* p, const char* name)
   16731              : {
   16732         2409 :   gfc_tbp_generic* target;
   16733         2409 :   gfc_symtree* first_target;
   16734         2409 :   gfc_symtree* inherited;
   16735              : 
   16736         2409 :   gcc_assert (p && p->is_generic);
   16737              : 
   16738              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   16739         2409 :   gcc_assert (p->u.generic);
   16740         5422 :   for (target = p->u.generic; target; target = target->next)
   16741         3030 :     if (!target->specific)
   16742              :       {
   16743         2615 :         gfc_typebound_proc* overridden_tbp;
   16744         2615 :         gfc_tbp_generic* g;
   16745         2615 :         const char* target_name;
   16746              : 
   16747         2615 :         target_name = target->specific_st->name;
   16748              : 
   16749              :         /* Defined for this type directly.  */
   16750         2615 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   16751              :           {
   16752         2606 :             target->specific = target->specific_st->n.tb;
   16753         2606 :             goto specific_found;
   16754              :           }
   16755              : 
   16756              :         /* Look for an inherited specific binding.  */
   16757            9 :         if (super_type)
   16758              :           {
   16759            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   16760              :                                                  true, NULL);
   16761              : 
   16762            5 :             if (inherited)
   16763              :               {
   16764            5 :                 gcc_assert (inherited->n.tb);
   16765            5 :                 target->specific = inherited->n.tb;
   16766            5 :                 goto specific_found;
   16767              :               }
   16768              :           }
   16769              : 
   16770            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   16771              :                    " at %L", target_name, name, &p->where);
   16772            4 :         return false;
   16773              : 
   16774              :         /* Once we've found the specific binding, check it is not ambiguous with
   16775              :            other specifics already found or inherited for the same GENERIC.  */
   16776         2611 : specific_found:
   16777         2611 :         gcc_assert (target->specific);
   16778              : 
   16779              :         /* This must really be a specific binding!  */
   16780         2611 :         if (target->specific->is_generic)
   16781              :           {
   16782            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   16783              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   16784            3 :             return false;
   16785              :           }
   16786              : 
   16787              :         /* Check those already resolved on this type directly.  */
   16788         6666 :         for (g = p->u.generic; g; g = g->next)
   16789         1464 :           if (g != target && g->specific
   16790         4797 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   16791              :             return false;
   16792              : 
   16793              :         /* Check for ambiguity with inherited specific targets.  */
   16794         2617 :         for (overridden_tbp = p->overridden; overridden_tbp;
   16795           16 :              overridden_tbp = overridden_tbp->overridden)
   16796           19 :           if (overridden_tbp->is_generic)
   16797              :             {
   16798           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   16799              :                 {
   16800           18 :                   gcc_assert (g->specific);
   16801           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   16802              :                     return false;
   16803              :                 }
   16804              :             }
   16805              :       }
   16806              : 
   16807              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   16808         2392 :   if (p->overridden && !p->overridden->is_generic)
   16809              :     {
   16810            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   16811              :                  " the same name", name, &p->where);
   16812            1 :       return false;
   16813              :     }
   16814              : 
   16815              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   16816              :      all must have the same attributes here.  */
   16817         2391 :   first_target = p->u.generic->specific->u.specific;
   16818         2391 :   gcc_assert (first_target);
   16819         2391 :   p->subroutine = first_target->n.sym->attr.subroutine;
   16820         2391 :   p->function = first_target->n.sym->attr.function;
   16821              : 
   16822         2391 :   return true;
   16823              : }
   16824              : 
   16825              : 
   16826              : /* Resolve a GENERIC procedure binding for a derived type.  */
   16827              : 
   16828              : static bool
   16829         1249 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   16830              : {
   16831         1249 :   gfc_symbol* super_type;
   16832              : 
   16833              :   /* Find the overridden binding if any.  */
   16834         1249 :   st->n.tb->overridden = NULL;
   16835         1249 :   super_type = gfc_get_derived_super_type (derived);
   16836         1249 :   if (super_type)
   16837              :     {
   16838           40 :       gfc_symtree* overridden;
   16839           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   16840              :                                             true, NULL);
   16841              : 
   16842           40 :       if (overridden && overridden->n.tb)
   16843           21 :         st->n.tb->overridden = overridden->n.tb;
   16844              :     }
   16845              : 
   16846              :   /* Resolve using worker function.  */
   16847         1249 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   16848              : }
   16849              : 
   16850              : 
   16851              : /* Retrieve the target-procedure of an operator binding and do some checks in
   16852              :    common for intrinsic and user-defined type-bound operators.  */
   16853              : 
   16854              : static gfc_symbol*
   16855         1232 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   16856              : {
   16857         1232 :   gfc_symbol* target_proc;
   16858              : 
   16859         1232 :   gcc_assert (target->specific && !target->specific->is_generic);
   16860         1232 :   target_proc = target->specific->u.specific->n.sym;
   16861         1232 :   gcc_assert (target_proc);
   16862              : 
   16863              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   16864         1232 :   if (target->specific->nopass)
   16865              :     {
   16866            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   16867            2 :       return NULL;
   16868              :     }
   16869              : 
   16870              :   return target_proc;
   16871              : }
   16872              : 
   16873              : 
   16874              : /* Resolve a type-bound intrinsic operator.  */
   16875              : 
   16876              : static bool
   16877         1047 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   16878              :                                 gfc_typebound_proc* p)
   16879              : {
   16880         1047 :   gfc_symbol* super_type;
   16881         1047 :   gfc_tbp_generic* target;
   16882              : 
   16883              :   /* If there's already an error here, do nothing (but don't fail again).  */
   16884         1047 :   if (p->error)
   16885              :     return true;
   16886              : 
   16887              :   /* Operators should always be GENERIC bindings.  */
   16888         1047 :   gcc_assert (p->is_generic);
   16889              : 
   16890              :   /* Look for an overridden binding.  */
   16891         1047 :   super_type = gfc_get_derived_super_type (derived);
   16892         1047 :   if (super_type && super_type->f2k_derived)
   16893            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   16894              :                                                      op, true, NULL);
   16895              :   else
   16896         1046 :     p->overridden = NULL;
   16897              : 
   16898              :   /* Resolve general GENERIC properties using worker function.  */
   16899         1047 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   16900            1 :     goto error;
   16901              : 
   16902              :   /* Check the targets to be procedures of correct interface.  */
   16903         2139 :   for (target = p->u.generic; target; target = target->next)
   16904              :     {
   16905         1118 :       gfc_symbol* target_proc;
   16906              : 
   16907         1118 :       target_proc = get_checked_tb_operator_target (target, p->where);
   16908         1118 :       if (!target_proc)
   16909            1 :         goto error;
   16910              : 
   16911         1117 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   16912            3 :         goto error;
   16913              : 
   16914              :       /* Add target to non-typebound operator list.  */
   16915         1114 :       if (!target->specific->deferred && !derived->attr.use_assoc
   16916          391 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   16917              :         {
   16918          389 :           gfc_interface *head, *intr;
   16919              : 
   16920              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   16921              :              mechanism for handling module procedures winds up resolving
   16922              :              operator interfaces twice and would otherwise cause an error.
   16923              :              Likewise, new instances of PDTs can cause the operator inter-
   16924              :              faces to be resolved multiple times.  */
   16925          461 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   16926           91 :             if (intr->sym == target_proc
   16927           21 :                 && (target_proc->attr.used_in_submodule
   16928            4 :                     || derived->attr.pdt_type
   16929            2 :                     || derived->attr.pdt_template))
   16930              :               return true;
   16931              : 
   16932          370 :           if (!gfc_check_new_interface (derived->ns->op[op],
   16933              :                                         target_proc, p->where))
   16934              :             return false;
   16935          368 :           head = derived->ns->op[op];
   16936          368 :           intr = gfc_get_interface ();
   16937          368 :           intr->sym = target_proc;
   16938          368 :           intr->where = p->where;
   16939          368 :           intr->next = head;
   16940          368 :           derived->ns->op[op] = intr;
   16941              :         }
   16942              :     }
   16943              : 
   16944              :   return true;
   16945              : 
   16946            5 : error:
   16947            5 :   p->error = 1;
   16948            5 :   return false;
   16949              : }
   16950              : 
   16951              : 
   16952              : /* Resolve a type-bound user operator (tree-walker callback).  */
   16953              : 
   16954              : static gfc_symbol* resolve_bindings_derived;
   16955              : static bool resolve_bindings_result;
   16956              : 
   16957              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   16958              : 
   16959              : static void
   16960          113 : resolve_typebound_user_op (gfc_symtree* stree)
   16961              : {
   16962          113 :   gfc_symbol* super_type;
   16963          113 :   gfc_tbp_generic* target;
   16964              : 
   16965          113 :   gcc_assert (stree && stree->n.tb);
   16966              : 
   16967          113 :   if (stree->n.tb->error)
   16968              :     return;
   16969              : 
   16970              :   /* Operators should always be GENERIC bindings.  */
   16971          113 :   gcc_assert (stree->n.tb->is_generic);
   16972              : 
   16973              :   /* Find overridden procedure, if any.  */
   16974          113 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16975          113 :   if (super_type && super_type->f2k_derived)
   16976              :     {
   16977           18 :       gfc_symtree* overridden;
   16978           18 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   16979              :                                                stree->name, true, NULL);
   16980              : 
   16981           18 :       if (overridden && overridden->n.tb)
   16982            0 :         stree->n.tb->overridden = overridden->n.tb;
   16983              :     }
   16984              :   else
   16985           95 :     stree->n.tb->overridden = NULL;
   16986              : 
   16987              :   /* Resolve basically using worker function.  */
   16988          113 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   16989            0 :     goto error;
   16990              : 
   16991              :   /* Check the targets to be functions of correct interface.  */
   16992          224 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   16993              :     {
   16994          114 :       gfc_symbol* target_proc;
   16995              : 
   16996          114 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   16997          114 :       if (!target_proc)
   16998            1 :         goto error;
   16999              : 
   17000          113 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   17001            2 :         goto error;
   17002              :     }
   17003              : 
   17004              :   return;
   17005              : 
   17006            3 : error:
   17007            3 :   resolve_bindings_result = false;
   17008            3 :   stree->n.tb->error = 1;
   17009              : }
   17010              : 
   17011              : 
   17012              : /* Resolve the type-bound procedures for a derived type.  */
   17013              : 
   17014              : static void
   17015        10183 : resolve_typebound_procedure (gfc_symtree* stree)
   17016              : {
   17017        10183 :   gfc_symbol* proc;
   17018        10183 :   locus where;
   17019        10183 :   gfc_symbol* me_arg;
   17020        10183 :   gfc_symbol* super_type;
   17021        10183 :   gfc_component* comp;
   17022              : 
   17023        10183 :   gcc_assert (stree);
   17024              : 
   17025              :   /* Undefined specific symbol from GENERIC target definition.  */
   17026        10183 :   if (!stree->n.tb)
   17027        10101 :     return;
   17028              : 
   17029        10177 :   if (stree->n.tb->error)
   17030              :     return;
   17031              : 
   17032              :   /* If this is a GENERIC binding, use that routine.  */
   17033        10161 :   if (stree->n.tb->is_generic)
   17034              :     {
   17035         1249 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   17036           17 :         goto error;
   17037              :       return;
   17038              :     }
   17039              : 
   17040              :   /* Get the target-procedure to check it.  */
   17041         8912 :   gcc_assert (!stree->n.tb->is_generic);
   17042         8912 :   gcc_assert (stree->n.tb->u.specific);
   17043         8912 :   proc = stree->n.tb->u.specific->n.sym;
   17044         8912 :   where = stree->n.tb->where;
   17045              : 
   17046              :   /* Default access should already be resolved from the parser.  */
   17047         8912 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   17048              : 
   17049         8912 :   if (stree->n.tb->deferred)
   17050              :     {
   17051          676 :       if (!check_proc_interface (proc, &where))
   17052            5 :         goto error;
   17053              :     }
   17054              :   else
   17055              :     {
   17056              :       /* If proc has not been resolved at this point, proc->name may
   17057              :          actually be a USE associated entity. See PR fortran/89647. */
   17058         8236 :       if (!proc->resolve_symbol_called
   17059         5698 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   17060              :         {
   17061           11 :           gfc_symbol *tmp;
   17062           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   17063           11 :           if (tmp && tmp->attr.use_assoc)
   17064              :             {
   17065            1 :               proc->module = tmp->module;
   17066            1 :               proc->attr.proc = tmp->attr.proc;
   17067            1 :               proc->attr.function = tmp->attr.function;
   17068            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   17069            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   17070            1 :               proc->ts = tmp->ts;
   17071            1 :               proc->result = tmp->result;
   17072              :             }
   17073              :         }
   17074              : 
   17075              :       /* Check for F08:C465.  */
   17076         8236 :       if ((!proc->attr.subroutine && !proc->attr.function)
   17077         8226 :           || (proc->attr.proc != PROC_MODULE
   17078           70 :               && proc->attr.if_source != IFSRC_IFBODY
   17079            7 :               && !proc->attr.module_procedure)
   17080         8225 :           || proc->attr.abstract)
   17081              :         {
   17082           12 :           gfc_error ("%qs must be a module procedure or an external "
   17083              :                      "procedure with an explicit interface at %L",
   17084              :                      proc->name, &where);
   17085           12 :           goto error;
   17086              :         }
   17087              :     }
   17088              : 
   17089         8895 :   stree->n.tb->subroutine = proc->attr.subroutine;
   17090         8895 :   stree->n.tb->function = proc->attr.function;
   17091              : 
   17092              :   /* Find the super-type of the current derived type.  We could do this once and
   17093              :      store in a global if speed is needed, but as long as not I believe this is
   17094              :      more readable and clearer.  */
   17095         8895 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17096              : 
   17097              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   17098              :      from a .mod file.  */
   17099         8895 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   17100              :     {
   17101         2838 :       gfc_formal_arglist *dummy_args;
   17102              : 
   17103         2838 :       dummy_args = gfc_sym_get_dummy_args (proc);
   17104         2838 :       if (stree->n.tb->pass_arg)
   17105              :         {
   17106          468 :           gfc_formal_arglist *i;
   17107              : 
   17108              :           /* If an explicit passing argument name is given, walk the arg-list
   17109              :              and look for it.  */
   17110              : 
   17111          468 :           me_arg = NULL;
   17112          468 :           stree->n.tb->pass_arg_num = 1;
   17113          601 :           for (i = dummy_args; i; i = i->next)
   17114              :             {
   17115          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   17116              :                 {
   17117              :                   me_arg = i->sym;
   17118              :                   break;
   17119              :                 }
   17120          133 :               ++stree->n.tb->pass_arg_num;
   17121              :             }
   17122              : 
   17123          468 :           if (!me_arg)
   17124              :             {
   17125            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   17126              :                          " argument %qs",
   17127              :                          proc->name, stree->n.tb->pass_arg, &where,
   17128              :                          stree->n.tb->pass_arg);
   17129            2 :               goto error;
   17130              :             }
   17131              :         }
   17132              :       else
   17133              :         {
   17134              :           /* Otherwise, take the first one; there should in fact be at least
   17135              :              one.  */
   17136         2370 :           stree->n.tb->pass_arg_num = 1;
   17137         2370 :           if (!dummy_args)
   17138              :             {
   17139            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   17140              :                          " least one argument", proc->name, &where);
   17141            2 :               goto error;
   17142              :             }
   17143         2368 :           me_arg = dummy_args->sym;
   17144              :         }
   17145              : 
   17146              :       /* Now check that the argument-type matches and the passed-object
   17147              :          dummy argument is generally fine.  */
   17148              : 
   17149         2368 :       gcc_assert (me_arg);
   17150              : 
   17151         2834 :       if (me_arg->ts.type != BT_CLASS)
   17152              :         {
   17153            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17154              :                      " at %L", proc->name, &where);
   17155            5 :           goto error;
   17156              :         }
   17157              : 
   17158              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   17159         2829 :       if (!resolve_bindings_derived->attr.pdt_template
   17160         2820 :           && !(containing_dt && containing_dt->attr.pdt_type
   17161           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   17162         2800 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   17163              :         {
   17164            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17165              :                      "the derived-type %qs", me_arg->name, proc->name,
   17166              :                      me_arg->name, &where, resolve_bindings_derived->name);
   17167            0 :           goto error;
   17168              :         }
   17169              : 
   17170         2829 :       if (resolve_bindings_derived->attr.pdt_template
   17171         2838 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   17172            9 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   17173              :         {
   17174            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17175              :                      "the parametric derived-type %qs", me_arg->name,
   17176              :                      proc->name, me_arg->name, &where,
   17177              :                      resolve_bindings_derived->name);
   17178            0 :           goto error;
   17179              :         }
   17180              : 
   17181         2829 :       if (((resolve_bindings_derived->attr.pdt_template
   17182            9 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   17183            9 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   17184         2820 :            || resolve_bindings_derived->attr.pdt_type)
   17185           69 :           && (me_arg->param_list != NULL)
   17186         2898 :           && (gfc_spec_list_type (me_arg->param_list,
   17187           69 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   17188              :                                   != SPEC_ASSUMED))
   17189              :         {
   17190              : 
   17191              :           /* Add a check to verify if there are any LEN parameters in the
   17192              :              first place.  If there are LEN parameters, throw this error.
   17193              :              If there are only KIND parameters, then don't trigger
   17194              :              this error.  */
   17195            6 :           gfc_component *c;
   17196            6 :           bool seen_len_param = false;
   17197            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   17198              : 
   17199            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   17200              :             {
   17201            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   17202              :                                      me_arg_param->name, true, true, NULL);
   17203              : 
   17204            6 :               gcc_assert (c != NULL);
   17205              : 
   17206            6 :               if (c->attr.pdt_kind)
   17207            0 :                 continue;
   17208              : 
   17209              :               /* Getting here implies that there is a pdt_len parameter
   17210              :                  in the list.  */
   17211              :               seen_len_param = true;
   17212              :               break;
   17213              :             }
   17214              : 
   17215            6 :             if (seen_len_param)
   17216              :               {
   17217            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   17218              :                            "argument %qs of %qs at %L must be ASSUMED.",
   17219              :                            me_arg->name, proc->name, &where);
   17220            6 :                 goto error;
   17221              :               }
   17222              :         }
   17223              : 
   17224         2823 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   17225         2823 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   17226              :         {
   17227            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   17228              :                      " scalar", proc->name, &where);
   17229            1 :           goto error;
   17230              :         }
   17231         2822 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17232              :         {
   17233            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17234              :                      " be ALLOCATABLE", proc->name, &where);
   17235            2 :           goto error;
   17236              :         }
   17237         2820 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17238              :         {
   17239            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17240              :                      " be POINTER", proc->name, &where);
   17241            2 :           goto error;
   17242              :         }
   17243              :     }
   17244              : 
   17245              :   /* If we are extending some type, check that we don't override a procedure
   17246              :      flagged NON_OVERRIDABLE.  */
   17247         8875 :   stree->n.tb->overridden = NULL;
   17248         8875 :   if (super_type)
   17249              :     {
   17250         1513 :       gfc_symtree* overridden;
   17251         1513 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   17252              :                                             stree->name, true, NULL);
   17253              : 
   17254         1513 :       if (overridden)
   17255              :         {
   17256         1218 :           if (overridden->n.tb)
   17257         1218 :             stree->n.tb->overridden = overridden->n.tb;
   17258              : 
   17259         1218 :           if (!gfc_check_typebound_override (stree, overridden))
   17260           26 :             goto error;
   17261              :         }
   17262              :     }
   17263              : 
   17264              :   /* See if there's a name collision with a component directly in this type.  */
   17265        21237 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   17266        12389 :     if (!strcmp (comp->name, stree->name))
   17267              :       {
   17268            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   17269              :                    " %qs",
   17270              :                    stree->name, &where, resolve_bindings_derived->name);
   17271            1 :         goto error;
   17272              :       }
   17273              : 
   17274              :   /* Try to find a name collision with an inherited component.  */
   17275         8848 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   17276              :                                         NULL))
   17277              :     {
   17278            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   17279              :                  " component of %qs",
   17280              :                  stree->name, &where, resolve_bindings_derived->name);
   17281            1 :       goto error;
   17282              :     }
   17283              : 
   17284         8847 :   stree->n.tb->error = 0;
   17285         8847 :   return;
   17286              : 
   17287           82 : error:
   17288           82 :   resolve_bindings_result = false;
   17289           82 :   stree->n.tb->error = 1;
   17290              : }
   17291              : 
   17292              : 
   17293              : static bool
   17294        86538 : resolve_typebound_procedures (gfc_symbol* derived)
   17295              : {
   17296        86538 :   int op;
   17297        86538 :   gfc_symbol* super_type;
   17298              : 
   17299              :   /* Resolve the super-type first so that inherited bindings (including
   17300              :      user operators) are fully resolved before we look them up via
   17301              :      gfc_find_typebound_user_op.  This must happen even when 'derived'
   17302              :      has no direct type-bound bindings of its own.  */
   17303        86538 :   super_type = gfc_get_derived_super_type (derived);
   17304        86538 :   if (super_type)
   17305        13472 :     resolve_symbol (super_type);
   17306              : 
   17307        86538 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17308              :     return true;
   17309              : 
   17310         4870 :   resolve_bindings_derived = derived;
   17311         4870 :   resolve_bindings_result = true;
   17312              : 
   17313         4870 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17314         4870 :   if (derived->f2k_derived->tb_sym_root)
   17315         4870 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17316              :                           &resolve_typebound_procedure);
   17317              : 
   17318         4870 :   if (derived->f2k_derived->tb_uop_root)
   17319           91 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17320              :                           &resolve_typebound_user_op);
   17321         4870 :   containing_dt = NULL;
   17322              : 
   17323       141230 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17324              :     {
   17325       136360 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17326       136360 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17327              :                                                 (gfc_intrinsic_op)op, p))
   17328            7 :         resolve_bindings_result = false;
   17329              :     }
   17330              : 
   17331         4870 :   return resolve_bindings_result;
   17332              : }
   17333              : 
   17334              : 
   17335              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17336              :    to give all identical derived types the same backend_decl.  */
   17337              : static void
   17338       177705 : add_dt_to_dt_list (gfc_symbol *derived)
   17339              : {
   17340       177705 :   if (!derived->dt_next)
   17341              :     {
   17342        82684 :       if (gfc_derived_types)
   17343              :         {
   17344        67518 :           derived->dt_next = gfc_derived_types->dt_next;
   17345        67518 :           gfc_derived_types->dt_next = derived;
   17346              :         }
   17347              :       else
   17348              :         {
   17349        15166 :           derived->dt_next = derived;
   17350              :         }
   17351        82684 :       gfc_derived_types = derived;
   17352              :     }
   17353       177705 : }
   17354              : 
   17355              : 
   17356              : /* Ensure that a derived-type is really not abstract, meaning that every
   17357              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17358              : 
   17359              : static bool
   17360         7092 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17361              : {
   17362         7092 :   if (!st)
   17363              :     return true;
   17364              : 
   17365         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17366              :     return false;
   17367         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17368              :     return false;
   17369              : 
   17370         2771 :   if (st->n.tb && st->n.tb->deferred)
   17371              :     {
   17372         2019 :       gfc_symtree* overriding;
   17373         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17374         2019 :       if (!overriding)
   17375              :         return false;
   17376         2018 :       gcc_assert (overriding->n.tb);
   17377         2018 :       if (overriding->n.tb->deferred)
   17378              :         {
   17379            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17380              :                      " %qs is DEFERRED and not overridden",
   17381              :                      sub->name, &sub->declared_at, st->name);
   17382            5 :           return false;
   17383              :         }
   17384              :     }
   17385              : 
   17386              :   return true;
   17387              : }
   17388              : 
   17389              : static bool
   17390         1400 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17391              : {
   17392              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17393              :      and for each ancestor-type, check all bindings.  If any of them is
   17394              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17395              :      binding is not DEFERRED.
   17396              :      This is not the most efficient way to do this, but it should be ok and is
   17397              :      clearer than something sophisticated.  */
   17398              : 
   17399         1549 :   gcc_assert (ancestor && !sub->attr.abstract);
   17400              : 
   17401         1549 :   if (!ancestor->attr.abstract)
   17402              :     return true;
   17403              : 
   17404              :   /* Walk bindings of this ancestor.  */
   17405         1548 :   if (ancestor->f2k_derived)
   17406              :     {
   17407         1548 :       bool t;
   17408         1548 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17409         1548 :       if (!t)
   17410              :         return false;
   17411              :     }
   17412              : 
   17413              :   /* Find next ancestor type and recurse on it.  */
   17414         1542 :   ancestor = gfc_get_derived_super_type (ancestor);
   17415         1542 :   if (ancestor)
   17416              :     return ensure_not_abstract (sub, ancestor);
   17417              : 
   17418              :   return true;
   17419              : }
   17420              : 
   17421              : 
   17422              : /* This check for typebound defined assignments is done recursively
   17423              :    since the order in which derived types are resolved is not always in
   17424              :    order of the declarations.  */
   17425              : 
   17426              : static void
   17427       182276 : check_defined_assignments (gfc_symbol *derived)
   17428              : {
   17429       182276 :   gfc_component *c;
   17430              : 
   17431       610988 :   for (c = derived->components; c; c = c->next)
   17432              :     {
   17433       430489 :       if (!gfc_bt_struct (c->ts.type)
   17434       104302 :           || c->attr.pointer
   17435        20806 :           || c->attr.proc_pointer_comp
   17436        20806 :           || c->attr.class_pointer
   17437        20800 :           || c->attr.proc_pointer)
   17438       410223 :         continue;
   17439              : 
   17440        20266 :       if (c->ts.u.derived->attr.defined_assign_comp
   17441        20031 :           || (c->ts.u.derived->f2k_derived
   17442        19449 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17443              :         {
   17444         1753 :           derived->attr.defined_assign_comp = 1;
   17445         1753 :           return;
   17446              :         }
   17447              : 
   17448        18513 :       if (c->attr.allocatable)
   17449         6751 :         continue;
   17450              : 
   17451        11762 :       check_defined_assignments (c->ts.u.derived);
   17452        11762 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17453              :         {
   17454           24 :           derived->attr.defined_assign_comp = 1;
   17455           24 :           return;
   17456              :         }
   17457              :     }
   17458              : }
   17459              : 
   17460              : 
   17461              : /* Resolve a single component of a derived type or structure.  */
   17462              : 
   17463              : static bool
   17464       410362 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17465              : {
   17466       410362 :   gfc_symbol *super_type;
   17467       410362 :   symbol_attribute *attr;
   17468              : 
   17469       410362 :   if (c->attr.artificial)
   17470              :     return true;
   17471              : 
   17472              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17473              :      such as block data because the procedure pointers will cause ICEs
   17474              :      and vtables are not needed in these contexts.  */
   17475       280281 :   if (sym->attr.vtype && sym->attr.use_assoc
   17476        48026 :       && sym->ns->proc_name == NULL)
   17477              :     return true;
   17478              : 
   17479              :   /* F2008, C442.  */
   17480       280272 :   if ((!sym->attr.is_class || c != sym->components)
   17481       280272 :       && c->attr.codimension
   17482          212 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17483              :     {
   17484            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17485              :                  "deferred shape", c->name, &c->loc);
   17486            4 :       return false;
   17487              :     }
   17488              : 
   17489              :   /* F2008, C443.  */
   17490       280268 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17491           85 :       && c->ts.u.derived->ts.is_iso_c)
   17492              :     {
   17493            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17494              :                  "shall not be a coarray", c->name, &c->loc);
   17495            1 :       return false;
   17496              :     }
   17497              : 
   17498              :   /* F2008, C444.  */
   17499       280267 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17500           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17501           26 :           || c->attr.allocatable))
   17502              :     {
   17503            3 :       gfc_error ("Component %qs at %L with coarray component "
   17504              :                  "shall be a nonpointer, nonallocatable scalar",
   17505              :                  c->name, &c->loc);
   17506            3 :       return false;
   17507              :     }
   17508              : 
   17509              :   /* F2008, C448.  */
   17510       280264 :   if (c->ts.type == BT_CLASS)
   17511              :     {
   17512         6992 :       if (c->attr.class_ok && CLASS_DATA (c))
   17513              :         {
   17514         6984 :           attr = &(CLASS_DATA (c)->attr);
   17515              : 
   17516              :           /* Fix up contiguous attribute.  */
   17517         6984 :           if (c->attr.contiguous)
   17518           11 :             attr->contiguous = 1;
   17519              :         }
   17520              :       else
   17521              :         attr = NULL;
   17522              :     }
   17523              :   else
   17524       273272 :     attr = &c->attr;
   17525              : 
   17526       280267 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17527              :     {
   17528            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17529              :                  "is not an array pointer", c->name, &c->loc);
   17530            5 :       return false;
   17531              :     }
   17532              : 
   17533              :   /* F2003, 15.2.1 - length has to be one.  */
   17534        40728 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17535       280278 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17536           19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17537           19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17538              :     {
   17539            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17540              :                  c->name, &c->loc);
   17541            1 :       return false;
   17542              :     }
   17543              : 
   17544        52201 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17545          313 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17546       280266 :       && !(gfc_get_derived_super_type (sym)
   17547            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17548            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17549              :     {
   17550            8 :       gfc_actual_arglist *type_spec_list;
   17551            8 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17552              :                                 &type_spec_list)
   17553              :           != MATCH_YES)
   17554            0 :         return false;
   17555            8 :       gfc_free_actual_arglist (c->param_list);
   17556            8 :       c->param_list = type_spec_list;
   17557            8 :       if (!sym->attr.pdt_type)
   17558            8 :         sym->attr.pdt_comp = 1;
   17559              :     }
   17560       280250 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17561           54 :     sym->attr.pdt_comp = 1;
   17562              : 
   17563       280258 :   if (c->attr.proc_pointer && c->ts.interface)
   17564              :     {
   17565        14894 :       gfc_symbol *ifc = c->ts.interface;
   17566              : 
   17567        14894 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17568              :         {
   17569            6 :           c->tb->error = 1;
   17570            6 :           return false;
   17571              :         }
   17572              : 
   17573        14888 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17574              :         {
   17575              :           /* Resolve interface and copy attributes.  */
   17576        14839 :           if (ifc->formal && !ifc->formal_ns)
   17577         2605 :             resolve_symbol (ifc);
   17578        14839 :           if (ifc->attr.intrinsic)
   17579            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17580              : 
   17581        14839 :           if (ifc->result)
   17582              :             {
   17583         7747 :               c->ts = ifc->result->ts;
   17584         7747 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17585         7747 :               c->attr.pointer = ifc->result->attr.pointer;
   17586         7747 :               c->attr.dimension = ifc->result->attr.dimension;
   17587         7747 :               c->as = gfc_copy_array_spec (ifc->result->as);
   17588         7747 :               c->attr.class_ok = ifc->result->attr.class_ok;
   17589              :             }
   17590              :           else
   17591              :             {
   17592         7092 :               c->ts = ifc->ts;
   17593         7092 :               c->attr.allocatable = ifc->attr.allocatable;
   17594         7092 :               c->attr.pointer = ifc->attr.pointer;
   17595         7092 :               c->attr.dimension = ifc->attr.dimension;
   17596         7092 :               c->as = gfc_copy_array_spec (ifc->as);
   17597         7092 :               c->attr.class_ok = ifc->attr.class_ok;
   17598              :             }
   17599        14839 :           c->ts.interface = ifc;
   17600        14839 :           c->attr.function = ifc->attr.function;
   17601        14839 :           c->attr.subroutine = ifc->attr.subroutine;
   17602              : 
   17603        14839 :           c->attr.pure = ifc->attr.pure;
   17604        14839 :           c->attr.elemental = ifc->attr.elemental;
   17605        14839 :           c->attr.recursive = ifc->attr.recursive;
   17606        14839 :           c->attr.always_explicit = ifc->attr.always_explicit;
   17607        14839 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   17608              :           /* Copy char length.  */
   17609        14839 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   17610              :             {
   17611          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   17612          454 :               if (cl->length && !cl->resolved
   17613          601 :                   && !gfc_resolve_expr (cl->length))
   17614              :                 {
   17615            0 :                   c->tb->error = 1;
   17616            0 :                   return false;
   17617              :                 }
   17618          491 :               c->ts.u.cl = cl;
   17619              :             }
   17620              :         }
   17621              :     }
   17622       265364 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   17623              :     {
   17624              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   17625              :          interface must be a subroutine.  */
   17626          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   17627              :     }
   17628              : 
   17629              :   /* Procedure pointer components: Check PASS arg.  */
   17630       280252 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   17631          560 :       && !sym->attr.vtype)
   17632              :     {
   17633           95 :       gfc_symbol* me_arg;
   17634              : 
   17635           95 :       if (c->tb->pass_arg)
   17636              :         {
   17637           20 :           gfc_formal_arglist* i;
   17638              : 
   17639              :           /* If an explicit passing argument name is given, walk the arg-list
   17640              :             and look for it.  */
   17641              : 
   17642           20 :           me_arg = NULL;
   17643           20 :           c->tb->pass_arg_num = 1;
   17644           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   17645              :             {
   17646           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   17647              :                 {
   17648              :                   me_arg = i->sym;
   17649              :                   break;
   17650              :                 }
   17651           14 :               c->tb->pass_arg_num++;
   17652              :             }
   17653              : 
   17654           20 :           if (!me_arg)
   17655              :             {
   17656            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   17657              :                          "at %L has no argument %qs", c->name,
   17658              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   17659            1 :               c->tb->error = 1;
   17660            1 :               return false;
   17661              :             }
   17662              :         }
   17663              :       else
   17664              :         {
   17665              :           /* Otherwise, take the first one; there should in fact be at least
   17666              :             one.  */
   17667           75 :           c->tb->pass_arg_num = 1;
   17668           75 :           if (!c->ts.interface->formal)
   17669              :             {
   17670            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   17671              :                          "must have at least one argument",
   17672              :                          c->name, &c->loc);
   17673            3 :               c->tb->error = 1;
   17674            3 :               return false;
   17675              :             }
   17676           72 :           me_arg = c->ts.interface->formal->sym;
   17677              :         }
   17678              : 
   17679              :       /* Now check that the argument-type matches.  */
   17680           72 :       gcc_assert (me_arg);
   17681           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   17682           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   17683           90 :           || (me_arg->ts.type == BT_CLASS
   17684           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   17685              :         {
   17686            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   17687              :                      " the derived type %qs", me_arg->name, c->name,
   17688              :                      me_arg->name, &c->loc, sym->name);
   17689            1 :           c->tb->error = 1;
   17690            1 :           return false;
   17691              :         }
   17692              : 
   17693              :       /* Check for F03:C453.  */
   17694           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   17695              :         {
   17696            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17697              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   17698              :                      &c->loc);
   17699            1 :           c->tb->error = 1;
   17700            1 :           return false;
   17701              :         }
   17702              : 
   17703           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17704              :         {
   17705            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17706              :                      "may not have the POINTER attribute", me_arg->name,
   17707              :                      c->name, me_arg->name, &c->loc);
   17708            1 :           c->tb->error = 1;
   17709            1 :           return false;
   17710              :         }
   17711              : 
   17712           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17713              :         {
   17714            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17715              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   17716              :                      me_arg->name, &c->loc);
   17717            1 :           c->tb->error = 1;
   17718            1 :           return false;
   17719              :         }
   17720              : 
   17721           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   17722              :         {
   17723            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17724              :                      " at %L", c->name, &c->loc);
   17725            2 :           return false;
   17726              :         }
   17727              : 
   17728              :     }
   17729              : 
   17730              :   /* Check type-spec if this is not the parent-type component.  */
   17731       280242 :   if (((sym->attr.is_class
   17732        12551 :         && (!sym->components->ts.u.derived->attr.extension
   17733         2400 :             || c != CLASS_DATA (sym->components)))
   17734       269042 :        || (!sym->attr.is_class
   17735       267691 :            && (!sym->attr.extension || c != sym->components)))
   17736       272064 :       && !sym->attr.vtype
   17737       443548 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   17738              :     return false;
   17739              : 
   17740       280241 :   super_type = gfc_get_derived_super_type (sym);
   17741              : 
   17742              :   /* If this type is an extension, set the accessibility of the parent
   17743              :      component.  */
   17744       280241 :   if (super_type
   17745        25806 :       && ((sym->attr.is_class
   17746        12551 :            && c == CLASS_DATA (sym->components))
   17747        17023 :           || (!sym->attr.is_class && c == sym->components))
   17748        15610 :       && strcmp (super_type->name, c->name) == 0)
   17749         6641 :     c->attr.access = super_type->attr.access;
   17750              : 
   17751              :   /* If this type is an extension, see if this component has the same name
   17752              :      as an inherited type-bound procedure.  */
   17753        25806 :   if (super_type && !sym->attr.is_class
   17754        13255 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   17755              :     {
   17756            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   17757              :                  " inherited type-bound procedure",
   17758              :                  c->name, sym->name, &c->loc);
   17759            1 :       return false;
   17760              :     }
   17761              : 
   17762       280240 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   17763         9471 :       && !c->ts.deferred)
   17764              :     {
   17765         7212 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   17766          258 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   17767              : 
   17768         7212 :       if (c->ts.u.cl->length == NULL
   17769         7206 :           || !resolve_charlen(c->ts.u.cl)
   17770        14417 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   17771              :         {
   17772            9 :           gfc_error ("Character length of component %qs needs to "
   17773              :                      "be a constant specification expression at %L",
   17774              :                      c->name,
   17775            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   17776            9 :           return false;
   17777              :         }
   17778              : 
   17779         7203 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   17780              :         {
   17781            2 :          if (!c->ts.u.cl->length->error)
   17782              :            {
   17783            1 :              gfc_error ("Character length expression of component %qs at %L "
   17784              :                         "must be of INTEGER type, found %s",
   17785            1 :                         c->name, &c->ts.u.cl->length->where,
   17786              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   17787            1 :              c->ts.u.cl->length->error = 1;
   17788              :            }
   17789            2 :          return false;
   17790              :        }
   17791              :     }
   17792              : 
   17793       280229 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   17794         2295 :       && !c->attr.pointer && !c->attr.allocatable)
   17795              :     {
   17796            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   17797              :                  "length must be a POINTER or ALLOCATABLE",
   17798              :                  c->name, sym->name, &c->loc);
   17799            1 :       return false;
   17800              :     }
   17801              : 
   17802              :   /* Add the hidden deferred length field.  */
   17803       280228 :   if (c->ts.type == BT_CHARACTER
   17804         9971 :       && (c->ts.deferred || c->attr.pdt_string)
   17805         2469 :       && !c->attr.function
   17806         2433 :       && !sym->attr.is_class)
   17807              :     {
   17808         2286 :       char name[GFC_MAX_SYMBOL_LEN+9];
   17809         2286 :       gfc_component *strlen;
   17810         2286 :       sprintf (name, "_%s_length", c->name);
   17811         2286 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   17812         2286 :       if (strlen == NULL)
   17813              :         {
   17814          484 :           if (!gfc_add_component (sym, name, &strlen))
   17815            0 :             return false;
   17816          484 :           strlen->ts.type = BT_INTEGER;
   17817          484 :           strlen->ts.kind = gfc_charlen_int_kind;
   17818          484 :           strlen->attr.access = ACCESS_PRIVATE;
   17819          484 :           strlen->attr.artificial = 1;
   17820              :         }
   17821              :     }
   17822              : 
   17823       280228 :   if (c->ts.type == BT_DERIVED
   17824        52411 :       && sym->component_access != ACCESS_PRIVATE
   17825        51391 :       && gfc_check_symbol_access (sym)
   17826       100746 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   17827        50314 :       && !c->ts.u.derived->attr.use_assoc
   17828        27285 :       && !gfc_check_symbol_access (c->ts.u.derived)
   17829       280425 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   17830              :                           "PRIVATE type and cannot be a component of "
   17831              :                           "%qs, which is PUBLIC at %L", c->name,
   17832              :                           sym->name, &sym->declared_at))
   17833              :     return false;
   17834              : 
   17835       280227 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   17836              :     {
   17837            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   17838              :                  "type %s", c->name, &c->loc, sym->name);
   17839            2 :       return false;
   17840              :     }
   17841              : 
   17842       280225 :   if (sym->attr.sequence)
   17843              :     {
   17844         2505 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   17845              :         {
   17846            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   17847              :                      "not have the SEQUENCE attribute",
   17848              :                      c->ts.u.derived->name, &sym->declared_at);
   17849            0 :           return false;
   17850              :         }
   17851              :     }
   17852              : 
   17853       280225 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   17854            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   17855       280225 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   17856         7326 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   17857            0 :     CLASS_DATA (c)->ts.u.derived
   17858            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   17859              : 
   17860              :   /* If an allocatable component derived type is of the same type as
   17861              :      the enclosing derived type, we need a vtable generating so that
   17862              :      the __deallocate procedure is created.  */
   17863       280225 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   17864        59747 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   17865          399 :     gfc_find_vtab (&c->ts);
   17866              : 
   17867              :   /* Ensure that all the derived type components are put on the
   17868              :      derived type list; even in formal namespaces, where derived type
   17869              :      pointer components might not have been declared.  */
   17870       280225 :   if (c->ts.type == BT_DERIVED
   17871        52410 :       && c->ts.u.derived
   17872        52410 :       && c->ts.u.derived->components
   17873        49136 :       && c->attr.pointer
   17874        33543 :       && sym != c->ts.u.derived)
   17875         4277 :     add_dt_to_dt_list (c->ts.u.derived);
   17876              : 
   17877       280225 :   if (c->as && c->as->type != AS_DEFERRED
   17878         6488 :       && (c->attr.pointer || c->attr.allocatable))
   17879              :     return false;
   17880              : 
   17881       280211 :   if (!gfc_resolve_array_spec (c->as,
   17882       280211 :                                !(c->attr.pointer || c->attr.proc_pointer
   17883       227939 :                                  || c->attr.allocatable)))
   17884              :     return false;
   17885              : 
   17886       106716 :   if (c->initializer && !sym->attr.vtype
   17887        32733 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   17888       309770 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   17889              :     return false;
   17890              : 
   17891              :   return true;
   17892              : }
   17893              : 
   17894              : 
   17895              : /* Be nice about the locus for a structure expression - show the locus of the
   17896              :    first non-null sub-expression if we can.  */
   17897              : 
   17898              : static locus *
   17899            4 : cons_where (gfc_expr *struct_expr)
   17900              : {
   17901            4 :   gfc_constructor *cons;
   17902              : 
   17903            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   17904              : 
   17905            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   17906           12 :   for (; cons; cons = gfc_constructor_next (cons))
   17907              :     {
   17908            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   17909            4 :         return &cons->expr->where;
   17910              :     }
   17911              : 
   17912            0 :   return &struct_expr->where;
   17913              : }
   17914              : 
   17915              : /* Resolve the components of a structure type. Much less work than derived
   17916              :    types.  */
   17917              : 
   17918              : static bool
   17919          913 : resolve_fl_struct (gfc_symbol *sym)
   17920              : {
   17921          913 :   gfc_component *c;
   17922          913 :   gfc_expr *init = NULL;
   17923          913 :   bool success;
   17924              : 
   17925              :   /* Make sure UNIONs do not have overlapping initializers.  */
   17926          913 :   if (sym->attr.flavor == FL_UNION)
   17927              :     {
   17928          498 :       for (c = sym->components; c; c = c->next)
   17929              :         {
   17930          331 :           if (init && c->initializer)
   17931              :             {
   17932            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   17933              :                          cons_where (init), cons_where (c->initializer));
   17934            2 :               gfc_free_expr (c->initializer);
   17935            2 :               c->initializer = NULL;
   17936              :             }
   17937          291 :           if (init == NULL)
   17938          291 :             init = c->initializer;
   17939              :         }
   17940              :     }
   17941              : 
   17942          913 :   success = true;
   17943         2830 :   for (c = sym->components; c; c = c->next)
   17944         1917 :     if (!resolve_component (c, sym))
   17945            0 :       success = false;
   17946              : 
   17947          913 :   if (!success)
   17948              :     return false;
   17949              : 
   17950          913 :   if (sym->components)
   17951          862 :     add_dt_to_dt_list (sym);
   17952              : 
   17953              :   return true;
   17954              : }
   17955              : 
   17956              : /* Figure if the derived type is using itself directly in one of its components
   17957              :    or through referencing other derived types.  The information is required to
   17958              :    generate the __deallocate and __final type bound procedures to ensure
   17959              :    freeing larger hierarchies of derived types with allocatable objects.  */
   17960              : 
   17961              : static void
   17962       138465 : resolve_cyclic_derived_type (gfc_symbol *derived)
   17963              : {
   17964       138465 :   hash_set<gfc_symbol *> seen, to_examin;
   17965       138465 :   gfc_component *c;
   17966       138465 :   seen.add (derived);
   17967       138465 :   to_examin.add (derived);
   17968       464688 :   while (!to_examin.is_empty ())
   17969              :     {
   17970       189950 :       gfc_symbol *cand = *to_examin.begin ();
   17971       189950 :       to_examin.remove (cand);
   17972       512545 :       for (c = cand->components; c; c = c->next)
   17973       324787 :         if (c->ts.type == BT_DERIVED)
   17974              :           {
   17975        71992 :             if (c->ts.u.derived == derived)
   17976              :               {
   17977         1168 :                 derived->attr.recursive = 1;
   17978         2192 :                 return;
   17979              :               }
   17980        70824 :             else if (!seen.contains (c->ts.u.derived))
   17981              :               {
   17982        46870 :                 seen.add (c->ts.u.derived);
   17983        46870 :                 to_examin.add (c->ts.u.derived);
   17984              :               }
   17985              :           }
   17986       252795 :         else if (c->ts.type == BT_CLASS)
   17987              :           {
   17988         9642 :             if (!c->attr.class_ok)
   17989            7 :               continue;
   17990         9635 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   17991              :               {
   17992         1024 :                 derived->attr.recursive = 1;
   17993         1024 :                 return;
   17994              :               }
   17995         8611 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   17996              :               {
   17997         4846 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   17998         4846 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   17999              :               }
   18000              :           }
   18001              :     }
   18002       138465 : }
   18003              : 
   18004              : /* Resolve the components of a derived type. This does not have to wait until
   18005              :    resolution stage, but can be done as soon as the dt declaration has been
   18006              :    parsed.  */
   18007              : 
   18008              : static bool
   18009       170610 : resolve_fl_derived0 (gfc_symbol *sym)
   18010              : {
   18011       170610 :   gfc_symbol* super_type;
   18012       170610 :   gfc_component *c;
   18013       170610 :   gfc_formal_arglist *f;
   18014       170610 :   bool success;
   18015              : 
   18016       170610 :   if (sym->attr.unlimited_polymorphic)
   18017              :     return true;
   18018              : 
   18019       170610 :   super_type = gfc_get_derived_super_type (sym);
   18020              : 
   18021              :   /* F2008, C432.  */
   18022       170610 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   18023              :     {
   18024            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   18025              :                  "parent type %qs shall also have one", sym->name,
   18026              :                  &sym->declared_at, super_type->name);
   18027            2 :       return false;
   18028              :     }
   18029              : 
   18030              :   /* Ensure the extended type gets resolved before we do.  */
   18031        17656 :   if (super_type && !resolve_fl_derived0 (super_type))
   18032              :     return false;
   18033              : 
   18034              :   /* An ABSTRACT type must be extensible.  */
   18035       170602 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   18036              :     {
   18037            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   18038              :                  sym->name, &sym->declared_at);
   18039            2 :       return false;
   18040              :     }
   18041              : 
   18042              :   /* Resolving components below, may create vtabs for which the cyclic type
   18043              :      information needs to be present.  */
   18044       170600 :   if (!sym->attr.vtype)
   18045       138465 :     resolve_cyclic_derived_type (sym);
   18046              : 
   18047       170600 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   18048              :                            : sym->components;
   18049              : 
   18050              :   success = true;
   18051       579045 :   for ( ; c != NULL; c = c->next)
   18052       408445 :     if (!resolve_component (c, sym))
   18053           96 :       success = false;
   18054              : 
   18055       170600 :   if (!success)
   18056              :     return false;
   18057              : 
   18058              :   /* Now add the caf token field, where needed.  */
   18059       170514 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   18060          994 :       && !sym->attr.vtype)
   18061              :     {
   18062         2238 :       for (c = sym->components; c; c = c->next)
   18063         1441 :         if (!c->attr.dimension && !c->attr.codimension
   18064          795 :             && (c->attr.allocatable || c->attr.pointer))
   18065              :           {
   18066          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   18067          146 :             gfc_component *token;
   18068          146 :             sprintf (name, "_caf_%s", c->name);
   18069          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   18070          146 :             if (token == NULL)
   18071              :               {
   18072           82 :                 if (!gfc_add_component (sym, name, &token))
   18073            0 :                   return false;
   18074           82 :                 token->ts.type = BT_VOID;
   18075           82 :                 token->ts.kind = gfc_default_integer_kind;
   18076           82 :                 token->attr.access = ACCESS_PRIVATE;
   18077           82 :                 token->attr.artificial = 1;
   18078           82 :                 token->attr.caf_token = 1;
   18079              :               }
   18080          146 :             c->caf_token = token;
   18081              :           }
   18082              :     }
   18083              : 
   18084       170514 :   check_defined_assignments (sym);
   18085              : 
   18086       170514 :   if (!sym->attr.defined_assign_comp && super_type)
   18087        16649 :     sym->attr.defined_assign_comp
   18088        16649 :                         = super_type->attr.defined_assign_comp;
   18089              : 
   18090              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   18091              :      all DEFERRED bindings are overridden.  */
   18092        17649 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   18093         1403 :       && !sym->attr.is_class
   18094         3153 :       && !ensure_not_abstract (sym, super_type))
   18095              :     return false;
   18096              : 
   18097              :   /* Check that there is a component for every PDT parameter.  */
   18098       170508 :   if (sym->attr.pdt_template)
   18099              :     {
   18100         2502 :       for (f = sym->formal; f; f = f->next)
   18101              :         {
   18102         1444 :           if (!f->sym)
   18103            1 :             continue;
   18104         1443 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   18105         1443 :           if (c == NULL)
   18106              :             {
   18107            9 :               gfc_error ("Parameterized type %qs does not have a component "
   18108              :                          "corresponding to parameter %qs at %L", sym->name,
   18109            9 :                          f->sym->name, &sym->declared_at);
   18110            9 :               break;
   18111              :             }
   18112              :         }
   18113              :     }
   18114              : 
   18115              :   /* Add derived type to the derived type list.  */
   18116       170508 :   add_dt_to_dt_list (sym);
   18117              : 
   18118       170508 :   return true;
   18119              : }
   18120              : 
   18121              : /* The following procedure does the full resolution of a derived type,
   18122              :    including resolution of all type-bound procedures (if present). In contrast
   18123              :    to 'resolve_fl_derived0' this can only be done after the module has been
   18124              :    parsed completely.  */
   18125              : 
   18126              : static bool
   18127        88700 : resolve_fl_derived (gfc_symbol *sym)
   18128              : {
   18129        88700 :   gfc_symbol *gen_dt = NULL;
   18130              : 
   18131        88700 :   if (sym->attr.unlimited_polymorphic)
   18132              :     return true;
   18133              : 
   18134        88700 :   if (!sym->attr.is_class)
   18135        75814 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   18136        57105 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   18137         2285 :       && (!gen_dt->generic->sym->attr.use_assoc
   18138         2142 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   18139        88876 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   18140              :                           "%qs at %L being the same name as derived "
   18141              :                           "type at %L", sym->name,
   18142              :                           gen_dt->generic->sym == sym
   18143           11 :                           ? gen_dt->generic->next->sym->name
   18144              :                           : gen_dt->generic->sym->name,
   18145              :                           gen_dt->generic->sym == sym
   18146           11 :                           ? &gen_dt->generic->next->sym->declared_at
   18147              :                           : &gen_dt->generic->sym->declared_at,
   18148              :                           &sym->declared_at))
   18149              :     return false;
   18150              : 
   18151        88696 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   18152              :     {
   18153           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   18154              :                   sym->name, &sym->declared_at);
   18155           13 :       return false;
   18156              :     }
   18157              : 
   18158              :   /* Resolve the finalizer procedures.  */
   18159        88683 :   if (!gfc_resolve_finalizers (sym, NULL))
   18160              :     return false;
   18161              : 
   18162        88680 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   18163              :     {
   18164              :       /* Fix up incomplete CLASS symbols.  */
   18165        12886 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   18166        12886 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18167              : 
   18168        12886 :       if (data->ts.u.derived->attr.pdt_template)
   18169              :         {
   18170            0 :           match m;
   18171            0 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   18172              :                                     &data->param_list);
   18173            0 :           if (m != MATCH_YES
   18174            0 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   18175              :             {
   18176            0 :               gfc_error ("Failed to build PDT class component at %L",
   18177              :                          &sym->declared_at);
   18178            0 :               return false;
   18179              :             }
   18180            0 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   18181            0 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18182              :         }
   18183              : 
   18184              :       /* Nothing more to do for unlimited polymorphic entities.  */
   18185        12886 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   18186              :         {
   18187         2058 :           add_dt_to_dt_list (sym);
   18188         2058 :           return true;
   18189              :         }
   18190        10828 :       else if (vptr->ts.u.derived == NULL)
   18191              :         {
   18192         6396 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   18193         6396 :           gcc_assert (vtab);
   18194         6396 :           vptr->ts.u.derived = vtab->ts.u.derived;
   18195         6396 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   18196              :             return false;
   18197              :         }
   18198              :     }
   18199              : 
   18200        86622 :   if (!resolve_fl_derived0 (sym))
   18201              :     return false;
   18202              : 
   18203              :   /* Resolve the type-bound procedures.  */
   18204        86538 :   if (!resolve_typebound_procedures (sym))
   18205              :     return false;
   18206              : 
   18207              :   /* Generate module vtables subject to their accessibility and their not
   18208              :      being vtables or pdt templates. If this is not done class declarations
   18209              :      in external procedures wind up with their own version and so SELECT TYPE
   18210              :      fails because the vptrs do not have the same address.  */
   18211        86497 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   18212        86436 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   18213        64463 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   18214        22127 :       && sym->attr.access != ACCESS_PRIVATE
   18215        22094 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   18216              :     {
   18217        19826 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   18218        19826 :       gfc_set_sym_referenced (vtab);
   18219              :     }
   18220              : 
   18221              :   return true;
   18222              : }
   18223              : 
   18224              : 
   18225              : static bool
   18226          863 : resolve_fl_namelist (gfc_symbol *sym)
   18227              : {
   18228          863 :   gfc_namelist *nl;
   18229          863 :   gfc_symbol *nlsym;
   18230              : 
   18231         3040 :   for (nl = sym->namelist; nl; nl = nl->next)
   18232              :     {
   18233              :       /* Check again, the check in match only works if NAMELIST comes
   18234              :          after the decl.  */
   18235         2182 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   18236              :         {
   18237            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   18238              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   18239            1 :           return false;
   18240              :         }
   18241              : 
   18242          678 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   18243         2189 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18244              :                               "with assumed shape in namelist %qs at %L",
   18245              :                               nl->sym->name, sym->name, &sym->declared_at))
   18246              :         return false;
   18247              : 
   18248         2180 :       if (is_non_constant_shape_array (nl->sym)
   18249         2230 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18250              :                               "with nonconstant shape in namelist %qs at %L",
   18251           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   18252              :         return false;
   18253              : 
   18254         2179 :       if (nl->sym->ts.type == BT_CHARACTER
   18255          593 :           && (nl->sym->ts.u.cl->length == NULL
   18256          554 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   18257         2261 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   18258              :                               "nonconstant character length in "
   18259           82 :                               "namelist %qs at %L", nl->sym->name,
   18260              :                               sym->name, &sym->declared_at))
   18261              :         return false;
   18262              : 
   18263              :     }
   18264              : 
   18265              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   18266          858 :   if (gfc_check_symbol_access (sym))
   18267              :     {
   18268         3021 :       for (nl = sym->namelist; nl; nl = nl->next)
   18269              :         {
   18270         2176 :           if (!nl->sym->attr.use_assoc
   18271         4056 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   18272         4182 :               && !gfc_check_symbol_access (nl->sym))
   18273              :             {
   18274            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   18275              :                          "cannot be member of PUBLIC namelist %qs at %L",
   18276            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   18277            2 :               return false;
   18278              :             }
   18279              : 
   18280         2174 :           if (nl->sym->ts.type == BT_DERIVED
   18281          472 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   18282          470 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   18283              :            {
   18284            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   18285              :                                   "namelist %qs at %L with ALLOCATABLE "
   18286              :                                   "or POINTER components", nl->sym->name,
   18287              :                                   sym->name, &sym->declared_at))
   18288              :                return false;
   18289              :              return true;
   18290              :            }
   18291              : 
   18292              :           /* Types with private components that came here by USE-association.  */
   18293         2169 :           if (nl->sym->ts.type == BT_DERIVED
   18294         2169 :               && derived_inaccessible (nl->sym->ts.u.derived))
   18295              :             {
   18296            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   18297              :                          "components and cannot be member of namelist %qs at %L",
   18298              :                          nl->sym->name, sym->name, &sym->declared_at);
   18299            6 :               return false;
   18300              :             }
   18301              : 
   18302              :           /* Types with private components that are defined in the same module.  */
   18303         2163 :           if (nl->sym->ts.type == BT_DERIVED
   18304          922 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18305         2447 :               && nl->sym->ts.u.derived->attr.private_comp)
   18306              :             {
   18307            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18308              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18309              :                          nl->sym->name, sym->name, &sym->declared_at);
   18310            0 :               return false;
   18311              :             }
   18312              :         }
   18313              :     }
   18314              : 
   18315              : 
   18316              :   /* 14.1.2 A module or internal procedure represent local entities
   18317              :      of the same type as a namelist member and so are not allowed.  */
   18318         3005 :   for (nl = sym->namelist; nl; nl = nl->next)
   18319              :     {
   18320         2163 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18321         1598 :         continue;
   18322              : 
   18323          565 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18324            7 :         if ((nl->sym == sym->ns->proc_name)
   18325            1 :                ||
   18326            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18327            6 :           continue;
   18328              : 
   18329          559 :       nlsym = NULL;
   18330          559 :       if (nl->sym->name)
   18331          559 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18332          559 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18333              :         {
   18334            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18335              :                      "attribute in %qs at %L", nlsym->name,
   18336              :                      &sym->declared_at);
   18337            3 :           return false;
   18338              :         }
   18339              :     }
   18340              : 
   18341              :   return true;
   18342              : }
   18343              : 
   18344              : 
   18345              : static bool
   18346       408469 : resolve_fl_parameter (gfc_symbol *sym)
   18347              : {
   18348              :   /* A parameter array's shape needs to be constant.  */
   18349       408469 :   if (sym->as != NULL
   18350       408469 :       && (sym->as->type == AS_DEFERRED
   18351         6266 :           || is_non_constant_shape_array (sym)))
   18352              :     {
   18353           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18354              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18355           17 :       return false;
   18356              :     }
   18357              : 
   18358              :   /* Constraints on deferred type parameter.  */
   18359       408452 :   if (!deferred_requirements (sym))
   18360              :     return false;
   18361              : 
   18362              :   /* Make sure a parameter that has been implicitly typed still
   18363              :      matches the implicit type, since PARAMETER statements can precede
   18364              :      IMPLICIT statements.  */
   18365       408451 :   if (sym->attr.implicit_type
   18366       409164 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18367          713 :                                                              sym->ns)))
   18368              :     {
   18369            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18370              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18371            0 :       return false;
   18372              :     }
   18373              : 
   18374              :   /* Make sure the types of derived parameters are consistent.  This
   18375              :      type checking is deferred until resolution because the type may
   18376              :      refer to a derived type from the host.  */
   18377       408451 :   if (sym->ts.type == BT_DERIVED
   18378       408451 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18379              :     {
   18380            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18381            0 :                  &sym->value->where);
   18382            0 :       return false;
   18383              :     }
   18384              : 
   18385              :   /* F03:C509,C514.  */
   18386       408451 :   if (sym->ts.type == BT_CLASS)
   18387              :     {
   18388            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18389              :                  sym->name, &sym->declared_at);
   18390            0 :       return false;
   18391              :     }
   18392              : 
   18393              :   /* Some programmers can have a typo when using an implied-do loop to
   18394              :      initialize an array constant.  For example,
   18395              :        INTEGER I,J
   18396              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18397              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18398              :      This check catches the typo.  */
   18399       408451 :   if (sym->attr.dimension
   18400         6259 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18401       414704 :       && !gfc_is_constant_expr (sym->value))
   18402              :     {
   18403              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18404              :          the array constructor of a parameter.  This seems inconsistent with
   18405              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18406           20 :       if (sym->value->ts.type == BT_DERIVED
   18407           18 :           && sym->value->ts.u.derived
   18408           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18409              :         return true;
   18410            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18411            2 :       return false;
   18412              :     }
   18413              : 
   18414              :   return true;
   18415              : }
   18416              : 
   18417              : 
   18418              : /* Called by resolve_symbol to check PDTs.  */
   18419              : 
   18420              : static void
   18421         1462 : resolve_pdt (gfc_symbol* sym)
   18422              : {
   18423         1462 :   gfc_symbol *derived = NULL;
   18424         1462 :   gfc_actual_arglist *param;
   18425         1462 :   gfc_component *c;
   18426         1462 :   bool const_len_exprs = true;
   18427         1462 :   bool assumed_len_exprs = false;
   18428         1462 :   symbol_attribute *attr;
   18429              : 
   18430         1462 :   if (sym->ts.type == BT_DERIVED)
   18431              :     {
   18432         1223 :       derived = sym->ts.u.derived;
   18433         1223 :       attr = &(sym->attr);
   18434              :     }
   18435          239 :   else if (sym->ts.type == BT_CLASS)
   18436              :     {
   18437          239 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18438          239 :       attr = &(CLASS_DATA (sym)->attr);
   18439              :     }
   18440              :   else
   18441            0 :     gcc_unreachable ();
   18442              : 
   18443         1462 :   gcc_assert (derived->attr.pdt_type);
   18444              : 
   18445         3447 :   for (param = sym->param_list; param; param = param->next)
   18446              :     {
   18447         1985 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18448         1985 :       gcc_assert (c);
   18449         1985 :       if (c->attr.pdt_kind)
   18450         1042 :         continue;
   18451              : 
   18452          662 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18453         1039 :           && c->attr.pdt_len)
   18454              :         const_len_exprs = false;
   18455          847 :       else if (param->spec_type == SPEC_ASSUMED)
   18456          303 :         assumed_len_exprs = true;
   18457              : 
   18458          943 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18459           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18460           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18461            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18462              :                    "parameter %qs and requires either the POINTER "
   18463              :                    "or ALLOCATABLE attribute",
   18464              :                    sym->name, &sym->declared_at,
   18465              :                    param->name);
   18466              : 
   18467              :     }
   18468              : 
   18469         1462 :   if (!const_len_exprs
   18470           96 :       && (sym->ns->proc_name->attr.is_main_program
   18471           95 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18472           94 :           || sym->attr.save != SAVE_NONE))
   18473            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18474              :                "SAVE attribute or be a variable declared in the "
   18475              :                "main program, a module or a submodule(F08/C513)",
   18476              :                sym->name, &sym->declared_at);
   18477              : 
   18478         1462 :   if (assumed_len_exprs && !(sym->attr.dummy
   18479            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18480            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18481              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18482              :                sym->name, &sym->declared_at);
   18483         1462 : }
   18484              : 
   18485              : 
   18486              : /* Resolve the symbol's array spec.  */
   18487              : 
   18488              : static bool
   18489      1752723 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18490              : {
   18491      1752723 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18492      1752723 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18493              : 
   18494      1752723 :   bool saved_specification_expr = specification_expr;
   18495      1752723 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18496      1752723 :   specification_expr = true;
   18497      1752723 :   specification_expr_symbol = sym;
   18498              : 
   18499      1752723 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18500              : 
   18501      1752723 :   specification_expr = saved_specification_expr;
   18502      1752723 :   specification_expr_symbol = saved_specification_expr_symbol;
   18503      1752723 :   gfc_current_ns = orig_current_ns;
   18504              : 
   18505      1752723 :   return result;
   18506              : }
   18507              : 
   18508              : 
   18509              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18510              :    assume that an otherwise unknown symbol is a variable.  This sort
   18511              :    of thing commonly happens for symbols in module.  */
   18512              : 
   18513              : static void
   18514      1910887 : resolve_symbol (gfc_symbol *sym)
   18515              : {
   18516      1910887 :   int check_constant, mp_flag;
   18517      1910887 :   gfc_symtree *symtree;
   18518      1910887 :   gfc_symtree *this_symtree;
   18519      1910887 :   gfc_namespace *ns;
   18520      1910887 :   gfc_component *c;
   18521      1910887 :   symbol_attribute class_attr;
   18522      1910887 :   gfc_array_spec *as;
   18523      1910887 :   bool declared_has_coarray_comp = false;
   18524              : 
   18525      1910887 :   if (sym->resolve_symbol_called >= 1)
   18526       189553 :     return;
   18527      1822402 :   sym->resolve_symbol_called = 1;
   18528              : 
   18529              :   /* No symbol will ever have union type; only components can be unions.
   18530              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18531              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18532      1822402 :   gcc_assert (sym->ts.type != BT_UNION);
   18533              : 
   18534              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18535              :      yet unsupported for -fcoarray=lib.  */
   18536      1822402 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18537          112 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18538          112 :       && CLASS_DATA (sym)->attr.codimension
   18539           94 :       && CLASS_DATA (sym)->ts.u.derived
   18540           93 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18541           90 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18542              :     {
   18543            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18544              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18545            6 :       return;
   18546              :     }
   18547              : 
   18548      1822396 :   if (sym->attr.artificial)
   18549              :     return;
   18550              : 
   18551      1724065 :   if (sym->attr.unlimited_polymorphic)
   18552              :     return;
   18553              : 
   18554      1722577 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18555              :     {
   18556            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18557              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18558            4 :       return;
   18559              :     }
   18560              : 
   18561      1722573 :   if (sym->attr.flavor == FL_UNKNOWN
   18562      1701227 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18563       454207 :           && !sym->attr.generic && !sym->attr.external
   18564       181646 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18565        81924 :           && sym->ts.type == BT_UNKNOWN))
   18566              :     {
   18567              :       /* A symbol in a common block might not have been resolved yet properly.
   18568              :          Do not try to find an interface with the same name.  */
   18569        94734 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18570        21342 :           && !sym->attr.generic && !sym->attr.external
   18571        21291 :           && sym->attr.in_common)
   18572         2594 :         goto skip_interfaces;
   18573              : 
   18574              :     /* If we find that a flavorless symbol is an interface in one of the
   18575              :        parent namespaces, find its symtree in this namespace, free the
   18576              :        symbol and set the symtree to point to the interface symbol.  */
   18577       131712 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18578              :         {
   18579        40282 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   18580        40282 :           if (symtree && (symtree->n.sym->generic ||
   18581          767 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   18582          677 :                            && sym->ns->construct_entities)))
   18583              :             {
   18584          718 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   18585              :                                                sym->name);
   18586          718 :               if (this_symtree->n.sym == sym)
   18587              :                 {
   18588          710 :                   symtree->n.sym->refs++;
   18589          710 :                   gfc_release_symbol (sym);
   18590          710 :                   this_symtree->n.sym = symtree->n.sym;
   18591          710 :                   return;
   18592              :                 }
   18593              :             }
   18594              :         }
   18595              : 
   18596        91430 : skip_interfaces:
   18597              :       /* Otherwise give it a flavor according to such attributes as
   18598              :          it has.  */
   18599        94024 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   18600        21161 :           && sym->attr.intrinsic == 0)
   18601        21157 :         sym->attr.flavor = FL_VARIABLE;
   18602        72867 :       else if (sym->attr.flavor == FL_UNKNOWN)
   18603              :         {
   18604           55 :           sym->attr.flavor = FL_PROCEDURE;
   18605           55 :           if (sym->attr.dimension)
   18606            0 :             sym->attr.function = 1;
   18607              :         }
   18608              :     }
   18609              : 
   18610      1721863 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   18611         2384 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   18612              : 
   18613         1517 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   18614      1723380 :       && !resolve_procedure_interface (sym))
   18615              :     return;
   18616              : 
   18617      1721852 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   18618          130 :       && (sym->attr.procedure || sym->attr.external))
   18619              :     {
   18620            0 :       if (sym->attr.external)
   18621            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   18622              :                    "at %L", &sym->declared_at);
   18623              :       else
   18624            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   18625              :                    "at %L", &sym->declared_at);
   18626              : 
   18627            0 :       return;
   18628              :     }
   18629              : 
   18630              :   /* Ensure that variables of derived or class type having a finalizer are
   18631              :      marked used even when the variable is not used anything else in the scope.
   18632              :      This fixes PR118730.  */
   18633       663996 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   18634       456716 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   18635      1771175 :       && gfc_may_be_finalized (sym->ts))
   18636         8721 :     gfc_set_sym_referenced (sym);
   18637              : 
   18638      1721852 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   18639              :     return;
   18640              : 
   18641      1721076 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   18642      1721839 :            && !resolve_fl_struct (sym))
   18643              :     return;
   18644              : 
   18645              :   /* Symbols that are module procedures with results (functions) have
   18646              :      the types and array specification copied for type checking in
   18647              :      procedures that call them, as well as for saving to a module
   18648              :      file.  These symbols can't stand the scrutiny that their results
   18649              :      can.  */
   18650      1721707 :   mp_flag = (sym->result != NULL && sym->result != sym);
   18651              : 
   18652              :   /* Make sure that the intrinsic is consistent with its internal
   18653              :      representation. This needs to be done before assigning a default
   18654              :      type to avoid spurious warnings.  */
   18655      1686519 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   18656      1758711 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   18657              :     return;
   18658              : 
   18659              :   /* Resolve associate names.  */
   18660      1721671 :   if (sym->assoc)
   18661         6919 :     resolve_assoc_var (sym, true);
   18662              : 
   18663              :   /* Assign default type to symbols that need one and don't have one.  */
   18664      1721671 :   if (sym->ts.type == BT_UNKNOWN)
   18665              :     {
   18666       410445 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   18667              :         {
   18668        11788 :           gfc_set_default_type (sym, 1, NULL);
   18669              :         }
   18670              : 
   18671       266011 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   18672        61914 :           && !sym->attr.function && !sym->attr.subroutine
   18673       412118 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   18674          622 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   18675              : 
   18676       410445 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18677              :         {
   18678              :           /* The specific case of an external procedure should emit an error
   18679              :              in the case that there is no implicit type.  */
   18680       104014 :           if (!mp_flag)
   18681              :             {
   18682        97896 :               if (!sym->attr.mixed_entry_master)
   18683        97788 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   18684              :             }
   18685              :           else
   18686              :             {
   18687              :               /* Result may be in another namespace.  */
   18688         6118 :               resolve_symbol (sym->result);
   18689              : 
   18690         6118 :               if (!sym->result->attr.proc_pointer)
   18691              :                 {
   18692         5939 :                   sym->ts = sym->result->ts;
   18693         5939 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   18694         5939 :                   sym->attr.dimension = sym->result->attr.dimension;
   18695         5939 :                   sym->attr.codimension = sym->result->attr.codimension;
   18696         5939 :                   sym->attr.pointer = sym->result->attr.pointer;
   18697         5939 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   18698         5939 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   18699              :                 }
   18700              :             }
   18701              :         }
   18702              :     }
   18703      1311226 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18704        31386 :     resolve_symbol_array_spec (sym->result, false);
   18705              : 
   18706              :   /* For a CLASS-valued function with a result variable, affirm that it has
   18707              :      been resolved also when looking at the symbol 'sym'.  */
   18708       441831 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   18709          720 :     sym->attr.class_ok = sym->result->attr.class_ok;
   18710              : 
   18711      1721671 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   18712        19690 :       && CLASS_DATA (sym))
   18713              :     {
   18714        19689 :       as = CLASS_DATA (sym)->as;
   18715        19689 :       class_attr = CLASS_DATA (sym)->attr;
   18716        19689 :       class_attr.pointer = class_attr.class_pointer;
   18717        19689 :       declared_has_coarray_comp = CLASS_DATA (sym)->ts.u.derived
   18718        19689 :                                   && CLASS_DATA (sym)->ts.u.derived->attr.coarray_comp;
   18719              :     }
   18720              :   else
   18721              :     {
   18722      1701982 :       class_attr = sym->attr;
   18723      1701982 :       as = sym->as;
   18724              :     }
   18725              : 
   18726              :   /* F2008, C530.  */
   18727      1721671 :   if (sym->attr.contiguous
   18728         7748 :       && !sym->attr.associate_var
   18729         7747 :       && (!class_attr.dimension
   18730         7744 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   18731          140 :               && !class_attr.pointer)))
   18732              :     {
   18733            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   18734              :                  "array pointer or an assumed-shape or assumed-rank array",
   18735              :                  sym->name, &sym->declared_at);
   18736            7 :       return;
   18737              :     }
   18738              : 
   18739              :   /* Assumed size arrays and assumed shape arrays must be dummy
   18740              :      arguments.  Array-spec's of implied-shape should have been resolved to
   18741              :      AS_EXPLICIT already.  */
   18742              : 
   18743      1714060 :   if (as)
   18744              :     {
   18745              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   18746              :          specification expression.  */
   18747       147804 :       if (as->type == AS_IMPLIED_SHAPE)
   18748              :         {
   18749              :           int i;
   18750            1 :           for (i=0; i<as->rank; i++)
   18751              :             {
   18752            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   18753              :                 {
   18754            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   18755              :                              &as->lower[i]->where);
   18756            1 :                   return;
   18757              :                 }
   18758              :             }
   18759            0 :           gcc_unreachable();
   18760              :         }
   18761              : 
   18762       147803 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   18763       114966 :            || as->type == AS_ASSUMED_SHAPE)
   18764        44689 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   18765            8 :           && !sym->attr.associate_var)
   18766              :         {
   18767            7 :           if (as->type == AS_ASSUMED_SIZE)
   18768            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   18769              :                        &sym->declared_at);
   18770              :           else
   18771            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   18772              :                        &sym->declared_at);
   18773            7 :           return;
   18774              :         }
   18775              :       /* TS 29113, C535a.  */
   18776       147796 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   18777           60 :           && !sym->attr.select_type_temporary
   18778           60 :           && !(cs_base && cs_base->current
   18779           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   18780            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   18781            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   18782              :         {
   18783           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   18784              :                      &sym->declared_at);
   18785           18 :           return;
   18786              :         }
   18787       147778 :       if (as->type == AS_ASSUMED_RANK
   18788        26305 :           && (sym->attr.codimension || sym->attr.value))
   18789              :         {
   18790            2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   18791              :                      "CODIMENSION attribute", &sym->declared_at);
   18792            2 :           return;
   18793              :         }
   18794              :     }
   18795              : 
   18796              :   /* Make sure symbols with known intent or optional are really dummy
   18797              :      variable.  Because of ENTRY statement, this has to be deferred
   18798              :      until resolution time.  */
   18799              : 
   18800      1721636 :   if (!sym->attr.dummy
   18801      1241010 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   18802              :     {
   18803            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   18804            2 :       return;
   18805              :     }
   18806              : 
   18807      1721634 :   if (sym->attr.value && !sym->attr.dummy)
   18808              :     {
   18809            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   18810              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   18811            2 :       return;
   18812              :     }
   18813              : 
   18814      1721632 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   18815              :     {
   18816          616 :       gfc_charlen *cl = sym->ts.u.cl;
   18817          616 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   18818              :         {
   18819            2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   18820              :                      "attribute must have constant length",
   18821              :                      sym->name, &sym->declared_at);
   18822            2 :           return;
   18823              :         }
   18824              : 
   18825          614 :       if (sym->ts.is_c_interop
   18826          381 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   18827              :         {
   18828            1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   18829              :                      "with VALUE attribute must have length one",
   18830              :                      sym->name, &sym->declared_at);
   18831            1 :           return;
   18832              :         }
   18833              :     }
   18834              : 
   18835      1721629 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18836       122776 :       && sym->ts.u.derived->attr.generic)
   18837              :     {
   18838           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   18839           20 :       if (!sym->ts.u.derived)
   18840              :         {
   18841            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   18842              :                      "which has not been defined", sym->name,
   18843              :                      &sym->declared_at, sym->ts.u.derived->name);
   18844            0 :           sym->ts.type = BT_UNKNOWN;
   18845            0 :           return;
   18846              :         }
   18847              :     }
   18848              : 
   18849              :     /* Use the same constraints as TYPE(*), except for the type check
   18850              :        and that only scalars and assumed-size arrays are permitted.  */
   18851      1721629 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   18852              :       {
   18853        12960 :         if (!sym->attr.dummy)
   18854              :           {
   18855            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18856              :                        "a dummy argument", sym->name, &sym->declared_at);
   18857            1 :             return;
   18858              :           }
   18859              : 
   18860        12959 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   18861            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   18862            0 :             && sym->ts.type != BT_COMPLEX)
   18863              :           {
   18864            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18865              :                        "of type TYPE(*) or of an numeric intrinsic type",
   18866              :                        sym->name, &sym->declared_at);
   18867            0 :             return;
   18868              :           }
   18869              : 
   18870        12959 :       if (sym->attr.allocatable || sym->attr.codimension
   18871        12957 :           || sym->attr.pointer || sym->attr.value)
   18872              :         {
   18873            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18874              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   18875              :                      "attribute", sym->name, &sym->declared_at);
   18876            4 :           return;
   18877              :         }
   18878              : 
   18879        12955 :       if (sym->attr.intent == INTENT_OUT)
   18880              :         {
   18881            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18882              :                      "have the INTENT(OUT) attribute",
   18883              :                      sym->name, &sym->declared_at);
   18884            0 :           return;
   18885              :         }
   18886        12955 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   18887              :         {
   18888            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   18889              :                      "either be a scalar or an assumed-size array",
   18890              :                      sym->name, &sym->declared_at);
   18891            1 :           return;
   18892              :         }
   18893              : 
   18894              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   18895              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   18896              :          packing.  */
   18897        12954 :       sym->ts.type = BT_ASSUMED;
   18898        12954 :       sym->as = gfc_get_array_spec ();
   18899        12954 :       sym->as->type = AS_ASSUMED_SIZE;
   18900        12954 :       sym->as->rank = 1;
   18901        12954 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   18902              :     }
   18903      1708669 :   else if (sym->ts.type == BT_ASSUMED)
   18904              :     {
   18905              :       /* TS 29113, C407a.  */
   18906        11000 :       if (!sym->attr.dummy)
   18907              :         {
   18908            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   18909              :                      "for dummy variables", sym->name, &sym->declared_at);
   18910            7 :           return;
   18911              :         }
   18912        10993 :       if (sym->attr.allocatable || sym->attr.codimension
   18913        10989 :           || sym->attr.pointer || sym->attr.value)
   18914              :         {
   18915            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18916              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   18917              :                      sym->name, &sym->declared_at);
   18918            8 :           return;
   18919              :         }
   18920        10985 :       if (sym->attr.intent == INTENT_OUT)
   18921              :         {
   18922            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18923              :                      "INTENT(OUT) attribute",
   18924              :                      sym->name, &sym->declared_at);
   18925            2 :           return;
   18926              :         }
   18927        10983 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   18928              :         {
   18929            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   18930              :                      "explicit-shape array", sym->name, &sym->declared_at);
   18931            3 :           return;
   18932              :         }
   18933              :     }
   18934              : 
   18935              :   /* If the symbol is marked as bind(c), that it is declared at module level
   18936              :      scope and verify its type and kind.  Do not do the latter for symbols
   18937              :      that are implicitly typed because that is handled in
   18938              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   18939              :      separately.  Also, anything that is use associated is not handled here
   18940              :      but instead is handled in the module it is declared in.  Finally, derived
   18941              :      type definitions are allowed to be BIND(C) since that only implies that
   18942              :      they're interoperable, and they are checked fully for interoperability
   18943              :      when a variable is declared of that type.  */
   18944      1721603 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   18945         7808 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   18946          567 :       && sym->attr.flavor != FL_DERIVED)
   18947              :     {
   18948          167 :       bool t = true;
   18949              : 
   18950              :       /* First, make sure the variable is declared at the
   18951              :          module-level scope (J3/04-007, Section 15.3).  */
   18952          167 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   18953            7 :           && !sym->attr.in_common)
   18954              :         {
   18955            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   18956              :                      "is neither a COMMON block nor declared at the "
   18957              :                      "module level scope", sym->name, &(sym->declared_at));
   18958            6 :           t = false;
   18959              :         }
   18960          161 :       else if (sym->ts.type == BT_CHARACTER
   18961          161 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   18962            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   18963            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   18964              :         {
   18965            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   18966            1 :                      sym->name, &sym->declared_at);
   18967            1 :           t = false;
   18968              :         }
   18969          160 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   18970              :         {
   18971            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   18972              :         }
   18973          159 :       else if (sym->attr.implicit_type == 0)
   18974              :         {
   18975              :           /* If type() declaration, we need to verify that the components
   18976              :              of the given type are all C interoperable, etc.  */
   18977          157 :           if (sym->ts.type == BT_DERIVED &&
   18978           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   18979              :             {
   18980              :               /* Make sure the user marked the derived type as BIND(C).  If
   18981              :                  not, call the verify routine.  This could print an error
   18982              :                  for the derived type more than once if multiple variables
   18983              :                  of that type are declared.  */
   18984           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   18985            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   18986          157 :               t = false;
   18987              :             }
   18988              : 
   18989              :           /* Verify the variable itself as C interoperable if it
   18990              :              is BIND(C).  It is not possible for this to succeed if
   18991              :              the verify_bind_c_derived_type failed, so don't have to handle
   18992              :              any error returned by verify_bind_c_derived_type.  */
   18993          157 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   18994          157 :                                  sym->common_block);
   18995              :         }
   18996              : 
   18997          165 :       if (!t)
   18998              :         {
   18999              :           /* clear the is_bind_c flag to prevent reporting errors more than
   19000              :              once if something failed.  */
   19001           10 :           sym->attr.is_bind_c = 0;
   19002           10 :           return;
   19003              :         }
   19004              :     }
   19005              : 
   19006              :   /* If a derived type symbol has reached this point, without its
   19007              :      type being declared, we have an error.  Notice that most
   19008              :      conditions that produce undefined derived types have already
   19009              :      been dealt with.  However, the likes of:
   19010              :      implicit type(t) (t) ..... call foo (t) will get us here if
   19011              :      the type is not declared in the scope of the implicit
   19012              :      statement. Change the type to BT_UNKNOWN, both because it is so
   19013              :      and to prevent an ICE.  */
   19014      1721593 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   19015       122774 :       && sym->ts.u.derived->components == NULL
   19016         1145 :       && !sym->ts.u.derived->attr.zero_comp)
   19017              :     {
   19018            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   19019              :                  "which has not been defined", sym->name,
   19020              :                   &sym->declared_at, sym->ts.u.derived->name);
   19021            3 :       sym->ts.type = BT_UNKNOWN;
   19022            3 :       return;
   19023              :     }
   19024              : 
   19025              :   /* Make sure that the derived type has been resolved and that the
   19026              :      derived type is visible in the symbol's namespace, if it is a
   19027              :      module function and is not PRIVATE.  */
   19028      1721590 :   if (sym->ts.type == BT_DERIVED
   19029       129913 :         && sym->ts.u.derived->attr.use_assoc
   19030       112189 :         && sym->ns->proc_name
   19031       112181 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   19032      1727539 :         && !resolve_fl_derived (sym->ts.u.derived))
   19033              :     return;
   19034              : 
   19035              :   /* Unless the derived-type declaration is use associated, Fortran 95
   19036              :      does not allow public entries of private derived types.
   19037              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   19038              :      161 in 95-006r3.  */
   19039      1721590 :   if (sym->ts.type == BT_DERIVED
   19040       129913 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   19041         8093 :       && !sym->ts.u.derived->attr.use_assoc
   19042         2144 :       && gfc_check_symbol_access (sym)
   19043         1931 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   19044      1721604 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   19045              :                           "derived type %qs",
   19046           14 :                           (sym->attr.flavor == FL_PARAMETER)
   19047              :                           ? "parameter" : "variable",
   19048              :                           sym->name, &sym->declared_at,
   19049           14 :                           sym->ts.u.derived->name))
   19050              :     return;
   19051              : 
   19052              :   /* F2008, C1302.  */
   19053      1721583 :   if (sym->ts.type == BT_DERIVED
   19054       129906 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19055          154 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   19056       129875 :           || sym->ts.u.derived->attr.lock_comp)
   19057           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19058              :     {
   19059            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   19060              :                  "type LOCK_TYPE must be a coarray", sym->name,
   19061              :                  &sym->declared_at);
   19062            4 :       return;
   19063              :     }
   19064              : 
   19065              :   /* TS18508, C702/C703.  */
   19066      1721579 :   if (sym->ts.type == BT_DERIVED
   19067       129902 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19068          153 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   19069       129885 :           || sym->ts.u.derived->attr.event_comp)
   19070           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19071              :     {
   19072            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   19073              :                  "type EVENT_TYPE must be a coarray", sym->name,
   19074              :                  &sym->declared_at);
   19075            1 :       return;
   19076              :     }
   19077              : 
   19078              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   19079              :      default initialization is defined (5.1.2.4.4).  */
   19080      1721578 :   if (sym->ts.type == BT_DERIVED
   19081       129901 :       && sym->attr.dummy
   19082        44542 :       && sym->attr.intent == INTENT_OUT
   19083         2308 :       && sym->as
   19084          381 :       && sym->as->type == AS_ASSUMED_SIZE)
   19085              :     {
   19086            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   19087              :         {
   19088            1 :           if (c->initializer)
   19089              :             {
   19090            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   19091              :                          "ASSUMED SIZE and so cannot have a default initializer",
   19092              :                          sym->name, &sym->declared_at);
   19093            1 :               return;
   19094              :             }
   19095              :         }
   19096              :     }
   19097              : 
   19098              :   /* F2008, C542.  */
   19099      1721577 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19100        44541 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   19101              :     {
   19102            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   19103              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19104            0 :       return;
   19105              :     }
   19106              : 
   19107              :   /* TS18508.  */
   19108      1721577 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19109        44541 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   19110              :     {
   19111            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   19112              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19113            0 :       return;
   19114              :     }
   19115              : 
   19116              :   /* F2008, C525.  */
   19117      1721577 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19118      1721477 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19119        19693 :              && sym->ts.u.derived && CLASS_DATA (sym)
   19120        19687 :              && CLASS_DATA (sym)->attr.coarray_comp))
   19121      1721477 :        || class_attr.codimension)
   19122         1774 :       && (sym->attr.result || sym->result == sym))
   19123              :     {
   19124            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   19125              :                  "a coarray component", sym->name, &sym->declared_at);
   19126            8 :       return;
   19127              :     }
   19128              : 
   19129              :   /* F2008, C524.  */
   19130      1721569 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   19131          420 :       && sym->ts.u.derived->ts.is_iso_c)
   19132              :     {
   19133            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   19134              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   19135            3 :       return;
   19136              :     }
   19137              : 
   19138              :   /* F2008, C525.  */
   19139      1721566 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19140      1721469 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19141        19692 :             && sym->ts.u.derived && CLASS_DATA (sym)
   19142        19686 :             && CLASS_DATA (sym)->attr.coarray_comp))
   19143           97 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   19144           93 :           || class_attr.allocatable))
   19145              :     {
   19146            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   19147              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   19148              :                  sym->name, &sym->declared_at);
   19149            4 :       return;
   19150              :     }
   19151              : 
   19152              :   /* F2008, C526.  The function-result case was handled above.  */
   19153      1721562 :   if (class_attr.codimension
   19154         1666 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   19155          350 :            || sym->attr.select_type_temporary
   19156          274 :            || sym->attr.associate_var
   19157          256 :            || (sym->ns->save_all && !sym->attr.automatic)
   19158          256 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   19159          256 :            || sym->ns->proc_name->attr.is_main_program
   19160            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   19161              :     {
   19162            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   19163              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   19164            4 :       return;
   19165              :     }
   19166              :   /* F2008, C528.  */
   19167      1721558 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   19168         1586 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   19169              :     {
   19170            6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   19171              :                  "deferred shape without allocatable", sym->name,
   19172              :                  &sym->declared_at);
   19173            6 :       return;
   19174              :     }
   19175      1721552 :   else if (class_attr.codimension && class_attr.allocatable && as
   19176          614 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   19177              :     {
   19178            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   19179              :                  "deferred shape", sym->name, &sym->declared_at);
   19180            9 :       return;
   19181              :     }
   19182              : 
   19183              :   /* F2008, C541.  */
   19184      1721543 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19185      1721450 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19186        19687 :             && declared_has_coarray_comp))
   19187      1721443 :         || (class_attr.codimension && class_attr.allocatable))
   19188          705 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   19189              :     {
   19190            4 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   19191              :                  "allocatable coarray or have coarray components",
   19192              :                  sym->name, &sym->declared_at);
   19193            4 :       return;
   19194              :     }
   19195              : 
   19196      1721539 :   if (class_attr.codimension && sym->attr.dummy
   19197          467 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   19198              :     {
   19199            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   19200              :                  "procedure %qs", sym->name, &sym->declared_at,
   19201              :                  sym->ns->proc_name->name);
   19202            2 :       return;
   19203              :     }
   19204              : 
   19205      1721537 :   if (sym->ts.type == BT_LOGICAL
   19206       113621 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   19207       113618 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   19208        32387 :               && sym->ns->proc_name->attr.is_bind_c)))
   19209              :     {
   19210              :       int i;
   19211          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   19212          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   19213              :           break;
   19214           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   19215          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   19216              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   19217              :                               "%qs", sym->name, &sym->declared_at,
   19218           13 :                               sym->ns->proc_name->name))
   19219              :         return;
   19220          167 :       else if (!gfc_logical_kinds[i].c_bool
   19221          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   19222              :                                    "%qs at %L with non-C_Bool kind in "
   19223              :                                    "BIND(C) procedure %qs", sym->name,
   19224              :                                    &sym->declared_at,
   19225           15 :                                    sym->attr.function ? sym->name
   19226           13 :                                    : sym->ns->proc_name->name))
   19227              :         return;
   19228              :     }
   19229              : 
   19230      1721534 :   switch (sym->attr.flavor)
   19231              :     {
   19232       663878 :     case FL_VARIABLE:
   19233       663878 :       if (!resolve_fl_variable (sym, mp_flag))
   19234              :         return;
   19235              :       break;
   19236              : 
   19237       490625 :     case FL_PROCEDURE:
   19238       490625 :       if (sym->formal && !sym->formal_ns)
   19239              :         {
   19240              :           /* Check that none of the arguments are a namelist.  */
   19241              :           gfc_formal_arglist *formal = sym->formal;
   19242              : 
   19243       106427 :           for (; formal; formal = formal->next)
   19244        72164 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   19245              :               {
   19246            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   19247              :                            "subroutine or function at %L",
   19248              :                            formal->sym->name, &sym->declared_at);
   19249            1 :                 return;
   19250              :               }
   19251              :         }
   19252              : 
   19253       490624 :       if (!resolve_fl_procedure (sym, mp_flag))
   19254              :         return;
   19255              :       break;
   19256              : 
   19257          863 :     case FL_NAMELIST:
   19258          863 :       if (!resolve_fl_namelist (sym))
   19259              :         return;
   19260              :       break;
   19261              : 
   19262       408469 :     case FL_PARAMETER:
   19263       408469 :       if (!resolve_fl_parameter (sym))
   19264              :         return;
   19265              :       break;
   19266              : 
   19267              :     default:
   19268              :       break;
   19269              :     }
   19270              : 
   19271              :   /* Resolve array specifier. Check as well some constraints
   19272              :      on COMMON blocks.  */
   19273              : 
   19274      1721337 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   19275              : 
   19276      1721337 :   resolve_symbol_array_spec (sym, check_constant);
   19277              : 
   19278              :   /* Resolve formal namespaces.  */
   19279      1721337 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   19280       272334 :       && !sym->attr.contained && !sym->attr.intrinsic)
   19281       242815 :     gfc_resolve (sym->formal_ns);
   19282              : 
   19283              :   /* Make sure the formal namespace is present.  */
   19284      1721337 :   if (sym->formal && !sym->formal_ns)
   19285              :     {
   19286              :       gfc_formal_arglist *formal = sym->formal;
   19287        34732 :       while (formal && !formal->sym)
   19288           11 :         formal = formal->next;
   19289              : 
   19290        34721 :       if (formal)
   19291              :         {
   19292        34710 :           sym->formal_ns = formal->sym->ns;
   19293        34710 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   19294        26279 :             sym->formal_ns->refs++;
   19295              :         }
   19296              :     }
   19297              : 
   19298              :   /* Check threadprivate restrictions.  */
   19299      1721337 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   19300          384 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   19301           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19302           32 :       && sym->module == NULL
   19303           17 :       && (sym->ns->proc_name == NULL
   19304           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19305            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19306              :     {
   19307            2 :       if (sym->attr.threadprivate)
   19308            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19309              :       else
   19310            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19311              :                    "attribute", sym->name, &sym->declared_at);
   19312              :     }
   19313              : 
   19314      1721337 :   if (sym->attr.omp_groupprivate && sym->value)
   19315            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19316              :                "initializer", sym->name, &sym->declared_at);
   19317              : 
   19318              :   /* Check omp declare target restrictions.  */
   19319      1721337 :   if ((sym->attr.omp_declare_target
   19320      1719920 :        || sym->attr.omp_declare_target_link
   19321      1719872 :        || sym->attr.omp_declare_target_local)
   19322         1505 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19323         1458 :       && sym->attr.flavor == FL_VARIABLE
   19324          616 :       && !sym->attr.save
   19325          199 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19326          199 :       && (!sym->attr.in_common
   19327          186 :           && sym->module == NULL
   19328           96 :           && (sym->ns->proc_name == NULL
   19329           96 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19330            6 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19331            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19332              :                sym->name, &sym->declared_at);
   19333              : 
   19334              :   /* If we have come this far we can apply default-initializers, as
   19335              :      described in 14.7.5, to those variables that have not already
   19336              :      been assigned one.  */
   19337      1721337 :   if (sym->ts.type == BT_DERIVED
   19338       129871 :       && !sym->value
   19339       105244 :       && !sym->attr.allocatable
   19340       102233 :       && !sym->attr.alloc_comp)
   19341              :     {
   19342       102168 :       symbol_attribute *a = &sym->attr;
   19343              : 
   19344       102168 :       if ((!a->save && !a->dummy && !a->pointer
   19345        56270 :            && !a->in_common && !a->use_assoc
   19346        10599 :            && a->referenced
   19347         8310 :            && !((a->function || a->result)
   19348         1704 :                 && (!a->dimension
   19349          160 :                     || sym->ts.u.derived->attr.alloc_comp
   19350           95 :                     || sym->ts.u.derived->attr.pointer_comp))
   19351         6687 :            && !(a->function && sym != sym->result))
   19352        95501 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19353         1480 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19354         8048 :         apply_default_init (sym);
   19355        94120 :       else if (a->function && !a->pointer && !a->allocatable
   19356        20639 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19357              :         /* Default initialization for function results.  */
   19358         2752 :         apply_default_init (sym->result);
   19359        91368 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19360        11645 :                && (sym->ts.u.derived->attr.alloc_comp
   19361        11080 :                    || sym->ts.u.derived->attr.pointer_comp))
   19362              :         /* Mark the result symbol to be referenced, when it has allocatable
   19363              :            components.  */
   19364          624 :         sym->result->attr.referenced = 1;
   19365              :     }
   19366              : 
   19367      1721337 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19368        19187 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19369         1226 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19370         1151 :       && !CLASS_DATA (sym)->attr.class_pointer
   19371         1125 :       && !CLASS_DATA (sym)->attr.allocatable)
   19372          853 :     apply_default_init (sym);
   19373              : 
   19374              :   /* If this symbol has a type-spec, check it.  */
   19375      1721337 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19376       649100 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19377      1400804 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19378              :       return;
   19379              : 
   19380      1721334 :   if (sym->param_list)
   19381         1462 :     resolve_pdt (sym);
   19382              : }
   19383              : 
   19384              : 
   19385         4125 : void gfc_resolve_symbol (gfc_symbol *sym)
   19386              : {
   19387         4125 :   resolve_symbol (sym);
   19388         4125 :   return;
   19389              : }
   19390              : 
   19391              : 
   19392              : /************* Resolve DATA statements *************/
   19393              : 
   19394              : static struct
   19395              : {
   19396              :   gfc_data_value *vnode;
   19397              :   mpz_t left;
   19398              : }
   19399              : values;
   19400              : 
   19401              : 
   19402              : /* Advance the values structure to point to the next value in the data list.  */
   19403              : 
   19404              : static bool
   19405        10892 : next_data_value (void)
   19406              : {
   19407        16660 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19408              :     {
   19409              : 
   19410         8198 :       if (values.vnode->next == NULL)
   19411              :         return false;
   19412              : 
   19413         5768 :       values.vnode = values.vnode->next;
   19414         5768 :       mpz_set (values.left, values.vnode->repeat);
   19415              :     }
   19416              : 
   19417              :   return true;
   19418              : }
   19419              : 
   19420              : 
   19421              : static bool
   19422         3557 : check_data_variable (gfc_data_variable *var, locus *where)
   19423              : {
   19424         3557 :   gfc_expr *e;
   19425         3557 :   mpz_t size;
   19426         3557 :   mpz_t offset;
   19427         3557 :   bool t;
   19428         3557 :   ar_type mark = AR_UNKNOWN;
   19429         3557 :   int i;
   19430         3557 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19431         3557 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19432         3557 :   gfc_ref *ref;
   19433         3557 :   gfc_array_ref *ar;
   19434         3557 :   gfc_symbol *sym;
   19435         3557 :   int has_pointer;
   19436              : 
   19437         3557 :   if (!gfc_resolve_expr (var->expr))
   19438              :     return false;
   19439              : 
   19440         3557 :   ar = NULL;
   19441         3557 :   e = var->expr;
   19442              : 
   19443         3557 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19444            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19445            0 :     e = e->value.function.actual->expr;
   19446              : 
   19447         3557 :   if (e->expr_type != EXPR_VARIABLE)
   19448              :     {
   19449            0 :       gfc_error ("Expecting definable entity near %L", where);
   19450            0 :       return false;
   19451              :     }
   19452              : 
   19453         3557 :   sym = e->symtree->n.sym;
   19454              : 
   19455         3557 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19456              :     {
   19457            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19458              :                  sym->name, &sym->declared_at);
   19459            2 :       return false;
   19460              :     }
   19461              : 
   19462         3555 :   if (e->ref == NULL && sym->as)
   19463              :     {
   19464            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19465              :                  " declaration", sym->name, where);
   19466            1 :       return false;
   19467              :     }
   19468              : 
   19469         3554 :   if (gfc_is_coindexed (e))
   19470              :     {
   19471            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19472              :                  where);
   19473            7 :       return false;
   19474              :     }
   19475              : 
   19476         3547 :   has_pointer = sym->attr.pointer;
   19477              : 
   19478         5988 :   for (ref = e->ref; ref; ref = ref->next)
   19479              :     {
   19480         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19481              :         has_pointer = 1;
   19482              : 
   19483         2419 :       if (has_pointer)
   19484              :         {
   19485           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19486              :             {
   19487            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19488              :                          "be a full array", sym->name, where);
   19489            1 :               return false;
   19490              :             }
   19491              : 
   19492           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19493              :             {
   19494            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19495              :                          "and the corresponding DATA value is not a valid "
   19496              :                          "initial-data-target", where);
   19497            1 :               return false;
   19498              :             }
   19499              :         }
   19500              : 
   19501         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19502              :         {
   19503            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19504              :                      "attribute", ref->u.c.component->name, &e->where);
   19505            1 :           return false;
   19506              :         }
   19507              : 
   19508              :       /* Reject substrings of strings of non-constant length.  */
   19509         2442 :       if (ref->type == REF_SUBSTRING
   19510           73 :           && ref->u.ss.length
   19511           73 :           && ref->u.ss.length->length
   19512         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19513            1 :         goto bad_charlen;
   19514              :     }
   19515              : 
   19516              :   /* Reject strings with deferred length or non-constant length.  */
   19517         3543 :   if (e->ts.type == BT_CHARACTER
   19518         3543 :       && (e->ts.deferred
   19519          374 :           || (e->ts.u.cl->length
   19520          323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19521            5 :     goto bad_charlen;
   19522              : 
   19523         3538 :   mpz_init_set_si (offset, 0);
   19524              : 
   19525         3538 :   if (e->rank == 0 || has_pointer)
   19526              :     {
   19527         2691 :       mpz_init_set_ui (size, 1);
   19528         2691 :       ref = NULL;
   19529              :     }
   19530              :   else
   19531              :     {
   19532          847 :       ref = e->ref;
   19533              : 
   19534              :       /* Find the array section reference.  */
   19535         1030 :       for (ref = e->ref; ref; ref = ref->next)
   19536              :         {
   19537         1030 :           if (ref->type != REF_ARRAY)
   19538           92 :             continue;
   19539          938 :           if (ref->u.ar.type == AR_ELEMENT)
   19540           91 :             continue;
   19541              :           break;
   19542              :         }
   19543          847 :       gcc_assert (ref);
   19544              : 
   19545              :       /* Set marks according to the reference pattern.  */
   19546          847 :       switch (ref->u.ar.type)
   19547              :         {
   19548              :         case AR_FULL:
   19549              :           mark = AR_FULL;
   19550              :           break;
   19551              : 
   19552          151 :         case AR_SECTION:
   19553          151 :           ar = &ref->u.ar;
   19554              :           /* Get the start position of array section.  */
   19555          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   19556          151 :           mark = AR_SECTION;
   19557          151 :           break;
   19558              : 
   19559            0 :         default:
   19560            0 :           gcc_unreachable ();
   19561              :         }
   19562              : 
   19563          847 :       if (!gfc_array_size (e, &size))
   19564              :         {
   19565            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   19566              :                      where);
   19567            1 :           mpz_clear (offset);
   19568            1 :           return false;
   19569              :         }
   19570              :     }
   19571              : 
   19572         3537 :   t = true;
   19573              : 
   19574        11937 :   while (mpz_cmp_ui (size, 0) > 0)
   19575              :     {
   19576         8463 :       if (!next_data_value ())
   19577              :         {
   19578            1 :           gfc_error ("DATA statement at %L has more variables than values",
   19579              :                      where);
   19580            1 :           t = false;
   19581            1 :           break;
   19582              :         }
   19583              : 
   19584         8462 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   19585         8462 :       if (!t)
   19586              :         break;
   19587              : 
   19588              :       /* If we have more than one element left in the repeat count,
   19589              :          and we have more than one element left in the target variable,
   19590              :          then create a range assignment.  */
   19591              :       /* FIXME: Only done for full arrays for now, since array sections
   19592              :          seem tricky.  */
   19593         8443 :       if (mark == AR_FULL && ref && ref->next == NULL
   19594         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   19595              :         {
   19596          137 :           mpz_t range;
   19597              : 
   19598          137 :           if (mpz_cmp (size, values.left) >= 0)
   19599              :             {
   19600          126 :               mpz_init_set (range, values.left);
   19601          126 :               mpz_sub (size, size, values.left);
   19602          126 :               mpz_set_ui (values.left, 0);
   19603              :             }
   19604              :           else
   19605              :             {
   19606           11 :               mpz_init_set (range, size);
   19607           11 :               mpz_sub (values.left, values.left, size);
   19608           11 :               mpz_set_ui (size, 0);
   19609              :             }
   19610              : 
   19611          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19612              :                                      offset, &range);
   19613              : 
   19614          137 :           mpz_add (offset, offset, range);
   19615          137 :           mpz_clear (range);
   19616              : 
   19617          137 :           if (!t)
   19618              :             break;
   19619          129 :         }
   19620              : 
   19621              :       /* Assign initial value to symbol.  */
   19622              :       else
   19623              :         {
   19624         8306 :           mpz_sub_ui (values.left, values.left, 1);
   19625         8306 :           mpz_sub_ui (size, size, 1);
   19626              : 
   19627         8306 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19628              :                                      offset, NULL);
   19629         8306 :           if (!t)
   19630              :             break;
   19631              : 
   19632         8271 :           if (mark == AR_FULL)
   19633         5259 :             mpz_add_ui (offset, offset, 1);
   19634              : 
   19635              :           /* Modify the array section indexes and recalculate the offset
   19636              :              for next element.  */
   19637         3012 :           else if (mark == AR_SECTION)
   19638          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   19639              :         }
   19640              :     }
   19641              : 
   19642         3537 :   if (mark == AR_SECTION)
   19643              :     {
   19644          344 :       for (i = 0; i < ar->dimen; i++)
   19645          194 :         mpz_clear (section_index[i]);
   19646              :     }
   19647              : 
   19648         3537 :   mpz_clear (size);
   19649         3537 :   mpz_clear (offset);
   19650              : 
   19651         3537 :   return t;
   19652              : 
   19653            6 : bad_charlen:
   19654            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   19655              :              &e->where);
   19656            6 :   return false;
   19657              : }
   19658              : 
   19659              : 
   19660              : static bool traverse_data_var (gfc_data_variable *, locus *);
   19661              : 
   19662              : /* Iterate over a list of elements in a DATA statement.  */
   19663              : 
   19664              : static bool
   19665          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   19666              : {
   19667          237 :   mpz_t trip;
   19668          237 :   iterator_stack frame;
   19669          237 :   gfc_expr *e, *start, *end, *step;
   19670          237 :   bool retval = true;
   19671              : 
   19672          237 :   mpz_init (frame.value);
   19673          237 :   mpz_init (trip);
   19674              : 
   19675          237 :   start = gfc_copy_expr (var->iter.start);
   19676          237 :   end = gfc_copy_expr (var->iter.end);
   19677          237 :   step = gfc_copy_expr (var->iter.step);
   19678              : 
   19679          237 :   if (!gfc_simplify_expr (start, 1)
   19680          237 :       || start->expr_type != EXPR_CONSTANT)
   19681              :     {
   19682            0 :       gfc_error ("start of implied-do loop at %L could not be "
   19683              :                  "simplified to a constant value", &start->where);
   19684            0 :       retval = false;
   19685            0 :       goto cleanup;
   19686              :     }
   19687          237 :   if (!gfc_simplify_expr (end, 1)
   19688          237 :       || end->expr_type != EXPR_CONSTANT)
   19689              :     {
   19690            0 :       gfc_error ("end of implied-do loop at %L could not be "
   19691              :                  "simplified to a constant value", &end->where);
   19692            0 :       retval = false;
   19693            0 :       goto cleanup;
   19694              :     }
   19695          237 :   if (!gfc_simplify_expr (step, 1)
   19696          237 :       || step->expr_type != EXPR_CONSTANT)
   19697              :     {
   19698            0 :       gfc_error ("step of implied-do loop at %L could not be "
   19699              :                  "simplified to a constant value", &step->where);
   19700            0 :       retval = false;
   19701            0 :       goto cleanup;
   19702              :     }
   19703          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   19704              :     {
   19705            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   19706              :                  &step->where);
   19707            1 :       retval = false;
   19708            1 :       goto cleanup;
   19709              :     }
   19710              : 
   19711          236 :   mpz_set (trip, end->value.integer);
   19712          236 :   mpz_sub (trip, trip, start->value.integer);
   19713          236 :   mpz_add (trip, trip, step->value.integer);
   19714              : 
   19715          236 :   mpz_div (trip, trip, step->value.integer);
   19716              : 
   19717          236 :   mpz_set (frame.value, start->value.integer);
   19718              : 
   19719          236 :   frame.prev = iter_stack;
   19720          236 :   frame.variable = var->iter.var->symtree;
   19721          236 :   iter_stack = &frame;
   19722              : 
   19723         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   19724              :     {
   19725          905 :       if (!traverse_data_var (var->list, where))
   19726              :         {
   19727           14 :           retval = false;
   19728           14 :           goto cleanup;
   19729              :         }
   19730              : 
   19731          891 :       e = gfc_copy_expr (var->expr);
   19732          891 :       if (!gfc_simplify_expr (e, 1))
   19733              :         {
   19734            0 :           gfc_free_expr (e);
   19735            0 :           retval = false;
   19736            0 :           goto cleanup;
   19737              :         }
   19738              : 
   19739          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   19740              : 
   19741          891 :       mpz_sub_ui (trip, trip, 1);
   19742              :     }
   19743              : 
   19744          222 : cleanup:
   19745          237 :   mpz_clear (frame.value);
   19746          237 :   mpz_clear (trip);
   19747              : 
   19748          237 :   gfc_free_expr (start);
   19749          237 :   gfc_free_expr (end);
   19750          237 :   gfc_free_expr (step);
   19751              : 
   19752          237 :   iter_stack = frame.prev;
   19753          237 :   return retval;
   19754              : }
   19755              : 
   19756              : 
   19757              : /* Type resolve variables in the variable list of a DATA statement.  */
   19758              : 
   19759              : static bool
   19760         3418 : traverse_data_var (gfc_data_variable *var, locus *where)
   19761              : {
   19762         3418 :   bool t;
   19763              : 
   19764         7114 :   for (; var; var = var->next)
   19765              :     {
   19766         3794 :       if (var->expr == NULL)
   19767          237 :         t = traverse_data_list (var, where);
   19768              :       else
   19769         3557 :         t = check_data_variable (var, where);
   19770              : 
   19771         3794 :       if (!t)
   19772              :         return false;
   19773              :     }
   19774              : 
   19775              :   return true;
   19776              : }
   19777              : 
   19778              : 
   19779              : /* Resolve the expressions and iterators associated with a data statement.
   19780              :    This is separate from the assignment checking because data lists should
   19781              :    only be resolved once.  */
   19782              : 
   19783              : static bool
   19784         2668 : resolve_data_variables (gfc_data_variable *d)
   19785              : {
   19786         5707 :   for (; d; d = d->next)
   19787              :     {
   19788         3044 :       if (d->list == NULL)
   19789              :         {
   19790         2891 :           if (!gfc_resolve_expr (d->expr))
   19791              :             return false;
   19792              :         }
   19793              :       else
   19794              :         {
   19795          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   19796              :             return false;
   19797              : 
   19798          150 :           if (!resolve_data_variables (d->list))
   19799              :             return false;
   19800              :         }
   19801              :     }
   19802              : 
   19803              :   return true;
   19804              : }
   19805              : 
   19806              : 
   19807              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   19808              :    the value list into static variables, and then recursively traversing the
   19809              :    variables list, expanding iterators and such.  */
   19810              : 
   19811              : static void
   19812         2518 : resolve_data (gfc_data *d)
   19813              : {
   19814              : 
   19815         2518 :   if (!resolve_data_variables (d->var))
   19816              :     return;
   19817              : 
   19818         2513 :   values.vnode = d->value;
   19819         2513 :   if (d->value == NULL)
   19820            0 :     mpz_set_ui (values.left, 0);
   19821              :   else
   19822         2513 :     mpz_set (values.left, d->value->repeat);
   19823              : 
   19824         2513 :   if (!traverse_data_var (d->var, &d->where))
   19825              :     return;
   19826              : 
   19827              :   /* At this point, we better not have any values left.  */
   19828              : 
   19829         2429 :   if (next_data_value ())
   19830            0 :     gfc_error ("DATA statement at %L has more values than variables",
   19831              :                &d->where);
   19832              : }
   19833              : 
   19834              : 
   19835              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   19836              :    accessed by host or use association, is a dummy argument to a pure function,
   19837              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   19838              :    is storage associated with any such variable, shall not be used in the
   19839              :    following contexts: (clients of this function).  */
   19840              : 
   19841              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   19842              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   19843              :    problem.  */
   19844              : bool
   19845        56367 : gfc_impure_variable (gfc_symbol *sym)
   19846              : {
   19847        56367 :   gfc_symbol *proc;
   19848        56367 :   gfc_namespace *ns;
   19849              : 
   19850        56367 :   if (sym->attr.use_assoc || sym->attr.in_common)
   19851              :     return 1;
   19852              : 
   19853              :   /* The namespace of a module procedure interface holds the arguments and
   19854              :      symbols, and so the symbol namespace can be different to that of the
   19855              :      procedure.  */
   19856        55749 :   if (sym->ns != gfc_current_ns
   19857         5957 :       && gfc_current_ns->proc_name->abr_modproc_decl
   19858           48 :       && sym->ns->proc_name->attr.function
   19859           12 :       && sym->attr.result
   19860           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   19861              :     return 0;
   19862              : 
   19863              :   /* Check if the symbol's ns is inside the pure procedure.  */
   19864        60440 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   19865              :     {
   19866        60156 :       if (ns == sym->ns)
   19867              :         break;
   19868         6264 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   19869         5196 :           && !(sym->attr.function || sym->attr.result))
   19870              :         return 1;
   19871              :     }
   19872              : 
   19873        54176 :   proc = sym->ns->proc_name;
   19874        54176 :   if (sym->attr.dummy
   19875         5994 :       && !sym->attr.value
   19876         5872 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   19877         5669 :           || proc->attr.function))
   19878          697 :     return 1;
   19879              : 
   19880              :   /* TODO: Sort out what can be storage associated, if anything, and include
   19881              :      it here.  In principle equivalences should be scanned but it does not
   19882              :      seem to be possible to storage associate an impure variable this way.  */
   19883              :   return 0;
   19884              : }
   19885              : 
   19886              : 
   19887              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   19888              :    current namespace is inside a pure procedure.  */
   19889              : 
   19890              : bool
   19891      2351349 : gfc_pure (gfc_symbol *sym)
   19892              : {
   19893      2351349 :   symbol_attribute attr;
   19894      2351349 :   gfc_namespace *ns;
   19895              : 
   19896      2351349 :   if (sym == NULL)
   19897              :     {
   19898              :       /* Check if the current namespace or one of its parents
   19899              :         belongs to a pure procedure.  */
   19900      3199482 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19901              :         {
   19902      1890715 :           sym = ns->proc_name;
   19903      1890715 :           if (sym == NULL)
   19904              :             return 0;
   19905      1889574 :           attr = sym->attr;
   19906      1889574 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   19907              :             return 1;
   19908              :         }
   19909              :       return 0;
   19910              :     }
   19911              : 
   19912      1033939 :   attr = sym->attr;
   19913              : 
   19914      1033939 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   19915              : }
   19916              : 
   19917              : 
   19918              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   19919              :    checks if the current namespace is implicitly pure.  Note that this
   19920              :    function returns false for a PURE procedure.  */
   19921              : 
   19922              : bool
   19923       728521 : gfc_implicit_pure (gfc_symbol *sym)
   19924              : {
   19925       728521 :   gfc_namespace *ns;
   19926              : 
   19927       728521 :   if (sym == NULL)
   19928              :     {
   19929              :       /* Check if the current procedure is implicit_pure.  Walk up
   19930              :          the procedure list until we find a procedure.  */
   19931      1003868 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19932              :         {
   19933       716568 :           sym = ns->proc_name;
   19934       716568 :           if (sym == NULL)
   19935              :             return 0;
   19936              : 
   19937       716495 :           if (sym->attr.flavor == FL_PROCEDURE)
   19938              :             break;
   19939              :         }
   19940              :     }
   19941              : 
   19942       441145 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   19943       755966 :     && !sym->attr.pure;
   19944              : }
   19945              : 
   19946              : 
   19947              : void
   19948       426970 : gfc_unset_implicit_pure (gfc_symbol *sym)
   19949              : {
   19950       426970 :   gfc_namespace *ns;
   19951              : 
   19952       426970 :   if (sym == NULL)
   19953              :     {
   19954              :       /* Check if the current procedure is implicit_pure.  Walk up
   19955              :          the procedure list until we find a procedure.  */
   19956       697610 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19957              :         {
   19958       431573 :           sym = ns->proc_name;
   19959       431573 :           if (sym == NULL)
   19960              :             return;
   19961              : 
   19962       430740 :           if (sym->attr.flavor == FL_PROCEDURE)
   19963              :             break;
   19964              :         }
   19965              :     }
   19966              : 
   19967       426137 :   if (sym->attr.flavor == FL_PROCEDURE)
   19968       151731 :     sym->attr.implicit_pure = 0;
   19969              :   else
   19970       274406 :     sym->attr.pure = 0;
   19971              : }
   19972              : 
   19973              : 
   19974              : /* Test whether the current procedure is elemental or not.  */
   19975              : 
   19976              : bool
   19977      1391760 : gfc_elemental (gfc_symbol *sym)
   19978              : {
   19979      1391760 :   symbol_attribute attr;
   19980              : 
   19981      1391760 :   if (sym == NULL)
   19982            0 :     sym = gfc_current_ns->proc_name;
   19983            0 :   if (sym == NULL)
   19984              :     return 0;
   19985      1391760 :   attr = sym->attr;
   19986              : 
   19987      1391760 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   19988              : }
   19989              : 
   19990              : 
   19991              : /* Warn about unused labels.  */
   19992              : 
   19993              : static void
   19994         4840 : warn_unused_fortran_label (gfc_st_label *label)
   19995              : {
   19996         4866 :   if (label == NULL)
   19997              :     return;
   19998              : 
   19999           27 :   warn_unused_fortran_label (label->left);
   20000              : 
   20001           27 :   if (label->defined == ST_LABEL_UNKNOWN)
   20002              :     return;
   20003              : 
   20004           26 :   switch (label->referenced)
   20005              :     {
   20006            2 :     case ST_LABEL_UNKNOWN:
   20007            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   20008              :                    label->value, &label->where);
   20009            2 :       break;
   20010              : 
   20011            1 :     case ST_LABEL_BAD_TARGET:
   20012            1 :       gfc_warning (OPT_Wunused_label,
   20013              :                    "Label %d at %L defined but cannot be used",
   20014              :                    label->value, &label->where);
   20015            1 :       break;
   20016              : 
   20017              :     default:
   20018              :       break;
   20019              :     }
   20020              : 
   20021           26 :   warn_unused_fortran_label (label->right);
   20022              : }
   20023              : 
   20024              : 
   20025              : /* Returns the sequence type of a symbol or sequence.  */
   20026              : 
   20027              : static seq_type
   20028         1076 : sequence_type (gfc_typespec ts)
   20029              : {
   20030         1076 :   seq_type result;
   20031         1076 :   gfc_component *c;
   20032              : 
   20033         1076 :   switch (ts.type)
   20034              :   {
   20035           49 :     case BT_DERIVED:
   20036              : 
   20037           49 :       if (ts.u.derived->components == NULL)
   20038              :         return SEQ_NONDEFAULT;
   20039              : 
   20040           49 :       result = sequence_type (ts.u.derived->components->ts);
   20041          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   20042           67 :         if (sequence_type (c->ts) != result)
   20043              :           return SEQ_MIXED;
   20044              : 
   20045              :       return result;
   20046              : 
   20047          129 :     case BT_CHARACTER:
   20048          129 :       if (ts.kind != gfc_default_character_kind)
   20049            0 :           return SEQ_NONDEFAULT;
   20050              : 
   20051              :       return SEQ_CHARACTER;
   20052              : 
   20053          240 :     case BT_INTEGER:
   20054          240 :       if (ts.kind != gfc_default_integer_kind)
   20055           25 :           return SEQ_NONDEFAULT;
   20056              : 
   20057              :       return SEQ_NUMERIC;
   20058              : 
   20059          559 :     case BT_REAL:
   20060          559 :       if (!(ts.kind == gfc_default_real_kind
   20061          269 :             || ts.kind == gfc_default_double_kind))
   20062            0 :           return SEQ_NONDEFAULT;
   20063              : 
   20064              :       return SEQ_NUMERIC;
   20065              : 
   20066           81 :     case BT_COMPLEX:
   20067           81 :       if (ts.kind != gfc_default_complex_kind)
   20068           48 :           return SEQ_NONDEFAULT;
   20069              : 
   20070              :       return SEQ_NUMERIC;
   20071              : 
   20072           17 :     case BT_LOGICAL:
   20073           17 :       if (ts.kind != gfc_default_logical_kind)
   20074            0 :           return SEQ_NONDEFAULT;
   20075              : 
   20076              :       return SEQ_NUMERIC;
   20077              : 
   20078              :     default:
   20079              :       return SEQ_NONDEFAULT;
   20080              :   }
   20081              : }
   20082              : 
   20083              : 
   20084              : /* Resolve derived type EQUIVALENCE object.  */
   20085              : 
   20086              : static bool
   20087           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   20088              : {
   20089           80 :   gfc_component *c = derived->components;
   20090              : 
   20091           80 :   if (!derived)
   20092              :     return true;
   20093              : 
   20094              :   /* Shall not be an object of nonsequence derived type.  */
   20095           80 :   if (!derived->attr.sequence)
   20096              :     {
   20097            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   20098              :                  "attribute to be an EQUIVALENCE object", sym->name,
   20099              :                  &e->where);
   20100            0 :       return false;
   20101              :     }
   20102              : 
   20103              :   /* Shall not have allocatable components.  */
   20104           80 :   if (derived->attr.alloc_comp)
   20105              :     {
   20106            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   20107              :                  "components to be an EQUIVALENCE object",sym->name,
   20108              :                  &e->where);
   20109            1 :       return false;
   20110              :     }
   20111              : 
   20112           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   20113              :     {
   20114            1 :       gfc_error ("Derived type variable %qs at %L with default "
   20115              :                  "initialization cannot be in EQUIVALENCE with a variable "
   20116              :                  "in COMMON", sym->name, &e->where);
   20117            1 :       return false;
   20118              :     }
   20119              : 
   20120          245 :   for (; c ; c = c->next)
   20121              :     {
   20122          167 :       if (gfc_bt_struct (c->ts.type)
   20123          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   20124              :         return false;
   20125              : 
   20126              :       /* Shall not be an object of sequence derived type containing a pointer
   20127              :          in the structure.  */
   20128          167 :       if (c->attr.pointer)
   20129              :         {
   20130            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   20131              :                      "component(s) cannot be an EQUIVALENCE object",
   20132              :                      sym->name, &e->where);
   20133            0 :           return false;
   20134              :         }
   20135              :     }
   20136              :   return true;
   20137              : }
   20138              : 
   20139              : 
   20140              : /* Resolve equivalence object.
   20141              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   20142              :    an allocatable array, an object of nonsequence derived type, an object of
   20143              :    sequence derived type containing a pointer at any level of component
   20144              :    selection, an automatic object, a function name, an entry name, a result
   20145              :    name, a named constant, a structure component, or a subobject of any of
   20146              :    the preceding objects.  A substring shall not have length zero.  A
   20147              :    derived type shall not have components with default initialization nor
   20148              :    shall two objects of an equivalence group be initialized.
   20149              :    Either all or none of the objects shall have an protected attribute.
   20150              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   20151              :    are implemented here.  */
   20152              : 
   20153              : static void
   20154         1565 : resolve_equivalence (gfc_equiv *eq)
   20155              : {
   20156         1565 :   gfc_symbol *sym;
   20157         1565 :   gfc_symbol *first_sym;
   20158         1565 :   gfc_expr *e;
   20159         1565 :   gfc_ref *r;
   20160         1565 :   locus *last_where = NULL;
   20161         1565 :   seq_type eq_type, last_eq_type;
   20162         1565 :   gfc_typespec *last_ts;
   20163         1565 :   int object, cnt_protected;
   20164         1565 :   const char *msg;
   20165              : 
   20166         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   20167              : 
   20168         1565 :   first_sym = eq->expr->symtree->n.sym;
   20169              : 
   20170         1565 :   cnt_protected = 0;
   20171              : 
   20172         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   20173              :     {
   20174         3171 :       e = eq->expr;
   20175              : 
   20176         3171 :       e->ts = e->symtree->n.sym->ts;
   20177              :       /* match_varspec might not know yet if it is seeing
   20178              :          array reference or substring reference, as it doesn't
   20179              :          know the types.  */
   20180         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   20181              :         {
   20182         2152 :           gfc_ref *ref = e->ref;
   20183         2152 :           sym = e->symtree->n.sym;
   20184              : 
   20185         2152 :           if (sym->attr.dimension)
   20186              :             {
   20187         1855 :               ref->u.ar.as = sym->as;
   20188         1855 :               ref = ref->next;
   20189              :             }
   20190              : 
   20191              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   20192         2152 :           if (e->ts.type == BT_CHARACTER
   20193          592 :               && ref
   20194          371 :               && ref->type == REF_ARRAY
   20195          371 :               && ref->u.ar.dimen == 1
   20196          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   20197          371 :               && ref->u.ar.stride[0] == NULL)
   20198              :             {
   20199          370 :               gfc_expr *start = ref->u.ar.start[0];
   20200          370 :               gfc_expr *end = ref->u.ar.end[0];
   20201          370 :               void *mem = NULL;
   20202              : 
   20203              :               /* Optimize away the (:) reference.  */
   20204          370 :               if (start == NULL && end == NULL)
   20205              :                 {
   20206            9 :                   if (e->ref == ref)
   20207            0 :                     e->ref = ref->next;
   20208              :                   else
   20209            9 :                     e->ref->next = ref->next;
   20210              :                   mem = ref;
   20211              :                 }
   20212              :               else
   20213              :                 {
   20214          361 :                   ref->type = REF_SUBSTRING;
   20215          361 :                   if (start == NULL)
   20216            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   20217              :                                               NULL, 1);
   20218          361 :                   ref->u.ss.start = start;
   20219          361 :                   if (end == NULL && e->ts.u.cl)
   20220           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   20221          361 :                   ref->u.ss.end = end;
   20222          361 :                   ref->u.ss.length = e->ts.u.cl;
   20223          361 :                   e->ts.u.cl = NULL;
   20224              :                 }
   20225          370 :               ref = ref->next;
   20226          370 :               free (mem);
   20227              :             }
   20228              : 
   20229              :           /* Any further ref is an error.  */
   20230         1930 :           if (ref)
   20231              :             {
   20232            1 :               gcc_assert (ref->type == REF_ARRAY);
   20233            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   20234              :                          &ref->u.ar.where);
   20235            1 :               continue;
   20236              :             }
   20237              :         }
   20238              : 
   20239         3170 :       if (!gfc_resolve_expr (e))
   20240            2 :         continue;
   20241              : 
   20242         3168 :       sym = e->symtree->n.sym;
   20243              : 
   20244         3168 :       if (sym->attr.is_protected)
   20245            2 :         cnt_protected++;
   20246         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   20247              :         {
   20248            2 :               gfc_error ("Either all or none of the objects in the "
   20249              :                          "EQUIVALENCE set at %L shall have the "
   20250              :                          "PROTECTED attribute",
   20251              :                          &e->where);
   20252            2 :               break;
   20253              :         }
   20254              : 
   20255              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   20256         3166 :       if (sym->ns->proc_name
   20257         3150 :           && sym->ns->proc_name->attr.pure
   20258            7 :           && sym->attr.in_common)
   20259              :         {
   20260              :           /* Need to check for symbols that may have entered the pure
   20261              :              procedure via a USE statement.  */
   20262            7 :           bool saw_sym = false;
   20263            7 :           if (sym->ns->use_stmts)
   20264              :             {
   20265            6 :               gfc_use_rename *r;
   20266           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   20267            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   20268              :             }
   20269              :           else
   20270              :             saw_sym = true;
   20271              : 
   20272            6 :           if (saw_sym)
   20273            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   20274              :                        "EQUIVALENCE object in the pure procedure %qs",
   20275              :                        sym->name, &e->where, sym->ns->proc_name->name);
   20276              :           break;
   20277              :         }
   20278              : 
   20279              :       /* Shall not be a named constant.  */
   20280         3159 :       if (e->expr_type == EXPR_CONSTANT)
   20281              :         {
   20282            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   20283              :                      "object", sym->name, &e->where);
   20284            0 :           continue;
   20285              :         }
   20286              : 
   20287         3161 :       if (e->ts.type == BT_DERIVED
   20288         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   20289            2 :         continue;
   20290              : 
   20291              :       /* Check that the types correspond correctly:
   20292              :          Note 5.28:
   20293              :          A numeric sequence structure may be equivalenced to another sequence
   20294              :          structure, an object of default integer type, default real type, double
   20295              :          precision real type, default logical type such that components of the
   20296              :          structure ultimately only become associated to objects of the same
   20297              :          kind. A character sequence structure may be equivalenced to an object
   20298              :          of default character kind or another character sequence structure.
   20299              :          Other objects may be equivalenced only to objects of the same type and
   20300              :          kind parameters.  */
   20301              : 
   20302              :       /* Identical types are unconditionally OK.  */
   20303         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   20304         2677 :         goto identical_types;
   20305              : 
   20306          480 :       last_eq_type = sequence_type (*last_ts);
   20307          480 :       eq_type = sequence_type (sym->ts);
   20308              : 
   20309              :       /* Since the pair of objects is not of the same type, mixed or
   20310              :          non-default sequences can be rejected.  */
   20311              : 
   20312          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20313              :                "statement at %L with different type objects");
   20314          481 :       if ((object ==2
   20315          480 :            && last_eq_type == SEQ_MIXED
   20316            7 :            && last_where
   20317            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20318          486 :           || (eq_type == SEQ_MIXED
   20319            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20320            1 :         continue;
   20321              : 
   20322          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20323              :                "statement at %L with objects of different type");
   20324          483 :       if ((object ==2
   20325          479 :            && last_eq_type == SEQ_NONDEFAULT
   20326           50 :            && last_where
   20327           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20328          525 :           || (eq_type == SEQ_NONDEFAULT
   20329           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20330            4 :         continue;
   20331              : 
   20332          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20333              :                "EQUIVALENCE statement at %L");
   20334          479 :       if (last_eq_type == SEQ_CHARACTER
   20335          475 :           && eq_type != SEQ_CHARACTER
   20336          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20337            4 :                 continue;
   20338              : 
   20339          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20340              :                "EQUIVALENCE statement at %L");
   20341          473 :       if (last_eq_type == SEQ_NUMERIC
   20342          471 :           && eq_type != SEQ_NUMERIC
   20343          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20344            2 :                 continue;
   20345              : 
   20346         3146 : identical_types:
   20347              : 
   20348         3146 :       last_ts =&sym->ts;
   20349         3146 :       last_where = &e->where;
   20350              : 
   20351         3146 :       if (!e->ref)
   20352         1003 :         continue;
   20353              : 
   20354              :       /* Shall not be an automatic array.  */
   20355         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20356              :         {
   20357            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20358              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20359            3 :           continue;
   20360              :         }
   20361              : 
   20362         2140 :       r = e->ref;
   20363         4326 :       while (r)
   20364              :         {
   20365              :           /* Shall not be a structure component.  */
   20366         2187 :           if (r->type == REF_COMPONENT)
   20367              :             {
   20368            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20369              :                          "EQUIVALENCE object",
   20370            0 :                          r->u.c.component->name, &e->where);
   20371            0 :               break;
   20372              :             }
   20373              : 
   20374              :           /* A substring shall not have length zero.  */
   20375         2187 :           if (r->type == REF_SUBSTRING)
   20376              :             {
   20377          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20378              :                 {
   20379            1 :                   gfc_error ("Substring at %L has length zero",
   20380              :                              &r->u.ss.start->where);
   20381            1 :                   break;
   20382              :                 }
   20383              :             }
   20384         2186 :           r = r->next;
   20385              :         }
   20386              :     }
   20387         1565 : }
   20388              : 
   20389              : 
   20390              : /* Function called by resolve_fntype to flag other symbols used in the
   20391              :    length type parameter specification of function results.  */
   20392              : 
   20393              : static bool
   20394         4224 : flag_fn_result_spec (gfc_expr *expr,
   20395              :                      gfc_symbol *sym,
   20396              :                      int *f ATTRIBUTE_UNUSED)
   20397              : {
   20398         4224 :   gfc_namespace *ns;
   20399         4224 :   gfc_symbol *s;
   20400              : 
   20401         4224 :   if (expr->expr_type == EXPR_VARIABLE)
   20402              :     {
   20403         1378 :       s = expr->symtree->n.sym;
   20404         2159 :       for (ns = s->ns; ns; ns = ns->parent)
   20405         2159 :         if (!ns->parent)
   20406              :           break;
   20407              : 
   20408         1378 :       if (sym == s)
   20409              :         {
   20410            1 :           gfc_error ("Self reference in character length expression "
   20411              :                      "for %qs at %L", sym->name, &expr->where);
   20412            1 :           return true;
   20413              :         }
   20414              : 
   20415         1377 :       if (!s->fn_result_spec
   20416         1377 :           && s->attr.flavor == FL_PARAMETER)
   20417              :         {
   20418              :           /* Function contained in a module.... */
   20419           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20420              :             {
   20421           32 :               gfc_symtree *st;
   20422           32 :               s->fn_result_spec = 1;
   20423              :               /* Make sure that this symbol is translated as a module
   20424              :                  variable.  */
   20425           32 :               st = gfc_get_unique_symtree (ns);
   20426           32 :               st->n.sym = s;
   20427           32 :               s->refs++;
   20428           32 :             }
   20429              :           /* ... which is use associated and called.  */
   20430           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20431            0 :                         ||
   20432              :                   /* External function matched with an interface.  */
   20433            0 :                   (s->ns->proc_name
   20434            0 :                    && ((s->ns == ns
   20435            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20436            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20437            0 :                    && s->ns->proc_name->attr.function))
   20438           31 :             s->fn_result_spec = 1;
   20439              :         }
   20440              :     }
   20441              :   return false;
   20442              : }
   20443              : 
   20444              : 
   20445              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20446              : 
   20447              : static void
   20448       353613 : resolve_fntype (gfc_namespace *ns)
   20449              : {
   20450       353613 :   gfc_entry_list *el;
   20451       353613 :   gfc_symbol *sym;
   20452              : 
   20453       353613 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20454              :     return;
   20455              : 
   20456              :   /* If there are any entries, ns->proc_name is the entry master
   20457              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20458       186868 :   if (ns->entries)
   20459          596 :     sym = ns->entries->sym;
   20460              :   else
   20461              :     sym = ns->proc_name;
   20462       186868 :   if (sym->result == sym
   20463       151459 :       && sym->ts.type == BT_UNKNOWN
   20464            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20465       186872 :       && !sym->attr.untyped)
   20466              :     {
   20467            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20468              :                  sym->name, &sym->declared_at);
   20469            3 :       sym->attr.untyped = 1;
   20470              :     }
   20471              : 
   20472        13639 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20473         1862 :       && !sym->attr.contained
   20474          299 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20475       186868 :       && gfc_check_symbol_access (sym))
   20476              :     {
   20477            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20478              :                       "%L of PRIVATE type %qs", sym->name,
   20479            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20480              :     }
   20481              : 
   20482       186868 :     if (ns->entries)
   20483         1253 :     for (el = ns->entries->next; el; el = el->next)
   20484              :       {
   20485          657 :         if (el->sym->result == el->sym
   20486          445 :             && el->sym->ts.type == BT_UNKNOWN
   20487            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20488          659 :             && !el->sym->attr.untyped)
   20489              :           {
   20490            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20491              :                        el->sym->name, &el->sym->declared_at);
   20492            2 :             el->sym->attr.untyped = 1;
   20493              :           }
   20494              :       }
   20495              : 
   20496       186868 :   if (sym->ts.type == BT_CHARACTER
   20497         6955 :       && sym->ts.u.cl->length
   20498         1876 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20499         1871 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20500              : }
   20501              : 
   20502              : 
   20503              : /* 12.3.2.1.1 Defined operators.  */
   20504              : 
   20505              : static bool
   20506          508 : check_uop_procedure (gfc_symbol *sym, locus where)
   20507              : {
   20508          508 :   gfc_formal_arglist *formal;
   20509              : 
   20510          508 :   if (!sym->attr.function)
   20511              :     {
   20512            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20513              :                  sym->name, &where);
   20514            4 :       return false;
   20515              :     }
   20516              : 
   20517          504 :   if (sym->ts.type == BT_CHARACTER
   20518           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20519            2 :       && !(sym->result && ((sym->result->ts.u.cl
   20520            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   20521              :     {
   20522            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   20523              :                  "character length", sym->name, &where);
   20524            2 :       return false;
   20525              :     }
   20526              : 
   20527          502 :   formal = gfc_sym_get_dummy_args (sym);
   20528          502 :   if (!formal || !formal->sym)
   20529              :     {
   20530            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   20531              :                  "one argument", sym->name, &where);
   20532            1 :       return false;
   20533              :     }
   20534              : 
   20535          501 :   if (formal->sym->attr.intent != INTENT_IN)
   20536              :     {
   20537            0 :       gfc_error ("First argument of operator interface at %L must be "
   20538              :                  "INTENT(IN)", &where);
   20539            0 :       return false;
   20540              :     }
   20541              : 
   20542          501 :   if (formal->sym->attr.optional)
   20543              :     {
   20544            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   20545              :                  "optional", &where);
   20546            0 :       return false;
   20547              :     }
   20548              : 
   20549          501 :   formal = formal->next;
   20550          501 :   if (!formal || !formal->sym)
   20551              :     return true;
   20552              : 
   20553          297 :   if (formal->sym->attr.intent != INTENT_IN)
   20554              :     {
   20555            0 :       gfc_error ("Second argument of operator interface at %L must be "
   20556              :                  "INTENT(IN)", &where);
   20557            0 :       return false;
   20558              :     }
   20559              : 
   20560          297 :   if (formal->sym->attr.optional)
   20561              :     {
   20562            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   20563              :                  "optional", &where);
   20564            1 :       return false;
   20565              :     }
   20566              : 
   20567          296 :   if (formal->next)
   20568              :     {
   20569            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   20570              :                  "arguments", &where);
   20571            2 :       return false;
   20572              :     }
   20573              : 
   20574              :   return true;
   20575              : }
   20576              : 
   20577              : static void
   20578       354419 : gfc_resolve_uops (gfc_symtree *symtree)
   20579              : {
   20580       354419 :   gfc_interface *itr;
   20581              : 
   20582       354419 :   if (symtree == NULL)
   20583              :     return;
   20584              : 
   20585          403 :   gfc_resolve_uops (symtree->left);
   20586          403 :   gfc_resolve_uops (symtree->right);
   20587              : 
   20588          798 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   20589          395 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   20590              : }
   20591              : 
   20592              : /* Mark all lhs in assignment statement as used.  It is better to put this into
   20593              :    its own function rather than into the different switch cases in
   20594              :    gfc_resolve_code.  */
   20595              : 
   20596              : static void
   20597       689151 : mark_lhs_assignments_set (gfc_code *code)
   20598              : {
   20599              : 
   20600      1832514 :   for (; code; code = code->next)
   20601              :     {
   20602      1143363 :       gfc_expr *lvalue = code->expr1, *rvalue = code->expr2;
   20603              : 
   20604      1143363 :       if (lvalue == NULL || lvalue->symtree == NULL || rvalue == NULL)
   20605       844416 :         continue;
   20606              : 
   20607       298947 :       switch (code->op)
   20608              :         {
   20609       287339 :         case EXEC_ASSIGN:
   20610       287339 :           if (gfc_is_reallocatable_lhs (lvalue) && lvalue->rank == rvalue->rank)
   20611         8411 :             gfc_lvalue_allocated_at (lvalue->symtree->n.sym, &lvalue->where);
   20612              : 
   20613       297476 :           gcc_fallthrough();
   20614       297476 :         case EXEC_POINTER_ASSIGN:
   20615       297476 :           gfc_expr_set_at (lvalue, &rvalue->where, VALUE_VARDEF);
   20616              :         default:
   20617              :           break;
   20618              :         }
   20619              :     }
   20620       689151 : }
   20621              : 
   20622              : /* Examine all of the expressions associated with a program unit,
   20623              :    assign types to all intermediate expressions, make sure that all
   20624              :    assignments are to compatible types and figure out which names
   20625              :    refer to which functions or subroutines.  It doesn't check code
   20626              :    block, which is handled by gfc_resolve_code.  */
   20627              : 
   20628              : static void
   20629       356162 : resolve_types (gfc_namespace *ns)
   20630              : {
   20631       356162 :   gfc_namespace *n;
   20632       356162 :   gfc_charlen *cl;
   20633       356162 :   gfc_data *d;
   20634       356162 :   gfc_equiv *eq;
   20635       356162 :   gfc_namespace* old_ns = gfc_current_ns;
   20636       356162 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   20637              : 
   20638       356162 :   if (ns->types_resolved)
   20639              :     return;
   20640              : 
   20641              :   /* Check that all IMPLICIT types are ok.  */
   20642       353614 :   if (!ns->seen_implicit_none)
   20643              :     {
   20644              :       unsigned letter;
   20645      8901874 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   20646      8572175 :         if (ns->set_flag[letter]
   20647      8572175 :             && !resolve_typespec_used (&ns->default_type[letter],
   20648              :                                        &ns->implicit_loc[letter], NULL))
   20649              :           return;
   20650              :     }
   20651              : 
   20652       353613 :   gfc_current_ns = ns;
   20653              : 
   20654       353613 :   resolve_entries (ns);
   20655              : 
   20656       353613 :   resolve_common_vars (&ns->blank_common, false);
   20657       353613 :   resolve_common_blocks (ns->common_root);
   20658              : 
   20659       353613 :   resolve_contained_functions (ns);
   20660              : 
   20661       353613 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   20662       302885 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20663       200137 :     gfc_resolve_formal_arglist (ns->proc_name);
   20664              : 
   20665       353613 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   20666              : 
   20667       449400 :   for (cl = ns->cl_list; cl; cl = cl->next)
   20668        95787 :     resolve_charlen (cl);
   20669              : 
   20670       353613 :   gfc_traverse_ns (ns, resolve_symbol);
   20671              : 
   20672       353613 :   resolve_fntype (ns);
   20673              : 
   20674       402399 :   for (n = ns->contained; n; n = n->sibling)
   20675              :     {
   20676              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   20677        48786 :       if (gfc_pure (ns->proc_name)
   20678            5 :           && !gfc_pure (n->proc_name)
   20679        48786 :           && !n->proc_name->attr.artificial)
   20680            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   20681              :                    "also be PURE", n->proc_name->name,
   20682              :                    &n->proc_name->declared_at);
   20683              : 
   20684        48786 :       resolve_types (n);
   20685              :     }
   20686              : 
   20687       353613 :   forall_flag = 0;
   20688       353613 :   gfc_do_concurrent_flag = 0;
   20689       353613 :   gfc_check_interfaces (ns);
   20690              : 
   20691       353613 :   gfc_traverse_ns (ns, resolve_values);
   20692              : 
   20693       353613 :   if (ns->save_all || (!flag_automatic && !recursive))
   20694          315 :     gfc_save_all (ns);
   20695              : 
   20696       353613 :   iter_stack = NULL;
   20697       356131 :   for (d = ns->data; d; d = d->next)
   20698         2518 :     resolve_data (d);
   20699              : 
   20700       353613 :   iter_stack = NULL;
   20701       353613 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   20702              : 
   20703       353613 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   20704              : 
   20705       355178 :   for (eq = ns->equiv; eq; eq = eq->next)
   20706         1565 :     resolve_equivalence (eq);
   20707              : 
   20708              :   /* Warn about unused labels.  */
   20709       353613 :   if (warn_unused_label)
   20710         4813 :     warn_unused_fortran_label (ns->st_labels);
   20711              : 
   20712       353613 :   gfc_resolve_uops (ns->uop_root);
   20713              : 
   20714       353613 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   20715              : 
   20716       353613 :   gfc_resolve_omp_declare (ns);
   20717              : 
   20718       353613 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   20719              : 
   20720       353613 :   gfc_resolve_omp_udms (ns->omp_udm_root);
   20721              : 
   20722       353613 :   ns->types_resolved = 1;
   20723              : 
   20724       353613 :   gfc_current_ns = old_ns;
   20725              : }
   20726              : 
   20727              : 
   20728              : /* Call gfc_resolve_code recursively.  */
   20729              : 
   20730              : static void
   20731       356218 : resolve_codes (gfc_namespace *ns)
   20732              : {
   20733       356218 :   gfc_namespace *n;
   20734       356218 :   bitmap_obstack old_obstack;
   20735              : 
   20736       356218 :   if (ns->resolved == 1)
   20737        14377 :     return;
   20738              : 
   20739       390683 :   for (n = ns->contained; n; n = n->sibling)
   20740        48842 :     resolve_codes (n);
   20741              : 
   20742       341841 :   gfc_current_ns = ns;
   20743              : 
   20744              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   20745       341841 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   20746       329517 :     cs_base = NULL;
   20747              : 
   20748              :   /* Set to an out of range value.  */
   20749       341841 :   current_entry_id = -1;
   20750              : 
   20751       341841 :   old_obstack = labels_obstack;
   20752       341841 :   bitmap_obstack_initialize (&labels_obstack);
   20753              : 
   20754       341841 :   gfc_resolve_oacc_declare (ns);
   20755       341841 :   gfc_resolve_oacc_routines (ns);
   20756       341841 :   gfc_resolve_omp_local_vars (ns);
   20757       341841 :   if (ns->omp_allocate)
   20758           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   20759       341841 :   gfc_resolve_code (ns->code, ns);
   20760              : 
   20761       341840 :   bitmap_obstack_release (&labels_obstack);
   20762       341840 :   labels_obstack = old_obstack;
   20763              : }
   20764              : 
   20765              : /* Return true if the value of a variable can be considered used, either
   20766              :    through the value_used flag or because it is a suitable dummy argument.  */
   20767              : 
   20768              : static bool
   20769          455 : var_value_is_used (gfc_symbol *sym)
   20770              : {
   20771          455 :   if (sym->attr.value_used != VALUE_UNUSED)
   20772              :     return true;
   20773              : 
   20774          104 :   if (!sym->attr.dummy)
   20775              :     return false;
   20776              : 
   20777           88 :   if (sym->attr.value)
   20778              :     return false;
   20779              : 
   20780           88 :   switch (sym->attr.intent)
   20781              :     {
   20782              :     case INTENT_UNKNOWN:
   20783              :     case INTENT_INOUT:
   20784              :     case INTENT_OUT:
   20785              :       return true;
   20786              : 
   20787              :     case INTENT_IN:
   20788              :     default:
   20789              :       return false;
   20790              :     }
   20791              : }
   20792              : 
   20793              : /* Similar, see if the variable could have gotten its value from somewhere.  */
   20794              : 
   20795              : static bool
   20796         2391 : var_value_is_set (gfc_symbol *sym)
   20797              : {
   20798         2391 :   if (sym->attr.value_set != VALUE_UNSET)
   20799              :     return true;
   20800              : 
   20801         1691 :   if (sym->value)
   20802              :     return true;
   20803              : 
   20804         1676 :   if (sym->ts.type == BT_DERIVED
   20805         1676 :       && gfc_has_default_initializer (sym->ts.u.derived))
   20806              :     return true;
   20807              : 
   20808         1676 :   if (!sym->attr.dummy)
   20809              :     return false;
   20810              : 
   20811         1630 :   if (sym->attr.value)
   20812              :     return true;
   20813              : 
   20814         1589 :   if (sym->attr.intent == INTENT_OUT)
   20815              :     return false;
   20816              : 
   20817              :   return true;
   20818              : }
   20819              : 
   20820              : /* Callback function to catch set but never used variables.  */
   20821              : 
   20822              : static void
   20823        34231 : find_unused_vs_set (gfc_symbol *sym)
   20824              : {
   20825        34231 :   symbol_attribute *attr = &sym->attr;
   20826              : 
   20827        34231 :   if (attr->flavor != FL_VARIABLE)
   20828              :     return;
   20829              : 
   20830              :   /* Do not warn about anything too far out of the ordinary.  This might be
   20831              :      tightened later.  */
   20832         8587 :   if (attr->in_common || attr->in_equivalence || attr->artificial
   20833         8181 :       || attr->cray_pointer || attr->cray_pointee || attr->associate_var
   20834         8180 :       || attr->target || attr->fe_temp || attr->omp_declare_target
   20835         8177 :       || attr->omp_declare_target_link || attr->omp_declare_target_local
   20836         8168 :       || attr->omp_declare_target_indirect || attr->oacc_declare_create
   20837         8168 :       || attr->oacc_declare_copyin || attr->oacc_declare_deviceptr
   20838         8168 :       || attr->oacc_declare_device_resident || attr->oacc_declare_link
   20839         8168 :       || attr->result || attr->warning_emitted || attr->use_assoc
   20840         5628 :       || attr->volatile_ || attr->asynchronous || !attr->referenced)
   20841              :     return;
   20842              : 
   20843           39 :   if (warn_unused_intent_out && attr->value_set == VALUE_INTENT_OUT
   20844         2439 :       && !var_value_is_used (sym))
   20845              :     {
   20846            1 :       gfc_warning (OPT_Wunused_intent_out, "Variable %qs passed to "
   20847              :                    "INTENT(OUT) argument at %L but value never used",
   20848              :                    sym->name, &sym->other_loc);
   20849            1 :       attr->warning_emitted = 1;
   20850            1 :       return;
   20851              :     }
   20852              : 
   20853         2433 :   if (warn_unused_read && attr->value_set == VALUE_READ && !var_value_is_used (sym))
   20854              :     {
   20855            1 :       gfc_warning (OPT_Wunused_read, "Variable %qs read at %L but never "
   20856              :                    "used", sym->name, &sym->other_loc);
   20857            1 :       attr->warning_emitted = 1;
   20858            1 :       return;
   20859              :     }
   20860              : 
   20861              :   /* There is no allocation in sight, but the variable is used anyway.  This
   20862              :      might be hidden behind PRESENT, but issue a warning nonetheless.  If
   20863              :      people complain, we might want to make this to an extra option to be
   20864              :      included with -Wextra.  */
   20865              : 
   20866         2391 :   if (warn_undefined_vars && attr->allocatable && !attr->allocated
   20867         2439 :       && var_value_is_used (sym))
   20868              :     {
   20869            1 :       if (attr->dummy && attr->intent == INTENT_OUT)
   20870              :         {
   20871            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated INTENT(OUT) variable "
   20872              :                        "%qs referenced at %L", sym->name, &sym->other_loc);
   20873            0 :           attr->warning_emitted = 1;
   20874            0 :           return;
   20875              :         }
   20876              : 
   20877            1 :       if (!attr->dummy)
   20878              :         {
   20879            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated variable %qs "
   20880              :                        "referenced at %L", sym->name, &sym->other_loc);
   20881            0 :           attr->warning_emitted = 1;
   20882            0 :           return;
   20883              :         }
   20884              :     }
   20885              : 
   20886         2432 :   if (warn_undefined_vars && !var_value_is_set (sym))
   20887              :     {
   20888              :       /* Warn about variables which have been allocated and used, but never
   20889              :          set.  */
   20890           47 :       if (attr->allocated && sym->attr.value_used > VALUE_MAYBE_USED)
   20891              :         {
   20892            3 :           switch (sym->attr.value_used)
   20893              :             {
   20894            1 :             case VALUE_INTENT_IN:
   20895            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20896              :                            "undefined to INTENT(IN) argument at %L", sym->name,
   20897              :                            &sym->other_loc);
   20898            1 :               break;
   20899              : 
   20900            1 :             case VALUE_VALUE_ARG:
   20901            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20902              :                            "undefined to VALUE argument at %L", sym->name,
   20903              :                            &sym->other_loc);
   20904            1 :               break;
   20905            1 :             case VALUE_USED:
   20906            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated undefined variable "
   20907              :                            "%qs used at %L", sym->name, &sym->other_loc);
   20908            1 :               break;
   20909            0 :             default:
   20910            0 :               gfc_internal_error ("Wrong value_set");
   20911            3 :               break;
   20912              :             }
   20913            3 :           attr->warning_emitted = 1;
   20914            3 :           return;
   20915              :         }
   20916              : 
   20917              :       /* Similar, when undefined variables are passed to INTENT(IN), VALUE
   20918              :          arguments or are used in general.  */
   20919              : 
   20920           44 :       if (attr->value_used == VALUE_INTENT_IN)
   20921              :         {
   20922            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20923              :                        "to INTENT(IN) argument at %L", sym->name, &sym->other_loc);
   20924            1 :           attr->warning_emitted = 1;
   20925            1 :           return;
   20926              :         }
   20927           43 :       else if (attr->value_used == VALUE_VALUE_ARG)
   20928              :         {
   20929            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20930              :                        "to VALUE argument at %L", sym->name, &sym->other_loc);
   20931            1 :           attr->warning_emitted = 1;
   20932            1 :           return;
   20933              :         }
   20934           42 :       else if (attr->value_used == VALUE_USED)
   20935              :         {
   20936            7 :           if (attr->dummy && attr->intent == INTENT_OUT)
   20937            1 :             gfc_warning (OPT_Wundefined_vars, "Undefined INTENT(OUT) variable %qs "
   20938              :                          "used at %L", sym->name, &sym->other_loc);
   20939              :           else
   20940            6 :             gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs used at "
   20941              :                          "%L", sym->name, &sym->other_loc);
   20942              : 
   20943            7 :           attr->warning_emitted = 1;
   20944            7 :           return;
   20945              :         }
   20946              : 
   20947              :       /* PR 28004 - warn about INTENT(OUT) variables that are never set.  If
   20948              :          the variable or a component are allocatable, do not warn since this is
   20949              :          a frequent shortcut for deallocation.  */
   20950              : 
   20951           35 :       if (sym->attr.dummy && sym->attr.intent == INTENT_OUT
   20952            0 :           && !(attr->allocatable || attr->alloc_comp))
   20953              :         {
   20954            0 :           gfc_warning (OPT_Wundefined_vars, "INTENT(OUT) variable %qs  "
   20955              :                        "declared at %L is not assigned a value", sym->name,
   20956              :                        &sym->declared_at);
   20957            0 :           attr->warning_emitted = 1;
   20958            0 :           return;
   20959              :         }
   20960              :     }
   20961              : 
   20962              :   /* Warn for unused but defined variables.  */
   20963              : 
   20964         2420 :   if (warn_unused_but_set_variable)
   20965              :     {
   20966         2316 :       if (attr->value_set == VALUE_VARDEF && !var_value_is_used (sym))
   20967              :         {
   20968            7 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs defined at "
   20969              :                        "%L but never used", sym->name, &sym->other_loc);
   20970            7 :           attr->warning_emitted = 1;
   20971            7 :           return;
   20972              :         }
   20973         2309 :       if (attr->allocatable && attr->allocated && !var_value_is_used (sym))
   20974              :         {
   20975            1 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs "
   20976              :                        "allocated at %L but never used", sym->name,
   20977              :                        &sym->extra_loc);
   20978            1 :           attr->warning_emitted = 1;
   20979            1 :           return;
   20980              :         }
   20981              :     }
   20982              : }
   20983              : 
   20984              : /* Run warn_unused_vs_set over a namespace recursively.  */
   20985              : 
   20986              : static void
   20987         4827 : warn_unused_vs_set (gfc_namespace *ns)
   20988              : {
   20989         4827 :   gfc_traverse_ns (ns, find_unused_vs_set);
   20990              : 
   20991         5343 :   for (gfc_namespace *n = ns->contained; n; n = n->sibling)
   20992          516 :     warn_unused_vs_set (n);
   20993         4827 : }
   20994              : 
   20995              : /* This function is called after a complete program unit has been compiled.
   20996              :    Its purpose is to examine all of the expressions associated with a program
   20997              :    unit, assign types to all intermediate expressions, make sure that all
   20998              :    assignments are to compatible types and figure out which names refer to
   20999              :    which functions or subroutines.  */
   21000              : 
   21001              : void
   21002       312228 : gfc_resolve (gfc_namespace *ns)
   21003              : {
   21004       312228 :   gfc_namespace *old_ns;
   21005       312228 :   code_stack *old_cs_base;
   21006       312228 :   struct gfc_omp_saved_state old_omp_state;
   21007              : 
   21008       312228 :   if (ns->resolved)
   21009         4852 :     return;
   21010              : 
   21011       307376 :   ns->resolved = -1;
   21012       307376 :   old_ns = gfc_current_ns;
   21013       307376 :   old_cs_base = cs_base;
   21014              : 
   21015              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   21016              :      body, we should clear any state associated to it, so that say NS's
   21017              :      DO loops are not interpreted as OpenMP loops.  */
   21018       307376 :   if (!ns->construct_entities)
   21019       295052 :     gfc_omp_save_and_clear_state (&old_omp_state);
   21020              : 
   21021       307376 :   resolve_types (ns);
   21022       307376 :   component_assignment_level = 0;
   21023       307376 :   resolve_codes (ns);
   21024              : 
   21025       307375 :   if (warn_unused_but_set_variable || warn_unused_intent_out
   21026       303114 :       || warn_unused_read || warn_undefined_vars)
   21027              :     {
   21028         4335 :       int error_count;
   21029         4335 :       gfc_get_errors (NULL, &error_count);
   21030         4335 :       if (error_count == 0)
   21031         4311 :         warn_unused_vs_set (ns);
   21032              :     }
   21033              : 
   21034       307375 :   if (ns->omp_assumes)
   21035           13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   21036              : 
   21037       307375 :   gfc_current_ns = old_ns;
   21038       307375 :   cs_base = old_cs_base;
   21039       307375 :   ns->resolved = 1;
   21040              : 
   21041       307375 :   gfc_run_passes (ns);
   21042              : 
   21043       307375 :   if (!ns->construct_entities)
   21044       295051 :     gfc_omp_restore_state (&old_omp_state);
   21045              : }
        

Generated by: LCOV version 2.4-beta

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