LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.6 % 9943 9304
Test Date: 2026-08-22 16:33:35 Functions: 99.6 % 255 254
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        54506 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        59711 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         5463 :       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      1592404 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1592404 :   if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
     138              :     {
     139            5 :       if (where)
     140              :         {
     141            5 :           if (name)
     142            4 :             gfc_error ("%qs at %L is of the ABSTRACT type %qs",
     143              :                        name, where, ts->u.derived->name);
     144              :           else
     145            1 :             gfc_error ("ABSTRACT type %qs used at %L",
     146              :                        ts->u.derived->name, where);
     147              :         }
     148              : 
     149              :       return false;
     150              :     }
     151              : 
     152              :   return true;
     153              : }
     154              : 
     155              : 
     156              : static bool
     157         5669 : check_proc_interface (gfc_symbol *ifc, locus *where)
     158              : {
     159              :   /* Several checks for F08:C1216.  */
     160         5669 :   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         5667 :   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         5663 :   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         5659 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     187         5659 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     188           17 :     ifc->attr.intrinsic = 1;
     189         5659 :   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         5656 :   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         2141 : resolve_procedure_interface (gfc_symbol *sym)
     211              : {
     212         2141 :   gfc_symbol *ifc = sym->ts.interface;
     213              : 
     214         2141 :   if (!ifc)
     215              :     return true;
     216              : 
     217         1981 :   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         1979 :   if (!check_proc_interface (ifc, &sym->declared_at))
     224              :     return false;
     225              : 
     226         1970 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     227              :     {
     228              :       /* Resolve interface and copy attributes.  */
     229         1691 :       resolve_symbol (ifc);
     230         1691 :       if (ifc->attr.intrinsic)
     231           14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     232              : 
     233         1691 :       if (ifc->result)
     234              :         {
     235          780 :           sym->ts = ifc->result->ts;
     236          780 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     237          780 :           sym->attr.pointer = ifc->result->attr.pointer;
     238          780 :           sym->attr.dimension = ifc->result->attr.dimension;
     239          780 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     240          780 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     241          780 :           sym->result = sym;
     242              :         }
     243              :       else
     244              :         {
     245          911 :           sym->ts = ifc->ts;
     246          911 :           sym->attr.allocatable = ifc->attr.allocatable;
     247          911 :           sym->attr.pointer = ifc->attr.pointer;
     248          911 :           sym->attr.dimension = ifc->attr.dimension;
     249          911 :           sym->attr.class_ok = ifc->attr.class_ok;
     250          911 :           sym->as = gfc_copy_array_spec (ifc->as);
     251              :         }
     252         1691 :       sym->ts.interface = ifc;
     253         1691 :       sym->attr.function = ifc->attr.function;
     254         1691 :       sym->attr.subroutine = ifc->attr.subroutine;
     255              : 
     256         1691 :       sym->attr.pure = ifc->attr.pure;
     257         1691 :       sym->attr.elemental = ifc->attr.elemental;
     258         1691 :       sym->attr.contiguous = ifc->attr.contiguous;
     259         1691 :       sym->attr.recursive = ifc->attr.recursive;
     260         1691 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     261         1691 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     262         1691 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     263              :       /* Copy char length.  */
     264         1691 :       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       552311 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       552311 :   gfc_formal_arglist *f;
     290       552311 :   gfc_symbol *sym;
     291       552311 :   bool saved_specification_expr;
     292       552311 :   int i;
     293              : 
     294       552311 :   if (proc->result != NULL)
     295       344464 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       552311 :   if (gfc_elemental (proc)
     300       389630 :       || sym->attr.pointer || sym->attr.allocatable
     301       929589 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       177363 :       proc->attr.always_explicit = 1;
     304       177363 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       552311 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       552311 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1430308 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       877999 :       gfc_array_spec *as;
     313       877999 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       877999 :       sym = f->sym;
     316              : 
     317       877999 :       if (sym == NULL)
     318              :         {
     319              :           /* Alternate return placeholder.  */
     320          171 :           if (gfc_elemental (proc))
     321            1 :             gfc_error ("Alternate return specifier in elemental subroutine "
     322              :                        "%qs at %L is not allowed", proc->name,
     323              :                        &proc->declared_at);
     324          171 :           if (proc->attr.function)
     325            1 :             gfc_error ("Alternate return specifier in function "
     326              :                        "%qs at %L is not allowed", proc->name,
     327              :                        &proc->declared_at);
     328          171 :           continue;
     329              :         }
     330              : 
     331          611 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     332       878439 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       877828 :       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       877826 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          903 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       877826 :       if (sym->attr.subroutine || sym->attr.external)
     347              :         {
     348          913 :           if (sym->attr.flavor == FL_UNKNOWN)
     349            9 :             gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
     350              :         }
     351              :       else
     352              :         {
     353       876913 :           if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
     354         3690 :               && (!sym->attr.function || sym->result == sym))
     355         3652 :             gfc_set_default_type (sym, 1, sym->ns);
     356              :         }
     357              : 
     358       877826 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       892171 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       877826 :       saved_specification_expr = specification_expr;
     362       877826 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       877826 :       specification_expr = true;
     364       877826 :       specification_expr_symbol = sym;
     365       877826 :       gfc_resolve_array_spec (as, 0);
     366       877826 :       specification_expr = saved_specification_expr;
     367       877826 :       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       877826 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        12589 :           && ((sym->ts.type != BT_CLASS
     374        11415 :                && !(sym->attr.pointer || sym->attr.allocatable))
     375         5443 :               || (sym->ts.type == BT_CLASS
     376         1174 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     377          974 :                        || CLASS_DATA (sym)->attr.allocatable)))
     378         7681 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         7680 :           as->type = AS_ASSUMED_SHAPE;
     381        17825 :           for (i = 0; i < as->rank; i++)
     382        10145 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       138609 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       124478 :           || (as && as->type == AS_ASSUMED_RANK)
     387       823883 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       813647 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        11968 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11485 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10545 :                   || CLASS_DATA (sym)->attr.target))
     392       812224 :           || sym->attr.optional)
     393              :         {
     394        80974 :           proc->attr.always_explicit = 1;
     395        80974 :           if (proc->result)
     396        36743 :             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       877826 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        52130 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       877826 :       if (gfc_pure (proc))
     406              :         {
     407       328195 :           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       328166 :           else if (!sym->attr.pointer)
     418              :             {
     419       328152 :               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       328152 :               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       328194 :           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       877824 :       if (proc->attr.implicit_pure)
     458              :         {
     459        25607 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          337 :               if (!gfc_pure (sym))
     462          305 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        25270 :           else if (!sym->attr.pointer)
     465              :             {
     466        24480 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2747 :                   && !sym->value)
     468         2747 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        24480 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4250 :                   && !sym->value)
     472         4250 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       877824 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       302558 :           if (sym->attr.codimension
     480       302557 :               || (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       302555 :           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       302553 :           if (sym->attr.allocatable
     497       302552 :               || (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       302551 :           if (sym->attr.pointer
     507       302550 :               || (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       302549 :           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       302547 :           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       877811 :       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       552311 :   if (sym)
     562       552219 :     sym->formal_resolved = 1;
     563       552311 :   gfc_current_ns = orig_current_ns;
     564       552311 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1915436 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1915436 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       347395 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       345028 :   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       361488 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       361488 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        37883 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        37883 :   bool t;
     598              : 
     599        37883 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        37883 :       && sym->ns->parent
     601         1452 :       && sym->ns->parent->proc_name
     602         1452 :       && 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        37883 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        11060 :       || sym->attr.entry_master)
     611              :     return;
     612              : 
     613        10871 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        10871 :   if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
     618              :     {
     619           58 :       t = gfc_set_default_type (sym->result, 0, ns);
     620              : 
     621           58 :       if (!t && !sym->result->attr.untyped)
     622              :         {
     623           19 :           if (sym->result == sym)
     624            1 :             gfc_error ("Contained function %qs at %L has no IMPLICIT type",
     625              :                        sym->name, &sym->declared_at);
     626           18 :           else if (!sym->result->attr.proc_pointer)
     627            0 :             gfc_error ("Result %qs of contained function %qs at %L has "
     628              :                        "no IMPLICIT type", sym->result->name, sym->name,
     629              :                        &sym->result->declared_at);
     630           19 :           sym->result->attr.untyped = 1;
     631              :         }
     632              :     }
     633              : 
     634              :   /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
     635              :      type, lists the only ways a character length value of * can be used:
     636              :      dummy arguments of procedures, named constants, function results and
     637              :      in allocate statements if the allocate_object is an assumed length dummy
     638              :      in external functions.  Internal function results and results of module
     639              :      procedures are not on this list, ergo, not permitted.  */
     640              : 
     641        10871 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1211 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1211 :       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       398864 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       398864 :   gfc_namespace *old_ns;
     729       398864 :   gfc_code *c;
     730       398864 :   gfc_symbol *proc;
     731       398864 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       398864 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       398864 :   static int master_count = 0;
     735              : 
     736       398864 :   if (ns->proc_name == NULL)
     737       398161 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       398815 :   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       363465 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       363465 :   gfc_symbol *csym = common_block->head;
     998       363465 :   gfc_gsymbol *gsym;
     999              : 
    1000       369517 :   for (; csym; csym = csym->common_next)
    1001              :     {
    1002         6052 :       gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
    1003         6052 :       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         6052 :       if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
    1018              :         {
    1019         5779 :           gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
    1020         5779 :           gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
    1021              :                           &common_block->where);
    1022              :         }
    1023              : 
    1024         6052 :       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         6052 :       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         6052 :       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         6052 :       if (csym->ts.type != BT_DERIVED)
    1051         6005 :         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       363465 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       362018 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       362018 :   gfc_symbol *sym = NULL;
    1077       362018 :   gfc_gsymbol * gsym;
    1078              : 
    1079       362018 :   if (common_root == NULL)
    1080       361896 :     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       361488 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       361488 :   gfc_namespace *child;
    1208       361488 :   gfc_entry_list *el;
    1209              : 
    1210       361488 :   resolve_formal_arglists (ns);
    1211              : 
    1212       398864 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        37376 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        37376 :       resolve_contained_fntype (child->proc_name, child);
    1219        37883 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       361488 : }
    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        64291 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        64291 :   gfc_constructor *cons;
    1321        64291 :   gfc_component *comp;
    1322        64291 :   bool t;
    1323        64291 :   symbol_attribute a;
    1324              : 
    1325        64291 :   t = true;
    1326              : 
    1327        64291 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        61263 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        61113 :         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        61263 :       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        64291 :   if (expr->ref)
    1358          160 :     comp = expr->ref->u.c.sym->components;
    1359        64131 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        64129 :            && expr->ts.u.derived)
    1362        64129 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        64289 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       279213 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       150637 :       int rank;
    1371              : 
    1372       150637 :       if (!cons->expr)
    1373        10065 :         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       140572 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       140557 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       140557 :       rank = comp->as ? comp->as->rank : 0;
    1388       140557 :       if (comp->ts.type == BT_CLASS
    1389         1861 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1390         1860 :           && CLASS_DATA (comp)->as)
    1391          561 :         rank = CLASS_DATA (comp)->as->rank;
    1392              : 
    1393       140557 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          234 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       140557 :       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       245539 :       if (!comp->attr.proc_pointer &&
    1409       104982 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        12866 :           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         9441 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3425 :           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         3423 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3360 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3360 :               if (t)
    1431       140557 :                 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       140557 :       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       140557 :       if (cons->expr->expr_type == EXPR_NULL
    1494        42435 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        21102 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1496         1196 :                || (comp->ts.type == BT_CLASS
    1497         1194 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1498          977 :                        || 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       140557 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        15996 :           gfc_symbol *s2 = NULL;
    1511        15996 :           gfc_component *c2;
    1512        15996 :           const char *name;
    1513        15996 :           char err[200];
    1514              : 
    1515        15996 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        15996 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        15984 :           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        15984 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        15564 :               s2 = cons->expr->symtree->n.sym;
    1529        15564 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        15576 :           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       140555 :       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         3930 :           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       140555 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        22711 :           || cons->expr->expr_type == EXPR_NULL)
    1581       130096 :         continue;
    1582              : 
    1583        10459 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        10459 :       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        10459 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596        10391 :           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        10391 :           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        10459 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        20917 :           && 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        10459 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        10459 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        10422 :                         || 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        10459 :       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       750386 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       750386 :   symbol_attribute a;
    1649              : 
    1650       750386 :   a = sym->attr;
    1651              : 
    1652       750386 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       635720 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       626858 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       626856 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       626802 :       || a.asynchronous || a.codimension
    1659       626802 :       || (a.subroutine && a.proc != PROC_UNKNOWN) || a.result)
    1660        66747 :     return 1;
    1661              : 
    1662              :   return 0;
    1663              : }
    1664              : 
    1665              : 
    1666              : /* Determine if a symbol is generic or not.  */
    1667              : 
    1668              : static int
    1669       417193 : generic_sym (gfc_symbol *sym)
    1670              : {
    1671       417193 :   gfc_symbol *s;
    1672              : 
    1673       417193 :   if (sym->attr.generic ||
    1674       387306 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1675              :     return 1;
    1676              : 
    1677       386192 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1678              :     return 0;
    1679              : 
    1680        79108 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1681              : 
    1682        79108 :   if (s != NULL)
    1683              :     {
    1684          163 :       if (s == sym)
    1685              :         return 0;
    1686              :       else
    1687          162 :         return generic_sym (s);
    1688              :     }
    1689              : 
    1690              :   return 0;
    1691              : }
    1692              : 
    1693              : 
    1694              : /* Determine if a symbol is specific or not.  */
    1695              : 
    1696              : static int
    1697       386104 : specific_sym (gfc_symbol *sym)
    1698              : {
    1699       386104 :   gfc_symbol *s;
    1700              : 
    1701       386104 :   if (sym->attr.if_source == IFSRC_IFBODY
    1702       374673 :       || sym->attr.proc == PROC_MODULE
    1703       346134 :       || sym->attr.proc == PROC_INTERNAL
    1704       297723 :       || sym->attr.proc == PROC_ST_FUNCTION
    1705       297433 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1706       682806 :       || sym->attr.external)
    1707              :     return 1;
    1708              : 
    1709       294293 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1710              :     return 0;
    1711              : 
    1712        79006 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1713              : 
    1714        79006 :   return (s == NULL) ? 0 : specific_sym (s);
    1715              : }
    1716              : 
    1717              : 
    1718              : /* Figure out if the procedure is specific, generic or unknown.  */
    1719              : 
    1720              : enum proc_type
    1721              : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
    1722              : 
    1723              : static proc_type
    1724       416882 : procedure_kind (gfc_symbol *sym)
    1725              : {
    1726       416882 :   if (generic_sym (sym))
    1727              :     return PTYPE_GENERIC;
    1728              : 
    1729       386027 :   if (specific_sym (sym))
    1730        91811 :     return PTYPE_SPECIFIC;
    1731              : 
    1732              :   return PTYPE_UNKNOWN;
    1733              : }
    1734              : 
    1735              : /* Check references to assumed size arrays.  The flag need_full_assumed_size
    1736              :    is nonzero when matching actual arguments.  */
    1737              : 
    1738              : static int need_full_assumed_size = 0;
    1739              : 
    1740              : static bool
    1741      1438719 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1742              : {
    1743      1438719 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1744              :       return false;
    1745              : 
    1746              :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1747              :      What should it be?  */
    1748         3812 :   if (e->ref
    1749         3810 :       && e->ref->u.ar.as
    1750         3809 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1751         3302 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1752         3302 :       && (e->ref->u.ar.type == AR_FULL))
    1753              :     {
    1754           25 :       gfc_error ("The upper bound in the last dimension must "
    1755              :                  "appear in the reference to the assumed size "
    1756              :                  "array %qs at %L", sym->name, &e->where);
    1757           25 :       return true;
    1758              :     }
    1759              :   return false;
    1760              : }
    1761              : 
    1762              : 
    1763              : /* Look for bad assumed size array references in argument expressions
    1764              :   of elemental and array valued intrinsic procedures.  Since this is
    1765              :   called from procedure resolution functions, it only recurses at
    1766              :   operators.  */
    1767              : 
    1768              : static bool
    1769       231786 : resolve_assumed_size_actual (gfc_expr *e)
    1770              : {
    1771       231786 :   if (e == NULL)
    1772              :    return false;
    1773              : 
    1774       231219 :   switch (e->expr_type)
    1775              :     {
    1776       111378 :     case EXPR_VARIABLE:
    1777       111378 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1778              :         return true;
    1779              :       break;
    1780              : 
    1781        49172 :     case EXPR_OP:
    1782        49172 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1783        49172 :           || resolve_assumed_size_actual (e->value.op.op2))
    1784            0 :         return true;
    1785              :       break;
    1786              : 
    1787              :     default:
    1788              :       break;
    1789              :     }
    1790              :   return false;
    1791              : }
    1792              : 
    1793              : 
    1794              : /* Check a generic procedure, passed as an actual argument, to see if
    1795              :    there is a matching specific name.  If none, it is an error, and if
    1796              :    more than one, the reference is ambiguous.  */
    1797              : static int
    1798            8 : count_specific_procs (gfc_expr *e)
    1799              : {
    1800            8 :   int n;
    1801            8 :   gfc_interface *p;
    1802            8 :   gfc_symbol *sym;
    1803              : 
    1804            8 :   n = 0;
    1805            8 :   sym = e->symtree->n.sym;
    1806              : 
    1807           22 :   for (p = sym->generic; p; p = p->next)
    1808           14 :     if (strcmp (sym->name, p->sym->name) == 0)
    1809              :       {
    1810            8 :         e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
    1811              :                                        sym->name);
    1812            8 :         n++;
    1813              :       }
    1814              : 
    1815            8 :   if (n > 1)
    1816            1 :     gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
    1817              :                &e->where);
    1818              : 
    1819            8 :   if (n == 0)
    1820            1 :     gfc_error ("GENERIC procedure %qs is not allowed as an actual "
    1821              :                "argument at %L", sym->name, &e->where);
    1822              : 
    1823            8 :   return n;
    1824              : }
    1825              : 
    1826              : 
    1827              : /* See if a call to sym could possibly be a not allowed RECURSION because of
    1828              :    a missing RECURSIVE declaration.  This means that either sym is the current
    1829              :    context itself, or sym is the parent of a contained procedure calling its
    1830              :    non-RECURSIVE containing procedure.
    1831              :    This also works if sym is an ENTRY.  */
    1832              : 
    1833              : static bool
    1834       153969 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1835              : {
    1836       153969 :   gfc_symbol* proc_sym;
    1837       153969 :   gfc_symbol* context_proc;
    1838       153969 :   gfc_namespace* real_context;
    1839              : 
    1840       153969 :   if (sym->attr.flavor == FL_PROGRAM
    1841              :       || gfc_fl_struct (sym->attr.flavor))
    1842              :     return false;
    1843              : 
    1844              :   /* If we've got an ENTRY, find real procedure.  */
    1845       153968 :   if (sym->attr.entry && sym->ns->entries)
    1846           45 :     proc_sym = sym->ns->entries->sym;
    1847              :   else
    1848              :     proc_sym = sym;
    1849              : 
    1850              :   /* If sym is RECURSIVE, all is well of course.  */
    1851       153968 :   if (proc_sym->attr.recursive || flag_recursive)
    1852              :     return false;
    1853              : 
    1854              :   /* Find the context procedure's "real" symbol if it has entries.
    1855              :      We look for a procedure symbol, so recurse on the parents if we don't
    1856              :      find one (like in case of a BLOCK construct).  */
    1857         1965 :   for (real_context = context; ; real_context = real_context->parent)
    1858              :     {
    1859              :       /* We should find something, eventually!  */
    1860       130292 :       gcc_assert (real_context);
    1861              : 
    1862       130292 :       context_proc = (real_context->entries ? real_context->entries->sym
    1863              :                                             : real_context->proc_name);
    1864              : 
    1865              :       /* In some special cases, there may not be a proc_name, like for this
    1866              :          invalid code:
    1867              :          real(bad_kind()) function foo () ...
    1868              :          when checking the call to bad_kind ().
    1869              :          In these cases, we simply return here and assume that the
    1870              :          call is ok.  */
    1871       130292 :       if (!context_proc)
    1872              :         return false;
    1873              : 
    1874       130028 :       if (context_proc->attr.flavor != FL_LABEL)
    1875              :         break;
    1876              :     }
    1877              : 
    1878              :   /* A call from sym's body to itself is recursion, of course.  */
    1879       128063 :   if (context_proc == proc_sym)
    1880              :     return true;
    1881              : 
    1882              :   /* The same is true if context is a contained procedure and sym the
    1883              :      containing one.  */
    1884       128048 :   if (context_proc->attr.contained)
    1885              :     {
    1886        21691 :       gfc_symbol* parent_proc;
    1887              : 
    1888        21691 :       gcc_assert (context->parent);
    1889        21691 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1890              :                                               : context->parent->proc_name);
    1891              : 
    1892        21691 :       if (parent_proc == proc_sym)
    1893            9 :         return true;
    1894              :     }
    1895              : 
    1896              :   return false;
    1897              : }
    1898              : 
    1899              : 
    1900              : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
    1901              :    its typespec and formal argument list.  */
    1902              : 
    1903              : bool
    1904        47392 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1905              : {
    1906        47392 :   gfc_intrinsic_sym* isym = NULL;
    1907        47392 :   const char* symstd;
    1908              : 
    1909        47392 :   if (sym->resolve_symbol_called >= 2)
    1910              :     return true;
    1911              : 
    1912        37350 :   sym->resolve_symbol_called = 2;
    1913              : 
    1914              :   /* Already resolved.  */
    1915        37350 :   if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
    1916              :     return true;
    1917              : 
    1918              :   /* We already know this one is an intrinsic, so we don't call
    1919              :      gfc_is_intrinsic for full checking but rather use gfc_find_function and
    1920              :      gfc_find_subroutine directly to check whether it is a function or
    1921              :      subroutine.  */
    1922              : 
    1923        29282 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1924              :     {
    1925        12754 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1926        12754 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1927        12754 :     }
    1928        16528 :   else if (sym->intmod_sym_id)
    1929              :     {
    1930        12685 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1931        12685 :       isym = gfc_intrinsic_function_by_id (id);
    1932              :     }
    1933         3843 :   else if (!sym->attr.subroutine)
    1934         3756 :     isym = gfc_find_function (sym->name);
    1935              : 
    1936        29195 :   if (isym && !sym->attr.subroutine)
    1937              :     {
    1938        16396 :       if (sym->ts.type != BT_UNKNOWN && warn_surprising
    1939           24 :           && !sym->attr.implicit_type)
    1940           10 :         gfc_warning (OPT_Wsurprising,
    1941              :                      "Type specified for intrinsic function %qs at %L is"
    1942              :                       " ignored", sym->name, &sym->declared_at);
    1943              : 
    1944        20883 :       if (!sym->attr.function &&
    1945         4487 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1946              :         return false;
    1947              : 
    1948        16396 :       sym->ts = isym->ts;
    1949              :     }
    1950        12886 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1951              :     {
    1952        12883 :       if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
    1953              :         {
    1954            1 :           gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
    1955              :                       " specifier", sym->name, &sym->declared_at);
    1956            1 :           return false;
    1957              :         }
    1958              : 
    1959        12923 :       if (!sym->attr.subroutine &&
    1960           41 :           !gfc_add_subroutine(&sym->attr, sym->name, loc))
    1961              :         return false;
    1962              :     }
    1963              :   else
    1964              :     {
    1965            3 :       gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
    1966              :                  &sym->declared_at);
    1967            3 :       return false;
    1968              :     }
    1969              : 
    1970        29277 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1971              : 
    1972        29277 :   sym->attr.pure = isym->pure;
    1973        29277 :   sym->attr.elemental = isym->elemental;
    1974              : 
    1975              :   /* Check it is actually available in the standard settings.  */
    1976        29277 :   if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
    1977              :     {
    1978           31 :       gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
    1979              :                  "available in the current standard settings but %s. Use "
    1980              :                  "an appropriate %<-std=*%> option or enable "
    1981              :                  "%<-fall-intrinsics%> in order to use it.",
    1982              :                  sym->name, &sym->declared_at, symstd);
    1983           31 :       return false;
    1984              :     }
    1985              : 
    1986              :   return true;
    1987              : }
    1988              : 
    1989              : 
    1990              : /* Resolve a procedure expression, like passing it to a called procedure or as
    1991              :    RHS for a procedure pointer assignment.  */
    1992              : 
    1993              : static bool
    1994      1340801 : resolve_procedure_expression (gfc_expr* expr)
    1995              : {
    1996      1340801 :   gfc_symbol* sym;
    1997              : 
    1998      1340801 :   if (expr->expr_type != EXPR_VARIABLE)
    1999              :     return true;
    2000      1340784 :   gcc_assert (expr->symtree);
    2001              : 
    2002      1340784 :   sym = expr->symtree->n.sym;
    2003              : 
    2004      1340784 :   if (sym->attr.intrinsic)
    2005         1360 :     gfc_resolve_intrinsic (sym, &expr->where);
    2006              : 
    2007      1340784 :   if (sym->attr.flavor != FL_PROCEDURE
    2008        32505 :       || (sym->attr.function && sym->result == sym))
    2009              :     return true;
    2010              : 
    2011              :    /* A non-RECURSIVE procedure that is used as procedure expression within its
    2012              :      own body is in danger of being called recursively.  */
    2013        17747 :   if (is_illegal_recursion (sym, gfc_current_ns))
    2014              :     {
    2015           10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    2016            0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    2017              :                      " possibly calling itself recursively in procedure %qs. "
    2018              :                      " Declare it RECURSIVE or use %<-frecursive%>",
    2019            0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    2020              :       else
    2021           10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    2022              :                      " itself recursively.  Declare it RECURSIVE or use"
    2023              :                      " %<-frecursive%>", sym->name, &expr->where);
    2024              :     }
    2025              : 
    2026              :   return true;
    2027              : }
    2028              : 
    2029              : 
    2030              : /* Check that name is not a derived type.  */
    2031              : 
    2032              : static bool
    2033         3434 : is_dt_name (const char *name)
    2034              : {
    2035         3434 :   gfc_symbol *dt_list, *dt_first;
    2036              : 
    2037         3434 :   dt_list = dt_first = gfc_derived_types;
    2038         5888 :   for (; dt_list; dt_list = dt_list->dt_next)
    2039              :     {
    2040         3577 :       if (strcmp(dt_list->name, name) == 0)
    2041              :         return true;
    2042         3574 :       if (dt_first == dt_list->dt_next)
    2043              :         break;
    2044              :     }
    2045              :   return false;
    2046              : }
    2047              : 
    2048              : 
    2049              : /* Resolve an actual argument list.  Most of the time, this is just
    2050              :    resolving the expressions in the list.
    2051              :    The exception is that we sometimes have to decide whether arguments
    2052              :    that look like procedure arguments are really simple variable
    2053              :    references.  */
    2054              : 
    2055              : static bool
    2056       431201 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2057              :                         bool no_formal_args)
    2058              : {
    2059       431201 :   gfc_symbol *sym = NULL;
    2060       431201 :   gfc_symtree *parent_st;
    2061       431201 :   gfc_expr *e;
    2062       431201 :   gfc_component *comp;
    2063       431201 :   int save_need_full_assumed_size;
    2064       431201 :   bool return_value = false;
    2065       431201 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2066              : 
    2067       431201 :   actual_arg = true;
    2068       431201 :   first_actual_arg = true;
    2069              : 
    2070      1105710 :   for (; arg; arg = arg->next)
    2071              :     {
    2072       674610 :       e = arg->expr;
    2073       674610 :       if (e == NULL)
    2074              :         {
    2075              :           /* Check the label is a valid branching target.  */
    2076         2485 :           if (arg->label)
    2077              :             {
    2078          236 :               if (arg->label->defined == ST_LABEL_UNKNOWN)
    2079              :                 {
    2080            0 :                   gfc_error ("Label %d referenced at %L is never defined",
    2081              :                              arg->label->value, &arg->label->where);
    2082            0 :                   goto cleanup;
    2083              :                 }
    2084              :             }
    2085         2485 :           first_actual_arg = false;
    2086         2485 :           continue;
    2087              :         }
    2088              : 
    2089       672125 :       if (e->expr_type == EXPR_VARIABLE
    2090       296719 :             && e->symtree->n.sym->attr.generic
    2091            8 :             && no_formal_args
    2092       672130 :             && count_specific_procs (e) != 1)
    2093            2 :         goto cleanup;
    2094              : 
    2095       672123 :       if (e->ts.type != BT_PROCEDURE)
    2096              :         {
    2097       598788 :           save_need_full_assumed_size = need_full_assumed_size;
    2098       598788 :           if (e->expr_type != EXPR_VARIABLE)
    2099       375406 :             need_full_assumed_size = 0;
    2100       598788 :           if (!gfc_resolve_expr (e))
    2101           60 :             goto cleanup;
    2102       598728 :           need_full_assumed_size = save_need_full_assumed_size;
    2103       598728 :           goto argument_list;
    2104              :         }
    2105              : 
    2106              :       /* See if the expression node should really be a variable reference.  */
    2107              : 
    2108        73335 :       sym = e->symtree->n.sym;
    2109              : 
    2110        73335 :       if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
    2111              :         {
    2112            3 :           gfc_error ("Derived type %qs is used as an actual "
    2113              :                      "argument at %L", sym->name, &e->where);
    2114            3 :           goto cleanup;
    2115              :         }
    2116              : 
    2117        73332 :       if (sym->attr.flavor == FL_PROCEDURE
    2118        69901 :           || sym->attr.intrinsic
    2119        69901 :           || sym->attr.external)
    2120              :         {
    2121         3431 :           int actual_ok;
    2122              : 
    2123              :           /* If a procedure is not already determined to be something else
    2124              :              check if it is intrinsic.  */
    2125         3431 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2126         1254 :             sym->attr.intrinsic = 1;
    2127              : 
    2128         3431 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2129              :             {
    2130            2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2131              :                          "actual argument", sym->name, &e->where);
    2132              :             }
    2133              : 
    2134         6862 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2135         3431 :                                                sym->attr.subroutine);
    2136         3431 :           if (sym->attr.intrinsic && actual_ok == 0)
    2137              :             {
    2138            0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2139              :                          "actual argument", sym->name, &e->where);
    2140              :             }
    2141              : 
    2142         3431 :           if (sym->attr.contained && !sym->attr.use_assoc
    2143          444 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2144              :             {
    2145          256 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2146              :                                    " used as actual argument at %L",
    2147              :                                    sym->name, &e->where))
    2148            3 :                 goto cleanup;
    2149              :             }
    2150              : 
    2151         3428 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2152              :             {
    2153            2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2154              :                          "allowed as an actual argument at %L", sym->name,
    2155              :                          &e->where);
    2156              :             }
    2157              : 
    2158              :           /* Check if a generic interface has a specific procedure
    2159              :             with the same name before emitting an error.  */
    2160         3428 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2161            0 :             goto cleanup;
    2162              : 
    2163              :           /* Just in case a specific was found for the expression.  */
    2164         3428 :           sym = e->symtree->n.sym;
    2165              : 
    2166              :           /* If the symbol is the function that names the current (or
    2167              :              parent) scope, then we really have a variable reference.  */
    2168              : 
    2169         3428 :           if (gfc_is_function_return_value (sym, sym->ns))
    2170            0 :             goto got_variable;
    2171              : 
    2172              :           /* If all else fails, see if we have a specific intrinsic.  */
    2173         3428 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2174              :             {
    2175            0 :               gfc_intrinsic_sym *isym;
    2176              : 
    2177            0 :               isym = gfc_find_function (sym->name);
    2178            0 :               if (isym == NULL || !isym->specific)
    2179              :                 {
    2180            0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2181              :                              "for the reference %qs at %L", sym->name,
    2182              :                              &e->where);
    2183            0 :                   goto cleanup;
    2184              :                 }
    2185            0 :               sym->ts = isym->ts;
    2186            0 :               sym->attr.intrinsic = 1;
    2187            0 :               sym->attr.function = 1;
    2188              :             }
    2189              : 
    2190         3428 :           if (!gfc_resolve_expr (e))
    2191            0 :             goto cleanup;
    2192         3428 :           goto argument_list;
    2193              :         }
    2194              : 
    2195              :       /* See if the name is a module procedure in a parent unit.  */
    2196              : 
    2197        69901 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2198        69807 :         goto got_variable;
    2199              : 
    2200           94 :       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
    2201              :         {
    2202            0 :           gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
    2203            0 :           goto cleanup;
    2204              :         }
    2205              : 
    2206           94 :       if (parent_st == NULL)
    2207           94 :         goto got_variable;
    2208              : 
    2209            0 :       sym = parent_st->n.sym;
    2210            0 :       e->symtree = parent_st;                /* Point to the right thing.  */
    2211              : 
    2212            0 :       if (sym->attr.flavor == FL_PROCEDURE
    2213            0 :           || sym->attr.intrinsic
    2214            0 :           || sym->attr.external)
    2215              :         {
    2216            0 :           if (!gfc_resolve_expr (e))
    2217            0 :             goto cleanup;
    2218            0 :           goto argument_list;
    2219              :         }
    2220              : 
    2221            0 :     got_variable:
    2222        69901 :       e->expr_type = EXPR_VARIABLE;
    2223        69901 :       e->ts = sym->ts;
    2224        69901 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2225        36317 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2226         3936 :               && CLASS_DATA (sym)->as))
    2227              :         {
    2228        39256 :           gfc_array_spec *as
    2229        36420 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2230        36420 :           e->rank = as->rank;
    2231        36420 :           e->corank = as->corank;
    2232        36420 :           e->ref = gfc_get_ref ();
    2233        36420 :           e->ref->type = REF_ARRAY;
    2234        36420 :           e->ref->u.ar.type = AR_FULL;
    2235        36420 :           e->ref->u.ar.as = as;
    2236              :         }
    2237              : 
    2238              :       /* These symbols are set untyped by calls to gfc_set_default_type
    2239              :          with 'error_flag' = false.  Reset the untyped attribute so that
    2240              :          the error will be generated in gfc_resolve_expr.  */
    2241        69901 :       if (e->expr_type == EXPR_VARIABLE
    2242        69901 :           && sym->ts.type == BT_UNKNOWN
    2243           36 :           && sym->attr.untyped)
    2244            5 :         sym->attr.untyped = 0;
    2245              : 
    2246              :       /* Expressions are assigned a default ts.type of BT_PROCEDURE in
    2247              :          primary.cc (match_actual_arg). If above code determines that it
    2248              :          is a  variable instead, it needs to be resolved as it was not
    2249              :          done at the beginning of this function.  */
    2250        69901 :       save_need_full_assumed_size = need_full_assumed_size;
    2251        69901 :       if (e->expr_type != EXPR_VARIABLE)
    2252            0 :         need_full_assumed_size = 0;
    2253        69901 :       if (!gfc_resolve_expr (e))
    2254           22 :         goto cleanup;
    2255        69879 :       need_full_assumed_size = save_need_full_assumed_size;
    2256              : 
    2257       672035 :     argument_list:
    2258              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2259              :          nothing to do for %REF.  */
    2260       672035 :       if (arg->name && arg->name[0] == '%')
    2261              :         {
    2262           42 :           if (strcmp ("%VAL", arg->name) == 0)
    2263              :             {
    2264           28 :               if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
    2265              :                 {
    2266            2 :                   gfc_error ("By-value argument at %L is not of numeric "
    2267              :                              "type", &e->where);
    2268            2 :                   goto cleanup;
    2269              :                 }
    2270              : 
    2271           26 :               if (e->rank)
    2272              :                 {
    2273            1 :                   gfc_error ("By-value argument at %L cannot be an array or "
    2274              :                              "an array section", &e->where);
    2275            1 :                   goto cleanup;
    2276              :                 }
    2277              : 
    2278              :               /* Intrinsics are still PROC_UNKNOWN here.  However,
    2279              :                  since same file external procedures are not resolvable
    2280              :                  in gfortran, it is a good deal easier to leave them to
    2281              :                  intrinsic.cc.  */
    2282           25 :               if (ptype != PROC_UNKNOWN
    2283           25 :                   && ptype != PROC_DUMMY
    2284            9 :                   && ptype != PROC_EXTERNAL
    2285            9 :                   && ptype != PROC_MODULE)
    2286              :                 {
    2287            3 :                   gfc_error ("By-value argument at %L is not allowed "
    2288              :                              "in this context", &e->where);
    2289            3 :                   goto cleanup;
    2290              :                 }
    2291              :             }
    2292              : 
    2293              :           /* Statement functions have already been excluded above.  */
    2294           14 :           else if (strcmp ("%LOC", arg->name) == 0
    2295            8 :                    && e->ts.type == BT_PROCEDURE)
    2296              :             {
    2297            0 :               if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
    2298              :                 {
    2299            0 :                   gfc_error ("Passing internal procedure at %L by location "
    2300              :                              "not allowed", &e->where);
    2301            0 :                   goto cleanup;
    2302              :                 }
    2303              :             }
    2304              :         }
    2305              : 
    2306       672029 :       comp = gfc_get_proc_ptr_comp(e);
    2307       672029 :       if (e->expr_type == EXPR_VARIABLE
    2308       295341 :           && comp && comp->attr.elemental)
    2309              :         {
    2310            1 :             gfc_error ("ELEMENTAL procedure pointer component %qs is not "
    2311              :                        "allowed as an actual argument at %L", comp->name,
    2312              :                        &e->where);
    2313              :         }
    2314              : 
    2315              :       /* Fortran 2008, C1237.  */
    2316       295341 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2317       672474 :           && gfc_has_ultimate_pointer (e))
    2318              :         {
    2319            3 :           gfc_error ("Coindexed actual argument at %L with ultimate pointer "
    2320              :                      "component", &e->where);
    2321            3 :           goto cleanup;
    2322              :         }
    2323              : 
    2324       672026 :       if (e->expr_type == EXPR_VARIABLE
    2325       295338 :           && e->ts.type == BT_PROCEDURE
    2326         3428 :           && no_formal_args
    2327         1505 :           && sym->attr.flavor == FL_PROCEDURE
    2328         1505 :           && sym->attr.if_source == IFSRC_UNKNOWN
    2329          142 :           && !sym->attr.external
    2330            2 :           && !sym->attr.intrinsic
    2331            2 :           && !sym->attr.artificial
    2332            2 :           && !sym->ts.interface)
    2333              :         {
    2334              :           /* Emit a warning for -std=legacy and an error otherwise. */
    2335            2 :           if (gfc_option.warn_std == 0)
    2336            0 :             gfc_warning (0, "Procedure %qs at %L used as actual argument but "
    2337              :                          "does neither have an explicit interface nor the "
    2338              :                          "EXTERNAL attribute", sym->name, &e->where);
    2339              :           else
    2340              :             {
    2341            2 :               gfc_error ("Procedure %qs at %L used as actual argument but "
    2342              :                          "does neither have an explicit interface nor the "
    2343              :                          "EXTERNAL attribute", sym->name, &e->where);
    2344            2 :               goto cleanup;
    2345              :             }
    2346              :         }
    2347              : 
    2348       672024 :       first_actual_arg = false;
    2349              :     }
    2350              : 
    2351              :   return_value = true;
    2352              : 
    2353       431201 : cleanup:
    2354       431201 :   actual_arg = actual_arg_sav;
    2355       431201 :   first_actual_arg = first_actual_arg_sav;
    2356              : 
    2357       431201 :   return return_value;
    2358              : }
    2359              : 
    2360              : 
    2361              : /* Do the checks of the actual argument list that are specific to elemental
    2362              :    procedures.  If called with c == NULL, we have a function, otherwise if
    2363              :    expr == NULL, we have a subroutine.  */
    2364              : 
    2365              : static bool
    2366       328463 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2367              : {
    2368       328463 :   gfc_actual_arglist *arg0;
    2369       328463 :   gfc_actual_arglist *arg;
    2370       328463 :   gfc_symbol *esym = NULL;
    2371       328463 :   gfc_intrinsic_sym *isym = NULL;
    2372       328463 :   gfc_expr *e = NULL;
    2373       328463 :   gfc_intrinsic_arg *iformal = NULL;
    2374       328463 :   gfc_formal_arglist *eformal = NULL;
    2375       328463 :   bool formal_optional = false;
    2376       328463 :   bool set_by_optional = false;
    2377       328463 :   int i;
    2378       328463 :   int rank = 0;
    2379              : 
    2380              :   /* Is this an elemental procedure?  */
    2381       328463 :   if (expr && expr->value.function.actual != NULL)
    2382              :     {
    2383       237849 :       if (expr->value.function.esym != NULL
    2384        44366 :           && expr->value.function.esym->attr.elemental)
    2385              :         {
    2386              :           arg0 = expr->value.function.actual;
    2387              :           esym = expr->value.function.esym;
    2388              :         }
    2389       221541 :       else if (expr->value.function.isym != NULL
    2390       192424 :                && expr->value.function.isym->elemental)
    2391              :         {
    2392              :           arg0 = expr->value.function.actual;
    2393              :           isym = expr->value.function.isym;
    2394              :         }
    2395              :       else
    2396              :         return true;
    2397              :     }
    2398        90614 :   else if (c && c->ext.actual != NULL)
    2399              :     {
    2400        71590 :       arg0 = c->ext.actual;
    2401              : 
    2402        71590 :       if (c->resolved_sym)
    2403              :         esym = c->resolved_sym;
    2404              :       else
    2405          323 :         esym = c->symtree->n.sym;
    2406        71590 :       gcc_assert (esym);
    2407              : 
    2408        71590 :       if (!esym->attr.elemental)
    2409              :         return true;
    2410              :     }
    2411              :   else
    2412              :     return true;
    2413              : 
    2414              :   /* The rank of an elemental is the rank of its array argument(s).  */
    2415       174628 :   for (arg = arg0; arg; arg = arg->next)
    2416              :     {
    2417       113148 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2418              :         {
    2419        10752 :           rank = arg->expr->rank;
    2420        10752 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2421         5490 :               && arg->expr->symtree->n.sym->attr.optional)
    2422        10752 :             set_by_optional = true;
    2423              : 
    2424              :           /* Function specific; set the result rank and shape.  */
    2425        10752 :           if (expr)
    2426              :             {
    2427         8344 :               expr->rank = rank;
    2428         8344 :               expr->corank = arg->expr->corank;
    2429         8344 :               if (!expr->shape && arg->expr->shape)
    2430              :                 {
    2431         3974 :                   expr->shape = gfc_get_shape (rank);
    2432         8743 :                   for (i = 0; i < rank; i++)
    2433         4769 :                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
    2434              :                 }
    2435              :             }
    2436              :           break;
    2437              :         }
    2438              :     }
    2439              : 
    2440              :   /* If it is an array, it shall not be supplied as an actual argument
    2441              :      to an elemental procedure unless an array of the same rank is supplied
    2442              :      as an actual argument corresponding to a nonoptional dummy argument of
    2443              :      that elemental procedure(12.4.1.5).  */
    2444        72232 :   formal_optional = false;
    2445        72232 :   if (isym)
    2446        49711 :     iformal = isym->formal;
    2447              :   else
    2448        22521 :     eformal = esym->formal;
    2449              : 
    2450       190896 :   for (arg = arg0; arg; arg = arg->next)
    2451              :     {
    2452       118664 :       if (eformal)
    2453              :         {
    2454        40411 :           if (eformal->sym && eformal->sym->attr.optional)
    2455        40411 :             formal_optional = true;
    2456        40411 :           eformal = eformal->next;
    2457              :         }
    2458        78253 :       else if (isym && iformal)
    2459              :         {
    2460        67970 :           if (iformal->optional)
    2461        13459 :             formal_optional = true;
    2462        67970 :           iformal = iformal->next;
    2463              :         }
    2464        10283 :       else if (isym)
    2465        10275 :         formal_optional = true;
    2466              : 
    2467       118664 :       if (pedantic && arg->expr != NULL
    2468        67711 :           && arg->expr->expr_type == EXPR_VARIABLE
    2469        31920 :           && arg->expr->symtree->n.sym->attr.optional
    2470          572 :           && formal_optional
    2471          479 :           && arg->expr->rank
    2472          153 :           && (set_by_optional || arg->expr->rank != rank)
    2473           42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2474              :         {
    2475          114 :           bool t = false;
    2476              :           gfc_actual_arglist *a;
    2477              : 
    2478              :           /* Scan the argument list for a non-optional argument with the
    2479              :              same rank as arg.  */
    2480          114 :           for (a = arg0; a; a = a->next)
    2481           87 :             if (a != arg
    2482           45 :                 && a->expr->rank == arg->expr->rank
    2483           39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2484           37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2485           37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2486              :               {
    2487              :                 t = true;
    2488              :                 break;
    2489              :               }
    2490              : 
    2491           42 :           if (!t)
    2492           27 :             gfc_warning (OPT_Wpedantic,
    2493              :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2494              :                          "present, then it cannot be the actual argument of "
    2495              :                          "an ELEMENTAL procedure unless there is a non-optional"
    2496              :                          " argument with the same rank "
    2497              :                          "(Fortran 2018, 15.5.2.12)",
    2498              :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2499              :         }
    2500              :     }
    2501              : 
    2502       190885 :   for (arg = arg0; arg; arg = arg->next)
    2503              :     {
    2504       118662 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2505       105022 :         continue;
    2506              : 
    2507              :       /* Being elemental, the last upper bound of an assumed size array
    2508              :          argument must be present.  */
    2509        13640 :       if (resolve_assumed_size_actual (arg->expr))
    2510              :         return false;
    2511              : 
    2512              :       /* Elemental procedure's array actual arguments must conform.  */
    2513        13637 :       if (e != NULL)
    2514              :         {
    2515         2888 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2516              :             return false;
    2517              :         }
    2518              :       else
    2519        10749 :         e = arg->expr;
    2520              :     }
    2521              : 
    2522              :   /* INTENT(OUT) is only allowed for subroutines; if any actual argument
    2523              :      is an array, the intent inout/out variable needs to be also an array.  */
    2524        72223 :   if (rank > 0 && esym && expr == NULL)
    2525         7333 :     for (eformal = esym->formal, arg = arg0; arg && eformal;
    2526         4931 :          arg = arg->next, eformal = eformal->next)
    2527         4933 :       if (eformal->sym
    2528         4932 :           && (eformal->sym->attr.intent == INTENT_OUT
    2529         3850 :               || eformal->sym->attr.intent == INTENT_INOUT)
    2530         1716 :           && arg->expr && arg->expr->rank == 0)
    2531              :         {
    2532            2 :           gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
    2533              :                      "ELEMENTAL subroutine %qs is a scalar, but another "
    2534              :                      "actual argument is an array", &arg->expr->where,
    2535              :                      (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
    2536              :                      : "INOUT", eformal->sym->name, esym->name);
    2537            2 :           return false;
    2538              :         }
    2539              :   return true;
    2540              : }
    2541              : 
    2542              : 
    2543              : /* This function does the checking of references to global procedures
    2544              :    as defined in sections 18.1 and 14.1, respectively, of the Fortran
    2545              :    77 and 95 standards.  It checks for a gsymbol for the name, making
    2546              :    one if it does not already exist.  If it already exists, then the
    2547              :    reference being resolved must correspond to the type of gsymbol.
    2548              :    Otherwise, the new symbol is equipped with the attributes of the
    2549              :    reference.  The corresponding code that is called in creating
    2550              :    global entities is parse.cc.
    2551              : 
    2552              :    In addition, for all but -std=legacy, the gsymbols are used to
    2553              :    check the interfaces of external procedures from the same file.
    2554              :    The namespace of the gsymbol is resolved and then, once this is
    2555              :    done the interface is checked.  */
    2556              : 
    2557              : 
    2558              : static bool
    2559        14997 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2560              : {
    2561        14997 :   if (!gsym_ns->proc_name->attr.recursive)
    2562              :     return true;
    2563              : 
    2564          151 :   if (sym->ns == gsym_ns)
    2565              :     return false;
    2566              : 
    2567          151 :   if (sym->ns->parent && sym->ns->parent == gsym_ns)
    2568            0 :     return false;
    2569              : 
    2570              :   return true;
    2571              : }
    2572              : 
    2573              : static bool
    2574        14997 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2575              : {
    2576        14997 :   if (gsym_ns->entries)
    2577              :     {
    2578              :       gfc_entry_list *entry = gsym_ns->entries;
    2579              : 
    2580         3312 :       for (; entry; entry = entry->next)
    2581              :         {
    2582         2333 :           if (strcmp (sym->name, entry->sym->name) == 0)
    2583              :             {
    2584          971 :               if (strcmp (gsym_ns->proc_name->name,
    2585          971 :                           sym->ns->proc_name->name) == 0)
    2586              :                 return false;
    2587              : 
    2588          971 :               if (sym->ns->parent
    2589            0 :                   && strcmp (gsym_ns->proc_name->name,
    2590            0 :                              sym->ns->parent->proc_name->name) == 0)
    2591              :                 return false;
    2592              :             }
    2593              :         }
    2594              :     }
    2595              :   return true;
    2596              : }
    2597              : 
    2598              : 
    2599              : /* Check for the requirement of an explicit interface. F08:12.4.2.2.  */
    2600              : 
    2601              : bool
    2602        15831 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2603              : {
    2604        15831 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2605              : 
    2606        59088 :   for ( ; arg; arg = arg->next)
    2607              :     {
    2608        27834 :       if (!arg->sym)
    2609          157 :         continue;
    2610              : 
    2611        27677 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2612              :         {
    2613            0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2614            0 :           return true;
    2615              :         }
    2616        27677 :       else if (arg->sym->attr.asynchronous)
    2617              :         {
    2618            0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2619            0 :           return true;
    2620              :         }
    2621        27677 :       else if (arg->sym->attr.optional)
    2622              :         {
    2623           75 :           strncpy (errmsg, _("optional argument"), err_len);
    2624           75 :           return true;
    2625              :         }
    2626        27602 :       else if (arg->sym->attr.pointer)
    2627              :         {
    2628           12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2629           12 :           return true;
    2630              :         }
    2631        27590 :       else if (arg->sym->attr.target)
    2632              :         {
    2633           72 :           strncpy (errmsg, _("target argument"), err_len);
    2634           72 :           return true;
    2635              :         }
    2636        27518 :       else if (arg->sym->attr.value)
    2637              :         {
    2638           12 :           strncpy (errmsg, _("value argument"), err_len);
    2639           12 :           return true;
    2640              :         }
    2641        27506 :       else if (arg->sym->attr.volatile_)
    2642              :         {
    2643            1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2644            1 :           return true;
    2645              :         }
    2646        27505 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2647              :         {
    2648           69 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2649           69 :           return true;
    2650              :         }
    2651        27436 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK)  /* TS 29113, 6.2.  */
    2652              :         {
    2653            1 :           strncpy (errmsg, _("assumed-rank argument"), err_len);
    2654            1 :           return true;
    2655              :         }
    2656        27435 :       else if (arg->sym->attr.codimension)  /* (2c)  */
    2657              :         {
    2658            1 :           strncpy (errmsg, _("coarray argument"), err_len);
    2659            1 :           return true;
    2660              :         }
    2661        27434 :       else if (false)  /* (2d) TODO: parametrized derived type  */
    2662              :         {
    2663              :           strncpy (errmsg, _("parametrized derived type argument"), err_len);
    2664              :           return true;
    2665              :         }
    2666        27434 :       else if (arg->sym->ts.type == BT_CLASS)  /* (2e)  */
    2667              :         {
    2668          164 :           strncpy (errmsg, _("polymorphic argument"), err_len);
    2669          164 :           return true;
    2670              :         }
    2671        27270 :       else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2672              :         {
    2673            0 :           strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
    2674            0 :           return true;
    2675              :         }
    2676        27270 :       else if (arg->sym->ts.type == BT_ASSUMED)
    2677              :         {
    2678              :           /* As assumed-type is unlimited polymorphic (cf. above).
    2679              :              See also TS 29113, Note 6.1.  */
    2680            1 :           strncpy (errmsg, _("assumed-type argument"), err_len);
    2681            1 :           return true;
    2682              :         }
    2683              :     }
    2684              : 
    2685        15423 :   if (sym->attr.function)
    2686              :     {
    2687         3485 :       gfc_symbol *res = sym->result ? sym->result : sym;
    2688              : 
    2689         3485 :       if (res->attr.dimension)  /* (3a)  */
    2690              :         {
    2691           93 :           strncpy (errmsg, _("array result"), err_len);
    2692           93 :           return true;
    2693              :         }
    2694         3392 :       else if (res->attr.pointer || res->attr.allocatable)  /* (3b)  */
    2695              :         {
    2696           38 :           strncpy (errmsg, _("pointer or allocatable result"), err_len);
    2697           38 :           return true;
    2698              :         }
    2699         3354 :       else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
    2700          347 :                && res->ts.u.cl->length
    2701          166 :                && res->ts.u.cl->length->expr_type != EXPR_CONSTANT)  /* (3c)  */
    2702              :         {
    2703           12 :           strncpy (errmsg, _("result with non-constant character length"), err_len);
    2704           12 :           return true;
    2705              :         }
    2706              :     }
    2707              : 
    2708        15280 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2709              :     {
    2710            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2711            7 :       return true;
    2712              :     }
    2713        15273 :   else if (sym->attr.is_bind_c)  /* (5)  */
    2714              :     {
    2715            0 :       strncpy (errmsg, _("bind(c) procedure"), err_len);
    2716            0 :       return true;
    2717              :     }
    2718              : 
    2719              :   return false;
    2720              : }
    2721              : 
    2722              : 
    2723              : static void
    2724        29698 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2725              : {
    2726        29698 :   gfc_gsymbol * gsym;
    2727        29698 :   gfc_namespace *ns;
    2728        29698 :   enum gfc_symbol_type type;
    2729        29698 :   char reason[200];
    2730              : 
    2731        29698 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2732              : 
    2733        29698 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2734        29698 :                           sym->binding_label != NULL);
    2735              : 
    2736        29698 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2737            9 :     gfc_global_used (gsym, where);
    2738              : 
    2739        29698 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2740         9461 :        || sym->attr.if_source == IFSRC_IFBODY)
    2741        25170 :       && gsym->type != GSYM_UNKNOWN
    2742        22978 :       && !gsym->binding_label
    2743        20661 :       && gsym->ns
    2744        14997 :       && gsym->ns->proc_name
    2745        14997 :       && not_in_recursive (sym, gsym->ns)
    2746        44695 :       && not_entry_self_reference (sym, gsym->ns))
    2747              :     {
    2748        14997 :       gfc_symbol *def_sym;
    2749        14997 :       def_sym = gsym->ns->proc_name;
    2750              : 
    2751        14997 :       if (gsym->ns->resolved != -1)
    2752              :         {
    2753              : 
    2754              :           /* Resolve the gsymbol namespace if needed.  */
    2755        14975 :           if (!gsym->ns->resolved)
    2756              :             {
    2757         2781 :               gfc_symbol *old_dt_list;
    2758              : 
    2759              :               /* Stash away derived types so that the backend_decls
    2760              :                  do not get mixed up.  */
    2761         2781 :               old_dt_list = gfc_derived_types;
    2762         2781 :               gfc_derived_types = NULL;
    2763              : 
    2764         2781 :               gfc_resolve (gsym->ns);
    2765              : 
    2766              :               /* Store the new derived types with the global namespace.  */
    2767         2781 :               if (gfc_derived_types)
    2768          306 :                 gsym->ns->derived_types = gfc_derived_types;
    2769              : 
    2770              :               /* Restore the derived types of this namespace.  */
    2771         2781 :               gfc_derived_types = old_dt_list;
    2772              :             }
    2773              : 
    2774              :           /* Make sure that translation for the gsymbol occurs before
    2775              :              the procedure currently being resolved.  */
    2776        14975 :           ns = gfc_global_ns_list;
    2777        25430 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2778              :             {
    2779        17020 :               if (ns->sibling == gsym->ns)
    2780              :                 {
    2781         6565 :                   ns->sibling = gsym->ns->sibling;
    2782         6565 :                   gsym->ns->sibling = gfc_global_ns_list;
    2783         6565 :                   gfc_global_ns_list = gsym->ns;
    2784         6565 :                   break;
    2785              :                 }
    2786              :             }
    2787              : 
    2788              :           /* This can happen if a binding name has been specified.  */
    2789        14975 :           if (gsym->binding_label && gsym->sym_name != def_sym->name)
    2790            0 :             gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
    2791              :         }
    2792              : 
    2793              :       /* Look up the specific entry symbol so that interface checks use
    2794              :          the entry's own formal argument list, not the entry master's.
    2795              :          This must run even when resolved == -1 (recursive resolution in
    2796              :          progress), because def_sym starts as the namespace proc_name
    2797              :          which is the entry master with the combined formals.  */
    2798        14997 :       if (def_sym->attr.entry_master || def_sym->attr.entry)
    2799              :         {
    2800          979 :           gfc_entry_list *entry;
    2801         1699 :           for (entry = gsym->ns->entries; entry; entry = entry->next)
    2802         1699 :             if (strcmp (entry->sym->name, sym->name) == 0)
    2803              :               {
    2804          979 :                 def_sym = entry->sym;
    2805          979 :                 break;
    2806              :               }
    2807              :         }
    2808              : 
    2809        14997 :       if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
    2810              :         {
    2811            6 :           gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
    2812              :                      sym->name, &sym->declared_at, gfc_typename (&sym->ts),
    2813            6 :                      gfc_typename (&def_sym->ts));
    2814           28 :           goto done;
    2815              :         }
    2816              : 
    2817        14991 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2818        14991 :           && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
    2819              :         {
    2820            8 :           gfc_error ("Explicit interface required for %qs at %L: %s",
    2821              :                      sym->name, &sym->declared_at, reason);
    2822            8 :           goto done;
    2823              :         }
    2824              : 
    2825        14983 :       bool bad_result_characteristics;
    2826        14983 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2827              :                                    reason, sizeof(reason), NULL, NULL,
    2828              :                                    &bad_result_characteristics))
    2829              :         {
    2830              :           /* Turn errors into warnings with -std=gnu and -std=legacy,
    2831              :              unless a function returns a wrong type, which can lead
    2832              :              to all kinds of ICEs and wrong code.  */
    2833              : 
    2834           14 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2835            2 :               && !bad_result_characteristics)
    2836            2 :             gfc_errors_to_warnings (true);
    2837              : 
    2838           14 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2839              :                      sym->name, &sym->declared_at, reason);
    2840           14 :           sym->error = 1;
    2841           14 :           gfc_errors_to_warnings (false);
    2842           14 :           goto done;
    2843              :         }
    2844              :     }
    2845              : 
    2846        29698 : done:
    2847              : 
    2848        29698 :   if (gsym->type == GSYM_UNKNOWN)
    2849              :     {
    2850         4078 :       gsym->type = type;
    2851         4078 :       gsym->where = *where;
    2852              :     }
    2853              : 
    2854        29698 :   gsym->used = 1;
    2855        29698 : }
    2856              : 
    2857              : 
    2858              : /************* Function resolution *************/
    2859              : 
    2860              : /* Resolve a function call known to be generic.
    2861              :    Section 14.1.2.4.1.  */
    2862              : 
    2863              : static match
    2864        28118 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2865              : {
    2866        28118 :   gfc_symbol *s;
    2867              : 
    2868        28118 :   if (sym->attr.generic)
    2869              :     {
    2870        26962 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2871        26962 :       if (s != NULL)
    2872              :         {
    2873        20197 :           expr->value.function.name = s->name;
    2874        20197 :           expr->value.function.esym = s;
    2875              : 
    2876        20197 :           if (s->ts.type != BT_UNKNOWN)
    2877        20180 :             expr->ts = s->ts;
    2878           17 :           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
    2879           15 :             expr->ts = s->result->ts;
    2880              : 
    2881        20197 :           if (s->as != NULL)
    2882              :             {
    2883           55 :               expr->rank = s->as->rank;
    2884           55 :               expr->corank = s->as->corank;
    2885              :             }
    2886        20142 :           else if (s->result != NULL && s->result->as != NULL)
    2887              :             {
    2888            0 :               expr->rank = s->result->as->rank;
    2889            0 :               expr->corank = s->result->as->corank;
    2890              :             }
    2891              : 
    2892        20197 :           gfc_set_sym_referenced (expr->value.function.esym);
    2893              : 
    2894        20197 :           return MATCH_YES;
    2895              :         }
    2896              : 
    2897              :       /* TODO: Need to search for elemental references in generic
    2898              :          interface.  */
    2899              :     }
    2900              : 
    2901         7921 :   if (sym->attr.intrinsic)
    2902         1113 :     return gfc_intrinsic_func_interface (expr, 0);
    2903              : 
    2904              :   return MATCH_NO;
    2905              : }
    2906              : 
    2907              : 
    2908              : static bool
    2909        27974 : resolve_generic_f (gfc_expr *expr)
    2910              : {
    2911        27974 :   gfc_symbol *sym;
    2912        27974 :   match m;
    2913        27974 :   gfc_interface *intr = NULL;
    2914              : 
    2915        27974 :   sym = expr->symtree->n.sym;
    2916              : 
    2917        28118 :   for (;;)
    2918              :     {
    2919        28118 :       m = resolve_generic_f0 (expr, sym);
    2920        28118 :       if (m == MATCH_YES)
    2921              :         return true;
    2922         6810 :       else if (m == MATCH_ERROR)
    2923              :         return false;
    2924              : 
    2925         6810 : generic:
    2926         6813 :       if (!intr)
    2927         6781 :         for (intr = sym->generic; intr; intr = intr->next)
    2928         6697 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2929              :             break;
    2930              : 
    2931         6813 :       if (sym->ns->parent == NULL)
    2932              :         break;
    2933          316 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2934              : 
    2935          316 :       if (sym == NULL)
    2936              :         break;
    2937          147 :       if (!generic_sym (sym))
    2938            3 :         goto generic;
    2939              :     }
    2940              : 
    2941              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2942              :      that possesses a matching interface.  14.1.2.4  */
    2943         6666 :   if (sym  && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
    2944              :     {
    2945            5 :       if (gfc_init_expr_flag)
    2946            1 :         gfc_error ("Function %qs in initialization expression at %L "
    2947              :                    "must be an intrinsic function",
    2948            1 :                    expr->symtree->n.sym->name, &expr->where);
    2949              :       else
    2950            4 :         gfc_error ("There is no specific function for the generic %qs "
    2951            4 :                    "at %L", expr->symtree->n.sym->name, &expr->where);
    2952              :       return false;
    2953              :     }
    2954              : 
    2955         6661 :   if (intr)
    2956              :     {
    2957         6626 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2958              :                                                  NULL, false))
    2959              :         return false;
    2960         6599 :       if (!gfc_use_derived (expr->ts.u.derived))
    2961              :         return false;
    2962         6599 :       return resolve_structure_cons (expr, 0);
    2963              :     }
    2964              : 
    2965           35 :   m = gfc_intrinsic_func_interface (expr, 0);
    2966           35 :   if (m == MATCH_YES)
    2967              :     return true;
    2968              : 
    2969            3 :   if (m == MATCH_NO)
    2970            3 :     gfc_error ("Generic function %qs at %L is not consistent with a "
    2971            3 :                "specific intrinsic interface", expr->symtree->n.sym->name,
    2972              :                &expr->where);
    2973              : 
    2974              :   return false;
    2975              : }
    2976              : 
    2977              : 
    2978              : /* Resolve a function call known to be specific.  */
    2979              : 
    2980              : static match
    2981        28349 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2982              : {
    2983        28349 :   match m;
    2984              : 
    2985        28349 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2986              :     {
    2987         8203 :       if (sym->attr.dummy)
    2988              :         {
    2989          282 :           sym->attr.proc = PROC_DUMMY;
    2990          282 :           goto found;
    2991              :         }
    2992              : 
    2993         7921 :       sym->attr.proc = PROC_EXTERNAL;
    2994         7921 :       goto found;
    2995              :     }
    2996              : 
    2997        20146 :   if (sym->attr.proc == PROC_MODULE
    2998        11267 :       || sym->attr.proc == PROC_ST_FUNCTION
    2999        10977 :       || sym->attr.proc == PROC_INTERNAL)
    3000        19408 :     goto found;
    3001              : 
    3002          738 :   if (sym->attr.intrinsic)
    3003              :     {
    3004          731 :       m = gfc_intrinsic_func_interface (expr, 1);
    3005          731 :       if (m == MATCH_YES)
    3006              :         return MATCH_YES;
    3007            0 :       if (m == MATCH_NO)
    3008            0 :         gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
    3009              :                    "with an intrinsic", sym->name, &expr->where);
    3010              : 
    3011              :       return MATCH_ERROR;
    3012              :     }
    3013              : 
    3014              :   return MATCH_NO;
    3015              : 
    3016        27611 : found:
    3017        27611 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3018              : 
    3019        27611 :   if (sym->result)
    3020        27611 :     expr->ts = sym->result->ts;
    3021              :   else
    3022            0 :     expr->ts = sym->ts;
    3023        27611 :   expr->value.function.name = sym->name;
    3024        27611 :   expr->value.function.esym = sym;
    3025              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3026              :      error(s).  */
    3027        27611 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3028              :     return MATCH_ERROR;
    3029        27610 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
    3030              :     {
    3031          322 :       expr->rank = CLASS_DATA (sym)->as->rank;
    3032          322 :       expr->corank = CLASS_DATA (sym)->as->corank;
    3033              :     }
    3034        27288 :   else if (sym->as != NULL)
    3035              :     {
    3036         2335 :       expr->rank = sym->as->rank;
    3037         2335 :       expr->corank = sym->as->corank;
    3038              :     }
    3039              : 
    3040              :   return MATCH_YES;
    3041              : }
    3042              : 
    3043              : 
    3044              : static bool
    3045        28342 : resolve_specific_f (gfc_expr *expr)
    3046              : {
    3047        28342 :   gfc_symbol *sym;
    3048        28342 :   match m;
    3049              : 
    3050        28342 :   sym = expr->symtree->n.sym;
    3051              : 
    3052        28349 :   for (;;)
    3053              :     {
    3054        28349 :       m = resolve_specific_f0 (sym, expr);
    3055        28349 :       if (m == MATCH_YES)
    3056              :         return true;
    3057            8 :       if (m == MATCH_ERROR)
    3058              :         return false;
    3059              : 
    3060            7 :       if (sym->ns->parent == NULL)
    3061              :         break;
    3062              : 
    3063            7 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3064              : 
    3065            7 :       if (sym == NULL)
    3066              :         break;
    3067              :     }
    3068              : 
    3069            0 :   gfc_error ("Unable to resolve the specific function %qs at %L",
    3070            0 :              expr->symtree->n.sym->name, &expr->where);
    3071              : 
    3072            0 :   return true;
    3073              : }
    3074              : 
    3075              : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
    3076              :    candidates in CANDIDATES_LEN.  */
    3077              : 
    3078              : static void
    3079          212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
    3080              :                                        char **&candidates,
    3081              :                                        size_t &candidates_len)
    3082              : {
    3083          388 :   gfc_symtree *p;
    3084              : 
    3085          388 :   if (sym == NULL)
    3086              :     return;
    3087          388 :   if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
    3088          126 :       && sym->n.sym->attr.flavor == FL_PROCEDURE)
    3089           51 :     vec_push (candidates, candidates_len, sym->name);
    3090              : 
    3091          388 :   p = sym->left;
    3092          388 :   if (p)
    3093          155 :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3094              : 
    3095          388 :   p = sym->right;
    3096          388 :   if (p)
    3097              :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3098              : }
    3099              : 
    3100              : 
    3101              : /* Lookup function FN fuzzily, taking names in SYMROOT into account.  */
    3102              : 
    3103              : const char*
    3104           57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
    3105              : {
    3106           57 :   char **candidates = NULL;
    3107           57 :   size_t candidates_len = 0;
    3108           57 :   lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
    3109           57 :   return gfc_closest_fuzzy_match (fn, candidates);
    3110              : }
    3111              : 
    3112              : 
    3113              : /* Resolve a procedure call not known to be generic nor specific.  */
    3114              : 
    3115              : static bool
    3116       278275 : resolve_unknown_f (gfc_expr *expr)
    3117              : {
    3118       278275 :   gfc_symbol *sym;
    3119       278275 :   gfc_typespec *ts;
    3120              : 
    3121       278275 :   sym = expr->symtree->n.sym;
    3122              : 
    3123       278275 :   if (sym->attr.dummy)
    3124              :     {
    3125          293 :       sym->attr.proc = PROC_DUMMY;
    3126          293 :       expr->value.function.name = sym->name;
    3127          293 :       goto set_type;
    3128              :     }
    3129              : 
    3130              :   /* See if we have an intrinsic function reference.  */
    3131              : 
    3132       277982 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3133              :     {
    3134       275720 :       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
    3135              :         return true;
    3136          811 :       return false;
    3137              :     }
    3138              : 
    3139              :   /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
    3140              :   /* Intrinsics were handled above, only non-intrinsics left here.  */
    3141         2262 :   if (sym->attr.flavor == FL_PROCEDURE
    3142         2259 :       && sym->attr.implicit_type
    3143          376 :       && sym->ns
    3144          376 :       && sym->ns->has_implicit_none_export)
    3145              :     {
    3146            3 :           gfc_error ("Missing explicit declaration with EXTERNAL attribute "
    3147              :               "for symbol %qs at %L", sym->name, &sym->declared_at);
    3148            3 :           sym->error = 1;
    3149            3 :           return false;
    3150              :     }
    3151              : 
    3152              :   /* The reference is to an external name.  */
    3153              : 
    3154         2259 :   sym->attr.proc = PROC_EXTERNAL;
    3155         2259 :   expr->value.function.name = sym->name;
    3156         2259 :   expr->value.function.esym = expr->symtree->n.sym;
    3157              : 
    3158         2259 :   if (sym->as != NULL)
    3159              :     {
    3160            1 :       expr->rank = sym->as->rank;
    3161            1 :       expr->corank = sym->as->corank;
    3162              :     }
    3163              : 
    3164              :   /* Type of the expression is either the type of the symbol or the
    3165              :      default type of the symbol.  */
    3166              : 
    3167         2258 : set_type:
    3168         2552 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3169              : 
    3170         2552 :   if (sym->ts.type != BT_UNKNOWN)
    3171         2501 :     expr->ts = sym->ts;
    3172              :   else
    3173              :     {
    3174           51 :       ts = gfc_get_default_type (sym->name, sym->ns);
    3175              : 
    3176           51 :       if (ts->type == BT_UNKNOWN)
    3177              :         {
    3178           41 :           const char *guessed
    3179           41 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    3180           41 :           if (guessed)
    3181            3 :             gfc_error ("Function %qs at %L has no IMPLICIT type"
    3182              :                        "; did you mean %qs?",
    3183              :                        sym->name, &expr->where, guessed);
    3184              :           else
    3185           38 :             gfc_error ("Function %qs at %L has no IMPLICIT type",
    3186              :                        sym->name, &expr->where);
    3187              :           return false;
    3188              :         }
    3189              :       else
    3190           10 :         expr->ts = *ts;
    3191              :     }
    3192              : 
    3193              :   return true;
    3194              : }
    3195              : 
    3196              : 
    3197              : /* Return true, if the symbol is an external procedure.  */
    3198              : static bool
    3199       859747 : is_external_proc (gfc_symbol *sym)
    3200              : {
    3201       858024 :   if (!sym->attr.dummy && !sym->attr.contained
    3202       748752 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3203       163986 :         && sym->attr.proc != PROC_ST_FUNCTION
    3204       163391 :         && !sym->attr.proc_pointer
    3205       162185 :         && !sym->attr.use_assoc
    3206       919174 :         && sym->name)
    3207        59427 :     return true;
    3208              : 
    3209              :   return false;
    3210              : }
    3211              : 
    3212              : 
    3213              : /* Figure out if a function reference is pure or not.  Also set the name
    3214              :    of the function for a potential error message.  Return nonzero if the
    3215              :    function is PURE, zero if not.  */
    3216              : static bool
    3217              : pure_stmt_function (gfc_expr *, gfc_symbol *);
    3218              : 
    3219              : bool
    3220       258514 : gfc_pure_function (gfc_expr *e, const char **name)
    3221              : {
    3222       258514 :   bool pure;
    3223       258514 :   gfc_component *comp;
    3224              : 
    3225       258514 :   *name = NULL;
    3226              : 
    3227       258514 :   if (e->symtree != NULL
    3228       258158 :         && e->symtree->n.sym != NULL
    3229       258158 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3230          305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3231              : 
    3232       258209 :   comp = gfc_get_proc_ptr_comp (e);
    3233       258209 :   if (comp)
    3234              :     {
    3235          479 :       pure = gfc_pure (comp->ts.interface);
    3236          479 :       *name = comp->name;
    3237              :     }
    3238       257730 :   else if (e->value.function.esym)
    3239              :     {
    3240        53407 :       pure = gfc_pure (e->value.function.esym);
    3241        53407 :       *name = e->value.function.esym->name;
    3242              :     }
    3243       204323 :   else if (e->value.function.isym)
    3244              :     {
    3245       406498 :       pure = e->value.function.isym->pure
    3246       203249 :              || e->value.function.isym->elemental;
    3247       203249 :       *name = e->value.function.isym->name;
    3248              :     }
    3249         1074 :   else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
    3250              :     {
    3251              :       /* The function has been resolved, but esym is not yet set.
    3252              :          This can happen with functions as dummy argument.  */
    3253          291 :       pure = e->symtree->n.sym->attr.pure;
    3254          291 :       *name = e->symtree->n.sym->name;
    3255              :     }
    3256              :   else
    3257              :     {
    3258              :       /* Implicit functions are not pure.  */
    3259          783 :       pure = 0;
    3260          783 :       *name = e->value.function.name;
    3261              :     }
    3262              : 
    3263              :   return pure;
    3264              : }
    3265              : 
    3266              : 
    3267              : /* Check if the expression is a reference to an implicitly pure function.  */
    3268              : 
    3269              : bool
    3270        38721 : gfc_implicit_pure_function (gfc_expr *e)
    3271              : {
    3272        38721 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3273        38721 :   if (comp)
    3274          463 :     return gfc_implicit_pure (comp->ts.interface);
    3275        38258 :   else if (e->value.function.esym)
    3276        32848 :     return gfc_implicit_pure (e->value.function.esym);
    3277              :   else
    3278              :     return 0;
    3279              : }
    3280              : 
    3281              : 
    3282              : static bool
    3283          981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
    3284              :                  int *f ATTRIBUTE_UNUSED)
    3285              : {
    3286          981 :   const char *name;
    3287              : 
    3288              :   /* Don't bother recursing into other statement functions
    3289              :      since they will be checked individually for purity.  */
    3290          981 :   if (e->expr_type != EXPR_FUNCTION
    3291          343 :         || !e->symtree
    3292          343 :         || e->symtree->n.sym == sym
    3293           20 :         || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3294              :     return false;
    3295              : 
    3296           19 :   return !gfc_pure_function (e, &name);
    3297              : }
    3298              : 
    3299              : 
    3300              : static bool
    3301          305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
    3302              : {
    3303          305 :   return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
    3304              : }
    3305              : 
    3306              : 
    3307              : /* Check if an impure function is allowed in the current context. */
    3308              : 
    3309       246524 : static bool check_pure_function (gfc_expr *e)
    3310              : {
    3311       246524 :   const char *name = NULL;
    3312       246524 :   code_stack *stack;
    3313       246524 :   bool saw_block = false;
    3314              : 
    3315              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3316              :      gfc_do_concurrent_flag = 0 when the check for an impure function
    3317              :      occurs.  Check the stack to see if the source code has a nested
    3318              :      BLOCK construct.  */
    3319              : 
    3320       570781 :   for (stack = cs_base; stack; stack = stack->prev)
    3321              :     {
    3322       324259 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3323              :         {
    3324         7637 :           saw_block = true;
    3325         7637 :           continue;
    3326              :         }
    3327              : 
    3328         5431 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3329              :         {
    3330           10 :           bool is_pure;
    3331       324257 :           is_pure = (e->value.function.isym
    3332            9 :                      && (e->value.function.isym->pure
    3333            1 :                          || e->value.function.isym->elemental))
    3334           11 :                     || (e->value.function.esym
    3335            1 :                         && (e->value.function.esym->attr.pure
    3336            1 :                             || e->value.function.esym->attr.elemental));
    3337            2 :           if (!is_pure)
    3338              :             {
    3339            2 :               gfc_error ("Reference to impure function at %L inside a "
    3340              :                          "DO CONCURRENT", &e->where);
    3341            2 :               return false;
    3342              :             }
    3343              :         }
    3344              :     }
    3345              : 
    3346       246522 :   if (!gfc_pure_function (e, &name) && name)
    3347              :     {
    3348        37424 :       if (forall_flag)
    3349              :         {
    3350            4 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3351              :                      "FORALL %s", name, &e->where,
    3352              :                      forall_flag == 2 ? "mask" : "block");
    3353            4 :           return false;
    3354              :         }
    3355        37420 :       else if (gfc_do_concurrent_flag)
    3356              :         {
    3357            2 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3358              :                      "DO CONCURRENT %s", name, &e->where,
    3359              :                      gfc_do_concurrent_flag == 2 ? "mask" : "block");
    3360            2 :           return false;
    3361              :         }
    3362        37418 :       else if (gfc_pure (NULL))
    3363              :         {
    3364            5 :           gfc_error ("Reference to impure function %qs at %L "
    3365              :                      "within a PURE procedure", name, &e->where);
    3366            5 :           return false;
    3367              :         }
    3368        37413 :       if (!gfc_implicit_pure_function (e))
    3369        30881 :         gfc_unset_implicit_pure (NULL);
    3370              :     }
    3371              :   return true;
    3372              : }
    3373              : 
    3374              : 
    3375              : /* Update current procedure's array_outer_dependency flag, considering
    3376              :    a call to procedure SYM.  */
    3377              : 
    3378              : static void
    3379       134158 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
    3380              : {
    3381              :   /* Check to see if this is a sibling function that has not yet
    3382              :      been resolved.  */
    3383       134158 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3384       252282 :   for (; sibling; sibling = sibling->sibling)
    3385              :     {
    3386       125304 :       if (sibling->proc_name == sym)
    3387              :         {
    3388         7180 :           gfc_resolve (sibling);
    3389         7180 :           break;
    3390              :         }
    3391              :     }
    3392              : 
    3393              :   /* If SYM has references to outer arrays, so has the procedure calling
    3394              :      SYM.  If SYM is a procedure pointer, we can assume the worst.  */
    3395       134158 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3396        68789 :       && gfc_current_ns->proc_name)
    3397        68745 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3398       134158 : }
    3399              : 
    3400              : 
    3401              : /* Resolve a function call, which means resolving the arguments, then figuring
    3402              :    out which entity the name refers to.  */
    3403              : 
    3404              : static bool
    3405       347939 : resolve_function (gfc_expr *expr)
    3406              : {
    3407       347939 :   gfc_actual_arglist *arg;
    3408       347939 :   gfc_symbol *sym;
    3409       347939 :   bool t;
    3410       347939 :   int temp;
    3411       347939 :   procedure_type p = PROC_INTRINSIC;
    3412       347939 :   bool no_formal_args;
    3413              : 
    3414       347939 :   sym = NULL;
    3415       347939 :   if (expr->symtree)
    3416       347583 :     sym = expr->symtree->n.sym;
    3417              : 
    3418              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3419       347939 :   if (gfc_is_proc_ptr_comp (expr))
    3420              :     return true;
    3421              : 
    3422              :   /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
    3423              :      another caf_get.  */
    3424       347523 :   if (sym && sym->attr.intrinsic
    3425         8755 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3426         8755 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3427              :     return true;
    3428              : 
    3429       347523 :   if (expr->ref)
    3430              :     {
    3431            1 :       gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
    3432              :                  &expr->where);
    3433            1 :       return false;
    3434              :     }
    3435              : 
    3436       347166 :   if (sym && sym->attr.intrinsic
    3437       356277 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3438              :     return false;
    3439              : 
    3440       347522 :   if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
    3441              :     {
    3442            4 :       gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
    3443            4 :       return false;
    3444              :     }
    3445              : 
    3446              :   /* If this is a deferred TBP with an abstract interface (which may
    3447              :      of course be referenced), expr->value.function.esym will be set.  */
    3448       347162 :   if (sym && sym->attr.abstract && !expr->value.function.esym)
    3449              :     {
    3450            1 :       gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3451              :                  sym->name, &expr->where);
    3452            1 :       return false;
    3453              :     }
    3454              : 
    3455              :   /* If this is a deferred TBP with an abstract interface, its result
    3456              :      cannot be an assumed length character (F2003: C418).  */
    3457       347161 :   if (sym && sym->attr.abstract && sym->attr.function
    3458          192 :       && sym->result->ts.u.cl
    3459          158 :       && sym->result->ts.u.cl->length == NULL
    3460            2 :       && !sym->result->ts.deferred)
    3461              :     {
    3462            1 :       gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
    3463              :                  "character length result (F2008: C418)", sym->name,
    3464              :                  &sym->declared_at);
    3465            1 :       return false;
    3466              :     }
    3467              : 
    3468              :   /* Switch off assumed size checking and do this again for certain kinds
    3469              :      of procedure, once the procedure itself is resolved.  */
    3470       347516 :   need_full_assumed_size++;
    3471              : 
    3472       347516 :   if (expr->symtree && expr->symtree->n.sym)
    3473       347160 :     p = expr->symtree->n.sym->attr.proc;
    3474              : 
    3475       347516 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3476         1179 :     inquiry_argument = true;
    3477       347160 :   no_formal_args = sym && is_external_proc (sym)
    3478       361512 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3479              : 
    3480       347516 :   if (!resolve_actual_arglist (expr->value.function.actual,
    3481              :                                p, no_formal_args))
    3482              :     {
    3483           67 :       inquiry_argument = false;
    3484           67 :       return false;
    3485              :     }
    3486              : 
    3487       347449 :   inquiry_argument = false;
    3488              : 
    3489              :   /* Resume assumed_size checking.  */
    3490       347449 :   need_full_assumed_size--;
    3491              : 
    3492              :   /* If the procedure is external, check for usage.  */
    3493       347449 :   if (sym && is_external_proc (sym))
    3494        13976 :     resolve_global_procedure (sym, &expr->where, 0);
    3495              : 
    3496       347449 :   if (sym && sym->ts.type == BT_CHARACTER
    3497         3365 :       && sym->ts.u.cl
    3498         3271 :       && sym->ts.u.cl->length == NULL
    3499          683 :       && !sym->attr.dummy
    3500          676 :       && !sym->ts.deferred
    3501            2 :       && expr->value.function.esym == NULL
    3502            2 :       && !sym->attr.contained)
    3503              :     {
    3504              :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3505            1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3506              :                  "be used at %L since it is not a dummy argument",
    3507              :                  sym->name, &expr->where);
    3508            1 :       return false;
    3509              :     }
    3510              : 
    3511              :   /* Add and check formal interface when -fc-prototypes-external is in
    3512              :      force, see comment in resolve_call().  */
    3513              : 
    3514       347448 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3515           18 :       && sym->attr.external)
    3516              :     {
    3517           18 :       if (sym->formal)
    3518              :         {
    3519            6 :           bool conflict;
    3520            6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3521              :                                                  sym->formal, 0, 0, 0, NULL);
    3522            6 :           if (conflict)
    3523              :             {
    3524            6 :               sym->ext_dummy_arglist_mismatch = 1;
    3525            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3526              :                            "Different argument lists in external dummy "
    3527              :                            "function %s at %L and %L", sym->name,
    3528              :                            &expr->where, &sym->other_loc);
    3529              :             }
    3530              :         }
    3531           12 :       else if (!sym->formal_resolved)
    3532              :         {
    3533            6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3534            6 :           sym->other_loc = expr->where;
    3535              :         }
    3536              :     }
    3537              :   /* See if function is already resolved.  */
    3538              : 
    3539       347448 :   if (expr->value.function.name != NULL
    3540       335435 :       || expr->value.function.isym != NULL)
    3541              :     {
    3542        12857 :       if (expr->ts.type == BT_UNKNOWN)
    3543            3 :         expr->ts = sym->ts;
    3544              :       t = true;
    3545              :     }
    3546              :   else
    3547              :     {
    3548              :       /* Apply the rules of section 14.1.2.  */
    3549              : 
    3550       334591 :       switch (procedure_kind (sym))
    3551              :         {
    3552        27974 :         case PTYPE_GENERIC:
    3553        27974 :           t = resolve_generic_f (expr);
    3554        27974 :           break;
    3555              : 
    3556        28342 :         case PTYPE_SPECIFIC:
    3557        28342 :           t = resolve_specific_f (expr);
    3558        28342 :           break;
    3559              : 
    3560       278275 :         case PTYPE_UNKNOWN:
    3561       278275 :           t = resolve_unknown_f (expr);
    3562       278275 :           break;
    3563              : 
    3564              :         default:
    3565              :           gfc_internal_error ("resolve_function(): bad function type");
    3566              :         }
    3567              :     }
    3568              : 
    3569              :   /* If the expression is still a function (it might have simplified),
    3570              :      then we check to see if we are calling an elemental function.  */
    3571              : 
    3572       347448 :   if (expr->expr_type != EXPR_FUNCTION)
    3573              :     return t;
    3574              : 
    3575              :   /* Walk the argument list looking for invalid BOZ.  */
    3576       745415 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3577       499347 :     if (arg->expr && arg->expr->ts.type == BT_BOZ)
    3578              :       {
    3579            5 :         gfc_error ("A BOZ literal constant at %L cannot appear as an "
    3580              :                    "actual argument in a function reference",
    3581              :                    &arg->expr->where);
    3582            5 :         return false;
    3583              :       }
    3584              : 
    3585       246068 :   temp = need_full_assumed_size;
    3586       246068 :   need_full_assumed_size = 0;
    3587              : 
    3588       246068 :   if (!resolve_elemental_actual (expr, NULL))
    3589              :     return false;
    3590              : 
    3591       246065 :   if (omp_workshare_flag
    3592           32 :       && expr->value.function.esym
    3593       246070 :       && ! gfc_elemental (expr->value.function.esym))
    3594              :     {
    3595            4 :       gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
    3596            4 :                  "in WORKSHARE construct", expr->value.function.esym->name,
    3597              :                  &expr->where);
    3598            4 :       t = false;
    3599              :     }
    3600              : 
    3601              : #define GENERIC_ID expr->value.function.isym->id
    3602       246061 :   else if (expr->value.function.actual != NULL
    3603       237846 :            && expr->value.function.isym != NULL
    3604       192423 :            && GENERIC_ID != GFC_ISYM_LBOUND
    3605              :            && GENERIC_ID != GFC_ISYM_LCOBOUND
    3606              :            && GENERIC_ID != GFC_ISYM_UCOBOUND
    3607              :            && GENERIC_ID != GFC_ISYM_LEN
    3608              :            && GENERIC_ID != GFC_ISYM_LOC
    3609              :            && GENERIC_ID != GFC_ISYM_C_LOC
    3610              :            && GENERIC_ID != GFC_ISYM_PRESENT)
    3611              :     {
    3612              :       /* Array intrinsics must also have the last upper bound of an
    3613              :          assumed size array argument.  UBOUND and SIZE have to be
    3614              :          excluded from the check if the second argument is anything
    3615              :          than a constant.  */
    3616              : 
    3617       541266 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3618              :         {
    3619       374880 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3620        46043 :               && arg == expr->value.function.actual
    3621        16959 :               && arg->next != NULL && arg->next->expr)
    3622              :             {
    3623         8351 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3624              :                 break;
    3625              : 
    3626         8127 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3627              :                 break;
    3628              : 
    3629         8127 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3630         8127 :                         < arg->expr->rank)
    3631              :                 break;
    3632              :             }
    3633              : 
    3634       372465 :           if (arg->expr != NULL
    3635       248300 :               && arg->expr->rank > 0
    3636       492267 :               && resolve_assumed_size_actual (arg->expr))
    3637              :             return false;
    3638              :         }
    3639              :     }
    3640              : #undef GENERIC_ID
    3641              : 
    3642       246062 :   need_full_assumed_size = temp;
    3643              : 
    3644       246062 :   if (!check_pure_function(expr))
    3645           12 :     t = false;
    3646              : 
    3647              :   /* Functions without the RECURSIVE attribution are not allowed to
    3648              :    * call themselves.  */
    3649       246062 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3650              :     {
    3651        52132 :       gfc_symbol *esym;
    3652        52132 :       esym = expr->value.function.esym;
    3653              : 
    3654        52132 :       if (is_illegal_recursion (esym, gfc_current_ns))
    3655              :       {
    3656            5 :         if (esym->attr.entry && esym->ns->entries)
    3657            3 :           gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
    3658              :                      " function %qs is not RECURSIVE",
    3659            3 :                      esym->name, &expr->where, esym->ns->entries->sym->name);
    3660              :         else
    3661            2 :           gfc_error ("Function %qs at %L cannot be called recursively, as it"
    3662              :                      " is not RECURSIVE", esym->name, &expr->where);
    3663              : 
    3664              :         t = false;
    3665              :       }
    3666              :     }
    3667              : 
    3668              :   /* Character lengths of use associated functions may contains references to
    3669              :      symbols not referenced from the current program unit otherwise.  Make sure
    3670              :      those symbols are marked as referenced.  */
    3671              : 
    3672       246062 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3673         3469 :       && expr->value.function.esym->attr.use_assoc)
    3674              :     {
    3675         1256 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3676              :     }
    3677              : 
    3678              :   /* Make sure that the expression has a typespec that works.  */
    3679       246062 :   if (expr->ts.type == BT_UNKNOWN)
    3680              :     {
    3681          922 :       if (expr->symtree->n.sym->result
    3682          913 :             && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
    3683          561 :             && !expr->symtree->n.sym->result->attr.proc_pointer)
    3684          561 :         expr->ts = expr->symtree->n.sym->result->ts;
    3685              :     }
    3686              : 
    3687              :   /* These derived types with an incomplete namespace, arising from use
    3688              :      association, cause gfc_get_derived_vtab to segfault. If the function
    3689              :      namespace does not suffice, something is badly wrong.  */
    3690       246062 :   if (expr->ts.type == BT_DERIVED
    3691         9592 :       && !expr->ts.u.derived->ns->proc_name)
    3692              :     {
    3693            3 :       gfc_symbol *der;
    3694            3 :       gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
    3695            3 :       if (der)
    3696              :         {
    3697            3 :           expr->ts.u.derived->refs--;
    3698            3 :           expr->ts.u.derived = der;
    3699            3 :           der->refs++;
    3700              :         }
    3701              :       else
    3702            0 :         expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
    3703              :     }
    3704              : 
    3705       246062 :   if (!expr->ref && !expr->value.function.isym)
    3706              :     {
    3707        53518 :       if (expr->value.function.esym)
    3708        52444 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3709              :       else
    3710         1074 :         update_current_proc_array_outer_dependency (sym);
    3711              :     }
    3712       192544 :   else if (expr->ref)
    3713              :     /* typebound procedure: Assume the worst.  */
    3714            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3715              : 
    3716       246062 :   if (expr->value.function.esym
    3717        52444 :       && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    3718           26 :     gfc_warning (OPT_Wdeprecated_declarations,
    3719              :                  "Using function %qs at %L is deprecated",
    3720              :                  sym->name, &expr->where);
    3721              : 
    3722              :   /* Check an external function supplied as a dummy argument has an external
    3723              :      attribute when a program unit uses 'implicit none (external)'.  */
    3724       246062 :   if (expr->expr_type == EXPR_FUNCTION
    3725       246062 :       && expr->symtree
    3726       245706 :       && expr->symtree->n.sym->attr.dummy
    3727          574 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3728       246063 :       && !gfc_is_intrinsic(expr->symtree->n.sym, 0, expr->where))
    3729              :     {
    3730            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    3731              :                  sym->name, &expr->where);
    3732            1 :       return false;
    3733              :     }
    3734              : 
    3735              :   return t;
    3736              : }
    3737              : 
    3738              : 
    3739              : /************* Subroutine resolution *************/
    3740              : 
    3741              : static bool
    3742        78093 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3743              : {
    3744        78093 :   code_stack *stack;
    3745        78093 :   bool saw_block = false;
    3746              : 
    3747        78093 :   if (gfc_pure (sym))
    3748              :     return true;
    3749              : 
    3750              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3751              :      gfc_do_concurrent_flag = 0 when the check for an impure subroutine
    3752              :      occurs.  Walk up the stack to see if the source code has a nested
    3753              :      construct.  */
    3754              : 
    3755       161114 :   for (stack = cs_base; stack; stack = stack->prev)
    3756              :     {
    3757        88672 :       if (stack->current->op == EXEC_BLOCK)
    3758              :         {
    3759         1930 :           saw_block = true;
    3760         1930 :           continue;
    3761              :         }
    3762              : 
    3763        86742 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3764              :         {
    3765              : 
    3766            2 :           bool is_pure = true;
    3767        88672 :           is_pure = sym->attr.pure || sym->attr.elemental;
    3768              : 
    3769            2 :           if (!is_pure)
    3770              :             {
    3771            2 :               gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
    3772              :                          "is not PURE", loc);
    3773            2 :               return false;
    3774              :             }
    3775              :         }
    3776              :     }
    3777              : 
    3778        72442 :   if (forall_flag)
    3779              :     {
    3780            0 :       gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
    3781              :                  name, loc);
    3782            0 :       return false;
    3783              :     }
    3784        72442 :   else if (gfc_do_concurrent_flag)
    3785              :     {
    3786            6 :       gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
    3787              :                  "PURE", name, loc);
    3788            6 :       return false;
    3789              :     }
    3790        72436 :   else if (gfc_pure (NULL))
    3791              :     {
    3792            4 :       gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
    3793            4 :       return false;
    3794              :     }
    3795              : 
    3796        72432 :   gfc_unset_implicit_pure (NULL);
    3797        72432 :   return true;
    3798              : }
    3799              : 
    3800              : 
    3801              : static match
    3802         2883 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3803              : {
    3804         2883 :   gfc_symbol *s;
    3805              : 
    3806         2883 :   if (sym->attr.generic)
    3807              :     {
    3808         2882 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3809         2882 :       if (s != NULL)
    3810              :         {
    3811         2873 :           c->resolved_sym = s;
    3812         2873 :           if (!pure_subroutine (s, s->name, &c->loc))
    3813              :             return MATCH_ERROR;
    3814         2873 :           return MATCH_YES;
    3815              :         }
    3816              : 
    3817              :       /* TODO: Need to search for elemental references in generic interface.  */
    3818              :     }
    3819              : 
    3820           10 :   if (sym->attr.intrinsic)
    3821            1 :     return gfc_intrinsic_sub_interface (c, 0);
    3822              : 
    3823              :   return MATCH_NO;
    3824              : }
    3825              : 
    3826              : 
    3827              : static bool
    3828         2881 : resolve_generic_s (gfc_code *c)
    3829              : {
    3830         2881 :   gfc_symbol *sym;
    3831         2881 :   match m;
    3832              : 
    3833         2881 :   sym = c->symtree->n.sym;
    3834              : 
    3835         2883 :   for (;;)
    3836              :     {
    3837         2883 :       m = resolve_generic_s0 (c, sym);
    3838         2883 :       if (m == MATCH_YES)
    3839              :         return true;
    3840            9 :       else if (m == MATCH_ERROR)
    3841              :         return false;
    3842              : 
    3843            9 : generic:
    3844            9 :       if (sym->ns->parent == NULL)
    3845              :         break;
    3846            3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3847              : 
    3848            3 :       if (sym == NULL)
    3849              :         break;
    3850            2 :       if (!generic_sym (sym))
    3851            0 :         goto generic;
    3852              :     }
    3853              : 
    3854              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3855              :      that possesses a matching interface.  14.1.2.4  */
    3856            7 :   sym = c->symtree->n.sym;
    3857              : 
    3858            7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3859              :     {
    3860            4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3861              :                  sym->name, &c->loc);
    3862            4 :       return false;
    3863              :     }
    3864              : 
    3865            3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3866            3 :   if (m == MATCH_YES)
    3867              :     return true;
    3868            1 :   if (m == MATCH_NO)
    3869            1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3870              :                "intrinsic subroutine interface", sym->name, &c->loc);
    3871              : 
    3872              :   return false;
    3873              : }
    3874              : 
    3875              : 
    3876              : /* Resolve a subroutine call known to be specific.  */
    3877              : 
    3878              : static match
    3879        63475 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3880              : {
    3881        63475 :   match m;
    3882              : 
    3883        63475 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3884              :     {
    3885         5723 :       if (sym->attr.dummy)
    3886              :         {
    3887          263 :           sym->attr.proc = PROC_DUMMY;
    3888          263 :           goto found;
    3889              :         }
    3890              : 
    3891         5460 :       sym->attr.proc = PROC_EXTERNAL;
    3892         5460 :       goto found;
    3893              :     }
    3894              : 
    3895        57752 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3896        57746 :     goto found;
    3897              : 
    3898            6 :   if (sym->attr.intrinsic)
    3899              :     {
    3900            0 :       m = gfc_intrinsic_sub_interface (c, 1);
    3901            0 :       if (m == MATCH_YES)
    3902              :         return MATCH_YES;
    3903            0 :       if (m == MATCH_NO)
    3904            0 :         gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
    3905              :                    "with an intrinsic", sym->name, &c->loc);
    3906              : 
    3907              :       return MATCH_ERROR;
    3908              :     }
    3909              : 
    3910              :   return MATCH_NO;
    3911              : 
    3912        63469 : found:
    3913        63469 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3914              : 
    3915        63469 :   c->resolved_sym = sym;
    3916        63469 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3917            7 :     return MATCH_ERROR;
    3918              : 
    3919              :   return MATCH_YES;
    3920              : }
    3921              : 
    3922              : 
    3923              : static bool
    3924        63469 : resolve_specific_s (gfc_code *c)
    3925              : {
    3926        63469 :   gfc_symbol *sym;
    3927        63469 :   match m;
    3928              : 
    3929        63469 :   sym = c->symtree->n.sym;
    3930              : 
    3931        63475 :   for (;;)
    3932              :     {
    3933        63475 :       m = resolve_specific_s0 (c, sym);
    3934        63475 :       if (m == MATCH_YES)
    3935              :         return true;
    3936           13 :       if (m == MATCH_ERROR)
    3937              :         return false;
    3938              : 
    3939            6 :       if (sym->ns->parent == NULL)
    3940              :         break;
    3941              : 
    3942            6 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3943              : 
    3944            6 :       if (sym == NULL)
    3945              :         break;
    3946              :     }
    3947              : 
    3948            0 :   sym = c->symtree->n.sym;
    3949            0 :   gfc_error ("Unable to resolve the specific subroutine %qs at %L",
    3950              :              sym->name, &c->loc);
    3951              : 
    3952            0 :   return false;
    3953              : }
    3954              : 
    3955              : 
    3956              : /* Resolve a subroutine call not known to be generic nor specific.  */
    3957              : 
    3958              : static bool
    3959        15941 : resolve_unknown_s (gfc_code *c)
    3960              : {
    3961        15941 :   gfc_symbol *sym;
    3962              : 
    3963        15941 :   sym = c->symtree->n.sym;
    3964              : 
    3965        15941 :   if (sym->attr.dummy)
    3966              :     {
    3967           26 :       sym->attr.proc = PROC_DUMMY;
    3968           26 :       goto found;
    3969              :     }
    3970              : 
    3971              :   /* See if we have an intrinsic function reference.  */
    3972              : 
    3973        15915 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3974              :     {
    3975         4313 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3976              :         return true;
    3977          319 :       return false;
    3978              :     }
    3979              : 
    3980              :   /* The reference is to an external name.  */
    3981              : 
    3982        11602 : found:
    3983        11628 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3984              : 
    3985        11628 :   c->resolved_sym = sym;
    3986              : 
    3987        11628 :   return pure_subroutine (sym, sym->name, &c->loc);
    3988              : }
    3989              : 
    3990              : 
    3991              : 
    3992              : static bool
    3993          805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
    3994              :                          gfc_code *c, gfc_namespace *ns)
    3995              : {
    3996          805 :   locus *here;
    3997              : 
    3998              :   /* If the type has been imported then its vtype functions are OK.  */
    3999          805 :   if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
    4000              :     return true;
    4001              : 
    4002              :   if (e)
    4003          791 :     here = &e->where;
    4004              :   else
    4005            7 :     here = &c->loc;
    4006              : 
    4007          798 :   if (s && !s->import_only)
    4008          705 :     s = gfc_find_symtree (ns->sym_root, sym->name);
    4009              : 
    4010          798 :   if (ns->import_state == IMPORT_ONLY
    4011           75 :       && sym->ns != ns
    4012           58 :       && (!s || !s->import_only))
    4013              :     {
    4014           21 :       gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
    4015              :                  "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
    4016           21 :       return false;
    4017              :     }
    4018          777 :   else if (ns->import_state == IMPORT_NONE
    4019           27 :            && sym->ns != ns)
    4020              :     {
    4021           12 :       gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
    4022              :                  "has IMPORT, NONE", sym->name, here);
    4023           12 :       return false;
    4024              :     }
    4025              :   return true;
    4026              : }
    4027              : 
    4028              : 
    4029              : static bool
    4030         7354 : check_import_status (gfc_expr *e)
    4031              : {
    4032         7354 :   gfc_symtree *st;
    4033         7354 :   gfc_ref *ref;
    4034         7354 :   gfc_symbol *sym, *der;
    4035         7354 :   gfc_namespace *ns = gfc_current_ns;
    4036              : 
    4037         7354 :   switch (e->expr_type)
    4038              :     {
    4039          727 :       case EXPR_VARIABLE:
    4040          727 :       case EXPR_FUNCTION:
    4041          727 :       case EXPR_SUBSTRING:
    4042          727 :         sym = e->symtree ? e->symtree->n.sym : NULL;
    4043              : 
    4044              :         /* Check the symbol itself.  */
    4045          727 :         if (sym
    4046          727 :             && !(ns->proc_name
    4047              :                  && (sym == ns->proc_name))
    4048         1450 :             && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
    4049              :           return false;
    4050              : 
    4051              :         /* Check the declared derived type.  */
    4052          717 :         if (sym->ts.type == BT_DERIVED)
    4053              :           {
    4054           16 :             der = sym->ts.u.derived;
    4055           16 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4056              : 
    4057           16 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4058              :               return false;
    4059              :           }
    4060          701 :         else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
    4061              :           {
    4062           44 :             der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
    4063              :                                    : sym->ts.u.derived;
    4064           44 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4065              : 
    4066           44 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4067              :               return false;
    4068              :           }
    4069              : 
    4070              :         /* Check the declared derived types of component references.  */
    4071          724 :         for (ref = e->ref; ref; ref = ref->next)
    4072           20 :           if (ref->type == REF_COMPONENT)
    4073              :             {
    4074           19 :               gfc_component *c = ref->u.c.component;
    4075           19 :               if (c->ts.type == BT_DERIVED)
    4076              :                 {
    4077            7 :                   der = c->ts.u.derived;
    4078            7 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4079            7 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4080              :                     return false;
    4081              :                 }
    4082           12 :               else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
    4083              :                 {
    4084            0 :                   der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
    4085              :                                        : c->ts.u.derived;
    4086            0 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4087            0 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4088              :                     return false;
    4089              :                 }
    4090              :             }
    4091              : 
    4092              :         break;
    4093              : 
    4094            8 :       case EXPR_ARRAY:
    4095            8 :       case EXPR_STRUCTURE:
    4096              :         /* Check the declared derived type.  */
    4097            8 :         if (e->ts.type == BT_DERIVED)
    4098              :           {
    4099            8 :             der = e->ts.u.derived;
    4100            8 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4101              : 
    4102            8 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4103              :               return false;
    4104              :           }
    4105            0 :         else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
    4106              :           {
    4107            0 :             der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
    4108              :                                    : e->ts.u.derived;
    4109            0 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4110              : 
    4111            0 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4112              :               return false;
    4113              :           }
    4114              : 
    4115              :         break;
    4116              : 
    4117              : /* Either not applicable or resolved away
    4118              :       case EXPR_OP:
    4119              :       case EXPR_UNKNOWN:
    4120              :       case EXPR_CONSTANT:
    4121              :       case EXPR_NULL:
    4122              :       case EXPR_COMPCALL:
    4123              :       case EXPR_PPC: */
    4124              : 
    4125              :       default:
    4126              :         break;
    4127              :     }
    4128              : 
    4129              :   return true;
    4130              : }
    4131              : 
    4132              : 
    4133              : /* If an elemental call has an INTENT_IN argument that has a dependency on an
    4134              :    argument which is not INTENT_IN and requires a temporary, build a temporary
    4135              :    for the INTENT_IN actual argument as well.  */
    4136              : 
    4137              : static void
    4138              : add_temp_assign_before_call (gfc_code *, gfc_namespace *, gfc_expr **);
    4139              : 
    4140              : static void
    4141         5257 : resolve_elemental_dependencies (gfc_code *c)
    4142              : {
    4143         5257 :   gfc_actual_arglist *arg1 = c->ext.actual;
    4144         5257 :   gfc_actual_arglist *arg2 = NULL;
    4145         5257 :   gfc_formal_arglist *formal1 = c->resolved_sym->formal;
    4146         5257 :   gfc_formal_arglist *formal2 = NULL;
    4147         5257 :   gfc_expr *expr1;
    4148         5257 :   gfc_expr **expr2;
    4149              : 
    4150        16645 :   for (; arg1 && formal1; arg1 = arg1->next, formal1 = formal1->next)
    4151              :     {
    4152        11388 :       if (formal1->sym
    4153        11388 :           && (formal1->sym->attr.intent == INTENT_IN
    4154         3536 :               || formal1->sym->attr.value))
    4155         8110 :         continue;
    4156              : 
    4157         3278 :       if (!arg1->expr || arg1->expr->expr_type != EXPR_VARIABLE)
    4158            0 :         continue;
    4159              : 
    4160         3278 :       arg2 = c->ext.actual;
    4161         3278 :       formal2 = c->resolved_sym->formal;
    4162        10696 :       for (; arg2 && formal2; arg2 = arg2->next, formal2 = formal2->next)
    4163              :         {
    4164         7418 :           if (arg2 == arg1 || !arg2->expr
    4165         4128 :               || !(formal2->sym && formal2->sym->attr.intent == INTENT_IN))
    4166         3304 :             continue;
    4167              : 
    4168         4114 :           expr1 = arg1->expr;
    4169         4114 :           expr2 = &arg2->expr;
    4170              : 
    4171              :           /* If the arg1 has something horrible like a vector index and
    4172              :              there is a dependency between arg1 and arg2, build a
    4173              :              temporary from arg2, assign the arg2 to it and use the
    4174              :              temporary in the call expression.  */
    4175         2009 :           if (expr1->rank && gfc_ref_needs_temporary_p (expr1->ref)
    4176         4234 :               && gfc_check_dependency (expr1, *expr2, false))
    4177           36 :             add_temp_assign_before_call (c, gfc_current_ns, expr2);
    4178              :         }
    4179              :     }
    4180         5257 : }
    4181              : 
    4182              : /* Resolve a subroutine call.  Although it was tempting to use the same code
    4183              :    for functions, subroutines and functions are stored differently and this
    4184              :    makes things awkward.  */
    4185              : 
    4186              : 
    4187              : static bool
    4188        82436 : resolve_call (gfc_code *c)
    4189              : {
    4190        82436 :   bool t;
    4191        82436 :   procedure_type ptype = PROC_INTRINSIC;
    4192        82436 :   gfc_symbol *csym, *sym;
    4193        82436 :   bool no_formal_args;
    4194              : 
    4195        82436 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4196              : 
    4197        82436 :   if (csym && csym->ts.type != BT_UNKNOWN)
    4198              :     {
    4199            4 :       gfc_error ("%qs at %L has a type, which is not consistent with "
    4200              :                  "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
    4201            4 :       return false;
    4202              :     }
    4203              : 
    4204        82432 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    4205              :     {
    4206        17556 :       gfc_symtree *st;
    4207        17556 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    4208        17556 :       sym = st ? st->n.sym : NULL;
    4209        17556 :       if (sym && csym != sym
    4210            3 :               && sym->ns == gfc_current_ns
    4211            3 :               && sym->attr.flavor == FL_PROCEDURE
    4212            3 :               && sym->attr.contained)
    4213              :         {
    4214            3 :           sym->refs++;
    4215            3 :           if (csym->attr.generic)
    4216            2 :             c->symtree->n.sym = sym;
    4217              :           else
    4218            1 :             c->symtree = st;
    4219            3 :           csym = c->symtree->n.sym;
    4220              :         }
    4221              :     }
    4222              : 
    4223              :   /* If this ia a deferred TBP, c->expr1 will be set.  */
    4224        82432 :   if (!c->expr1 && csym)
    4225              :     {
    4226        80685 :       if (csym->attr.abstract)
    4227              :         {
    4228            1 :           gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    4229              :                     csym->name, &c->loc);
    4230            1 :           return false;
    4231              :         }
    4232              : 
    4233              :       /* Subroutines without the RECURSIVE attribution are not allowed to
    4234              :          call themselves.  */
    4235        80684 :       if (is_illegal_recursion (csym, gfc_current_ns))
    4236              :         {
    4237            4 :           if (csym->attr.entry && csym->ns->entries)
    4238            2 :             gfc_error ("ENTRY %qs at %L cannot be called recursively, "
    4239              :                        "as subroutine %qs is not RECURSIVE",
    4240            2 :                        csym->name, &c->loc, csym->ns->entries->sym->name);
    4241              :           else
    4242            2 :             gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
    4243              :                        "as it is not RECURSIVE", csym->name, &c->loc);
    4244              : 
    4245        82431 :           t = false;
    4246              :         }
    4247              :     }
    4248              : 
    4249              :   /* Switch off assumed size checking and do this again for certain kinds
    4250              :      of procedure, once the procedure itself is resolved.  */
    4251        82431 :   need_full_assumed_size++;
    4252              : 
    4253        82431 :   if (csym)
    4254        82431 :     ptype = csym->attr.proc;
    4255              : 
    4256        82431 :   no_formal_args = csym && is_external_proc (csym)
    4257        15728 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4258        82431 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4259              :     return false;
    4260              : 
    4261              :   /* Resume assumed_size checking.  */
    4262        82397 :   need_full_assumed_size--;
    4263              : 
    4264              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4265              :      check for an 'external' attribute.  */
    4266        82397 :   if (csym->ns->has_implicit_none_export
    4267         4486 :       && csym->attr.external == 0 && csym->attr.dummy == 1)
    4268              :     {
    4269            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    4270              :                  csym->name, &c->loc);
    4271            1 :       return false;
    4272              :     }
    4273              : 
    4274              :   /* If external, check for usage.  */
    4275        82396 :   if (csym && is_external_proc (csym))
    4276        15722 :     resolve_global_procedure (csym, &c->loc, 1);
    4277              : 
    4278              :   /* If we have an external dummy argument, we want to write out its arguments
    4279              :      with -fc-prototypes-external.  Code like
    4280              : 
    4281              :      subroutine foo(a,n)
    4282              :        external a
    4283              :        if (n == 1) call a(1)
    4284              :        if (n == 2) call a(2,3)
    4285              :      end subroutine foo
    4286              : 
    4287              :      is actually legal Fortran, but it is not possible to generate a C23-
    4288              :      compliant prototype for this, so we just record the fact here and
    4289              :      handle that during -fc-prototypes-external processing.  */
    4290              : 
    4291        82396 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4292           14 :       && csym->attr.external)
    4293              :     {
    4294           14 :       if (csym->formal)
    4295              :         {
    4296            6 :           bool conflict;
    4297            6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4298              :                                                  0, 0, 0, NULL);
    4299            6 :           if (conflict)
    4300              :             {
    4301            6 :               csym->ext_dummy_arglist_mismatch = 1;
    4302            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4303              :                            "Different argument lists in external dummy "
    4304              :                            "subroutine %s at %L and %L", csym->name,
    4305              :                            &c->loc, &csym->other_loc);
    4306              :             }
    4307              :         }
    4308            8 :       else if (!csym->formal_resolved)
    4309              :         {
    4310            7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4311            7 :           csym->other_loc = c->loc;
    4312              :         }
    4313              :     }
    4314              : 
    4315        82396 :   t = true;
    4316        82396 :   if (c->resolved_sym == NULL)
    4317              :     {
    4318        82291 :       c->resolved_isym = NULL;
    4319        82291 :       switch (procedure_kind (csym))
    4320              :         {
    4321         2881 :         case PTYPE_GENERIC:
    4322         2881 :           t = resolve_generic_s (c);
    4323         2881 :           break;
    4324              : 
    4325        63469 :         case PTYPE_SPECIFIC:
    4326        63469 :           t = resolve_specific_s (c);
    4327        63469 :           break;
    4328              : 
    4329        15941 :         case PTYPE_UNKNOWN:
    4330        15941 :           t = resolve_unknown_s (c);
    4331        15941 :           break;
    4332              : 
    4333              :         default:
    4334              :           gfc_internal_error ("resolve_subroutine(): bad function type");
    4335              :         }
    4336              :     }
    4337              : 
    4338              :   /* Some checks of elemental subroutine actual arguments.  */
    4339        82395 :   if (!resolve_elemental_actual (NULL, c))
    4340              :     return false;
    4341              : 
    4342              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4343        82387 :   if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args
    4344         6206 :       && c->ext.actual && c->ext.actual->next)
    4345         5257 :     resolve_elemental_dependencies (c);
    4346              : 
    4347        82387 :   if (!c->expr1)
    4348        80640 :     update_current_proc_array_outer_dependency (csym);
    4349              :   else
    4350              :     /* Typebound procedure: Assume the worst.  */
    4351         1747 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4352              : 
    4353        82387 :   if (c->resolved_sym
    4354        82064 :       && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    4355           34 :     gfc_warning (OPT_Wdeprecated_declarations,
    4356              :                  "Using subroutine %qs at %L is deprecated",
    4357              :                  c->resolved_sym->name, &c->loc);
    4358              : 
    4359        82387 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4360        82387 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
    4361            2 :       && csym != gfc_current_ns->proc_name)
    4362            1 :     return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
    4363              : 
    4364              :   return t;
    4365              : }
    4366              : 
    4367              : 
    4368              : /* Compare the shapes of two arrays that have non-NULL shapes.  If both
    4369              :    op1->shape and op2->shape are non-NULL return true if their shapes
    4370              :    match.  If both op1->shape and op2->shape are non-NULL return false
    4371              :    if their shapes do not match.  If either op1->shape or op2->shape is
    4372              :    NULL, return true.  */
    4373              : 
    4374              : static bool
    4375        32981 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4376              : {
    4377        32981 :   bool t;
    4378        32981 :   int i;
    4379              : 
    4380        32981 :   t = true;
    4381              : 
    4382        32981 :   if (op1->shape != NULL && op2->shape != NULL)
    4383              :     {
    4384        43368 :       for (i = 0; i < op1->rank; i++)
    4385              :         {
    4386        23124 :           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
    4387              :            {
    4388            3 :              gfc_error ("Shapes for operands at %L and %L are not conformable",
    4389              :                         &op1->where, &op2->where);
    4390            3 :              t = false;
    4391            3 :              break;
    4392              :            }
    4393              :         }
    4394              :     }
    4395              : 
    4396        32981 :   return t;
    4397              : }
    4398              : 
    4399              : /* Convert a logical operator to the corresponding bitwise intrinsic call.
    4400              :    For example A .AND. B becomes IAND(A, B).  */
    4401              : static gfc_expr *
    4402          668 : logical_to_bitwise (gfc_expr *e)
    4403              : {
    4404          668 :   gfc_expr *tmp, *op1, *op2;
    4405          668 :   gfc_isym_id isym;
    4406          668 :   gfc_actual_arglist *args = NULL;
    4407              : 
    4408          668 :   gcc_assert (e->expr_type == EXPR_OP);
    4409              : 
    4410          668 :   isym = GFC_ISYM_NONE;
    4411          668 :   op1 = e->value.op.op1;
    4412          668 :   op2 = e->value.op.op2;
    4413              : 
    4414          668 :   switch (e->value.op.op)
    4415              :     {
    4416              :     case INTRINSIC_NOT:
    4417              :       isym = GFC_ISYM_NOT;
    4418              :       break;
    4419          126 :     case INTRINSIC_AND:
    4420          126 :       isym = GFC_ISYM_IAND;
    4421          126 :       break;
    4422          127 :     case INTRINSIC_OR:
    4423          127 :       isym = GFC_ISYM_IOR;
    4424          127 :       break;
    4425          270 :     case INTRINSIC_NEQV:
    4426          270 :       isym = GFC_ISYM_IEOR;
    4427          270 :       break;
    4428          126 :     case INTRINSIC_EQV:
    4429              :       /* "Bitwise eqv" is just the complement of NEQV === IEOR.
    4430              :          Change the old expression to NEQV, which will get replaced by IEOR,
    4431              :          and wrap it in NOT.  */
    4432          126 :       tmp = gfc_copy_expr (e);
    4433          126 :       tmp->value.op.op = INTRINSIC_NEQV;
    4434          126 :       tmp = logical_to_bitwise (tmp);
    4435          126 :       isym = GFC_ISYM_NOT;
    4436          126 :       op1 = tmp;
    4437          126 :       op2 = NULL;
    4438          126 :       break;
    4439            0 :     default:
    4440            0 :       gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
    4441              :     }
    4442              : 
    4443              :   /* Inherit the original operation's operands as arguments.  */
    4444          668 :   args = gfc_get_actual_arglist ();
    4445          668 :   args->expr = op1;
    4446          668 :   if (op2)
    4447              :     {
    4448          523 :       args->next = gfc_get_actual_arglist ();
    4449          523 :       args->next->expr = op2;
    4450              :     }
    4451              : 
    4452              :   /* Convert the expression to a function call.  */
    4453          668 :   e->expr_type = EXPR_FUNCTION;
    4454          668 :   e->value.function.actual = args;
    4455          668 :   e->value.function.isym = gfc_intrinsic_function_by_id (isym);
    4456          668 :   e->value.function.name = e->value.function.isym->name;
    4457          668 :   e->value.function.esym = NULL;
    4458              : 
    4459              :   /* Make up a pre-resolved function call symtree if we need to.  */
    4460          668 :   if (!e->symtree || !e->symtree->n.sym)
    4461              :     {
    4462          668 :       gfc_symbol *sym;
    4463          668 :       gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
    4464          668 :       sym = e->symtree->n.sym;
    4465          668 :       sym->result = sym;
    4466          668 :       sym->attr.flavor = FL_PROCEDURE;
    4467          668 :       sym->attr.function = 1;
    4468          668 :       sym->attr.elemental = 1;
    4469          668 :       sym->attr.pure = 1;
    4470          668 :       sym->attr.referenced = 1;
    4471          668 :       gfc_intrinsic_symbol (sym);
    4472          668 :       gfc_commit_symbol (sym);
    4473              :     }
    4474              : 
    4475          668 :   args->name = e->value.function.isym->formal->name;
    4476          668 :   if (e->value.function.isym->formal->next)
    4477          523 :     args->next->name = e->value.function.isym->formal->next->name;
    4478              : 
    4479          668 :   return e;
    4480              : }
    4481              : 
    4482              : /* Recursively append candidate UOP to CANDIDATES.  Store the number of
    4483              :    candidates in CANDIDATES_LEN.  */
    4484              : static void
    4485          114 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4486              :                                   char **&candidates,
    4487              :                                   size_t &candidates_len)
    4488              : {
    4489          116 :   gfc_symtree *p;
    4490              : 
    4491          116 :   if (uop == NULL)
    4492              :     return;
    4493              : 
    4494              :   /* Not sure how to properly filter here.  Use all for a start.
    4495              :      n.uop.op is NULL for empty interface operators (is that legal?) disregard
    4496              :      these as i suppose they don't make terribly sense.  */
    4497              : 
    4498          116 :   if (uop->n.uop->op != NULL)
    4499            2 :     vec_push (candidates, candidates_len, uop->name);
    4500              : 
    4501          116 :   p = uop->left;
    4502          116 :   if (p)
    4503           36 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4504              : 
    4505          116 :   p = uop->right;
    4506          116 :   if (p)
    4507              :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4508              : }
    4509              : 
    4510              : /* Lookup user-operator OP fuzzily, taking names in UOP into account.  */
    4511              : 
    4512              : static const char*
    4513           78 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4514              : {
    4515           78 :   char **candidates = NULL;
    4516           78 :   size_t candidates_len = 0;
    4517           78 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4518           78 :   return gfc_closest_fuzzy_match (op, candidates);
    4519              : }
    4520              : 
    4521              : 
    4522              : /* Callback finding an impure function as an operand to an .and. or
    4523              :    .or.  expression.  Remember the last function warned about to
    4524              :    avoid double warnings when recursing.  */
    4525              : 
    4526              : static int
    4527       193652 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4528              :                           void *data)
    4529              : {
    4530       193652 :   gfc_expr *f = *e;
    4531       193652 :   const char *name;
    4532       193652 :   static gfc_expr *last = NULL;
    4533       193652 :   bool *found = (bool *) data;
    4534              : 
    4535       193652 :   if (f->expr_type == EXPR_FUNCTION)
    4536              :     {
    4537        11961 :       *found = 1;
    4538        11961 :       if (f != last && !gfc_pure_function (f, &name)
    4539        13264 :           && !gfc_implicit_pure_function (f))
    4540              :         {
    4541         1164 :           if (name)
    4542         1164 :             gfc_warning (OPT_Wfunction_elimination,
    4543              :                          "Impure function %qs at %L might not be evaluated",
    4544              :                          name, &f->where);
    4545              :           else
    4546            0 :             gfc_warning (OPT_Wfunction_elimination,
    4547              :                          "Impure function at %L might not be evaluated",
    4548              :                          &f->where);
    4549              :         }
    4550        11961 :       last = f;
    4551              :     }
    4552              : 
    4553       193652 :   return 0;
    4554              : }
    4555              : 
    4556              : /* Return true if TYPE is character based, false otherwise.  */
    4557              : 
    4558              : static int
    4559         1373 : is_character_based (bt type)
    4560              : {
    4561         1373 :   return type == BT_CHARACTER || type == BT_HOLLERITH;
    4562              : }
    4563              : 
    4564              : 
    4565              : /* If expression is a hollerith, convert it to character and issue a warning
    4566              :    for the conversion.  */
    4567              : 
    4568              : static void
    4569          408 : convert_hollerith_to_character (gfc_expr *e)
    4570              : {
    4571          408 :   if (e->ts.type == BT_HOLLERITH)
    4572              :     {
    4573          108 :       gfc_typespec t;
    4574          108 :       gfc_clear_ts (&t);
    4575          108 :       t.type = BT_CHARACTER;
    4576          108 :       t.kind = e->ts.kind;
    4577          108 :       gfc_convert_type_warn (e, &t, 2, 1);
    4578              :     }
    4579          408 : }
    4580              : 
    4581              : /* Convert to numeric and issue a warning for the conversion.  */
    4582              : 
    4583              : static void
    4584          240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
    4585              : {
    4586          240 :   gfc_typespec t;
    4587          240 :   gfc_clear_ts (&t);
    4588          240 :   t.type = b->ts.type;
    4589          240 :   t.kind = b->ts.kind;
    4590          240 :   gfc_convert_type_warn (a, &t, 2, 1);
    4591          240 : }
    4592              : 
    4593              : /* Resolve an operator expression node.  This can involve replacing the
    4594              :    operation with a user defined function call.  CHECK_INTERFACES is a
    4595              :    helper macro.  */
    4596              : 
    4597              : #define CHECK_INTERFACES \
    4598              :   { \
    4599              :     match m = gfc_extend_expr (e); \
    4600              :     if (m == MATCH_YES) \
    4601              :       return true; \
    4602              :     if (m == MATCH_ERROR) \
    4603              :       return false; \
    4604              :   }
    4605              : 
    4606              : static bool
    4607       535852 : resolve_operator (gfc_expr *e)
    4608              : {
    4609       535852 :   gfc_expr *op1, *op2;
    4610              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4611       535852 :   bool t = true;
    4612              : 
    4613              :   /* Reduce stacked parentheses to single pair  */
    4614       535852 :   while (e->expr_type == EXPR_OP
    4615       536010 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4616        23568 :          && e->value.op.op1->expr_type == EXPR_OP
    4617       552775 :          && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
    4618              :     {
    4619          158 :       gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
    4620          158 :       gfc_replace_expr (e, tmp);
    4621              :     }
    4622              : 
    4623              :   /* Resolve all subnodes-- give them types.  */
    4624              : 
    4625       535852 :   switch (e->value.op.op)
    4626              :     {
    4627       483601 :     default:
    4628       483601 :       if (!gfc_resolve_expr (e->value.op.op2))
    4629       535852 :         t = false;
    4630              : 
    4631              :     /* Fall through.  */
    4632              : 
    4633       535852 :     case INTRINSIC_NOT:
    4634       535852 :     case INTRINSIC_UPLUS:
    4635       535852 :     case INTRINSIC_UMINUS:
    4636       535852 :     case INTRINSIC_PARENTHESES:
    4637       535852 :       if (!gfc_resolve_expr (e->value.op.op1))
    4638              :         return false;
    4639       535691 :       if (e->value.op.op1
    4640       535682 :           && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
    4641              :         {
    4642            0 :           gfc_error ("BOZ literal constant at %L cannot be an operand of "
    4643            0 :                      "unary operator %qs", &e->value.op.op1->where,
    4644              :                      gfc_op2string (e->value.op.op));
    4645            0 :           return false;
    4646              :         }
    4647       535691 :       if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
    4648            6 :           && e->value.op.op == INTRINSIC_UMINUS)
    4649              :         {
    4650            2 :           gfc_error ("Negation of unsigned expression at %L not permitted ",
    4651              :                      &e->value.op.op1->where);
    4652            2 :           return false;
    4653              :         }
    4654       535689 :       break;
    4655              :     }
    4656              : 
    4657              :   /* Typecheck the new node.  */
    4658              : 
    4659       535689 :   op1 = e->value.op.op1;
    4660       535689 :   op2 = e->value.op.op2;
    4661       535689 :   if (op1 == NULL && op2 == NULL)
    4662              :     return false;
    4663              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4664       535680 :   if (t == false)
    4665              :     return false;
    4666              : 
    4667              :   /* op1 and op2 cannot both be BOZ.  */
    4668       535614 :   if (op1 && op1->ts.type == BT_BOZ
    4669            0 :       && op2 && op2->ts.type == BT_BOZ)
    4670              :     {
    4671            0 :       gfc_error ("Operands at %L and %L cannot appear as operands of "
    4672            0 :                  "binary operator %qs", &op1->where, &op2->where,
    4673              :                  gfc_op2string (e->value.op.op));
    4674            0 :       return false;
    4675              :     }
    4676              : 
    4677       535614 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4678       535612 :       || (op2 && op2->expr_type == EXPR_NULL))
    4679              :     {
    4680            3 :       CHECK_INTERFACES
    4681            3 :       gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
    4682            3 :       return false;
    4683              :     }
    4684              : 
    4685       535611 :   switch (e->value.op.op)
    4686              :     {
    4687         8238 :     case INTRINSIC_UPLUS:
    4688         8238 :     case INTRINSIC_UMINUS:
    4689         8238 :       if (op1->ts.type == BT_INTEGER
    4690              :           || op1->ts.type == BT_REAL
    4691              :           || op1->ts.type == BT_COMPLEX
    4692              :           || op1->ts.type == BT_UNSIGNED)
    4693              :         {
    4694         8169 :           e->ts = op1->ts;
    4695         8169 :           break;
    4696              :         }
    4697              : 
    4698           69 :       CHECK_INTERFACES
    4699           43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4700              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4701           43 :       return false;
    4702              : 
    4703       155951 :     case INTRINSIC_POWER:
    4704       155951 :     case INTRINSIC_PLUS:
    4705       155951 :     case INTRINSIC_MINUS:
    4706       155951 :     case INTRINSIC_TIMES:
    4707       155951 :     case INTRINSIC_DIVIDE:
    4708              : 
    4709              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4710              :              conversion.  */
    4711       155951 :       if (flag_unsigned &&  gfc_invalid_unsigned_ops (op1, op2))
    4712              :         {
    4713            3 :           CHECK_INTERFACES
    4714            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are "
    4715              :                      "%s/%s", gfc_op2string (e->value.op.op), &e->where,
    4716              :                      gfc_typename (op1), gfc_typename (op2));
    4717            3 :           return false;
    4718              :         }
    4719              : 
    4720       155948 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4721              :         {
    4722              :           /* Do not perform conversions if operands are not conformable as
    4723              :              required for the binary intrinsic operators (F2018:10.1.5).
    4724              :              Defer to a possibly overloading user-defined operator.  */
    4725       155494 :           if (!gfc_op_rank_conformable (op1, op2))
    4726              :             {
    4727           36 :               CHECK_INTERFACES
    4728            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4729            0 :                          &op1->where, &op2->where);
    4730            0 :               return false;
    4731              :             }
    4732              : 
    4733       155458 :           gfc_type_convert_binary (e, 1);
    4734       155458 :           break;
    4735              :         }
    4736              : 
    4737          454 :       if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
    4738              :         {
    4739          225 :           CHECK_INTERFACES
    4740            2 :           gfc_error ("Unexpected derived-type entities in binary intrinsic "
    4741              :                      "numeric operator %qs at %L",
    4742              :                      gfc_op2string (e->value.op.op), &e->where);
    4743            2 :           return false;
    4744              :         }
    4745              :       else
    4746              :         {
    4747          229 :           CHECK_INTERFACES
    4748            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
    4749              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4750              :                      gfc_typename (op2));
    4751            3 :           return false;
    4752              :         }
    4753              : 
    4754         2279 :     case INTRINSIC_CONCAT:
    4755         2279 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4756         2254 :           && op1->ts.kind == op2->ts.kind)
    4757              :         {
    4758         2245 :           e->ts.type = BT_CHARACTER;
    4759         2245 :           e->ts.kind = op1->ts.kind;
    4760         2245 :           break;
    4761              :         }
    4762              : 
    4763           34 :       CHECK_INTERFACES
    4764           10 :       gfc_error ("Operands of string concatenation operator at %L are %s/%s",
    4765              :                  &e->where, gfc_typename (op1), gfc_typename (op2));
    4766           10 :       return false;
    4767              : 
    4768        69794 :     case INTRINSIC_AND:
    4769        69794 :     case INTRINSIC_OR:
    4770        69794 :     case INTRINSIC_EQV:
    4771        69794 :     case INTRINSIC_NEQV:
    4772        69794 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4773              :         {
    4774        69243 :           e->ts.type = BT_LOGICAL;
    4775        69243 :           e->ts.kind = gfc_kind_max (op1, op2);
    4776        69243 :           if (op1->ts.kind < e->ts.kind)
    4777          140 :             gfc_convert_type (op1, &e->ts, 2);
    4778        69103 :           else if (op2->ts.kind < e->ts.kind)
    4779          117 :             gfc_convert_type (op2, &e->ts, 2);
    4780              : 
    4781        69243 :           if (flag_frontend_optimize &&
    4782        58176 :             (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
    4783              :             {
    4784              :               /* Warn about short-circuiting
    4785              :                  with impure function as second operand.  */
    4786        52171 :               bool op2_f = false;
    4787        52171 :               gfc_expr_walker (&op2, impure_function_callback, &op2_f);
    4788              :             }
    4789              :           break;
    4790              :         }
    4791              : 
    4792              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4793          551 :       else if (flag_dec
    4794          523 :                && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
    4795              :         {
    4796          523 :           e->ts.type = BT_INTEGER;
    4797          523 :           e->ts.kind = gfc_kind_max (op1, op2);
    4798          523 :           if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
    4799          289 :             gfc_convert_type (op1, &e->ts, 1);
    4800          523 :           if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
    4801          144 :             gfc_convert_type (op2, &e->ts, 1);
    4802          523 :           e = logical_to_bitwise (e);
    4803          523 :           goto simplify_op;
    4804              :         }
    4805              : 
    4806           28 :       CHECK_INTERFACES
    4807           16 :       gfc_error ("Operands of logical operator %qs at %L are %s/%s",
    4808              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4809              :                  gfc_typename (op2));
    4810           16 :       return false;
    4811              : 
    4812        20587 :     case INTRINSIC_NOT:
    4813              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4814        20587 :       if (flag_dec && op1->ts.type == BT_INTEGER)
    4815              :         {
    4816           19 :           e->ts.type = BT_INTEGER;
    4817           19 :           e->ts.kind = op1->ts.kind;
    4818           19 :           e = logical_to_bitwise (e);
    4819           19 :           goto simplify_op;
    4820              :         }
    4821              : 
    4822        20568 :       if (op1->ts.type == BT_LOGICAL)
    4823              :         {
    4824        20562 :           e->ts.type = BT_LOGICAL;
    4825        20562 :           e->ts.kind = op1->ts.kind;
    4826        20562 :           break;
    4827              :         }
    4828              : 
    4829            6 :       CHECK_INTERFACES
    4830            3 :       gfc_error ("Operand of .not. operator at %L is %s", &e->where,
    4831              :                  gfc_typename (op1));
    4832            3 :       return false;
    4833              : 
    4834        21562 :     case INTRINSIC_GT:
    4835        21562 :     case INTRINSIC_GT_OS:
    4836        21562 :     case INTRINSIC_GE:
    4837        21562 :     case INTRINSIC_GE_OS:
    4838        21562 :     case INTRINSIC_LT:
    4839        21562 :     case INTRINSIC_LT_OS:
    4840        21562 :     case INTRINSIC_LE:
    4841        21562 :     case INTRINSIC_LE_OS:
    4842        21562 :       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
    4843              :         {
    4844           18 :           CHECK_INTERFACES
    4845            0 :           gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
    4846            0 :           return false;
    4847              :         }
    4848              : 
    4849              :       /* Fall through.  */
    4850              : 
    4851       255070 :     case INTRINSIC_EQ:
    4852       255070 :     case INTRINSIC_EQ_OS:
    4853       255070 :     case INTRINSIC_NE:
    4854       255070 :     case INTRINSIC_NE_OS:
    4855              : 
    4856       255070 :       if (flag_dec
    4857         1038 :           && is_character_based (op1->ts.type)
    4858       255405 :           && is_character_based (op2->ts.type))
    4859              :         {
    4860          204 :           convert_hollerith_to_character (op1);
    4861          204 :           convert_hollerith_to_character (op2);
    4862              :         }
    4863              : 
    4864       255070 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4865        38598 :           && op1->ts.kind == op2->ts.kind)
    4866              :         {
    4867        38561 :           e->ts.type = BT_LOGICAL;
    4868        38561 :           e->ts.kind = gfc_default_logical_kind;
    4869        38561 :           break;
    4870              :         }
    4871              : 
    4872              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4873       216509 :       if (op1->ts.type == BT_BOZ)
    4874              :         {
    4875            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
    4876              :                                "as an operand of a relational operator"),
    4877              :                                &op1->where))
    4878              :             return false;
    4879              : 
    4880            0 :           if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
    4881              :             return false;
    4882              : 
    4883            0 :           if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
    4884              :             return false;
    4885              :         }
    4886              : 
    4887              :       /* If op2 is BOZ, then op1 is not!.  Try to convert to type of op2. */
    4888       216509 :       if (op2->ts.type == BT_BOZ)
    4889              :         {
    4890            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
    4891              :                                " as an operand of a relational operator"),
    4892              :                                 &op2->where))
    4893              :             return false;
    4894              : 
    4895            0 :           if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
    4896              :             return false;
    4897              : 
    4898            0 :           if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
    4899              :             return false;
    4900              :         }
    4901       216509 :       if (flag_dec
    4902       216509 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4903          120 :         convert_to_numeric (op1, op2);
    4904              : 
    4905       216509 :       if (flag_dec
    4906       216509 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4907          120 :         convert_to_numeric (op2, op1);
    4908              : 
    4909       216509 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4910              :         {
    4911              :           /* Do not perform conversions if operands are not conformable as
    4912              :              required for the binary intrinsic operators (F2018:10.1.5).
    4913              :              Defer to a possibly overloading user-defined operator.  */
    4914       215380 :           if (!gfc_op_rank_conformable (op1, op2))
    4915              :             {
    4916           70 :               CHECK_INTERFACES
    4917            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4918            0 :                          &op1->where, &op2->where);
    4919            0 :               return false;
    4920              :             }
    4921              : 
    4922       215310 :           if (flag_unsigned  && gfc_invalid_unsigned_ops (op1, op2))
    4923              :             {
    4924            1 :               CHECK_INTERFACES
    4925            1 :               gfc_error ("Inconsistent types for operator at %L and %L: "
    4926            1 :                          "%s and %s", &op1->where, &op2->where,
    4927              :                          gfc_typename (op1), gfc_typename (op2));
    4928            1 :               return false;
    4929              :             }
    4930              : 
    4931       215309 :           gfc_type_convert_binary (e, 1);
    4932              : 
    4933       215309 :           e->ts.type = BT_LOGICAL;
    4934       215309 :           e->ts.kind = gfc_default_logical_kind;
    4935              : 
    4936       215309 :           if (warn_compare_reals)
    4937              :             {
    4938           70 :               gfc_intrinsic_op op = e->value.op.op;
    4939              : 
    4940              :               /* Type conversion has made sure that the types of op1 and op2
    4941              :                  agree, so it is only necessary to check the first one.   */
    4942           70 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4943           13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4944            6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4945              :                 {
    4946           13 :                   const char *msg;
    4947              : 
    4948           13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4949              :                     msg = G_("Equality comparison for %s at %L");
    4950              :                   else
    4951            6 :                     msg = G_("Inequality comparison for %s at %L");
    4952              : 
    4953           13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4954              :                                gfc_typename (op1), &op1->where);
    4955              :                 }
    4956              :             }
    4957              : 
    4958              :           break;
    4959              :         }
    4960              : 
    4961         1129 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4962              :         {
    4963            2 :           CHECK_INTERFACES
    4964            4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4965              :                      &e->where,
    4966            2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4967              :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4968            2 :         }
    4969              :       else
    4970              :         {
    4971         1127 :           CHECK_INTERFACES
    4972          113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4973              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4974              :                      gfc_typename (op2));
    4975              :         }
    4976              : 
    4977              :       return false;
    4978              : 
    4979          303 :     case INTRINSIC_USER:
    4980          303 :       if (e->value.op.uop->op == NULL)
    4981              :         {
    4982           78 :           const char *name = e->value.op.uop->name;
    4983           78 :           const char *guessed;
    4984           78 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4985           78 :           CHECK_INTERFACES
    4986            5 :           if (guessed)
    4987            1 :             gfc_error ("Unknown operator %qs at %L; did you mean "
    4988              :                         "%qs?", name, &e->where, guessed);
    4989              :           else
    4990            4 :             gfc_error ("Unknown operator %qs at %L", name, &e->where);
    4991              :         }
    4992          225 :       else if (op2 == NULL)
    4993              :         {
    4994           48 :           CHECK_INTERFACES
    4995            0 :           gfc_error ("Operand of user operator %qs at %L is %s",
    4996            0 :                   e->value.op.uop->name, &e->where, gfc_typename (op1));
    4997              :         }
    4998              :       else
    4999              :         {
    5000          177 :           e->value.op.uop->op->sym->attr.referenced = 1;
    5001          177 :           CHECK_INTERFACES
    5002            5 :           gfc_error ("Operands of user operator %qs at %L are %s/%s",
    5003            5 :                     e->value.op.uop->name, &e->where, gfc_typename (op1),
    5004              :                     gfc_typename (op2));
    5005              :         }
    5006              : 
    5007              :       return false;
    5008              : 
    5009        23371 :     case INTRINSIC_PARENTHESES:
    5010        23371 :       e->ts = op1->ts;
    5011        23371 :       if (e->ts.type == BT_CHARACTER)
    5012          323 :         e->ts.u.cl = op1->ts.u.cl;
    5013              :       break;
    5014              : 
    5015            0 :     default:
    5016            0 :       gfc_internal_error ("resolve_operator(): Bad intrinsic");
    5017              :     }
    5018              : 
    5019              :   /* Deal with arrayness of an operand through an operator.  */
    5020              : 
    5021       532918 :   switch (e->value.op.op)
    5022              :     {
    5023       480816 :     case INTRINSIC_PLUS:
    5024       480816 :     case INTRINSIC_MINUS:
    5025       480816 :     case INTRINSIC_TIMES:
    5026       480816 :     case INTRINSIC_DIVIDE:
    5027       480816 :     case INTRINSIC_POWER:
    5028       480816 :     case INTRINSIC_CONCAT:
    5029       480816 :     case INTRINSIC_AND:
    5030       480816 :     case INTRINSIC_OR:
    5031       480816 :     case INTRINSIC_EQV:
    5032       480816 :     case INTRINSIC_NEQV:
    5033       480816 :     case INTRINSIC_EQ:
    5034       480816 :     case INTRINSIC_EQ_OS:
    5035       480816 :     case INTRINSIC_NE:
    5036       480816 :     case INTRINSIC_NE_OS:
    5037       480816 :     case INTRINSIC_GT:
    5038       480816 :     case INTRINSIC_GT_OS:
    5039       480816 :     case INTRINSIC_GE:
    5040       480816 :     case INTRINSIC_GE_OS:
    5041       480816 :     case INTRINSIC_LT:
    5042       480816 :     case INTRINSIC_LT_OS:
    5043       480816 :     case INTRINSIC_LE:
    5044       480816 :     case INTRINSIC_LE_OS:
    5045              : 
    5046       480816 :       if (op1->rank == 0 && op2->rank == 0)
    5047       427930 :         e->rank = 0;
    5048              : 
    5049       480816 :       if (op1->rank == 0 && op2->rank != 0)
    5050              :         {
    5051         2621 :           e->rank = op2->rank;
    5052              : 
    5053         2621 :           if (e->shape == NULL)
    5054         2591 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    5055              :         }
    5056              : 
    5057       480816 :       if (op1->rank != 0 && op2->rank == 0)
    5058              :         {
    5059        17223 :           e->rank = op1->rank;
    5060              : 
    5061        17223 :           if (e->shape == NULL)
    5062        17199 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5063              :         }
    5064              : 
    5065       480816 :       if (op1->rank != 0 && op2->rank != 0)
    5066              :         {
    5067        33042 :           if (op1->rank == op2->rank)
    5068              :             {
    5069        33042 :               e->rank = op1->rank;
    5070        33042 :               if (e->shape == NULL)
    5071              :                 {
    5072        32981 :                   t = compare_shapes (op1, op2);
    5073        32981 :                   if (!t)
    5074            3 :                     e->shape = NULL;
    5075              :                   else
    5076        32978 :                     e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5077              :                 }
    5078              :             }
    5079              :           else
    5080              :             {
    5081              :               /* Allow higher level expressions to work.  */
    5082            0 :               e->rank = 0;
    5083              : 
    5084              :               /* Try user-defined operators, and otherwise throw an error.  */
    5085            0 :               CHECK_INTERFACES
    5086            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    5087            0 :                          &op1->where, &op2->where);
    5088            0 :               return false;
    5089              :             }
    5090              :         }
    5091              :       break;
    5092              : 
    5093        52102 :     case INTRINSIC_PARENTHESES:
    5094        52102 :     case INTRINSIC_NOT:
    5095        52102 :     case INTRINSIC_UPLUS:
    5096        52102 :     case INTRINSIC_UMINUS:
    5097              :       /* Simply copy arrayness attribute */
    5098        52102 :       e->rank = op1->rank;
    5099        52102 :       e->corank = op1->corank;
    5100              : 
    5101        52102 :       if (e->shape == NULL)
    5102        52092 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5103              : 
    5104              :       break;
    5105              : 
    5106              :     default:
    5107              :       break;
    5108              :     }
    5109              : 
    5110       533460 : simplify_op:
    5111              : 
    5112              :   /* Attempt to simplify the expression.  */
    5113            3 :   if (t)
    5114              :     {
    5115       533457 :       t = gfc_simplify_expr (e, 0);
    5116              :       /* Some calls do not succeed in simplification and return false
    5117              :          even though there is no error; e.g. variable references to
    5118              :          PARAMETER arrays.  */
    5119       533457 :       if (!gfc_is_constant_expr (e))
    5120       487401 :         t = true;
    5121              :     }
    5122              :   return t;
    5123              : }
    5124              : 
    5125              : static bool
    5126          170 : resolve_conditional (gfc_expr *expr)
    5127              : {
    5128          170 :   gfc_expr *condition, *true_expr, *false_expr;
    5129              : 
    5130          170 :   condition = expr->value.conditional.condition;
    5131          170 :   true_expr = expr->value.conditional.true_expr;
    5132          170 :   false_expr = expr->value.conditional.false_expr;
    5133              : 
    5134          340 :   if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
    5135          340 :       || !gfc_resolve_expr (false_expr))
    5136              :     return false;
    5137              : 
    5138          170 :   if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
    5139              :     {
    5140            2 :       gfc_error (
    5141              :         "Condition in conditional expression must be a scalar logical at %L",
    5142              :         &condition->where);
    5143            2 :       return false;
    5144              :     }
    5145              : 
    5146          168 :   if (true_expr->ts.type != false_expr->ts.type)
    5147              :     {
    5148            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5149              :                  "must have the same declared type",
    5150              :                  &true_expr->where, &false_expr->where);
    5151            1 :       return false;
    5152              :     }
    5153              : 
    5154          167 :   if (true_expr->ts.kind != false_expr->ts.kind)
    5155              :     {
    5156            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5157              :                  "must have the same kind parameter",
    5158              :                  &true_expr->where, &false_expr->where);
    5159            1 :       return false;
    5160              :     }
    5161              : 
    5162          166 :   if (true_expr->rank != false_expr->rank)
    5163              :     {
    5164            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5165              :                  "must have the same rank",
    5166              :                  &true_expr->where, &false_expr->where);
    5167            1 :       return false;
    5168              :     }
    5169              : 
    5170              :   /* TODO: support more data types for conditional expressions  */
    5171          165 :   if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
    5172          165 :       && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
    5173           67 :       && true_expr->ts.type != BT_CHARACTER)
    5174              :     {
    5175            1 :       gfc_error (
    5176              :         "Sorry, only integer, logical, real, complex and character types are "
    5177              :         "currently supported for conditional expressions at %L",
    5178              :         &expr->where);
    5179            1 :       return false;
    5180              :     }
    5181              : 
    5182              :   /* TODO: support arrays in conditional expressions  */
    5183          164 :   if (true_expr->rank > 0)
    5184              :     {
    5185            1 :       gfc_error ("Sorry, array is currently unsupported for conditional "
    5186              :                  "expressions at %L",
    5187              :                  &expr->where);
    5188            1 :       return false;
    5189              :     }
    5190              : 
    5191          163 :   expr->ts = true_expr->ts;
    5192          163 :   expr->rank = true_expr->rank;
    5193          163 :   return true;
    5194              : }
    5195              : 
    5196              : /************** Array resolution subroutines **************/
    5197              : 
    5198              : enum compare_result
    5199              : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
    5200              : 
    5201              : /* Compare two integer expressions.  */
    5202              : 
    5203              : static compare_result
    5204       472687 : compare_bound (gfc_expr *a, gfc_expr *b)
    5205              : {
    5206       472687 :   int i;
    5207              : 
    5208       472687 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5209       310408 :       || b == NULL || b->expr_type != EXPR_CONSTANT)
    5210              :     return CMP_UNKNOWN;
    5211              : 
    5212              :   /* If either of the types isn't INTEGER, we must have
    5213              :      raised an error earlier.  */
    5214              : 
    5215       214056 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5216              :     return CMP_UNKNOWN;
    5217              : 
    5218       214052 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5219              : 
    5220       214052 :   if (i < 0)
    5221              :     return CMP_LT;
    5222       100538 :   if (i > 0)
    5223        39966 :     return CMP_GT;
    5224              :   return CMP_EQ;
    5225              : }
    5226              : 
    5227              : 
    5228              : /* Compare an integer expression with an integer.  */
    5229              : 
    5230              : static compare_result
    5231        75620 : compare_bound_int (gfc_expr *a, int b)
    5232              : {
    5233        75620 :   int i;
    5234              : 
    5235        75620 :   if (a == NULL
    5236        32625 :       || a->expr_type != EXPR_CONSTANT
    5237        29672 :       || a->ts.type != BT_INTEGER)
    5238              :     return CMP_UNKNOWN;
    5239              : 
    5240        29672 :   i = mpz_cmp_si (a->value.integer, b);
    5241              : 
    5242        29672 :   if (i < 0)
    5243              :     return CMP_LT;
    5244        25198 :   if (i > 0)
    5245        21657 :     return CMP_GT;
    5246              :   return CMP_EQ;
    5247              : }
    5248              : 
    5249              : 
    5250              : /* Compare an integer expression with a mpz_t.  */
    5251              : 
    5252              : static compare_result
    5253        70321 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5254              : {
    5255        70321 :   int i;
    5256              : 
    5257        70321 :   if (a == NULL
    5258        57446 :       || a->expr_type != EXPR_CONSTANT
    5259        55318 :       || a->ts.type != BT_INTEGER)
    5260              :     return CMP_UNKNOWN;
    5261              : 
    5262        55315 :   i = mpz_cmp (a->value.integer, b);
    5263              : 
    5264        55315 :   if (i < 0)
    5265              :     return CMP_LT;
    5266        25185 :   if (i > 0)
    5267        10746 :     return CMP_GT;
    5268              :   return CMP_EQ;
    5269              : }
    5270              : 
    5271              : 
    5272              : /* Compute the last value of a sequence given by a triplet.
    5273              :    Return 0 if it wasn't able to compute the last value, or if the
    5274              :    sequence if empty, and 1 otherwise.  */
    5275              : 
    5276              : static int
    5277        52513 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5278              :                                 gfc_expr *stride, mpz_t last)
    5279              : {
    5280        52513 :   mpz_t rem;
    5281              : 
    5282        52513 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5283        37296 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5284        32568 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5285              :     return 0;
    5286              : 
    5287        32249 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5288        32248 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    5289              :     return 0;
    5290              : 
    5291         6713 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    5292              :     {
    5293        25661 :       if (compare_bound (start, end) == CMP_GT)
    5294              :         return 0;
    5295        24272 :       mpz_set (last, end->value.integer);
    5296        24272 :       return 1;
    5297              :     }
    5298              : 
    5299         6587 :   if (compare_bound_int (stride, 0) == CMP_GT)
    5300              :     {
    5301              :       /* Stride is positive */
    5302         5222 :       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
    5303              :         return 0;
    5304              :     }
    5305              :   else
    5306              :     {
    5307              :       /* Stride is negative */
    5308         1365 :       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
    5309              :         return 0;
    5310              :     }
    5311              : 
    5312         6567 :   mpz_init (rem);
    5313         6567 :   mpz_sub (rem, end->value.integer, start->value.integer);
    5314         6567 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    5315         6567 :   mpz_sub (last, end->value.integer, rem);
    5316         6567 :   mpz_clear (rem);
    5317              : 
    5318         6567 :   return 1;
    5319              : }
    5320              : 
    5321              : 
    5322              : /* Compare a single dimension of an array reference to the array
    5323              :    specification.  */
    5324              : 
    5325              : static bool
    5326       219229 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5327              : {
    5328       219229 :   mpz_t last_value;
    5329              : 
    5330       219229 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5331              :     {
    5332          541 :       gcc_assert (ar->stride[i] == NULL);
    5333              :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5334          541 :       if (ar->start[i] == NULL)
    5335              :         {
    5336          449 :           gcc_assert (ar->end[i] == NULL);
    5337              :           return true;
    5338              :         }
    5339              :     }
    5340              : 
    5341              : /* Given start, end and stride values, calculate the minimum and
    5342              :    maximum referenced indexes.  */
    5343              : 
    5344       218780 :   switch (ar->dimen_type[i])
    5345              :     {
    5346              :     case DIMEN_VECTOR:
    5347              :     case DIMEN_THIS_IMAGE:
    5348              :       break;
    5349              : 
    5350       157856 :     case DIMEN_STAR:
    5351       157856 :     case DIMEN_ELEMENT:
    5352       157856 :       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
    5353              :         {
    5354            2 :           if (i < as->rank)
    5355            2 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5356              :                          "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5357            2 :                          mpz_get_si (ar->start[i]->value.integer),
    5358            2 :                          mpz_get_si (as->lower[i]->value.integer), i+1);
    5359              :           else
    5360            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5361              :                          "(%ld < %ld) in codimension %d", &ar->c_where[i],
    5362            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5363            0 :                          mpz_get_si (as->lower[i]->value.integer),
    5364            0 :                          i + 1 - as->rank);
    5365              :           return true;
    5366              :         }
    5367       157854 :       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
    5368              :         {
    5369           39 :           if (i < as->rank)
    5370           39 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5371              :                          "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5372           39 :                          mpz_get_si (ar->start[i]->value.integer),
    5373           39 :                          mpz_get_si (as->upper[i]->value.integer), i+1);
    5374              :           else
    5375            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5376              :                          "(%ld > %ld) in codimension %d", &ar->c_where[i],
    5377            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5378            0 :                          mpz_get_si (as->upper[i]->value.integer),
    5379            0 :                          i + 1 - as->rank);
    5380              :           return true;
    5381              :         }
    5382              : 
    5383              :       break;
    5384              : 
    5385        52558 :     case DIMEN_RANGE:
    5386        52558 :       {
    5387              : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
    5388              : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
    5389              : 
    5390        52558 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5391        52558 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5392              : 
    5393              :         /* Check for zero stride, which is not allowed.  */
    5394        52558 :         if (comp_stride_zero == CMP_EQ)
    5395              :           {
    5396            1 :             gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
    5397            1 :             return false;
    5398              :           }
    5399              : 
    5400              :         /* if start == end || (stride > 0 && start < end)
    5401              :                            || (stride < 0 && start > end),
    5402              :            then the array section contains at least one element.  In this
    5403              :            case, there is an out-of-bounds access if
    5404              :            (start < lower || start > upper).  */
    5405        52557 :         if (comp_start_end == CMP_EQ
    5406        51795 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5407        49006 :                 && comp_start_end == CMP_LT)
    5408        23017 :             || (comp_stride_zero == CMP_LT
    5409        23017 :                 && comp_start_end == CMP_GT))
    5410              :           {
    5411        30885 :             if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
    5412              :               {
    5413           27 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5414              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5415           27 :                        mpz_get_si (AR_START->value.integer),
    5416           27 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5417           27 :                 return true;
    5418              :               }
    5419        30858 :             if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
    5420              :               {
    5421           17 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5422              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5423           17 :                        mpz_get_si (AR_START->value.integer),
    5424           17 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5425           17 :                 return true;
    5426              :               }
    5427              :           }
    5428              : 
    5429              :         /* If we can compute the highest index of the array section,
    5430              :            then it also has to be between lower and upper.  */
    5431        52513 :         mpz_init (last_value);
    5432        52513 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5433              :                                             last_value))
    5434              :           {
    5435        30839 :             if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
    5436              :               {
    5437            3 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5438              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5439              :                        mpz_get_si (last_value),
    5440            3 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5441            3 :                 mpz_clear (last_value);
    5442            3 :                 return true;
    5443              :               }
    5444        30836 :             if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
    5445              :               {
    5446            7 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5447              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5448              :                        mpz_get_si (last_value),
    5449            7 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5450            7 :                 mpz_clear (last_value);
    5451            7 :                 return true;
    5452              :               }
    5453              :           }
    5454        52503 :         mpz_clear (last_value);
    5455              : 
    5456              : #undef AR_START
    5457              : #undef AR_END
    5458              :       }
    5459        52503 :       break;
    5460              : 
    5461            0 :     default:
    5462            0 :       gfc_internal_error ("check_dimension(): Bad array reference");
    5463              :     }
    5464              : 
    5465              :   return true;
    5466              : }
    5467              : 
    5468              : 
    5469              : /* Compare an array reference with an array specification.  */
    5470              : 
    5471              : static bool
    5472       430851 : compare_spec_to_ref (gfc_array_ref *ar)
    5473              : {
    5474       430851 :   gfc_array_spec *as;
    5475       430851 :   int i;
    5476              : 
    5477       430851 :   as = ar->as;
    5478       430851 :   i = as->rank - 1;
    5479              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5480       430851 :   if (as->type == AS_ASSUMED_SIZE
    5481         5810 :       && (/*ar->type == AR_FULL
    5482         5810 :           ||*/ (ar->type == AR_SECTION
    5483          523 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5484              :     {
    5485            5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5486              :                  "not specified at %L", &ar->where);
    5487            5 :       return false;
    5488              :     }
    5489              : 
    5490       430846 :   if (ar->type == AR_FULL)
    5491              :     return true;
    5492              : 
    5493       166251 :   if (as->rank != ar->dimen)
    5494              :     {
    5495           28 :       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
    5496              :                  &ar->where, ar->dimen, as->rank);
    5497           28 :       return false;
    5498              :     }
    5499              : 
    5500              :   /* ar->codimen == 0 is a local array.  */
    5501       166223 :   if (as->corank != ar->codimen && ar->codimen != 0)
    5502              :     {
    5503            0 :       gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
    5504              :                  &ar->where, ar->codimen, as->corank);
    5505            0 :       return false;
    5506              :     }
    5507              : 
    5508       375477 :   for (i = 0; i < as->rank; i++)
    5509       209255 :     if (!check_dimension (i, ar, as))
    5510              :       return false;
    5511              : 
    5512              :   /* Local access has no coarray spec.  */
    5513       166222 :   if (ar->codimen != 0)
    5514        19190 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5515              :       {
    5516         9976 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5517         6948 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5518              :           {
    5519            2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5520            2 :                        i + 1 - as->rank, &ar->where);
    5521            2 :             return false;
    5522              :           }
    5523         9974 :         if (!check_dimension (i, ar, as))
    5524              :           return false;
    5525              :       }
    5526              : 
    5527              :   return true;
    5528              : }
    5529              : 
    5530              : 
    5531              : /* Resolve one part of an array index.  */
    5532              : 
    5533              : static bool
    5534       743488 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5535              :                      int force_index_integer_kind)
    5536              : {
    5537       743488 :   gfc_typespec ts;
    5538              : 
    5539       743488 :   if (index == NULL)
    5540              :     return true;
    5541              : 
    5542       220614 :   if (!gfc_resolve_expr (index))
    5543              :     return false;
    5544              : 
    5545       220603 :   if (check_scalar && index->rank != 0)
    5546              :     {
    5547            2 :       gfc_error ("Array index at %L must be scalar", &index->where);
    5548            2 :       return false;
    5549              :     }
    5550              : 
    5551       220601 :   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
    5552              :     {
    5553            4 :       gfc_error ("Array index at %L must be of INTEGER type, found %s",
    5554              :                  &index->where, gfc_basic_typename (index->ts.type));
    5555            4 :       return false;
    5556              :     }
    5557              : 
    5558       220597 :   if (index->ts.type == BT_REAL)
    5559          657 :     if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
    5560              :                          &index->where))
    5561              :       return false;
    5562              : 
    5563       220597 :   if ((index->ts.kind != gfc_index_integer_kind
    5564       215548 :        && force_index_integer_kind)
    5565       189010 :       || (index->ts.type != BT_INTEGER
    5566              :           && index->ts.type != BT_UNKNOWN))
    5567              :     {
    5568        32243 :       gfc_clear_ts (&ts);
    5569        32243 :       ts.type = BT_INTEGER;
    5570        32243 :       ts.kind = gfc_index_integer_kind;
    5571              : 
    5572        32243 :       gfc_convert_type_warn (index, &ts, 2, 0);
    5573              :     }
    5574              : 
    5575              :   return true;
    5576              : }
    5577              : 
    5578              : /* Resolve one part of an array index.  */
    5579              : 
    5580              : bool
    5581       495917 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5582              : {
    5583       495917 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5584              : }
    5585              : 
    5586              : /* Resolve a dim argument to an intrinsic function.  */
    5587              : 
    5588              : bool
    5589        23915 : gfc_resolve_dim_arg (gfc_expr *dim)
    5590              : {
    5591        23915 :   if (dim == NULL)
    5592              :     return true;
    5593              : 
    5594        23915 :   if (!gfc_resolve_expr (dim))
    5595              :     return false;
    5596              : 
    5597        23915 :   if (dim->rank != 0)
    5598              :     {
    5599            0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5600            0 :       return false;
    5601              : 
    5602              :     }
    5603              : 
    5604        23915 :   if (dim->ts.type != BT_INTEGER)
    5605              :     {
    5606            0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5607            0 :       return false;
    5608              :     }
    5609              : 
    5610        23915 :   if (dim->ts.kind != gfc_index_integer_kind)
    5611              :     {
    5612        15306 :       gfc_typespec ts;
    5613              : 
    5614        15306 :       gfc_clear_ts (&ts);
    5615        15306 :       ts.type = BT_INTEGER;
    5616        15306 :       ts.kind = gfc_index_integer_kind;
    5617              : 
    5618        15306 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5619              :     }
    5620              : 
    5621              :   return true;
    5622              : }
    5623              : 
    5624              : /* Given an expression that contains array references, update those array
    5625              :    references to point to the right array specifications.  While this is
    5626              :    filled in during matching, this information is difficult to save and load
    5627              :    in a module, so we take care of it here.
    5628              : 
    5629              :    The idea here is that the original array reference comes from the
    5630              :    base symbol.  We traverse the list of reference structures, setting
    5631              :    the stored reference to references.  Component references can
    5632              :    provide an additional array specification.  */
    5633              : static void
    5634              : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5635              : 
    5636              : static bool
    5637          918 : find_array_spec (gfc_expr *e)
    5638              : {
    5639          918 :   gfc_array_spec *as;
    5640          918 :   gfc_component *c;
    5641          918 :   gfc_ref *ref;
    5642          918 :   bool class_as = false;
    5643              : 
    5644          918 :   if (e->symtree->n.sym->assoc)
    5645              :     {
    5646          221 :       if (e->symtree->n.sym->assoc->target)
    5647          221 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5648          221 :       resolve_assoc_var (e->symtree->n.sym, false);
    5649              :     }
    5650              : 
    5651          918 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5652              :     {
    5653          124 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5654          124 :       class_as = true;
    5655              :     }
    5656              :   else
    5657          794 :     as = e->symtree->n.sym->as;
    5658              : 
    5659         2093 :   for (ref = e->ref; ref; ref = ref->next)
    5660         1182 :     switch (ref->type)
    5661              :       {
    5662          920 :       case REF_ARRAY:
    5663          920 :         if (as == NULL)
    5664              :           {
    5665            7 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5666           14 :                          ? ref->u.ar.where : e->where);
    5667            7 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5668              :                        &loc);
    5669            7 :             return false;
    5670              :           }
    5671              : 
    5672          913 :         ref->u.ar.as = as;
    5673          913 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5674              :         as = NULL;
    5675              :         break;
    5676              : 
    5677          238 :       case REF_COMPONENT:
    5678          238 :         c = ref->u.c.component;
    5679          238 :         if (c->attr.dimension)
    5680              :           {
    5681          107 :             if (as != NULL && !(class_as && as == c->as))
    5682            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5683          107 :             as = c->as;
    5684              :           }
    5685              : 
    5686              :         break;
    5687              : 
    5688              :       case REF_SUBSTRING:
    5689              :       case REF_INQUIRY:
    5690              :         break;
    5691              :       }
    5692              : 
    5693          911 :   if (as != NULL)
    5694            0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5695              : 
    5696              :   return true;
    5697              : }
    5698              : 
    5699              : 
    5700              : /* Resolve an array reference.  */
    5701              : 
    5702              : static bool
    5703       431565 : resolve_array_ref (gfc_array_ref *ar)
    5704              : {
    5705       431565 :   int i, check_scalar;
    5706       431565 :   gfc_expr *e;
    5707              : 
    5708       679119 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5709              :     {
    5710       247571 :       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
    5711              : 
    5712              :       /* Do not force gfc_index_integer_kind for the start.  We can
    5713              :          do fine with any integer kind.  This avoids temporary arrays
    5714              :          created for indexing with a vector.  */
    5715       247571 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5716              :         return false;
    5717       247556 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5718              :         return false;
    5719       247554 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5720              :         return false;
    5721              : 
    5722       247554 :       e = ar->start[i];
    5723              : 
    5724       247554 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5725       147917 :         switch (e->rank)
    5726              :           {
    5727       146825 :           case 0:
    5728       146825 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5729       146825 :             break;
    5730              : 
    5731         1092 :           case 1:
    5732         1092 :             ar->dimen_type[i] = DIMEN_VECTOR;
    5733         1092 :             if (e->expr_type == EXPR_VARIABLE
    5734          470 :                 && e->symtree->n.sym->ts.type == BT_DERIVED)
    5735           13 :               ar->start[i] = gfc_get_parentheses (e);
    5736              :             break;
    5737              : 
    5738            0 :           default:
    5739            0 :             gfc_error ("Array index at %L is an array of rank %d",
    5740              :                        &ar->c_where[i], e->rank);
    5741            0 :             return false;
    5742              :           }
    5743              : 
    5744              :       /* Fill in the upper bound, which may be lower than the
    5745              :          specified one for something like a(2:10:5), which is
    5746              :          identical to a(2:7:5).  Only relevant for strides not equal
    5747              :          to one.  Don't try a division by zero.  */
    5748       247554 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5749        72413 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5750         8469 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5751         8322 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5752              :         {
    5753         8321 :           mpz_t size, end;
    5754              : 
    5755         8321 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5756              :             {
    5757         6597 :               if (ar->end[i] == NULL)
    5758              :                 {
    5759         8046 :                   ar->end[i] =
    5760         4023 :                     gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
    5761              :                                            &ar->where);
    5762         4023 :                   mpz_set (ar->end[i]->value.integer, end);
    5763              :                 }
    5764         2574 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5765         2574 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5766              :                 {
    5767         2574 :                   mpz_set (ar->end[i]->value.integer, end);
    5768              :                 }
    5769              :               else
    5770            0 :                 gcc_unreachable ();
    5771              : 
    5772         6597 :               mpz_clear (size);
    5773         6597 :               mpz_clear (end);
    5774              :             }
    5775              :         }
    5776              :     }
    5777              : 
    5778       431548 :   if (ar->type == AR_FULL)
    5779              :     {
    5780       268098 :       if (ar->as->rank == 0)
    5781         3469 :         ar->type = AR_ELEMENT;
    5782              : 
    5783              :       /* Make sure array is the same as array(:,:), this way
    5784              :          we don't need to special case all the time.  */
    5785       268098 :       ar->dimen = ar->as->rank;
    5786       638495 :       for (i = 0; i < ar->dimen; i++)
    5787              :         {
    5788       370397 :           ar->dimen_type[i] = DIMEN_RANGE;
    5789              : 
    5790       370397 :           gcc_assert (ar->start[i] == NULL);
    5791       370397 :           gcc_assert (ar->end[i] == NULL);
    5792       370397 :           gcc_assert (ar->stride[i] == NULL);
    5793              :         }
    5794              :     }
    5795              : 
    5796              :   /* If the reference type is unknown, figure out what kind it is.  */
    5797              : 
    5798       431548 :   if (ar->type == AR_UNKNOWN)
    5799              :     {
    5800       150311 :       ar->type = AR_ELEMENT;
    5801       291408 :       for (i = 0; i < ar->dimen; i++)
    5802       179507 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5803       179507 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5804              :           {
    5805        38410 :             ar->type = AR_SECTION;
    5806        38410 :             break;
    5807              :           }
    5808              :     }
    5809              : 
    5810       431548 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5811              :     return false;
    5812              : 
    5813       431512 :   if (ar->as->corank && ar->codimen == 0)
    5814              :     {
    5815         2089 :       int n;
    5816         2089 :       ar->codimen = ar->as->corank;
    5817         5944 :       for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
    5818         3855 :         ar->dimen_type[n] = DIMEN_THIS_IMAGE;
    5819              :     }
    5820              : 
    5821       431512 :   if (ar->codimen)
    5822              :     {
    5823        13837 :       if (ar->team_type == TEAM_NUMBER)
    5824              :         {
    5825           60 :           if (!gfc_resolve_expr (ar->team))
    5826              :             return false;
    5827              : 
    5828           60 :           if (ar->team->rank != 0)
    5829              :             {
    5830            0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5831              :                          &ar->team->where);
    5832            0 :               return false;
    5833              :             }
    5834              : 
    5835           60 :           if (ar->team->ts.type != BT_INTEGER)
    5836              :             {
    5837            6 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5838              :                          "type, found %s",
    5839            6 :                          &ar->team->where,
    5840              :                          gfc_basic_typename (ar->team->ts.type));
    5841            6 :               return false;
    5842              :             }
    5843              :         }
    5844        13777 :       else if (ar->team_type == TEAM_TEAM)
    5845              :         {
    5846           42 :           if (!gfc_resolve_expr (ar->team))
    5847              :             return false;
    5848              : 
    5849           42 :           if (ar->team->rank != 0)
    5850              :             {
    5851            3 :               gfc_error ("TEAM argument at %L must be scalar",
    5852              :                          &ar->team->where);
    5853            3 :               return false;
    5854              :             }
    5855              : 
    5856           39 :           if (ar->team->ts.type != BT_DERIVED
    5857           36 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5858           36 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5859              :             {
    5860            3 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5861              :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5862            3 :                          &ar->team->where,
    5863              :                          gfc_basic_typename (ar->team->ts.type));
    5864            3 :               return false;
    5865              :             }
    5866              :         }
    5867        13825 :       if (ar->stat)
    5868              :         {
    5869           62 :           if (!gfc_resolve_expr (ar->stat))
    5870              :             return false;
    5871              : 
    5872           62 :           if (ar->stat->rank != 0)
    5873              :             {
    5874            3 :               gfc_error ("STAT argument at %L must be scalar",
    5875              :                          &ar->stat->where);
    5876            3 :               return false;
    5877              :             }
    5878              : 
    5879           59 :           if (ar->stat->ts.type != BT_INTEGER)
    5880              :             {
    5881            3 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5882              :                          "type, found %s",
    5883            3 :                          &ar->stat->where,
    5884              :                          gfc_basic_typename (ar->stat->ts.type));
    5885            3 :               return false;
    5886              :             }
    5887              : 
    5888           56 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5889              :             {
    5890            0 :               gfc_error ("STAT's expression at %L must be a variable",
    5891              :                          &ar->stat->where);
    5892            0 :               return false;
    5893              :             }
    5894              :         }
    5895              :     }
    5896              :   return true;
    5897              : }
    5898              : 
    5899              : 
    5900              : bool
    5901         8847 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5902              : {
    5903         8847 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5904              : 
    5905         8847 :   if (ref->u.ss.start != NULL)
    5906              :     {
    5907         8847 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5908              :         return false;
    5909              : 
    5910         8847 :       if (ref->u.ss.start->ts.type != BT_INTEGER)
    5911              :         {
    5912            1 :           gfc_error ("Substring start index at %L must be of type INTEGER",
    5913              :                      &ref->u.ss.start->where);
    5914            1 :           return false;
    5915              :         }
    5916              : 
    5917         8846 :       if (ref->u.ss.start->rank != 0)
    5918              :         {
    5919            0 :           gfc_error ("Substring start index at %L must be scalar",
    5920              :                      &ref->u.ss.start->where);
    5921            0 :           return false;
    5922              :         }
    5923              : 
    5924         8846 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5925         8846 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5926           37 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5927              :         {
    5928            1 :           gfc_error ("Substring start index at %L is less than one",
    5929              :                      &ref->u.ss.start->where);
    5930            1 :           return false;
    5931              :         }
    5932              :     }
    5933              : 
    5934         8845 :   if (ref->u.ss.end != NULL)
    5935              :     {
    5936         8651 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5937              :         return false;
    5938              : 
    5939         8651 :       if (ref->u.ss.end->ts.type != BT_INTEGER)
    5940              :         {
    5941            1 :           gfc_error ("Substring end index at %L must be of type INTEGER",
    5942              :                      &ref->u.ss.end->where);
    5943            1 :           return false;
    5944              :         }
    5945              : 
    5946         8650 :       if (ref->u.ss.end->rank != 0)
    5947              :         {
    5948            0 :           gfc_error ("Substring end index at %L must be scalar",
    5949              :                      &ref->u.ss.end->where);
    5950            0 :           return false;
    5951              :         }
    5952              : 
    5953         8650 :       if (ref->u.ss.length != NULL
    5954         8313 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5955         8662 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5956           12 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5957              :         {
    5958            4 :           gfc_error ("Substring end index at %L exceeds the string length",
    5959              :                      &ref->u.ss.start->where);
    5960            4 :           return false;
    5961              :         }
    5962              : 
    5963         8646 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5964         8646 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5965         8646 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5966            7 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5967              :         {
    5968            4 :           gfc_error ("Substring end index at %L is too large",
    5969              :                      &ref->u.ss.end->where);
    5970            4 :           return false;
    5971              :         }
    5972              :       /*  If the substring has the same length as the original
    5973              :           variable, the reference itself can be deleted.  */
    5974              : 
    5975         8642 :       if (ref->u.ss.length != NULL
    5976         8305 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5977         9558 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5978          230 :         *equal_length = true;
    5979              :     }
    5980              : 
    5981              :   return true;
    5982              : }
    5983              : 
    5984              : 
    5985              : /* This function supplies missing substring charlens.  */
    5986              : 
    5987              : void
    5988         4564 : gfc_resolve_substring_charlen (gfc_expr *e)
    5989              : {
    5990         4564 :   gfc_ref *char_ref;
    5991         4564 :   gfc_expr *start, *end;
    5992         4564 :   gfc_typespec *ts = NULL;
    5993         4564 :   mpz_t diff;
    5994              : 
    5995         8889 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    5996              :     {
    5997         7042 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    5998              :         break;
    5999         4325 :       if (char_ref->type == REF_COMPONENT)
    6000          328 :         ts = &char_ref->u.c.component->ts;
    6001              :     }
    6002              : 
    6003         4564 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    6004         1909 :     return;
    6005              : 
    6006         2717 :   gcc_assert (char_ref->next == NULL);
    6007              : 
    6008         2717 :   if (e->ts.u.cl)
    6009              :     {
    6010          120 :       if (e->ts.u.cl->length)
    6011          108 :         gfc_free_expr (e->ts.u.cl->length);
    6012           12 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    6013              :         return;
    6014              :     }
    6015              : 
    6016         2705 :   if (!e->ts.u.cl)
    6017         2597 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6018              : 
    6019         2705 :   if (char_ref->u.ss.start)
    6020         2705 :     start = gfc_copy_expr (char_ref->u.ss.start);
    6021              :   else
    6022            0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    6023              : 
    6024         2705 :   if (char_ref->u.ss.end)
    6025         2655 :     end = gfc_copy_expr (char_ref->u.ss.end);
    6026           50 :   else if (e->expr_type == EXPR_VARIABLE)
    6027              :     {
    6028           50 :       if (!ts)
    6029           32 :         ts = &e->symtree->n.sym->ts;
    6030           50 :       end = gfc_copy_expr (ts->u.cl->length);
    6031              :     }
    6032              :   else
    6033              :     end = NULL;
    6034              : 
    6035         2705 :   if (!start || !end)
    6036              :     {
    6037           50 :       gfc_free_expr (start);
    6038           50 :       gfc_free_expr (end);
    6039           50 :       return;
    6040              :     }
    6041              : 
    6042              :   /* Length = (end - start + 1).
    6043              :      Check first whether it has a constant length.  */
    6044         2655 :   if (gfc_dep_difference (end, start, &diff))
    6045              :     {
    6046         2539 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    6047              :                                              &e->where);
    6048              : 
    6049         2539 :       mpz_add_ui (len->value.integer, diff, 1);
    6050         2539 :       mpz_clear (diff);
    6051         2539 :       e->ts.u.cl->length = len;
    6052              :       /* The check for length < 0 is handled below */
    6053              :     }
    6054              :   else
    6055              :     {
    6056          116 :       e->ts.u.cl->length = gfc_subtract (end, start);
    6057          116 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    6058              :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    6059              :                                                       NULL, 1));
    6060              :     }
    6061              : 
    6062              :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    6063              :      be within the range 1, 2, ..., n unless the starting point exceeds
    6064              :      the ending point, in which case the substring has length zero.  */
    6065              : 
    6066         2655 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    6067           15 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    6068              : 
    6069         2655 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6070         2655 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6071              : 
    6072              :   /* Make sure that the length is simplified.  */
    6073         2655 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    6074         2655 :   gfc_resolve_expr (e->ts.u.cl->length);
    6075              : }
    6076              : 
    6077              : 
    6078              : /* Convert an array reference to an array element so that PDT KIND and LEN
    6079              :    or inquiry references are always scalar.  */
    6080              : 
    6081              : static void
    6082           21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
    6083              : {
    6084           21 :   gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    6085           21 :   int dim;
    6086              : 
    6087           21 :   array_ref->u.ar.type = AR_ELEMENT;
    6088           21 :   expr->rank = 0;
    6089              :   /* Suppress the runtime bounds check.  */
    6090           21 :   expr->no_bounds_check = 1;
    6091           42 :   for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    6092              :     {
    6093           21 :       array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    6094           21 :       if (array_ref->u.ar.start[dim])
    6095            0 :         gfc_free_expr (array_ref->u.ar.start[dim]);
    6096              : 
    6097           21 :       if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
    6098            9 :         array_ref->u.ar.start[dim]
    6099            9 :                         = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
    6100              :       else
    6101           12 :         array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
    6102              : 
    6103           21 :       if (array_ref->u.ar.end[dim])
    6104            0 :         gfc_free_expr (array_ref->u.ar.end[dim]);
    6105           21 :       if (array_ref->u.ar.stride[dim])
    6106            0 :         gfc_free_expr (array_ref->u.ar.stride[dim]);
    6107              :     }
    6108           21 :   gfc_free_expr (unity);
    6109           21 : }
    6110              : 
    6111              : 
    6112              : /* Resolve subtype references.  */
    6113              : 
    6114              : bool
    6115       549927 : gfc_resolve_ref (gfc_expr *expr)
    6116              : {
    6117       549927 :   int current_part_dimension, n_components, seen_part_dimension;
    6118       549927 :   gfc_ref *ref, **prev, *array_ref;
    6119       549927 :   bool equal_length;
    6120       549927 :   gfc_symbol *last_pdt = NULL;
    6121              : 
    6122      1080619 :   for (ref = expr->ref; ref; ref = ref->next)
    6123       531610 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    6124              :       {
    6125          918 :         if (!find_array_spec (expr))
    6126              :           return false;
    6127              :         break;
    6128              :       }
    6129              : 
    6130      1613067 :   for (prev = &expr->ref; *prev != NULL;
    6131       531676 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6132       531755 :     switch ((*prev)->type)
    6133              :       {
    6134       431565 :       case REF_ARRAY:
    6135       431565 :         if (!resolve_array_ref (&(*prev)->u.ar))
    6136              :             return false;
    6137              :         break;
    6138              : 
    6139              :       case REF_COMPONENT:
    6140              :       case REF_INQUIRY:
    6141              :         break;
    6142              : 
    6143         8566 :       case REF_SUBSTRING:
    6144         8566 :         equal_length = false;
    6145         8566 :         if (!gfc_resolve_substring (*prev, &equal_length))
    6146              :             return false;
    6147              : 
    6148         8558 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    6149              :           {
    6150              :             /* Remove the reference and move the charlen, if any.  */
    6151          205 :             ref = *prev;
    6152          205 :             *prev = ref->next;
    6153          205 :             ref->next = NULL;
    6154          205 :             expr->ts.u.cl = ref->u.ss.length;
    6155          205 :             ref->u.ss.length = NULL;
    6156          205 :             gfc_free_ref_list (ref);
    6157              :           }
    6158              :         break;
    6159              :       }
    6160              : 
    6161              :   /* Check constraints on part references.  */
    6162              : 
    6163       549841 :   current_part_dimension = 0;
    6164       549841 :   seen_part_dimension = 0;
    6165       549841 :   n_components = 0;
    6166       549841 :   array_ref = NULL;
    6167              : 
    6168              :   /* Use the declared type of the base symbol to initialize last_pdt when the
    6169              :      expression is not itself a PDT. This matters for ASSOCIATE variables whose
    6170              :      component reference may still point to a PDT template.  */
    6171       549841 :   if (expr->expr_type == EXPR_VARIABLE
    6172       456008 :       && (IS_PDT (expr)
    6173       455438 :           || (expr->ref && expr->symtree && IS_PDT (expr->symtree->n.sym))))
    6174         2267 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6175              : 
    6176      1081287 :   for (ref = expr->ref; ref; ref = ref->next)
    6177              :     {
    6178       531457 :       switch (ref->type)
    6179              :         {
    6180       431487 :         case REF_ARRAY:
    6181       431487 :           array_ref = ref;
    6182       431487 :           switch (ref->u.ar.type)
    6183              :             {
    6184       264627 :             case AR_FULL:
    6185              :               /* Coarray scalar.  */
    6186       264627 :               if (ref->u.ar.as->rank == 0)
    6187              :                 {
    6188              :                   current_part_dimension = 0;
    6189              :                   break;
    6190              :                 }
    6191              :               /* Fall through.  */
    6192       306113 :             case AR_SECTION:
    6193       306113 :               current_part_dimension = 1;
    6194       306113 :               break;
    6195              : 
    6196       125374 :             case AR_ELEMENT:
    6197       125374 :               array_ref = NULL;
    6198       125374 :               current_part_dimension = 0;
    6199       125374 :               break;
    6200              : 
    6201            0 :             case AR_UNKNOWN:
    6202            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6203              :             }
    6204              : 
    6205              :           break;
    6206              : 
    6207        90796 :         case REF_COMPONENT:
    6208        90796 :           if (current_part_dimension || seen_part_dimension)
    6209              :             {
    6210              :               /* F03:C614.  */
    6211         7081 :               if (ref->u.c.component->attr.pointer
    6212         7078 :                   || ref->u.c.component->attr.proc_pointer
    6213         7077 :                   || (ref->u.c.component->ts.type == BT_CLASS
    6214            1 :                         && CLASS_DATA (ref->u.c.component)->attr.pointer))
    6215              :                 {
    6216            4 :                   gfc_error ("Component to the right of a part reference "
    6217              :                              "with nonzero rank must not have the POINTER "
    6218              :                              "attribute at %L", &expr->where);
    6219            4 :                   return false;
    6220              :                 }
    6221         7077 :               else if (ref->u.c.component->attr.allocatable
    6222         7071 :                         || (ref->u.c.component->ts.type == BT_CLASS
    6223            1 :                             && CLASS_DATA (ref->u.c.component)->attr.allocatable))
    6224              : 
    6225              :                 {
    6226            7 :                   gfc_error ("Component to the right of a part reference "
    6227              :                              "with nonzero rank must not have the ALLOCATABLE "
    6228              :                              "attribute at %L", &expr->where);
    6229            7 :                   return false;
    6230              :                 }
    6231              :             }
    6232              : 
    6233              :           /* Sometimes the component in a component reference is that of the
    6234              :              pdt_template. Point to the component of pdt_type instead. This
    6235              :              ensures that the component gets a backend_decl in translation.  */
    6236        90785 :           if (last_pdt)
    6237              :             {
    6238         2198 :               gfc_component *cmp = last_pdt->components;
    6239         5397 :               for (; cmp; cmp = cmp->next)
    6240         5272 :                 if (!strcmp (cmp->name, ref->u.c.component->name))
    6241              :                   {
    6242         2073 :                     ref->u.c.component = cmp;
    6243         2073 :                     break;
    6244              :                   }
    6245         2198 :               ref->u.c.sym = last_pdt;
    6246              :             }
    6247              : 
    6248              :           /* Convert pdt_templates, if necessary, and update 'last_pdt'.  */
    6249        90785 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6250              :             {
    6251        21062 :               if (ref->u.c.component->ts.u.derived->attr.pdt_template)
    6252              :                 {
    6253            0 :                   if (gfc_get_pdt_instance (ref->u.c.component->param_list,
    6254              :                                             &ref->u.c.component->ts.u.derived,
    6255              :                                             NULL) != MATCH_YES)
    6256              :                     return false;
    6257            0 :                   last_pdt = ref->u.c.component->ts.u.derived;
    6258              :                 }
    6259        21062 :               else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
    6260          521 :                 last_pdt = ref->u.c.component->ts.u.derived;
    6261              :               else
    6262              :                 last_pdt = NULL;
    6263              :             }
    6264              : 
    6265              :           /* The F08 standard requires(See R425, R431, R435, and in particular
    6266              :              Note 6.7) that a PDT parameter reference be a scalar even if
    6267              :              the designator is an array."  */
    6268        90785 :           if (array_ref && last_pdt && last_pdt->attr.pdt_type
    6269          149 :               && (ref->u.c.component->attr.pdt_kind
    6270          149 :                   || ref->u.c.component->attr.pdt_len))
    6271            7 :             reset_array_ref_to_scalar (expr, array_ref);
    6272              : 
    6273        90785 :           n_components++;
    6274        90785 :           break;
    6275              : 
    6276              :         case REF_SUBSTRING:
    6277              :           break;
    6278              : 
    6279          821 :         case REF_INQUIRY:
    6280              :           /* Implement requirement in note 9.7 of F2018 that the result of the
    6281              :              LEN inquiry be a scalar.  */
    6282          821 :           if (ref->u.i == INQUIRY_LEN && array_ref
    6283           40 :               && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
    6284           40 :                   || expr->ts.type == BT_INTEGER))
    6285           14 :             reset_array_ref_to_scalar (expr, array_ref);
    6286              :           break;
    6287              :         }
    6288              : 
    6289       531446 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6290       518015 :            || ref->next == NULL)
    6291              :           && current_part_dimension
    6292       465350 :           && seen_part_dimension)
    6293              :         {
    6294            0 :           gfc_error ("Two or more part references with nonzero rank must "
    6295              :                      "not be specified at %L", &expr->where);
    6296            0 :           return false;
    6297              :         }
    6298              : 
    6299       531446 :       if (ref->type == REF_COMPONENT)
    6300              :         {
    6301        90785 :           if (current_part_dimension)
    6302         6883 :             seen_part_dimension = 1;
    6303              : 
    6304              :           /* reset to make sure */
    6305              :           current_part_dimension = 0;
    6306              :         }
    6307              :     }
    6308              : 
    6309              :   return true;
    6310              : }
    6311              : 
    6312              : 
    6313              : /* Given an expression, determine its shape.  This is easier than it sounds.
    6314              :    Leaves the shape array NULL if it is not possible to determine the shape.  */
    6315              : 
    6316              : static void
    6317      2621977 : expression_shape (gfc_expr *e)
    6318              : {
    6319      2621977 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6320      2621977 :   int i;
    6321              : 
    6322      2621977 :   if (e->rank <= 0 || e->shape != NULL)
    6323      2442900 :     return;
    6324              : 
    6325       713522 :   for (i = 0; i < e->rank; i++)
    6326       482025 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6327       179077 :       goto fail;
    6328              : 
    6329       231497 :   e->shape = gfc_get_shape (e->rank);
    6330              : 
    6331       231497 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6332              : 
    6333       231497 :   return;
    6334              : 
    6335       179077 : fail:
    6336       180748 :   for (i--; i >= 0; i--)
    6337         1671 :     mpz_clear (array[i]);
    6338              : }
    6339              : 
    6340              : 
    6341              : /* Given a variable expression node, compute the rank of the expression by
    6342              :    examining the base symbol and any reference structures it may have.  */
    6343              : 
    6344              : void
    6345      2621977 : gfc_expression_rank (gfc_expr *e)
    6346              : {
    6347      2621977 :   gfc_ref *ref, *last_arr_ref = nullptr;
    6348      2621977 :   int i, rank, corank;
    6349              : 
    6350              :   /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
    6351              :      could lead to serious confusion...  */
    6352      2621977 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6353              : 
    6354      2621977 :   if (e->ref == NULL)
    6355              :     {
    6356      1931369 :       if (e->expr_type == EXPR_ARRAY)
    6357        73250 :         goto done;
    6358              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6359              : 
    6360      1858119 :       if (e->symtree != NULL)
    6361              :         {
    6362              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6363      1858107 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    6364        14133 :                                 && e->symtree->n.sym->ts.u.derived
    6365        14128 :                                 && CLASS_DATA (e->symtree->n.sym))
    6366      1858107 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6367              :                                  : e->symtree->n.sym->as;
    6368      1858107 :           if (as)
    6369              :             {
    6370          638 :               e->rank = as->rank;
    6371          638 :               e->corank = as->corank;
    6372          638 :               goto done;
    6373              :             }
    6374              :         }
    6375      1857481 :       e->rank = 0;
    6376      1857481 :       e->corank = 0;
    6377      1857481 :       goto done;
    6378              :     }
    6379              : 
    6380              :   rank = 0;
    6381              :   corank = 0;
    6382              : 
    6383      1092852 :   for (ref = e->ref; ref; ref = ref->next)
    6384              :     {
    6385       800086 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    6386          568 :           && ref->u.c.component->attr.function && !ref->next)
    6387              :         {
    6388          372 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6389          372 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6390              :         }
    6391              : 
    6392       800086 :       if (ref->type != REF_ARRAY)
    6393       160595 :         continue;
    6394              : 
    6395       639491 :       last_arr_ref = ref;
    6396       639491 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6397              :         {
    6398       351639 :           rank = ref->u.ar.as->rank;
    6399       351639 :           break;
    6400              :         }
    6401              : 
    6402       287852 :       if (ref->u.ar.type == AR_SECTION)
    6403              :         {
    6404              :           /* Figure out the rank of the section.  */
    6405        46203 :           if (rank != 0)
    6406            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6407              : 
    6408       115114 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6409        68911 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6410        68911 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6411        60051 :               rank++;
    6412              : 
    6413              :           break;
    6414              :         }
    6415              :     }
    6416       690608 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6417       619284 :       && last_arr_ref->u.ar.as->rank != -1)
    6418              :     {
    6419        19521 :       for (i = last_arr_ref->u.ar.as->rank;
    6420       630533 :            i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
    6421              :         {
    6422              :           /* For unknown dimen in non-resolved as assume full corank.  */
    6423        20454 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6424        19844 :               || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
    6425          323 :                   && !last_arr_ref->u.ar.as->resolved))
    6426              :             {
    6427              :               corank = last_arr_ref->u.ar.as->corank;
    6428              :               break;
    6429              :             }
    6430        19521 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6431        19521 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6432        19423 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6433        16932 :             corank++;
    6434         2589 :           else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
    6435            0 :             gfc_internal_error ("Illegal coarray index");
    6436              :         }
    6437              :     }
    6438              : 
    6439       690608 :   e->rank = rank;
    6440       690608 :   e->corank = corank;
    6441              : 
    6442      2621977 : done:
    6443      2621977 :   expression_shape (e);
    6444      2621977 : }
    6445              : 
    6446              : 
    6447              : /* Given two expressions, check that their rank is conformable, i.e. either
    6448              :    both have the same rank or at least one is a scalar.  */
    6449              : 
    6450              : bool
    6451     12247130 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6452              : {
    6453     12247130 :   if (op1->expr_type == EXPR_VARIABLE)
    6454       740455 :     gfc_expression_rank (op1);
    6455     12247130 :   if (op2->expr_type == EXPR_VARIABLE)
    6456       447690 :     gfc_expression_rank (op2);
    6457              : 
    6458        77931 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6459     12324735 :          && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
    6460           30 :              || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
    6461              : }
    6462              : 
    6463              : /* Resolve a variable expression.  */
    6464              : 
    6465              : static bool
    6466      1341717 : resolve_variable (gfc_expr *e)
    6467              : {
    6468      1341717 :   gfc_symbol *sym;
    6469      1341717 :   bool t;
    6470              : 
    6471      1341717 :   t = true;
    6472              : 
    6473      1341717 :   if (e->symtree == NULL)
    6474              :     return false;
    6475      1341242 :   sym = e->symtree->n.sym;
    6476              : 
    6477              :   /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
    6478              :      as ts.type is set to BT_ASSUMED in resolve_symbol.  */
    6479      1341242 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    6480              :     {
    6481          183 :       if (!actual_arg || inquiry_argument)
    6482              :         {
    6483            2 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
    6484              :                      "be used as actual argument", sym->name, &e->where);
    6485            2 :           return false;
    6486              :         }
    6487              :     }
    6488              :   /* TS 29113, 407b.  */
    6489      1341059 :   else if (e->ts.type == BT_ASSUMED)
    6490              :     {
    6491          571 :       if (!actual_arg)
    6492              :         {
    6493           20 :           gfc_error ("Assumed-type variable %s at %L may only be used "
    6494              :                      "as actual argument", sym->name, &e->where);
    6495           20 :           return false;
    6496              :         }
    6497          551 :       else if (inquiry_argument && !first_actual_arg)
    6498              :         {
    6499              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6500              :              for all inquiry functions in resolve_function; the reason is
    6501              :              that the function-name resolution happens too late in that
    6502              :              function.  */
    6503            0 :           gfc_error ("Assumed-type variable %s at %L as actual argument to "
    6504              :                      "an inquiry function shall be the first argument",
    6505              :                      sym->name, &e->where);
    6506            0 :           return false;
    6507              :         }
    6508              :     }
    6509              :   /* TS 29113, C535b.  */
    6510      1340488 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6511        38278 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6512        38273 :              && CLASS_DATA (sym)->as
    6513        15047 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6514      1339518 :             || (sym->ts.type != BT_CLASS && sym->as
    6515       366811 :                 && sym->as->type == AS_ASSUMED_RANK))
    6516         8064 :            && !sym->attr.select_rank_temporary
    6517         8064 :            && !(sym->assoc && sym->assoc->ar))
    6518              :     {
    6519         8064 :       if (!actual_arg
    6520         1277 :           && !(cs_base && cs_base->current
    6521         1276 :                && (cs_base->current->op == EXEC_SELECT_RANK
    6522          188 :                    || sym->attr.target)))
    6523              :         {
    6524          144 :           gfc_error ("Assumed-rank variable %s at %L may only be used as "
    6525              :                      "actual argument", sym->name, &e->where);
    6526          144 :           return false;
    6527              :         }
    6528         7920 :       else if (inquiry_argument && !first_actual_arg)
    6529              :         {
    6530              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6531              :              for all inquiry functions in resolve_function; the reason is
    6532              :              that the function-name resolution happens too late in that
    6533              :              function.  */
    6534            0 :           gfc_error ("Assumed-rank variable %s at %L as actual argument "
    6535              :                      "to an inquiry function shall be the first argument",
    6536              :                      sym->name, &e->where);
    6537            0 :           return false;
    6538              :         }
    6539              :     }
    6540              : 
    6541      1341076 :   if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
    6542          181 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6543          180 :            && e->ref->next == NULL))
    6544              :     {
    6545            1 :       gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
    6546              :                  "a subobject reference", sym->name, &e->ref->u.ar.where);
    6547            1 :       return false;
    6548              :     }
    6549              :   /* TS 29113, 407b.  */
    6550      1341075 :   else if (e->ts.type == BT_ASSUMED && e->ref
    6551          687 :            && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6552          680 :                 && e->ref->next == NULL))
    6553              :     {
    6554            7 :       gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
    6555              :                  "reference", sym->name, &e->ref->u.ar.where);
    6556            7 :       return false;
    6557              :     }
    6558              : 
    6559              :   /* TS 29113, C535b.  */
    6560      1341068 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6561        38278 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6562        38273 :         && CLASS_DATA (sym)->as
    6563        15047 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6564      1340098 :        || (sym->ts.type != BT_CLASS && sym->as
    6565       367347 :            && sym->as->type == AS_ASSUMED_RANK))
    6566         8204 :       && !(sym->assoc && sym->assoc->ar)
    6567         8204 :       && e->ref
    6568         8204 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6569         8200 :            && e->ref->next == NULL))
    6570              :     {
    6571            4 :       gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
    6572              :                  "reference", sym->name, &e->ref->u.ar.where);
    6573            4 :       return false;
    6574              :     }
    6575              : 
    6576              :   /* Guessed type variables are associate_names whose selector had not been
    6577              :      parsed at the time that the construct was parsed. Now the namespace is
    6578              :      being resolved, the TKR of the selector will be available for fixup of
    6579              :      the associate_name.  */
    6580      1341064 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6581              :     {
    6582          410 :       gfc_fixup_inferred_type_refs (e);
    6583              :       /* KIND inquiry ref returns the kind of the target.  */
    6584          410 :       if (e->expr_type == EXPR_CONSTANT)
    6585              :         return true;
    6586              :     }
    6587      1340654 :   else if (IS_INFERRED_TYPE (e)
    6588          489 :            && sym->ts.type != BT_UNKNOWN
    6589          489 :            && (sym->ts.type != e->ts.type || sym->ts.kind != e->ts.kind))
    6590              :     /* No subobject ref, but the expression's typespec was set at parse
    6591              :        time before the target's actual type/kind was known.  Refresh from
    6592              :        the now-resolved associate-name symbol.  */
    6593          192 :     e->ts = sym->ts;
    6594      1340462 :   else if (sym->attr.select_type_temporary
    6595         9152 :            && sym->ns->assoc_name_inferred)
    6596           92 :     gfc_fixup_inferred_type_refs (e);
    6597              : 
    6598              :   /* For variables that are used in an associate (target => object) where
    6599              :      the object's basetype is array valued while the target is scalar,
    6600              :      the ts' type of the component refs is still array valued, which
    6601              :      can't be translated that way.  */
    6602      1341052 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6603          605 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6604          605 :       && sym->assoc->target->ts.u.derived
    6605          605 :       && CLASS_DATA (sym->assoc->target)
    6606          605 :       && CLASS_DATA (sym->assoc->target)->as)
    6607              :     {
    6608              :       gfc_ref *ref = e->ref;
    6609          701 :       while (ref)
    6610              :         {
    6611          542 :           switch (ref->type)
    6612              :             {
    6613          237 :             case REF_COMPONENT:
    6614          237 :               ref->u.c.sym = sym->ts.u.derived;
    6615              :               /* Stop the loop.  */
    6616          237 :               ref = NULL;
    6617          237 :               break;
    6618          305 :             default:
    6619          305 :               ref = ref->next;
    6620          305 :               break;
    6621              :             }
    6622              :         }
    6623              :     }
    6624              : 
    6625              :   /* If this is an associate-name, it may be parsed with an array reference
    6626              :      in error even though the target is scalar.  Fail directly in this case.
    6627              :      TODO Understand why class scalar expressions must be excluded.  */
    6628      1341052 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6629              :     {
    6630        12111 :       if (sym->ts.type == BT_CLASS)
    6631          245 :         gfc_fix_class_refs (e);
    6632        12111 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6633         2276 :           && e->ref->type == REF_ARRAY)
    6634              :         {
    6635              :           /* Unambiguously scalar!  */
    6636            3 :           if (sym->assoc->target
    6637            3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6638            1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6639            2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6640              :                        sym->name, &e->where);
    6641              :           return false;
    6642              :         }
    6643        12108 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6644         7156 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6645              :         {
    6646              :           /* This can happen because the parser did not detect that the
    6647              :              associate name is an array and the expression had no array
    6648              :              part_ref.  */
    6649          225 :           gfc_ref *ref = gfc_get_ref ();
    6650          225 :           ref->type = REF_ARRAY;
    6651          225 :           ref->u.ar.type = AR_FULL;
    6652          225 :           if (sym->as)
    6653              :             {
    6654          224 :               ref->u.ar.as = sym->as;
    6655          224 :               ref->u.ar.dimen = sym->as->rank;
    6656              :             }
    6657          225 :           ref->next = e->ref;
    6658          225 :           e->ref = ref;
    6659              :         }
    6660              :     }
    6661              : 
    6662      1341049 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6663            0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6664              : 
    6665              :   /* On the other hand, the parser may not have known this is an array;
    6666              :      in this case, we have to add a FULL reference.  */
    6667      1341049 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6668              :     {
    6669            0 :       e->ref = gfc_get_ref ();
    6670            0 :       e->ref->type = REF_ARRAY;
    6671            0 :       e->ref->u.ar.type = AR_FULL;
    6672            0 :       e->ref->u.ar.dimen = 0;
    6673              :     }
    6674              : 
    6675              :   /* Like above, but for class types, where the checking whether an array
    6676              :      ref is present is more complicated.  Furthermore make sure not to add
    6677              :      the full array ref to _vptr or _len refs.  */
    6678      1341049 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6679         1023 :       && CLASS_DATA (sym)
    6680         1023 :       && (CLASS_DATA (sym)->attr.dimension
    6681          449 :           || CLASS_DATA (sym)->attr.codimension)
    6682          580 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6683              :     {
    6684          555 :       gfc_ref *ref, *newref;
    6685              : 
    6686          555 :       newref = gfc_get_ref ();
    6687          555 :       newref->type = REF_ARRAY;
    6688          555 :       newref->u.ar.type = AR_FULL;
    6689          555 :       newref->u.ar.dimen = 0;
    6690              : 
    6691              :       /* Because this is an associate var and the first ref either is a ref to
    6692              :          the _data component or not, no traversal of the ref chain is
    6693              :          needed.  The array ref needs to be inserted after the _data ref,
    6694              :          or when that is not present, which may happened for polymorphic
    6695              :          types, then at the first position.  */
    6696          555 :       ref = e->ref;
    6697          555 :       if (!ref)
    6698           18 :         e->ref = newref;
    6699          537 :       else if (ref->type == REF_COMPONENT
    6700          232 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6701              :         {
    6702          232 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6703              :             {
    6704           12 :               newref->next = ref->next;
    6705           12 :               ref->next = newref;
    6706              :             }
    6707              :           else
    6708              :             /* Array ref present already.  */
    6709          220 :             gfc_free_ref_list (newref);
    6710              :         }
    6711          305 :       else if (ref->type == REF_ARRAY)
    6712              :         /* Array ref present already.  */
    6713          305 :         gfc_free_ref_list (newref);
    6714              :       else
    6715              :         {
    6716            0 :           newref->next = ref;
    6717            0 :           e->ref = newref;
    6718              :         }
    6719              :     }
    6720      1340494 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6721              :     {
    6722          498 :       gfc_ref *ref;
    6723          922 :       for (ref = e->ref; ref; ref = ref->next)
    6724          454 :         if (ref->type == REF_SUBSTRING)
    6725              :           break;
    6726          498 :       if (ref == NULL)
    6727          468 :         e->ts = sym->ts;
    6728              :     }
    6729              : 
    6730      1341049 :   if (e->ref && !gfc_resolve_ref (e))
    6731              :     return false;
    6732              : 
    6733      1340956 :   if (sym->attr.flavor == FL_PROCEDURE
    6734        32523 :       && (!sym->attr.function
    6735        19037 :           || (sym->attr.function && sym->result
    6736        18582 :               && sym->result->attr.proc_pointer
    6737          726 :               && !sym->result->attr.function)))
    6738              :     {
    6739        13486 :       e->ts.type = BT_PROCEDURE;
    6740        13486 :       goto resolve_procedure;
    6741              :     }
    6742              : 
    6743      1327470 :   if (sym->ts.type != BT_UNKNOWN)
    6744      1326698 :     gfc_variable_attr (e, &e->ts);
    6745          772 :   else if (sym->attr.flavor == FL_PROCEDURE
    6746           12 :            && sym->attr.function && sym->result
    6747           12 :            && sym->result->ts.type != BT_UNKNOWN
    6748           10 :            && sym->result->attr.proc_pointer)
    6749           10 :     e->ts = sym->result->ts;
    6750              :   else
    6751              :     {
    6752              :       /* Must be a simple variable reference.  */
    6753          762 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6754              :         return false;
    6755          633 :       e->ts = sym->ts;
    6756              :     }
    6757              : 
    6758      1327341 :   if (check_assumed_size_reference (sym, e))
    6759              :     return false;
    6760              : 
    6761              :   /* Deal with forward references to entries during gfc_resolve_code, to
    6762              :      satisfy, at least partially, 12.5.2.5.  */
    6763      1327322 :   if (gfc_current_ns->entries
    6764         3229 :       && current_entry_id == sym->entry_id
    6765         1050 :       && cs_base
    6766          964 :       && cs_base->current
    6767          964 :       && cs_base->current->op != EXEC_ENTRY)
    6768              :     {
    6769          964 :       int n;
    6770          964 :       bool saved_specification_expr;
    6771          964 :       gfc_symbol *saved_specification_expr_symbol;
    6772              : 
    6773              :       /* If the symbol is a dummy...  */
    6774          964 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6775              :         {
    6776              :           /*  If it has not been seen as a dummy, this is an error.  */
    6777          462 :           if (!entry_dummy_seen_p (sym))
    6778              :             {
    6779            5 :               if (specification_expr
    6780            4 :                   && specification_expr_symbol
    6781            4 :                   && specification_expr_symbol->attr.dummy
    6782            2 :                   && specification_expr_symbol->ns == gfc_current_ns
    6783            7 :                   && !entry_dummy_seen_p (specification_expr_symbol))
    6784              :                 ;
    6785            3 :               else if (specification_expr)
    6786            2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6787              :                            ", is referenced at %L before the ENTRY statement "
    6788              :                            "in which it is a parameter",
    6789              :                            sym->name, &cs_base->current->loc);
    6790              :               else
    6791            1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6792              :                            "statement in which it is a parameter",
    6793              :                            sym->name, &cs_base->current->loc);
    6794              :               t = false;
    6795              :             }
    6796              :         }
    6797              : 
    6798              :       /* Now do the same check on the specification expressions.  */
    6799          964 :       saved_specification_expr = specification_expr;
    6800          964 :       saved_specification_expr_symbol = specification_expr_symbol;
    6801          964 :       specification_expr = true;
    6802          964 :       specification_expr_symbol = sym;
    6803          964 :       if (sym->ts.type == BT_CHARACTER
    6804          964 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6805              :         t = false;
    6806              : 
    6807          964 :       if (sym->as)
    6808              :         {
    6809          279 :           for (n = 0; n < sym->as->rank; n++)
    6810              :             {
    6811          164 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6812            0 :                 t = false;
    6813          164 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6814            1 :                 t = false;
    6815              :             }
    6816              :         }
    6817          964 :       specification_expr = saved_specification_expr;
    6818          964 :       specification_expr_symbol = saved_specification_expr_symbol;
    6819              : 
    6820          964 :       if (t)
    6821              :         /* Update the symbol's entry level.  */
    6822          957 :         sym->entry_id = current_entry_id + 1;
    6823              :     }
    6824              : 
    6825              :   /* If a symbol has been host_associated mark it.  This is used latter,
    6826              :      to identify if aliasing is possible via host association.  */
    6827      1327322 :   if (sym->attr.flavor == FL_VARIABLE
    6828      1288637 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6829         6228 :           || !sym->ns->code->ext.block.assoc)
    6830      1286535 :       && gfc_current_ns->parent
    6831       614750 :       && (gfc_current_ns->parent == sym->ns
    6832       575620 :           || (gfc_current_ns->parent->parent
    6833        12365 :               && gfc_current_ns->parent->parent == sym->ns)))
    6834        45841 :     sym->attr.host_assoc = 1;
    6835              : 
    6836      1327322 :   if (gfc_current_ns->proc_name
    6837      1323136 :       && sym->attr.dimension
    6838       360723 :       && (sym->ns != gfc_current_ns
    6839       336446 :           || sym->attr.use_assoc
    6840       332435 :           || sym->attr.in_common))
    6841        33077 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6842              : 
    6843      1340808 : resolve_procedure:
    6844      1340808 :   if (t && !resolve_procedure_expression (e))
    6845              :     t = false;
    6846              : 
    6847              :   /* F2008, C617 and C1229.  */
    6848      1339698 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6849      1441393 :       && gfc_is_coindexed (e))
    6850              :     {
    6851          359 :       gfc_ref *ref, *ref2 = NULL;
    6852              : 
    6853          442 :       for (ref = e->ref; ref; ref = ref->next)
    6854              :         {
    6855          442 :           if (ref->type == REF_COMPONENT)
    6856           83 :             ref2 = ref;
    6857          442 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6858              :             break;
    6859              :         }
    6860              : 
    6861          718 :       for ( ; ref; ref = ref->next)
    6862          371 :         if (ref->type == REF_COMPONENT)
    6863              :           break;
    6864              : 
    6865              :       /* Expression itself is not coindexed object.  */
    6866          359 :       if (ref && e->ts.type == BT_CLASS)
    6867              :         {
    6868            3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6869              :                      &e->where);
    6870            3 :           t = false;
    6871              :         }
    6872              : 
    6873              :       /* Expression itself is coindexed object.  */
    6874              :       if (ref == NULL)
    6875              :         {
    6876          347 :           gfc_component *c;
    6877          347 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6878          467 :           for ( ; c; c = c->next)
    6879          120 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6880              :               {
    6881            0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6882              :                          "subcomponent at %L", &e->where);
    6883            0 :                 t = false;
    6884            0 :                 break;
    6885              :               }
    6886              :         }
    6887              :     }
    6888              : 
    6889      1340808 :   if (t)
    6890      1340798 :     gfc_expression_rank (e);
    6891              : 
    6892      1340808 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6893            3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6894              :                  "Using variable %qs at %L is deprecated",
    6895              :                  sym->name, &e->where);
    6896              :   /* Simplify cases where access to a parameter array results in a
    6897              :      single constant.  Suppress errors since those will have been
    6898              :      issued before, as warnings.  */
    6899      1340808 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    6900              :     {
    6901         2731 :       gfc_push_suppress_errors ();
    6902         2731 :       gfc_simplify_expr (e, 1);
    6903         2731 :       gfc_pop_suppress_errors ();
    6904              :     }
    6905              : 
    6906              :   return t;
    6907              : }
    6908              : 
    6909              : 
    6910              : /* 'sym' was initially guessed to be derived type but has been corrected
    6911              :    in resolve_assoc_var to be a class entity or the derived type correcting.
    6912              :    If a class entity it will certainly need the _data reference or the
    6913              :    reference derived type symbol correcting in the first component ref if
    6914              :    a derived type.  */
    6915              : 
    6916              : void
    6917          920 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6918              : {
    6919          920 :   gfc_ref *ref, *new_ref;
    6920          920 :   gfc_symbol *sym, *derived;
    6921          920 :   gfc_expr *target;
    6922          920 :   sym = e->symtree->n.sym;
    6923              : 
    6924              :   /* An associate_name whose selector is (i) a component ref of a selector
    6925              :      that is a inferred type associate_name; or (ii) an intrinsic type that
    6926              :      has been inferred from an inquiry ref.  */
    6927          920 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    6928              :     {
    6929          318 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    6930          318 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    6931          318 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    6932              :         {
    6933           60 :           ref = e->ref;
    6934              :           /* A substring misidentified as an array section.  */
    6935           60 :           if (sym->ts.type == BT_CHARACTER
    6936           30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    6937            6 :               && !ref->u.ar.stride[0])
    6938              :             {
    6939            6 :               new_ref = gfc_get_ref ();
    6940            6 :               new_ref->type = REF_SUBSTRING;
    6941            6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    6942            6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    6943            6 :               new_ref->u.ss.length = sym->ts.u.cl;
    6944            6 :               *ref = *new_ref;
    6945            6 :               free (new_ref);
    6946              :             }
    6947              :           else
    6948              :             {
    6949           54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    6950           24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    6951           54 :               e->ref = ref->next;
    6952           54 :               free (ref);
    6953              :             }
    6954              :         }
    6955              : 
    6956              :       /* It is possible for an inquiry reference to be mistaken for a
    6957              :          component reference. Correct this now.  */
    6958          318 :       ref = e->ref;
    6959          318 :       if (ref && ref->type == REF_ARRAY)
    6960          138 :         ref = ref->next;
    6961          186 :       if (ref && ref->type == REF_COMPONENT
    6962          150 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    6963              :         {
    6964           12 :           e->symtree->n.sym = sym;
    6965           12 :           *ref = *new_ref;
    6966           12 :           gfc_free_ref_list (new_ref);
    6967              :         }
    6968              : 
    6969              :       /* The kind of the associate name is best evaluated directly from the
    6970              :          selector because of the guesses made in primary.cc, when the type
    6971              :          is still unknown.  */
    6972          318 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    6973              :         {
    6974           24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    6975           12 :                                            sym->assoc->target->ts.kind);
    6976           12 :           gfc_replace_expr (e, ne);
    6977           12 :         }
    6978          174 :       else if (ref && ref->type == REF_INQUIRY
    6979          150 :                && (ref->u.i == INQUIRY_RE || ref->u.i == INQUIRY_IM)
    6980          114 :                && sym->ts.type == BT_COMPLEX
    6981          114 :                && e->ts.type == BT_REAL
    6982          114 :                && e->ts.kind != sym->ts.kind)
    6983              :         /* primary.cc set the inquiry-result kind to the default real kind
    6984              :            when the associate-name's type was inferred from %re/%im before
    6985              :            the target was resolved.  Now use the (resolved) selector kind.  */
    6986           24 :         e->ts.kind = sym->ts.kind;
    6987              : 
    6988              :       /* Now that the references are all sorted out, set the expression rank
    6989              :          and return.  */
    6990          318 :       gfc_expression_rank (e);
    6991          318 :       return;
    6992              :     }
    6993              : 
    6994          602 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    6995              :                                      : sym->ts.u.derived;
    6996              : 
    6997              :   /* Ensure that class symbols have an array spec and ensure that there
    6998              :      is a _data field reference following class type references.  */
    6999          602 :   if (sym->ts.type == BT_CLASS
    7000          196 :       && sym->assoc->target->ts.type == BT_CLASS)
    7001              :     {
    7002          196 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    7003          196 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    7004          196 :       sym->attr.dimension = 0;
    7005          196 :       sym->attr.codimension = 0;
    7006          196 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    7007          196 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    7008          196 :       if (e->ref && (e->ref->type != REF_COMPONENT
    7009          160 :                      || e->ref->u.c.component->name[0] != '_'))
    7010              :         {
    7011           82 :           ref = gfc_get_ref ();
    7012           82 :           ref->type = REF_COMPONENT;
    7013           82 :           ref->next = e->ref;
    7014           82 :           e->ref = ref;
    7015           82 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    7016              :                                                    true, true, NULL);
    7017           82 :           ref->u.c.sym = sym->ts.u.derived;
    7018              :         }
    7019              :     }
    7020              : 
    7021              :   /* Proceed as far as the first component reference and ensure that the
    7022              :      correct derived type is being used.  */
    7023          865 :   for (ref = e->ref; ref; ref = ref->next)
    7024          829 :     if (ref->type == REF_COMPONENT)
    7025              :       {
    7026          566 :         if (ref->u.c.component->name[0] != '_')
    7027          370 :           ref->u.c.sym = derived;
    7028              :         else
    7029          196 :           ref->u.c.sym = sym->ts.u.derived;
    7030              :         break;
    7031              :       }
    7032              : 
    7033              :   /* Verify that the type inference mechanism has not introduced a spurious
    7034              :      array reference.  This can happen with an associate name, whose selector
    7035              :      is an element of another inferred type.  */
    7036          602 :   target = e->symtree->n.sym->assoc->target;
    7037          602 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7038          190 :       && e != target && !target->rank)
    7039              :     {
    7040              :       /* First case: array ref after the scalar class or derived
    7041              :          associate_name.  */
    7042          190 :       if (e->ref && e->ref->type == REF_ARRAY
    7043            7 :           && e->ref->u.ar.type != AR_ELEMENT)
    7044              :         {
    7045            7 :           ref = e->ref;
    7046            7 :           if (ref->u.ar.type == AR_UNKNOWN)
    7047            1 :             gfc_error ("Invalid array reference at %L", &e->where);
    7048            7 :           e->ref = ref->next;
    7049            7 :           free (ref);
    7050              : 
    7051              :           /* If it hasn't a ref to the '_data' field supply one.  */
    7052            7 :           if (sym->ts.type == BT_CLASS
    7053            0 :               && !(e->ref->type == REF_COMPONENT
    7054            0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    7055              :             {
    7056            0 :               gfc_ref *new_ref;
    7057            0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    7058              :                                   "_data", true, true, &new_ref);
    7059            0 :               new_ref->next = e->ref;
    7060            0 :               e->ref = new_ref;
    7061              :             }
    7062              :         }
    7063              :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    7064          183 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7065          183 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    7066           64 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    7067            0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    7068              :         {
    7069            0 :           ref = e->ref->next;
    7070            0 :           if (ref->u.ar.type == AR_UNKNOWN)
    7071            0 :             gfc_error ("Invalid array reference at %L", &e->where);
    7072            0 :           e->ref->next = e->ref->next->next;
    7073            0 :           free (ref);
    7074              :         }
    7075              :     }
    7076              : 
    7077              :   /* Now that all the references are OK, get the expression rank.  */
    7078          602 :   gfc_expression_rank (e);
    7079              : }
    7080              : 
    7081              : 
    7082              : /* Checks to see that the correct symbol has been host associated.
    7083              :    The only situations where this arises are:
    7084              :         (i)  That in which a twice contained function is parsed after
    7085              :              the host association is made. On detecting this, change
    7086              :              the symbol in the expression and convert the array reference
    7087              :              into an actual arglist if the old symbol is a variable; or
    7088              :         (ii) That in which an external function is typed but not declared
    7089              :              explicitly to be external. Here, the old symbol is changed
    7090              :              from a variable to an external function.  */
    7091              : static bool
    7092      1689656 : check_host_association (gfc_expr *e)
    7093              : {
    7094      1689656 :   gfc_symbol *sym, *old_sym;
    7095      1689656 :   gfc_symtree *st;
    7096      1689656 :   int n;
    7097      1689656 :   gfc_ref *ref;
    7098      1689656 :   gfc_actual_arglist *arg, *tail = NULL;
    7099      1689656 :   bool retval = e->expr_type == EXPR_FUNCTION;
    7100              : 
    7101              :   /*  If the expression is the result of substitution in
    7102              :       interface.cc(gfc_extend_expr) because there is no way in
    7103              :       which the host association can be wrong.  */
    7104      1689656 :   if (e->symtree == NULL
    7105      1688825 :         || e->symtree->n.sym == NULL
    7106      1688825 :         || e->user_operator)
    7107              :     return retval;
    7108              : 
    7109      1687045 :   old_sym = e->symtree->n.sym;
    7110              : 
    7111      1687045 :   if (gfc_current_ns->parent
    7112       742138 :         && old_sym->ns != gfc_current_ns)
    7113              :     {
    7114              :       /* Use the 'USE' name so that renamed module symbols are
    7115              :          correctly handled.  */
    7116        93149 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7117              : 
    7118        93149 :       if (sym && old_sym != sym
    7119          714 :               && sym->attr.flavor == FL_PROCEDURE
    7120          111 :               && sym->attr.contained)
    7121              :         {
    7122              :           /* Clear the shape, since it might not be valid.  */
    7123           83 :           gfc_free_shape (&e->shape, e->rank);
    7124              : 
    7125              :           /* Give the expression the right symtree!  */
    7126           83 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    7127           83 :           gcc_assert (st != NULL);
    7128              : 
    7129           83 :           if (old_sym->attr.flavor == FL_PROCEDURE
    7130           59 :                 || e->expr_type == EXPR_FUNCTION)
    7131              :             {
    7132              :               /* Original was function so point to the new symbol, since
    7133              :                  the actual argument list is already attached to the
    7134              :                  expression.  */
    7135           30 :               e->value.function.esym = NULL;
    7136           30 :               e->symtree = st;
    7137              :             }
    7138              :           else
    7139              :             {
    7140              :               /* Original was variable so convert array references into
    7141              :                  an actual arglist. This does not need any checking now
    7142              :                  since resolve_function will take care of it.  */
    7143           53 :               e->value.function.actual = NULL;
    7144           53 :               e->expr_type = EXPR_FUNCTION;
    7145           53 :               e->symtree = st;
    7146              : 
    7147              :               /* Ambiguity will not arise if the array reference is not
    7148              :                  the last reference.  */
    7149           55 :               for (ref = e->ref; ref; ref = ref->next)
    7150           38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    7151              :                   break;
    7152              : 
    7153           53 :               if ((ref == NULL || ref->type != REF_ARRAY)
    7154           17 :                   && sym->attr.proc == PROC_INTERNAL)
    7155              :                 {
    7156            4 :                   gfc_error ("%qs at %L is host associated at %L into "
    7157              :                              "a contained procedure with an internal "
    7158              :                              "procedure of the same name", sym->name,
    7159              :                               &old_sym->declared_at, &e->where);
    7160            4 :                   return false;
    7161              :                 }
    7162              : 
    7163           13 :               if (ref == NULL)
    7164              :                 return false;
    7165              : 
    7166           36 :               gcc_assert (ref->type == REF_ARRAY);
    7167              : 
    7168              :               /* Grab the start expressions from the array ref and
    7169              :                  copy them into actual arguments.  */
    7170           84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7171              :                 {
    7172           48 :                   arg = gfc_get_actual_arglist ();
    7173           48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    7174           48 :                   if (e->value.function.actual == NULL)
    7175           36 :                     tail = e->value.function.actual = arg;
    7176              :                   else
    7177              :                     {
    7178           12 :                       tail->next = arg;
    7179           12 :                       tail = arg;
    7180              :                     }
    7181              :                 }
    7182              : 
    7183              :               /* Dump the reference list and set the rank.  */
    7184           36 :               gfc_free_ref_list (e->ref);
    7185           36 :               e->ref = NULL;
    7186           36 :               e->rank = sym->as ? sym->as->rank : 0;
    7187           36 :               e->corank = sym->as ? sym->as->corank : 0;
    7188              :             }
    7189              : 
    7190           66 :           gfc_resolve_expr (e);
    7191           66 :           sym->refs++;
    7192              :         }
    7193              :       /* This case corresponds to a call, from a block or a contained
    7194              :          procedure, to an external function, which has not been declared
    7195              :          as being external in the main program but has been typed.  */
    7196        93066 :       else if (sym && old_sym != sym
    7197          631 :                && !e->ref
    7198          359 :                && sym->ts.type == BT_UNKNOWN
    7199           27 :                && old_sym->ts.type != BT_UNKNOWN
    7200           19 :                && sym->attr.flavor == FL_PROCEDURE
    7201           19 :                && old_sym->attr.flavor == FL_VARIABLE
    7202            7 :                && sym->ns->parent == old_sym->ns
    7203            7 :                && sym->ns->proc_name
    7204            7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    7205            6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    7206            6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    7207              :         {
    7208            6 :           old_sym->attr.flavor = FL_PROCEDURE;
    7209            6 :           old_sym->attr.external = 1;
    7210            6 :           old_sym->attr.function = 1;
    7211            6 :           old_sym->result = old_sym;
    7212            6 :           gfc_resolve_expr (e);
    7213              :         }
    7214              :     }
    7215              :   /* This might have changed!  */
    7216      1687028 :   return e->expr_type == EXPR_FUNCTION;
    7217              : }
    7218              : 
    7219              : 
    7220              : static void
    7221         1454 : gfc_resolve_character_operator (gfc_expr *e)
    7222              : {
    7223         1454 :   gfc_expr *op1 = e->value.op.op1;
    7224         1454 :   gfc_expr *op2 = e->value.op.op2;
    7225         1454 :   gfc_expr *e1 = NULL;
    7226         1454 :   gfc_expr *e2 = NULL;
    7227              : 
    7228         1454 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7229              : 
    7230         1454 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7231          767 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7232          687 :   else if (op1->expr_type == EXPR_CONSTANT)
    7233          268 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7234          268 :                            op1->value.character.length);
    7235              : 
    7236         1454 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7237          755 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7238          699 :   else if (op2->expr_type == EXPR_CONSTANT)
    7239          468 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7240          468 :                            op2->value.character.length);
    7241              : 
    7242         1454 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7243              : 
    7244         1454 :   if (!e1 || !e2)
    7245              :     {
    7246          547 :       gfc_free_expr (e1);
    7247          547 :       gfc_free_expr (e2);
    7248              : 
    7249          547 :       return;
    7250              :     }
    7251              : 
    7252          907 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7253          907 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7254          907 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7255          907 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7256          907 :   gfc_resolve_expr (e->ts.u.cl->length);
    7257              : 
    7258          907 :   return;
    7259              : }
    7260              : 
    7261              : 
    7262              : /*  Ensure that an character expression has a charlen and, if possible, a
    7263              :     length expression.  */
    7264              : 
    7265              : static void
    7266       184981 : fixup_charlen (gfc_expr *e)
    7267              : {
    7268              :   /* The cases fall through so that changes in expression type and the need
    7269              :      for multiple fixes are picked up.  In all circumstances, a charlen should
    7270              :      be available for the middle end to hang a backend_decl on.  */
    7271       184981 :   switch (e->expr_type)
    7272              :     {
    7273         1454 :     case EXPR_OP:
    7274         1454 :       gfc_resolve_character_operator (e);
    7275              :       /* FALLTHRU */
    7276              : 
    7277         1521 :     case EXPR_ARRAY:
    7278         1521 :       if (e->expr_type == EXPR_ARRAY)
    7279           67 :         gfc_resolve_character_array_constructor (e);
    7280              :       /* FALLTHRU */
    7281              : 
    7282         1978 :     case EXPR_SUBSTRING:
    7283         1978 :       if (!e->ts.u.cl && e->ref)
    7284          453 :         gfc_resolve_substring_charlen (e);
    7285              :       /* FALLTHRU */
    7286              : 
    7287       184981 :     default:
    7288       184981 :       if (!e->ts.u.cl)
    7289       183007 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7290              : 
    7291       184981 :       break;
    7292              :     }
    7293       184981 : }
    7294              : 
    7295              : 
    7296              : /* Update an actual argument to include the passed-object for type-bound
    7297              :    procedures at the right position.  */
    7298              : 
    7299              : static gfc_actual_arglist*
    7300         3038 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7301              :                      const char *name)
    7302              : {
    7303         3062 :   gcc_assert (argpos > 0);
    7304              : 
    7305         3062 :   if (argpos == 1)
    7306              :     {
    7307         2913 :       gfc_actual_arglist* result;
    7308              : 
    7309         2913 :       result = gfc_get_actual_arglist ();
    7310         2913 :       result->expr = po;
    7311         2913 :       result->next = lst;
    7312         2913 :       if (name)
    7313          514 :         result->name = name;
    7314              : 
    7315              :       return result;
    7316              :     }
    7317              : 
    7318          149 :   if (lst)
    7319          125 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    7320              :   else
    7321           24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    7322              :   return lst;
    7323              : }
    7324              : 
    7325              : 
    7326              : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    7327              : 
    7328              : static gfc_expr*
    7329         7407 : extract_compcall_passed_object (gfc_expr* e)
    7330              : {
    7331         7407 :   gfc_expr* po;
    7332              : 
    7333         7407 :   if (e->expr_type == EXPR_UNKNOWN)
    7334              :     {
    7335            0 :       gfc_error ("Error in typebound call at %L",
    7336              :                  &e->where);
    7337            0 :       return NULL;
    7338              :     }
    7339              : 
    7340         7407 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7341              : 
    7342         7407 :   if (e->value.compcall.base_object)
    7343         1668 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7344              :   else
    7345              :     {
    7346         5739 :       po = gfc_get_expr ();
    7347         5739 :       po->expr_type = EXPR_VARIABLE;
    7348         5739 :       po->symtree = e->symtree;
    7349         5739 :       po->ref = gfc_copy_ref (e->ref);
    7350         5739 :       po->where = e->where;
    7351              :     }
    7352              : 
    7353         7407 :   if (!gfc_resolve_expr (po))
    7354            3 :     return NULL;
    7355              : 
    7356              :   return po;
    7357              : }
    7358              : 
    7359              : 
    7360              : /* Update the arglist of an EXPR_COMPCALL expression to include the
    7361              :    passed-object.  */
    7362              : 
    7363              : static bool
    7364         3408 : update_compcall_arglist (gfc_expr* e)
    7365              : {
    7366         3408 :   gfc_expr* po;
    7367         3408 :   gfc_typebound_proc* tbp;
    7368              : 
    7369         3408 :   tbp = e->value.compcall.tbp;
    7370              : 
    7371         3408 :   if (tbp->error)
    7372              :     return false;
    7373              : 
    7374         3407 :   po = extract_compcall_passed_object (e);
    7375         3407 :   if (!po)
    7376              :     return false;
    7377              : 
    7378         3407 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7379              :     {
    7380         1158 :       gfc_free_expr (po);
    7381         1158 :       return true;
    7382              :     }
    7383              : 
    7384         2249 :   if (tbp->pass_arg_num <= 0)
    7385              :     return false;
    7386              : 
    7387         2248 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7388              :                                                   tbp->pass_arg_num,
    7389              :                                                   tbp->pass_arg);
    7390              : 
    7391         2248 :   return true;
    7392              : }
    7393              : 
    7394              : 
    7395              : /* Extract the passed object from a PPC call (a copy of it).  */
    7396              : 
    7397              : static gfc_expr*
    7398           85 : extract_ppc_passed_object (gfc_expr *e)
    7399              : {
    7400           85 :   gfc_expr *po;
    7401           85 :   gfc_ref **ref;
    7402              : 
    7403           85 :   po = gfc_get_expr ();
    7404           85 :   po->expr_type = EXPR_VARIABLE;
    7405           85 :   po->symtree = e->symtree;
    7406           85 :   po->ref = gfc_copy_ref (e->ref);
    7407           85 :   po->where = e->where;
    7408              : 
    7409              :   /* Remove PPC reference.  */
    7410           85 :   ref = &po->ref;
    7411           91 :   while ((*ref)->next)
    7412            6 :     ref = &(*ref)->next;
    7413           85 :   gfc_free_ref_list (*ref);
    7414           85 :   *ref = NULL;
    7415              : 
    7416           85 :   if (!gfc_resolve_expr (po))
    7417            0 :     return NULL;
    7418              : 
    7419              :   return po;
    7420              : }
    7421              : 
    7422              : 
    7423              : /* Update the actual arglist of a procedure pointer component to include the
    7424              :    passed-object.  */
    7425              : 
    7426              : static bool
    7427          588 : update_ppc_arglist (gfc_expr* e)
    7428              : {
    7429          588 :   gfc_expr* po;
    7430          588 :   gfc_component *ppc;
    7431          588 :   gfc_typebound_proc* tb;
    7432              : 
    7433          588 :   ppc = gfc_get_proc_ptr_comp (e);
    7434          588 :   if (!ppc)
    7435              :     return false;
    7436              : 
    7437          588 :   tb = ppc->tb;
    7438              : 
    7439          588 :   if (tb->error)
    7440              :     return false;
    7441          586 :   else if (tb->nopass)
    7442              :     return true;
    7443              : 
    7444           85 :   po = extract_ppc_passed_object (e);
    7445           85 :   if (!po)
    7446              :     return false;
    7447              : 
    7448              :   /* F08:R739.  */
    7449           85 :   if (po->rank != 0)
    7450              :     {
    7451            0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7452            0 :       return false;
    7453              :     }
    7454              : 
    7455              :   /* F08:C611.  */
    7456           85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7457              :     {
    7458            1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7459              :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7460            1 :       return false;
    7461              :     }
    7462              : 
    7463           84 :   gcc_assert (tb->pass_arg_num > 0);
    7464           84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7465              :                                                   tb->pass_arg_num,
    7466              :                                                   tb->pass_arg);
    7467              : 
    7468           84 :   return true;
    7469              : }
    7470              : 
    7471              : 
    7472              : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7473              :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7474              : 
    7475              : static bool
    7476         3419 : check_typebound_baseobject (gfc_expr* e)
    7477              : {
    7478         3419 :   gfc_expr* base;
    7479         3419 :   bool return_value = false;
    7480              : 
    7481         3419 :   base = extract_compcall_passed_object (e);
    7482         3419 :   if (!base)
    7483              :     return false;
    7484              : 
    7485         3416 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7486              :     {
    7487            1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7488            1 :       goto cleanup;
    7489              :     }
    7490              : 
    7491         3415 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7492            1 :     return false;
    7493              : 
    7494              :   /* F08:C611.  */
    7495         3414 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7496              :     {
    7497            3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7498              :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7499            3 :       goto cleanup;
    7500              :     }
    7501              : 
    7502              :   /* F08:C1230. If the procedure called is NOPASS,
    7503              :      the base object must be scalar.  */
    7504         3411 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7505              :     {
    7506            1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7507              :                  " be scalar", &e->where);
    7508            1 :       goto cleanup;
    7509              :     }
    7510              : 
    7511              :   return_value = true;
    7512              : 
    7513         3415 : cleanup:
    7514         3415 :   gfc_free_expr (base);
    7515         3415 :   return return_value;
    7516              : }
    7517              : 
    7518              : 
    7519              : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7520              :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7521              :    arglist and the target-procedure symtree are returned.  */
    7522              : 
    7523              : static bool
    7524         3408 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7525              :                           gfc_actual_arglist** actual)
    7526              : {
    7527         3408 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7528         3408 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7529              : 
    7530              :   /* Update the actual arglist for PASS.  */
    7531         3408 :   if (!update_compcall_arglist (e))
    7532              :     return false;
    7533              : 
    7534         3406 :   *actual = e->value.compcall.actual;
    7535         3406 :   *target = e->value.compcall.tbp->u.specific;
    7536              : 
    7537         3406 :   gfc_free_ref_list (e->ref);
    7538         3406 :   e->ref = NULL;
    7539         3406 :   e->value.compcall.actual = NULL;
    7540              : 
    7541              :   /* If we find a deferred typebound procedure, check for derived types
    7542              :      that an overriding typebound procedure has not been missed.  */
    7543         3406 :   if (e->value.compcall.name
    7544         3406 :       && !e->value.compcall.tbp->non_overridable
    7545         3388 :       && e->value.compcall.base_object
    7546          834 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7547              :     {
    7548          541 :       gfc_symtree *st;
    7549          541 :       gfc_symbol *derived;
    7550              : 
    7551              :       /* Use the derived type of the base_object.  */
    7552          541 :       derived = e->value.compcall.base_object->ts.u.derived;
    7553          541 :       st = NULL;
    7554              : 
    7555              :       /* If necessary, go through the inheritance chain.  */
    7556         1631 :       while (!st && derived)
    7557              :         {
    7558              :           /* Look for the typebound procedure 'name'.  */
    7559          549 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7560          541 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7561              :                                    e->value.compcall.name);
    7562          549 :           if (!st)
    7563            8 :             derived = gfc_get_derived_super_type (derived);
    7564              :         }
    7565              : 
    7566              :       /* Now find the specific name in the derived type namespace.  */
    7567          541 :       if (st && st->n.tb && st->n.tb->u.specific)
    7568          541 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7569          541 :                            derived->ns, 1, &st);
    7570          541 :       if (st)
    7571          541 :         *target = st;
    7572              :     }
    7573              : 
    7574         3406 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7575         3406 :       && !e->value.compcall.tbp->deferred)
    7576            1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7577              :                  " itself recursively.  Declare it RECURSIVE or use"
    7578              :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7579              : 
    7580              :   return true;
    7581              : }
    7582              : 
    7583              : 
    7584              : /* Get the ultimate declared type from an expression.  In addition,
    7585              :    return the last class/derived type reference and the copy of the
    7586              :    reference list.  If check_types is set true, derived types are
    7587              :    identified as well as class references.  */
    7588              : static gfc_symbol*
    7589         3321 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7590              :                         gfc_expr *e, bool check_types)
    7591              : {
    7592         3321 :   gfc_symbol *declared;
    7593         3321 :   gfc_ref *ref;
    7594              : 
    7595         3321 :   declared = NULL;
    7596         3321 :   if (class_ref)
    7597         2888 :     *class_ref = NULL;
    7598         3321 :   if (new_ref)
    7599         2595 :     *new_ref = gfc_copy_ref (e->ref);
    7600              : 
    7601         4116 :   for (ref = e->ref; ref; ref = ref->next)
    7602              :     {
    7603          795 :       if (ref->type != REF_COMPONENT)
    7604          292 :         continue;
    7605              : 
    7606          503 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7607          256 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7608          428 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7609              :         {
    7610          354 :           declared = ref->u.c.component->ts.u.derived;
    7611          354 :           if (class_ref)
    7612          332 :             *class_ref = ref;
    7613              :         }
    7614              :     }
    7615              : 
    7616         3321 :   if (declared == NULL)
    7617         2993 :     declared = e->symtree->n.sym->ts.u.derived;
    7618              : 
    7619         3321 :   return declared;
    7620              : }
    7621              : 
    7622              : 
    7623              : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7624              :    which of the specific bindings (if any) matches the arglist and transform
    7625              :    the expression into a call of that binding.  */
    7626              : 
    7627              : static bool
    7628         3410 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7629              : {
    7630         3410 :   gfc_typebound_proc* genproc;
    7631         3410 :   const char* genname;
    7632         3410 :   gfc_symtree *st;
    7633         3410 :   gfc_symbol *derived;
    7634              : 
    7635         3410 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7636         3410 :   genname = e->value.compcall.name;
    7637         3410 :   genproc = e->value.compcall.tbp;
    7638              : 
    7639         3410 :   if (!genproc->is_generic)
    7640              :     return true;
    7641              : 
    7642              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7643          445 :   for (; genproc; genproc = genproc->overridden)
    7644              :     {
    7645          443 :       gfc_tbp_generic* g;
    7646              : 
    7647          443 :       gcc_assert (genproc->is_generic);
    7648          677 :       for (g = genproc->u.generic; g; g = g->next)
    7649              :         {
    7650          667 :           gfc_symbol* target;
    7651          667 :           gfc_actual_arglist* args;
    7652          667 :           bool matches;
    7653              : 
    7654          667 :           gcc_assert (g->specific);
    7655              : 
    7656          667 :           if (g->specific->error)
    7657            0 :             continue;
    7658              : 
    7659          667 :           target = g->specific->u.specific->n.sym;
    7660              : 
    7661              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7662          667 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7663          667 :           if (!g->specific->nopass)
    7664              :             {
    7665          581 :               gfc_expr* po;
    7666          581 :               po = extract_compcall_passed_object (e);
    7667          581 :               if (!po)
    7668              :                 {
    7669            0 :                   gfc_free_actual_arglist (args);
    7670            0 :                   return false;
    7671              :                 }
    7672              : 
    7673          581 :               gcc_assert (g->specific->pass_arg_num > 0);
    7674          581 :               gcc_assert (!g->specific->error);
    7675          581 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7676              :                                           g->specific->pass_arg);
    7677              :             }
    7678         1339 :           resolve_actual_arglist (args, target->attr.proc,
    7679          667 :                                   is_external_proc (target)
    7680            5 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7681              : 
    7682              :           /* Check if this arglist matches the formal.  */
    7683          667 :           matches = gfc_arglist_matches_symbol (&args, target);
    7684              : 
    7685              :           /* Clean up and break out of the loop if we've found it.  */
    7686          667 :           gfc_free_actual_arglist (args);
    7687          667 :           if (matches)
    7688              :             {
    7689          433 :               e->value.compcall.tbp = g->specific;
    7690          433 :               genname = g->specific_st->name;
    7691              :               /* Pass along the name for CLASS methods, where the vtab
    7692              :                  procedure pointer component has to be referenced.  */
    7693          433 :               if (name)
    7694          161 :                 *name = genname;
    7695          433 :               goto success;
    7696              :             }
    7697              :         }
    7698              :     }
    7699              : 
    7700              :   /* Nothing matching found!  */
    7701            2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7702              :              " %qs at %L", genname, &e->where);
    7703            2 :   return false;
    7704              : 
    7705          433 : success:
    7706              :   /* Make sure that we have the right specific instance for the name.  */
    7707          433 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7708              : 
    7709          433 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7710          433 :   if (st)
    7711          433 :     e->value.compcall.tbp = st->n.tb;
    7712              : 
    7713              :   return true;
    7714              : }
    7715              : 
    7716              : 
    7717              : /* Resolve a call to a type-bound subroutine.  */
    7718              : 
    7719              : static bool
    7720         1762 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7721              : {
    7722         1762 :   gfc_actual_arglist* newactual;
    7723         1762 :   gfc_symtree* target;
    7724              : 
    7725              :   /* Check that's really a SUBROUTINE.  */
    7726         1762 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7727              :     {
    7728           17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7729           15 :           && c->expr1->value.compcall.tbp->u.specific
    7730           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7731           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7732           12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7733              :       else
    7734              :         {
    7735            5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7736              :                      c->expr1->value.compcall.name, &c->loc);
    7737            5 :           return false;
    7738              :         }
    7739              :     }
    7740              : 
    7741         1757 :   if (!check_typebound_baseobject (c->expr1))
    7742              :     return false;
    7743              : 
    7744              :   /* Pass along the name for CLASS methods, where the vtab
    7745              :      procedure pointer component has to be referenced.  */
    7746         1750 :   if (name)
    7747          480 :     *name = c->expr1->value.compcall.name;
    7748              : 
    7749         1750 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7750              :     return false;
    7751              : 
    7752              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7753         1749 :   if (overridable)
    7754          371 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7755              : 
    7756              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7757              : 
    7758         1749 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7759              :     return false;
    7760              : 
    7761         1747 :   c->ext.actual = newactual;
    7762         1747 :   c->symtree = target;
    7763         1747 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7764              : 
    7765         1747 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7766              : 
    7767         1747 :   gfc_free_expr (c->expr1);
    7768         1747 :   c->expr1 = gfc_get_expr ();
    7769         1747 :   c->expr1->expr_type = EXPR_FUNCTION;
    7770         1747 :   c->expr1->symtree = target;
    7771         1747 :   c->expr1->where = c->loc;
    7772              : 
    7773         1747 :   return resolve_call (c);
    7774              : }
    7775              : 
    7776              : 
    7777              : /* Resolve a component-call expression.  */
    7778              : static bool
    7779         1669 : resolve_compcall (gfc_expr* e, const char **name)
    7780              : {
    7781         1669 :   gfc_actual_arglist* newactual;
    7782         1669 :   gfc_symtree* target;
    7783              : 
    7784              :   /* Check that's really a FUNCTION.  */
    7785         1669 :   if (!e->value.compcall.tbp->function)
    7786              :     {
    7787            7 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    7788            5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    7789              :                    &e->where);
    7790              :       return false;
    7791              :     }
    7792              : 
    7793              : 
    7794              :   /* These must not be assign-calls!  */
    7795         1662 :   gcc_assert (!e->value.compcall.assign);
    7796              : 
    7797         1662 :   if (!check_typebound_baseobject (e))
    7798              :     return false;
    7799              : 
    7800              :   /* Pass along the name for CLASS methods, where the vtab
    7801              :      procedure pointer component has to be referenced.  */
    7802         1660 :   if (name)
    7803          864 :     *name = e->value.compcall.name;
    7804              : 
    7805         1660 :   if (!resolve_typebound_generic_call (e, name))
    7806              :     return false;
    7807         1659 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7808              : 
    7809              :   /* Take the rank from the function's symbol.  */
    7810         1659 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    7811              :     {
    7812          155 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    7813          155 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    7814              :     }
    7815              : 
    7816              :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    7817              :      arglist to the TBP's binding target.  */
    7818              : 
    7819         1659 :   if (!resolve_typebound_static (e, &target, &newactual))
    7820              :     return false;
    7821              : 
    7822         1659 :   e->value.function.actual = newactual;
    7823         1659 :   e->value.function.name = NULL;
    7824         1659 :   e->value.function.esym = target->n.sym;
    7825         1659 :   e->value.function.isym = NULL;
    7826         1659 :   e->symtree = target;
    7827         1659 :   e->ts = target->n.sym->ts;
    7828         1659 :   e->expr_type = EXPR_FUNCTION;
    7829              : 
    7830              :   /* Resolution is not necessary if this is a class subroutine; this
    7831              :      function only has to identify the specific proc. Resolution of
    7832              :      the call will be done next in resolve_typebound_call.  */
    7833         1659 :   return gfc_resolve_expr (e);
    7834              : }
    7835              : 
    7836              : 
    7837              : static bool resolve_fl_derived (gfc_symbol *sym);
    7838              : 
    7839              : 
    7840              : /* Resolve a typebound function, or 'method'. First separate all
    7841              :    the non-CLASS references by calling resolve_compcall directly.  */
    7842              : 
    7843              : static bool
    7844         1669 : resolve_typebound_function (gfc_expr* e)
    7845              : {
    7846         1669 :   gfc_symbol *declared;
    7847         1669 :   gfc_component *c;
    7848         1669 :   gfc_ref *new_ref;
    7849         1669 :   gfc_ref *class_ref;
    7850         1669 :   gfc_symtree *st;
    7851         1669 :   const char *name;
    7852         1669 :   gfc_typespec ts;
    7853         1669 :   gfc_expr *expr;
    7854         1669 :   bool overridable;
    7855              : 
    7856         1669 :   st = e->symtree;
    7857              : 
    7858              :   /* Deal with typebound operators for CLASS objects.  */
    7859         1669 :   expr = e->value.compcall.base_object;
    7860         1669 :   overridable = !e->value.compcall.tbp->non_overridable;
    7861         1669 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    7862              :     {
    7863              :       /* Since the typebound operators are generic, we have to ensure
    7864              :          that any delays in resolution are corrected and that the vtab
    7865              :          is present.  */
    7866          184 :       ts = expr->ts;
    7867          184 :       declared = ts.u.derived;
    7868          184 :       if (!resolve_fl_derived (declared))
    7869              :         return false;
    7870              : 
    7871          184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7872          184 :       if (c->ts.u.derived == NULL)
    7873            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7874              : 
    7875          184 :       if (!resolve_compcall (e, &name))
    7876              :         return false;
    7877              : 
    7878              :       /* Use the generic name if it is there.  */
    7879          184 :       name = name ? name : e->value.function.esym->name;
    7880          184 :       e->symtree = expr->symtree;
    7881          184 :       e->ref = gfc_copy_ref (expr->ref);
    7882          184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    7883              : 
    7884              :       /* Trim away the extraneous references that emerge from nested
    7885              :          use of interface.cc (extend_expr).  */
    7886          184 :       if (class_ref && class_ref->next)
    7887              :         {
    7888            0 :           gfc_free_ref_list (class_ref->next);
    7889            0 :           class_ref->next = NULL;
    7890              :         }
    7891          184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    7892              :         {
    7893            0 :           gfc_free_ref_list (e->ref);
    7894            0 :           e->ref = NULL;
    7895              :         }
    7896              : 
    7897          184 :       gfc_add_vptr_component (e);
    7898          184 :       gfc_add_component_ref (e, name);
    7899          184 :       e->value.function.esym = NULL;
    7900          184 :       if (expr->expr_type != EXPR_VARIABLE)
    7901           80 :         e->base_expr = expr;
    7902              :       return true;
    7903              :     }
    7904              : 
    7905         1485 :   if (st == NULL)
    7906          195 :     return resolve_compcall (e, NULL);
    7907              : 
    7908         1290 :   if (!gfc_resolve_ref (e))
    7909              :     return false;
    7910              : 
    7911              :   /* It can happen that a generic, typebound procedure is marked as overridable
    7912              :      with all of the specific procedures being non-overridable. If this is the
    7913              :      case, it is safe to resolve the compcall.  */
    7914         1290 :   if (!expr && overridable
    7915         1282 :       && e->value.compcall.tbp->is_generic
    7916          198 :       && e->value.compcall.tbp->u.generic->specific
    7917          197 :       && e->value.compcall.tbp->u.generic->specific->non_overridable)
    7918              :     {
    7919              :       gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
    7920            6 :       for (; g; g = g->next)
    7921            4 :         if (!g->specific->non_overridable)
    7922              :           break;
    7923            2 :       if (g == NULL && resolve_compcall (e, &name))
    7924              :         return true;
    7925              :     }
    7926              : 
    7927              :   /* Get the CLASS declared type.  */
    7928         1288 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7929              : 
    7930         1288 :   if (!resolve_fl_derived (declared))
    7931              :     return false;
    7932              : 
    7933              :   /* Weed out cases of the ultimate component being a derived type.  */
    7934         1288 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7935         1194 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7936              :     {
    7937          608 :       gfc_free_ref_list (new_ref);
    7938          608 :       return resolve_compcall (e, NULL);
    7939              :     }
    7940              : 
    7941          680 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    7942              : 
    7943              :   /* Treat the call as if it is a typebound procedure, in order to roll
    7944              :      out the correct name for the specific function.  */
    7945          680 :   if (!resolve_compcall (e, &name))
    7946              :     {
    7947            3 :       gfc_free_ref_list (new_ref);
    7948            3 :       return false;
    7949              :     }
    7950          677 :   ts = e->ts;
    7951              : 
    7952          677 :   if (overridable)
    7953              :     {
    7954              :       /* Convert the expression to a procedure pointer component call.  */
    7955          675 :       e->value.function.esym = NULL;
    7956          675 :       e->symtree = st;
    7957              : 
    7958          675 :       if (new_ref)
    7959          125 :         e->ref = new_ref;
    7960              : 
    7961              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7962          675 :       gfc_add_vptr_component (e);
    7963          675 :       gfc_add_component_ref (e, name);
    7964              : 
    7965              :       /* Recover the typespec for the expression.  This is really only
    7966              :         necessary for generic procedures, where the additional call
    7967              :         to gfc_add_component_ref seems to throw the collection of the
    7968              :         correct typespec.  */
    7969          675 :       e->ts = ts;
    7970              :     }
    7971            2 :   else if (new_ref)
    7972            0 :     gfc_free_ref_list (new_ref);
    7973              : 
    7974              :   return true;
    7975              : }
    7976              : 
    7977              : /* Resolve a typebound subroutine, or 'method'. First separate all
    7978              :    the non-CLASS references by calling resolve_typebound_call
    7979              :    directly.  */
    7980              : 
    7981              : static bool
    7982         1762 : resolve_typebound_subroutine (gfc_code *code)
    7983              : {
    7984         1762 :   gfc_symbol *declared;
    7985         1762 :   gfc_component *c;
    7986         1762 :   gfc_ref *new_ref;
    7987         1762 :   gfc_ref *class_ref;
    7988         1762 :   gfc_symtree *st;
    7989         1762 :   const char *name;
    7990         1762 :   gfc_typespec ts;
    7991         1762 :   gfc_expr *expr;
    7992         1762 :   bool overridable;
    7993              : 
    7994         1762 :   st = code->expr1->symtree;
    7995              : 
    7996              :   /* Deal with typebound operators for CLASS objects.  */
    7997         1762 :   expr = code->expr1->value.compcall.base_object;
    7998         1762 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7999         1762 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    8000              :     {
    8001              :       /* If the base_object is not a variable, the corresponding actual
    8002              :          argument expression must be stored in e->base_expression so
    8003              :          that the corresponding tree temporary can be used as the base
    8004              :          object in gfc_conv_procedure_call.  */
    8005          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8006              :         {
    8007              :           gfc_actual_arglist *args;
    8008              : 
    8009              :           args= code->expr1->value.function.actual;
    8010              :           for (; args; args = args->next)
    8011              :             if (expr == args->expr)
    8012              :               expr = args->expr;
    8013              :         }
    8014              : 
    8015              :       /* Since the typebound operators are generic, we have to ensure
    8016              :          that any delays in resolution are corrected and that the vtab
    8017              :          is present.  */
    8018          109 :       declared = expr->ts.u.derived;
    8019          109 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    8020          109 :       if (c->ts.u.derived == NULL)
    8021            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8022              : 
    8023          109 :       if (!resolve_typebound_call (code, &name, NULL))
    8024              :         return false;
    8025              : 
    8026              :       /* Use the generic name if it is there.  */
    8027          109 :       name = name ? name : code->expr1->value.function.esym->name;
    8028          109 :       code->expr1->symtree = expr->symtree;
    8029          109 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    8030              : 
    8031              :       /* Trim away the extraneous references that emerge from nested
    8032              :          use of interface.cc (extend_expr).  */
    8033          109 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    8034          109 :       if (class_ref && class_ref->next)
    8035              :         {
    8036            0 :           gfc_free_ref_list (class_ref->next);
    8037            0 :           class_ref->next = NULL;
    8038              :         }
    8039          109 :       else if (code->expr1->ref && !class_ref)
    8040              :         {
    8041           18 :           gfc_free_ref_list (code->expr1->ref);
    8042           18 :           code->expr1->ref = NULL;
    8043              :         }
    8044              : 
    8045              :       /* Now use the procedure in the vtable.  */
    8046          109 :       gfc_add_vptr_component (code->expr1);
    8047          109 :       gfc_add_component_ref (code->expr1, name);
    8048          109 :       code->expr1->value.function.esym = NULL;
    8049          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8050            0 :         code->expr1->base_expr = expr;
    8051              :       return true;
    8052              :     }
    8053              : 
    8054         1653 :   if (st == NULL)
    8055          346 :     return resolve_typebound_call (code, NULL, NULL);
    8056              : 
    8057         1307 :   if (!gfc_resolve_ref (code->expr1))
    8058              :     return false;
    8059              : 
    8060              :   /* Get the CLASS declared type.  */
    8061         1307 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    8062              : 
    8063              :   /* Weed out cases of the ultimate component being a derived type.  */
    8064         1307 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8065         1242 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8066              :     {
    8067          931 :       gfc_free_ref_list (new_ref);
    8068          931 :       return resolve_typebound_call (code, NULL, NULL);
    8069              :     }
    8070              : 
    8071          376 :   if (!resolve_typebound_call (code, &name, &overridable))
    8072              :     {
    8073            5 :       gfc_free_ref_list (new_ref);
    8074            5 :       return false;
    8075              :     }
    8076          371 :   ts = code->expr1->ts;
    8077              : 
    8078          371 :   if (overridable)
    8079              :     {
    8080              :       /* Convert the expression to a procedure pointer component call.  */
    8081          369 :       code->expr1->value.function.esym = NULL;
    8082          369 :       code->expr1->symtree = st;
    8083              : 
    8084          369 :       if (new_ref)
    8085           93 :         code->expr1->ref = new_ref;
    8086              : 
    8087              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8088          369 :       gfc_add_vptr_component (code->expr1);
    8089          369 :       gfc_add_component_ref (code->expr1, name);
    8090              : 
    8091              :       /* Recover the typespec for the expression.  This is really only
    8092              :         necessary for generic procedures, where the additional call
    8093              :         to gfc_add_component_ref seems to throw the collection of the
    8094              :         correct typespec.  */
    8095          369 :       code->expr1->ts = ts;
    8096              :     }
    8097            2 :   else if (new_ref)
    8098            0 :     gfc_free_ref_list (new_ref);
    8099              : 
    8100              :   return true;
    8101              : }
    8102              : 
    8103              : 
    8104              : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    8105              : 
    8106              : static bool
    8107          124 : resolve_ppc_call (gfc_code* c)
    8108              : {
    8109          124 :   gfc_component *comp;
    8110              : 
    8111          124 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    8112          124 :   gcc_assert (comp != NULL);
    8113              : 
    8114          124 :   c->resolved_sym = c->expr1->symtree->n.sym;
    8115          124 :   c->expr1->expr_type = EXPR_VARIABLE;
    8116              : 
    8117          124 :   if (!comp->attr.subroutine)
    8118            1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    8119              : 
    8120          124 :   if (!gfc_resolve_ref (c->expr1))
    8121              :     return false;
    8122              : 
    8123          124 :   if (!update_ppc_arglist (c->expr1))
    8124              :     return false;
    8125              : 
    8126          123 :   c->ext.actual = c->expr1->value.compcall.actual;
    8127              : 
    8128          123 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    8129          123 :                                !(comp->ts.interface
    8130           93 :                                  && comp->ts.interface->formal)))
    8131              :     return false;
    8132              : 
    8133          123 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    8134              :     return false;
    8135              : 
    8136          122 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    8137              : 
    8138          122 :   return true;
    8139              : }
    8140              : 
    8141              : 
    8142              : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    8143              : 
    8144              : static bool
    8145          464 : resolve_expr_ppc (gfc_expr* e)
    8146              : {
    8147          464 :   gfc_component *comp;
    8148              : 
    8149          464 :   comp = gfc_get_proc_ptr_comp (e);
    8150          464 :   gcc_assert (comp != NULL);
    8151              : 
    8152              :   /* Convert to EXPR_FUNCTION.  */
    8153          464 :   e->expr_type = EXPR_FUNCTION;
    8154          464 :   e->value.function.isym = NULL;
    8155          464 :   e->value.function.actual = e->value.compcall.actual;
    8156          464 :   e->ts = comp->ts;
    8157          464 :   if (comp->as != NULL)
    8158              :     {
    8159           28 :       e->rank = comp->as->rank;
    8160           28 :       e->corank = comp->as->corank;
    8161              :     }
    8162              : 
    8163          464 :   if (!comp->attr.function)
    8164            3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    8165              : 
    8166          464 :   if (!gfc_resolve_ref (e))
    8167              :     return false;
    8168              : 
    8169          464 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    8170          464 :                                !(comp->ts.interface
    8171          463 :                                  && comp->ts.interface->formal)))
    8172              :     return false;
    8173              : 
    8174          464 :   if (!update_ppc_arglist (e))
    8175              :     return false;
    8176              : 
    8177          462 :   if (!check_pure_function(e))
    8178              :     return false;
    8179              : 
    8180          461 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    8181              : 
    8182          461 :   return true;
    8183              : }
    8184              : 
    8185              : 
    8186              : static bool
    8187        12176 : gfc_is_expandable_expr (gfc_expr *e)
    8188              : {
    8189        12176 :   gfc_constructor *con;
    8190              : 
    8191        12176 :   if (e->expr_type == EXPR_ARRAY)
    8192              :     {
    8193              :       /* Traverse the constructor looking for variables that are flavor
    8194              :          parameter.  Parameters must be expanded since they are fully used at
    8195              :          compile time.  */
    8196        12176 :       con = gfc_constructor_first (e->value.constructor);
    8197        32259 :       for (; con; con = gfc_constructor_next (con))
    8198              :         {
    8199        14117 :           if (con->expr->expr_type == EXPR_VARIABLE
    8200         5425 :               && con->expr->symtree
    8201         5425 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8202         5425 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8203              :             return true;
    8204         8692 :           if (con->expr->expr_type == EXPR_ARRAY
    8205         8692 :               && gfc_is_expandable_expr (con->expr))
    8206              :             return true;
    8207              :         }
    8208              :     }
    8209              : 
    8210              :   return false;
    8211              : }
    8212              : 
    8213              : 
    8214              : /* Sometimes variables in specification expressions of the result
    8215              :    of module procedures in submodules wind up not being the 'real'
    8216              :    dummy.  Find this, if possible, in the namespace of the first
    8217              :    formal argument.  */
    8218              : 
    8219              : static void
    8220         4919 : fixup_unique_dummy (gfc_expr *e)
    8221              : {
    8222         4919 :   gfc_symtree *st = NULL;
    8223         4919 :   gfc_symbol *s = NULL;
    8224              : 
    8225         4919 :   if (e->symtree->n.sym->ns->proc_name
    8226         4889 :       && e->symtree->n.sym->ns->proc_name->formal)
    8227         4889 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8228              : 
    8229         4889 :   if (s != NULL)
    8230         4889 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8231              : 
    8232         4919 :   if (st != NULL
    8233           14 :       && st->n.sym != NULL
    8234           14 :       && st->n.sym->attr.dummy)
    8235           14 :     e->symtree = st;
    8236         4919 : }
    8237              : 
    8238              : 
    8239              : /* Resolve an expression.  That is, make sure that types of operands agree
    8240              :    with their operators, intrinsic operators are converted to function calls
    8241              :    for overloaded types and unresolved function references are resolved.  */
    8242              : 
    8243              : bool
    8244      7053215 : gfc_resolve_expr (gfc_expr *e)
    8245              : {
    8246      7053215 :   bool t;
    8247      7053215 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8248              : 
    8249      7053215 :   if (e == NULL || e->do_not_resolve_again)
    8250              :     return true;
    8251              : 
    8252              :   /* inquiry_argument only applies to variables.  */
    8253      5107504 :   inquiry_save = inquiry_argument;
    8254      5107504 :   actual_arg_save = actual_arg;
    8255      5107504 :   first_actual_arg_save = first_actual_arg;
    8256              : 
    8257      5107504 :   if (e->expr_type != EXPR_VARIABLE)
    8258              :     {
    8259      3765751 :       inquiry_argument = false;
    8260      3765751 :       actual_arg = false;
    8261      3765751 :       first_actual_arg = false;
    8262              :     }
    8263      1341753 :   else if (e->symtree != NULL
    8264      1341278 :            && *e->symtree->name == '@'
    8265         5649 :            && e->symtree->n.sym->attr.dummy)
    8266              :     {
    8267              :       /* Deal with submodule specification expressions that are not
    8268              :          found to be referenced in module.cc(read_cleanup).  */
    8269         4919 :       fixup_unique_dummy (e);
    8270              :     }
    8271              : 
    8272      5107504 :   switch (e->expr_type)
    8273              :     {
    8274       535852 :     case EXPR_OP:
    8275       535852 :       t = resolve_operator (e);
    8276       535852 :       break;
    8277              : 
    8278          170 :     case EXPR_CONDITIONAL:
    8279          170 :       t = resolve_conditional (e);
    8280          170 :       break;
    8281              : 
    8282      1689656 :     case EXPR_FUNCTION:
    8283      1689656 :     case EXPR_VARIABLE:
    8284              : 
    8285      1689656 :       if (check_host_association (e))
    8286       347939 :         t = resolve_function (e);
    8287              :       else
    8288      1341717 :         t = resolve_variable (e);
    8289              : 
    8290      1689656 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    8291         7379 :           && e->ref->type != REF_SUBSTRING)
    8292         2162 :         gfc_resolve_substring_charlen (e);
    8293              : 
    8294              :       break;
    8295              : 
    8296         1669 :     case EXPR_COMPCALL:
    8297         1669 :       t = resolve_typebound_function (e);
    8298         1669 :       break;
    8299              : 
    8300          508 :     case EXPR_SUBSTRING:
    8301          508 :       t = gfc_resolve_ref (e);
    8302          508 :       break;
    8303              : 
    8304              :     case EXPR_CONSTANT:
    8305              :     case EXPR_NULL:
    8306              :       t = true;
    8307              :       break;
    8308              : 
    8309          464 :     case EXPR_PPC:
    8310          464 :       t = resolve_expr_ppc (e);
    8311          464 :       break;
    8312              : 
    8313        73479 :     case EXPR_ARRAY:
    8314        73479 :       t = false;
    8315        73479 :       if (!gfc_resolve_ref (e))
    8316              :         break;
    8317              : 
    8318        73479 :       t = gfc_resolve_array_constructor (e);
    8319              :       /* Also try to expand a constructor.  */
    8320        73479 :       if (t)
    8321              :         {
    8322        73377 :           gfc_expression_rank (e);
    8323        73377 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8324        68665 :             gfc_expand_constructor (e, false);
    8325              :         }
    8326              : 
    8327              :       /* This provides the opportunity for the length of constructors with
    8328              :          character valued function elements to propagate the string length
    8329              :          to the expression.  */
    8330        73377 :       if (t && e->ts.type == BT_CHARACTER)
    8331              :         {
    8332              :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    8333              :              here rather then add a duplicate test for it above.  */
    8334        10814 :           gfc_expand_constructor (e, false);
    8335        10814 :           t = gfc_resolve_character_array_constructor (e);
    8336              :         }
    8337              : 
    8338              :       break;
    8339              : 
    8340        16782 :     case EXPR_STRUCTURE:
    8341        16782 :       t = gfc_resolve_ref (e);
    8342        16782 :       if (!t)
    8343              :         break;
    8344              : 
    8345        16782 :       t = resolve_structure_cons (e, 0);
    8346        16782 :       if (!t)
    8347              :         break;
    8348              : 
    8349        16770 :       t = gfc_simplify_expr (e, 0);
    8350        16770 :       break;
    8351              : 
    8352            0 :     default:
    8353            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8354              :     }
    8355              : 
    8356      5107504 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8357       184981 :     fixup_charlen (e);
    8358              : 
    8359      5107504 :   inquiry_argument = inquiry_save;
    8360      5107504 :   actual_arg = actual_arg_save;
    8361      5107504 :   first_actual_arg = first_actual_arg_save;
    8362              : 
    8363              :   /* For some reason, resolving these expressions a second time mangles
    8364              :      the typespec of the expression itself.  */
    8365      5107504 :   if (t && e->expr_type == EXPR_VARIABLE
    8366      1338841 :       && e->symtree->n.sym->attr.select_rank_temporary
    8367         3470 :       && UNLIMITED_POLY (e->symtree->n.sym))
    8368           83 :     e->do_not_resolve_again = 1;
    8369              : 
    8370      5104942 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8371         7354 :     t = check_import_status (e);
    8372              : 
    8373              :   return t;
    8374              : }
    8375              : 
    8376              : 
    8377              : /* Resolve an expression from an iterator.  They must be scalar and have
    8378              :    INTEGER or (optionally) REAL type.  */
    8379              : 
    8380              : static bool
    8381       154449 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8382              :                            const char *name_msgid)
    8383              : {
    8384       154449 :   if (!gfc_resolve_expr (expr))
    8385              :     return false;
    8386              : 
    8387       154444 :   if (expr->rank != 0)
    8388              :     {
    8389            0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    8390            0 :       return false;
    8391              :     }
    8392              : 
    8393       154444 :   if (expr->ts.type != BT_INTEGER)
    8394              :     {
    8395          317 :       if (expr->ts.type == BT_REAL)
    8396              :         {
    8397          317 :           if (real_ok)
    8398          314 :             return gfc_notify_std (GFC_STD_F95_DEL,
    8399              :                                    "%s at %L must be integer",
    8400          314 :                                    _(name_msgid), &expr->where);
    8401              :           else
    8402              :             {
    8403            3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    8404              :                          &expr->where);
    8405            3 :               return false;
    8406              :             }
    8407              :         }
    8408              :       else
    8409              :         {
    8410            0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    8411            0 :           return false;
    8412              :         }
    8413              :     }
    8414              :   return true;
    8415              : }
    8416              : 
    8417              : 
    8418              : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    8419              :    false allow only INTEGER type iterators, otherwise allow REAL types.
    8420              :    Set own_scope to true for ac-implied-do and data-implied-do as those
    8421              :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    8422              : 
    8423              : bool
    8424        38621 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8425              : {
    8426        38621 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8427              :     return false;
    8428              : 
    8429        38617 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8430        38617 :                                  _("iterator variable")))
    8431              :     return false;
    8432              : 
    8433        38611 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8434              :                                   "Start expression in DO loop"))
    8435              :     return false;
    8436              : 
    8437        38610 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8438              :                                   "End expression in DO loop"))
    8439              :     return false;
    8440              : 
    8441        38607 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8442              :                                   "Step expression in DO loop"))
    8443              :     return false;
    8444              : 
    8445              :   /* Convert start, end, and step to the same type as var.  */
    8446        38606 :   if (iter->start->ts.kind != iter->var->ts.kind
    8447        38277 :       || iter->start->ts.type != iter->var->ts.type)
    8448          393 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8449              : 
    8450        38606 :   if (iter->end->ts.kind != iter->var->ts.kind
    8451        38304 :       || iter->end->ts.type != iter->var->ts.type)
    8452          345 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8453              : 
    8454        38606 :   if (iter->step->ts.kind != iter->var->ts.kind
    8455        38314 :       || iter->step->ts.type != iter->var->ts.type)
    8456          358 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8457              : 
    8458        38606 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8459              :     {
    8460        37483 :       if ((iter->step->ts.type == BT_INTEGER
    8461        37370 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8462        74851 :           || (iter->step->ts.type == BT_REAL
    8463          113 :               && mpfr_sgn (iter->step->value.real) == 0))
    8464              :         {
    8465            3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8466            3 :                      &iter->step->where);
    8467            3 :           return false;
    8468              :         }
    8469              :     }
    8470              : 
    8471        38603 :   if (iter->start->expr_type == EXPR_CONSTANT
    8472        35461 :       && iter->end->expr_type == EXPR_CONSTANT
    8473        27712 :       && iter->step->expr_type == EXPR_CONSTANT)
    8474              :     {
    8475        27445 :       int sgn, cmp;
    8476        27445 :       if (iter->start->ts.type == BT_INTEGER)
    8477              :         {
    8478        27390 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8479        27390 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8480              :         }
    8481              :       else
    8482              :         {
    8483           55 :           sgn = mpfr_sgn (iter->step->value.real);
    8484           55 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8485              :         }
    8486        27445 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8487          146 :         gfc_warning (OPT_Wzerotrip,
    8488              :                      "DO loop at %L will be executed zero times",
    8489          146 :                      &iter->step->where);
    8490              :     }
    8491              : 
    8492        38603 :   if (iter->end->expr_type == EXPR_CONSTANT
    8493        28080 :       && iter->end->ts.type == BT_INTEGER
    8494        28025 :       && iter->step->expr_type == EXPR_CONSTANT
    8495        27715 :       && iter->step->ts.type == BT_INTEGER
    8496        27715 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8497        27344 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8498              :     {
    8499        26558 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8500        26558 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8501              : 
    8502        26558 :       if (is_step_positive
    8503        26187 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8504            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8505              :                      "DO loop at %L is undefined as it overflows",
    8506            7 :                      &iter->step->where);
    8507              :       else if (!is_step_positive
    8508          371 :                && mpz_cmp (iter->end->value.integer,
    8509          371 :                            gfc_integer_kinds[k].min_int) == 0)
    8510            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8511              :                      "DO loop at %L is undefined as it underflows",
    8512            7 :                      &iter->step->where);
    8513              :     }
    8514              : 
    8515        38603 :   gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8516              :                           VALUE_USED);
    8517        38603 :   gfc_value_used_expr (iter->start, VALUE_USED);
    8518        38603 :   gfc_value_used_expr (iter->end, VALUE_USED);
    8519        38603 :   gfc_value_used_expr (iter->step, VALUE_USED);
    8520              : 
    8521        38603 :   return true;
    8522              : }
    8523              : 
    8524              : 
    8525              : /* Traversal function for find_forall_index.  f == 2 signals that
    8526              :    that variable itself is not to be checked - only the references.  */
    8527              : 
    8528              : static bool
    8529        42682 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8530              : {
    8531        42682 :   if (expr->expr_type != EXPR_VARIABLE)
    8532              :     return false;
    8533              : 
    8534              :   /* A scalar assignment  */
    8535        18195 :   if (!expr->ref || *f == 1)
    8536              :     {
    8537        12133 :       if (expr->symtree->n.sym == sym)
    8538              :         return true;
    8539              :       else
    8540         8117 :         return false;
    8541              :     }
    8542              : 
    8543         6062 :   if (*f == 2)
    8544         1731 :     *f = 1;
    8545              :   return false;
    8546              : }
    8547              : 
    8548              : 
    8549              : /* Check whether the FORALL index appears in the expression or not.
    8550              :    Returns true if SYM is found in EXPR.  */
    8551              : 
    8552              : bool
    8553        27060 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8554              : {
    8555        27060 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8556              :     return true;
    8557              :   else
    8558              :     return false;
    8559              : }
    8560              : 
    8561              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8562              :    This constraint specifies rules for variables in locality-specs.  */
    8563              : 
    8564              : static int
    8565          765 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8566              : {
    8567          765 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8568              : 
    8569          765 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8570              :     {
    8571           22 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8572           22 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8573           24 :            list; list = list->next)
    8574              :         {
    8575            5 :           if (list->expr->symtree->n.sym == sym)
    8576              :             {
    8577            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8578              :                          "must not appear in LOCAL locality-spec at %L",
    8579              :                          sym->name, &(*expr)->where, &list->expr->where);
    8580            3 :               *walk_subtrees = 0;
    8581            3 :               return 1;
    8582              :             }
    8583              :         }
    8584              :     }
    8585              : 
    8586          762 :     *walk_subtrees = 1;
    8587          762 :     return 0;
    8588              : }
    8589              : 
    8590              : static int
    8591         4058 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8592              : {
    8593         4058 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8594              : 
    8595         4058 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8596              :     {
    8597         1866 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8598              : 
    8599         1866 :       if (d->sym_hash->contains (sym))
    8600         1275 :         sym->mark = 1;
    8601              : 
    8602          591 :       else if (d->default_none)
    8603              :         {
    8604            8 :           gfc_namespace *ns2 = d->ns;
    8605           13 :           while (ns2)
    8606              :             {
    8607            8 :               if (ns2 == sym->ns)
    8608              :                 break;
    8609            5 :               ns2 = ns2->parent;
    8610              :             }
    8611              : 
    8612              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.
    8613              :              Use d->code (the DO CONCURRENT node) rather than sym->ns->code,
    8614              :              which may be a different code type (e.g. EXEC_ASSOCIATE) whose
    8615              :              ext union would be read incorrectly.  */
    8616            8 :           for (gfc_forall_iterator *iter = d->code->ext.concur.forall_iterator;
    8617           17 :                iter; iter = iter->next)
    8618              :             {
    8619           10 :               if (!iter->var || !iter->var->symtree)
    8620            0 :                 continue;
    8621           10 :               const char *iter_name = iter->var->symtree->name;
    8622              :               /* Shadow iterators (from inline type-spec: integer :: i = ...)
    8623              :                  store the iterator with a leading underscore internally; the
    8624              :                  user-visible name does not have the underscore.  */
    8625           10 :               if (iter->shadow)
    8626            0 :                 iter_name++;
    8627           10 :               if (strcmp (sym->name, iter_name) == 0)
    8628            1 :                 return 0;
    8629              :             }
    8630              : 
    8631              :           /* A named constant is not a variable, so skip test.  */
    8632            7 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8633              :             {
    8634            2 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8635              :                         "of DO CONCURRENT at %L but required due to "
    8636              :                         "DEFAULT (NONE)",
    8637              :                         sym->name, &(*e)->where, &d->code->loc);
    8638            2 :               d->sym_hash->add (sym);
    8639              :             }
    8640              :         }
    8641              :     }
    8642              :   return 0;
    8643              : }
    8644              : 
    8645              : static void
    8646          224 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8647              : {
    8648          224 :   struct check_default_none_data data;
    8649          224 :   data.code = code;
    8650          224 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8651          224 :   data.ns = ns;
    8652          224 :   data.default_none = code->ext.concur.default_none;
    8653              : 
    8654         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8655              :     {
    8656          896 :       const char *name;
    8657          896 :       switch (locality)
    8658              :         {
    8659              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8660          224 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8661          224 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8662          224 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8663              :           default: gcc_unreachable ();
    8664              :         }
    8665              : 
    8666         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8667          391 :            list = list->next)
    8668              :         {
    8669          391 :           gfc_expr *expr = list->expr;
    8670              : 
    8671          391 :           if (locality == LOCALITY_REDUCE
    8672           72 :               && (expr->expr_type == EXPR_FUNCTION
    8673           48 :                   || expr->expr_type == EXPR_OP))
    8674           35 :             continue;
    8675              : 
    8676          367 :           if (!gfc_resolve_expr (expr))
    8677            3 :             continue;
    8678              : 
    8679          364 :           if (expr->expr_type != EXPR_VARIABLE
    8680          364 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8681          364 :               || (expr->ref
    8682          151 :                   && (expr->ref->type != REF_ARRAY
    8683          151 :                       || expr->ref->u.ar.type != AR_FULL
    8684          147 :                       || expr->ref->next)))
    8685              :             {
    8686            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8687              :                          name, &expr->where);
    8688            4 :                 continue;
    8689              :             }
    8690              : 
    8691          360 :           gfc_symbol *sym = expr->symtree->n.sym;
    8692              : 
    8693          360 :           if (data.sym_hash->contains (sym))
    8694              :             {
    8695            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8696              :                          "locality-spec", sym->name, &expr->where);
    8697            4 :               continue;
    8698              :             }
    8699              : 
    8700          356 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8701          716 :                iter; iter = iter->next)
    8702              :             {
    8703          360 :               if (iter->var->symtree->n.sym == sym)
    8704              :                 {
    8705            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8706              :                              "locality-spec", sym->name, &expr->where);
    8707            1 :                   continue;
    8708              :                 }
    8709              : 
    8710          359 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8711              :             }
    8712              : 
    8713          356 :           if (locality == LOCALITY_LOCAL
    8714          356 :               || locality == LOCALITY_LOCAL_INIT
    8715          356 :               || locality == LOCALITY_REDUCE)
    8716              :             {
    8717          198 :               if (sym->attr.optional)
    8718            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8719              :                            "locality-spec at %L",
    8720              :                            sym->name, name, &expr->where);
    8721              : 
    8722          198 :               if (sym->attr.dimension
    8723           66 :                   && sym->as
    8724           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8725            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8726              :                            "locality-spec at %L",
    8727              :                            sym->name, name, &expr->where);
    8728              : 
    8729          198 :               gfc_check_vardef_context (expr, false, false, false, name);
    8730              :             }
    8731              : 
    8732          198 :           if (locality == LOCALITY_LOCAL
    8733              :               || locality == LOCALITY_LOCAL_INIT)
    8734              :             {
    8735          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    8736              : 
    8737          181 :               if (attr.allocatable)
    8738            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8739              :                            "locality-spec at %L",
    8740              :                            sym->name, name, &expr->where);
    8741              : 
    8742          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8743            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8744              :                            " for %qs in %s locality-spec at %L",
    8745              :                            sym->name, name, &expr->where);
    8746              : 
    8747          177 :               else if (attr.codimension)
    8748            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8749              :                            "at %L",
    8750              :                            sym->name, name, &expr->where);
    8751              : 
    8752          177 :               else if (expr->ts.type == BT_DERIVED
    8753          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8754            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8755              :                            "locality-spec at %L",
    8756              :                            sym->name, name, &expr->where);
    8757              : 
    8758          177 :               else if (gfc_has_ultimate_allocatable (expr))
    8759            4 :                 gfc_error ("Type with ultimate allocatable component not "
    8760              :                            "permitted for %qs in %s locality-spec at %L",
    8761              :                            sym->name, name, &expr->where);
    8762              :             }
    8763              : 
    8764          175 :           else if (locality == LOCALITY_REDUCE)
    8765              :             {
    8766           17 :               if (sym->attr.asynchronous)
    8767            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8768              :                            "REDUCE locality-spec at %L",
    8769              :                            sym->name, &expr->where);
    8770           17 :               if (sym->attr.volatile_)
    8771            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8772              :                            "locality-spec at %L", sym->name, &expr->where);
    8773              :             }
    8774              : 
    8775          356 :           data.sym_hash->add (sym);
    8776              :         }
    8777              : 
    8778          896 :       if (locality == LOCALITY_LOCAL)
    8779              :         {
    8780          224 :           gcc_assert (locality == 0);
    8781              : 
    8782          224 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8783          467 :                iter; iter = iter->next)
    8784              :             {
    8785          243 :               gfc_expr_walker (&iter->start,
    8786              :                                do_concur_locality_specs_f2023,
    8787              :                                &data);
    8788              : 
    8789          243 :               gfc_expr_walker (&iter->end,
    8790              :                                do_concur_locality_specs_f2023,
    8791              :                                &data);
    8792              : 
    8793          243 :               gfc_expr_walker (&iter->stride,
    8794              :                                do_concur_locality_specs_f2023,
    8795              :                                &data);
    8796              :             }
    8797              : 
    8798          224 :           if (code->expr1)
    8799            7 :             gfc_expr_walker (&code->expr1,
    8800              :                              do_concur_locality_specs_f2023,
    8801              :                              &data);
    8802              :         }
    8803              :     }
    8804              : 
    8805          224 :   gfc_expr *reduce_op = NULL;
    8806              : 
    8807          224 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8808          272 :        list; list = list->next)
    8809              :     {
    8810           48 :       gfc_expr *expr = list->expr;
    8811              : 
    8812           48 :       if (expr->expr_type != EXPR_VARIABLE)
    8813              :         {
    8814           24 :           reduce_op = expr;
    8815           24 :           continue;
    8816              :         }
    8817              : 
    8818           24 :       if (reduce_op->expr_type == EXPR_OP)
    8819              :         {
    8820           17 :           switch (reduce_op->value.op.op)
    8821              :             {
    8822           17 :               case INTRINSIC_PLUS:
    8823           17 :               case INTRINSIC_TIMES:
    8824           17 :                 if (!gfc_numeric_ts (&expr->ts))
    8825            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8826            3 :                              "got %s", expr->symtree->n.sym->name,
    8827              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8828              :                 break;
    8829            0 :               case INTRINSIC_AND:
    8830            0 :               case INTRINSIC_OR:
    8831            0 :               case INTRINSIC_EQV:
    8832            0 :               case INTRINSIC_NEQV:
    8833            0 :                 if (expr->ts.type != BT_LOGICAL)
    8834            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8835            0 :                              "got %qs", expr->symtree->n.sym->name,
    8836              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8837              :                 break;
    8838            0 :               default:
    8839            0 :                 gcc_unreachable ();
    8840              :             }
    8841              :         }
    8842              : 
    8843            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8844              :         {
    8845            7 :           switch (reduce_op->value.function.isym->id)
    8846              :             {
    8847            6 :               case GFC_ISYM_MIN:
    8848            6 :               case GFC_ISYM_MAX:
    8849            6 :                 if (expr->ts.type != BT_INTEGER
    8850              :                     && expr->ts.type != BT_REAL
    8851              :                     && expr->ts.type != BT_CHARACTER)
    8852            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8853              :                              "in REDUCE with MIN/MAX at %L, got %s",
    8854            2 :                              expr->symtree->n.sym->name, &expr->where,
    8855              :                              gfc_basic_typename (expr->ts.type));
    8856              :                 break;
    8857            1 :               case GFC_ISYM_IAND:
    8858            1 :               case GFC_ISYM_IOR:
    8859            1 :               case GFC_ISYM_IEOR:
    8860            1 :                 if (expr->ts.type != BT_INTEGER)
    8861            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8862              :                              "IAND/IOR/IEOR at %L, got %s",
    8863            1 :                              expr->symtree->n.sym->name, &expr->where,
    8864              :                              gfc_basic_typename (expr->ts.type));
    8865              :                 break;
    8866            0 :               default:
    8867            0 :                 gcc_unreachable ();
    8868              :             }
    8869              :         }
    8870              : 
    8871              :       else
    8872            0 :         gcc_unreachable ();
    8873              :     }
    8874              : 
    8875         1120 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8876              :     {
    8877         1287 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8878          391 :            list = list->next)
    8879              :         {
    8880          391 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8881          367 :             list->expr->symtree->n.sym->mark = 0;
    8882              :         }
    8883              :     }
    8884              : 
    8885          224 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8886              :                    check_default_none_expr, &data);
    8887              : 
    8888         1344 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8889              :     {
    8890          896 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8891         1287 :       while (*plist)
    8892              :         {
    8893          391 :           gfc_expr *expr = (*plist)->expr;
    8894          391 :           if (expr->expr_type == EXPR_VARIABLE)
    8895              :             {
    8896          367 :               gfc_symbol *sym = expr->symtree->n.sym;
    8897          367 :               if (sym->mark == 0)
    8898              :                 {
    8899           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8900              :                                "locality-spec at %L is not used",
    8901              :                                sym->name, &expr->where);
    8902           70 :                   gfc_expr_list *tmp = *plist;
    8903           70 :                   *plist = (*plist)->next;
    8904           70 :                   gfc_free_expr (tmp->expr);
    8905           70 :                   free (tmp);
    8906           70 :                   continue;
    8907           70 :                 }
    8908              :             }
    8909          321 :           plist = &((*plist)->next);
    8910              :         }
    8911              :     }
    8912              : 
    8913          448 :   delete data.sym_hash;
    8914          224 : }
    8915              : 
    8916              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8917              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8918              :    INTEGERs, and if stride is a constant it must be nonzero.
    8919              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8920              :    not contain a reference to any index-name in the
    8921              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8922              : 
    8923              : static void
    8924         2217 : resolve_forall_iterators (gfc_forall_iterator *it)
    8925              : {
    8926         2217 :   gfc_forall_iterator *iter, *iter2;
    8927              : 
    8928         6352 :   for (iter = it; iter; iter = iter->next)
    8929              :     {
    8930         4135 :       if (gfc_resolve_expr (iter->var)
    8931         4135 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8932            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8933              :                    &iter->var->where);
    8934              : 
    8935         4135 :       if (gfc_resolve_expr (iter->start)
    8936         4135 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8937            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8938              :                    &iter->start->where);
    8939         4135 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8940            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8941              : 
    8942         4135 :       if (gfc_resolve_expr (iter->end)
    8943         4135 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8944            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8945              :                    &iter->end->where);
    8946         4135 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8947            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8948              : 
    8949         4135 :       if (gfc_resolve_expr (iter->stride))
    8950              :         {
    8951         4135 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8952            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8953              :                        &iter->stride->where, "INTEGER");
    8954              : 
    8955         4135 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8956         4131 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8957            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8958              :                        &iter->stride->where);
    8959              :         }
    8960         4135 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8961            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8962              : 
    8963         4135 :       gfc_value_set_and_used (iter->var, &iter->var->where, VALUE_VARDEF,
    8964              :                               VALUE_USED);
    8965         4135 :       gfc_value_used_expr (iter->start, VALUE_USED);
    8966         4135 :       gfc_value_used_expr (iter->end, VALUE_USED);
    8967         4135 :       gfc_value_used_expr (iter->stride, VALUE_USED);
    8968              :     }
    8969              : 
    8970         6352 :   for (iter = it; iter; iter = iter->next)
    8971        11114 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8972              :       {
    8973         6979 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8974         6977 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8975        13954 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8976            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8977            6 :                      "specification at %L", iter->var->symtree->name,
    8978            6 :                      &iter2->start->where);
    8979              :       }
    8980         2217 : }
    8981              : 
    8982              : 
    8983              : /* Given a pointer to a symbol that is a derived type, see if it's
    8984              :    inaccessible, i.e. if it's defined in another module and the components are
    8985              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8986              :    inaccessible components are found, nonzero otherwise.  */
    8987              : 
    8988              : static bool
    8989         1358 : derived_inaccessible (gfc_symbol *sym)
    8990              : {
    8991         1358 :   gfc_component *c;
    8992              : 
    8993         1358 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8994              :     return 1;
    8995              : 
    8996         4013 :   for (c = sym->components; c; c = c->next)
    8997              :     {
    8998              :         /* Prevent an infinite loop through this function.  */
    8999         2668 :         if (c->ts.type == BT_DERIVED
    9000          289 :             && (c->attr.pointer || c->attr.allocatable)
    9001           72 :             && sym == c->ts.u.derived)
    9002           72 :           continue;
    9003              : 
    9004         2596 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    9005              :           return 1;
    9006              :     }
    9007              : 
    9008              :   return 0;
    9009              : }
    9010              : 
    9011              : 
    9012              : /* Resolve the argument of a deallocate expression.  The expression must be
    9013              :    a pointer or a full array.  */
    9014              : 
    9015              : static bool
    9016         8493 : resolve_deallocate_expr (gfc_expr *e)
    9017              : {
    9018         8493 :   symbol_attribute attr;
    9019         8493 :   int allocatable, pointer;
    9020         8493 :   gfc_ref *ref;
    9021         8493 :   gfc_symbol *sym;
    9022         8493 :   gfc_component *c;
    9023         8493 :   bool unlimited;
    9024              : 
    9025         8493 :   if (!gfc_resolve_expr (e))
    9026              :     return false;
    9027              : 
    9028         8493 :   if (e->expr_type != EXPR_VARIABLE)
    9029            0 :     goto bad;
    9030              : 
    9031         8493 :   sym = e->symtree->n.sym;
    9032         8493 :   unlimited = UNLIMITED_POLY(sym);
    9033              : 
    9034         8493 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    9035              :     {
    9036         1604 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    9037         1604 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    9038              :     }
    9039              :   else
    9040              :     {
    9041         6889 :       allocatable = sym->attr.allocatable;
    9042         6889 :       pointer = sym->attr.pointer;
    9043              :     }
    9044        17065 :   for (ref = e->ref; ref; ref = ref->next)
    9045              :     {
    9046         8572 :       switch (ref->type)
    9047              :         {
    9048         6390 :         case REF_ARRAY:
    9049         6390 :           if (ref->u.ar.type != AR_FULL
    9050         6628 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9051          238 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9052              :             allocatable = 0;
    9053              :           break;
    9054              : 
    9055         2182 :         case REF_COMPONENT:
    9056         2182 :           c = ref->u.c.component;
    9057         2182 :           if (c->ts.type == BT_CLASS)
    9058              :             {
    9059          303 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9060          303 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9061              :             }
    9062              :           else
    9063              :             {
    9064         1879 :               allocatable = c->attr.allocatable;
    9065         1879 :               pointer = c->attr.pointer;
    9066              :             }
    9067              :           break;
    9068              : 
    9069              :         case REF_SUBSTRING:
    9070              :         case REF_INQUIRY:
    9071         8572 :           allocatable = 0;
    9072              :           break;
    9073              :         }
    9074              :     }
    9075              : 
    9076         8493 :   attr = gfc_expr_attr (e);
    9077              : 
    9078         8493 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9079              :     {
    9080            3 :     bad:
    9081            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9082              :                  &e->where);
    9083            3 :       return false;
    9084              :     }
    9085              : 
    9086              :   /* F2008, C644.  */
    9087         8490 :   if (gfc_is_coindexed (e))
    9088              :     {
    9089            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9090            1 :       return false;
    9091              :     }
    9092              : 
    9093         8489 :   if (pointer
    9094        10887 :       && !gfc_check_vardef_context (e, true, true, false,
    9095         2398 :                                     _("DEALLOCATE object")))
    9096              :     return false;
    9097         8487 :   if (!gfc_check_vardef_context (e, false, true, false,
    9098         8487 :                                  _("DEALLOCATE object")))
    9099              :     return false;
    9100              : 
    9101              :   return true;
    9102              : }
    9103              : 
    9104              : 
    9105              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9106              : static bool
    9107        47403 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9108              : {
    9109        47403 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9110         2081 :     return true;
    9111              : 
    9112              :   return false;
    9113              : }
    9114              : 
    9115              : bool
    9116        20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9117              : {
    9118        20080 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9119              : }
    9120              : 
    9121              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9122              :    of character expressions.  */
    9123              : static bool
    9124        20499 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9125              : {
    9126            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9127              : }
    9128              : 
    9129              : 
    9130              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9131              :    allocated, get the expression node to be initialized afterwards (needed for
    9132              :    derived types with default initializers, and derived types with allocatable
    9133              :    components that need nullification.)  */
    9134              : 
    9135              : gfc_expr *
    9136         5915 : gfc_expr_to_initialize (gfc_expr *e)
    9137              : {
    9138         5915 :   gfc_expr *result;
    9139         5915 :   gfc_ref *ref;
    9140         5915 :   int i;
    9141              : 
    9142         5915 :   result = gfc_copy_expr (e);
    9143              : 
    9144              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9145        11668 :   for (ref = result->ref; ref; ref = ref->next)
    9146         9153 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9147              :       {
    9148         3400 :         if (ref->u.ar.dimen == 0
    9149           89 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9150              :           return result;
    9151              : 
    9152         3311 :         ref->u.ar.type = AR_FULL;
    9153              : 
    9154         7474 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9155         4163 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9156              : 
    9157              :         break;
    9158              :       }
    9159              : 
    9160         5826 :   gfc_free_shape (&result->shape, result->rank);
    9161              : 
    9162              :   /* Recalculate rank, shape, etc.  */
    9163         5826 :   gfc_resolve_expr (result);
    9164         5826 :   return result;
    9165              : }
    9166              : 
    9167              : 
    9168              : /* If the last ref of an expression is an array ref, return a copy of the
    9169              :    expression with that one removed.  Otherwise, a copy of the original
    9170              :    expression.  This is used for allocate-expressions and pointer assignment
    9171              :    LHS, where there may be an array specification that needs to be stripped
    9172              :    off when using gfc_check_vardef_context.  */
    9173              : 
    9174              : static gfc_expr*
    9175        28105 : remove_last_array_ref (gfc_expr* e)
    9176              : {
    9177        28105 :   gfc_expr* e2;
    9178        28105 :   gfc_ref** r;
    9179              : 
    9180        28105 :   e2 = gfc_copy_expr (e);
    9181        36364 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9182        24857 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9183              :       {
    9184        16598 :         gfc_free_ref_list (*r);
    9185        16598 :         *r = NULL;
    9186        16598 :         break;
    9187              :       }
    9188              : 
    9189        28105 :   return e2;
    9190              : }
    9191              : 
    9192              : 
    9193              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9194              :    a source-expr are conformable.  This does not catch all possible
    9195              :    cases; in particular a runtime checking is needed.  */
    9196              : 
    9197              : static bool
    9198         1928 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9199              : {
    9200         1928 :   gfc_ref *tail;
    9201         1928 :   bool scalar;
    9202              : 
    9203         2672 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9204              : 
    9205              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9206              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9207              :      so let's emit a warning if -Wsurprising is given.  */
    9208         1928 :   scalar = !tail || tail->type == REF_COMPONENT;
    9209         1928 :   if (e1->mold && e1->rank > 0
    9210          166 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9211              :     {
    9212           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9213           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9214              :                      "but MOLD= expression at %L has rank %d",
    9215            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9216              :                      &e1->where, e1->rank);
    9217              :       return true;
    9218              :     }
    9219              : 
    9220              :   /* First compare rank.  */
    9221         1898 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9222            2 :       || (!tail && e1->rank != e2->rank))
    9223              :     {
    9224            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9225              :                  "same rank as the allocate-object at %L",
    9226              :                  &e1->where, &e2->where);
    9227            7 :       return false;
    9228              :     }
    9229              : 
    9230         1891 :   if (e1->shape)
    9231              :     {
    9232         1373 :       int i;
    9233         1373 :       mpz_t s;
    9234              : 
    9235         1373 :       mpz_init (s);
    9236              : 
    9237         3165 :       for (i = 0; i < e1->rank; i++)
    9238              :         {
    9239         1379 :           if (tail->u.ar.start[i] == NULL)
    9240              :             break;
    9241              : 
    9242          419 :           if (tail->u.ar.end[i])
    9243              :             {
    9244           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9245           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9246           54 :               mpz_add_ui (s, s, 1);
    9247              :             }
    9248              :           else
    9249              :             {
    9250          365 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9251              :             }
    9252              : 
    9253          419 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9254              :             {
    9255            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9256              :                          "have the same shape", &e1->where, &e2->where);
    9257            0 :               mpz_clear (s);
    9258            0 :               return false;
    9259              :             }
    9260              :         }
    9261              : 
    9262         1373 :       mpz_clear (s);
    9263              :     }
    9264              : 
    9265              :   return true;
    9266              : }
    9267              : 
    9268              : 
    9269              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9270              :    checks to see whether the expression is OK or not.  The expression must
    9271              :    have a trailing array reference that gives the size of the array.  */
    9272              : 
    9273              : static bool
    9274        17639 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9275              : {
    9276        17639 :   int i, pointer, allocatable, dimension, is_abstract;
    9277        17639 :   int codimension;
    9278        17639 :   bool coindexed;
    9279        17639 :   bool unlimited;
    9280        17639 :   symbol_attribute attr;
    9281        17639 :   gfc_ref *ref, *ref2;
    9282        17639 :   gfc_expr *e2;
    9283        17639 :   gfc_array_ref *ar;
    9284        17639 :   gfc_symbol *sym = NULL;
    9285        17639 :   gfc_alloc *a;
    9286        17639 :   gfc_component *c;
    9287        17639 :   bool t;
    9288              : 
    9289              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9290              :      checking of coarrays.  */
    9291        22539 :   for (ref = e->ref; ref; ref = ref->next)
    9292        18270 :     if (ref->next == NULL)
    9293              :       break;
    9294              : 
    9295        17639 :   if (ref && ref->type == REF_ARRAY)
    9296        12175 :     ref->u.ar.in_allocate = true;
    9297              : 
    9298        17639 :   if (!gfc_resolve_expr (e))
    9299            1 :     goto failure;
    9300              : 
    9301              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9302              :      pointer, the next-to-last reference must be a pointer.  */
    9303              : 
    9304        17638 :   ref2 = NULL;
    9305        17638 :   if (e->symtree)
    9306        17638 :     sym = e->symtree->n.sym;
    9307              : 
    9308              :   /* Check whether ultimate component is abstract and CLASS.  */
    9309        35276 :   is_abstract = 0;
    9310              : 
    9311              :   /* Is the allocate-object unlimited polymorphic?  */
    9312        17638 :   unlimited = UNLIMITED_POLY(e);
    9313              : 
    9314        17638 :   if (e->expr_type != EXPR_VARIABLE)
    9315              :     {
    9316            0 :       allocatable = 0;
    9317            0 :       attr = gfc_expr_attr (e);
    9318            0 :       pointer = attr.pointer;
    9319            0 :       dimension = attr.dimension;
    9320            0 :       codimension = attr.codimension;
    9321              :     }
    9322              :   else
    9323              :     {
    9324        17638 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9325              :         {
    9326         3528 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9327         3528 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9328         3528 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9329         3528 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9330         3528 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9331              :         }
    9332              :       else
    9333              :         {
    9334        14110 :           allocatable = sym->attr.allocatable;
    9335        14110 :           pointer = sym->attr.pointer;
    9336        14110 :           dimension = sym->attr.dimension;
    9337        14110 :           codimension = sym->attr.codimension;
    9338              :         }
    9339              : 
    9340        17638 :       coindexed = false;
    9341              : 
    9342        35902 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9343              :         {
    9344        18266 :           switch (ref->type)
    9345              :             {
    9346        13658 :               case REF_ARRAY:
    9347        13658 :                 if (ref->u.ar.codimen > 0)
    9348              :                   {
    9349          803 :                     int n;
    9350         1104 :                     for (n = ref->u.ar.dimen;
    9351         1104 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9352          844 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9353              :                         {
    9354              :                           coindexed = true;
    9355              :                           break;
    9356              :                         }
    9357              :                    }
    9358              : 
    9359        13658 :                 if (ref->next != NULL)
    9360         1485 :                   pointer = 0;
    9361              :                 break;
    9362              : 
    9363         4608 :               case REF_COMPONENT:
    9364              :                 /* F2008, C644.  */
    9365         4608 :                 if (coindexed)
    9366              :                   {
    9367            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9368              :                                &e->where);
    9369            2 :                     goto failure;
    9370              :                   }
    9371              : 
    9372         4606 :                 c = ref->u.c.component;
    9373         4606 :                 if (c->ts.type == BT_CLASS)
    9374              :                   {
    9375         1012 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9376         1012 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9377         1012 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9378         1012 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9379         1012 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9380              :                   }
    9381              :                 else
    9382              :                   {
    9383         3594 :                     allocatable = c->attr.allocatable;
    9384         3594 :                     pointer = c->attr.pointer;
    9385         3594 :                     dimension = c->attr.dimension;
    9386         3594 :                     codimension = c->attr.codimension;
    9387         3594 :                     is_abstract = c->attr.abstract;
    9388              :                   }
    9389              :                 break;
    9390              : 
    9391            0 :               case REF_SUBSTRING:
    9392            0 :               case REF_INQUIRY:
    9393            0 :                 allocatable = 0;
    9394            0 :                 pointer = 0;
    9395            0 :                 break;
    9396              :             }
    9397              :         }
    9398              :     }
    9399              : 
    9400              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9401              :      pointer or an allocatable variable.  */
    9402        17636 :   if (allocatable == 0 && pointer == 0)
    9403              :     {
    9404            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9405              :                  &e->where);
    9406            4 :       goto failure;
    9407              :     }
    9408              : 
    9409              :   /* Some checks for the SOURCE tag.  */
    9410        17632 :   if (code->expr3)
    9411              :     {
    9412              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9413              :          rank as allocate-object".  This would require the MOLD argument to
    9414              :          NULL() as source-expr for subsequent checking.  However, even the
    9415              :          resulting disassociated pointer or unallocated array has no shape that
    9416              :          could be used for SOURCE= or MOLD=.  */
    9417         3924 :       if (code->expr3->expr_type == EXPR_NULL)
    9418              :         {
    9419            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9420              :                      &code->expr3->where);
    9421            4 :           goto failure;
    9422              :         }
    9423              : 
    9424              :       /* Check F03:C631.  */
    9425         3920 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9426              :         {
    9427           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9428           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9429           10 :           goto failure;
    9430              :         }
    9431              : 
    9432              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9433         3910 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9434            7 :         goto failure;
    9435              : 
    9436              :       /* Check F03:C633.  */
    9437         3903 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9438              :         {
    9439            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9440              :                      "shall have the same kind type parameter",
    9441              :                      &e->where, &code->expr3->where);
    9442            1 :           goto failure;
    9443              :         }
    9444              : 
    9445              :       /* Check F2008, C642.  */
    9446         3902 :       if (code->expr3->ts.type == BT_DERIVED
    9447         3902 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9448         1222 :               || (code->expr3->ts.u.derived->from_intmod
    9449              :                      == INTMOD_ISO_FORTRAN_ENV
    9450            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9451              :                      == ISOFORTRAN_LOCK_TYPE)))
    9452              :         {
    9453            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9454              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9455              :                       "allocate-object at %L is a coarray",
    9456            0 :                       &code->expr3->where, &e->where);
    9457            0 :           goto failure;
    9458              :         }
    9459              : 
    9460              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9461              :          allocate-object and source-expr shall have the same values."  */
    9462         3902 :       if (e->ts.type == BT_CHARACTER
    9463          816 :           && !e->ts.deferred
    9464          162 :           && e->ts.u.cl->length
    9465          162 :           && code->expr3->ts.type == BT_CHARACTER
    9466         4064 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9467              :                                      "SOURCE= or MOLD= specifier"))
    9468           17 :             goto failure;
    9469              : 
    9470              :       /* Check TS18508, C702/C703.  */
    9471         3885 :       if (code->expr3->ts.type == BT_DERIVED
    9472         5107 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9473         1222 :               || (code->expr3->ts.u.derived->from_intmod
    9474              :                      == INTMOD_ISO_FORTRAN_ENV
    9475            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9476              :                      == ISOFORTRAN_EVENT_TYPE)))
    9477              :         {
    9478            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9479              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9480              :                       "allocate-object at %L is a coarray",
    9481            0 :                       &code->expr3->where, &e->where);
    9482            0 :           goto failure;
    9483              :         }
    9484              :     }
    9485              : 
    9486              :   /* Check F08:C629.  */
    9487        17593 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9488          159 :       && !code->expr3)
    9489              :     {
    9490            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9491            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9492              :                  "type-spec or source-expr", sym->name, &e->where);
    9493            2 :       goto failure;
    9494              :     }
    9495              : 
    9496              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9497              :      if and only if each allocate-object is a dummy argument for which the
    9498              :      corresponding type parameter is assumed.  */
    9499        17591 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9500          515 :       && code->ext.alloc.ts.u.cl->length != NULL
    9501          500 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9502           23 :       && e->ts.u.cl->length == NULL
    9503            2 :       && e->symtree->n.sym->attr.dummy)
    9504              :     {
    9505            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9506              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9507              :                  "dummy argument with assumed type parameter",
    9508              :                  sym->name, &e->where);
    9509            2 :       goto failure;
    9510              :     }
    9511              : 
    9512              :   /* Check F08:C632.  */
    9513        17589 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9514           60 :       && !UNLIMITED_POLY (e))
    9515              :     {
    9516           36 :       int cmp;
    9517              : 
    9518           36 :       if (!e->ts.u.cl->length)
    9519           15 :         goto failure;
    9520              : 
    9521           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9522           21 :                                   code->ext.alloc.ts.u.cl->length);
    9523           21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9524              :         {
    9525            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9526              :                      "character-length parameter as in the declaration",
    9527              :                      sym->name, &e->where);
    9528            2 :           goto failure;
    9529              :         }
    9530              :     }
    9531              : 
    9532              :   /* In the variable definition context checks, gfc_expr_attr is used
    9533              :      on the expression.  This is fooled by the array specification
    9534              :      present in e, thus we have to eliminate that one temporarily.  */
    9535        17572 :   e2 = remove_last_array_ref (e);
    9536        17572 :   t = true;
    9537        17572 :   if (t && pointer)
    9538         3927 :     t = gfc_check_vardef_context (e2, true, true, false,
    9539         3927 :                                   _("ALLOCATE object"));
    9540         3927 :   if (t)
    9541        17564 :     t = gfc_check_vardef_context (e2, false, true, false,
    9542        17564 :                                   _("ALLOCATE object"));
    9543        17572 :   gfc_free_expr (e2);
    9544        17572 :   if (!t)
    9545           11 :     goto failure;
    9546              : 
    9547        17561 :   code->ext.alloc.expr3_not_explicit = 0;
    9548        17561 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9549         1683 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9550              :     {
    9551              :       /* For class arrays, the initialization with SOURCE is done
    9552              :          using _copy and trans_call. It is convenient to exploit that
    9553              :          when the allocated type is different from the declared type but
    9554              :          no SOURCE exists by setting expr3.  */
    9555          341 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9556          341 :       code->ext.alloc.expr3_not_explicit = 1;
    9557              :     }
    9558        17220 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9559         2677 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9560            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9561              :     {
    9562              :       /* We have to zero initialize the integer variable.  */
    9563            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9564            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9565              :     }
    9566              : 
    9567        17561 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9568              :     {
    9569              :       /* Make sure the vtab symbol is present when
    9570              :          the module variables are generated.  */
    9571         3080 :       gfc_typespec ts = e->ts;
    9572         3080 :       if (code->expr3)
    9573         1343 :         ts = code->expr3->ts;
    9574         1737 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9575          768 :         ts = code->ext.alloc.ts;
    9576              : 
    9577              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9578              :          statement is necessary.  */
    9579         3080 :       gfc_find_derived_vtab (ts.u.derived);
    9580         3080 :     }
    9581        14481 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9582              :     {
    9583              :       /* Again, make sure the vtab symbol is present when
    9584              :          the module variables are generated.  */
    9585          440 :       gfc_typespec *ts = NULL;
    9586          440 :       if (code->expr3)
    9587          353 :         ts = &code->expr3->ts;
    9588              :       else
    9589           87 :         ts = &code->ext.alloc.ts;
    9590              : 
    9591          440 :       gcc_assert (ts);
    9592              : 
    9593              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9594              :          statement is necessary.  */
    9595          440 :       gfc_find_vtab (ts);
    9596              :     }
    9597              : 
    9598        17561 :   if (dimension == 0 && codimension == 0)
    9599         5417 :     goto success;
    9600              : 
    9601              :   /* Make sure the last reference node is an array specification.  */
    9602              : 
    9603        12144 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9604        10893 :       || (dimension && ref2->u.ar.dimen == 0))
    9605              :     {
    9606              :       /* F08:C633.  */
    9607         1251 :       if (code->expr3)
    9608              :         {
    9609         1250 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9610              :                                "in ALLOCATE statement at %L", &e->where))
    9611            0 :             goto failure;
    9612         1250 :           if (code->expr3->rank != 0)
    9613         1249 :             *array_alloc_wo_spec = true;
    9614              :           else
    9615              :             {
    9616            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9617              :                          "expression required in ALLOCATE statement at %L",
    9618              :                          &e->where);
    9619            1 :               goto failure;
    9620              :             }
    9621              :         }
    9622              :       else
    9623              :         {
    9624            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9625              :                      "at %L", &e->where);
    9626            1 :           goto failure;
    9627              :         }
    9628              :     }
    9629              : 
    9630              :   /* Make sure that the array section reference makes sense in the
    9631              :      context of an ALLOCATE specification.  */
    9632              : 
    9633        12142 :   ar = &ref2->u.ar;
    9634              : 
    9635        12142 :   if (codimension)
    9636         1265 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9637              :       {
    9638          735 :         switch (ar->dimen_type[i])
    9639              :           {
    9640            2 :           case DIMEN_THIS_IMAGE:
    9641            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9642              :                        "at %L", &e->where);
    9643            2 :             goto failure;
    9644              : 
    9645           98 :           case  DIMEN_RANGE:
    9646              :             /* F2018:R937:
    9647              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9648              :              */
    9649           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9650              :               {
    9651            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9652              :                            "at %L", &e->where);
    9653            8 :                 goto failure;
    9654              :               }
    9655           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9656              :               {
    9657            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9658            2 :                            &ar->start[i]->where);
    9659            2 :                 goto failure;
    9660              :               }
    9661              :             break;
    9662              : 
    9663          105 :           case DIMEN_ELEMENT:
    9664          105 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9665              :               {
    9666           97 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9667           97 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9668              :                   {
    9669            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9670              :                                "of 1 at %L", &ar->start[i]->where);
    9671            1 :                     goto failure;
    9672              :                   }
    9673              :               }
    9674              :             break;
    9675              : 
    9676              :           case DIMEN_STAR:
    9677              :             break;
    9678              : 
    9679            0 :           default:
    9680            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9681              :                        &e->where);
    9682            0 :             goto failure;
    9683              : 
    9684              :           }
    9685              :       }
    9686        29635 :   for (i = 0; i < ar->dimen; i++)
    9687              :     {
    9688        17510 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9689        14771 :         goto check_symbols;
    9690              : 
    9691         2739 :       switch (ar->dimen_type[i])
    9692              :         {
    9693              :         case DIMEN_ELEMENT:
    9694              :           break;
    9695              : 
    9696         2473 :         case DIMEN_RANGE:
    9697         2473 :           if (ar->start[i] != NULL
    9698         2473 :               && ar->end[i] != NULL
    9699         2472 :               && ar->stride[i] == NULL)
    9700              :             break;
    9701              : 
    9702              :           /* Fall through.  */
    9703              : 
    9704            1 :         case DIMEN_UNKNOWN:
    9705            1 :         case DIMEN_VECTOR:
    9706            1 :         case DIMEN_STAR:
    9707            1 :         case DIMEN_THIS_IMAGE:
    9708            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9709              :                      &e->where);
    9710            1 :           goto failure;
    9711              :         }
    9712              : 
    9713         2472 : check_symbols:
    9714        45455 :       for (a = code->ext.alloc.list; a; a = a->next)
    9715              :         {
    9716        27949 :           sym = a->expr->symtree->n.sym;
    9717              : 
    9718              :           /* TODO - check derived type components.  */
    9719        27949 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9720         9492 :             continue;
    9721              : 
    9722        18457 :           if ((ar->start[i] != NULL
    9723        17776 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9724        36230 :               || (ar->end[i] != NULL
    9725         2723 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9726              :             {
    9727            3 :               gfc_error ("%qs must not appear in the array specification at "
    9728              :                          "%L in the same ALLOCATE statement where it is "
    9729              :                          "itself allocated", sym->name, &ar->where);
    9730            3 :               goto failure;
    9731              :             }
    9732              :         }
    9733              :     }
    9734              : 
    9735        12316 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9736              :     {
    9737          911 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9738          720 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9739              :         {
    9740          191 :           if (i == (ar->dimen + ar->codimen - 1))
    9741              :             {
    9742            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9743              :                          "statement at %L", &e->where);
    9744            0 :               goto failure;
    9745              :             }
    9746          191 :           continue;
    9747              :         }
    9748              : 
    9749          529 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9750          529 :           && ar->stride[i] == NULL)
    9751              :         break;
    9752              : 
    9753            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9754              :                  &e->where);
    9755            0 :       goto failure;
    9756              :     }
    9757              : 
    9758        12125 : success:
    9759        17542 :   gfc_used_in_allocate_expr (e, &e->where, ALLOCATED_ALLOCATE_STMT);
    9760              : 
    9761        17542 :   if (code->expr3)
    9762         4080 :     gfc_value_set_at (e->symtree->n.sym, &code->expr3->where, VALUE_VARDEF);
    9763              : 
    9764              :   return true;
    9765              : 
    9766        17639 : failure:
    9767              :   return false;
    9768              : }
    9769              : 
    9770              : 
    9771              : static void
    9772        20780 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9773              : {
    9774        20780 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9775        20780 :   gfc_alloc *a, *p, *q;
    9776              : 
    9777        20780 :   stat = code->expr1;
    9778        20780 :   errmsg = code->expr2;
    9779              : 
    9780              :   /* Check the stat variable.  */
    9781        20780 :   if (stat)
    9782              :     {
    9783          661 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9784          661 :                                      _("STAT variable")))
    9785            8 :           goto done_stat;
    9786              : 
    9787          653 :       if (stat->ts.type != BT_INTEGER
    9788          644 :           || stat->rank > 0)
    9789           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9790              :                    "variable", &stat->where);
    9791              : 
    9792          653 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9793            0 :         goto done_stat;
    9794              : 
    9795              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9796              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9797              :        */
    9798         1354 :       for (p = code->ext.alloc.list; p; p = p->next)
    9799          708 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9800              :           {
    9801            9 :             gfc_ref *ref1, *ref2;
    9802            9 :             bool found = true;
    9803              : 
    9804           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9805            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9806              :               {
    9807            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9808            5 :                   continue;
    9809            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9810              :                   {
    9811              :                     found = false;
    9812              :                     break;
    9813              :                   }
    9814              :               }
    9815              : 
    9816            9 :             if (found)
    9817              :               {
    9818            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9819              :                            "the same %s statement", &stat->where, fcn, fcn);
    9820            7 :                 break;
    9821              :               }
    9822              :           }
    9823              :     }
    9824              : 
    9825        20119 : done_stat:
    9826              : 
    9827              :   /* Check the errmsg variable.  */
    9828        20780 :   if (errmsg)
    9829              :     {
    9830          150 :       if (!stat)
    9831            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9832              :                      &errmsg->where);
    9833              : 
    9834          150 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9835          150 :                                      _("ERRMSG variable")))
    9836            6 :           goto done_errmsg;
    9837              : 
    9838              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9839              :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9840              :          F18:R906  default-char-variable is variable
    9841              :          F18:C906  default-char-variable shall be default character.  */
    9842          144 :       if (errmsg->ts.type != BT_CHARACTER
    9843          142 :           || errmsg->rank > 0
    9844          141 :           || errmsg->ts.kind != gfc_default_character_kind)
    9845            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9846              :                    "variable", &errmsg->where);
    9847              : 
    9848          144 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9849            0 :         goto done_errmsg;
    9850              : 
    9851              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9852              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9853              :        */
    9854          286 :       for (p = code->ext.alloc.list; p; p = p->next)
    9855          147 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9856              :           {
    9857            9 :             gfc_ref *ref1, *ref2;
    9858            9 :             bool found = true;
    9859              : 
    9860           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9861            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9862              :               {
    9863           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9864            4 :                   continue;
    9865            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9866              :                   {
    9867              :                     found = false;
    9868              :                     break;
    9869              :                   }
    9870              :               }
    9871              : 
    9872            9 :             if (found)
    9873              :               {
    9874            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9875              :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9876            5 :                 break;
    9877              :               }
    9878              :           }
    9879              :     }
    9880              : 
    9881        20630 : done_errmsg:
    9882              : 
    9883              :   /* Check that an allocate-object appears only once in the statement.  */
    9884              : 
    9885        46912 :   for (p = code->ext.alloc.list; p; p = p->next)
    9886              :     {
    9887        26132 :       pe = p->expr;
    9888        35480 :       for (q = p->next; q; q = q->next)
    9889              :         {
    9890         9348 :           qe = q->expr;
    9891         9348 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9892              :             {
    9893              :               /* This is a potential collision.  */
    9894         2094 :               gfc_ref *pr = pe->ref;
    9895         2094 :               gfc_ref *qr = qe->ref;
    9896              : 
    9897              :               /* Follow the references  until
    9898              :                  a) They start to differ, in which case there is no error;
    9899              :                  you can deallocate a%b and a%c in a single statement
    9900              :                  b) Both of them stop, which is an error
    9901              :                  c) One of them stops, which is also an error.  */
    9902         4518 :               while (1)
    9903              :                 {
    9904         3306 :                   if (pr == NULL && qr == NULL)
    9905              :                     {
    9906            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9907              :                                  &pe->where, &qe->where);
    9908            7 :                       break;
    9909              :                     }
    9910         3299 :                   else if (pr != NULL && qr == NULL)
    9911              :                     {
    9912            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9913              :                                  " object at %L", &pe->where, &qe->where);
    9914            2 :                       break;
    9915              :                     }
    9916         3297 :                   else if (pr == NULL && qr != NULL)
    9917              :                     {
    9918            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9919              :                                  " object at %L", &qe->where, &pe->where);
    9920            2 :                       break;
    9921              :                     }
    9922              :                   /* Here, pr != NULL && qr != NULL  */
    9923         3295 :                   gcc_assert(pr->type == qr->type);
    9924         3295 :                   if (pr->type == REF_ARRAY)
    9925              :                     {
    9926              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9927              :                          which are legal.  */
    9928         1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9929              : 
    9930         1065 :                       if (pr->next && qr->next)
    9931              :                         {
    9932              :                           int i;
    9933              :                           gfc_array_ref *par = &(pr->u.ar);
    9934              :                           gfc_array_ref *qar = &(qr->u.ar);
    9935              : 
    9936         1840 :                           for (i=0; i<par->dimen; i++)
    9937              :                             {
    9938          954 :                               if ((par->start[i] != NULL
    9939            0 :                                    || qar->start[i] != NULL)
    9940         1908 :                                   && gfc_dep_compare_expr (par->start[i],
    9941          954 :                                                            qar->start[i]) != 0)
    9942          168 :                                 goto break_label;
    9943              :                             }
    9944              :                         }
    9945              :                     }
    9946              :                   else
    9947              :                     {
    9948         2230 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9949              :                         break;
    9950              :                     }
    9951              : 
    9952         1212 :                   pr = pr->next;
    9953         1212 :                   qr = qr->next;
    9954         1212 :                 }
    9955         9348 :             break_label:
    9956              :               ;
    9957              :             }
    9958              :         }
    9959              :     }
    9960              : 
    9961        20780 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9962              :     {
    9963        14581 :       bool arr_alloc_wo_spec = false;
    9964              : 
    9965              :       /* Resolve and mark as used the length of the type spec.  */
    9966        14581 :       if (code->ext.alloc.ts.type == BT_CHARACTER)
    9967              :         {
    9968          473 :           gfc_expr *length = code->ext.alloc.ts.u.cl->length;
    9969          473 :           gfc_resolve_expr (length);
    9970          473 :           gfc_value_used_expr (length, VALUE_USED);
    9971              :         }
    9972              : 
    9973              :       /* Resolving the expr3 in the loop over all objects to allocate would
    9974              :          execute loop invariant code for each loop item.  Therefore do it just
    9975              :          once here.  */
    9976        14581 :       if (code->expr3 && code->expr3->mold
    9977          363 :           && code->expr3->ts.type == BT_DERIVED
    9978           30 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
    9979              :         {
    9980              :           /* Default initialization via MOLD (non-polymorphic).  */
    9981           28 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9982           28 :           if (rhs != NULL)
    9983              :             {
    9984            9 :               gfc_resolve_expr (rhs);
    9985            9 :               gfc_free_expr (code->expr3);
    9986            9 :               code->expr3 = rhs;
    9987              :             }
    9988              :         }
    9989        32220 :       for (a = code->ext.alloc.list; a; a = a->next)
    9990        17639 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9991              : 
    9992        14581 :       if (arr_alloc_wo_spec && code->expr3)
    9993              :         {
    9994              :           /* Mark the allocate to have to take the array specification
    9995              :              from the expr3.  */
    9996         1243 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9997              :         }
    9998              :     }
    9999              :   else
   10000              :     {
   10001        14692 :       for (a = code->ext.alloc.list; a; a = a->next)
   10002         8493 :         resolve_deallocate_expr (a->expr);
   10003              :     }
   10004        20780 : }
   10005              : 
   10006              : 
   10007              : /************ SELECT CASE resolution subroutines ************/
   10008              : 
   10009              : /* Callback function for our mergesort variant.  Determines interval
   10010              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
   10011              :    op1 > op2.  Assumes we're not dealing with the default case.
   10012              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
   10013              :    There are nine situations to check.  */
   10014              : 
   10015              : static int
   10016         1582 : compare_cases (const gfc_case *op1, const gfc_case *op2)
   10017              : {
   10018         1582 :   int retval;
   10019              : 
   10020         1582 :   if (op1->low == NULL) /* op1 = (:L)  */
   10021              :     {
   10022              :       /* op2 = (:N), so overlap.  */
   10023           52 :       retval = 0;
   10024              :       /* op2 = (M:) or (M:N),  L < M  */
   10025           52 :       if (op2->low != NULL
   10026           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10027              :         retval = -1;
   10028              :     }
   10029         1530 :   else if (op1->high == NULL) /* op1 = (K:)  */
   10030              :     {
   10031              :       /* op2 = (M:), so overlap.  */
   10032           10 :       retval = 0;
   10033              :       /* op2 = (:N) or (M:N), K > N  */
   10034           10 :       if (op2->high != NULL
   10035           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10036              :         retval = 1;
   10037              :     }
   10038              :   else /* op1 = (K:L)  */
   10039              :     {
   10040         1520 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
   10041           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10042           18 :                  ? 1 : 0;
   10043         1502 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
   10044           10 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10045           10 :                  ? -1 : 0;
   10046              :       else                      /* op2 = (M:N)  */
   10047              :         {
   10048         1492 :           retval =  0;
   10049              :           /* L < M  */
   10050         1492 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10051              :             retval =  -1;
   10052              :           /* K > N  */
   10053          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10054          438 :             retval =  1;
   10055              :         }
   10056              :     }
   10057              : 
   10058         1582 :   return retval;
   10059              : }
   10060              : 
   10061              : 
   10062              : /* Merge-sort a double linked case list, detecting overlap in the
   10063              :    process.  LIST is the head of the double linked case list before it
   10064              :    is sorted.  Returns the head of the sorted list if we don't see any
   10065              :    overlap, or NULL otherwise.  */
   10066              : 
   10067              : static gfc_case *
   10068          647 : check_case_overlap (gfc_case *list)
   10069              : {
   10070          647 :   gfc_case *p, *q, *e, *tail;
   10071          647 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10072              : 
   10073              :   /* If the passed list was empty, return immediately.  */
   10074          647 :   if (!list)
   10075              :     return NULL;
   10076              : 
   10077              :   overlap_seen = 0;
   10078              :   insize = 1;
   10079              : 
   10080              :   /* Loop unconditionally.  The only exit from this loop is a return
   10081              :      statement, when we've finished sorting the case list.  */
   10082         1353 :   for (;;)
   10083              :     {
   10084         1000 :       p = list;
   10085         1000 :       list = NULL;
   10086         1000 :       tail = NULL;
   10087              : 
   10088              :       /* Count the number of merges we do in this pass.  */
   10089         1000 :       nmerges = 0;
   10090              : 
   10091              :       /* Loop while there exists a merge to be done.  */
   10092         2528 :       while (p)
   10093              :         {
   10094         1528 :           int i;
   10095              : 
   10096              :           /* Count this merge.  */
   10097         1528 :           nmerges++;
   10098              : 
   10099              :           /* Cut the list in two pieces by stepping INSIZE places
   10100              :              forward in the list, starting from P.  */
   10101         1528 :           psize = 0;
   10102         1528 :           q = p;
   10103         3215 :           for (i = 0; i < insize; i++)
   10104              :             {
   10105         2247 :               psize++;
   10106         2247 :               q = q->right;
   10107         2247 :               if (!q)
   10108              :                 break;
   10109              :             }
   10110         1528 :           qsize = insize;
   10111              : 
   10112              :           /* Now we have two lists.  Merge them!  */
   10113         5024 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10114              :             {
   10115              :               /* See from which the next case to merge comes from.  */
   10116          811 :               if (psize == 0)
   10117              :                 {
   10118              :                   /* P is empty so the next case must come from Q.  */
   10119          811 :                   e = q;
   10120          811 :                   q = q->right;
   10121          811 :                   qsize--;
   10122              :                 }
   10123         2685 :               else if (qsize == 0 || q == NULL)
   10124              :                 {
   10125              :                   /* Q is empty.  */
   10126         1103 :                   e = p;
   10127         1103 :                   p = p->right;
   10128         1103 :                   psize--;
   10129              :                 }
   10130              :               else
   10131              :                 {
   10132         1582 :                   cmp = compare_cases (p, q);
   10133         1582 :                   if (cmp < 0)
   10134              :                     {
   10135              :                       /* The whole case range for P is less than the
   10136              :                          one for Q.  */
   10137         1140 :                       e = p;
   10138         1140 :                       p = p->right;
   10139         1140 :                       psize--;
   10140              :                     }
   10141          442 :                   else if (cmp > 0)
   10142              :                     {
   10143              :                       /* The whole case range for Q is greater than
   10144              :                          the case range for P.  */
   10145          438 :                       e = q;
   10146          438 :                       q = q->right;
   10147          438 :                       qsize--;
   10148              :                     }
   10149              :                   else
   10150              :                     {
   10151              :                       /* The cases overlap, or they are the same
   10152              :                          element in the list.  Either way, we must
   10153              :                          issue an error and get the next case from P.  */
   10154              :                       /* FIXME: Sort P and Q by line number.  */
   10155            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10156              :                                  "label at %L", &p->where, &q->where);
   10157            4 :                       overlap_seen = 1;
   10158            4 :                       e = p;
   10159            4 :                       p = p->right;
   10160            4 :                       psize--;
   10161              :                     }
   10162              :                 }
   10163              : 
   10164              :                 /* Add the next element to the merged list.  */
   10165         3496 :               if (tail)
   10166         2496 :                 tail->right = e;
   10167              :               else
   10168              :                 list = e;
   10169         3496 :               e->left = tail;
   10170         3496 :               tail = e;
   10171              :             }
   10172              : 
   10173              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10174              :              they're the same.  */
   10175              :           p = q;
   10176              :         }
   10177         1000 :       tail->right = NULL;
   10178              : 
   10179              :       /* If we have done only one merge or none at all, we've
   10180              :          finished sorting the cases.  */
   10181         1000 :       if (nmerges <= 1)
   10182              :         {
   10183          647 :           if (!overlap_seen)
   10184              :             return list;
   10185              :           else
   10186            4 :             return NULL;
   10187              :         }
   10188              : 
   10189              :       /* Otherwise repeat, merging lists twice the size.  */
   10190          353 :       insize *= 2;
   10191          353 :     }
   10192              : }
   10193              : 
   10194              : 
   10195              : /* Check to see if an expression is suitable for use in a CASE statement.
   10196              :    Makes sure that all case expressions are scalar constants of the same
   10197              :    type.  Return false if anything is wrong.  */
   10198              : 
   10199              : static bool
   10200         3315 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10201              : {
   10202         3315 :   if (e == NULL) return true;
   10203              : 
   10204         3222 :   if (e->ts.type != case_expr->ts.type)
   10205              :     {
   10206            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10207              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10208            4 :       return false;
   10209              :     }
   10210              : 
   10211              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10212              :      the same type as case-expr.  For character type, length differences
   10213              :      are allowed, but the kind type parameters shall be the same.  */
   10214              : 
   10215         3218 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10216              :     {
   10217            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10218              :                  &e->where, case_expr->ts.kind);
   10219            4 :       return false;
   10220              :     }
   10221              : 
   10222              :   /* Convert the case value kind to that of case expression kind,
   10223              :      if needed */
   10224              : 
   10225         3214 :   if (e->ts.kind != case_expr->ts.kind)
   10226           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10227              : 
   10228         3214 :   if (e->rank != 0)
   10229              :     {
   10230            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10231              :                  &e->where);
   10232            0 :       return false;
   10233              :     }
   10234              : 
   10235              :   return true;
   10236              : }
   10237              : 
   10238              : 
   10239              : /* Given a completely parsed select statement, we:
   10240              : 
   10241              :      - Validate all expressions and code within the SELECT.
   10242              :      - Make sure that the selection expression is not of the wrong type.
   10243              :      - Make sure that no case ranges overlap.
   10244              :      - Eliminate unreachable cases and unreachable code resulting from
   10245              :        removing case labels.
   10246              : 
   10247              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10248              :    they are a hassle for code generation, and to prevent that, we just
   10249              :    cut them out here.  This is not necessary for overlapping cases
   10250              :    because they are illegal and we never even try to generate code.
   10251              : 
   10252              :    We have the additional caveat that a SELECT construct could have
   10253              :    been a computed GOTO in the source code. Fortunately we can fairly
   10254              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10255              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10256              :    we have to do is make sure that the case_expr is a scalar integer
   10257              :    expression.  */
   10258              : 
   10259              : static void
   10260          688 : resolve_select (gfc_code *code, bool select_type)
   10261              : {
   10262          688 :   gfc_code *body;
   10263          688 :   gfc_expr *case_expr;
   10264          688 :   gfc_case *cp, *default_case, *tail, *head;
   10265          688 :   int seen_unreachable;
   10266          688 :   int seen_logical;
   10267          688 :   int ncases;
   10268          688 :   bt type;
   10269          688 :   bool t;
   10270              : 
   10271          688 :   if (code->expr1 == NULL)
   10272              :     {
   10273              :       /* This was actually a computed GOTO statement.  */
   10274            5 :       case_expr = code->expr2;
   10275            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10276            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10277              :                    "at %L must be a scalar integer expression",
   10278              :                    &case_expr->where);
   10279              : 
   10280              :       /* Further checking is not necessary because this SELECT was built
   10281              :          by the compiler, so it should always be OK.  Just move the
   10282              :          case_expr from expr2 to expr so that we can handle computed
   10283              :          GOTOs as normal SELECTs from here on.  */
   10284            5 :       code->expr1 = code->expr2;
   10285            5 :       code->expr2 = NULL;
   10286            5 :       gfc_value_used_expr (code->expr1, VALUE_USED);
   10287            5 :       return;
   10288              :     }
   10289              : 
   10290          683 :   case_expr = code->expr1;
   10291          683 :   type = case_expr->ts.type;
   10292              : 
   10293              :   /* F08:C830.  */
   10294          683 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10295            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10296              : 
   10297              :     {
   10298            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10299              :                  &case_expr->where, gfc_typename (case_expr));
   10300              : 
   10301              :       /* Punt. Going on here just produce more garbage error messages.  */
   10302            0 :       return;
   10303              :     }
   10304              : 
   10305              :   /* F08:R842.  */
   10306          683 :   if (!select_type && case_expr->rank != 0)
   10307              :     {
   10308            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10309              :                  "expression", &case_expr->where);
   10310              : 
   10311              :       /* Punt.  */
   10312            1 :       return;
   10313              :     }
   10314              : 
   10315              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10316              :      the case-expr. Later, all expressions will be promoted to the
   10317              :      largest kind of all case-labels.  */
   10318              : 
   10319          682 :   if (type == BT_INTEGER)
   10320         1933 :     for (body = code->block; body; body = body->block)
   10321         2862 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10322              :         {
   10323         1467 :           if (cp->low
   10324         1467 :               && gfc_check_integer_range (cp->low->value.integer,
   10325              :                                           case_expr->ts.kind) != ARITH_OK)
   10326            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10327            6 :                          "not in the range of %s", &cp->low->where,
   10328              :                          gfc_typename (case_expr));
   10329              : 
   10330         1467 :           if (cp->high
   10331         1182 :               && cp->low != cp->high
   10332         1575 :               && gfc_check_integer_range (cp->high->value.integer,
   10333              :                                           case_expr->ts.kind) != ARITH_OK)
   10334            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10335            0 :                          "not in the range of %s", &cp->high->where,
   10336              :                          gfc_typename (case_expr));
   10337              :         }
   10338              : 
   10339              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10340              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10341              :      of case values, and if we find a mismatch, promote case_expr to
   10342              :      the appropriate kind.  */
   10343              : 
   10344          682 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10345              :     {
   10346         2119 :       for (body = code->block; body; body = body->block)
   10347              :         {
   10348              :           /* Walk the case label list.  */
   10349         3123 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10350              :             {
   10351              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10352         1602 :               if (cp->low == NULL && cp->high == NULL)
   10353          293 :                 continue;
   10354              : 
   10355              :               /* Unreachable case ranges are discarded, so ignore.  */
   10356         1264 :               if (cp->low != NULL && cp->high != NULL
   10357         1216 :                   && cp->low != cp->high
   10358         1374 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10359           33 :                 continue;
   10360              : 
   10361         1276 :               if (cp->low != NULL
   10362         1276 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10363           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10364              : 
   10365         1276 :               if (cp->high != NULL
   10366         1276 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10367            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10368              :             }
   10369              :          }
   10370              :     }
   10371              : 
   10372              :   /* Assume there is no DEFAULT case.  */
   10373          682 :   default_case = NULL;
   10374          682 :   head = tail = NULL;
   10375          682 :   ncases = 0;
   10376          682 :   seen_logical = 0;
   10377              : 
   10378         2508 :   for (body = code->block; body; body = body->block)
   10379              :     {
   10380              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10381         1826 :       t = true;
   10382         1826 :       seen_unreachable = 0;
   10383              : 
   10384              :       /* Walk the case label list, making sure that all case labels
   10385              :          are legal.  */
   10386         3839 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10387              :         {
   10388              :           /* Count the number of cases in the whole construct.  */
   10389         2024 :           ncases++;
   10390              : 
   10391              :           /* Intercept the DEFAULT case.  */
   10392         2024 :           if (cp->low == NULL && cp->high == NULL)
   10393              :             {
   10394          363 :               if (default_case != NULL)
   10395              :                 {
   10396            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10397              :                              "by a second DEFAULT CASE at %L",
   10398              :                              &default_case->where, &cp->where);
   10399            0 :                   t = false;
   10400            0 :                   break;
   10401              :                 }
   10402              :               else
   10403              :                 {
   10404          363 :                   default_case = cp;
   10405          363 :                   continue;
   10406              :                 }
   10407              :             }
   10408              : 
   10409              :           /* Deal with single value cases and case ranges.  Errors are
   10410              :              issued from the validation function.  */
   10411         1661 :           if (!validate_case_label_expr (cp->low, case_expr)
   10412         1661 :               || !validate_case_label_expr (cp->high, case_expr))
   10413              :             {
   10414              :               t = false;
   10415              :               break;
   10416              :             }
   10417              : 
   10418         1653 :           if (type == BT_LOGICAL
   10419           78 :               && ((cp->low == NULL || cp->high == NULL)
   10420           76 :                   || cp->low != cp->high))
   10421              :             {
   10422            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10423              :                          "allowed",
   10424            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10425            2 :               t = false;
   10426            2 :               break;
   10427              :             }
   10428              : 
   10429           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10430              :             {
   10431           76 :               int value;
   10432           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10433           76 :               if (value & seen_logical)
   10434              :                 {
   10435            1 :                   gfc_error ("Constant logical value in CASE statement "
   10436              :                              "is repeated at %L",
   10437              :                              &cp->low->where);
   10438            1 :                   t = false;
   10439            1 :                   break;
   10440              :                 }
   10441           75 :               seen_logical |= value;
   10442              :             }
   10443              : 
   10444         1606 :           if (cp->low != NULL && cp->high != NULL
   10445         1559 :               && cp->low != cp->high
   10446         1762 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10447              :             {
   10448           35 :               if (warn_surprising)
   10449            1 :                 gfc_warning (OPT_Wsurprising,
   10450              :                              "Range specification at %L can never be matched",
   10451              :                              &cp->where);
   10452              : 
   10453           35 :               cp->unreachable = 1;
   10454           35 :               seen_unreachable = 1;
   10455              :             }
   10456              :           else
   10457              :             {
   10458              :               /* If the case range can be matched, it can also overlap with
   10459              :                  other cases.  To make sure it does not, we put it in a
   10460              :                  double linked list here.  We sort that with a merge sort
   10461              :                  later on to detect any overlapping cases.  */
   10462         1615 :               if (!head)
   10463              :                 {
   10464          647 :                   head = tail = cp;
   10465          647 :                   head->right = head->left = NULL;
   10466              :                 }
   10467              :               else
   10468              :                 {
   10469          968 :                   tail->right = cp;
   10470          968 :                   tail->right->left = tail;
   10471          968 :                   tail = tail->right;
   10472          968 :                   tail->right = NULL;
   10473              :                 }
   10474              :             }
   10475              :         }
   10476              : 
   10477              :       /* It there was a failure in the previous case label, give up
   10478              :          for this case label list.  Continue with the next block.  */
   10479         1826 :       if (!t)
   10480           11 :         continue;
   10481              : 
   10482              :       /* See if any case labels that are unreachable have been seen.
   10483              :          If so, we eliminate them.  This is a bit of a kludge because
   10484              :          the case lists for a single case statement (label) is a
   10485              :          single forward linked lists.  */
   10486         1815 :       if (seen_unreachable)
   10487              :       {
   10488              :         /* Advance until the first case in the list is reachable.  */
   10489           69 :         while (body->ext.block.case_list != NULL
   10490           69 :                && body->ext.block.case_list->unreachable)
   10491              :           {
   10492           34 :             gfc_case *n = body->ext.block.case_list;
   10493           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10494           34 :             n->next = NULL;
   10495           34 :             gfc_free_case_list (n);
   10496              :           }
   10497              : 
   10498              :         /* Strip all other unreachable cases.  */
   10499           35 :         if (body->ext.block.case_list)
   10500              :           {
   10501            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10502              :               {
   10503            1 :                 if (cp->next->unreachable)
   10504              :                   {
   10505            1 :                     gfc_case *n = cp->next;
   10506            1 :                     cp->next = cp->next->next;
   10507            1 :                     n->next = NULL;
   10508            1 :                     gfc_free_case_list (n);
   10509              :                   }
   10510              :               }
   10511              :           }
   10512              :       }
   10513              :     }
   10514              : 
   10515              :   /* See if there were overlapping cases.  If the check returns NULL,
   10516              :      there was overlap.  In that case we don't do anything.  If head
   10517              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10518              :      then used during code generation for SELECT CASE constructs with
   10519              :      a case expression of a CHARACTER type.  */
   10520          682 :   if (head)
   10521              :     {
   10522          647 :       head = check_case_overlap (head);
   10523              : 
   10524              :       /* Prepend the default_case if it is there.  */
   10525          647 :       if (head != NULL && default_case)
   10526              :         {
   10527          346 :           default_case->left = NULL;
   10528          346 :           default_case->right = head;
   10529          346 :           head->left = default_case;
   10530              :         }
   10531              :     }
   10532              : 
   10533              :   /* Eliminate dead blocks that may be the result if we've seen
   10534              :      unreachable case labels for a block.  */
   10535         2474 :   for (body = code; body && body->block; body = body->block)
   10536              :     {
   10537         1792 :       if (body->block->ext.block.case_list == NULL)
   10538              :         {
   10539              :           /* Cut the unreachable block from the code chain.  */
   10540           34 :           gfc_code *c = body->block;
   10541           34 :           body->block = c->block;
   10542              : 
   10543              :           /* Kill the dead block, but not the blocks below it.  */
   10544           34 :           c->block = NULL;
   10545           34 :           gfc_free_statements (c);
   10546              :         }
   10547              :     }
   10548              : 
   10549              :   /* More than two cases is legal but insane for logical selects.
   10550              :      Issue a warning for it.  */
   10551          682 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10552            0 :     gfc_warning (OPT_Wsurprising,
   10553              :                  "Logical SELECT CASE block at %L has more that two cases",
   10554              :                  &code->loc);
   10555              : 
   10556              :   /* Finally, mark the expression as used.  */
   10557          682 :   gfc_value_used_expr (case_expr, VALUE_USED);
   10558              : }
   10559              : 
   10560              : 
   10561              : /* Check if a derived type is extensible.  */
   10562              : 
   10563              : bool
   10564        24713 : gfc_type_is_extensible (gfc_symbol *sym)
   10565              : {
   10566        24713 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10567        24697 :            || (sym->attr.is_class
   10568         2226 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10569              : }
   10570              : 
   10571              : 
   10572              : static void
   10573              : resolve_types (gfc_namespace *ns);
   10574              : 
   10575              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10576              :    correct as well as possibly the array-spec.  */
   10577              : 
   10578              : static void
   10579        13235 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10580              : {
   10581        13235 :   gfc_expr* target;
   10582              : 
   10583        13235 :   gcc_assert (sym->assoc);
   10584        13235 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10585              : 
   10586        13235 :   if (sym->assoc->target
   10587         7933 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10588          598 :       && sym->assoc->target->symtree
   10589          598 :       && sym->assoc->target->symtree->n.sym
   10590          598 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10591              :     {
   10592           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10593           33 :         sym->ts = sym->assoc->target->ts;
   10594              :       else
   10595              :         {
   10596            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10597            0 :                      sym->assoc->target->symtree->n.sym->name,
   10598            0 :                      &sym->assoc->target->where);
   10599            0 :           return;
   10600              :         }
   10601              :     }
   10602              : 
   10603              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10604              :      case, return.  Resolution will be called later manually again when
   10605              :      this is done.  */
   10606        13235 :   target = sym->assoc->target;
   10607        13235 :   if (!target)
   10608              :     return;
   10609         7933 :   gcc_assert (!sym->assoc->dangling);
   10610              : 
   10611         7933 :   if (resolve_target && !gfc_resolve_expr (target))
   10612              :     return;
   10613              : 
   10614         7928 :   if (sym->assoc->ar)
   10615              :     {
   10616              :       int dim;
   10617              :       gfc_array_ref *ar = sym->assoc->ar;
   10618           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10619              :         {
   10620           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10621           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10622           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10623           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10624            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10625              :                        "remapping of associate name %s at %L",
   10626              :                        sym->name, &sym->declared_at);
   10627              :         }
   10628              :     }
   10629              : 
   10630              :   /* For variable targets, we get some attributes from the target.  */
   10631         7928 :   if (target->expr_type == EXPR_VARIABLE
   10632         1104 :       || (target->expr_type == EXPR_OP
   10633          287 :           && target->value.op.op == INTRINSIC_PARENTHESES
   10634           68 :           && target->value.op.op1->expr_type == EXPR_VARIABLE))
   10635              :     {
   10636         6879 :       gfc_symbol *tsym, *dsym;
   10637              : 
   10638         6879 :       tsym = target->expr_type == EXPR_VARIABLE ? target->symtree->n.sym :
   10639           55 :                                   target->value.op.op1->symtree->n.sym;
   10640              : 
   10641         6879 :       if (gfc_expr_attr (target).proc_pointer)
   10642              :         {
   10643            0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10644              :                      tsym->name, &target->where);
   10645            0 :           return;
   10646              :         }
   10647              : 
   10648           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10649            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10650         6880 :           && dsym->attr.flavor == FL_DERIVED)
   10651              :         {
   10652            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10653              :                      tsym->name, &target->where);
   10654            1 :           return;
   10655              :         }
   10656              : 
   10657         6878 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10658              :         {
   10659           73 :           bool is_error = true;
   10660           73 :           if (tsym->attr.function && tsym->result == tsym)
   10661          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10662          137 :               if (tsym == ns->proc_name)
   10663              :                 {
   10664              :                   is_error = false;
   10665              :                   break;
   10666              :                 }
   10667           64 :           if (is_error)
   10668              :             {
   10669           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10670              :                          tsym->name, &target->where);
   10671           13 :               return;
   10672              :             }
   10673              :         }
   10674              : 
   10675         6865 :       if (target->expr_type == EXPR_VARIABLE)
   10676              :         {
   10677         6812 :           sym->attr.asynchronous = tsym->attr.asynchronous;
   10678         6812 :           sym->attr.volatile_ = tsym->attr.volatile_;
   10679              : 
   10680        13624 :           sym->attr.target = tsym->attr.target
   10681         6812 :                              || gfc_expr_attr (target).pointer;
   10682         6812 :           if (is_subref_array (target))
   10683          421 :             sym->attr.subref_array_pointer = 1;
   10684              :         }
   10685              :     }
   10686         1049 :   else if (target->ts.type == BT_PROCEDURE)
   10687              :     {
   10688            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10689              :                  &target->where);
   10690            0 :       return;
   10691              :     }
   10692              : 
   10693         7914 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10694              :     {
   10695              :       /* By now, the type of the target has been fixed up.  */
   10696          314 :       symbol_attribute attr;
   10697              : 
   10698          314 :       if (sym->ts.type == BT_DERIVED
   10699          181 :           && target->ts.type == BT_CLASS
   10700           31 :           && !UNLIMITED_POLY (target))
   10701              :         {
   10702              :           /* Inferred to be derived type but the target has type class.  */
   10703           31 :           sym->ts = CLASS_DATA (target)->ts;
   10704           31 :           if (!sym->as)
   10705           31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10706           31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10707           31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10708           31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10709              :                             target->corank);
   10710           31 :           sym->as = NULL;
   10711              :         }
   10712          283 :       else if (target->ts.type == BT_DERIVED
   10713          150 :                && target->symtree && target->symtree->n.sym
   10714          126 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10715            0 :                && IS_INFERRED_TYPE (target)
   10716            0 :                && target->ref && target->ref->next
   10717            0 :                && target->ref->next->type == REF_ARRAY
   10718            0 :                && !target->ref->next->next)
   10719              :         {
   10720              :           /* A inferred type selector whose symbol has been determined to be
   10721              :              a class array but which only has an array reference. Change the
   10722              :              associate name and the selector to class type.  */
   10723            0 :           sym->ts = target->ts;
   10724            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10725            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10726            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10727              :                             target->corank);
   10728            0 :           sym->as = NULL;
   10729            0 :           target->ts = sym->ts;
   10730              :         }
   10731          283 :       else if ((target->ts.type == BT_DERIVED)
   10732          133 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10733           61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10734              :         /* Confirmed to be either a derived type or misidentified to be a
   10735              :            scalar class object, when the selector is a class array.  */
   10736          156 :         sym->ts = target->ts;
   10737          127 :       else if (sym->assoc->inferred_type
   10738          120 :                && (sym->ts.type == BT_COMPLEX
   10739           78 :                    || sym->ts.type == BT_CHARACTER)
   10740           66 :                && target->ts.type == sym->ts.type
   10741           66 :                && sym->ts.kind != target->ts.kind)
   10742              :         /* The inferred type was set from a %re, %im or %len inquiry on
   10743              :            the associate name with the default kind, before the target's
   10744              :            actual type was known.  Now that the target has been resolved,
   10745              :            update the kind to match.  */
   10746            6 :         sym->ts = target->ts;
   10747              :     }
   10748              : 
   10749              : 
   10750         7914 :   if (target->expr_type == EXPR_NULL)
   10751              :     {
   10752            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10753            1 :       return;
   10754              :     }
   10755         7913 :   else if (target->ts.type == BT_UNKNOWN)
   10756              :     {
   10757            2 :       gfc_error ("Selector at %L has no type", &target->where);
   10758            2 :       return;
   10759              :     }
   10760              : 
   10761              :   /* Get type if this was not already set.  Note that it can be
   10762              :      some other type than the target in case this is a SELECT TYPE
   10763              :      selector!  So we must not update when the type is already there.  */
   10764         7911 :   if (sym->ts.type == BT_UNKNOWN)
   10765          259 :     sym->ts = target->ts;
   10766              : 
   10767         7911 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10768              : 
   10769              :   /* See if this is a valid association-to-variable.  */
   10770        15822 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10771         6812 :                            && !gfc_has_vector_subscript (target))
   10772         7938 :                           || gfc_is_ptr_fcn (target));
   10773              : 
   10774              :   /* Finally resolve if this is an array or not.  */
   10775         7911 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10776          237 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10777              :     {
   10778          142 :       gfc_expression_rank (target);
   10779          142 :       if (target->ts.type == BT_DERIVED
   10780           95 :           && !sym->as
   10781           95 :           && target->symtree->n.sym->as)
   10782              :         {
   10783            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10784            0 :           sym->attr.dimension = 1;
   10785              :         }
   10786          142 :       else if (target->ts.type == BT_CLASS
   10787           47 :                && CLASS_DATA (target)->as)
   10788              :         {
   10789            0 :           target->rank = CLASS_DATA (target)->as->rank;
   10790            0 :           target->corank = CLASS_DATA (target)->as->corank;
   10791            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10792              :             {
   10793            0 :               sym->ts = target->ts;
   10794            0 :               sym->attr.dimension = 0;
   10795              :             }
   10796              :         }
   10797              :     }
   10798              : 
   10799              : 
   10800         7911 :   if (sym->attr.dimension && target->rank == 0)
   10801              :     {
   10802              :       /* primary.cc makes the assumption that a reference to an associate
   10803              :          name followed by a left parenthesis is an array reference.  */
   10804           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10805              :         {
   10806           12 :           gfc_expression_rank (sym->assoc->target);
   10807           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10808           12 :           if (!sym->attr.dimension && sym->as)
   10809            0 :             sym->as = NULL;
   10810              :         }
   10811              : 
   10812           17 :       if (sym->attr.dimension && target->rank == 0)
   10813              :         {
   10814            5 :           if (sym->ts.type != BT_CHARACTER)
   10815            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10816              :                        sym->name, &sym->declared_at);
   10817            5 :           sym->attr.dimension = 0;
   10818            5 :           return;
   10819              :         }
   10820              :     }
   10821              : 
   10822              :   /* We cannot deal with class selectors that need temporaries.  */
   10823         7906 :   if (target->ts.type == BT_CLASS
   10824         7906 :         && gfc_ref_needs_temporary_p (target->ref))
   10825              :     {
   10826            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10827              :                  "yet implemented", &target->where);
   10828            1 :       return;
   10829              :     }
   10830              : 
   10831         7905 :   if (target->ts.type == BT_CLASS)
   10832         2890 :     gfc_fix_class_refs (target);
   10833              : 
   10834         7905 :   if ((target->rank > 0 || target->corank > 0)
   10835         2834 :       && !sym->attr.select_rank_temporary)
   10836              :     {
   10837         2834 :       gfc_array_spec *as;
   10838              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10839              :          it is corrected now.  */
   10840         2834 :       if (sym->ts.type != BT_CLASS
   10841         2231 :           && (!sym->as || sym->as->corank != target->corank))
   10842              :         {
   10843          163 :           if (!sym->as)
   10844          156 :             sym->as = gfc_get_array_spec ();
   10845          163 :           as = sym->as;
   10846          163 :           as->rank = target->rank;
   10847          163 :           as->type = AS_DEFERRED;
   10848          163 :           as->corank = target->corank;
   10849          163 :           sym->attr.dimension = 1;
   10850          163 :           if (as->corank != 0)
   10851            7 :             sym->attr.codimension = 1;
   10852              :         }
   10853         2671 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10854          602 :                && (!CLASS_DATA (sym)->as
   10855          602 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10856              :         {
   10857            0 :           if (!CLASS_DATA (sym)->as)
   10858            0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10859            0 :           as = CLASS_DATA (sym)->as;
   10860            0 :           as->rank = target->rank;
   10861            0 :           as->type = AS_DEFERRED;
   10862            0 :           as->corank = target->corank;
   10863            0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10864            0 :           if (as->corank != 0)
   10865            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10866              :         }
   10867              :     }
   10868         5071 :   else if (!sym->attr.select_rank_temporary)
   10869              :     {
   10870              :       /* target's rank is 0, but the type of the sym is still array valued,
   10871              :          which has to be corrected.  */
   10872         3646 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10873          736 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10874              :         {
   10875           24 :           gfc_array_spec *as;
   10876           24 :           symbol_attribute attr;
   10877              :           /* The associated variable's type is still the array type
   10878              :              correct this now.  */
   10879           24 :           gfc_typespec *ts = &target->ts;
   10880           24 :           gfc_ref *ref;
   10881              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10882              :            */
   10883           24 :           bool internal_ref = true;
   10884              : 
   10885           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10886              :             {
   10887           48 :               switch (ref->type)
   10888              :                 {
   10889           24 :                 case REF_COMPONENT:
   10890           24 :                   ts = &ref->u.c.component->ts;
   10891           24 :                   internal_ref
   10892           24 :                     = target->ref == ref && ref->next
   10893           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10894              :                   break;
   10895           24 :                 case REF_ARRAY:
   10896           24 :                   if (ts->type == BT_CLASS)
   10897            0 :                     ts = &ts->u.derived->components->ts;
   10898           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10899            0 :                     for (int i = ref->u.ar.dimen;
   10900              :                          internal_ref
   10901            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10902              :                          ++i)
   10903            0 :                       internal_ref
   10904            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10905              :                   break;
   10906              :                 default:
   10907              :                   break;
   10908              :                 }
   10909              :             }
   10910              :           /* Only rewrite the type of this symbol, when the refs are not the
   10911              :              internal ones for class and co-array this-image.  */
   10912           24 :           if (!internal_ref)
   10913              :             {
   10914              :               /* Create a scalar instance of the current class type.  Because
   10915              :                  the rank of a class array goes into its name, the type has to
   10916              :                  be rebuilt.  The alternative of (re-)setting just the
   10917              :                  attributes and as in the current type, destroys the type also
   10918              :                  in other places.  */
   10919            0 :               as = NULL;
   10920            0 :               sym->ts = *ts;
   10921            0 :               sym->ts.type = BT_CLASS;
   10922            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10923            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10924            0 :               sym->as = NULL;
   10925              :             }
   10926              :         }
   10927              :     }
   10928              : 
   10929              :   /* Mark this as an associate variable.  */
   10930         7905 :   sym->attr.associate_var = 1;
   10931              : 
   10932              :   /* Fix up the type-spec for CHARACTER types.  */
   10933         7905 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10934              :     {
   10935          527 :       gfc_ref *ref;
   10936          811 :       for (ref = target->ref; ref; ref = ref->next)
   10937          310 :         if (ref->type == REF_SUBSTRING
   10938           74 :             && (ref->u.ss.start == NULL
   10939           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10940           74 :                 || ref->u.ss.end == NULL
   10941           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10942              :           break;
   10943              : 
   10944          527 :       if (!sym->ts.u.cl)
   10945          182 :         sym->ts.u.cl = target->ts.u.cl;
   10946              : 
   10947          527 :       if (sym->ts.deferred
   10948          195 :           && sym->ts.u.cl == target->ts.u.cl)
   10949              :         {
   10950          116 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10951          116 :           sym->ts.deferred = 1;
   10952              :         }
   10953              : 
   10954          527 :       if (!sym->ts.u.cl->length
   10955          333 :           && !sym->ts.deferred
   10956          138 :           && target->expr_type == EXPR_CONSTANT)
   10957              :         {
   10958           30 :           sym->ts.u.cl->length =
   10959           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10960           30 :                                   target->value.character.length);
   10961              :         }
   10962          497 :       else if (((!sym->ts.u.cl->length
   10963          194 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10964          309 :                 && target->expr_type != EXPR_VARIABLE)
   10965          367 :                || ref)
   10966              :         {
   10967          156 :           if (!sym->ts.deferred)
   10968              :             {
   10969           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10970           45 :               sym->ts.deferred = 1;
   10971              :             }
   10972              : 
   10973              :           /* This is reset in trans-stmt.cc after the assignment
   10974              :              of the target expression to the associate name.  */
   10975          156 :           if (ref && sym->as)
   10976           26 :             sym->attr.pointer = 1;
   10977              :           else
   10978          130 :             sym->attr.allocatable = 1;
   10979              :         }
   10980              :     }
   10981              : 
   10982         7905 :   if (sym->ts.type == BT_CLASS
   10983         1484 :       && IS_INFERRED_TYPE (target)
   10984           13 :       && target->ts.type == BT_DERIVED
   10985            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10986            0 :       && target->ref && target->ref->next && !target->ref->next->next
   10987            0 :       && target->ref->next->type == REF_ARRAY)
   10988            0 :     target->ts = target->symtree->n.sym->ts;
   10989              : 
   10990              :   /* If the target is a good class object, so is the associate variable.  */
   10991         7905 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10992          731 :     sym->attr.class_ok = 1;
   10993              : 
   10994              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   10995         7905 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   10996            3 :     sym->attr.contiguous = 1;
   10997              : }
   10998              : 
   10999              : 
   11000              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   11001              :    array reference, where necessary.  The symbols are artificial and so
   11002              :    the dimension attribute and arrayspec can also be set.  In addition,
   11003              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   11004              :    This is corrected here as well.*/
   11005              : 
   11006              : static void
   11007         1755 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   11008              :                  gfc_ref *ref)
   11009              : {
   11010         1755 :   gfc_ref *nref = (*expr1)->ref;
   11011         1755 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   11012         1755 :   gfc_symbol *sym2;
   11013         1755 :   gfc_expr *selector = gfc_copy_expr (expr2);
   11014              : 
   11015         1755 :   (*expr1)->rank = rank;
   11016         1755 :   (*expr1)->corank = corank;
   11017         1755 :   if (selector)
   11018              :     {
   11019          336 :       gfc_resolve_expr (selector);
   11020          336 :       if (selector->expr_type == EXPR_OP
   11021            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   11022            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   11023          334 :       else if (selector->expr_type == EXPR_VARIABLE
   11024            7 :                || selector->expr_type == EXPR_FUNCTION)
   11025          334 :         sym2 = selector->symtree->n.sym;
   11026              :       else
   11027            0 :         gcc_unreachable ();
   11028              :     }
   11029              :   else
   11030              :     sym2 = NULL;
   11031              : 
   11032         1755 :   if (sym1->ts.type == BT_CLASS)
   11033              :     {
   11034         1755 :       if ((*expr1)->ts.type != BT_CLASS)
   11035           13 :         (*expr1)->ts = sym1->ts;
   11036              : 
   11037         1755 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   11038         1755 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   11039         1755 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   11040            1 :         CLASS_DATA (sym1)->as
   11041            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   11042              :     }
   11043              :   else
   11044              :     {
   11045            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   11046            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   11047            0 :       if (sym1->as == NULL && sym2)
   11048            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   11049              :     }
   11050              : 
   11051         3168 :   for (; nref; nref = nref->next)
   11052         2832 :     if (nref->next == NULL)
   11053              :       break;
   11054              : 
   11055         1755 :   if (ref && nref && nref->type != REF_ARRAY)
   11056            6 :     nref->next = gfc_copy_ref (ref);
   11057         1749 :   else if (ref && !nref)
   11058          327 :     (*expr1)->ref = gfc_copy_ref (ref);
   11059         1422 :   else if (ref && nref->u.ar.codimen != corank)
   11060              :     {
   11061          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   11062          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11063           61 :       nref->u.ar.codimen = corank;
   11064              :     }
   11065         1755 : }
   11066              : 
   11067              : 
   11068              : static gfc_expr *
   11069         6964 : build_loc_call (gfc_expr *sym_expr)
   11070              : {
   11071         6964 :   gfc_expr *loc_call;
   11072         6964 :   loc_call = gfc_get_expr ();
   11073         6964 :   loc_call->expr_type = EXPR_FUNCTION;
   11074         6964 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11075         6964 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11076         6964 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11077         6964 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11078         6964 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11079         6964 :   loc_call->ts.type = BT_INTEGER;
   11080         6964 :   loc_call->ts.kind = gfc_index_integer_kind;
   11081         6964 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11082         6964 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11083         6964 :   loc_call->value.function.actual->expr = sym_expr;
   11084         6964 :   loc_call->where = sym_expr->where;
   11085         6964 :   return loc_call;
   11086              : }
   11087              : 
   11088              : /* Resolve a SELECT TYPE statement.  */
   11089              : 
   11090              : static void
   11091         3135 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11092              : {
   11093         3135 :   gfc_symbol *selector_type;
   11094         3135 :   gfc_code *body, *new_st, *if_st, *tail;
   11095         3135 :   gfc_code *class_is = NULL, *default_case = NULL;
   11096         3135 :   gfc_case *c;
   11097         3135 :   gfc_symtree *st;
   11098         3135 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11099         3135 :   gfc_namespace *ns;
   11100         3135 :   int error = 0;
   11101         3135 :   int rank = 0, corank = 0;
   11102         3135 :   gfc_ref* ref = NULL;
   11103         3135 :   gfc_expr *selector_expr = NULL;
   11104         3135 :   gfc_code *old_code = code;
   11105              : 
   11106         3135 :   ns = code->ext.block.ns;
   11107         3135 :   if (code->expr2)
   11108              :     {
   11109              :       /* Set this, or coarray checks in resolve will fail.  */
   11110          688 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11111              :     }
   11112         3135 :   gfc_resolve (ns);
   11113              : 
   11114              :   /* Check for F03:C813.  */
   11115         3135 :   if (code->expr1->ts.type != BT_CLASS
   11116           36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11117              :     {
   11118           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11119              :                  "at %L", &code->loc);
   11120           42 :       return;
   11121              :     }
   11122              : 
   11123              :   /* Prevent segfault, when class type is not initialized due to previous
   11124              :      error.  */
   11125         3122 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11126         3120 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11127              :     return;
   11128              : 
   11129         3115 :   if (code->expr2)
   11130              :     {
   11131          679 :       gfc_ref *ref2 = NULL;
   11132         1568 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11133          889 :          if (ref->type == REF_COMPONENT
   11134          453 :              && ref->u.c.component->ts.type == BT_CLASS)
   11135          889 :            ref2 = ref;
   11136              : 
   11137          679 :       if (ref2)
   11138              :         {
   11139          359 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11140            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11141          359 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11142              :         }
   11143              :       else
   11144              :         {
   11145          320 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11146           28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11147              :           /* Sometimes the selector expression is given the typespec of the
   11148              :              '_data' field, which is logical enough but inappropriate here. */
   11149          320 :           if (code->expr2->ts.type == BT_DERIVED
   11150           73 :               && code->expr2->symtree
   11151           73 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11152           73 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11153          320 :           selector_type = CLASS_DATA (code->expr2)
   11154              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11155              :         }
   11156              : 
   11157          679 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11158              :         {
   11159          322 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11160          322 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11161          322 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11162              :         }
   11163              : 
   11164              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11165          679 :       if (gfc_is_coindexed (code->expr2))
   11166              :         {
   11167            4 :           gfc_error ("Selector at %L must not be coindexed",
   11168            4 :                      &code->expr2->where);
   11169            4 :           return;
   11170              :         }
   11171              : 
   11172              :     }
   11173              :   else
   11174              :     {
   11175         2436 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11176              : 
   11177         2436 :       if (gfc_is_coindexed (code->expr1))
   11178              :         {
   11179            0 :           gfc_error ("Selector at %L must not be coindexed",
   11180            0 :                      &code->expr1->where);
   11181            0 :           return;
   11182              :         }
   11183              :     }
   11184              : 
   11185              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11186         8651 :   for (body = code->block; body; body = body->block)
   11187              :     {
   11188         5541 :       c = body->ext.block.case_list;
   11189              : 
   11190         5541 :       if (!error)
   11191              :         {
   11192              :           /* Check for repeated cases.  */
   11193         8566 :           for (tail = code->block; tail; tail = tail->block)
   11194              :             {
   11195         8566 :               gfc_case *d = tail->ext.block.case_list;
   11196         8566 :               if (tail == body)
   11197              :                 break;
   11198              : 
   11199         3034 :               if (c->ts.type == d->ts.type
   11200          516 :                   && ((c->ts.type == BT_DERIVED
   11201          418 :                        && c->ts.u.derived && d->ts.u.derived
   11202          418 :                        && !strcmp (c->ts.u.derived->name,
   11203              :                                    d->ts.u.derived->name))
   11204          515 :                       || c->ts.type == BT_UNKNOWN
   11205          515 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11206           55 :                           && c->ts.kind == d->ts.kind)))
   11207              :                 {
   11208            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11209              :                              &c->where, &d->where);
   11210            1 :                   return;
   11211              :                 }
   11212              :             }
   11213              :         }
   11214              : 
   11215              :       /* Check F03:C815.  */
   11216         3502 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11217         2388 :           && selector_type
   11218         2388 :           && !selector_type->attr.unlimited_polymorphic
   11219         7605 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11220              :         {
   11221            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11222            1 :                      c->ts.u.derived->name, &c->where);
   11223            1 :           error++;
   11224            1 :           continue;
   11225              :         }
   11226              : 
   11227              :       /* Check F03:C816.  */
   11228         5545 :       if (c->ts.type != BT_UNKNOWN
   11229         3869 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11230         7607 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11231         2064 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11232              :         {
   11233            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11234            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11235            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11236              :           else
   11237            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11238              :                        gfc_basic_typename (c->ts.type), &c->where);
   11239            6 :           error++;
   11240            6 :           continue;
   11241              :         }
   11242              : 
   11243              :       /* Check F03:C814.  */
   11244         5533 :       if (c->ts.type == BT_CHARACTER
   11245          742 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11246              :         {
   11247            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11248              :                      "type parameter is assumed", &c->where);
   11249            0 :           error++;
   11250            0 :           continue;
   11251              :         }
   11252              : 
   11253              :       /* Intercept the DEFAULT case.  */
   11254         5533 :       if (c->ts.type == BT_UNKNOWN)
   11255              :         {
   11256              :           /* Check F03:C818.  */
   11257         1670 :           if (default_case)
   11258              :             {
   11259            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11260              :                          "by a second DEFAULT CASE at %L",
   11261            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11262            1 :               error++;
   11263            1 :               continue;
   11264              :             }
   11265              : 
   11266              :           default_case = body;
   11267              :         }
   11268              :     }
   11269              : 
   11270         3110 :   if (error > 0)
   11271              :     return;
   11272              : 
   11273              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11274              :      target if present.  If there are any EXIT statements referring to the
   11275              :      SELECT TYPE construct, this is no problem because the gfc_code
   11276              :      reference stays the same and EXIT is equally possible from the BLOCK
   11277              :      it is changed to.  */
   11278         3107 :   code->op = EXEC_BLOCK;
   11279         3107 :   if (code->expr2)
   11280              :     {
   11281          675 :       gfc_association_list* assoc;
   11282              : 
   11283          675 :       assoc = gfc_get_association_list ();
   11284          675 :       assoc->st = code->expr1->symtree;
   11285          675 :       assoc->target = gfc_copy_expr (code->expr2);
   11286          675 :       assoc->target->where = code->expr2->where;
   11287              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11288              : 
   11289          675 :       code->ext.block.assoc = assoc;
   11290          675 :       code->expr1->symtree->n.sym->assoc = assoc;
   11291              : 
   11292          675 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11293              :     }
   11294              :   else
   11295         2432 :     code->ext.block.assoc = NULL;
   11296              : 
   11297              :   /* Ensure that the selector rank and arrayspec are available to
   11298              :      correct expressions in which they might be missing.  */
   11299         3107 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11300              :     {
   11301          336 :       rank = code->expr2->rank;
   11302          336 :       corank = code->expr2->corank;
   11303          620 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11304          611 :         if (ref->next == NULL)
   11305              :           break;
   11306          336 :       if (ref && ref->type == REF_ARRAY)
   11307          327 :         ref = gfc_copy_ref (ref);
   11308              : 
   11309              :       /* Fixup expr1 if necessary.  */
   11310          336 :       if (rank || corank)
   11311          336 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11312              :     }
   11313         2771 :   else if (code->expr1->rank || code->expr1->corank)
   11314              :     {
   11315          904 :       rank = code->expr1->rank;
   11316          904 :       corank = code->expr1->corank;
   11317          904 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11318          904 :         if (ref->next == NULL)
   11319              :           break;
   11320          904 :       if (ref && ref->type == REF_ARRAY)
   11321          904 :         ref = gfc_copy_ref (ref);
   11322              :     }
   11323              : 
   11324         3107 :   gfc_expr *orig_expr1 = code->expr1;
   11325              : 
   11326              :   /* Add EXEC_SELECT to switch on type.  */
   11327         3107 :   new_st = gfc_get_code (code->op);
   11328         3107 :   new_st->expr1 = code->expr1;
   11329         3107 :   new_st->expr2 = code->expr2;
   11330         3107 :   new_st->block = code->block;
   11331         3107 :   code->expr1 = code->expr2 =  NULL;
   11332         3107 :   code->block = NULL;
   11333         3107 :   if (!ns->code)
   11334         3107 :     ns->code = new_st;
   11335              :   else
   11336            0 :     ns->code->next = new_st;
   11337         3107 :   code = new_st;
   11338         3107 :   code->op = EXEC_SELECT_TYPE;
   11339              : 
   11340              :   /* Use the intrinsic LOC function to generate an integer expression
   11341              :      for the vtable of the selector.  Note that the rank of the selector
   11342              :      expression has to be set to zero.  */
   11343         3107 :   gfc_add_vptr_component (code->expr1);
   11344         3107 :   code->expr1->rank = 0;
   11345         3107 :   code->expr1->corank = 0;
   11346         3107 :   code->expr1 = build_loc_call (code->expr1);
   11347         3107 :   selector_expr = code->expr1->value.function.actual->expr;
   11348              : 
   11349              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11350         8632 :   for (body = code->block; body; body = body->block)
   11351              :     {
   11352         5525 :       gfc_symbol *vtab;
   11353         5525 :       c = body->ext.block.case_list;
   11354              : 
   11355              :       /* Generate an index integer expression for address of the
   11356              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11357              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11358         5525 :       if (c->ts.type != BT_UNKNOWN)
   11359              :         {
   11360         3857 :           gfc_expr *e;
   11361         3857 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11362              :             {
   11363         2379 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11364         2379 :               gcc_assert (vtab);
   11365         2379 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11366         2379 :                                           c->ts.u.derived->hash_value);
   11367              :             }
   11368              :           else
   11369              :             {
   11370         1478 :               vtab = gfc_find_vtab (&c->ts);
   11371         1478 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11372         1478 :               e = CLASS_DATA (vtab)->initializer;
   11373         1478 :               c->high = gfc_copy_expr (e);
   11374         1478 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11375              :                 {
   11376            1 :                   gfc_typespec ts;
   11377            1 :                   ts.kind = gfc_integer_4_kind;
   11378            1 :                   ts.type = BT_INTEGER;
   11379            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11380              :                 }
   11381              :             }
   11382              : 
   11383         3857 :           e = gfc_lval_expr_from_sym (vtab);
   11384         3857 :           c->low = build_loc_call (e);
   11385              :         }
   11386              :       else
   11387         1668 :         continue;
   11388              : 
   11389              :       /* Associate temporary to selector.  This should only be done
   11390              :          when this case is actually true, so build a new ASSOCIATE
   11391              :          that does precisely this here (instead of using the
   11392              :          'global' one).  */
   11393              : 
   11394              :       /* First check the derived type import status.  */
   11395         3857 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11396            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11397              :         {
   11398           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11399            6 :                                  c->ts.u.derived->name);
   11400            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11401              :                                         gfc_current_ns))
   11402            6 :             error++;
   11403              :         }
   11404              : 
   11405         3857 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11406         3857 :       if (c->ts.type == BT_CLASS)
   11407          348 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11408          348 :                   c->ts.u.derived->name, var_name);
   11409         3509 :       else if (c->ts.type == BT_DERIVED)
   11410         2031 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11411         2031 :                   c->ts.u.derived->name, var_name);
   11412         1478 :       else if (c->ts.type == BT_CHARACTER)
   11413              :         {
   11414          742 :           HOST_WIDE_INT charlen = 0;
   11415          742 :           if (c->ts.u.cl && c->ts.u.cl->length
   11416            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11417            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11418          742 :           snprintf (name, sizeof (name),
   11419              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11420              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11421              :                     var_name);
   11422              :         }
   11423              :       else
   11424          736 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11425              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11426              : 
   11427         3857 :       st = gfc_find_symtree (ns->sym_root, name);
   11428         3857 :       gcc_assert (st->n.sym->assoc);
   11429         3857 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11430         3857 :       st->n.sym->assoc->target->where = selector_expr->where;
   11431         3857 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11432              :         {
   11433         3509 :           gfc_add_data_component (st->n.sym->assoc->target);
   11434              :           /* Fixup the target expression if necessary.  */
   11435         3509 :           if (rank || corank)
   11436         1419 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11437              :                              ref);
   11438              :         }
   11439              : 
   11440         3857 :       new_st = gfc_get_code (EXEC_BLOCK);
   11441         3857 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11442         3857 :       new_st->ext.block.ns->code = body->next;
   11443         3857 :       body->next = new_st;
   11444              : 
   11445              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11446              :          there is a CASE label overlap and this is already used.  Just ignore,
   11447              :          the error is diagnosed elsewhere.  */
   11448         3857 :       if (st->n.sym->assoc->dangling)
   11449              :         {
   11450         3856 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11451         3856 :           st->n.sym->assoc->dangling = 0;
   11452              :         }
   11453              : 
   11454         3857 :       resolve_assoc_var (st->n.sym, false);
   11455              :     }
   11456              : 
   11457              :   /* Take out CLASS IS cases for separate treatment.  */
   11458              :   body = code;
   11459         8632 :   while (body && body->block)
   11460              :     {
   11461         5525 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11462              :         {
   11463              :           /* Add to class_is list.  */
   11464          348 :           if (class_is == NULL)
   11465              :             {
   11466          317 :               class_is = body->block;
   11467          317 :               tail = class_is;
   11468              :             }
   11469              :           else
   11470              :             {
   11471           43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11472           31 :               tail->block = body->block;
   11473           31 :               tail = tail->block;
   11474              :             }
   11475              :           /* Remove from EXEC_SELECT list.  */
   11476          348 :           body->block = body->block->block;
   11477          348 :           tail->block = NULL;
   11478              :         }
   11479              :       else
   11480              :         body = body->block;
   11481              :     }
   11482              : 
   11483         3107 :   if (class_is)
   11484              :     {
   11485          317 :       gfc_symbol *vtab;
   11486              : 
   11487          317 :       if (!default_case)
   11488              :         {
   11489              :           /* Add a default case to hold the CLASS IS cases.  */
   11490          315 :           for (tail = code; tail->block; tail = tail->block) ;
   11491          207 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11492          207 :           tail = tail->block;
   11493          207 :           tail->ext.block.case_list = gfc_get_case ();
   11494          207 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11495          207 :           tail->next = NULL;
   11496          207 :           default_case = tail;
   11497              :         }
   11498              : 
   11499              :       /* More than one CLASS IS block?  */
   11500          317 :       if (class_is->block)
   11501              :         {
   11502           37 :           gfc_code **c1,*c2;
   11503           37 :           bool swapped;
   11504              :           /* Sort CLASS IS blocks by extension level.  */
   11505           36 :           do
   11506              :             {
   11507           37 :               swapped = false;
   11508           97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11509              :                 {
   11510           61 :                   c2 = (*c1)->block;
   11511              :                   /* F03:C817 (check for doubles).  */
   11512           61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   11513           61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   11514              :                     {
   11515            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11516              :                                  "statement at %L",
   11517              :                                  &c2->ext.block.case_list->where);
   11518            1 :                       return;
   11519              :                     }
   11520           60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11521           60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11522              :                     {
   11523              :                       /* Swap.  */
   11524           24 :                       (*c1)->block = c2->block;
   11525           24 :                       c2->block = *c1;
   11526           24 :                       *c1 = c2;
   11527           24 :                       swapped = true;
   11528              :                     }
   11529              :                 }
   11530              :             }
   11531              :           while (swapped);
   11532              :         }
   11533              : 
   11534              :       /* Generate IF chain.  */
   11535          316 :       if_st = gfc_get_code (EXEC_IF);
   11536          316 :       new_st = if_st;
   11537          662 :       for (body = class_is; body; body = body->block)
   11538              :         {
   11539          346 :           new_st->block = gfc_get_code (EXEC_IF);
   11540          346 :           new_st = new_st->block;
   11541              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11542          346 :           new_st->expr1 = gfc_get_expr ();
   11543          346 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11544          346 :           new_st->expr1->ts.type = BT_LOGICAL;
   11545          346 :           new_st->expr1->ts.kind = 4;
   11546          346 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11547          346 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11548          346 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11549              :           /* Set up arguments.  */
   11550          346 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11551          346 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11552          346 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11553          346 :           new_st->expr1->where = code->loc;
   11554          346 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11555          346 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11556          346 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11557          346 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11558          346 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11559          346 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11560              :           /* Set up types in formal arg list.  */
   11561          346 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11562          346 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11563          346 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11564          346 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11565              : 
   11566          346 :           new_st->next = body->next;
   11567              :         }
   11568          316 :         if (default_case->next)
   11569              :           {
   11570          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11571          110 :             new_st = new_st->block;
   11572          110 :             new_st->next = default_case->next;
   11573              :           }
   11574              : 
   11575              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11576          316 :         default_case->next = if_st;
   11577              :     }
   11578              : 
   11579              :   /* Resolve the internal code.  This cannot be done earlier because
   11580              :      it requires that the sym->assoc of selectors is set already.  */
   11581         3106 :   gfc_current_ns = ns;
   11582         3106 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11583         3106 :   gfc_current_ns = old_ns;
   11584              : 
   11585         3106 :   free (ref);
   11586              : }
   11587              : 
   11588              : 
   11589              : /* Resolve a SELECT RANK statement.  */
   11590              : 
   11591              : static void
   11592         1048 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11593              : {
   11594         1048 :   gfc_namespace *ns;
   11595         1048 :   gfc_code *body, *new_st, *tail;
   11596         1048 :   gfc_case *c;
   11597         1048 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11598         1048 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11599         1048 :   gfc_symtree *st;
   11600         1048 :   gfc_expr *selector_expr = NULL;
   11601         1048 :   int case_value;
   11602         1048 :   HOST_WIDE_INT charlen = 0;
   11603              : 
   11604         1048 :   ns = code->ext.block.ns;
   11605         1048 :   gfc_resolve (ns);
   11606              : 
   11607         1048 :   code->op = EXEC_BLOCK;
   11608         1048 :   if (code->expr2)
   11609              :     {
   11610           42 :       gfc_association_list* assoc;
   11611              : 
   11612           42 :       assoc = gfc_get_association_list ();
   11613           42 :       assoc->st = code->expr1->symtree;
   11614           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11615           42 :       assoc->target->where = code->expr2->where;
   11616              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11617              : 
   11618           42 :       code->ext.block.assoc = assoc;
   11619           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11620              : 
   11621           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11622              :     }
   11623              :   else
   11624         1006 :     code->ext.block.assoc = NULL;
   11625              : 
   11626              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11627              :      cascade of further errors because the case blocks do not compile
   11628              :      correctly.  */
   11629         3416 :   for (body = code->block; body; body = body->block)
   11630              :     {
   11631         2368 :       c = body->ext.block.case_list;
   11632         2368 :       if (c->low)
   11633         1425 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11634              :       else
   11635              :         case_value = -2;
   11636              : 
   11637              :       /* Check for repeated cases.  */
   11638         5950 :       for (tail = code->block; tail; tail = tail->block)
   11639              :         {
   11640         5950 :           gfc_case *d = tail->ext.block.case_list;
   11641         5950 :           int case_value2;
   11642              : 
   11643         5950 :           if (tail == body)
   11644              :             break;
   11645              : 
   11646              :           /* Check F2018: C1153.  */
   11647         3582 :           if (!c->low && !d->low)
   11648            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11649              :                        &c->where, &d->where);
   11650              : 
   11651         3582 :           if (!c->low || !d->low)
   11652         1289 :             continue;
   11653              : 
   11654              :           /* Check F2018: C1153.  */
   11655         2293 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11656         2293 :           if ((case_value == case_value2) && case_value == -1)
   11657            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11658              :                        &c->where, &d->where);
   11659         2292 :           else if (case_value == case_value2)
   11660            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11661              :                        case_value, &c->where, &d->where);
   11662              :         }
   11663              : 
   11664         2368 :       if (!c->low)
   11665          943 :         continue;
   11666              : 
   11667              :       /* Check F2018: C1155.  */
   11668         1425 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11669         1425 :                                || gfc_expr_attr (code->expr1).pointer))
   11670            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11671            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11672              :     }
   11673              : 
   11674              :   /* Add EXEC_SELECT to switch on rank.  */
   11675         1048 :   new_st = gfc_get_code (code->op);
   11676         1048 :   new_st->expr1 = code->expr1;
   11677         1048 :   new_st->expr2 = code->expr2;
   11678         1048 :   new_st->block = code->block;
   11679         1048 :   code->expr1 = code->expr2 =  NULL;
   11680         1048 :   code->block = NULL;
   11681         1048 :   if (!ns->code)
   11682         1048 :     ns->code = new_st;
   11683              :   else
   11684            0 :     ns->code->next = new_st;
   11685         1048 :   code = new_st;
   11686         1048 :   code->op = EXEC_SELECT_RANK;
   11687              : 
   11688         1048 :   selector_expr = code->expr1;
   11689              : 
   11690              :   /* Loop over SELECT RANK cases.  */
   11691         3416 :   for (body = code->block; body; body = body->block)
   11692              :     {
   11693         2368 :       c = body->ext.block.case_list;
   11694         2368 :       int case_value;
   11695              : 
   11696              :       /* Pass on the default case.  */
   11697         2368 :       if (c->low == NULL)
   11698          943 :         continue;
   11699              : 
   11700              :       /* Associate temporary to selector.  This should only be done
   11701              :          when this case is actually true, so build a new ASSOCIATE
   11702              :          that does precisely this here (instead of using the
   11703              :          'global' one).  */
   11704         1425 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11705          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11706          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11707              : 
   11708         1425 :       if (c->ts.type == BT_CLASS)
   11709          145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11710         1280 :       else if (c->ts.type == BT_DERIVED)
   11711          116 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11712         1164 :       else if (c->ts.type != BT_CHARACTER)
   11713          605 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11714              :       else
   11715          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11716              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11717              : 
   11718         1425 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11719         1425 :       if (case_value >= 0)
   11720         1392 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11721              :       else
   11722           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11723              : 
   11724         1425 :       st = gfc_find_symtree (ns->sym_root, name);
   11725         1425 :       gcc_assert (st->n.sym->assoc);
   11726              : 
   11727         1425 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11728         1425 :       st->n.sym->assoc->target->where = selector_expr->where;
   11729              : 
   11730         1425 :       new_st = gfc_get_code (EXEC_BLOCK);
   11731         1425 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11732         1425 :       new_st->ext.block.ns->code = body->next;
   11733         1425 :       body->next = new_st;
   11734              : 
   11735              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11736              :          there is a CASE label overlap and this is already used.  Just ignore,
   11737              :          the error is diagnosed elsewhere.  */
   11738         1425 :       if (st->n.sym->assoc->dangling)
   11739              :         {
   11740         1423 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11741         1423 :           st->n.sym->assoc->dangling = 0;
   11742              :         }
   11743              : 
   11744         1425 :       resolve_assoc_var (st->n.sym, false);
   11745              :     }
   11746              : 
   11747         1048 :   gfc_current_ns = ns;
   11748         1048 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11749         1048 :   gfc_current_ns = old_ns;
   11750         1048 : }
   11751              : 
   11752              : 
   11753              : /* Resolve a transfer statement. This is making sure that:
   11754              :    -- a derived type being transferred has only non-pointer components
   11755              :    -- a derived type being transferred doesn't have private components, unless
   11756              :       it's being transferred from the module where the type was defined
   11757              :    -- we're not trying to transfer a whole assumed size array.  */
   11758              : 
   11759              : static void
   11760        47647 : resolve_transfer (gfc_code *code)
   11761              : {
   11762        47647 :   gfc_symbol *sym, *derived;
   11763        47647 :   gfc_ref *ref;
   11764        47647 :   gfc_expr *exp;
   11765        47647 :   bool write = false;
   11766        47647 :   bool formatted = false;
   11767        47647 :   gfc_dt *dt = code->ext.dt;
   11768        47647 :   gfc_symbol *dtio_sub = NULL;
   11769              : 
   11770        47647 :   exp = code->expr1;
   11771              : 
   11772        95300 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11773        48581 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11774            6 :     exp = exp->value.op.op1;
   11775              : 
   11776        47647 :   if (exp && exp->expr_type == EXPR_NULL
   11777            2 :       && code->ext.dt)
   11778              :     {
   11779            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11780              :                  &exp->where);
   11781            2 :       return;
   11782              :     }
   11783              : 
   11784        47645 :   if (dt && (dt->dt_io_kind->value.iokind == M_WRITE
   11785        47493 :              || dt->dt_io_kind->value.iokind == M_PRINT))
   11786        39887 :     gfc_value_used_expr (exp, VALUE_USED);
   11787              : 
   11788        47645 :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11789              :                       && exp->expr_type != EXPR_FUNCTION
   11790              :                       && exp->expr_type != EXPR_ARRAY
   11791              :                       && exp->expr_type != EXPR_STRUCTURE))
   11792              :     return;
   11793              : 
   11794        26431 :   if (dt && dt->dt_io_kind->value.iokind == M_READ)
   11795              :     {
   11796              :       /* If we are reading, the variable will be changed.  Note that
   11797              :          code->ext.dt may be NULL if the TRANSFER is related to an INQUIRE
   11798              :          statement -- but in this case, we are not reading, either.  */
   11799         7606 :       if (!gfc_check_vardef_context (exp, false, false, false,
   11800         7606 :                                      _("item in READ")))
   11801              :         return;
   11802              : 
   11803         7602 :       gfc_expr_set_at (exp, &exp->where, VALUE_READ);
   11804              :     }
   11805              : 
   11806        26427 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11807        26427 :                         || exp->expr_type == EXPR_FUNCTION
   11808        22029 :                         || exp->expr_type == EXPR_ARRAY
   11809        48456 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11810              : 
   11811              :   /* Go to actual component transferred.  */
   11812        34260 :   for (ref = exp->ref; ref; ref = ref->next)
   11813         7833 :     if (ref->type == REF_COMPONENT)
   11814         2211 :       ts = &ref->u.c.component->ts;
   11815              : 
   11816        26427 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11817        26279 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11818              :     {
   11819          720 :       derived = ts->u.derived;
   11820              : 
   11821              :       /* Determine when to use the formatted DTIO procedure.  */
   11822          720 :       if (dt && (dt->format_expr || dt->format_label))
   11823          645 :         formatted = true;
   11824              : 
   11825          720 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11826          720 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11827          720 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11828              : 
   11829          720 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11830              :         {
   11831          450 :           dt->udtio = exp;
   11832          450 :           sym = exp->symtree->n.sym->ns->proc_name;
   11833              :           /* Check to see if this is a nested DTIO call, with the
   11834              :              dummy as the io-list object.  */
   11835          450 :           if (sym && sym == dtio_sub && sym->formal
   11836           30 :               && sym->formal->sym == exp->symtree->n.sym
   11837           30 :               && exp->ref == NULL)
   11838              :             {
   11839            0 :               if (!sym->attr.recursive)
   11840              :                 {
   11841            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11842              :                              sym->name, &sym->declared_at);
   11843            0 :                   return;
   11844              :                 }
   11845              :             }
   11846              :         }
   11847              :     }
   11848              : 
   11849        26427 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11850              :     {
   11851            3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11852              :                 "it is processed by a defined input/output procedure",
   11853              :                 &code->loc);
   11854            3 :       return;
   11855              :     }
   11856              : 
   11857        26424 :   if (ts->type == BT_DERIVED)
   11858              :     {
   11859              :       /* Check that transferred derived type doesn't contain POINTER
   11860              :          components unless it is processed by a defined input/output
   11861              :          procedure".  */
   11862          688 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11863              :         {
   11864            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11865              :                      "components unless it is processed by a defined "
   11866              :                      "input/output procedure", &code->loc);
   11867            2 :           return;
   11868              :         }
   11869              : 
   11870              :       /* F08:C935.  */
   11871          686 :       if (ts->u.derived->attr.proc_pointer_comp)
   11872              :         {
   11873            2 :           gfc_error ("Data transfer element at %L cannot have "
   11874              :                      "procedure pointer components", &code->loc);
   11875            2 :           return;
   11876              :         }
   11877              : 
   11878          684 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11879              :         {
   11880            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11881              :                      "components unless it is processed by a defined "
   11882              :                      "input/output procedure", &code->loc);
   11883            6 :           return;
   11884              :         }
   11885              : 
   11886              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11887              :          be printed.  However, if -std=gnu and not -pedantic, allow
   11888              :          the component to be printed to help debugging.  */
   11889          678 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11890              :         {
   11891            4 :           gfc_error ("Data transfer element at %L "
   11892              :                      "cannot have PRIVATE components", &code->loc);
   11893            4 :             return;
   11894              :         }
   11895          674 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11896              :         {
   11897            4 :           gfc_error ("Data transfer element at %L cannot have "
   11898              :                      "PRIVATE components unless it is processed by "
   11899              :                      "a defined input/output procedure", &code->loc);
   11900            4 :           return;
   11901              :         }
   11902              :     }
   11903              : 
   11904        26406 :   if (exp->expr_type == EXPR_STRUCTURE)
   11905              :     return;
   11906              : 
   11907        26361 :   if (exp->expr_type == EXPR_ARRAY)
   11908              :     return;
   11909              : 
   11910        25979 :   sym = exp->symtree->n.sym;
   11911              : 
   11912        25979 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11913           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11914              :     {
   11915            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11916              :                  "an assumed-size array", &code->loc);
   11917            1 :       return;
   11918              :     }
   11919              : 
   11920              : }
   11921              : 
   11922              : 
   11923              : /*********** Toplevel code resolution subroutines ***********/
   11924              : 
   11925              : /* Find the set of labels that are reachable from this block.  We also
   11926              :    record the last statement in each block.  */
   11927              : 
   11928              : static void
   11929       698425 : find_reachable_labels (gfc_code *block)
   11930              : {
   11931       698425 :   gfc_code *c;
   11932              : 
   11933       698425 :   if (!block)
   11934              :     return;
   11935              : 
   11936       431032 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11937              : 
   11938              :   /* Collect labels in this block.  We don't keep those corresponding
   11939              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11940              :      up through the code_stack.  */
   11941      1579914 :   for (c = block; c; c = c->next)
   11942              :     {
   11943      1148882 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11944         3662 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11945              :     }
   11946              : 
   11947              :   /* Merge with labels from parent block.  */
   11948       431032 :   if (cs_base->prev)
   11949              :     {
   11950       353506 :       gcc_assert (cs_base->prev->reachable_labels);
   11951       353506 :       bitmap_ior_into (cs_base->reachable_labels,
   11952              :                        cs_base->prev->reachable_labels);
   11953              :     }
   11954              : }
   11955              : 
   11956              : static void
   11957          197 : resolve_lock_unlock_event (gfc_code *code)
   11958              : {
   11959          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11960          197 :       && (code->expr1->ts.type != BT_DERIVED
   11961          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   11962          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11963          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11964          136 :           || code->expr1->rank != 0
   11965          181 :           || (!gfc_is_coarray (code->expr1) &&
   11966           46 :               !gfc_is_coindexed (code->expr1))))
   11967            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11968            4 :                &code->expr1->where);
   11969          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11970           58 :            && (code->expr1->ts.type != BT_DERIVED
   11971           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   11972           58 :                || code->expr1->ts.u.derived->from_intmod
   11973              :                   != INTMOD_ISO_FORTRAN_ENV
   11974           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   11975              :                   != ISOFORTRAN_EVENT_TYPE
   11976           58 :                || code->expr1->rank != 0))
   11977            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11978              :                &code->expr1->where);
   11979           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11980          209 :            && !gfc_is_coindexed (code->expr1))
   11981            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11982            0 :                &code->expr1->where);
   11983          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11984            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11985            0 :                "coindexed", &code->expr1->where);
   11986              : 
   11987              :   /* Check STAT.  */
   11988          197 :   if (code->expr2
   11989           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11990           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11991            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11992              :                &code->expr2->where);
   11993              : 
   11994          197 :   if (code->expr2
   11995          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11996           54 :                                     _("STAT variable")))
   11997              :     return;
   11998              : 
   11999              :   /* Check ERRMSG.  */
   12000          197 :   if (code->expr3
   12001            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   12002            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   12003            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12004              :                &code->expr3->where);
   12005              : 
   12006          197 :   if (code->expr3
   12007          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   12008            2 :                                     _("ERRMSG variable")))
   12009              :     return;
   12010              : 
   12011              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   12012          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12013           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   12014           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   12015            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   12016              :                "variable", &code->expr4->where);
   12017              : 
   12018          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   12019          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   12020           22 :                                     _("ACQUIRED_LOCK variable")))
   12021              :     return;
   12022              : 
   12023              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   12024          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   12025              :     {
   12026           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   12027           36 :           || code->expr4->rank != 0)
   12028            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   12029            0 :                    "expression", &code->expr4->where);
   12030              :     }
   12031              : }
   12032              : 
   12033              : static void
   12034          294 : resolve_team_argument (gfc_expr *team)
   12035              : {
   12036          294 :   gfc_resolve_expr (team);
   12037          294 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   12038          287 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   12039          287 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   12040              :     {
   12041            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   12042              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   12043              :                  &team->where);
   12044              :     }
   12045          294 : }
   12046              : 
   12047              : static void
   12048         1514 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   12049              :                                 gfc_expr *e)
   12050              : {
   12051         1514 :   gfc_resolve_expr (e);
   12052         1514 :   if (e
   12053          139 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   12054          124 :           || e->expr_type != EXPR_VARIABLE))
   12055           15 :     gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   12056              :                "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   12057              :                exp_kind);
   12058         1514 : }
   12059              : 
   12060              : void
   12061          757 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   12062              : {
   12063          757 :   resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
   12064          757 :   resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   12065              :                                   gfc_default_character_kind,
   12066              :                                   sync_stat->errmsg);
   12067          757 : }
   12068              : 
   12069              : static void
   12070          308 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   12071              :                          gfc_expr *e)
   12072              : {
   12073          308 :   gfc_resolve_expr (e);
   12074          308 :   if (e
   12075          185 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12076            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12077              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12078          308 : }
   12079              : 
   12080              : static void
   12081          154 : resolve_form_team (gfc_code *code)
   12082              : {
   12083          154 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12084              :                            code->expr1);
   12085          154 :   resolve_team_argument (code->expr2);
   12086          154 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12087              :                            code->expr3);
   12088          154 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12089          154 : }
   12090              : 
   12091              : static void resolve_block_construct (gfc_code *);
   12092              : 
   12093              : static void
   12094           97 : resolve_change_team (gfc_code *code)
   12095              : {
   12096           97 :   resolve_team_argument (code->expr1);
   12097           97 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12098          194 :   resolve_block_construct (code);
   12099              :   /* Map the coarray bounds as selected.  */
   12100          100 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12101            3 :     if (a->ar)
   12102              :       {
   12103            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12104            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12105            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12106              :         else
   12107            3 :           dst = a->st->n.sym->as;
   12108            3 :         dst->corank = src->corank;
   12109            3 :         dst->cotype = src->cotype;
   12110            6 :         for (int i = 0; i < src->corank; ++i)
   12111              :           {
   12112            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12113            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12114            3 :             src->lower[i] = src->upper[i] = nullptr;
   12115              :           }
   12116            3 :         gfc_free_array_spec (src);
   12117            3 :         free (a->ar);
   12118            3 :         a->ar = nullptr;
   12119            3 :         dst->resolved = false;
   12120            3 :         gfc_resolve_array_spec (dst, 0);
   12121              :       }
   12122           97 : }
   12123              : 
   12124              : static void
   12125           43 : resolve_sync_team (gfc_code *code)
   12126              : {
   12127           43 :   resolve_team_argument (code->expr1);
   12128           43 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12129           43 : }
   12130              : 
   12131              : static void
   12132           95 : resolve_end_team (gfc_code *code)
   12133              : {
   12134           95 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12135           95 : }
   12136              : 
   12137              : static void
   12138           54 : resolve_critical (gfc_code *code)
   12139              : {
   12140           54 :   gfc_symtree *symtree;
   12141           54 :   gfc_symbol *lock_type;
   12142           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12143           54 :   static int serial = 0;
   12144              : 
   12145           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12146              : 
   12147           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12148           30 :     return;
   12149              : 
   12150           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12151              :                               GFC_PREFIX ("lock_type"));
   12152           24 :   if (symtree)
   12153           12 :     lock_type = symtree->n.sym;
   12154              :   else
   12155              :     {
   12156           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12157              :                             false) != 0)
   12158            0 :         gcc_unreachable ();
   12159           12 :       lock_type = symtree->n.sym;
   12160           12 :       lock_type->attr.flavor = FL_DERIVED;
   12161           12 :       lock_type->attr.zero_comp = 1;
   12162           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12163           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12164              :     }
   12165              : 
   12166           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12167           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12168            0 :     gcc_unreachable ();
   12169              : 
   12170           24 :   code->resolved_sym = symtree->n.sym;
   12171           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12172           24 :   symtree->n.sym->attr.referenced = 1;
   12173           24 :   symtree->n.sym->attr.artificial = 1;
   12174           24 :   symtree->n.sym->attr.codimension = 1;
   12175           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12176           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12177           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12178           24 :   symtree->n.sym->as->corank = 1;
   12179           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12180           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12181           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12182              :                                                    NULL, 1);
   12183           24 :   gfc_commit_symbols();
   12184              : }
   12185              : 
   12186              : 
   12187              : static void
   12188         1317 : resolve_sync (gfc_code *code)
   12189              : {
   12190              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12191         1317 :   if (code->expr1)
   12192              :     {
   12193           71 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12194            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12195              :                    "INTEGER expression", &code->expr1->where);
   12196           71 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12197           27 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12198            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12199              :                    &code->expr1->where);
   12200           70 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12201           70 :                && gfc_simplify_expr (code->expr1, 0))
   12202              :         {
   12203           20 :            gfc_constructor *cons;
   12204           20 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12205           60 :            for (; cons; cons = gfc_constructor_next (cons))
   12206           20 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12207           20 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12208            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12209              :                           "num_images()", &cons->expr->where);
   12210              :         }
   12211              :     }
   12212              : 
   12213              :   /* Check STAT.  */
   12214         1317 :   gfc_resolve_expr (code->expr2);
   12215         1317 :   if (code->expr2)
   12216              :     {
   12217          108 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12218            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12219              :                    &code->expr2->where);
   12220              :       else
   12221          107 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12222          107 :                                   _("STAT variable"));
   12223              :     }
   12224              : 
   12225              :   /* Check ERRMSG.  */
   12226         1317 :   gfc_resolve_expr (code->expr3);
   12227         1317 :   if (code->expr3)
   12228              :     {
   12229           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12230            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12231              :                    &code->expr3->where);
   12232              :       else
   12233           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12234           86 :                                   _("ERRMSG variable"));
   12235              :     }
   12236         1317 : }
   12237              : 
   12238              : 
   12239              : /* Given a branch to a label, see if the branch is conforming.
   12240              :    The code node describes where the branch is located.  */
   12241              : 
   12242              : static void
   12243       112189 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12244              : {
   12245       112189 :   code_stack *stack;
   12246              : 
   12247       112189 :   if (label == NULL)
   12248              :     return;
   12249              : 
   12250              :   /* Step one: is this a valid branching target?  */
   12251              : 
   12252         2460 :   if (label->defined == ST_LABEL_UNKNOWN)
   12253              :     {
   12254            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12255              :                  &code->loc);
   12256            4 :       return;
   12257              :     }
   12258              : 
   12259         2456 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12260              :     {
   12261            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12262              :                  "for the branch statement at %L", &label->where, &code->loc);
   12263            4 :       return;
   12264              :     }
   12265              : 
   12266              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12267              : 
   12268         2452 :   if (code->here == label)
   12269              :     {
   12270            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12271              :                    &code->loc);
   12272            0 :       return;
   12273              :     }
   12274              : 
   12275              :   /* Step three:  See if the label is in the same block as the
   12276              :      branching statement.  The hard work has been done by setting up
   12277              :      the bitmap reachable_labels.  */
   12278              : 
   12279         2452 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12280              :     {
   12281              :       /* Check now whether there is a CRITICAL construct; if so, check
   12282              :          whether the label is still visible outside of the CRITICAL block,
   12283              :          which is invalid.  */
   12284         6267 :       for (stack = cs_base; stack; stack = stack->prev)
   12285              :         {
   12286         3883 :           if (stack->current->op == EXEC_CRITICAL
   12287         3883 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12288            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12289              :                       "label at %L", &code->loc, &label->where);
   12290         3881 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12291         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12292            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12293              :                       "for label at %L", &code->loc, &label->where);
   12294         3881 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12295         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12296            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12297              :                       "for label at %L", &code->loc, &label->where);
   12298              :         }
   12299              : 
   12300              :       return;
   12301              :     }
   12302              : 
   12303              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12304              :     still be the label of the END of the enclosing block, in which
   12305              :     case we find it by going up the code_stack.  */
   12306              : 
   12307          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12308              :     {
   12309          131 :       if (stack->current->next && stack->current->next->here == label)
   12310              :         break;
   12311          101 :       if (stack->current->op == EXEC_CRITICAL)
   12312              :         {
   12313              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12314              :              construct as END CRITICAL is still part of it.  */
   12315            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12316              :                       " at %L", &code->loc, &label->where);
   12317            2 :           return;
   12318              :         }
   12319           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12320              :         {
   12321            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12322              :                      "label at %L", &code->loc, &label->where);
   12323            0 :           return;
   12324              :         }
   12325              :     }
   12326              : 
   12327           66 :   if (stack)
   12328              :     {
   12329           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12330              :       return;
   12331              :     }
   12332              : 
   12333              :   /* The label is not in an enclosing block, so illegal.  This was
   12334              :      allowed in Fortran 66, so we allow it as extension.  No
   12335              :      further checks are necessary in this case.  */
   12336           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12337              :                   "as the GOTO statement at %L", &label->where,
   12338              :                   &code->loc);
   12339           36 :   return;
   12340              : }
   12341              : 
   12342              : 
   12343              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12344              : 
   12345              : static bool
   12346         1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12347              : {
   12348         1467 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12349         1467 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12350         1467 :   bool result = false;
   12351         1467 :   int i;
   12352              : 
   12353              :   /* Compare the rank.  */
   12354         1467 :   if (expr1->rank != expr2->rank)
   12355              :     return result;
   12356              : 
   12357              :   /* Compare the size of each dimension.  */
   12358         2811 :   for (i=0; i<expr1->rank; i++)
   12359              :     {
   12360         1495 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12361          151 :         goto ignore;
   12362              : 
   12363         1344 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12364            0 :         goto ignore;
   12365              : 
   12366         1344 :       if (mpz_cmp (shape[i], shape2[i]))
   12367            0 :         goto over;
   12368              :     }
   12369              : 
   12370              :   /* When either of the two expression is an assumed size array, we
   12371              :      ignore the comparison of dimension sizes.  */
   12372         1316 : ignore:
   12373              :   result = true;
   12374              : 
   12375         1467 : over:
   12376         1467 :   gfc_clear_shape (shape, i);
   12377         1467 :   gfc_clear_shape (shape2, i);
   12378         1467 :   return result;
   12379              : }
   12380              : 
   12381              : 
   12382              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12383              :    has the same shape as the outermost WHERE mask expression.  */
   12384              : 
   12385              : static void
   12386          509 : resolve_where (gfc_code *code, gfc_expr *mask)
   12387              : {
   12388          509 :   gfc_code *cblock;
   12389          509 :   gfc_code *cnext;
   12390          509 :   gfc_expr *e = NULL;
   12391              : 
   12392          509 :   cblock = code->block;
   12393              : 
   12394              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12395              :      In case of nested WHERE, only the outermost one is stored.  */
   12396          509 :   if (mask == NULL) /* outermost WHERE */
   12397          453 :     e = cblock->expr1;
   12398              :   else /* inner WHERE */
   12399          509 :     e = mask;
   12400              : 
   12401         1387 :   while (cblock)
   12402              :     {
   12403          878 :       if (cblock->expr1)
   12404              :         {
   12405              :           /* Check if the mask-expr has a consistent shape with the
   12406              :              outermost WHERE mask-expr.  */
   12407          714 :           if (!resolve_where_shape (cblock->expr1, e))
   12408            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12409            0 :                        &cblock->expr1->where);
   12410              :          }
   12411              : 
   12412              :       /* the assignment statement of a WHERE statement, or the first
   12413              :          statement in where-body-construct of a WHERE construct */
   12414          878 :       cnext = cblock->next;
   12415         1733 :       while (cnext)
   12416              :         {
   12417          855 :           switch (cnext->op)
   12418              :             {
   12419              :             /* WHERE assignment statement */
   12420          753 :             case EXEC_ASSIGN:
   12421              : 
   12422              :               /* Check shape consistent for WHERE assignment target.  */
   12423          753 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12424            0 :                gfc_error ("WHERE assignment target at %L has "
   12425            0 :                           "inconsistent shape", &cnext->expr1->where);
   12426              : 
   12427          753 :               if (cnext->op == EXEC_ASSIGN
   12428          753 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12429            0 :                 cnext->expr1->must_finalize = 1;
   12430              : 
   12431              :               break;
   12432              : 
   12433              : 
   12434           46 :             case EXEC_ASSIGN_CALL:
   12435           46 :               resolve_call (cnext);
   12436           46 :               if (!cnext->resolved_sym->attr.elemental)
   12437            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12438            2 :                           &cnext->ext.actual->expr->where);
   12439              :               break;
   12440              : 
   12441              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12442           56 :             case EXEC_WHERE:
   12443           56 :               resolve_where (cnext, e);
   12444           56 :               break;
   12445              : 
   12446            0 :             default:
   12447            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12448              :                          &cnext->loc);
   12449              :             }
   12450              :          /* the next statement within the same where-body-construct */
   12451          855 :          cnext = cnext->next;
   12452              :        }
   12453              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12454          878 :     cblock = cblock->block;
   12455              :   }
   12456          509 : }
   12457              : 
   12458              : 
   12459              : /* Resolve assignment in FORALL construct.
   12460              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12461              :    FORALL index variables.  */
   12462              : 
   12463              : static void
   12464         2376 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12465              : {
   12466         2376 :   int n;
   12467         2376 :   gfc_symbol *forall_index;
   12468              : 
   12469         6774 :   for (n = 0; n < nvar; n++)
   12470              :     {
   12471         4398 :       forall_index = var_expr[n]->symtree->n.sym;
   12472              : 
   12473              :       /* Check whether the assignment target is one of the FORALL index
   12474              :          variable.  */
   12475         4398 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12476         4398 :           && (code->expr1->symtree->n.sym == forall_index))
   12477            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12478              :                    &code->expr1->where);
   12479              :       else
   12480              :         {
   12481              :           /* If one of the FORALL index variables doesn't appear in the
   12482              :              assignment variable, then there could be a many-to-one
   12483              :              assignment.  Emit a warning rather than an error because the
   12484              :              mask could be resolving this problem.
   12485              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12486              :              many-to-one assignments are semantically valid (formalized with
   12487              :              the REDUCE locality-spec in Fortran 2023).  */
   12488         4398 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12489         4398 :               && !gfc_do_concurrent_flag)
   12490            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12491              :                          "left side of the assignment at %L and so might "
   12492              :                          "cause multiple assignment to this object",
   12493            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12494              :         }
   12495              :     }
   12496         2376 : }
   12497              : 
   12498              : 
   12499              : /* Resolve WHERE statement in FORALL construct.  */
   12500              : 
   12501              : static void
   12502           47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12503              :                                   gfc_expr **var_expr)
   12504              : {
   12505           47 :   gfc_code *cblock;
   12506           47 :   gfc_code *cnext;
   12507              : 
   12508           47 :   cblock = code->block;
   12509          113 :   while (cblock)
   12510              :     {
   12511              :       /* the assignment statement of a WHERE statement, or the first
   12512              :          statement in where-body-construct of a WHERE construct */
   12513           66 :       cnext = cblock->next;
   12514          132 :       while (cnext)
   12515              :         {
   12516           66 :           switch (cnext->op)
   12517              :             {
   12518              :             /* WHERE assignment statement */
   12519           66 :             case EXEC_ASSIGN:
   12520           66 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12521              : 
   12522           66 :               if (cnext->op == EXEC_ASSIGN
   12523           66 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12524            0 :                 cnext->expr1->must_finalize = 1;
   12525              : 
   12526              :               break;
   12527              : 
   12528              :             /* WHERE operator assignment statement */
   12529            0 :             case EXEC_ASSIGN_CALL:
   12530            0 :               resolve_call (cnext);
   12531            0 :               if (!cnext->resolved_sym->attr.elemental)
   12532            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12533            0 :                           &cnext->ext.actual->expr->where);
   12534              :               break;
   12535              : 
   12536              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12537            0 :             case EXEC_WHERE:
   12538            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12539            0 :               break;
   12540              : 
   12541            0 :             default:
   12542            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12543              :                          &cnext->loc);
   12544              :             }
   12545              :           /* the next statement within the same where-body-construct */
   12546           66 :           cnext = cnext->next;
   12547              :         }
   12548              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12549           66 :       cblock = cblock->block;
   12550              :     }
   12551           47 : }
   12552              : 
   12553              : 
   12554              : /* Traverse the FORALL body to check whether the following errors exist:
   12555              :    1. For assignment, check if a many-to-one assignment happens.
   12556              :    2. For WHERE statement, check the WHERE body to see if there is any
   12557              :       many-to-one assignment.  */
   12558              : 
   12559              : static void
   12560         2217 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12561              : {
   12562         2217 :   gfc_code *c;
   12563              : 
   12564         2217 :   c = code->block->next;
   12565         4856 :   while (c)
   12566              :     {
   12567         2639 :       switch (c->op)
   12568              :         {
   12569         2310 :         case EXEC_ASSIGN:
   12570         2310 :         case EXEC_POINTER_ASSIGN:
   12571         2310 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12572              : 
   12573         2310 :           if (c->op == EXEC_ASSIGN
   12574         2310 :               && gfc_may_be_finalized (c->expr1->ts))
   12575            0 :             c->expr1->must_finalize = 1;
   12576              : 
   12577              :           break;
   12578              : 
   12579            0 :         case EXEC_ASSIGN_CALL:
   12580            0 :           resolve_call (c);
   12581            0 :           break;
   12582              : 
   12583              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12584              :            there is no need to handle it here.  */
   12585              :         case EXEC_FORALL:
   12586              :           break;
   12587           47 :         case EXEC_WHERE:
   12588           47 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12589           47 :           break;
   12590              :         default:
   12591              :           break;
   12592              :         }
   12593              :       /* The next statement in the FORALL body.  */
   12594         2639 :       c = c->next;
   12595              :     }
   12596         2217 : }
   12597              : 
   12598              : 
   12599              : /* Counts the number of iterators needed inside a forall construct, including
   12600              :    nested forall constructs. This is used to allocate the needed memory
   12601              :    in gfc_resolve_forall.  */
   12602              : 
   12603              : static int gfc_count_forall_iterators (gfc_code *code);
   12604              : 
   12605              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12606              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12607              : 
   12608              : static int
   12609         2415 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12610              : {
   12611         2415 :   int max_iters = 0;
   12612              : 
   12613         5281 :   for (gfc_code *c = code; c; c = c->next)
   12614              :     {
   12615         2866 :       int sub_iters = 0;
   12616              : 
   12617         2866 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12618           94 :         sub_iters = gfc_count_forall_iterators (c);
   12619         2772 :       else if (c->op == EXEC_BLOCK)
   12620              :         {
   12621              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12622              :              not in the generic c->block arm list used by IF/SELECT.  */
   12623           34 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12624           34 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12625              :         }
   12626         2738 :       else if (c->block)
   12627          307 :         for (gfc_code *b = c->block; b; b = b->block)
   12628              :           {
   12629          164 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12630          164 :             if (arm_iters > sub_iters)
   12631              :               sub_iters = arm_iters;
   12632              :           }
   12633              : 
   12634         2866 :       if (sub_iters > max_iters)
   12635              :         max_iters = sub_iters;
   12636              :     }
   12637              : 
   12638         2415 :   return max_iters;
   12639              : }
   12640              : 
   12641              : 
   12642              : static int
   12643         2217 : gfc_count_forall_iterators (gfc_code *code)
   12644              : {
   12645         2217 :   int current_iters = 0;
   12646         2217 :   gfc_forall_iterator *fa;
   12647              : 
   12648         2217 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12649              : 
   12650         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12651         4135 :     current_iters++;
   12652              : 
   12653         2217 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   12654              : }
   12655              : 
   12656              : 
   12657              : /* Given a FORALL construct.
   12658              :    1) Resolve the FORALL iterator.
   12659              :    2) Check for shadow index-name(s) and update code block.
   12660              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   12661              : 
   12662              : /* Custom recursive expression walker that replaces symbols.
   12663              :    Visits all expressions including array subscripts.  Also called from
   12664              :    replace_in_code_recursive to handle ASSOCIATE selector expressions.  */
   12665              : 
   12666              : static void
   12667          192 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
   12668              : {
   12669          228 :   if (!expr)
   12670              :     return;
   12671              : 
   12672              :   /* Check if this is a variable reference to replace */
   12673          144 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   12674              :     {
   12675           30 :       expr->symtree = new_st;
   12676           30 :       expr->ts = new_st->n.sym->ts;
   12677              :     }
   12678              : 
   12679              :   /* Walk through reference chain (array subscripts, substrings, etc.) */
   12680          150 :   for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
   12681              :     {
   12682            6 :       if (ref->type == REF_ARRAY)
   12683              :         {
   12684              :           gfc_array_ref *ar = &ref->u.ar;
   12685           12 :           for (int i = 0; i < ar->dimen; i++)
   12686              :             {
   12687            6 :               replace_in_expr_recursive (ar->start[i], old_sym, new_st);
   12688            6 :               replace_in_expr_recursive (ar->end[i], old_sym, new_st);
   12689            6 :               replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
   12690              :             }
   12691              :         }
   12692            0 :       else if (ref->type == REF_SUBSTRING)
   12693              :         {
   12694            0 :           replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
   12695            0 :           replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
   12696              :         }
   12697              :     }
   12698              : 
   12699              :   /* Walk through sub-expressions based on expression type */
   12700          144 :   switch (expr->expr_type)
   12701              :     {
   12702           36 :     case EXPR_OP:
   12703           36 :       replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
   12704           36 :       replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
   12705           36 :       break;
   12706              : 
   12707            6 :     case EXPR_FUNCTION:
   12708           18 :       for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
   12709           12 :         replace_in_expr_recursive (a->expr, old_sym, new_st);
   12710              :       break;
   12711              : 
   12712            0 :     case EXPR_ARRAY:
   12713            0 :     case EXPR_STRUCTURE:
   12714            0 :       for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   12715            0 :            c; c = gfc_constructor_next (c))
   12716              :         {
   12717            0 :           replace_in_expr_recursive (c->expr, old_sym, new_st);
   12718            0 :           if (c->iterator)
   12719              :             {
   12720            0 :               replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
   12721            0 :               replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
   12722            0 :               replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
   12723              :             }
   12724              :         }
   12725              :       break;
   12726              : 
   12727              :     default:
   12728              :       break;
   12729              :     }
   12730              : }
   12731              : 
   12732              : 
   12733              : /* Walk code tree and replace all variable references */
   12734              : 
   12735              : static void
   12736           30 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   12737              : {
   12738           30 :   if (!code)
   12739              :     return;
   12740              : 
   12741           60 :   for (gfc_code *c = code; c; c = c->next)
   12742              :     {
   12743              :       /* Replace in expressions associated with this code node */
   12744           30 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   12745           30 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   12746           30 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   12747           30 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   12748              : 
   12749              :       /* Handle special code types with additional expressions */
   12750           30 :       switch (c->op)
   12751              :         {
   12752            0 :         case EXEC_DO:
   12753            0 :           if (c->ext.iterator)
   12754              :             {
   12755            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   12756            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   12757            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   12758              :             }
   12759              :           break;
   12760              : 
   12761            0 :         case EXEC_CALL:
   12762            0 :         case EXEC_ASSIGN_CALL:
   12763            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   12764            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   12765              :           break;
   12766              : 
   12767            0 :         case EXEC_SELECT:
   12768            0 :           for (gfc_code *b = c->block; b; b = b->block)
   12769              :             {
   12770            0 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   12771              :                 {
   12772            0 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   12773            0 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   12774              :                 }
   12775            0 :               replace_in_code_recursive (b->next, old_sym, new_st);
   12776              :             }
   12777              :           break;
   12778              : 
   12779            0 :         case EXEC_FORALL:
   12780            0 :         case EXEC_DO_CONCURRENT:
   12781            0 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   12782              :             {
   12783            0 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   12784            0 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   12785            0 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   12786              :             }
   12787              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   12788              :              they'll be handled separately */
   12789              :           break;
   12790              : 
   12791            6 :         case EXEC_BLOCK:
   12792              :           /* Replace in ASSOCIATE selector expressions and the body.
   12793              :              The body of an EXEC_BLOCK lives in c->ext.block.ns->code, not
   12794              :              c->block->next, so without this case both selectors and body
   12795              :              are silently skipped, leaving shadow iterator references unreplaced
   12796              :              and producing wrong values at runtime.  */
   12797            6 :           for (gfc_association_list *alist = c->ext.block.assoc;
   12798           12 :                alist; alist = alist->next)
   12799            6 :             replace_in_expr_recursive (alist->target, old_sym, new_st);
   12800            6 :           if (c->ext.block.ns)
   12801            6 :             replace_in_code_recursive (c->ext.block.ns->code, old_sym, new_st);
   12802              :           break;
   12803              : 
   12804              :         default:
   12805              :           break;
   12806              :         }
   12807              : 
   12808              :       /* Recurse into blocks */
   12809           30 :       if (c->block)
   12810            0 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   12811              :     }
   12812              : }
   12813              : 
   12814              : 
   12815              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   12816              : 
   12817              : static void
   12818           24 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   12819              :                               gfc_symtree *shadow_st)
   12820              : {
   12821              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   12822            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   12823            0 : }
   12824              : 
   12825              : 
   12826              : static void
   12827         2217 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   12828              : {
   12829         2217 :   static gfc_expr **var_expr;
   12830         2217 :   static int total_var = 0;
   12831         2217 :   static int nvar = 0;
   12832         2217 :   int i, old_nvar, tmp;
   12833         2217 :   gfc_forall_iterator *fa;
   12834         2217 :   bool shadow = false;
   12835              : 
   12836         2217 :   old_nvar = nvar;
   12837              : 
   12838              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   12839         2217 :   if (code->op == EXEC_FORALL
   12840         2217 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   12841              :     return;
   12842              : 
   12843              :   /* Start to resolve a FORALL construct   */
   12844              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   12845              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   12846              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   12847              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   12848         2217 :   if (forall_save == 0 && nvar == 0)
   12849              :     {
   12850              :       /* Count the total number of FORALL indices in the nested FORALL
   12851              :          construct in order to allocate the VAR_EXPR with proper size.  */
   12852         2123 :       total_var = gfc_count_forall_iterators (code);
   12853              : 
   12854              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12855         2123 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12856              :     }
   12857              : 
   12858              :   /* The information about FORALL iterator, including FORALL indices start,
   12859              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   12860              :      stride.  Check for a shadow index-name.  */
   12861         6352 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12862              :     {
   12863              :       /* Fortran 2008: C738 (R753).  */
   12864         4135 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12865              :         {
   12866            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12867              :                      "of type integer", &fa->var->where);
   12868            2 :           continue;
   12869              :         }
   12870              : 
   12871              :       /* Check if any outer FORALL index name is the same as the current
   12872              :          one.  Skip this check if the iterator is a shadow variable (from
   12873              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   12874         7144 :       for (i = 0; i < nvar; i++)
   12875              :         {
   12876         3011 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   12877         3011 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12878            0 :             gfc_error ("An outer FORALL construct already has an index "
   12879              :                         "with this name %L", &fa->var->where);
   12880              :         }
   12881              : 
   12882         4133 :       if (fa->shadow)
   12883           24 :         shadow = true;
   12884              : 
   12885              :       /* Record the current FORALL index.  */
   12886         4133 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12887              : 
   12888         4133 :       nvar++;
   12889              : 
   12890              :       /* No memory leak.  */
   12891         4133 :       gcc_assert (nvar <= total_var);
   12892              :     }
   12893              : 
   12894              :   /* Need to walk the code and replace references to the index-name with
   12895              :      references to the shadow index-name. This must be done BEFORE resolving
   12896              :      the body so that resolution uses the correct shadow variables.  */
   12897         2217 :   if (shadow)
   12898              :     {
   12899              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   12900           54 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12901              :         {
   12902           30 :           if (fa->shadow)
   12903              :             {
   12904           24 :               gfc_symtree *shadow_st;
   12905           24 :               const char *shadow_name_str;
   12906           24 :               char *outer_name;
   12907              : 
   12908              :               /* fa->var now points to the shadow variable "_name".  */
   12909           24 :               shadow_name_str = fa->var->symtree->name;
   12910           24 :               shadow_st = fa->var->symtree;
   12911              : 
   12912           24 :               if (shadow_name_str[0] != '_')
   12913            0 :                 gfc_internal_error ("Expected shadow variable name to start with _");
   12914              : 
   12915           24 :               outer_name = (char *) alloca (strlen (shadow_name_str));
   12916           24 :               strcpy (outer_name, shadow_name_str + 1);
   12917              : 
   12918              :               /* Find the ITERATOR symbol in the current namespace.
   12919              :                  This is the local DO CONCURRENT variable that body expressions reference.  */
   12920           24 :               gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
   12921              : 
   12922           24 :               if (!iter_st)
   12923              :                 /* No iterator variable found - this shouldn't happen */
   12924            0 :                 continue;
   12925              : 
   12926           24 :               gfc_symbol *iter_sym = iter_st->n.sym;
   12927              : 
   12928              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   12929           24 :               if (code->block && code->block->next)
   12930           24 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   12931              :             }
   12932              :         }
   12933              :     }
   12934              : 
   12935              :   /* Resolve the FORALL body.  */
   12936         2217 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12937              : 
   12938              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12939         2217 :   gfc_resolve_blocks (code->block, ns);
   12940              : 
   12941         2217 :   tmp = nvar;
   12942         2217 :   nvar = old_nvar;
   12943              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12944         6350 :   for (i = nvar; i < tmp; i++)
   12945         4133 :      gfc_free_expr (var_expr[i]);
   12946              : 
   12947         2217 :   if (nvar == 0)
   12948              :     {
   12949              :       /* We are in the outermost FORALL construct.  */
   12950         2123 :       gcc_assert (forall_save == 0);
   12951              : 
   12952              :       /* VAR_EXPR is not needed any more.  */
   12953         2123 :       free (var_expr);
   12954         2123 :       total_var = 0;
   12955              :     }
   12956              : }
   12957              : 
   12958              : 
   12959              : /* Resolve a BLOCK construct statement.  */
   12960              : 
   12961              : static void
   12962         8428 : resolve_block_construct (gfc_code* code)
   12963              : {
   12964         8428 :   gfc_namespace *ns = code->ext.block.ns;
   12965              : 
   12966              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12967              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12968              :      namespace.  However, marking variables as used ans defined requires
   12969              :      passing ext.block.assoc.  */
   12970         8428 :   gfc_resolve (ns, code->ext.block.assoc);
   12971         8331 : }
   12972              : 
   12973              : /* Mark everything in an association list as used and set if applicable,
   12974              :    respectively.  */
   12975              : 
   12976              : static void
   12977       314722 : mark_assoc_used (gfc_association_list *a)
   12978              : {
   12979       321713 :   while (a != NULL)
   12980              :     {
   12981         6991 :       gfc_symbol *n_sym = a->st->n.sym;
   12982         6991 :       if (n_sym->attr.value_used != VALUE_UNUSED)
   12983         4943 :         gfc_value_used_expr (a->target, n_sym->attr.value_used);
   12984              : 
   12985         6991 :       if (a->variable && n_sym->attr.value_set != VALUE_UNSET)
   12986         1366 :         gfc_expr_set_at (a->target, &n_sym->other_loc, n_sym->attr.value_set);
   12987              : 
   12988         6991 :       a = a->next;
   12989              :     }
   12990       314722 : }
   12991              : 
   12992              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12993              :    DO code nodes.  */
   12994              : 
   12995              : void
   12996       335702 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12997              : {
   12998       335702 :   bool t;
   12999              : 
   13000       682986 :   for (; b; b = b->block)
   13001              :     {
   13002       347284 :       t = gfc_resolve_expr (b->expr1);
   13003       347284 :       if (!gfc_resolve_expr (b->expr2))
   13004            0 :         t = false;
   13005              : 
   13006       347284 :       switch (b->op)
   13007              :         {
   13008       239585 :         case EXEC_IF:
   13009       239585 :           if (t && b->expr1 != NULL
   13010       235259 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   13011            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   13012              :                        &b->expr1->where);
   13013              :           break;
   13014              : 
   13015          764 :         case EXEC_WHERE:
   13016          764 :           if (t
   13017          764 :               && b->expr1 != NULL
   13018          631 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   13019            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   13020              :                        &b->expr1->where);
   13021              :           break;
   13022              : 
   13023           76 :         case EXEC_GOTO:
   13024           76 :           resolve_branch (b->label1, b);
   13025           76 :           break;
   13026              : 
   13027            0 :         case EXEC_BLOCK:
   13028            0 :           resolve_block_construct (b);
   13029            0 :           break;
   13030              : 
   13031              :         case EXEC_SELECT:
   13032              :         case EXEC_SELECT_TYPE:
   13033              :         case EXEC_SELECT_RANK:
   13034              :         case EXEC_FORALL:
   13035              :         case EXEC_DO:
   13036              :         case EXEC_DO_WHILE:
   13037              :         case EXEC_DO_CONCURRENT:
   13038              :         case EXEC_CRITICAL:
   13039              :         case EXEC_READ:
   13040              :         case EXEC_WRITE:
   13041              :         case EXEC_IOLENGTH:
   13042              :         case EXEC_WAIT:
   13043              :           break;
   13044              : 
   13045         2697 :         case EXEC_OMP_ATOMIC:
   13046         2697 :         case EXEC_OACC_ATOMIC:
   13047         2697 :           {
   13048              :             /* Verify this before calling gfc_resolve_code, which might
   13049              :                change it.  */
   13050         2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   13051              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   13052              :           }
   13053              :           break;
   13054              : 
   13055              :         case EXEC_OACC_PARALLEL_LOOP:
   13056              :         case EXEC_OACC_PARALLEL:
   13057              :         case EXEC_OACC_KERNELS_LOOP:
   13058              :         case EXEC_OACC_KERNELS:
   13059              :         case EXEC_OACC_SERIAL_LOOP:
   13060              :         case EXEC_OACC_SERIAL:
   13061              :         case EXEC_OACC_DATA:
   13062              :         case EXEC_OACC_HOST_DATA:
   13063              :         case EXEC_OACC_LOOP:
   13064              :         case EXEC_OACC_UPDATE:
   13065              :         case EXEC_OACC_WAIT:
   13066              :         case EXEC_OACC_CACHE:
   13067              :         case EXEC_OACC_ENTER_DATA:
   13068              :         case EXEC_OACC_EXIT_DATA:
   13069              :         case EXEC_OACC_ROUTINE:
   13070              :         case EXEC_OACC_INIT:
   13071              :         case EXEC_OACC_SHUTDOWN:
   13072              :         case EXEC_OACC_SET:
   13073              :         case EXEC_OMP_ALLOCATE:
   13074              :         case EXEC_OMP_ALLOCATORS:
   13075              :         case EXEC_OMP_ASSUME:
   13076              :         case EXEC_OMP_CRITICAL:
   13077              :         case EXEC_OMP_DISPATCH:
   13078              :         case EXEC_OMP_DISTRIBUTE:
   13079              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   13080              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   13081              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13082              :         case EXEC_OMP_DO:
   13083              :         case EXEC_OMP_DO_SIMD:
   13084              :         case EXEC_OMP_ERROR:
   13085              :         case EXEC_OMP_LOOP:
   13086              :         case EXEC_OMP_MASKED:
   13087              :         case EXEC_OMP_MASKED_TASKLOOP:
   13088              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13089              :         case EXEC_OMP_MASTER:
   13090              :         case EXEC_OMP_MASTER_TASKLOOP:
   13091              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13092              :         case EXEC_OMP_ORDERED:
   13093              :         case EXEC_OMP_PARALLEL:
   13094              :         case EXEC_OMP_PARALLEL_DO:
   13095              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13096              :         case EXEC_OMP_PARALLEL_LOOP:
   13097              :         case EXEC_OMP_PARALLEL_MASKED:
   13098              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13099              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13100              :         case EXEC_OMP_PARALLEL_MASTER:
   13101              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13102              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13103              :         case EXEC_OMP_PARALLEL_SECTIONS:
   13104              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13105              :         case EXEC_OMP_SECTIONS:
   13106              :         case EXEC_OMP_SIMD:
   13107              :         case EXEC_OMP_SCOPE:
   13108              :         case EXEC_OMP_SINGLE:
   13109              :         case EXEC_OMP_TARGET:
   13110              :         case EXEC_OMP_TARGET_DATA:
   13111              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13112              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13113              :         case EXEC_OMP_TARGET_PARALLEL:
   13114              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13115              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13116              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13117              :         case EXEC_OMP_TARGET_SIMD:
   13118              :         case EXEC_OMP_TARGET_TEAMS:
   13119              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13120              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13121              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13122              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13123              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13124              :         case EXEC_OMP_TARGET_UPDATE:
   13125              :         case EXEC_OMP_TASK:
   13126              :         case EXEC_OMP_TASKGROUP:
   13127              :         case EXEC_OMP_TASKLOOP:
   13128              :         case EXEC_OMP_TASKLOOP_SIMD:
   13129              :         case EXEC_OMP_TASKWAIT:
   13130              :         case EXEC_OMP_TASKYIELD:
   13131              :         case EXEC_OMP_TEAMS:
   13132              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13133              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13134              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13135              :         case EXEC_OMP_TEAMS_LOOP:
   13136              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13137              :         case EXEC_OMP_TILE:
   13138              :         case EXEC_OMP_UNROLL:
   13139              :         case EXEC_OMP_WORKSHARE:
   13140              :           break;
   13141              : 
   13142            0 :         default:
   13143            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13144              :         }
   13145       347284 :       gfc_value_used_expr (b->expr1, VALUE_USED);
   13146       347284 :       gfc_value_used_expr (b->expr2, VALUE_USED);
   13147       347284 :       gfc_resolve_code (b->next, ns);
   13148              :     }
   13149       335702 : }
   13150              : 
   13151              : bool
   13152            0 : caf_possible_reallocate (gfc_expr *e)
   13153              : {
   13154            0 :   symbol_attribute caf_attr;
   13155            0 :   gfc_ref *last_arr_ref = nullptr;
   13156              : 
   13157            0 :   caf_attr = gfc_caf_attr (e);
   13158            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13159              :     return false;
   13160              : 
   13161              :   /* Only full array refs can indicate a needed reallocation.  */
   13162            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13163            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13164            0 :       last_arr_ref = ref;
   13165              : 
   13166            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13167              : }
   13168              : 
   13169              : /* Does everything to resolve an ordinary assignment.  Returns true
   13170              :    if this is an interface assignment.  */
   13171              : static bool
   13172       288203 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13173              : {
   13174       288203 :   bool rval = false;
   13175       288203 :   gfc_expr *lhs;
   13176       288203 :   gfc_expr *rhs;
   13177       288203 :   int n;
   13178       288203 :   gfc_ref *ref;
   13179       288203 :   symbol_attribute attr;
   13180              : 
   13181       288203 :   if (gfc_extend_assign (code, ns))
   13182              :     {
   13183          924 :       gfc_expr** rhsptr;
   13184              : 
   13185          924 :       if (code->op == EXEC_ASSIGN_CALL)
   13186              :         {
   13187          469 :           lhs = code->ext.actual->expr;
   13188          469 :           rhsptr = &code->ext.actual->next->expr;
   13189              :         }
   13190              :       else
   13191              :         {
   13192          455 :           gfc_actual_arglist* args;
   13193          455 :           gfc_typebound_proc* tbp;
   13194              : 
   13195          455 :           gcc_assert (code->op == EXEC_COMPCALL);
   13196              : 
   13197          455 :           args = code->expr1->value.compcall.actual;
   13198          455 :           lhs = args->expr;
   13199          455 :           rhsptr = &args->next->expr;
   13200              : 
   13201          455 :           tbp = code->expr1->value.compcall.tbp;
   13202          455 :           gcc_assert (!tbp->is_generic);
   13203              :         }
   13204              : 
   13205              :       /* Make a temporary rhs when there is a default initializer
   13206              :          and rhs is the same symbol as the lhs.  */
   13207          924 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13208          513 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13209          442 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13210         1218 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13211           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13212              : 
   13213              :       return true;
   13214              :     }
   13215              : 
   13216       287279 :   lhs = code->expr1;
   13217       287279 :   rhs = code->expr2;
   13218              : 
   13219       287279 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13220       266847 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13221        23255 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13222       310534 :       && gfc_expr_attr (lhs).proc_pointer)
   13223              :     {
   13224            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13225              :                  "pointer component",
   13226              :                  &lhs->where);
   13227            1 :       return false;
   13228              :     }
   13229              : 
   13230       338730 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13231       251332 :       && rhs->ts.type == BT_CHARACTER
   13232       287671 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13233              :     {
   13234              :       /* Use of -fdec-char-conversions allows assignment of character data
   13235              :          to non-character variables.  This not permitted for nonconstant
   13236              :          strings.  */
   13237           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13238              :                  gfc_typename (lhs), &rhs->where);
   13239           29 :       return false;
   13240              :     }
   13241              : 
   13242       287249 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13243              :     {
   13244            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13245              :                    gfc_typename (lhs), &rhs->where);
   13246            0 :       return false;
   13247              :     }
   13248              : 
   13249              :   /* Handle the case of a BOZ literal on the RHS.  */
   13250       287249 :   if (rhs->ts.type == BT_BOZ)
   13251              :     {
   13252            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13253              :                            "statement value nor an actual argument of "
   13254              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13255              :                            &rhs->where))
   13256              :         return false;
   13257              : 
   13258            1 :       switch (lhs->ts.type)
   13259              :         {
   13260            0 :         case BT_INTEGER:
   13261            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13262              :             return false;
   13263              :           break;
   13264            1 :         case BT_REAL:
   13265            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13266              :             return false;
   13267              :           break;
   13268            0 :         default:
   13269            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13270            0 :           return false;
   13271              :         }
   13272              :     }
   13273              : 
   13274       287247 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13275              :     {
   13276           67 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13277           67 :       if (lhs->ts.u.cl != NULL
   13278           67 :             && lhs->ts.u.cl->length != NULL
   13279           56 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13280           56 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13281              : 
   13282           67 :       if (rhs->expr_type == EXPR_CONSTANT)
   13283           29 :         rlen = rhs->value.character.length;
   13284              : 
   13285           38 :       else if (rhs->ts.u.cl != NULL
   13286           38 :                  && rhs->ts.u.cl->length != NULL
   13287           35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13288           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13289              : 
   13290           67 :       if (rlen && llen && rlen > llen)
   13291           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13292              :                          "CHARACTER expression will be truncated "
   13293              :                          "in assignment (%wd/%wd) at %L",
   13294              :                          llen, rlen, &code->loc);
   13295              :     }
   13296              : 
   13297              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13298              :      to a temporary if the lvalue symbol is referenced in it.  */
   13299       287247 :   if (lhs->rank)
   13300              :     {
   13301       113955 :       for (ref = lhs->ref; ref; ref= ref->next)
   13302        60937 :         if (ref->type == REF_ARRAY)
   13303              :           {
   13304       134038 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13305        79206 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13306        79436 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13307          230 :                                            ref->u.ar.start[n]))
   13308           14 :                 ref->u.ar.start[n]
   13309           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13310              :           }
   13311              :     }
   13312              : 
   13313       287247 :   if (gfc_pure (NULL))
   13314              :     {
   13315         3549 :       if (lhs->ts.type == BT_DERIVED
   13316          136 :             && lhs->expr_type == EXPR_VARIABLE
   13317          136 :             && lhs->ts.u.derived->attr.pointer_comp
   13318            4 :             && rhs->expr_type == EXPR_VARIABLE
   13319         3552 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13320            2 :                 || gfc_is_coindexed (rhs)))
   13321              :         {
   13322              :           /* F2008, C1283.  */
   13323            2 :           if (gfc_is_coindexed (rhs))
   13324            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13325              :                         "a derived type variable with a POINTER "
   13326              :                         "component in a PURE procedure",
   13327              :                         &rhs->where);
   13328              :           else
   13329              :           /* F2008, C1283 (4).  */
   13330            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13331              :                         "shall not be used as the expr at %L of an intrinsic "
   13332              :                         "assignment statement in which the variable is of a "
   13333              :                         "derived type if the derived type has a pointer "
   13334              :                         "component at any level of component selection.",
   13335              :                         &rhs->where);
   13336              :           return rval;
   13337              :         }
   13338              : 
   13339              :       /* Fortran 2008, C1283.  */
   13340         3547 :       if (gfc_is_coindexed (lhs))
   13341              :         {
   13342            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13343              :                      "procedure", &rhs->where);
   13344            1 :           return rval;
   13345              :         }
   13346              :     }
   13347              : 
   13348       287244 :   if (gfc_implicit_pure (NULL))
   13349              :     {
   13350         7407 :       if (lhs->expr_type == EXPR_VARIABLE
   13351         7407 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13352         5292 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13353          256 :         gfc_unset_implicit_pure (NULL);
   13354              : 
   13355         7407 :       if (lhs->ts.type == BT_DERIVED
   13356          365 :             && lhs->expr_type == EXPR_VARIABLE
   13357          365 :             && lhs->ts.u.derived->attr.pointer_comp
   13358            7 :             && rhs->expr_type == EXPR_VARIABLE
   13359         7414 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13360            7 :                 || gfc_is_coindexed (rhs)))
   13361            0 :         gfc_unset_implicit_pure (NULL);
   13362              : 
   13363              :       /* Fortran 2008, C1283.  */
   13364         7407 :       if (gfc_is_coindexed (lhs))
   13365            0 :         gfc_unset_implicit_pure (NULL);
   13366              :     }
   13367              : 
   13368              :   /* F2008, 7.2.1.2.  */
   13369       287244 :   attr = gfc_expr_attr (lhs);
   13370       287244 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13371              :     {
   13372         1036 :       if (attr.codimension)
   13373              :         {
   13374            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13375              :                      "permitted", &lhs->where);
   13376            1 :           return false;
   13377              :         }
   13378         1035 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13379              :                            "polymorphic variable at %L", &lhs->where))
   13380              :         return false;
   13381         1034 :       if (!flag_realloc_lhs)
   13382              :         {
   13383            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13384              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13385            1 :           return false;
   13386              :         }
   13387              :     }
   13388       286208 :   else if (lhs->ts.type == BT_CLASS)
   13389              :     {
   13390            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13391              :                  "assignment at %L - check that there is a matching specific "
   13392              :                  "subroutine for %<=%> operator", &lhs->where);
   13393            9 :       return false;
   13394              :     }
   13395              : 
   13396       287232 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13397              : 
   13398              :   /* F2008, Section 7.2.1.2.  */
   13399       287232 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13400              :     {
   13401            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13402              :                  "component in assignment at %L", &lhs->where);
   13403            1 :       return false;
   13404              :     }
   13405              : 
   13406              :   /* Assign the 'data' of a class object to a derived type.  */
   13407       287231 :   if (lhs->ts.type == BT_DERIVED
   13408         7391 :       && rhs->ts.type == BT_CLASS
   13409          180 :       && (rhs->expr_type != EXPR_ARRAY
   13410          174 :           && rhs->expr_type != EXPR_OP))
   13411          168 :     gfc_add_data_component (rhs);
   13412              : 
   13413              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13414              :      rhs type.  */
   13415       287231 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13416          622 :     gfc_find_vtab (&rhs->ts);
   13417              : 
   13418       287231 :   gfc_check_assign (lhs, rhs, 1);
   13419              : 
   13420       287231 :   return false;
   13421              : }
   13422              : 
   13423              : 
   13424              : /* Add a component reference onto an expression.  */
   13425              : 
   13426              : static void
   13427          647 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13428              : {
   13429          647 :   gfc_ref **ref;
   13430          647 :   ref = &(e->ref);
   13431          871 :   while (*ref)
   13432          224 :     ref = &((*ref)->next);
   13433          647 :   *ref = gfc_get_ref ();
   13434          647 :   (*ref)->type = REF_COMPONENT;
   13435          647 :   (*ref)->u.c.sym = e->ts.u.derived;
   13436          647 :   (*ref)->u.c.component = c;
   13437          647 :   e->ts = c->ts;
   13438              : 
   13439              :   /* Add a full array ref, as necessary.  */
   13440          647 :   if (c->as)
   13441              :     {
   13442           84 :       gfc_add_full_array_ref (e, c->as);
   13443           84 :       e->rank = c->as->rank;
   13444           84 :       e->corank = c->as->corank;
   13445              :     }
   13446          647 : }
   13447              : 
   13448              : 
   13449              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13450              :    pointer assignments can be made.  */
   13451              : 
   13452              : static gfc_code *
   13453          976 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13454              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13455              : {
   13456          976 :   gfc_code *this_code;
   13457              : 
   13458          976 :   this_code = gfc_get_code (op);
   13459          976 :   this_code->next = NULL;
   13460          976 :   this_code->expr1 = gfc_copy_expr (expr1);
   13461          976 :   this_code->expr2 = gfc_copy_expr (expr2);
   13462          976 :   this_code->loc = loc;
   13463          976 :   if (comp1 && comp2)
   13464              :     {
   13465          288 :       add_comp_ref (this_code->expr1, comp1);
   13466          288 :       add_comp_ref (this_code->expr2, comp2);
   13467              :     }
   13468              : 
   13469          976 :   return this_code;
   13470              : }
   13471              : 
   13472              : 
   13473              : /* Makes a temporary variable expression based on the characteristics of
   13474              :    a given variable expression.  If allocatable is set, the temporary is
   13475              :    unconditionally allocatable*/
   13476              : 
   13477              : static gfc_expr*
   13478          464 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13479              :                     bool allocatable = false)
   13480              : {
   13481          464 :   static int serial = 0;
   13482          464 :   char name[GFC_MAX_SYMBOL_LEN];
   13483          464 :   gfc_symtree *tmp;
   13484          464 :   gfc_array_spec *as;
   13485          464 :   gfc_array_ref *aref;
   13486          464 :   gfc_ref *ref;
   13487              : 
   13488          464 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13489          464 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13490          464 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13491              : 
   13492          464 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13493            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13494              :                                                     NULL,
   13495            0 :                                                     e->value.character.length);
   13496              : 
   13497          464 :   as = NULL;
   13498          464 :   ref = NULL;
   13499          464 :   aref = NULL;
   13500              : 
   13501              :   /* Obtain the arrayspec for the temporary.  */
   13502          464 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13503              :        && e->expr_type != EXPR_FUNCTION
   13504              :        && e->expr_type != EXPR_OP)
   13505              :     {
   13506           52 :       aref = gfc_find_array_ref (e);
   13507           52 :       if (e->expr_type == EXPR_VARIABLE
   13508           52 :           && e->symtree->n.sym->as == aref->as)
   13509              :         as = aref->as;
   13510              :       else
   13511              :         {
   13512            0 :           for (ref = e->ref; ref; ref = ref->next)
   13513            0 :             if (ref->type == REF_COMPONENT
   13514            0 :                 && ref->u.c.component->as == aref->as)
   13515              :               {
   13516              :                 as = aref->as;
   13517              :                 break;
   13518              :               }
   13519              :         }
   13520              :     }
   13521              : 
   13522              :   /* Add the attributes and the arrayspec to the temporary.  */
   13523          464 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13524          464 :   tmp->n.sym->attr.function = 0;
   13525          464 :   tmp->n.sym->attr.proc_pointer = 0;
   13526          464 :   tmp->n.sym->attr.result = 0;
   13527          464 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13528          464 :   tmp->n.sym->attr.dummy = 0;
   13529          464 :   tmp->n.sym->attr.use_assoc = 0;
   13530          464 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13531              : 
   13532              : 
   13533          464 :   if (as && !allocatable)
   13534              :     {
   13535           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13536           52 :       if (!ref)
   13537           52 :         ref = e->ref;
   13538           52 :       if (as->type == AS_DEFERRED)
   13539           46 :         tmp->n.sym->attr.allocatable = 1;
   13540              :     }
   13541          412 :   else if ((e->rank || e->corank)
   13542          130 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13543           24 :                || e->expr_type == EXPR_OP || allocatable))
   13544              :     {
   13545          130 :       tmp->n.sym->as = gfc_get_array_spec ();
   13546          130 :       tmp->n.sym->as->type = AS_DEFERRED;
   13547          130 :       tmp->n.sym->as->rank = e->rank;
   13548          130 :       tmp->n.sym->as->corank = e->corank;
   13549          130 :       tmp->n.sym->attr.allocatable = 1;
   13550          130 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13551          260 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13552              :     }
   13553              :   else
   13554          282 :     tmp->n.sym->attr.dimension = 0;
   13555              : 
   13556          464 :   gfc_set_sym_referenced (tmp->n.sym);
   13557          464 :   gfc_commit_symbol (tmp->n.sym);
   13558          464 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13559              : 
   13560              :   /* Should the lhs be a section, use its array ref for the
   13561              :      temporary expression.  */
   13562          464 :   if (aref && aref->type != AR_FULL && !allocatable)
   13563              :     {
   13564            6 :       gfc_free_ref_list (e->ref);
   13565            6 :       e->ref = gfc_copy_ref (ref);
   13566              :     }
   13567          464 :   return e;
   13568              : }
   13569              : 
   13570              : 
   13571              : /* Helper function to take an argument in a subroutine call with a dependency
   13572              :    on another argument, copy it to an allocatable temporary and use the
   13573              :    temporary in the call expression. The new code is embedded in a block to
   13574              :    ensure local, automatic deallocation.  */
   13575              : 
   13576              : static void
   13577           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13578              :                              gfc_expr **rhsptr)
   13579              : {
   13580           36 :   gfc_namespace *block_ns;
   13581           36 :   gfc_expr *tmp_var;
   13582              : 
   13583              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13584           36 :   block_ns = gfc_build_block_ns (ns);
   13585              : 
   13586              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13587              :      the assignment would be converted to a call and, in any case, the modified
   13588              :      call fails in gfc_check_conformance.  */
   13589           36 :   block_ns->resolved = 1;
   13590              : 
   13591              :   /* Assign the original expression to the temporary.  */
   13592           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13593           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13594           36 :                                      NULL, NULL, (*rhsptr)->where);
   13595              : 
   13596              :   /* Transfer the call to the block and terminate block code.  */
   13597           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13598           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13599           36 :   *(block_ns->code->next) = *code;
   13600           36 :   block_ns->code->next->next = NULL;
   13601              : 
   13602              :   /* Convert the original code to execute the block.  */
   13603           36 :   code->op = EXEC_BLOCK;
   13604           36 :   code->ext.block.ns = block_ns;
   13605           36 :   code->ext.block.assoc = NULL;
   13606           36 :   code->expr1 = code->expr2 = NULL;
   13607           36 : }
   13608              : 
   13609              : 
   13610              : /* Add one line of code to the code chain, making sure that 'head' and
   13611              :    'tail' are appropriately updated.  */
   13612              : 
   13613              : static void
   13614          650 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13615              : {
   13616          650 :   gcc_assert (this_code);
   13617          650 :   if (*head == NULL)
   13618          302 :     *head = *tail = *this_code;
   13619              :   else
   13620          348 :     *tail = gfc_append_code (*tail, *this_code);
   13621          650 :   *this_code = NULL;
   13622          650 : }
   13623              : 
   13624              : 
   13625              : /* Generate a final call from a variable expression  */
   13626              : 
   13627              : static void
   13628           81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13629              : {
   13630           81 :   gfc_code *this_code;
   13631           81 :   gfc_expr *final_expr = NULL;
   13632           81 :   gfc_expr *size_expr;
   13633           81 :   gfc_expr *fini_coarray;
   13634              : 
   13635           81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13636           81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13637           75 :     return;
   13638              : 
   13639              :   /* Now generate the finalizer call.  */
   13640            6 :   this_code = gfc_get_code (EXEC_CALL);
   13641            6 :   this_code->symtree = final_expr->symtree;
   13642            6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13643              : 
   13644              :   //* Expression to be finalized  */
   13645            6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13646            6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13647              : 
   13648              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13649            6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13650            6 :   size_expr = gfc_get_expr ();
   13651            6 :   size_expr->where = gfc_current_locus;
   13652            6 :   size_expr->expr_type = EXPR_OP;
   13653            6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13654            6 :   size_expr->value.op.op1
   13655           12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13656              :                                     "storage_size", gfc_current_locus, 2,
   13657            6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13658              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13659              :                                                       NULL, 0));
   13660            6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   13661              :                                               gfc_character_storage_size);
   13662            6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   13663            6 :   size_expr->ts = size_expr->value.op.op1->ts;
   13664            6 :   this_code->ext.actual->next->expr = size_expr;
   13665              : 
   13666              :   /* fini_coarray  */
   13667            6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   13668            6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   13669              :                                         &tmp_expr->where);
   13670            6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   13671            6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   13672              : 
   13673            6 :   add_code_to_chain (&this_code, head, tail);
   13674              : 
   13675              : }
   13676              : 
   13677              : /* Counts the potential number of part array references that would
   13678              :    result from resolution of typebound defined assignments.  */
   13679              : 
   13680              : 
   13681              : static int
   13682          249 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   13683              : {
   13684          249 :   gfc_component *c;
   13685          249 :   int c_depth = 0, t_depth;
   13686              : 
   13687          596 :   for (c= derived->components; c; c = c->next)
   13688              :     {
   13689          347 :       if ((!gfc_bt_struct (c->ts.type)
   13690          267 :             || c->attr.pointer
   13691          267 :             || c->attr.allocatable
   13692          266 :             || c->attr.proc_pointer_comp
   13693          266 :             || c->attr.class_pointer
   13694          266 :             || c->attr.proc_pointer)
   13695           81 :           && !c->attr.defined_assign_comp)
   13696           81 :         continue;
   13697              : 
   13698          266 :       if (c->as && c_depth == 0)
   13699          266 :         c_depth = 1;
   13700              : 
   13701          266 :       if (c->ts.u.derived->attr.defined_assign_comp)
   13702          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   13703              :                                               c->as ? 1 : 0);
   13704              :       else
   13705              :         t_depth = 0;
   13706              : 
   13707          266 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   13708              :     }
   13709          249 :   return depth + c_depth;
   13710              : }
   13711              : 
   13712              : 
   13713              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   13714              :    "An intrinsic assignment where the variable is of derived type is performed
   13715              :     as if each component of the variable were assigned from the corresponding
   13716              :     component of expr using pointer assignment (10.2.2) for each pointer
   13717              :     component, defined assignment for each nonpointer nonallocatable component
   13718              :     of a type that has a type-bound defined assignment consistent with the
   13719              :     component, intrinsic assignment for each other nonpointer nonallocatable
   13720              :     component, and intrinsic assignment for each allocated coarray component.
   13721              :     For unallocated coarray components, the corresponding component of the
   13722              :     variable shall be unallocated. For a noncoarray allocatable component the
   13723              :     following sequence of operations is applied.
   13724              :         (1) If the component of the variable is allocated, it is deallocated.
   13725              :         (2) If the component of the value of expr is allocated, the
   13726              :             corresponding component of the variable is allocated with the same
   13727              :             dynamic type and type parameters as the component of the value of
   13728              :             expr. If it is an array, it is allocated with the same bounds. The
   13729              :             value of the component of the value of expr is then assigned to the
   13730              :             corresponding component of the variable using defined assignment if
   13731              :             the declared type of the component has a type-bound defined
   13732              :             assignment consistent with the component, and intrinsic assignment
   13733              :             for the dynamic type of that component otherwise."
   13734              : 
   13735              :    The pointer assignments are taken care of by the intrinsic assignment of the
   13736              :    structure itself.  This function recursively adds defined assignments where
   13737              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   13738              : 
   13739              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   13740              :    and the component of 'var' is finalizable, we need a temporary for the
   13741              :    lhs.  In pseudo-code for an assignment var = expr:
   13742              : 
   13743              :    ! Confine finalization of temporaries, as far as possible.
   13744              :      Enclose the code for the assignment in a block
   13745              :    ! Only call function 'expr' once.
   13746              :       #if ('expr is not a constant or an variable)
   13747              :         temp_expr = expr
   13748              :         expr = temp_x
   13749              :    ! Do the intrinsic assignment
   13750              :       #if typeof ('var') has a typebound final subroutine
   13751              :         finalize (var)
   13752              :       var = expr
   13753              :    ! Now do the component assignments
   13754              :       #do over derived type components [%cmp]
   13755              :         #if (cmp is a pointer of any kind)
   13756              :           continue
   13757              :         build the assignment
   13758              :         resolve the code
   13759              :         #if the code is a typebound assignment
   13760              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   13761              :              t1 = var
   13762              :              arg1 = t1
   13763              :              deal with allocatation or not of var and this component
   13764              :         #elseif the code is an assignment by itself
   13765              :            #if this component does not need finalization
   13766              :              delete code and continue
   13767              :         #else
   13768              :            remove the leading assignment
   13769              :         #endif
   13770              :         commit the code
   13771              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   13772              :            var%cmp = t1%cmp
   13773              :       #enddo
   13774              :       put all code chunks involving t1 to the top of the generated code
   13775              :       insert the generated block in place of the original code
   13776              : */
   13777              : 
   13778              : static bool
   13779          393 : is_finalizable_type (gfc_typespec ts)
   13780              : {
   13781          393 :   gfc_component *c;
   13782              : 
   13783          393 :   if (ts.type != BT_DERIVED)
   13784              :     return false;
   13785              : 
   13786              :   /* (1) Check for FINAL subroutines.  */
   13787          393 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   13788              :     return true;
   13789              : 
   13790              :   /* (2) Check for components of finalizable type.  */
   13791          815 :   for (c = ts.u.derived->components; c; c = c->next)
   13792          476 :     if (c->ts.type == BT_DERIVED
   13793          249 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   13794          248 :         && c->ts.u.derived->f2k_derived
   13795          248 :         && c->ts.u.derived->f2k_derived->finalizers)
   13796              :       return true;
   13797              : 
   13798              :   return false;
   13799              : }
   13800              : 
   13801              : /* The temporary assignments have to be put on top of the additional
   13802              :    code to avoid the result being changed by the intrinsic assignment.
   13803              :    */
   13804              : static int component_assignment_level = 0;
   13805              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   13806              : static bool finalizable_comp;
   13807              : 
   13808              : static void
   13809          194 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   13810              : {
   13811          194 :   gfc_component *comp1, *comp2;
   13812          194 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   13813          194 :   gfc_code *tmp_code = NULL;
   13814          194 :   gfc_expr *t1 = NULL;
   13815          194 :   gfc_expr *tmp_expr = NULL;
   13816          194 :   int error_count, depth;
   13817          194 :   bool finalizable_lhs;
   13818          194 :   bool use_finalize_only;
   13819              : 
   13820          194 :   gfc_get_errors (NULL, &error_count);
   13821              : 
   13822              :   /* Filter out continuing processing after an error.  */
   13823          194 :   if (error_count
   13824          194 :       || (*code)->expr1->ts.type != BT_DERIVED
   13825          194 :       || (*code)->expr2->ts.type != BT_DERIVED)
   13826          146 :     return;
   13827              : 
   13828              :   /* TODO: Handle more than one part array reference in assignments.  */
   13829          194 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   13830          194 :                                       (*code)->expr1->rank ? 1 : 0);
   13831          194 :   if (depth > 1)
   13832              :     {
   13833            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   13834              :                    "done because multiple part array references would "
   13835              :                    "occur in intermediate expressions.", &(*code)->loc);
   13836            6 :       return;
   13837              :     }
   13838              : 
   13839          188 :   if (!component_assignment_level)
   13840          140 :     finalizable_comp = true;
   13841              : 
   13842              :   /* Build a block so that function result temporaries are finalized
   13843              :      locally on exiting the rather than enclosing scope.  */
   13844          188 :   if (!component_assignment_level)
   13845              :     {
   13846          140 :       ns = gfc_build_block_ns (ns);
   13847          140 :       tmp_code = gfc_get_code (EXEC_NOP);
   13848          140 :       *tmp_code = **code;
   13849          140 :       tmp_code->next = NULL;
   13850          140 :       (*code)->op = EXEC_BLOCK;
   13851          140 :       (*code)->ext.block.ns = ns;
   13852          140 :       (*code)->ext.block.assoc = NULL;
   13853          140 :       (*code)->expr1 = (*code)->expr2 = NULL;
   13854          140 :       ns->code = tmp_code;
   13855          140 :       code = &ns->code;
   13856              :     }
   13857              : 
   13858          188 :   component_assignment_level++;
   13859              : 
   13860          188 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   13861              : 
   13862              :   /* When the lhs is finalized as a whole and none of its components needs the
   13863              :      structure copy to handle it (no pointer or allocatable components), the
   13864              :      copy can be done component by component.  The whole-derived-type assignment
   13865              :      then only finalizes the lhs and a component with a defined assignment keeps
   13866              :      its post-finalization value for the INTENT (OUT) finalization in that
   13867              :      defined assignment.  */
   13868          188 :   use_finalize_only = finalizable_lhs;
   13869          188 :   if (use_finalize_only)
   13870           66 :     for (comp1 = (*code)->expr1->ts.u.derived->components; comp1;
   13871           42 :          comp1 = comp1->next)
   13872           42 :       if (comp1->attr.pointer || comp1->attr.allocatable
   13873           42 :           || comp1->attr.proc_pointer_comp || comp1->attr.class_pointer
   13874           42 :           || comp1->attr.proc_pointer)
   13875              :         {
   13876              :           use_finalize_only = false;
   13877              :           break;
   13878              :         }
   13879              : 
   13880              :   /* Create a temporary so that functions get called only once.  */
   13881          188 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   13882          188 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   13883              :     {
   13884              :       /* Assign the rhs to the temporary.  */
   13885           81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13886           81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   13887              :         {
   13888              :           /* Use allocate on assignment for the sake of simplicity. The
   13889              :              temporary must not take on the optional attribute. Assume
   13890              :              that the assignment is guarded by a PRESENT condition if the
   13891              :              lhs is optional.  */
   13892           25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   13893           25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   13894           25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   13895              :         }
   13896          162 :       this_code = build_assignment (EXEC_ASSIGN,
   13897              :                                     tmp_expr, (*code)->expr2,
   13898           81 :                                     NULL, NULL, (*code)->loc);
   13899           81 :       this_code->expr2->must_finalize = 1;
   13900              :       /* Add the code and substitute the rhs expression.  */
   13901           81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   13902           81 :       gfc_free_expr ((*code)->expr2);
   13903           81 :       (*code)->expr2 = tmp_expr;
   13904              :     }
   13905              : 
   13906              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   13907              :      of the temporaries generated here, since the intrinsic assignment
   13908              :      to the final result already does this.  */
   13909          188 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   13910              :     {
   13911          188 :       if (finalizable_lhs)
   13912           24 :         (*code)->expr1->must_finalize = 1;
   13913          188 :       this_code = build_assignment (EXEC_ASSIGN,
   13914              :                                     (*code)->expr1, (*code)->expr2,
   13915              :                                     NULL, NULL, (*code)->loc);
   13916          188 :       if (use_finalize_only)
   13917           24 :         this_code->expr1->finalize_only = 1;
   13918          188 :       add_code_to_chain (&this_code, &head, &tail);
   13919              :     }
   13920              : 
   13921          188 :   comp1 = (*code)->expr1->ts.u.derived->components;
   13922          188 :   comp2 = (*code)->expr2->ts.u.derived->components;
   13923              : 
   13924          461 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   13925              :     {
   13926          273 :       bool inout = false;
   13927          273 :       bool finalizable_out = false;
   13928              : 
   13929              :       /* The intrinsic assignment does the right thing for pointers
   13930              :          of all kinds and allocatable components.  */
   13931          273 :       if (!gfc_bt_struct (comp1->ts.type)
   13932          206 :           || comp1->attr.pointer
   13933          206 :           || comp1->attr.allocatable
   13934          205 :           || comp1->attr.proc_pointer_comp
   13935          205 :           || comp1->attr.class_pointer
   13936          205 :           || comp1->attr.proc_pointer)
   13937              :         {
   13938              :           /* With finalize_only the whole-derived-type assignment does not copy
   13939              :              the components, so emit the copy for this one here.  Only plain
   13940              :              components reach this point, since use_finalize_only excludes
   13941              :              pointer and allocatable components.  */
   13942           68 :           if (use_finalize_only)
   13943              :             {
   13944           24 :               this_code = build_assignment (EXEC_ASSIGN,
   13945              :                                             (*code)->expr1, (*code)->expr2,
   13946           12 :                                             comp1, comp2, (*code)->loc);
   13947           12 :               add_code_to_chain (&this_code, &head, &tail);
   13948              :             }
   13949           68 :           continue;
   13950              :         }
   13951              : 
   13952          410 :       finalizable_comp = is_finalizable_type (comp1->ts)
   13953          205 :                          && !finalizable_lhs;
   13954              : 
   13955              :       /* Make an assignment for this component.  */
   13956          410 :       this_code = build_assignment (EXEC_ASSIGN,
   13957              :                                     (*code)->expr1, (*code)->expr2,
   13958          205 :                                     comp1, comp2, (*code)->loc);
   13959              : 
   13960              :       /* Convert the assignment if there is a defined assignment for
   13961              :          this type.  Otherwise, using the call from gfc_resolve_code,
   13962              :          recurse into its components.  */
   13963          205 :       gfc_resolve_code (this_code, ns);
   13964              : 
   13965          205 :       if (this_code->op == EXEC_ASSIGN_CALL)
   13966              :         {
   13967          150 :           gfc_formal_arglist *dummy_args;
   13968          150 :           gfc_symbol *rsym;
   13969              :           /* Check that there is a typebound defined assignment.  If not,
   13970              :              then this must be a module defined assignment.  We cannot
   13971              :              use the defined_assign_comp attribute here because it must
   13972              :              be this derived type that has the defined assignment and not
   13973              :              a parent type.  */
   13974          150 :           if (!(comp1->ts.u.derived->f2k_derived
   13975              :                 && comp1->ts.u.derived->f2k_derived
   13976          150 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   13977              :             {
   13978            1 :               gfc_free_statements (this_code);
   13979            1 :               this_code = NULL;
   13980            1 :               continue;
   13981              :             }
   13982              : 
   13983              :           /* If the first argument of the subroutine has intent INOUT
   13984              :              a temporary must be generated and used instead.  */
   13985          149 :           rsym = this_code->resolved_sym;
   13986          149 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   13987          274 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   13988           24 :                             && dummy_args
   13989          173 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   13990          274 :           inout = dummy_args
   13991          274 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   13992              :           /* With finalize_only the lhs component keeps its post-finalization
   13993              :              value, so the defined assignment can finalize it directly through
   13994              :              its INTENT (OUT) argument and no temporary is needed.  */
   13995           78 :           if ((inout || (finalizable_out && !use_finalize_only))
   13996           71 :               && !comp1->attr.allocatable)
   13997              :             {
   13998           71 :               gfc_code *temp_code;
   13999           71 :               inout = true;
   14000              : 
   14001              :               /* Build the temporary required for the assignment and put
   14002              :                  it at the head of the generated code.  */
   14003           71 :               if (!t1)
   14004              :                 {
   14005           71 :                   gfc_namespace *tmp_ns = ns;
   14006           71 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   14007            0 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   14008           71 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   14009           71 :                   t1->symtree->n.sym->attr.artificial = 1;
   14010          142 :                   temp_code = build_assignment (EXEC_ASSIGN,
   14011              :                                                 t1, (*code)->expr1,
   14012           71 :                                 NULL, NULL, (*code)->loc);
   14013              : 
   14014              :                   /* For allocatable LHS, check whether it is allocated.  Note
   14015              :                      that allocatable components with defined assignment are
   14016              :                      not yet support.  See PR 57696.  */
   14017           71 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   14018              :                     {
   14019           24 :                       gfc_code *block;
   14020           24 :                       gfc_expr *e =
   14021           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14022           24 :                       block = gfc_get_code (EXEC_IF);
   14023           24 :                       block->block = gfc_get_code (EXEC_IF);
   14024           24 :                       block->block->expr1
   14025           48 :                           = gfc_build_intrinsic_call (ns,
   14026              :                                     GFC_ISYM_ALLOCATED, "allocated",
   14027           24 :                                     (*code)->loc, 1, e);
   14028           24 :                       block->block->next = temp_code;
   14029           24 :                       temp_code = block;
   14030              :                     }
   14031           71 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   14032              :                 }
   14033              : 
   14034              :               /* Replace the first actual arg with the component of the
   14035              :                  temporary.  */
   14036           71 :               gfc_free_expr (this_code->ext.actual->expr);
   14037           71 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   14038           71 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   14039              : 
   14040              :               /* If the LHS variable is allocatable and wasn't allocated and
   14041              :                  the temporary is allocatable, pointer assign the address of
   14042              :                  the freshly allocated LHS to the temporary.  */
   14043           71 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   14044           71 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   14045              :                 {
   14046           18 :                   gfc_code *block;
   14047           18 :                   gfc_expr *cond;
   14048              : 
   14049           18 :                   cond = gfc_get_expr ();
   14050           18 :                   cond->ts.type = BT_LOGICAL;
   14051           18 :                   cond->ts.kind = gfc_default_logical_kind;
   14052           18 :                   cond->expr_type = EXPR_OP;
   14053           18 :                   cond->where = (*code)->loc;
   14054           18 :                   cond->value.op.op = INTRINSIC_NOT;
   14055           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   14056              :                                           GFC_ISYM_ALLOCATED, "allocated",
   14057           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   14058           18 :                   block = gfc_get_code (EXEC_IF);
   14059           18 :                   block->block = gfc_get_code (EXEC_IF);
   14060           18 :                   block->block->expr1 = cond;
   14061           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14062              :                                         t1, (*code)->expr1,
   14063           18 :                                         NULL, NULL, (*code)->loc);
   14064           18 :                   add_code_to_chain (&block, &head, &tail);
   14065              :                 }
   14066              :             }
   14067              :         }
   14068           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   14069              :         {
   14070              :           /* Don't add intrinsic assignments since they are already
   14071              :              effected by the intrinsic assignment of the structure, unless
   14072              :              finalization is required or, with finalize_only, the structure
   14073              :              assignment does not copy the components.  */
   14074            7 :           if (finalizable_comp)
   14075            0 :             this_code->expr1->must_finalize = 1;
   14076            7 :           else if (!use_finalize_only)
   14077              :             {
   14078            1 :               gfc_free_statements (this_code);
   14079            1 :               this_code = NULL;
   14080            1 :               continue;
   14081              :             }
   14082              :         }
   14083              :       else
   14084              :         {
   14085              :           /* Resolution has expanded an assignment of a derived type with
   14086              :              defined assigned components.  Remove the redundant, leading
   14087              :              assignment.  */
   14088           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   14089           48 :           gfc_code *tmp = this_code;
   14090           48 :           this_code = this_code->next;
   14091           48 :           tmp->next = NULL;
   14092           48 :           gfc_free_statements (tmp);
   14093              :         }
   14094              : 
   14095          203 :       add_code_to_chain (&this_code, &head, &tail);
   14096              : 
   14097          203 :       if (t1 && (inout || (finalizable_out && !use_finalize_only)))
   14098              :         {
   14099              :           /* Transfer the value to the final result.  */
   14100          142 :           this_code = build_assignment (EXEC_ASSIGN,
   14101              :                                         (*code)->expr1, t1,
   14102           71 :                                         comp1, comp2, (*code)->loc);
   14103           71 :           this_code->expr1->must_finalize = 0;
   14104           71 :           add_code_to_chain (&this_code, &head, &tail);
   14105              :         }
   14106              :     }
   14107              : 
   14108              :   /* Put the temporary assignments at the top of the generated code.  */
   14109          188 :   if (tmp_head && component_assignment_level == 1)
   14110              :     {
   14111          114 :       gfc_append_code (tmp_head, head);
   14112          114 :       head = tmp_head;
   14113          114 :       tmp_head = tmp_tail = NULL;
   14114              :     }
   14115              : 
   14116              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   14117              :      not accidentally deallocated. Hence, nullify t1.  */
   14118           71 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   14119          259 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   14120              :     {
   14121           18 :       gfc_code *block;
   14122           18 :       gfc_expr *cond;
   14123           18 :       gfc_expr *e;
   14124              : 
   14125           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14126           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   14127           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   14128           18 :       block = gfc_get_code (EXEC_IF);
   14129           18 :       block->block = gfc_get_code (EXEC_IF);
   14130           18 :       block->block->expr1 = cond;
   14131           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14132              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   14133           18 :                                         NULL, NULL, (*code)->loc);
   14134           18 :       gfc_append_code (tail, block);
   14135           18 :       tail = block;
   14136              :     }
   14137              : 
   14138          188 :   component_assignment_level--;
   14139              : 
   14140              :   /* Make an explicit final call for the function result.  */
   14141          188 :   if (tmp_expr)
   14142           81 :     generate_final_call (tmp_expr, &head, &tail);
   14143              : 
   14144          188 :   if (tmp_code)
   14145              :     {
   14146          140 :       ns->code = head;
   14147          140 :       return;
   14148              :     }
   14149              : 
   14150              :   /* Now attach the remaining code chain to the input code.  Step on
   14151              :      to the end of the new code since resolution is complete.  */
   14152           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14153           48 :   tail->next = (*code)->next;
   14154              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14155              :      before the temporary for the lhs is created.  */
   14156           48 :   gfc_free_expr ((*code)->expr1);
   14157           48 :   gfc_free_expr ((*code)->expr2);
   14158           48 :   **code = *head;
   14159           48 :   if (head != tail)
   14160           48 :     free (head);
   14161           48 :   *code = tail;
   14162              : }
   14163              : 
   14164              : 
   14165              : /* F2008: Pointer function assignments are of the form:
   14166              :         ptr_fcn (args) = expr
   14167              :    This function breaks these assignments into two statements:
   14168              :         temporary_pointer => ptr_fcn(args)
   14169              :         temporary_pointer = expr  */
   14170              : 
   14171              : static bool
   14172       288451 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14173              : {
   14174       288451 :   gfc_expr *tmp_ptr_expr;
   14175       288451 :   gfc_code *this_code;
   14176       288451 :   gfc_component *comp;
   14177       288451 :   gfc_symbol *s;
   14178              : 
   14179       288451 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14180              :     return false;
   14181              : 
   14182              :   /* Even if standard does not support this feature, continue to build
   14183              :      the two statements to avoid upsetting frontend_passes.c.  */
   14184          205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14185              :                   "%L", &(*code)->loc);
   14186              : 
   14187          205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14188              : 
   14189          205 :   if (comp)
   14190            6 :     s = comp->ts.interface;
   14191              :   else
   14192          199 :     s = (*code)->expr1->symtree->n.sym;
   14193              : 
   14194          205 :   if (s == NULL || !s->result->attr.pointer)
   14195              :     {
   14196            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14197              :                  "%L must have the pointer attribute.",
   14198            5 :                  &(*code)->expr1->where);
   14199            5 :       (*code)->op = EXEC_NOP;
   14200            5 :       return false;
   14201              :     }
   14202              : 
   14203          200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14204              : 
   14205              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14206              :      end, where array bounds are not known, arrays are made allocatable.
   14207              :      Change the temporary to a pointer here.  */
   14208          200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14209          200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14210          200 :   tmp_ptr_expr->where = (*code)->loc;
   14211              : 
   14212              :   /* A new charlen is required to ensure that the variable string length
   14213              :      is different to that of the original lhs for deferred results.  */
   14214          200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14215              :     {
   14216           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14217           60 :       tmp_ptr_expr->ts.deferred = 1;
   14218           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14219           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14220           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14221              :     }
   14222              : 
   14223          400 :   this_code = build_assignment (EXEC_ASSIGN,
   14224              :                                 tmp_ptr_expr, (*code)->expr2,
   14225          200 :                                 NULL, NULL, (*code)->loc);
   14226          200 :   this_code->next = (*code)->next;
   14227          200 :   (*code)->next = this_code;
   14228          200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14229          200 :   (*code)->expr2 = (*code)->expr1;
   14230          200 :   (*code)->expr1 = tmp_ptr_expr;
   14231              : 
   14232          200 :   return true;
   14233              : }
   14234              : 
   14235              : 
   14236              : /* Deferred character length assignments from an operator expression
   14237              :    require a temporary because the character length of the lhs can
   14238              :    change in the course of the assignment.  */
   14239              : 
   14240              : static bool
   14241       287279 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14242              : {
   14243       287279 :   gfc_expr *tmp_expr;
   14244       287279 :   gfc_code *this_code;
   14245              : 
   14246       287279 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14247        27508 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14248          836 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14249          835 :          && (*code)->expr2->expr_type == EXPR_OP))
   14250              :     return false;
   14251              : 
   14252           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14253              :     return false;
   14254              : 
   14255           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14256              :     return false;
   14257              : 
   14258           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14259           22 :   tmp_expr->where = (*code)->loc;
   14260              : 
   14261              :   /* A new charlen is required to ensure that the variable string
   14262              :      length is different to that of the original lhs.  */
   14263           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14264           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14265           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14266           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14267              : 
   14268           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14269              : 
   14270           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14271           22 :                                 (*code)->expr1,
   14272              :                                 gfc_copy_expr (tmp_expr),
   14273              :                                 NULL, NULL, (*code)->loc);
   14274              : 
   14275           22 :   (*code)->expr1 = tmp_expr;
   14276              : 
   14277           22 :   this_code->next = (*code)->next;
   14278           22 :   (*code)->next = this_code;
   14279              : 
   14280           22 :   return true;
   14281              : }
   14282              : 
   14283              : static void mark_lhs_assignments_set (gfc_code *code);
   14284              : 
   14285              : /* Given a block of code, recursively resolve everything pointed to by this
   14286              :    code block.  */
   14287              : 
   14288              : void
   14289       698425 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14290              : {
   14291       698425 :   int omp_workshare_save;
   14292       698425 :   int forall_save, do_concurrent_save;
   14293       698425 :   code_stack frame;
   14294       698425 :   bool t;
   14295       698425 :   gfc_code *orig_code = code;
   14296              : 
   14297       698425 :   frame.prev = cs_base;
   14298       698425 :   frame.head = code;
   14299       698425 :   cs_base = &frame;
   14300              : 
   14301       698425 :   find_reachable_labels (code);
   14302              : 
   14303      2546006 :   for (; code; code = code->next)
   14304              :     {
   14305      1149157 :       frame.current = code;
   14306      1149157 :       forall_save = forall_flag;
   14307      1149157 :       do_concurrent_save = gfc_do_concurrent_flag;
   14308              : 
   14309      1149157 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14310              :         {
   14311         2217 :           if (code->op == EXEC_FORALL)
   14312         1993 :             forall_flag = 1;
   14313          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14314          224 :             gfc_do_concurrent_flag = 1;
   14315         2217 :           gfc_resolve_forall (code, ns, forall_save);
   14316         2217 :           if (code->op == EXEC_FORALL)
   14317         1993 :             forall_flag = 2;
   14318          224 :           else if (code->op == EXEC_DO_CONCURRENT)
   14319          224 :             gfc_do_concurrent_flag = 2;
   14320              :         }
   14321      1146940 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14322          138 :         for (gfc_omp_variant *variant
   14323              :                = code->ext.omp_variants;
   14324          448 :              variant; variant = variant->next)
   14325          310 :           gfc_resolve_code (variant->code, ns);
   14326      1146802 :       else if (code->block)
   14327              :         {
   14328       333488 :           omp_workshare_save = -1;
   14329       333488 :           switch (code->op)
   14330              :             {
   14331        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14332        10119 :             case EXEC_OACC_PARALLEL:
   14333        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14334        10119 :             case EXEC_OACC_KERNELS:
   14335        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14336        10119 :             case EXEC_OACC_SERIAL:
   14337        10119 :             case EXEC_OACC_DATA:
   14338        10119 :             case EXEC_OACC_HOST_DATA:
   14339        10119 :             case EXEC_OACC_LOOP:
   14340        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14341        10119 :               break;
   14342           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14343           54 :               omp_workshare_save = omp_workshare_flag;
   14344           54 :               omp_workshare_flag = 1;
   14345           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14346           54 :               break;
   14347         6049 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14348         6049 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14349         6049 :             case EXEC_OMP_MASKED_TASKLOOP:
   14350         6049 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14351         6049 :             case EXEC_OMP_MASTER_TASKLOOP:
   14352         6049 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14353         6049 :             case EXEC_OMP_PARALLEL:
   14354         6049 :             case EXEC_OMP_PARALLEL_DO:
   14355         6049 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14356         6049 :             case EXEC_OMP_PARALLEL_LOOP:
   14357         6049 :             case EXEC_OMP_PARALLEL_MASKED:
   14358         6049 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14359         6049 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14360         6049 :             case EXEC_OMP_PARALLEL_MASTER:
   14361         6049 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14362         6049 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14363         6049 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14364         6049 :             case EXEC_OMP_TARGET_PARALLEL:
   14365         6049 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14366         6049 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14367         6049 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14368         6049 :             case EXEC_OMP_TARGET_TEAMS:
   14369         6049 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14370         6049 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14371         6049 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14372         6049 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14373         6049 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14374         6049 :             case EXEC_OMP_TASK:
   14375         6049 :             case EXEC_OMP_TASKLOOP:
   14376         6049 :             case EXEC_OMP_TASKLOOP_SIMD:
   14377         6049 :             case EXEC_OMP_TEAMS:
   14378         6049 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14379         6049 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14380         6049 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14381         6049 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14382         6049 :             case EXEC_OMP_TEAMS_LOOP:
   14383         6049 :               omp_workshare_save = omp_workshare_flag;
   14384         6049 :               omp_workshare_flag = 0;
   14385         6049 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14386         6049 :               break;
   14387         3064 :             case EXEC_OMP_DISTRIBUTE:
   14388         3064 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14389         3064 :             case EXEC_OMP_DO:
   14390         3064 :             case EXEC_OMP_DO_SIMD:
   14391         3064 :             case EXEC_OMP_LOOP:
   14392         3064 :             case EXEC_OMP_SIMD:
   14393         3064 :             case EXEC_OMP_TARGET_SIMD:
   14394         3064 :             case EXEC_OMP_TILE:
   14395         3064 :             case EXEC_OMP_UNROLL:
   14396         3064 :               gfc_resolve_omp_do_blocks (code, ns);
   14397         3064 :               break;
   14398              :             case EXEC_SELECT_TYPE:
   14399              :             case EXEC_SELECT_RANK:
   14400              :               /* Blocks are handled in resolve_select_type/rank because we
   14401              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14402              :               break;
   14403              :             case EXEC_DO_CONCURRENT:
   14404              :               gfc_do_concurrent_flag = 1;
   14405              :               gfc_resolve_blocks (code->block, ns);
   14406              :               gfc_do_concurrent_flag = 2;
   14407              :               break;
   14408           39 :             case EXEC_OMP_WORKSHARE:
   14409           39 :               omp_workshare_save = omp_workshare_flag;
   14410           39 :               omp_workshare_flag = 1;
   14411              :               /* FALL THROUGH */
   14412       310045 :             default:
   14413       310045 :               gfc_resolve_blocks (code->block, ns);
   14414       310045 :               break;
   14415              :             }
   14416              : 
   14417       329331 :           if (omp_workshare_save != -1)
   14418         6142 :             omp_workshare_flag = omp_workshare_save;
   14419              :         }
   14420      1149157 : start:
   14421      1149362 :       t = true;
   14422      1149362 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14423      1147931 :           t = gfc_resolve_expr (code->expr1);
   14424              : 
   14425      1149362 :       forall_flag = forall_save;
   14426      1149362 :       gfc_do_concurrent_flag = do_concurrent_save;
   14427              : 
   14428      1149362 :       if (!gfc_resolve_expr (code->expr2))
   14429          638 :         t = false;
   14430              : 
   14431      1149362 :       if (code->op == EXEC_ALLOCATE
   14432      1149362 :           && !gfc_resolve_expr (code->expr3))
   14433              :         t = false;
   14434              : 
   14435      1149362 :       switch (code->op)
   14436              :         {
   14437              :         case EXEC_NOP:
   14438              :         case EXEC_END_BLOCK:
   14439              :         case EXEC_END_NESTED_BLOCK:
   14440              :         case EXEC_CYCLE:
   14441              :           break;
   14442              : 
   14443       219729 :         case EXEC_STOP:
   14444       219729 :         case EXEC_ERROR_STOP:
   14445       219729 :           if (code->expr1 != NULL && t)
   14446              :             {
   14447       199276 :               if (!(code->expr1->ts.type == BT_CHARACTER
   14448              :                     || code->expr1->ts.type == BT_INTEGER))
   14449            1 :                 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER "
   14450              :                            "type", &code->expr1->where);
   14451       199275 :               else if (code->expr1->rank != 0)
   14452            0 :                 gfc_error ("STOP code at %L must be scalar",
   14453              :                            &code->expr1->where);
   14454       199275 :               else if (code->expr1->ts.type == BT_CHARACTER
   14455          478 :                        && code->expr1->ts.kind != gfc_default_character_kind)
   14456            0 :                 gfc_error ("STOP code at %L must be default character KIND=%d",
   14457              :                            &code->expr1->where, (int) gfc_default_character_kind);
   14458       199275 :               else if (code->expr1->ts.type == BT_INTEGER
   14459       198797 :                        && code->expr1->ts.kind != gfc_default_integer_kind)
   14460            8 :                 gfc_notify_std (GFC_STD_F2018, "STOP code at %L must be default "
   14461              :                                 "integer KIND=%d", &code->expr1->where,
   14462              :                                 (int) gfc_default_integer_kind);
   14463              :             }
   14464       219729 :           if (code->expr2 != NULL
   14465           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14466           37 :                   || code->expr2->rank != 0))
   14467            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14468              :                        &code->expr2->where);
   14469              : 
   14470              :           /* Fall through.  */
   14471       219759 :         case EXEC_PAUSE:
   14472       219759 :           gfc_value_used_expr (code->expr1, VALUE_USED);
   14473       219759 :           break;
   14474              : 
   14475              :         case EXEC_EXIT:
   14476              :         case EXEC_CONTINUE:
   14477              :         case EXEC_DT_END:
   14478              :         case EXEC_ASSIGN_CALL:
   14479              :           break;
   14480              : 
   14481           54 :         case EXEC_CRITICAL:
   14482           54 :           resolve_critical (code);
   14483           54 :           break;
   14484              : 
   14485         1317 :         case EXEC_SYNC_ALL:
   14486         1317 :         case EXEC_SYNC_IMAGES:
   14487         1317 :         case EXEC_SYNC_MEMORY:
   14488         1317 :           resolve_sync (code);
   14489         1317 :           break;
   14490              : 
   14491          197 :         case EXEC_LOCK:
   14492          197 :         case EXEC_UNLOCK:
   14493          197 :         case EXEC_EVENT_POST:
   14494          197 :         case EXEC_EVENT_WAIT:
   14495          197 :           resolve_lock_unlock_event (code);
   14496          197 :           break;
   14497              : 
   14498              :         case EXEC_FAIL_IMAGE:
   14499              :           break;
   14500              : 
   14501          154 :         case EXEC_FORM_TEAM:
   14502          154 :           resolve_form_team (code);
   14503          154 :           break;
   14504              : 
   14505           97 :         case EXEC_CHANGE_TEAM:
   14506           97 :           resolve_change_team (code);
   14507           97 :           break;
   14508              : 
   14509           95 :         case EXEC_END_TEAM:
   14510           95 :           resolve_end_team (code);
   14511           95 :           break;
   14512              : 
   14513           43 :         case EXEC_SYNC_TEAM:
   14514           43 :           resolve_sync_team (code);
   14515           43 :           break;
   14516              : 
   14517         1491 :         case EXEC_ENTRY:
   14518              :           /* Keep track of which entry we are up to.  */
   14519         1491 :           current_entry_id = code->ext.entry->id;
   14520         1491 :           break;
   14521              : 
   14522          453 :         case EXEC_WHERE:
   14523          453 :           resolve_where (code, NULL);
   14524          453 :           break;
   14525              : 
   14526         1250 :         case EXEC_GOTO:
   14527         1250 :           if (code->expr1 != NULL)
   14528              :             {
   14529           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14530           76 :                   || code->expr1->ts.type != BT_INTEGER
   14531           76 :                   || (code->expr1->ref
   14532            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14533           75 :                   || code->expr1->symtree == NULL
   14534           75 :                   || (code->expr1->symtree->n.sym
   14535           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14536           75 :                           == FL_PARAMETER)))
   14537            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14538              :                            "scalar INTEGER variable", &code->expr1->where);
   14539           74 :               else if (code->expr1->symtree->n.sym
   14540           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14541            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14542              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14543              :                            &code->expr1->where);
   14544              :             }
   14545              :           else
   14546         1172 :             resolve_branch (code->label1, code);
   14547              :           break;
   14548              : 
   14549         3224 :         case EXEC_RETURN:
   14550         3224 :           if (code->expr1 != NULL
   14551           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14552            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14553              :                        "INTEGER return specifier", &code->expr1->where);
   14554              :           break;
   14555              : 
   14556              :         case EXEC_INIT_ASSIGN:
   14557              :         case EXEC_END_PROCEDURE:
   14558              :           break;
   14559              : 
   14560       289627 :         case EXEC_ASSIGN:
   14561       289627 :           if (!t)
   14562              :             break;
   14563              : 
   14564       288951 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14565       288951 :               && gfc_is_coindexed (code->expr1))
   14566              :             {
   14567              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14568              :                  coindexed variable.  */
   14569          500 :               code->op = EXEC_CALL;
   14570          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14571              :                                 true);
   14572          500 :               code->resolved_sym = code->symtree->n.sym;
   14573          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14574          500 :               code->resolved_sym->attr.intrinsic = 1;
   14575          500 :               code->resolved_sym->attr.subroutine = 1;
   14576          500 :               code->resolved_isym
   14577          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14578          500 :               gfc_commit_symbol (code->resolved_sym);
   14579          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14580          500 :               code->ext.actual->expr = code->expr1;
   14581          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14582          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14583          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14584              :                 {
   14585              :                   /* Convert assignments of expr1[...] = expr2 into
   14586              :                         tvar = expr2
   14587              :                         expr1[...] = tvar
   14588              :                      when expr2 is not trivial.  */
   14589           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14590           54 :                   gfc_code next_code = *code;
   14591           54 :                   gfc_code *rhs_code
   14592          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14593           54 :                                         NULL, code->expr2->where);
   14594           54 :                   *code = *rhs_code;
   14595           54 :                   code->next = rhs_code;
   14596           54 :                   *rhs_code = next_code;
   14597              : 
   14598           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14599           54 :                   rhs_code->expr1 = NULL;
   14600           54 :                   rhs_code->expr2 = NULL;
   14601              :                 }
   14602              :               else
   14603              :                 {
   14604          446 :                   code->ext.actual->next->expr = code->expr2;
   14605              : 
   14606          446 :                   code->expr1 = NULL;
   14607          446 :                   code->expr2 = NULL;
   14608              :                 }
   14609              :               break;
   14610              :             }
   14611              : 
   14612       288451 :           if (code->expr1->ts.type == BT_CLASS)
   14613         1163 :             gfc_find_vtab (&code->expr2->ts);
   14614              : 
   14615              :           /* If this is a pointer function in an lvalue variable context,
   14616              :              the new code will have to be resolved afresh. This is also the
   14617              :              case with an error, where the code is transformed into NOP to
   14618              :              prevent ICEs downstream.  */
   14619       288451 :           if (resolve_ptr_fcn_assign (&code, ns)
   14620       288451 :               || code->op == EXEC_NOP)
   14621          205 :             goto start;
   14622              : 
   14623       288246 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14624       288246 :                                          _("assignment")))
   14625              :             break;
   14626              : 
   14627       288203 :           if (resolve_ordinary_assign (code, ns))
   14628              :             {
   14629          924 :               if (omp_workshare_flag)
   14630              :                 {
   14631            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14632            1 :                              "at %L", &code->loc);
   14633            1 :                   break;
   14634              :                 }
   14635          923 :               if (code->op == EXEC_COMPCALL)
   14636          455 :                 goto compcall;
   14637              :               else
   14638          468 :                 goto call;
   14639              :             }
   14640              : 
   14641              :           /* Check for dependencies in deferred character length array
   14642              :              assignments and generate a temporary, if necessary.  */
   14643       287279 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14644              :             break;
   14645              : 
   14646              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14647       287257 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14648         7394 :               && code->expr1->ts.u.derived
   14649         7394 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14650          194 :             generate_component_assignments (&code, ns);
   14651       287063 :           else if (code->op == EXEC_ASSIGN)
   14652              :             {
   14653       287063 :               if (gfc_may_be_finalized (code->expr1->ts))
   14654         1344 :                 code->expr1->must_finalize = 1;
   14655       287063 :               if (code->expr2->expr_type == EXPR_ARRAY
   14656       287063 :                   && gfc_may_be_finalized (code->expr2->ts))
   14657           73 :                 code->expr2->must_finalize = 1;
   14658              :             }
   14659              : 
   14660              :           break;
   14661              : 
   14662          126 :         case EXEC_LABEL_ASSIGN:
   14663          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   14664            0 :             gfc_error ("Label %d referenced at %L is never defined",
   14665              :                        code->label1->value, &code->label1->where);
   14666          126 :           if (t
   14667          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   14668          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   14669          126 :                   || code->expr1->symtree->n.sym->ts.kind
   14670          126 :                      != gfc_default_integer_kind
   14671          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   14672          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   14673            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   14674              :                        "default INTEGER variable", &code->expr1->where);
   14675              :           break;
   14676              : 
   14677        10538 :         case EXEC_POINTER_ASSIGN:
   14678        10538 :           {
   14679        10538 :             gfc_expr* e;
   14680              : 
   14681        10538 :             if (!t)
   14682              :               break;
   14683              : 
   14684              :             /* This is both a variable definition and pointer assignment
   14685              :                context, so check both of them.  For rank remapping, a final
   14686              :                array ref may be present on the LHS and fool gfc_expr_attr
   14687              :                used in gfc_check_vardef_context.  Remove it.  */
   14688        10533 :             e = remove_last_array_ref (code->expr1);
   14689        21066 :             t = gfc_check_vardef_context (e, true, false, false,
   14690        10533 :                                           _("pointer assignment"));
   14691        10533 :             if (t)
   14692        10504 :               t = gfc_check_vardef_context (e, false, false, false,
   14693        10504 :                                             _("pointer assignment"));
   14694        10533 :             gfc_free_expr (e);
   14695              : 
   14696        10533 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   14697              : 
   14698        10391 :             if (!t)
   14699              :               break;
   14700              : 
   14701              :             /* Assigning a class object always is a regular assign.  */
   14702        10391 :             if (code->expr2->ts.type == BT_CLASS
   14703          582 :                 && code->expr1->ts.type == BT_CLASS
   14704          491 :                 && CLASS_DATA (code->expr2)
   14705          490 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   14706        11028 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   14707           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   14708           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   14709           43 :                         == FL_PROCEDURE))
   14710          340 :               code->op = EXEC_ASSIGN;
   14711              :             break;
   14712              :           }
   14713              : 
   14714           72 :         case EXEC_ARITHMETIC_IF:
   14715           72 :           {
   14716           72 :             gfc_expr *e = code->expr1;
   14717              : 
   14718           72 :             gfc_resolve_expr (e);
   14719           72 :             if (e->expr_type == EXPR_NULL)
   14720            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   14721              : 
   14722           72 :             if (t && (e->rank > 0
   14723           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   14724            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   14725              :                          "REAL or INTEGER expression", &e->where);
   14726              : 
   14727           72 :             resolve_branch (code->label1, code);
   14728           72 :             resolve_branch (code->label2, code);
   14729           72 :             resolve_branch (code->label3, code);
   14730              :           }
   14731           72 :           break;
   14732              : 
   14733       233380 :         case EXEC_IF:
   14734       233380 :           if (t && code->expr1 != NULL
   14735            0 :               && (code->expr1->ts.type != BT_LOGICAL
   14736            0 :                   || code->expr1->rank != 0))
   14737            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   14738              :                        &code->expr1->where);
   14739              :           break;
   14740              : 
   14741        80643 :         case EXEC_CALL:
   14742        80643 :         call:
   14743        80643 :           resolve_call (code);
   14744        80643 :           break;
   14745              : 
   14746         1762 :         case EXEC_COMPCALL:
   14747         1762 :         compcall:
   14748         1762 :           resolve_typebound_subroutine (code);
   14749         1762 :           break;
   14750              : 
   14751          124 :         case EXEC_CALL_PPC:
   14752          124 :           resolve_ppc_call (code);
   14753          124 :           break;
   14754              : 
   14755          688 :         case EXEC_SELECT:
   14756              :           /* Select is complicated. Also, a SELECT construct could be
   14757              :              a transformed computed GOTO.  */
   14758          688 :           resolve_select (code, false);
   14759          688 :           break;
   14760              : 
   14761         3135 :         case EXEC_SELECT_TYPE:
   14762         3135 :           resolve_select_type (code, ns);
   14763         3135 :           break;
   14764              : 
   14765         1048 :         case EXEC_SELECT_RANK:
   14766         1048 :           resolve_select_rank (code, ns);
   14767         1048 :           break;
   14768              : 
   14769         8331 :         case EXEC_BLOCK:
   14770         8331 :           resolve_block_construct (code);
   14771         8331 :           break;
   14772              : 
   14773        33193 :         case EXEC_DO:
   14774        33193 :           if (code->ext.iterator != NULL)
   14775              :             {
   14776        33193 :               gfc_iterator *iter = code->ext.iterator;
   14777        33193 :               if (gfc_resolve_iterator (iter, true, false))
   14778        33179 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   14779              :                                          true);
   14780              :             }
   14781              :           break;
   14782              : 
   14783          531 :         case EXEC_DO_WHILE:
   14784          531 :           if (code->expr1 == NULL)
   14785            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   14786              :                                 "DO WHILE");
   14787          531 :           if (t
   14788          531 :               && (code->expr1->rank != 0
   14789          531 :                   || code->expr1->ts.type != BT_LOGICAL))
   14790            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   14791              :                        "a scalar LOGICAL expression", &code->expr1->where);
   14792              :           break;
   14793              : 
   14794        14583 :         case EXEC_ALLOCATE:
   14795        14583 :           if (t)
   14796        14581 :             resolve_allocate_deallocate (code, "ALLOCATE");
   14797              : 
   14798              :           break;
   14799              : 
   14800         6199 :         case EXEC_DEALLOCATE:
   14801         6199 :           if (t)
   14802         6199 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   14803              : 
   14804              :           break;
   14805              : 
   14806         3961 :         case EXEC_OPEN:
   14807         3961 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   14808              :             break;
   14809              : 
   14810         3734 :           resolve_branch (code->ext.open->err, code);
   14811         3734 :           break;
   14812              : 
   14813         3154 :         case EXEC_CLOSE:
   14814         3154 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   14815              :             break;
   14816              : 
   14817         3120 :           resolve_branch (code->ext.close->err, code);
   14818         3120 :           break;
   14819              : 
   14820         2857 :         case EXEC_BACKSPACE:
   14821         2857 :         case EXEC_ENDFILE:
   14822         2857 :         case EXEC_REWIND:
   14823         2857 :         case EXEC_FLUSH:
   14824         2857 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   14825              :             break;
   14826              : 
   14827         2791 :           resolve_branch (code->ext.filepos->err, code);
   14828         2791 :           break;
   14829              : 
   14830          838 :         case EXEC_INQUIRE:
   14831          838 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14832              :               break;
   14833              : 
   14834          790 :           resolve_branch (code->ext.inquire->err, code);
   14835          790 :           break;
   14836              : 
   14837           92 :         case EXEC_IOLENGTH:
   14838           92 :           gcc_assert (code->ext.inquire != NULL);
   14839           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14840              :             break;
   14841              : 
   14842           90 :           resolve_branch (code->ext.inquire->err, code);
   14843           90 :           break;
   14844              : 
   14845           89 :         case EXEC_WAIT:
   14846           89 :           if (!gfc_resolve_wait (code->ext.wait))
   14847              :             break;
   14848              : 
   14849           74 :           resolve_branch (code->ext.wait->err, code);
   14850           74 :           resolve_branch (code->ext.wait->end, code);
   14851           74 :           resolve_branch (code->ext.wait->eor, code);
   14852           74 :           break;
   14853              : 
   14854        33634 :         case EXEC_READ:
   14855        33634 :         case EXEC_WRITE:
   14856        33634 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   14857              :             break;
   14858              : 
   14859        33326 :           resolve_branch (code->ext.dt->err, code);
   14860        33326 :           resolve_branch (code->ext.dt->end, code);
   14861        33326 :           resolve_branch (code->ext.dt->eor, code);
   14862        33326 :           break;
   14863              : 
   14864        47647 :         case EXEC_TRANSFER:
   14865        47647 :           resolve_transfer (code);
   14866        47647 :           break;
   14867              : 
   14868         2217 :         case EXEC_DO_CONCURRENT:
   14869         2217 :         case EXEC_FORALL:
   14870         2217 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   14871              : 
   14872         2217 :           if (code->expr1 != NULL
   14873          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   14874            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   14875              :                        "expression", &code->expr1->where);
   14876              : 
   14877         2217 :     if (code->op == EXEC_DO_CONCURRENT)
   14878          224 :       resolve_locality_spec (code, ns);
   14879              :           break;
   14880              : 
   14881        13538 :         case EXEC_OACC_PARALLEL_LOOP:
   14882        13538 :         case EXEC_OACC_PARALLEL:
   14883        13538 :         case EXEC_OACC_KERNELS_LOOP:
   14884        13538 :         case EXEC_OACC_KERNELS:
   14885        13538 :         case EXEC_OACC_SERIAL_LOOP:
   14886        13538 :         case EXEC_OACC_SERIAL:
   14887        13538 :         case EXEC_OACC_DATA:
   14888        13538 :         case EXEC_OACC_HOST_DATA:
   14889        13538 :         case EXEC_OACC_LOOP:
   14890        13538 :         case EXEC_OACC_UPDATE:
   14891        13538 :         case EXEC_OACC_WAIT:
   14892        13538 :         case EXEC_OACC_CACHE:
   14893        13538 :         case EXEC_OACC_ENTER_DATA:
   14894        13538 :         case EXEC_OACC_EXIT_DATA:
   14895        13538 :         case EXEC_OACC_ATOMIC:
   14896        13538 :         case EXEC_OACC_DECLARE:
   14897        13538 :         case EXEC_OACC_INIT:
   14898        13538 :         case EXEC_OACC_SHUTDOWN:
   14899        13538 :         case EXEC_OACC_SET:
   14900        13538 :           gfc_resolve_oacc_directive (code, ns);
   14901        13538 :           break;
   14902              : 
   14903        17319 :         case EXEC_OMP_ALLOCATE:
   14904        17319 :         case EXEC_OMP_ALLOCATORS:
   14905        17319 :         case EXEC_OMP_ASSUME:
   14906        17319 :         case EXEC_OMP_ATOMIC:
   14907        17319 :         case EXEC_OMP_BARRIER:
   14908        17319 :         case EXEC_OMP_CANCEL:
   14909        17319 :         case EXEC_OMP_CANCELLATION_POINT:
   14910        17319 :         case EXEC_OMP_CRITICAL:
   14911        17319 :         case EXEC_OMP_FLUSH:
   14912        17319 :         case EXEC_OMP_DEPOBJ:
   14913        17319 :         case EXEC_OMP_DISPATCH:
   14914        17319 :         case EXEC_OMP_DISTRIBUTE:
   14915        17319 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14916        17319 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14917        17319 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   14918        17319 :         case EXEC_OMP_DO:
   14919        17319 :         case EXEC_OMP_DO_SIMD:
   14920        17319 :         case EXEC_OMP_ERROR:
   14921        17319 :         case EXEC_OMP_INTEROP:
   14922        17319 :         case EXEC_OMP_LOOP:
   14923        17319 :         case EXEC_OMP_MASTER:
   14924        17319 :         case EXEC_OMP_MASTER_TASKLOOP:
   14925        17319 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14926        17319 :         case EXEC_OMP_MASKED:
   14927        17319 :         case EXEC_OMP_MASKED_TASKLOOP:
   14928        17319 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14929        17319 :         case EXEC_OMP_METADIRECTIVE:
   14930        17319 :         case EXEC_OMP_ORDERED:
   14931        17319 :         case EXEC_OMP_SCAN:
   14932        17319 :         case EXEC_OMP_SCOPE:
   14933        17319 :         case EXEC_OMP_SECTIONS:
   14934        17319 :         case EXEC_OMP_SIMD:
   14935        17319 :         case EXEC_OMP_SINGLE:
   14936        17319 :         case EXEC_OMP_TARGET:
   14937        17319 :         case EXEC_OMP_TARGET_DATA:
   14938        17319 :         case EXEC_OMP_TARGET_ENTER_DATA:
   14939        17319 :         case EXEC_OMP_TARGET_EXIT_DATA:
   14940        17319 :         case EXEC_OMP_TARGET_PARALLEL:
   14941        17319 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   14942        17319 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14943        17319 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14944        17319 :         case EXEC_OMP_TARGET_SIMD:
   14945        17319 :         case EXEC_OMP_TARGET_TEAMS:
   14946        17319 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14947        17319 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14948        17319 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14949        17319 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14950        17319 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   14951        17319 :         case EXEC_OMP_TARGET_UPDATE:
   14952        17319 :         case EXEC_OMP_TASK:
   14953        17319 :         case EXEC_OMP_TASKGROUP:
   14954        17319 :         case EXEC_OMP_TASKLOOP:
   14955        17319 :         case EXEC_OMP_TASKLOOP_SIMD:
   14956        17319 :         case EXEC_OMP_TASKWAIT:
   14957        17319 :         case EXEC_OMP_TASKYIELD:
   14958        17319 :         case EXEC_OMP_TEAMS:
   14959        17319 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   14960        17319 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14961        17319 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14962        17319 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14963        17319 :         case EXEC_OMP_TEAMS_LOOP:
   14964        17319 :         case EXEC_OMP_TILE:
   14965        17319 :         case EXEC_OMP_UNROLL:
   14966        17319 :         case EXEC_OMP_WORKSHARE:
   14967        17319 :           gfc_resolve_omp_directive (code, ns);
   14968        17319 :           break;
   14969              : 
   14970         3927 :         case EXEC_OMP_PARALLEL:
   14971         3927 :         case EXEC_OMP_PARALLEL_DO:
   14972         3927 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   14973         3927 :         case EXEC_OMP_PARALLEL_LOOP:
   14974         3927 :         case EXEC_OMP_PARALLEL_MASKED:
   14975         3927 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14976         3927 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14977         3927 :         case EXEC_OMP_PARALLEL_MASTER:
   14978         3927 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14979         3927 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14980         3927 :         case EXEC_OMP_PARALLEL_SECTIONS:
   14981         3927 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   14982         3927 :           omp_workshare_save = omp_workshare_flag;
   14983         3927 :           omp_workshare_flag = 0;
   14984         3927 :           gfc_resolve_omp_directive (code, ns);
   14985         3927 :           omp_workshare_flag = omp_workshare_save;
   14986         3927 :           break;
   14987              : 
   14988            0 :         default:
   14989            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   14990              :         }
   14991      1149156 :       gfc_value_used_expr (code->expr2, VALUE_USED);
   14992      1149156 :       gfc_value_used_expr (code->expr3, VALUE_USED);
   14993      1149156 :       gfc_value_used_expr (code->expr4, VALUE_USED);
   14994              :     }
   14995              : 
   14996       698424 :   mark_lhs_assignments_set (orig_code);
   14997              : 
   14998       698424 :   cs_base = frame.prev;
   14999       698424 : }
   15000              : 
   15001              : 
   15002              : /* Resolve initial values and make sure they are compatible with
   15003              :    the variable.  */
   15004              : 
   15005              : static void
   15006      1946073 : resolve_values (gfc_symbol *sym)
   15007              : {
   15008      1946073 :   bool t;
   15009              : 
   15010      1946073 :   if (sym->value == NULL)
   15011              :     return;
   15012              : 
   15013       446570 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   15014           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   15015              :                  "Using parameter %qs declared at %L is deprecated",
   15016              :                  sym->name, &sym->declared_at);
   15017              : 
   15018       446570 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   15019        40910 :     t= resolve_structure_cons (sym->value, 1);
   15020              :   else
   15021       405660 :     t = gfc_resolve_expr (sym->value);
   15022              : 
   15023       446570 :   if (!t)
   15024              :     return;
   15025              : 
   15026       446568 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   15027              : }
   15028              : 
   15029              : 
   15030              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   15031              :    for them once, rather than for each variable declared of that type.  */
   15032              : 
   15033              : static void
   15034      1915580 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   15035              : {
   15036      1915580 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   15037        86205 :       && derived_sym->attr.is_bind_c == 1)
   15038        27885 :     verify_bind_c_derived_type (derived_sym);
   15039              : 
   15040      1915580 :   return;
   15041              : }
   15042              : 
   15043              : 
   15044              : /* Check the interfaces of DTIO procedures associated with derived
   15045              :    type 'sym'.  These procedures can either have typebound bindings or
   15046              :    can appear in DTIO generic interfaces.  */
   15047              : 
   15048              : static void
   15049      1947043 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   15050              : {
   15051      1947043 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   15052              :     return;
   15053              : 
   15054        95865 :   gfc_check_dtio_interfaces (sym);
   15055              : 
   15056        95865 :   return;
   15057              : }
   15058              : 
   15059              : /* Auxiliary function, checks if an argument decays to a pointer.  */
   15060              : 
   15061              : static bool
   15062        70414 : decays_to_pointer (gfc_symbol *sym)
   15063              : {
   15064        70414 :   if (!sym->as)
   15065              :     return true;
   15066              : 
   15067        19599 :   if (sym->as->type == AS_ASSUMED_SHAPE)
   15068              :     return false;
   15069              : 
   15070        15844 :   if (sym->as->type == AS_ASSUMED_RANK)
   15071              :     return false;
   15072              : 
   15073        10746 :   if (sym->as->type == AS_DEFERRED && sym->attr.dummy)
   15074          968 :     return false;
   15075              : 
   15076              :   return true;
   15077              : }
   15078              : 
   15079              : /* Helper function, returns true if the types conform according to the C
   15080              :    standard, when they are not equal on the Fortran side.  If we decide to
   15081              :    include or exclude any types from this, this is the place to change.  */
   15082              : 
   15083              : static bool
   15084          390 : c_types_conform (gfc_typespec *ts1, gfc_typespec *ts2)
   15085              : {
   15086          390 :   if (ts1->type == BT_ASSUMED || ts2->type == BT_ASSUMED)
   15087              :     return true;
   15088              : 
   15089          384 :   if (ts1->kind == ts2->kind
   15090              :       && (ts1->type == BT_CHARACTER || ts1->type == BT_INTEGER
   15091              :           || ts1->type == BT_UNSIGNED)
   15092              :       && (ts2->type == BT_CHARACTER || ts2->type == BT_INTEGER
   15093              :           || ts2->type == BT_UNSIGNED))
   15094          384 :     return true;
   15095              : 
   15096              :   return false;
   15097              : 
   15098              : }
   15099              : 
   15100              : /* Check argument lists of BIND(C) procedures against each other, return
   15101              :    false if they do not. */
   15102              : 
   15103              : static bool
   15104        12874 : compare_c_binding_arglists (gfc_symbol *osym, gfc_symbol *nsym)
   15105              : {
   15106        12874 :   gfc_formal_arglist *oarg, *narg;
   15107        12874 :   bool ret = true;
   15108        12874 :   locus *oloc, *nloc;
   15109              : 
   15110        12874 :   oarg = osym->formal;
   15111        12874 :   narg = nsym->formal;
   15112        12874 :   oloc = &osym->declared_at;
   15113        12874 :   nloc = &nsym->declared_at;
   15114        48091 :   for ( ; oarg && narg ; oarg = oarg->next, narg = narg->next)
   15115              :     {
   15116        35217 :       oloc = &oarg->sym->declared_at;
   15117        35217 :       nloc = &narg->sym->declared_at;
   15118              : 
   15119        35217 :       if (!gfc_compare_types (&oarg->sym->ts, &narg->sym->ts)
   15120        35217 :           && (pedantic || !c_types_conform (&oarg->sym->ts, &narg->sym->ts)))
   15121              :         {
   15122           24 :           gfc_error ("Type mismatch in argument %qs at %L (%s/%s) "
   15123            8 :                      "originally declared at %L", narg->sym->name,
   15124            8 :                      nloc, gfc_typename (&narg->sym->ts),
   15125            8 :                      gfc_typename (&oarg->sym->ts), oloc);
   15126            8 :                      ret = false;
   15127            8 :                      continue;
   15128              :         }
   15129        35209 :       if (oarg->sym->attr.value != narg->sym->attr.value)
   15130              :         {
   15131            1 :           gfc_error ("VALUE attribute mismatch in argument %qs at %L "
   15132              :                      "originally declared at %L",narg->sym->name,
   15133              :                      nloc, oloc);
   15134            1 :           ret = false;
   15135            1 :           continue;
   15136              :         }
   15137              : 
   15138              :       /* According to the Fortran standard, ranks have to match for arguments.
   15139              :          In this case, this makes little sense because both decay to C
   15140              :          pointers.  Only issue an error if -pedantic or if the argument does
   15141              :          not decay to a pointer.  Same thing for CFI_desc arrays, which include
   15142              :          assumed rank.  */
   15143              : 
   15144        35208 :       int orank = gfc_symbol_rank (oarg->sym);
   15145        35208 :       int nrank = gfc_symbol_rank (narg->sym);
   15146        35208 :       if (orank != nrank && pedantic)
   15147              :         {
   15148            1 :           gfc_error ("Rank mismatch in argument %qs (%d/%d) at %L originally "
   15149            1 :                      "declared at %L", narg->sym->name, nrank, orank,  nloc,
   15150              :                      oloc);
   15151            1 :           ret = false;
   15152            1 :           continue;
   15153              :         }
   15154              : 
   15155              :       /* Confusion between CFI_desc and "normal" arrays.  */
   15156              : 
   15157        35207 :       if (decays_to_pointer (oarg->sym) != decays_to_pointer (narg->sym))
   15158              :         {
   15159            1 :           gfc_error ("Array specification mismatch in argument %qs at %L "
   15160              :                      "originally declared at %L", narg->sym->name,
   15161              :                      nloc, oloc);
   15162            1 :           ret = false;
   15163            1 :           continue;
   15164              :         }
   15165              :     }
   15166              : 
   15167        12874 :   if (oarg && !narg)
   15168              :     {
   15169            0 :       gfc_error ("Not enough arguments for procedure %qs with binding label "
   15170              :                  "%qs after %L, originally declared at %L", nsym->name,
   15171            0 :                  nsym->binding_label, nloc, &oarg->sym->declared_at);
   15172            0 :       ret = false;
   15173              :     }
   15174              : 
   15175        12874 :   if (!oarg && narg)
   15176              :     {
   15177            2 :       gfc_error ("Too many arguments for procedure %qs with binding label "
   15178              :                  "%qs at %L, originally declared at %L", nsym->name,
   15179            2 :                  nsym->binding_label, &narg->sym->declared_at, oloc);
   15180            2 :       ret = false;
   15181              :     }
   15182              : 
   15183        12874 :   return ret;
   15184              : }
   15185              : 
   15186              : 
   15187              : /* Verify that any binding labels used in a given namespace do not collide
   15188              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   15189              :    for the same procedure are permitted.  Abstract interfaces and dummy
   15190              :    arguments are not checked.  */
   15191              : 
   15192              : static void
   15193      1947043 : gfc_verify_binding_labels (gfc_symbol *sym)
   15194              : {
   15195      1947043 :   gfc_gsymbol *gsym;
   15196      1947043 :   const char *module;
   15197              : 
   15198      1947043 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   15199        70668 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   15200        41842 :       || sym->attr.abstract || sym->attr.dummy)
   15201              :     return;
   15202              : 
   15203              :   /* Avoid double error reporting.  */
   15204        41706 :   if (sym->error)
   15205              :     return;
   15206              : 
   15207              :   /* TODO: Check the names of reserved external C identifiers here, see
   15208              :      PR 125251.  */
   15209              : 
   15210              :   /* According to the Fortran standard, global identifiers are case
   15211              :      insensitive, which also holds for C identifiers.  This was probably done
   15212              :      for systems which had case-insensitive linkers.  Such systems could not
   15213              :      accommodate the C standards referenced, so this restriction makes little
   15214              :      sense for modern systems. Therefore, check case-sensitive labels unless
   15215              :      -pedantic is in force.  */
   15216              : 
   15217        41706 :   if (pedantic)
   15218         4659 :     gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   15219              :   else
   15220        37047 :     gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
   15221              : 
   15222        41706 :   if (sym->module)
   15223              :     module = sym->module;
   15224        13129 :   else if (sym->ns && sym->ns->proc_name
   15225        13129 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   15226         4587 :     module = sym->ns->proc_name->name;
   15227         8542 :   else if (sym->ns && sym->ns->parent
   15228          358 :            && sym->ns && sym->ns->parent->proc_name
   15229          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15230          272 :     module = sym->ns->parent->proc_name->name;
   15231              :   else
   15232              :     module = NULL;
   15233              : 
   15234        41706 :   if (gsym)
   15235              :     {
   15236        12918 :       if (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)
   15237              :         {
   15238        12877 :           gfc_symbol *global_sym;
   15239        12877 :           gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &global_sym);
   15240              : 
   15241              :           /* For when the symtree does not match the symbol name, which can happen
   15242              :              in modules with PRIVATE.  */
   15243              : 
   15244        12877 :           if (global_sym == NULL)
   15245            1 :             gfc_find_symbol_by_name (gsym->sym_name, gsym->ns, &global_sym);
   15246              : 
   15247        12877 :           gcc_assert (global_sym);
   15248              : 
   15249              :           /* If subroutines and functions are conflated, there is little point
   15250              :              in continuing checks.  */
   15251        12877 :           if ((sym->attr.function && gsym->type == GSYM_SUBROUTINE)
   15252        12877 :               || (sym->attr.subroutine && gsym->type == GSYM_FUNCTION))
   15253              :             {
   15254            1 :               gfc_global_used (gsym, &sym->declared_at);
   15255            1 :               sym->binding_label = NULL;
   15256            1 :               sym->error = 1;
   15257           13 :               return;
   15258              :             }
   15259              : 
   15260         7242 :           if (gsym->type == GSYM_FUNCTION && sym->attr.function
   15261        20118 :               && !gfc_compare_types (&sym->ts, &global_sym->ts))
   15262              :             {
   15263            2 :               gfc_error ("Return type mismatch of function %qs with binding "
   15264              :                          "label %qs at %L (%s/%s), originally declared at %L",
   15265              :                          sym->name, sym->binding_label,
   15266              :                          &sym->declared_at,
   15267              :                          gfc_typename (&sym->ts),
   15268            2 :                          gfc_typename (&global_sym->ts),
   15269              :                          &gsym->where);
   15270            2 :               sym->binding_label = NULL;
   15271            2 :               sym->error = 1;
   15272            2 :               return;
   15273              :             }
   15274        12874 :           if (!compare_c_binding_arglists (global_sym, sym))
   15275              :             {
   15276           10 :               sym->binding_label = NULL;
   15277           10 :               sym->error = 1;
   15278           10 :               return;
   15279              :             }
   15280              :         }
   15281              :     }
   15282              : 
   15283        12864 :   if (!gsym
   15284        12905 :       || (!gsym->defined
   15285         9955 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   15286              :     {
   15287        28788 :       if (!gsym)
   15288        28788 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   15289        38743 :       gsym->where = sym->declared_at;
   15290        38743 :       gsym->sym_name = sym->name;
   15291        38743 :       gsym->binding_label = sym->binding_label;
   15292        38743 :       gsym->ns = sym->ns;
   15293        38743 :       gsym->mod_name = module;
   15294        38743 :       if (sym->attr.function)
   15295        26322 :         gsym->type = GSYM_FUNCTION;
   15296        12421 :       else if (sym->attr.subroutine)
   15297        12281 :         gsym->type = GSYM_SUBROUTINE;
   15298              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   15299        38743 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   15300        38743 :       return;
   15301              :     }
   15302              : 
   15303         2950 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   15304              :     {
   15305            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   15306              :                  "identifier as entity at %L", sym->name,
   15307              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15308              :       /* Clear the binding label to prevent checking multiple times.  */
   15309            1 :       sym->binding_label = NULL;
   15310            1 :       return;
   15311              :     }
   15312              : 
   15313         2949 :   if (sym->attr.flavor == FL_VARIABLE && module
   15314           37 :       && (strcmp (module, gsym->mod_name) != 0
   15315           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   15316              :     {
   15317              :       /* This can only happen if the variable is defined in a module - if it
   15318              :          isn't the same module, reject it.  */
   15319            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   15320              :                  "uses the same global identifier as entity at %L from module %qs",
   15321              :                  sym->name, module, sym->binding_label,
   15322              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   15323            3 :       sym->binding_label = NULL;
   15324            3 :       return;
   15325              :     }
   15326              : 
   15327         2946 :   if ((sym->attr.function || sym->attr.subroutine)
   15328         2910 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   15329         2908 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   15330         2525 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   15331         2093 :       && (module != gsym->mod_name
   15332         2089 :           || strcmp (gsym->sym_name, sym->name) != 0
   15333         2089 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   15334              :     {
   15335              :       /* Print an error if the procedure is defined multiple times; we have to
   15336              :          exclude references to the same procedure via module association or
   15337              :          multiple checks for the same procedure.  */
   15338            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   15339              :                  "global identifier as entity at %L", sym->name,
   15340              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   15341            4 :       sym->binding_label = NULL;
   15342            4 :       return;
   15343              :     }
   15344              : }
   15345              : 
   15346              : 
   15347              : /* Resolve an index expression.  */
   15348              : 
   15349              : static bool
   15350       267639 : resolve_index_expr (gfc_expr *e)
   15351              : {
   15352       267639 :   if (!gfc_resolve_expr (e))
   15353              :     return false;
   15354              : 
   15355       267629 :   if (!gfc_simplify_expr (e, 0))
   15356              :     return false;
   15357              : 
   15358       267627 :   if (!gfc_specification_expr (e))
   15359              :     return false;
   15360              : 
   15361              :   return true;
   15362              : }
   15363              : 
   15364              : 
   15365              : /* Resolve a charlen structure.  */
   15366              : 
   15367              : static bool
   15368       103679 : resolve_charlen (gfc_charlen *cl)
   15369              : {
   15370       103679 :   int k;
   15371       103679 :   bool saved_specification_expr;
   15372              : 
   15373       103679 :   if (cl->resolved)
   15374              :     return true;
   15375              : 
   15376        95147 :   cl->resolved = 1;
   15377        95147 :   saved_specification_expr = specification_expr;
   15378        95147 :   specification_expr = true;
   15379              : 
   15380        95147 :   if (cl->length_from_typespec)
   15381              :     {
   15382         1484 :       if (!gfc_resolve_expr (cl->length))
   15383              :         {
   15384            1 :           specification_expr = saved_specification_expr;
   15385            1 :           return false;
   15386              :         }
   15387              : 
   15388         1483 :       if (!gfc_simplify_expr (cl->length, 0))
   15389              :         {
   15390            0 :           specification_expr = saved_specification_expr;
   15391            0 :           return false;
   15392              :         }
   15393              : 
   15394              :       /* cl->length has been resolved.  It should have an integer type.  */
   15395         1483 :       if (cl->length
   15396         1482 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15397              :         {
   15398            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15399              :                      &cl->length->where);
   15400            4 :           return false;
   15401              :         }
   15402              :     }
   15403              :   else
   15404              :     {
   15405        93663 :       if (!resolve_index_expr (cl->length))
   15406              :         {
   15407           19 :           specification_expr = saved_specification_expr;
   15408           19 :           return false;
   15409              :         }
   15410              :     }
   15411              : 
   15412              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15413              :      a negative value, the length of character entities declared is zero.  */
   15414        95123 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15415        57225 :       && mpz_sgn (cl->length->value.integer) < 0)
   15416            0 :     gfc_replace_expr (cl->length,
   15417              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15418              : 
   15419              :   /* Check that the character length is not too large.  */
   15420        95123 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15421        95123 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15422        57225 :       && cl->length->ts.type == BT_INTEGER
   15423        57225 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15424              :     {
   15425            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15426            4 :       specification_expr = saved_specification_expr;
   15427            4 :       return false;
   15428              :     }
   15429              : 
   15430        95119 :   specification_expr = saved_specification_expr;
   15431        95119 :   return true;
   15432              : }
   15433              : 
   15434              : 
   15435              : /* Test for non-constant shape arrays.  */
   15436              : 
   15437              : static bool
   15438       119158 : is_non_constant_shape_array (gfc_symbol *sym)
   15439              : {
   15440       119158 :   gfc_expr *e;
   15441       119158 :   int i;
   15442       119158 :   bool not_constant;
   15443              : 
   15444       119158 :   not_constant = false;
   15445       119158 :   if (sym->as != NULL)
   15446              :     {
   15447              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15448              :          has not been simplified; parameter array references.  Do the
   15449              :          simplification now.  */
   15450       156809 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15451              :         {
   15452        90486 :           if (i == GFC_MAX_DIMENSIONS)
   15453              :             break;
   15454              : 
   15455        90484 :           e = sym->as->lower[i];
   15456        90484 :           if (e && (!resolve_index_expr(e)
   15457        87585 :                     || !gfc_is_constant_expr (e)))
   15458              :             not_constant = true;
   15459        90484 :           e = sym->as->upper[i];
   15460        90484 :           if (e && (!resolve_index_expr(e)
   15461        86363 :                     || !gfc_is_constant_expr (e)))
   15462              :             not_constant = true;
   15463              :         }
   15464              :     }
   15465       119158 :   return not_constant;
   15466              : }
   15467              : 
   15468              : /* Given a symbol and an initialization expression, add code to initialize
   15469              :    the symbol to the function entry.  */
   15470              : static void
   15471         2204 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15472              : {
   15473         2204 :   gfc_expr *lval;
   15474         2204 :   gfc_code *init_st;
   15475         2204 :   gfc_namespace *ns = sym->ns;
   15476              : 
   15477         2204 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15478              :     {
   15479           46 :       gfc_free_expr (init);
   15480           46 :       return;
   15481              :     }
   15482              : 
   15483              :   /* Search for the function namespace if this is a contained
   15484              :      function without an explicit result.  */
   15485         2158 :   if (sym->attr.function && sym == sym->result
   15486          299 :       && sym->name != sym->ns->proc_name->name)
   15487              :     {
   15488          298 :       ns = ns->contained;
   15489         1376 :       for (;ns; ns = ns->sibling)
   15490         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15491              :           break;
   15492              :     }
   15493              : 
   15494         2158 :   if (ns == NULL)
   15495              :     {
   15496           61 :       gfc_free_expr (init);
   15497           61 :       return;
   15498              :     }
   15499              : 
   15500              :   /* Build an l-value expression for the result.  */
   15501         2097 :   lval = gfc_lval_expr_from_sym (sym);
   15502              : 
   15503              :   /* Add the code at scope entry.  */
   15504         2097 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15505         2097 :   init_st->next = ns->code;
   15506         2097 :   ns->code = init_st;
   15507              : 
   15508              :   /* Assign the default initializer to the l-value.  */
   15509         2097 :   init_st->loc = sym->declared_at;
   15510         2097 :   init_st->expr1 = lval;
   15511         2097 :   init_st->expr2 = init;
   15512              : }
   15513              : 
   15514              : 
   15515              : /* Whether or not we can generate a default initializer for a symbol.  */
   15516              : 
   15517              : static bool
   15518        31032 : can_generate_init (gfc_symbol *sym)
   15519              : {
   15520        31032 :   symbol_attribute *a;
   15521        31032 :   if (!sym)
   15522              :     return false;
   15523        31032 :   a = &sym->attr;
   15524              : 
   15525              :   /* These symbols should never have a default initialization.  */
   15526        51090 :   return !(
   15527        31032 :        a->allocatable
   15528        31032 :     || a->external
   15529        29853 :     || a->pointer
   15530        29853 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15531         5995 :         && (CLASS_DATA (sym)->attr.class_pointer
   15532         3977 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15533        27835 :     || a->in_equivalence
   15534        27714 :     || a->in_common
   15535        27667 :     || a->data
   15536        27489 :     || sym->module
   15537        23594 :     || a->cray_pointee
   15538        23532 :     || a->cray_pointer
   15539        23532 :     || sym->assoc
   15540        20739 :     || (!a->referenced && !a->result)
   15541        20058 :     || (a->dummy && (a->intent != INTENT_OUT
   15542         1129 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15543        20058 :     || (a->function && sym != sym->result)
   15544              :   );
   15545              : }
   15546              : 
   15547              : 
   15548              : /* Assign the default initializer to a derived type variable or result.  */
   15549              : 
   15550              : static void
   15551        11829 : apply_default_init (gfc_symbol *sym)
   15552              : {
   15553        11829 :   gfc_expr *init = NULL;
   15554              : 
   15555        11829 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15556              :     return;
   15557              : 
   15558        11584 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15559        10671 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15560              : 
   15561        11584 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15562              :     return;
   15563              : 
   15564         1822 :   build_init_assign (sym, init);
   15565         1822 :   sym->attr.referenced = 1;
   15566              : }
   15567              : 
   15568              : 
   15569              : /* Build an initializer for a local. Returns null if the symbol should not have
   15570              :    a default initialization.  */
   15571              : 
   15572              : static gfc_expr *
   15573       208119 : build_default_init_expr (gfc_symbol *sym)
   15574              : {
   15575              :   /* These symbols should never have a default initialization.  */
   15576       208119 :   if (sym->attr.allocatable
   15577       194213 :       || sym->attr.external
   15578       194213 :       || sym->attr.dummy
   15579       127388 :       || sym->attr.pointer
   15580       119102 :       || sym->attr.in_equivalence
   15581       116726 :       || sym->attr.in_common
   15582       113624 :       || sym->attr.data
   15583       111326 :       || sym->module
   15584       108676 :       || sym->attr.cray_pointee
   15585       108375 :       || sym->attr.cray_pointer
   15586       108073 :       || sym->assoc)
   15587              :     return NULL;
   15588              : 
   15589              :   /* Get the appropriate init expression.  */
   15590       103153 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15591              : }
   15592              : 
   15593              : /* Add an initialization expression to a local variable.  */
   15594              : static void
   15595       208119 : apply_default_init_local (gfc_symbol *sym)
   15596              : {
   15597       208119 :   gfc_expr *init = NULL;
   15598              : 
   15599              :   /* The symbol should be a variable or a function return value.  */
   15600       208119 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15601       208119 :       || (sym->attr.function && sym->result != sym))
   15602              :     return;
   15603              : 
   15604              :   /* Try to build the initializer expression.  If we can't initialize
   15605              :      this symbol, then init will be NULL.  */
   15606       208119 :   init = build_default_init_expr (sym);
   15607       208119 :   if (init == NULL)
   15608              :     return;
   15609              : 
   15610              :   /* For saved variables, we don't want to add an initializer at function
   15611              :      entry, so we just add a static initializer. Note that automatic variables
   15612              :      are stack allocated even with -fno-automatic; we have also to exclude
   15613              :      result variable, which are also nonstatic.  */
   15614          419 :   if (!sym->attr.automatic
   15615          419 :       && (sym->attr.save || sym->ns->save_all
   15616          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15617           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15618           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15619              :     {
   15620              :       /* Don't clobber an existing initializer!  */
   15621           37 :       gcc_assert (sym->value == NULL);
   15622           37 :       sym->value = init;
   15623           37 :       return;
   15624              :     }
   15625              : 
   15626          382 :   build_init_assign (sym, init);
   15627              : }
   15628              : 
   15629              : 
   15630              : /* Resolution of common features of flavors variable and procedure.  */
   15631              : 
   15632              : static bool
   15633      1009693 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15634              : {
   15635      1009693 :   gfc_array_spec *as;
   15636              : 
   15637      1009693 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15638        20124 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15639        20119 :     as = CLASS_DATA (sym)->as;
   15640              :   else
   15641       989574 :     as = sym->as;
   15642              : 
   15643              :   /* Constraints on deferred shape variable.  */
   15644      1009693 :   if (as == NULL || as->type != AS_DEFERRED)
   15645              :     {
   15646       984896 :       bool pointer, allocatable, dimension;
   15647              : 
   15648       984896 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15649        16798 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15650              :         {
   15651        16793 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15652        16793 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15653        16793 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15654              :         }
   15655              :       else
   15656              :         {
   15657       968103 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   15658       968103 :           allocatable = sym->attr.allocatable;
   15659       968103 :           dimension = sym->attr.dimension;
   15660              :         }
   15661              : 
   15662       984896 :       if (allocatable)
   15663              :         {
   15664         8283 :           if (dimension
   15665         8283 :               && as
   15666          524 :               && as->type != AS_ASSUMED_RANK
   15667            5 :               && !sym->attr.select_rank_temporary)
   15668              :             {
   15669            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   15670              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   15671            3 :               return false;
   15672              :             }
   15673         8280 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   15674              :                                     "%qs at %L may not be ALLOCATABLE",
   15675              :                                     sym->name, &sym->declared_at))
   15676              :             return false;
   15677              :         }
   15678              : 
   15679       984892 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   15680              :         {
   15681            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   15682              :                      "assumed rank", sym->name, &sym->declared_at);
   15683            4 :           sym->error = 1;
   15684            4 :           return false;
   15685              :         }
   15686              :     }
   15687              :   else
   15688              :     {
   15689        24797 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   15690         4855 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   15691              :         {
   15692            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   15693              :                      sym->name, &sym->declared_at);
   15694            3 :           return false;
   15695              :          }
   15696              :     }
   15697              : 
   15698              :   /* Constraints on polymorphic variables.  */
   15699      1009682 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   15700              :     {
   15701              :       /* F03:C502.  */
   15702        19432 :       if (sym->attr.class_ok
   15703        19376 :           && sym->ts.u.derived
   15704        19371 :           && !sym->attr.select_type_temporary
   15705        18219 :           && !UNLIMITED_POLY (sym)
   15706        15541 :           && CLASS_DATA (sym)
   15707        15541 :           && CLASS_DATA (sym)->ts.u.derived
   15708        34972 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   15709              :         {
   15710            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   15711            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   15712              :                      &sym->declared_at);
   15713            5 :           return false;
   15714              :         }
   15715              : 
   15716              :       /* F03:C509.  */
   15717              :       /* Assume that use associated symbols were checked in the module ns.
   15718              :          Class-variables that are associate-names are also something special
   15719              :          and excepted from the test.  */
   15720        19427 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   15721           54 :           && !sym->attr.select_type_temporary
   15722           54 :           && !sym->attr.select_rank_temporary)
   15723              :         {
   15724           54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   15725              :                      "or pointer", sym->name, &sym->declared_at);
   15726           54 :           return false;
   15727              :         }
   15728              :     }
   15729              : 
   15730              :   return true;
   15731              : }
   15732              : 
   15733              : 
   15734              : /* Additional checks for symbols with flavor variable and derived
   15735              :    type.  To be called from resolve_fl_variable.  */
   15736              : 
   15737              : static bool
   15738        84554 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   15739              : {
   15740        84554 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   15741              : 
   15742              :   /* Check to see if a derived type is blocked from being host
   15743              :      associated by the presence of another class I symbol in the same
   15744              :      namespace.  14.6.1.3 of the standard and the discussion on
   15745              :      comp.lang.fortran.  */
   15746        84554 :   if (sym->ts.u.derived
   15747        84549 :       && sym->ns != sym->ts.u.derived->ns
   15748        48412 :       && !sym->ts.u.derived->attr.use_assoc
   15749        18042 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   15750              :     {
   15751        17053 :       gfc_symbol *s;
   15752        17053 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   15753        17053 :       if (s && s->attr.generic)
   15754            2 :         s = gfc_find_dt_in_generic (s);
   15755        17053 :       if (s && !gfc_fl_struct (s->attr.flavor))
   15756              :         {
   15757            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   15758              :                      "because it is blocked by an incompatible object "
   15759              :                      "of the same name declared at %L",
   15760            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   15761              :                      &s->declared_at);
   15762            2 :           return false;
   15763              :         }
   15764              :     }
   15765              : 
   15766              :   /* 4th constraint in section 11.3: "If an object of a type for which
   15767              :      component-initialization is specified (R429) appears in the
   15768              :      specification-part of a module and does not have the ALLOCATABLE
   15769              :      or POINTER attribute, the object shall have the SAVE attribute."
   15770              : 
   15771              :      The check for initializers is performed with
   15772              :      gfc_has_default_initializer because gfc_default_initializer generates
   15773              :      a hidden default for allocatable components.  */
   15774        83875 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   15775        19082 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   15776          423 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   15777           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   15778           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   15779        84561 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   15780              :                           "%qs at %L, needed due to the default "
   15781              :                           "initialization", sym->name, &sym->declared_at))
   15782              :     return false;
   15783              : 
   15784              :   /* Assign default initializer.  */
   15785        84550 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   15786        78169 :       && (!no_init_flag
   15787        60955 :           || (sym->attr.intent == INTENT_OUT
   15788         3321 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   15789        20361 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15790              : 
   15791              :   return true;
   15792              : }
   15793              : 
   15794              : 
   15795              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   15796              :    except in the declaration of an entity or component that has the POINTER
   15797              :    or ALLOCATABLE attribute.  */
   15798              : 
   15799              : static bool
   15800      1588722 : deferred_requirements (gfc_symbol *sym)
   15801              : {
   15802      1588722 :   if (sym->ts.deferred
   15803         8098 :       && !(sym->attr.pointer
   15804         2436 :            || sym->attr.allocatable
   15805           91 :            || sym->attr.associate_var
   15806            7 :            || sym->attr.omp_udr_artificial_var))
   15807              :     {
   15808              :       /* If a function has a result variable, only check the variable.  */
   15809            7 :       if (sym->result && sym->name != sym->result->name)
   15810              :         return true;
   15811              : 
   15812            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   15813              :                  "requires either the POINTER or ALLOCATABLE attribute",
   15814              :                  sym->name, &sym->declared_at);
   15815            6 :       return false;
   15816              :     }
   15817              :   return true;
   15818              : }
   15819              : 
   15820              : 
   15821              : /* Resolve symbols with flavor variable.  */
   15822              : 
   15823              : static bool
   15824       677547 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   15825              : {
   15826       677547 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   15827              :                                  "SAVE attribute");
   15828              : 
   15829       677547 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   15830              :     return false;
   15831              : 
   15832              :   /* Set this flag to check that variables are parameters of all entries.
   15833              :      This check is effected by the call to gfc_resolve_expr through
   15834              :      is_non_constant_shape_array.  */
   15835       677487 :   bool saved_specification_expr = specification_expr;
   15836       677487 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   15837       677487 :   specification_expr = true;
   15838       677487 :   specification_expr_symbol = sym;
   15839              : 
   15840       677487 :   if (sym->ns->proc_name
   15841       677392 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15842       672208 :           || sym->ns->proc_name->attr.is_main_program)
   15843        83872 :       && !sym->attr.use_assoc
   15844        80558 :       && !sym->attr.allocatable
   15845        74713 :       && !sym->attr.pointer
   15846       748511 :       && is_non_constant_shape_array (sym))
   15847              :     {
   15848              :       /* F08:C541. The shape of an array defined in a main program or module
   15849              :        * needs to be constant.  */
   15850            3 :       gfc_error ("The module or main program array %qs at %L must "
   15851              :                  "have constant shape", sym->name, &sym->declared_at);
   15852            3 :       specification_expr = saved_specification_expr;
   15853            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   15854            3 :       return false;
   15855              :     }
   15856              : 
   15857              :   /* Constraints on deferred type parameter.  */
   15858       677484 :   if (!deferred_requirements (sym))
   15859              :     return false;
   15860              : 
   15861       677480 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   15862              :     {
   15863              :       /* Make sure that character string variables with assumed length are
   15864              :          dummy arguments.  */
   15865        36420 :       gfc_expr *e = NULL;
   15866              : 
   15867        36420 :       if (sym->ts.u.cl)
   15868        36420 :         e = sym->ts.u.cl->length;
   15869              :       else
   15870              :         return false;
   15871              : 
   15872        36420 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   15873         2646 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   15874            2 :           && !sym->attr.omp_udr_artificial_var)
   15875              :         {
   15876            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   15877              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   15878            2 :           specification_expr = saved_specification_expr;
   15879            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15880            2 :           return false;
   15881              :         }
   15882              : 
   15883        21078 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   15884              :         {
   15885            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15886            1 :           specification_expr = saved_specification_expr;
   15887            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   15888            1 :           return false;
   15889              :         }
   15890              : 
   15891        36417 :       if (!gfc_is_constant_expr (e)
   15892        36417 :           && !(e->expr_type == EXPR_VARIABLE
   15893         1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   15894              :         {
   15895         2184 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   15896         1680 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15897         1679 :                   || sym->ns->proc_name->attr.is_main_program))
   15898              :             {
   15899            3 :               gfc_error ("%qs at %L must have constant character length "
   15900              :                         "in this context", sym->name, &sym->declared_at);
   15901            3 :               specification_expr = saved_specification_expr;
   15902            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   15903            3 :               return false;
   15904              :             }
   15905         2181 :           if (sym->attr.in_common)
   15906              :             {
   15907            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   15908              :                          "character length", sym->name, &sym->declared_at);
   15909            1 :               specification_expr = saved_specification_expr;
   15910            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   15911            1 :               return false;
   15912              :             }
   15913              :         }
   15914              :     }
   15915              : 
   15916       677473 :   if (sym->value == NULL && sym->attr.referenced
   15917       210066 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   15918       208119 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   15919              : 
   15920              :   /* Determine if the symbol may not have an initializer.  */
   15921       677473 :   int no_init_flag = 0, automatic_flag = 0;
   15922       677473 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   15923       173165 :       || sym->attr.intrinsic || sym->attr.result)
   15924              :     no_init_flag = 1;
   15925       140682 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   15926       175745 :            && is_non_constant_shape_array (sym))
   15927              :     {
   15928         1354 :       no_init_flag = automatic_flag = 1;
   15929              : 
   15930              :       /* Also, they must not have the SAVE attribute.
   15931              :          SAVE_IMPLICIT is checked below.  */
   15932         1354 :       if (sym->as && sym->attr.codimension)
   15933              :         {
   15934            7 :           int corank = sym->as->corank;
   15935            7 :           sym->as->corank = 0;
   15936            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   15937            7 :           sym->as->corank = corank;
   15938              :         }
   15939         1354 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   15940              :         {
   15941            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15942            2 :           specification_expr = saved_specification_expr;
   15943            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15944            2 :           return false;
   15945              :         }
   15946              :     }
   15947              : 
   15948              :   /* Ensure that any initializer is simplified.  */
   15949       677471 :   if (sym->value)
   15950         8318 :     gfc_simplify_expr (sym->value, 1);
   15951              : 
   15952              :   /* Reject illegal initializers.  */
   15953       677471 :   if (!sym->mark && sym->value)
   15954              :     {
   15955         8318 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   15956           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   15957            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   15958              :                    sym->name, &sym->declared_at);
   15959         8317 :       else if (sym->attr.external)
   15960            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   15961              :                    sym->name, &sym->declared_at);
   15962         8317 :       else if (sym->attr.dummy)
   15963            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   15964              :                    sym->name, &sym->declared_at);
   15965         8314 :       else if (sym->attr.intrinsic)
   15966            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   15967              :                    sym->name, &sym->declared_at);
   15968         8314 :       else if (sym->attr.result)
   15969            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   15970              :                    sym->name, &sym->declared_at);
   15971         8313 :       else if (automatic_flag)
   15972            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   15973              :                    sym->name, &sym->declared_at);
   15974              :       else
   15975         8308 :         goto no_init_error;
   15976           10 :       specification_expr = saved_specification_expr;
   15977           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   15978           10 :       return false;
   15979              :     }
   15980              : 
   15981       669153 : no_init_error:
   15982       677461 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   15983              :     {
   15984        84554 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   15985        84554 :       specification_expr = saved_specification_expr;
   15986        84554 :       specification_expr_symbol = saved_specification_expr_symbol;
   15987        84554 :       return res;
   15988              :     }
   15989              : 
   15990       592907 :   specification_expr = saved_specification_expr;
   15991       592907 :   specification_expr_symbol = saved_specification_expr_symbol;
   15992       592907 :   return true;
   15993              : }
   15994              : 
   15995              : 
   15996              : /* Compare the dummy characteristics of a module procedure interface
   15997              :    declaration with the corresponding declaration in a submodule.  */
   15998              : static gfc_formal_arglist *new_formal;
   15999              : static char errmsg[200];
   16000              : 
   16001              : static void
   16002         1352 : compare_fsyms (gfc_symbol *sym)
   16003              : {
   16004         1352 :   gfc_symbol *fsym;
   16005              : 
   16006         1352 :   if (sym == NULL || new_formal == NULL)
   16007              :     return;
   16008              : 
   16009         1352 :   fsym = new_formal->sym;
   16010              : 
   16011         1352 :   if (sym == fsym)
   16012              :     return;
   16013              : 
   16014         1328 :   if (strcmp (sym->name, fsym->name) == 0)
   16015              :     {
   16016          523 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   16017            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   16018              :     }
   16019              : }
   16020              : 
   16021              : 
   16022              : /* Resolve a procedure.  */
   16023              : 
   16024              : static bool
   16025       500077 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   16026              : {
   16027       500077 :   gfc_formal_arglist *arg;
   16028       500077 :   bool allocatable_or_pointer = false;
   16029              : 
   16030       500077 :   if (sym->attr.function
   16031       500077 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   16032              :     return false;
   16033              : 
   16034              :   /* Constraints on deferred type parameter.  */
   16035       500067 :   if (!deferred_requirements (sym))
   16036              :     return false;
   16037              : 
   16038       500066 :   if (sym->ts.type == BT_CHARACTER)
   16039              :     {
   16040        11958 :       gfc_charlen *cl = sym->ts.u.cl;
   16041              : 
   16042         7734 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   16043        13265 :              && !resolve_charlen (cl))
   16044              :         return false;
   16045              : 
   16046        11957 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   16047        10651 :           && sym->attr.proc == PROC_ST_FUNCTION)
   16048              :         {
   16049            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   16050              :                      "have constant length", sym->name, &sym->declared_at);
   16051            0 :           return false;
   16052              :         }
   16053              :     }
   16054              : 
   16055              :   /* Ensure that derived type for are not of a private type.  Internal
   16056              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   16057              :      externally accessible and can access all the objects accessible in
   16058              :      the host.  */
   16059       115387 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   16060       115387 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   16061       589046 :       && gfc_check_symbol_access (sym))
   16062              :     {
   16063       466366 :       gfc_interface *iface;
   16064              : 
   16065       994009 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   16066              :         {
   16067       527644 :           if (arg->sym
   16068       527504 :               && arg->sym->ts.type == BT_DERIVED
   16069        43674 :               && arg->sym->ts.u.derived
   16070        43674 :               && !arg->sym->ts.u.derived->attr.use_assoc
   16071         4302 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16072       527653 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   16073              :                                   "and cannot be a dummy argument"
   16074              :                                   " of %qs, which is PUBLIC at %L",
   16075            9 :                                   arg->sym->name, sym->name,
   16076              :                                   &sym->declared_at))
   16077              :             {
   16078              :               /* Stop this message from recurring.  */
   16079            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16080            1 :               return false;
   16081              :             }
   16082              :         }
   16083              : 
   16084              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   16085              :          PRIVATE to the containing module.  */
   16086       663166 :       for (iface = sym->generic; iface; iface = iface->next)
   16087              :         {
   16088       462791 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   16089              :             {
   16090       265990 :               if (arg->sym
   16091       265958 :                   && arg->sym->ts.type == BT_DERIVED
   16092         8033 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   16093          232 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   16094       265994 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   16095              :                                       "PUBLIC interface %qs at %L "
   16096              :                                       "takes dummy arguments of %qs which "
   16097              :                                       "is PRIVATE", iface->sym->name,
   16098            4 :                                       sym->name, &iface->sym->declared_at,
   16099            4 :                                       gfc_typename(&arg->sym->ts)))
   16100              :                 {
   16101              :                   /* Stop this message from recurring.  */
   16102            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   16103            1 :                   return false;
   16104              :                 }
   16105              :              }
   16106              :         }
   16107              :     }
   16108              : 
   16109       500063 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   16110           86 :       && !sym->attr.proc_pointer)
   16111              :     {
   16112            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   16113              :                  sym->name, &sym->declared_at);
   16114              : 
   16115              :       /* Make sure no second error is issued for this.  */
   16116            2 :       sym->value->error = 1;
   16117            2 :       return false;
   16118              :     }
   16119              : 
   16120              :   /* An external symbol may not have an initializer because it is taken to be
   16121              :      a procedure. Exception: Procedure Pointers.  */
   16122       500061 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   16123              :     {
   16124            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   16125              :                  sym->name, &sym->declared_at);
   16126            0 :       return false;
   16127              :     }
   16128              : 
   16129              :   /* An elemental function is required to return a scalar 12.7.1  */
   16130       500061 :   if (sym->attr.elemental && sym->attr.function
   16131        86536 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   16132            2 :                       && CLASS_DATA (sym)->as)))
   16133              :     {
   16134            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   16135              :                  "result", sym->name, &sym->declared_at);
   16136              :       /* Reset so that the error only occurs once.  */
   16137            3 :       sym->attr.elemental = 0;
   16138            3 :       return false;
   16139              :     }
   16140              : 
   16141       500058 :   if (sym->attr.proc == PROC_ST_FUNCTION
   16142          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   16143              :     {
   16144            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   16145              :                  "allocatable attribute", sym->name, &sym->declared_at);
   16146            2 :       return false;
   16147              :     }
   16148              : 
   16149              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   16150              :      char-len-param shall not be array-valued, pointer-valued, recursive
   16151              :      or pure.  ....snip... A character value of * may only be used in the
   16152              :      following ways: (i) Dummy arg of procedure - dummy associates with
   16153              :      actual length; (ii) To declare a named constant; or (iii) External
   16154              :      function - but length must be declared in calling scoping unit.  */
   16155       500056 :   if (sym->attr.function
   16156       332127 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   16157         6829 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   16158              :     {
   16159          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   16160          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   16161              :         {
   16162            4 :           if (sym->as && sym->as->rank)
   16163            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16164              :                        "array-valued", sym->name, &sym->declared_at);
   16165              : 
   16166            4 :           if (sym->attr.pointer)
   16167            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16168              :                        "pointer-valued", sym->name, &sym->declared_at);
   16169              : 
   16170            4 :           if (sym->attr.pure)
   16171            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16172              :                        "pure", sym->name, &sym->declared_at);
   16173              : 
   16174            4 :           if (sym->attr.recursive)
   16175            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   16176              :                        "recursive", sym->name, &sym->declared_at);
   16177              : 
   16178              :           return false;
   16179              :         }
   16180              : 
   16181              :       /* Appendix B.2 of the standard.  Contained functions give an
   16182              :          error anyway.  Deferred character length is an F2003 feature.
   16183              :          Don't warn on intrinsic conversion functions, which start
   16184              :          with two underscores.  */
   16185          176 :       if (!sym->attr.contained && !sym->ts.deferred
   16186          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   16187          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   16188              :                         "CHARACTER(*) function %qs at %L",
   16189              :                         sym->name, &sym->declared_at);
   16190              :     }
   16191              : 
   16192              :   /* F2008, C1218.  */
   16193       500052 :   if (sym->attr.elemental)
   16194              :     {
   16195        89838 :       if (sym->attr.proc_pointer)
   16196              :         {
   16197            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   16198              :                                                : sym->name);
   16199            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   16200              :                      name, &sym->declared_at);
   16201            7 :           return false;
   16202              :         }
   16203        89831 :       if (sym->attr.dummy)
   16204              :         {
   16205            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   16206              :                      sym->name, &sym->declared_at);
   16207            3 :           return false;
   16208              :         }
   16209              :     }
   16210              : 
   16211              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   16212              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   16213              :      pointer is tested and caught elsewhere.  */
   16214       500042 :   if (sym->result)
   16215       279448 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   16216       279448 :                              && CLASS_DATA (sym->result) ?
   16217         1696 :                              (CLASS_DATA (sym->result)->attr.allocatable
   16218         1696 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   16219       277752 :                              (sym->result->attr.allocatable
   16220       277752 :                               || sym->result->attr.pointer);
   16221              : 
   16222       500042 :   if (sym->attr.elemental && sym->result
   16223        86153 :       && allocatable_or_pointer)
   16224              :     {
   16225            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   16226              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   16227              :                  "attribute", sym->result->name,
   16228              :                  &sym->result->declared_at, sym->name);
   16229            4 :       return false;
   16230              :     }
   16231              : 
   16232              :   /* F2018:C1585: "The function result of a pure function shall not be both
   16233              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   16234              :      component."  */
   16235       500038 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   16236              :     {
   16237         2520 :       if (sym->ts.type == BT_CLASS
   16238            5 :           && sym->attr.class_ok
   16239            4 :           && CLASS_DATA (sym->result)
   16240            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   16241              :         {
   16242            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   16243              :                      "polymorphic allocatable",
   16244              :                      sym->result->name, &sym->result->declared_at);
   16245            4 :           return false;
   16246              :         }
   16247              : 
   16248         2516 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   16249              :         {
   16250              :           gfc_component *c = sym->ts.u.derived->components;
   16251         4613 :           for (; c; c = c->next)
   16252         2406 :             if (c->ts.type == BT_CLASS
   16253            2 :                 && CLASS_DATA (c)
   16254            2 :                 && CLASS_DATA (c)->attr.allocatable)
   16255              :               {
   16256            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   16257              :                            "polymorphic allocatable component %qs",
   16258              :                            sym->result->name, &sym->result->declared_at,
   16259              :                            c->name);
   16260            2 :                 return false;
   16261              :               }
   16262              :         }
   16263              :     }
   16264              : 
   16265       500032 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   16266              :     {
   16267         7236 :       gfc_formal_arglist *curr_arg;
   16268         7236 :       int has_non_interop_arg = 0;
   16269              : 
   16270         7236 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   16271         7236 :                               sym->common_block))
   16272              :         {
   16273              :           /* Clear these to prevent looking at them again if there was an
   16274              :              error.  */
   16275            2 :           sym->attr.is_bind_c = 0;
   16276            2 :           sym->attr.is_c_interop = 0;
   16277            2 :           sym->ts.is_c_interop = 0;
   16278              :         }
   16279              :       else
   16280              :         {
   16281              :           /* So far, no errors have been found.  */
   16282              :           sym->attr.is_c_interop = 1;
   16283              :           sym->ts.is_c_interop = 1;
   16284              :         }
   16285              : 
   16286         7236 :       curr_arg = gfc_sym_get_dummy_args (sym);
   16287        31845 :       while (curr_arg != NULL)
   16288              :         {
   16289              :           /* Skip implicitly typed dummy args here.  */
   16290        17373 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   16291        17316 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   16292              :               /* If something is found to fail, record the fact so we
   16293              :                  can mark the symbol for the procedure as not being
   16294              :                  BIND(C) to try and prevent multiple errors being
   16295              :                  reported.  */
   16296        17373 :               has_non_interop_arg = 1;
   16297              : 
   16298        17373 :           curr_arg = curr_arg->next;
   16299              :         }
   16300              : 
   16301              :       /* See if any of the arguments were not interoperable and if so, clear
   16302              :          the procedure symbol to prevent duplicate error messages.  */
   16303         7236 :       if (has_non_interop_arg != 0)
   16304              :         {
   16305          128 :           sym->attr.is_c_interop = 0;
   16306          128 :           sym->ts.is_c_interop = 0;
   16307          128 :           sym->attr.is_bind_c = 0;
   16308              :         }
   16309              :     }
   16310              : 
   16311       500032 :   if (!sym->attr.proc_pointer)
   16312              :     {
   16313       498925 :       if (sym->attr.save == SAVE_EXPLICIT)
   16314              :         {
   16315            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   16316              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16317            5 :           return false;
   16318              :         }
   16319       498920 :       if (sym->attr.intent)
   16320              :         {
   16321            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   16322              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16323            1 :           return false;
   16324              :         }
   16325       498919 :       if (sym->attr.subroutine && sym->attr.result)
   16326              :         {
   16327            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   16328            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   16329            2 :           return false;
   16330              :         }
   16331       498917 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   16332       144998 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   16333       144995 :               || sym->attr.contained))
   16334              :         {
   16335            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   16336              :                      "in %qs at %L", sym->name, &sym->declared_at);
   16337            3 :           return false;
   16338              :         }
   16339       498914 :       if (strcmp ("ppr@", sym->name) == 0)
   16340              :         {
   16341            0 :           gfc_error ("Procedure pointer result %qs at %L "
   16342              :                      "is missing the pointer attribute",
   16343            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   16344            0 :           return false;
   16345              :         }
   16346              :     }
   16347              : 
   16348              :   /* Assume that a procedure whose body is not known has references
   16349              :      to external arrays.  */
   16350       500021 :   if (sym->attr.if_source != IFSRC_DECL)
   16351       344738 :     sym->attr.array_outer_dependency = 1;
   16352              : 
   16353              :   /* Compare the characteristics of a module procedure with the
   16354              :      interface declaration. Ideally this would be done with
   16355              :      gfc_compare_interfaces but, at present, the formal interface
   16356              :      cannot be copied to the ts.interface.  */
   16357       500021 :   if (sym->attr.module_procedure
   16358         1615 :       && sym->attr.if_source == IFSRC_DECL)
   16359              :     {
   16360          659 :       gfc_symbol *iface;
   16361          659 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   16362          659 :       char *module_name;
   16363          659 :       char *submodule_name;
   16364          659 :       strcpy (name, sym->ns->proc_name->name);
   16365          659 :       module_name = strtok (name, ".");
   16366          659 :       submodule_name = strtok (NULL, ".");
   16367              : 
   16368          659 :       iface = sym->tlink;
   16369          659 :       sym->tlink = NULL;
   16370              : 
   16371              :       /* Make sure that the result uses the correct charlen for deferred
   16372              :          length results.  */
   16373          659 :       if (iface && sym->result
   16374          192 :           && iface->ts.type == BT_CHARACTER
   16375           19 :           && iface->ts.deferred)
   16376            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16377              : 
   16378            6 :       if (iface == NULL)
   16379          196 :         goto check_formal;
   16380              : 
   16381              :       /* Check the procedure characteristics.  */
   16382          463 :       if (sym->attr.elemental != iface->attr.elemental)
   16383              :         {
   16384            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16385              :                      "PROCEDURE at %L and its interface in %s",
   16386              :                      &sym->declared_at, module_name);
   16387           10 :           return false;
   16388              :         }
   16389              : 
   16390          462 :       if (sym->attr.pure != iface->attr.pure)
   16391              :         {
   16392            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16393              :                      "PROCEDURE at %L and its interface in %s",
   16394              :                      &sym->declared_at, module_name);
   16395            2 :           return false;
   16396              :         }
   16397              : 
   16398          460 :       if (sym->attr.recursive != iface->attr.recursive)
   16399              :         {
   16400            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16401              :                      "PROCEDURE at %L and its interface in %s",
   16402              :                      &sym->declared_at, module_name);
   16403            2 :           return false;
   16404              :         }
   16405              : 
   16406              :       /* Check the result characteristics.  */
   16407          458 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16408              :         {
   16409            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16410              :                      "in MODULE %qs and the declaration at %L in "
   16411              :                      "(SUB)MODULE %qs",
   16412              :                      errmsg, module_name, &sym->declared_at,
   16413              :                      submodule_name ? submodule_name : module_name);
   16414            5 :           return false;
   16415              :         }
   16416              : 
   16417          453 : check_formal:
   16418              :       /* Check the characteristics of the formal arguments.  */
   16419          649 :       if (sym->formal && sym->formal_ns)
   16420              :         {
   16421         1260 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16422              :             {
   16423          722 :               new_formal = arg;
   16424          722 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16425              :             }
   16426              :         }
   16427              :     }
   16428              : 
   16429              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16430              :      BIND(C) attribute.  */
   16431       500011 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16432              :     {
   16433            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16434              :                  sym->name, &sym->declared_at);
   16435            1 :       return false;
   16436              :     }
   16437              : 
   16438              :   return true;
   16439              : }
   16440              : 
   16441              : 
   16442              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16443              :    been defined and we now know their defined arguments, check that they fulfill
   16444              :    the requirements of the standard for procedures used as finalizers.  */
   16445              : 
   16446              : static bool
   16447       115805 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16448              : {
   16449       115805 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16450       115805 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16451       115805 :   bool result = true;
   16452       115805 :   bool seen_scalar = false;
   16453       115805 :   gfc_symbol *vtab;
   16454       115805 :   gfc_component *c;
   16455       115805 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16456              : 
   16457       115805 :   if (parent)
   16458        16123 :     gfc_resolve_finalizers (parent, finalizable);
   16459              : 
   16460              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16461       115805 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16462       364230 :   for (c = derived->components; c; c = c->next)
   16463       248425 :     if (c->ts.type == BT_DERIVED
   16464        70091 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16465              :       {
   16466         8718 :         bool has_final2 = false;
   16467         8718 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16468            0 :           return false;  /* Error.  */
   16469         8718 :         has_final = has_final || has_final2;
   16470              :       }
   16471              :   /* Return early if not finalizable.  */
   16472       115805 :   if (!has_final)
   16473              :     {
   16474       113060 :       if (finalizable)
   16475         8608 :         *finalizable = false;
   16476              :       return true;
   16477              :     }
   16478              : 
   16479              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16480              :      the template. If the finalizers field has the same value, it needs to be
   16481              :      supplied with finalizers of the same pdt_type.  */
   16482         2745 :   if (derived->attr.pdt_type
   16483           54 :       && derived->template_sym
   16484           24 :       && derived->template_sym->f2k_derived
   16485           24 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16486         2769 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16487              :     {
   16488           24 :       gfc_finalizer *tmp = NULL;
   16489           24 :       derived->f2k_derived->finalizers = NULL;
   16490           24 :       prev_link = &derived->f2k_derived->finalizers;
   16491           84 :       for (list = pdt_finalizers; list; list = list->next)
   16492              :         {
   16493           60 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16494           60 :           if (args->sym
   16495           60 :               && args->sym->ts.type == BT_DERIVED
   16496           60 :               && args->sym->ts.u.derived
   16497           60 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16498              :             {
   16499           36 :               tmp = gfc_get_finalizer ();
   16500           36 :               *tmp = *list;
   16501           36 :               tmp->next = NULL;
   16502           36 :               *prev_link = tmp;
   16503           36 :               prev_link = &(tmp->next);
   16504           36 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16505              :             }
   16506              :         }
   16507              :     }
   16508              : 
   16509              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16510              :      does not fit in with the standard's definition, print an error and remove
   16511              :      it from the list.  */
   16512         2745 :   prev_link = &derived->f2k_derived->finalizers;
   16513         5638 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16514              :     {
   16515         2893 :       gfc_formal_arglist *dummy_args;
   16516         2893 :       gfc_symbol* arg;
   16517         2893 :       gfc_finalizer* i;
   16518         2893 :       int my_rank;
   16519              : 
   16520              :       /* Skip this finalizer if we already resolved it.  */
   16521         2893 :       if (list->proc_tree)
   16522              :         {
   16523         2324 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16524          602 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16525         1722 :             seen_scalar = true;
   16526         2324 :           prev_link = &(list->next);
   16527         2324 :           continue;
   16528              :         }
   16529              : 
   16530              :       /* Check this exists and is a SUBROUTINE.  */
   16531          569 :       if (!list->proc_sym->attr.subroutine)
   16532              :         {
   16533            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16534              :                      list->proc_sym->name, &list->where);
   16535            3 :           goto error;
   16536              :         }
   16537              : 
   16538              :       /* We should have exactly one argument.  */
   16539          566 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16540          566 :       if (!dummy_args || dummy_args->next)
   16541              :         {
   16542            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16543              :                      &list->where);
   16544            2 :           goto error;
   16545              :         }
   16546          564 :       arg = dummy_args->sym;
   16547              : 
   16548          564 :       if (!arg)
   16549              :         {
   16550            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16551            1 :                      &list->proc_sym->declared_at, derived->name);
   16552            1 :           goto error;
   16553              :         }
   16554              : 
   16555          563 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16556            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16557              :         {
   16558            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16559              :                      "be the only finalizer with the same kind/type "
   16560              :                      "(F2018: C790)", &list->where);
   16561            0 :           goto error;
   16562              :         }
   16563              : 
   16564              :       /* This argument must be of our type.  */
   16565          563 :       if (!derived->attr.pdt_template
   16566          551 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16567              :         {
   16568            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16569              :                      &arg->declared_at, derived->name);
   16570            2 :           goto error;
   16571              :         }
   16572              : 
   16573              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16574          561 :       if (arg->attr.pointer)
   16575              :         {
   16576            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16577              :                      &arg->declared_at);
   16578            1 :           goto error;
   16579              :         }
   16580          560 :       if (arg->attr.allocatable)
   16581              :         {
   16582            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16583              :                      " ALLOCATABLE", &arg->declared_at);
   16584            1 :           goto error;
   16585              :         }
   16586          559 :       if (arg->attr.optional)
   16587              :         {
   16588            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16589              :                      &arg->declared_at);
   16590            1 :           goto error;
   16591              :         }
   16592              : 
   16593              :       /* It must not be INTENT(OUT).  */
   16594          558 :       if (arg->attr.intent == INTENT_OUT)
   16595              :         {
   16596            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16597              :                      " INTENT(OUT)", &arg->declared_at);
   16598            1 :           goto error;
   16599              :         }
   16600              : 
   16601              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16602          557 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16603            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16604            2 :         gfc_warning (OPT_Wsurprising,
   16605              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16606              :                      " shape argument", &arg->declared_at);
   16607              : 
   16608              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16609              :          defined earlier.  To really loop over the *earlier* declarations,
   16610              :          we need to walk the tail of the list as new ones were pushed at the
   16611              :          front.  */
   16612              :       /* TODO: Handle kind parameters once they are implemented.  */
   16613          557 :       my_rank = (arg->as ? arg->as->rank : 0);
   16614          664 :       for (i = list->next; i; i = i->next)
   16615              :         {
   16616          109 :           gfc_formal_arglist *dummy_args;
   16617              : 
   16618              :           /* Argument list might be empty; that is an error signalled earlier,
   16619              :              but we nevertheless continued resolving.  */
   16620          109 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16621          109 :           if (dummy_args && !derived->attr.pdt_template)
   16622              :             {
   16623          107 :               gfc_symbol* i_arg = dummy_args->sym;
   16624          107 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16625          107 :               if (i_rank == my_rank)
   16626              :                 {
   16627            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16628              :                              " rank (%d) as %qs",
   16629            2 :                              list->proc_sym->name, &list->where, my_rank,
   16630            2 :                              i->proc_sym->name);
   16631            2 :                   goto error;
   16632              :                 }
   16633              :             }
   16634              :         }
   16635              : 
   16636              :         /* Is this the/a scalar finalizer procedure?  */
   16637          555 :         if (my_rank == 0)
   16638          423 :           seen_scalar = true;
   16639              : 
   16640              :         /* Find the symtree for this procedure.  */
   16641          555 :         gcc_assert (!list->proc_tree);
   16642          555 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16643              : 
   16644          555 :         prev_link = &list->next;
   16645          555 :         continue;
   16646              : 
   16647              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16648              :            troubles in the future when they might fail later expectations.  */
   16649           14 : error:
   16650           14 :         i = list;
   16651           14 :         *prev_link = list->next;
   16652           14 :         gfc_free_finalizer (i);
   16653           14 :         result = false;
   16654          555 :     }
   16655              : 
   16656         2745 :   if (result == false)
   16657              :     return false;
   16658              : 
   16659              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   16660              :      were nodes in the list, must have been for arrays.  It is surely a good
   16661              :      idea to have a scalar version there if there's something to finalize.  */
   16662         2741 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   16663            1 :     gfc_warning (OPT_Wsurprising,
   16664              :                  "Only array FINAL procedures declared for derived type %qs"
   16665              :                  " defined at %L, suggest also scalar one unless an assumed"
   16666              :                  " rank finalizer has been declared",
   16667              :                  derived->name, &derived->declared_at);
   16668              : 
   16669         2741 :   if (!derived->attr.pdt_template)
   16670              :     {
   16671         2693 :       vtab = gfc_find_derived_vtab (derived);
   16672         2693 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   16673         2693 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   16674         2693 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   16675              :     }
   16676              : 
   16677         2741 :   if (finalizable)
   16678          676 :     *finalizable = true;
   16679              : 
   16680              :   return true;
   16681              : }
   16682              : 
   16683              : 
   16684              : static gfc_symbol * containing_dt;
   16685              : 
   16686              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   16687              :    arguments whose declared types are PDT instances only transmit the PASS arg
   16688              :    if they match the enclosing derived type.  */
   16689              : 
   16690              : static bool
   16691         1496 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   16692              : {
   16693         1496 :   gfc_formal_arglist *dummy_args;
   16694         1496 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   16695              :     {
   16696          532 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   16697         1190 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   16698          126 :         dummy_args = dummy_args->next;
   16699          532 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   16700          532 :       if (dummy_args->sym->ts.type == BT_CLASS
   16701          532 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   16702              :                      containing_dt->name))
   16703          356 :         return true;
   16704              :     }
   16705              :   return false;
   16706              : }
   16707              : 
   16708              : 
   16709              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   16710              : 
   16711              : static bool
   16712          750 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   16713              :                              const char* generic_name, locus where)
   16714              : {
   16715          750 :   gfc_symbol *sym1, *sym2;
   16716          750 :   const char *pass1, *pass2;
   16717          750 :   gfc_formal_arglist *dummy_args;
   16718              : 
   16719          750 :   gcc_assert (t1->specific && t2->specific);
   16720          750 :   gcc_assert (!t1->specific->is_generic);
   16721          750 :   gcc_assert (!t2->specific->is_generic);
   16722          750 :   gcc_assert (t1->is_operator == t2->is_operator);
   16723              : 
   16724          750 :   sym1 = t1->specific->u.specific->n.sym;
   16725          750 :   sym2 = t2->specific->u.specific->n.sym;
   16726              : 
   16727          750 :   if (sym1 == sym2)
   16728              :     return true;
   16729              : 
   16730              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   16731          750 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   16732          748 :       || sym1->attr.function != sym2->attr.function)
   16733              :     {
   16734            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   16735              :                  " GENERIC %qs at %L",
   16736              :                  sym1->name, sym2->name, generic_name, &where);
   16737            2 :       return false;
   16738              :     }
   16739              : 
   16740              :   /* Determine PASS arguments.  */
   16741          748 :   if (t1->specific->nopass)
   16742              :     pass1 = NULL;
   16743          697 :   else if (t1->specific->pass_arg)
   16744              :     pass1 = t1->specific->pass_arg;
   16745              :   else
   16746              :     {
   16747          438 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   16748          438 :       if (dummy_args)
   16749          437 :         pass1 = dummy_args->sym->name;
   16750              :       else
   16751              :         pass1 = NULL;
   16752              :     }
   16753          748 :   if (t2->specific->nopass)
   16754              :     pass2 = NULL;
   16755          696 :   else if (t2->specific->pass_arg)
   16756              :     pass2 = t2->specific->pass_arg;
   16757              :   else
   16758              :     {
   16759          559 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   16760          559 :       if (dummy_args)
   16761          558 :         pass2 = dummy_args->sym->name;
   16762              :       else
   16763              :         pass2 = NULL;
   16764              :     }
   16765              : 
   16766              :   /* Care must be taken with pdt types and templates because the declared type
   16767              :      of the argument that is not 'no_pass' need not be the same as the
   16768              :      containing derived type.  If this is the case, subject the argument to
   16769              :      the full interface check, even though it cannot be used in the type
   16770              :      bound context.  */
   16771          748 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   16772          748 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   16773              : 
   16774          748 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   16775          748 :     pass1 = pass2 = NULL;
   16776              : 
   16777              :   /* Compare the interfaces.  */
   16778          748 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   16779              :                               NULL, 0, pass1, pass2))
   16780              :     {
   16781            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   16782              :                  sym1->name, sym2->name, generic_name, &where);
   16783            8 :       return false;
   16784              :     }
   16785              : 
   16786              :   return true;
   16787              : }
   16788              : 
   16789              : 
   16790              : /* Worker function for resolving a generic procedure binding; this is used to
   16791              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   16792              : 
   16793              :    The difference between those cases is finding possible inherited bindings
   16794              :    that are overridden, as one has to look for them in tb_sym_root,
   16795              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   16796              :    the super-type and set p->overridden correctly.  */
   16797              : 
   16798              : static bool
   16799         2421 : resolve_tb_generic_targets (gfc_symbol* super_type,
   16800              :                             gfc_typebound_proc* p, const char* name)
   16801              : {
   16802         2421 :   gfc_tbp_generic* target;
   16803         2421 :   gfc_symtree* first_target;
   16804         2421 :   gfc_symtree* inherited;
   16805              : 
   16806         2421 :   gcc_assert (p && p->is_generic);
   16807              : 
   16808              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   16809         2421 :   gcc_assert (p->u.generic);
   16810         5446 :   for (target = p->u.generic; target; target = target->next)
   16811         3042 :     if (!target->specific)
   16812              :       {
   16813         2627 :         gfc_typebound_proc* overridden_tbp;
   16814         2627 :         gfc_tbp_generic* g;
   16815         2627 :         const char* target_name;
   16816              : 
   16817         2627 :         target_name = target->specific_st->name;
   16818              : 
   16819              :         /* Defined for this type directly.  */
   16820         2627 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   16821              :           {
   16822         2618 :             target->specific = target->specific_st->n.tb;
   16823         2618 :             goto specific_found;
   16824              :           }
   16825              : 
   16826              :         /* Look for an inherited specific binding.  */
   16827            9 :         if (super_type)
   16828              :           {
   16829            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   16830              :                                                  true, NULL);
   16831              : 
   16832            5 :             if (inherited)
   16833              :               {
   16834            5 :                 gcc_assert (inherited->n.tb);
   16835            5 :                 target->specific = inherited->n.tb;
   16836            5 :                 goto specific_found;
   16837              :               }
   16838              :           }
   16839              : 
   16840            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   16841              :                    " at %L", target_name, name, &p->where);
   16842            4 :         return false;
   16843              : 
   16844              :         /* Once we've found the specific binding, check it is not ambiguous with
   16845              :            other specifics already found or inherited for the same GENERIC.  */
   16846         2623 : specific_found:
   16847         2623 :         gcc_assert (target->specific);
   16848              : 
   16849              :         /* This must really be a specific binding!  */
   16850         2623 :         if (target->specific->is_generic)
   16851              :           {
   16852            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   16853              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   16854            3 :             return false;
   16855              :           }
   16856              : 
   16857              :         /* Check those already resolved on this type directly.  */
   16858         6690 :         for (g = p->u.generic; g; g = g->next)
   16859         1464 :           if (g != target && g->specific
   16860         4809 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   16861              :             return false;
   16862              : 
   16863              :         /* Check for ambiguity with inherited specific targets.  */
   16864         2629 :         for (overridden_tbp = p->overridden; overridden_tbp;
   16865           16 :              overridden_tbp = overridden_tbp->overridden)
   16866           19 :           if (overridden_tbp->is_generic)
   16867              :             {
   16868           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   16869              :                 {
   16870           18 :                   gcc_assert (g->specific);
   16871           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   16872              :                     return false;
   16873              :                 }
   16874              :             }
   16875              :       }
   16876              : 
   16877              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   16878         2404 :   if (p->overridden && !p->overridden->is_generic)
   16879              :     {
   16880            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   16881              :                  " the same name", name, &p->where);
   16882            1 :       return false;
   16883              :     }
   16884              : 
   16885              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   16886              :      all must have the same attributes here.  */
   16887         2403 :   first_target = p->u.generic->specific->u.specific;
   16888         2403 :   gcc_assert (first_target);
   16889         2403 :   p->subroutine = first_target->n.sym->attr.subroutine;
   16890         2403 :   p->function = first_target->n.sym->attr.function;
   16891              : 
   16892         2403 :   return true;
   16893              : }
   16894              : 
   16895              : 
   16896              : /* Resolve a GENERIC procedure binding for a derived type.  */
   16897              : 
   16898              : static bool
   16899         1249 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   16900              : {
   16901         1249 :   gfc_symbol* super_type;
   16902              : 
   16903              :   /* Find the overridden binding if any.  */
   16904         1249 :   st->n.tb->overridden = NULL;
   16905         1249 :   super_type = gfc_get_derived_super_type (derived);
   16906         1249 :   if (super_type)
   16907              :     {
   16908           40 :       gfc_symtree* overridden;
   16909           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   16910              :                                             true, NULL);
   16911              : 
   16912           40 :       if (overridden && overridden->n.tb)
   16913           21 :         st->n.tb->overridden = overridden->n.tb;
   16914              :     }
   16915              : 
   16916              :   /* Resolve using worker function.  */
   16917         1249 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   16918              : }
   16919              : 
   16920              : 
   16921              : /* Retrieve the target-procedure of an operator binding and do some checks in
   16922              :    common for intrinsic and user-defined type-bound operators.  */
   16923              : 
   16924              : static gfc_symbol*
   16925         1244 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   16926              : {
   16927         1244 :   gfc_symbol* target_proc;
   16928              : 
   16929         1244 :   gcc_assert (target->specific && !target->specific->is_generic);
   16930         1244 :   target_proc = target->specific->u.specific->n.sym;
   16931         1244 :   gcc_assert (target_proc);
   16932              : 
   16933              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   16934         1244 :   if (target->specific->nopass)
   16935              :     {
   16936            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   16937            2 :       return NULL;
   16938              :     }
   16939              : 
   16940              :   return target_proc;
   16941              : }
   16942              : 
   16943              : 
   16944              : /* Resolve a type-bound intrinsic operator.  */
   16945              : 
   16946              : static bool
   16947         1059 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   16948              :                                 gfc_typebound_proc* p)
   16949              : {
   16950         1059 :   gfc_symbol* super_type;
   16951         1059 :   gfc_tbp_generic* target;
   16952              : 
   16953              :   /* If there's already an error here, do nothing (but don't fail again).  */
   16954         1059 :   if (p->error)
   16955              :     return true;
   16956              : 
   16957              :   /* Operators should always be GENERIC bindings.  */
   16958         1059 :   gcc_assert (p->is_generic);
   16959              : 
   16960              :   /* Look for an overridden binding.  */
   16961         1059 :   super_type = gfc_get_derived_super_type (derived);
   16962         1059 :   if (super_type && super_type->f2k_derived)
   16963            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   16964              :                                                      op, true, NULL);
   16965              :   else
   16966         1058 :     p->overridden = NULL;
   16967              : 
   16968              :   /* Resolve general GENERIC properties using worker function.  */
   16969         1059 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   16970            1 :     goto error;
   16971              : 
   16972              :   /* Check the targets to be procedures of correct interface.  */
   16973         2163 :   for (target = p->u.generic; target; target = target->next)
   16974              :     {
   16975         1130 :       gfc_symbol* target_proc;
   16976              : 
   16977         1130 :       target_proc = get_checked_tb_operator_target (target, p->where);
   16978         1130 :       if (!target_proc)
   16979            1 :         goto error;
   16980              : 
   16981         1129 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   16982            3 :         goto error;
   16983              : 
   16984              :       /* Add target to non-typebound operator list.  */
   16985         1126 :       if (!target->specific->deferred && !derived->attr.use_assoc
   16986          397 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   16987              :         {
   16988          395 :           gfc_interface *head, *intr;
   16989              : 
   16990              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   16991              :              mechanism for handling module procedures winds up resolving
   16992              :              operator interfaces twice and would otherwise cause an error.
   16993              :              Likewise, new instances of PDTs can cause the operator inter-
   16994              :              faces to be resolved multiple times.  */
   16995          467 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   16996           91 :             if (intr->sym == target_proc
   16997           21 :                 && (target_proc->attr.used_in_submodule
   16998            4 :                     || derived->attr.pdt_type
   16999            2 :                     || derived->attr.pdt_template))
   17000              :               return true;
   17001              : 
   17002          376 :           if (!gfc_check_new_interface (derived->ns->op[op],
   17003              :                                         target_proc, p->where))
   17004              :             return false;
   17005          374 :           head = derived->ns->op[op];
   17006          374 :           intr = gfc_get_interface ();
   17007          374 :           intr->sym = target_proc;
   17008          374 :           intr->where = p->where;
   17009          374 :           intr->next = head;
   17010          374 :           derived->ns->op[op] = intr;
   17011              :         }
   17012              :     }
   17013              : 
   17014              :   return true;
   17015              : 
   17016            5 : error:
   17017            5 :   p->error = 1;
   17018            5 :   return false;
   17019              : }
   17020              : 
   17021              : 
   17022              : /* Resolve a type-bound user operator (tree-walker callback).  */
   17023              : 
   17024              : static gfc_symbol* resolve_bindings_derived;
   17025              : static bool resolve_bindings_result;
   17026              : 
   17027              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   17028              : 
   17029              : static void
   17030          113 : resolve_typebound_user_op (gfc_symtree* stree)
   17031              : {
   17032          113 :   gfc_symbol* super_type;
   17033          113 :   gfc_tbp_generic* target;
   17034              : 
   17035          113 :   gcc_assert (stree && stree->n.tb);
   17036              : 
   17037          113 :   if (stree->n.tb->error)
   17038              :     return;
   17039              : 
   17040              :   /* Operators should always be GENERIC bindings.  */
   17041          113 :   gcc_assert (stree->n.tb->is_generic);
   17042              : 
   17043              :   /* Find overridden procedure, if any.  */
   17044          113 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17045          113 :   if (super_type && super_type->f2k_derived)
   17046              :     {
   17047           18 :       gfc_symtree* overridden;
   17048           18 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   17049              :                                                stree->name, true, NULL);
   17050              : 
   17051           18 :       if (overridden && overridden->n.tb)
   17052            0 :         stree->n.tb->overridden = overridden->n.tb;
   17053              :     }
   17054              :   else
   17055           95 :     stree->n.tb->overridden = NULL;
   17056              : 
   17057              :   /* Resolve basically using worker function.  */
   17058          113 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   17059            0 :     goto error;
   17060              : 
   17061              :   /* Check the targets to be functions of correct interface.  */
   17062          224 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   17063              :     {
   17064          114 :       gfc_symbol* target_proc;
   17065              : 
   17066          114 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   17067          114 :       if (!target_proc)
   17068            1 :         goto error;
   17069              : 
   17070          113 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   17071            2 :         goto error;
   17072              :     }
   17073              : 
   17074              :   return;
   17075              : 
   17076            3 : error:
   17077            3 :   resolve_bindings_result = false;
   17078            3 :   stree->n.tb->error = 1;
   17079              : }
   17080              : 
   17081              : 
   17082              : /* Resolve the type-bound procedures for a derived type.  */
   17083              : 
   17084              : static void
   17085        10195 : resolve_typebound_procedure (gfc_symtree* stree)
   17086              : {
   17087        10195 :   gfc_symbol* proc;
   17088        10195 :   locus where;
   17089        10195 :   gfc_symbol* me_arg;
   17090        10195 :   gfc_symbol* super_type;
   17091        10195 :   gfc_component* comp;
   17092              : 
   17093        10195 :   gcc_assert (stree);
   17094              : 
   17095              :   /* Undefined specific symbol from GENERIC target definition.  */
   17096        10195 :   if (!stree->n.tb)
   17097        10113 :     return;
   17098              : 
   17099        10189 :   if (stree->n.tb->error)
   17100              :     return;
   17101              : 
   17102              :   /* If this is a GENERIC binding, use that routine.  */
   17103        10173 :   if (stree->n.tb->is_generic)
   17104              :     {
   17105         1249 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   17106           17 :         goto error;
   17107              :       return;
   17108              :     }
   17109              : 
   17110              :   /* Get the target-procedure to check it.  */
   17111         8924 :   gcc_assert (!stree->n.tb->is_generic);
   17112         8924 :   gcc_assert (stree->n.tb->u.specific);
   17113         8924 :   proc = stree->n.tb->u.specific->n.sym;
   17114         8924 :   where = stree->n.tb->where;
   17115              : 
   17116              :   /* Default access should already be resolved from the parser.  */
   17117         8924 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   17118              : 
   17119         8924 :   if (stree->n.tb->deferred)
   17120              :     {
   17121          676 :       if (!check_proc_interface (proc, &where))
   17122            5 :         goto error;
   17123              :     }
   17124              :   else
   17125              :     {
   17126              :       /* If proc has not been resolved at this point, proc->name may
   17127              :          actually be a USE associated entity. See PR fortran/89647. */
   17128         8248 :       if (!proc->resolve_symbol_called
   17129         5710 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   17130              :         {
   17131           11 :           gfc_symbol *tmp;
   17132           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   17133           11 :           if (tmp && tmp->attr.use_assoc)
   17134              :             {
   17135            1 :               proc->module = tmp->module;
   17136            1 :               proc->attr.proc = tmp->attr.proc;
   17137            1 :               proc->attr.function = tmp->attr.function;
   17138            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   17139            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   17140            1 :               proc->ts = tmp->ts;
   17141            1 :               proc->result = tmp->result;
   17142              :             }
   17143              :         }
   17144              : 
   17145              :       /* Check for F08:C465.  */
   17146         8248 :       if ((!proc->attr.subroutine && !proc->attr.function)
   17147         8238 :           || (proc->attr.proc != PROC_MODULE
   17148           70 :               && proc->attr.if_source != IFSRC_IFBODY
   17149            7 :               && !proc->attr.module_procedure)
   17150         8237 :           || proc->attr.abstract)
   17151              :         {
   17152           12 :           gfc_error ("%qs must be a module procedure or an external "
   17153              :                      "procedure with an explicit interface at %L",
   17154              :                      proc->name, &where);
   17155           12 :           goto error;
   17156              :         }
   17157              :     }
   17158              : 
   17159         8907 :   stree->n.tb->subroutine = proc->attr.subroutine;
   17160         8907 :   stree->n.tb->function = proc->attr.function;
   17161              : 
   17162              :   /* Find the super-type of the current derived type.  We could do this once and
   17163              :      store in a global if speed is needed, but as long as not I believe this is
   17164              :      more readable and clearer.  */
   17165         8907 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   17166              : 
   17167              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   17168              :      from a .mod file.  */
   17169         8907 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   17170              :     {
   17171         2844 :       gfc_formal_arglist *dummy_args;
   17172              : 
   17173         2844 :       dummy_args = gfc_sym_get_dummy_args (proc);
   17174         2844 :       if (stree->n.tb->pass_arg)
   17175              :         {
   17176          468 :           gfc_formal_arglist *i;
   17177              : 
   17178              :           /* If an explicit passing argument name is given, walk the arg-list
   17179              :              and look for it.  */
   17180              : 
   17181          468 :           me_arg = NULL;
   17182          468 :           stree->n.tb->pass_arg_num = 1;
   17183          601 :           for (i = dummy_args; i; i = i->next)
   17184              :             {
   17185          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   17186              :                 {
   17187              :                   me_arg = i->sym;
   17188              :                   break;
   17189              :                 }
   17190          133 :               ++stree->n.tb->pass_arg_num;
   17191              :             }
   17192              : 
   17193          468 :           if (!me_arg)
   17194              :             {
   17195            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   17196              :                          " argument %qs",
   17197              :                          proc->name, stree->n.tb->pass_arg, &where,
   17198              :                          stree->n.tb->pass_arg);
   17199            2 :               goto error;
   17200              :             }
   17201              :         }
   17202              :       else
   17203              :         {
   17204              :           /* Otherwise, take the first one; there should in fact be at least
   17205              :              one.  */
   17206         2376 :           stree->n.tb->pass_arg_num = 1;
   17207         2376 :           if (!dummy_args)
   17208              :             {
   17209            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   17210              :                          " least one argument", proc->name, &where);
   17211            2 :               goto error;
   17212              :             }
   17213         2374 :           me_arg = dummy_args->sym;
   17214              :         }
   17215              : 
   17216              :       /* Now check that the argument-type matches and the passed-object
   17217              :          dummy argument is generally fine.  */
   17218              : 
   17219         2374 :       gcc_assert (me_arg);
   17220              : 
   17221         2840 :       if (me_arg->ts.type != BT_CLASS)
   17222              :         {
   17223            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17224              :                      " at %L", proc->name, &where);
   17225            5 :           goto error;
   17226              :         }
   17227              : 
   17228              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   17229         2835 :       if (!resolve_bindings_derived->attr.pdt_template
   17230         2826 :           && !(containing_dt && containing_dt->attr.pdt_type
   17231           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   17232         2806 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   17233              :         {
   17234            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17235              :                      "the derived-type %qs", me_arg->name, proc->name,
   17236              :                      me_arg->name, &where, resolve_bindings_derived->name);
   17237            0 :           goto error;
   17238              :         }
   17239              : 
   17240         2835 :       if (resolve_bindings_derived->attr.pdt_template
   17241         2844 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   17242            9 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   17243              :         {
   17244            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   17245              :                      "the parametric derived-type %qs", me_arg->name,
   17246              :                      proc->name, me_arg->name, &where,
   17247              :                      resolve_bindings_derived->name);
   17248            0 :           goto error;
   17249              :         }
   17250              : 
   17251         2835 :       if (((resolve_bindings_derived->attr.pdt_template
   17252            9 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   17253            9 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   17254         2826 :            || resolve_bindings_derived->attr.pdt_type)
   17255           69 :           && (me_arg->param_list != NULL)
   17256         2904 :           && (gfc_spec_list_type (me_arg->param_list,
   17257           69 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   17258              :                                   != SPEC_ASSUMED))
   17259              :         {
   17260              : 
   17261              :           /* Add a check to verify if there are any LEN parameters in the
   17262              :              first place.  If there are LEN parameters, throw this error.
   17263              :              If there are only KIND parameters, then don't trigger
   17264              :              this error.  */
   17265            6 :           gfc_component *c;
   17266            6 :           bool seen_len_param = false;
   17267            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   17268              : 
   17269            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   17270              :             {
   17271            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   17272              :                                      me_arg_param->name, true, true, NULL);
   17273              : 
   17274            6 :               gcc_assert (c != NULL);
   17275              : 
   17276            6 :               if (c->attr.pdt_kind)
   17277            0 :                 continue;
   17278              : 
   17279              :               /* Getting here implies that there is a pdt_len parameter
   17280              :                  in the list.  */
   17281              :               seen_len_param = true;
   17282              :               break;
   17283              :             }
   17284              : 
   17285            6 :             if (seen_len_param)
   17286              :               {
   17287            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   17288              :                            "argument %qs of %qs at %L must be ASSUMED.",
   17289              :                            me_arg->name, proc->name, &where);
   17290            6 :                 goto error;
   17291              :               }
   17292              :         }
   17293              : 
   17294         2829 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   17295         2829 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   17296              :         {
   17297            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   17298              :                      " scalar", proc->name, &where);
   17299            1 :           goto error;
   17300              :         }
   17301         2828 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17302              :         {
   17303            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17304              :                      " be ALLOCATABLE", proc->name, &where);
   17305            2 :           goto error;
   17306              :         }
   17307         2826 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17308              :         {
   17309            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   17310              :                      " be POINTER", proc->name, &where);
   17311            2 :           goto error;
   17312              :         }
   17313              :     }
   17314              : 
   17315              :   /* If we are extending some type, check that we don't override a procedure
   17316              :      flagged NON_OVERRIDABLE.  */
   17317         8887 :   stree->n.tb->overridden = NULL;
   17318         8887 :   if (super_type)
   17319              :     {
   17320         1513 :       gfc_symtree* overridden;
   17321         1513 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   17322              :                                             stree->name, true, NULL);
   17323              : 
   17324         1513 :       if (overridden)
   17325              :         {
   17326         1218 :           if (overridden->n.tb)
   17327         1218 :             stree->n.tb->overridden = overridden->n.tb;
   17328              : 
   17329         1218 :           if (!gfc_check_typebound_override (stree, overridden))
   17330           26 :             goto error;
   17331              :         }
   17332              :     }
   17333              : 
   17334              :   /* See if there's a name collision with a component directly in this type.  */
   17335        21261 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   17336        12401 :     if (!strcmp (comp->name, stree->name))
   17337              :       {
   17338            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   17339              :                    " %qs",
   17340              :                    stree->name, &where, resolve_bindings_derived->name);
   17341            1 :         goto error;
   17342              :       }
   17343              : 
   17344              :   /* Try to find a name collision with an inherited component.  */
   17345         8860 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   17346              :                                         NULL))
   17347              :     {
   17348            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   17349              :                  " component of %qs",
   17350              :                  stree->name, &where, resolve_bindings_derived->name);
   17351            1 :       goto error;
   17352              :     }
   17353              : 
   17354         8859 :   stree->n.tb->error = 0;
   17355         8859 :   return;
   17356              : 
   17357           82 : error:
   17358           82 :   resolve_bindings_result = false;
   17359           82 :   stree->n.tb->error = 1;
   17360              : }
   17361              : 
   17362              : 
   17363              : static bool
   17364        88732 : resolve_typebound_procedures (gfc_symbol* derived)
   17365              : {
   17366        88732 :   int op;
   17367        88732 :   gfc_symbol* super_type;
   17368              : 
   17369              :   /* Resolve the super-type first so that inherited bindings (including
   17370              :      user operators) are fully resolved before we look them up via
   17371              :      gfc_find_typebound_user_op.  This must happen even when 'derived'
   17372              :      has no direct type-bound bindings of its own.  */
   17373        88732 :   super_type = gfc_get_derived_super_type (derived);
   17374        88732 :   if (super_type)
   17375        13755 :     resolve_symbol (super_type);
   17376              : 
   17377        88732 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17378              :     return true;
   17379              : 
   17380         4882 :   resolve_bindings_derived = derived;
   17381         4882 :   resolve_bindings_result = true;
   17382              : 
   17383         4882 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17384         4882 :   if (derived->f2k_derived->tb_sym_root)
   17385         4882 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17386              :                           &resolve_typebound_procedure);
   17387              : 
   17388         4882 :   if (derived->f2k_derived->tb_uop_root)
   17389           91 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17390              :                           &resolve_typebound_user_op);
   17391         4882 :   containing_dt = NULL;
   17392              : 
   17393       141578 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17394              :     {
   17395       136696 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17396       136696 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17397              :                                                 (gfc_intrinsic_op)op, p))
   17398            7 :         resolve_bindings_result = false;
   17399              :     }
   17400              : 
   17401         4882 :   return resolve_bindings_result;
   17402              : }
   17403              : 
   17404              : 
   17405              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17406              :    to give all identical derived types the same backend_decl.  */
   17407              : static void
   17408       181729 : add_dt_to_dt_list (gfc_symbol *derived)
   17409              : {
   17410       181729 :   if (!derived->dt_next)
   17411              :     {
   17412        85096 :       if (gfc_derived_types)
   17413              :         {
   17414        69711 :           derived->dt_next = gfc_derived_types->dt_next;
   17415        69711 :           gfc_derived_types->dt_next = derived;
   17416              :         }
   17417              :       else
   17418              :         {
   17419        15385 :           derived->dt_next = derived;
   17420              :         }
   17421        85096 :       gfc_derived_types = derived;
   17422              :     }
   17423       181729 : }
   17424              : 
   17425              : 
   17426              : /* Ensure that a derived-type is really not abstract, meaning that every
   17427              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17428              : 
   17429              : static bool
   17430         7146 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17431              : {
   17432         7146 :   if (!st)
   17433              :     return true;
   17434              : 
   17435         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17436              :     return false;
   17437         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17438              :     return false;
   17439              : 
   17440         2771 :   if (st->n.tb && st->n.tb->deferred)
   17441              :     {
   17442         2019 :       gfc_symtree* overriding;
   17443         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17444         2019 :       if (!overriding)
   17445              :         return false;
   17446         2018 :       gcc_assert (overriding->n.tb);
   17447         2018 :       if (overriding->n.tb->deferred)
   17448              :         {
   17449            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17450              :                      " %qs is DEFERRED and not overridden",
   17451              :                      sub->name, &sub->declared_at, st->name);
   17452            5 :           return false;
   17453              :         }
   17454              :     }
   17455              : 
   17456              :   return true;
   17457              : }
   17458              : 
   17459              : static bool
   17460         1454 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17461              : {
   17462              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17463              :      and for each ancestor-type, check all bindings.  If any of them is
   17464              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17465              :      binding is not DEFERRED.
   17466              :      This is not the most efficient way to do this, but it should be ok and is
   17467              :      clearer than something sophisticated.  */
   17468              : 
   17469         1603 :   gcc_assert (ancestor && !sub->attr.abstract);
   17470              : 
   17471         1603 :   if (!ancestor->attr.abstract)
   17472              :     return true;
   17473              : 
   17474              :   /* Walk bindings of this ancestor.  */
   17475         1602 :   if (ancestor->f2k_derived)
   17476              :     {
   17477         1602 :       bool t;
   17478         1602 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17479         1602 :       if (!t)
   17480              :         return false;
   17481              :     }
   17482              : 
   17483              :   /* Find next ancestor type and recurse on it.  */
   17484         1596 :   ancestor = gfc_get_derived_super_type (ancestor);
   17485         1596 :   if (ancestor)
   17486              :     return ensure_not_abstract (sub, ancestor);
   17487              : 
   17488              :   return true;
   17489              : }
   17490              : 
   17491              : 
   17492              : /* This check for typebound defined assignments is done recursively
   17493              :    since the order in which derived types are resolved is not always in
   17494              :    order of the declarations.  */
   17495              : 
   17496              : static void
   17497       186585 : check_defined_assignments (gfc_symbol *derived)
   17498              : {
   17499       186585 :   gfc_component *c;
   17500              : 
   17501       626045 :   for (c = derived->components; c; c = c->next)
   17502              :     {
   17503       441267 :       if (!gfc_bt_struct (c->ts.type)
   17504       107106 :           || c->attr.pointer
   17505        21479 :           || c->attr.proc_pointer_comp
   17506        21479 :           || c->attr.class_pointer
   17507        21473 :           || c->attr.proc_pointer)
   17508       420328 :         continue;
   17509              : 
   17510        20939 :       if (c->ts.u.derived->attr.defined_assign_comp
   17511        20704 :           || (c->ts.u.derived->f2k_derived
   17512        20122 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17513              :         {
   17514         1783 :           derived->attr.defined_assign_comp = 1;
   17515         1783 :           return;
   17516              :         }
   17517              : 
   17518        19156 :       if (c->attr.allocatable)
   17519         6938 :         continue;
   17520              : 
   17521        12218 :       check_defined_assignments (c->ts.u.derived);
   17522        12218 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17523              :         {
   17524           24 :           derived->attr.defined_assign_comp = 1;
   17525           24 :           return;
   17526              :         }
   17527              :     }
   17528              : }
   17529              : 
   17530              : 
   17531              : /* Resolve a single component of a derived type or structure.  */
   17532              : 
   17533              : static bool
   17534       420588 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17535              : {
   17536       420588 :   gfc_symbol *super_type;
   17537       420588 :   symbol_attribute *attr;
   17538              : 
   17539       420588 :   if (c->attr.artificial)
   17540              :     return true;
   17541              : 
   17542              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17543              :      such as block data because the procedure pointers will cause ICEs
   17544              :      and vtables are not needed in these contexts.  */
   17545       286763 :   if (sym->attr.vtype && sym->attr.use_assoc
   17546        49988 :       && sym->ns->proc_name == NULL)
   17547              :     return true;
   17548              : 
   17549              :   /* F2008, C442.  */
   17550       286754 :   if ((!sym->attr.is_class || c != sym->components)
   17551       286754 :       && c->attr.codimension
   17552          230 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17553              :     {
   17554            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17555              :                  "deferred shape", c->name, &c->loc);
   17556            4 :       return false;
   17557              :     }
   17558              : 
   17559              :   /* F2008, C443.  */
   17560       286750 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17561           85 :       && c->ts.u.derived->ts.is_iso_c)
   17562              :     {
   17563            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17564              :                  "shall not be a coarray", c->name, &c->loc);
   17565            1 :       return false;
   17566              :     }
   17567              : 
   17568              :   /* F2008, C444.  */
   17569       286749 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17570           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17571           26 :           || c->attr.allocatable))
   17572              :     {
   17573            3 :       gfc_error ("Component %qs at %L with coarray component "
   17574              :                  "shall be a nonpointer, nonallocatable scalar",
   17575              :                  c->name, &c->loc);
   17576            3 :       return false;
   17577              :     }
   17578              : 
   17579              :   /* F2008, C448.  */
   17580       286746 :   if (c->ts.type == BT_CLASS)
   17581              :     {
   17582         7274 :       if (c->attr.class_ok && CLASS_DATA (c))
   17583              :         {
   17584         7266 :           attr = &(CLASS_DATA (c)->attr);
   17585              : 
   17586              :           /* Fix up contiguous attribute.  */
   17587         7266 :           if (c->attr.contiguous)
   17588           11 :             attr->contiguous = 1;
   17589              :         }
   17590              :       else
   17591              :         attr = NULL;
   17592              :     }
   17593              :   else
   17594       279472 :     attr = &c->attr;
   17595              : 
   17596       286738 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17597              :     {
   17598            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17599              :                  "is not an array pointer", c->name, &c->loc);
   17600            5 :       return false;
   17601              :     }
   17602              : 
   17603              :   /* F2003, 15.2.1 - length has to be one.  */
   17604        41724 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17605       286760 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17606           19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17607           19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17608              :     {
   17609            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17610              :                  c->name, &c->loc);
   17611            1 :       return false;
   17612              :     }
   17613              : 
   17614        53827 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17615          313 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17616       286748 :       && !(gfc_get_derived_super_type (sym)
   17617            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17618            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17619              :     {
   17620            8 :       gfc_actual_arglist *type_spec_list;
   17621            8 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17622              :                                 &type_spec_list)
   17623              :           != MATCH_YES)
   17624            0 :         return false;
   17625            8 :       gfc_free_actual_arglist (c->param_list);
   17626            8 :       c->param_list = type_spec_list;
   17627            8 :       if (!sym->attr.pdt_type)
   17628            8 :         sym->attr.pdt_comp = 1;
   17629              :     }
   17630       286732 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17631           54 :     sym->attr.pdt_comp = 1;
   17632              : 
   17633       286740 :   if (c->attr.proc_pointer && c->ts.interface)
   17634              :     {
   17635        14912 :       gfc_symbol *ifc = c->ts.interface;
   17636              : 
   17637        14912 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17638              :         {
   17639            6 :           c->tb->error = 1;
   17640            6 :           return false;
   17641              :         }
   17642              : 
   17643        14906 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17644              :         {
   17645              :           /* Resolve interface and copy attributes.  */
   17646        14857 :           if (ifc->formal && !ifc->formal_ns)
   17647         2611 :             resolve_symbol (ifc);
   17648        14857 :           if (ifc->attr.intrinsic)
   17649            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17650              : 
   17651        14857 :           if (ifc->result)
   17652              :             {
   17653         7747 :               c->ts = ifc->result->ts;
   17654         7747 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17655         7747 :               c->attr.pointer = ifc->result->attr.pointer;
   17656         7747 :               c->attr.dimension = ifc->result->attr.dimension;
   17657         7747 :               c->as = gfc_copy_array_spec (ifc->result->as);
   17658         7747 :               c->attr.class_ok = ifc->result->attr.class_ok;
   17659              :             }
   17660              :           else
   17661              :             {
   17662         7110 :               c->ts = ifc->ts;
   17663         7110 :               c->attr.allocatable = ifc->attr.allocatable;
   17664         7110 :               c->attr.pointer = ifc->attr.pointer;
   17665         7110 :               c->attr.dimension = ifc->attr.dimension;
   17666         7110 :               c->as = gfc_copy_array_spec (ifc->as);
   17667         7110 :               c->attr.class_ok = ifc->attr.class_ok;
   17668              :             }
   17669        14857 :           c->ts.interface = ifc;
   17670        14857 :           c->attr.function = ifc->attr.function;
   17671        14857 :           c->attr.subroutine = ifc->attr.subroutine;
   17672              : 
   17673        14857 :           c->attr.pure = ifc->attr.pure;
   17674        14857 :           c->attr.elemental = ifc->attr.elemental;
   17675        14857 :           c->attr.recursive = ifc->attr.recursive;
   17676        14857 :           c->attr.always_explicit = ifc->attr.always_explicit;
   17677        14857 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   17678              :           /* Copy char length.  */
   17679        14857 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   17680              :             {
   17681          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   17682          454 :               if (cl->length && !cl->resolved
   17683          601 :                   && !gfc_resolve_expr (cl->length))
   17684              :                 {
   17685            0 :                   c->tb->error = 1;
   17686            0 :                   return false;
   17687              :                 }
   17688          491 :               c->ts.u.cl = cl;
   17689              :             }
   17690              :         }
   17691              :     }
   17692       271828 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   17693              :     {
   17694              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   17695              :          interface must be a subroutine.  */
   17696          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   17697              :     }
   17698              : 
   17699              :   /* Procedure pointer components: Check PASS arg.  */
   17700       286734 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   17701          578 :       && !sym->attr.vtype)
   17702              :     {
   17703           95 :       gfc_symbol* me_arg;
   17704              : 
   17705           95 :       if (c->tb->pass_arg)
   17706              :         {
   17707           20 :           gfc_formal_arglist* i;
   17708              : 
   17709              :           /* If an explicit passing argument name is given, walk the arg-list
   17710              :             and look for it.  */
   17711              : 
   17712           20 :           me_arg = NULL;
   17713           20 :           c->tb->pass_arg_num = 1;
   17714           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   17715              :             {
   17716           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   17717              :                 {
   17718              :                   me_arg = i->sym;
   17719              :                   break;
   17720              :                 }
   17721           14 :               c->tb->pass_arg_num++;
   17722              :             }
   17723              : 
   17724           20 :           if (!me_arg)
   17725              :             {
   17726            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   17727              :                          "at %L has no argument %qs", c->name,
   17728              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   17729            1 :               c->tb->error = 1;
   17730            1 :               return false;
   17731              :             }
   17732              :         }
   17733              :       else
   17734              :         {
   17735              :           /* Otherwise, take the first one; there should in fact be at least
   17736              :             one.  */
   17737           75 :           c->tb->pass_arg_num = 1;
   17738           75 :           if (!c->ts.interface->formal)
   17739              :             {
   17740            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   17741              :                          "must have at least one argument",
   17742              :                          c->name, &c->loc);
   17743            3 :               c->tb->error = 1;
   17744            3 :               return false;
   17745              :             }
   17746           72 :           me_arg = c->ts.interface->formal->sym;
   17747              :         }
   17748              : 
   17749              :       /* Now check that the argument-type matches.  */
   17750           72 :       gcc_assert (me_arg);
   17751           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   17752           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   17753           90 :           || (me_arg->ts.type == BT_CLASS
   17754           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   17755              :         {
   17756            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   17757              :                      " the derived type %qs", me_arg->name, c->name,
   17758              :                      me_arg->name, &c->loc, sym->name);
   17759            1 :           c->tb->error = 1;
   17760            1 :           return false;
   17761              :         }
   17762              : 
   17763              :       /* Check for F03:C453.  */
   17764           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   17765              :         {
   17766            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17767              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   17768              :                      &c->loc);
   17769            1 :           c->tb->error = 1;
   17770            1 :           return false;
   17771              :         }
   17772              : 
   17773           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17774              :         {
   17775            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17776              :                      "may not have the POINTER attribute", me_arg->name,
   17777              :                      c->name, me_arg->name, &c->loc);
   17778            1 :           c->tb->error = 1;
   17779            1 :           return false;
   17780              :         }
   17781              : 
   17782           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17783              :         {
   17784            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17785              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   17786              :                      me_arg->name, &c->loc);
   17787            1 :           c->tb->error = 1;
   17788            1 :           return false;
   17789              :         }
   17790              : 
   17791           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   17792              :         {
   17793            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17794              :                      " at %L", c->name, &c->loc);
   17795            2 :           return false;
   17796              :         }
   17797              : 
   17798              :     }
   17799              : 
   17800              :   /* Check type-spec if this is not the parent-type component.  */
   17801       286724 :   if (((sym->attr.is_class
   17802        12859 :         && (!sym->components->ts.u.derived->attr.extension
   17803         2400 :             || c != CLASS_DATA (sym->components)))
   17804       275216 :        || (!sym->attr.is_class
   17805       273865 :            && (!sym->attr.extension || c != sym->components)))
   17806       278366 :       && !sym->attr.vtype
   17807       453446 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   17808              :     return false;
   17809              : 
   17810       286723 :   super_type = gfc_get_derived_super_type (sym);
   17811              : 
   17812              :   /* If this type is an extension, set the accessibility of the parent
   17813              :      component.  */
   17814       286723 :   if (super_type
   17815        26474 :       && ((sym->attr.is_class
   17816        12859 :            && c == CLASS_DATA (sym->components))
   17817        17540 :           || (!sym->attr.is_class && c == sym->components))
   17818        15941 :       && strcmp (super_type->name, c->name) == 0)
   17819         6821 :     c->attr.access = super_type->attr.access;
   17820              : 
   17821              :   /* If this type is an extension, see if this component has the same name
   17822              :      as an inherited type-bound procedure.  */
   17823        26474 :   if (super_type && !sym->attr.is_class
   17824        13615 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   17825              :     {
   17826            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   17827              :                  " inherited type-bound procedure",
   17828              :                  c->name, sym->name, &c->loc);
   17829            1 :       return false;
   17830              :     }
   17831              : 
   17832       286722 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   17833         9477 :       && !c->ts.deferred)
   17834              :     {
   17835         7218 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   17836          258 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   17837              : 
   17838         7218 :       if (c->ts.u.cl->length == NULL
   17839         7212 :           || !resolve_charlen(c->ts.u.cl)
   17840        14429 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   17841              :         {
   17842            9 :           gfc_error ("Character length of component %qs needs to "
   17843              :                      "be a constant specification expression at %L",
   17844              :                      c->name,
   17845            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   17846            9 :           return false;
   17847              :         }
   17848              : 
   17849         7209 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   17850              :         {
   17851            2 :          if (!c->ts.u.cl->length->error)
   17852              :            {
   17853            1 :              gfc_error ("Character length expression of component %qs at %L "
   17854              :                         "must be of INTEGER type, found %s",
   17855            1 :                         c->name, &c->ts.u.cl->length->where,
   17856              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   17857            1 :              c->ts.u.cl->length->error = 1;
   17858              :            }
   17859              :          return false;
   17860              :        }
   17861              :     }
   17862              : 
   17863       286711 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   17864         2295 :       && !c->attr.pointer && !c->attr.allocatable)
   17865              :     {
   17866            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   17867              :                  "length must be a POINTER or ALLOCATABLE",
   17868              :                  c->name, sym->name, &c->loc);
   17869            1 :       return false;
   17870              :     }
   17871              : 
   17872              :   /* Add the hidden deferred length field.  */
   17873       286710 :   if (c->ts.type == BT_CHARACTER
   17874         9977 :       && (c->ts.deferred || c->attr.pdt_string)
   17875         2469 :       && !c->attr.function
   17876         2433 :       && !sym->attr.is_class)
   17877              :     {
   17878         2286 :       char name[GFC_MAX_SYMBOL_LEN+9];
   17879         2286 :       gfc_component *strlen;
   17880         2286 :       sprintf (name, "_%s_length", c->name);
   17881         2286 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   17882         2286 :       if (strlen == NULL)
   17883              :         {
   17884          484 :           if (!gfc_add_component (sym, name, &strlen))
   17885            0 :             return false;
   17886          484 :           strlen->ts.type = BT_INTEGER;
   17887          484 :           strlen->ts.kind = gfc_charlen_int_kind;
   17888          484 :           strlen->attr.access = ACCESS_PRIVATE;
   17889          484 :           strlen->attr.artificial = 1;
   17890              :         }
   17891              :     }
   17892              : 
   17893       286710 :   if (c->ts.type == BT_DERIVED
   17894        54037 :       && sym->component_access != ACCESS_PRIVATE
   17895        53017 :       && gfc_check_symbol_access (sym)
   17896       103998 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   17897        51940 :       && !c->ts.u.derived->attr.use_assoc
   17898        27979 :       && !gfc_check_symbol_access (c->ts.u.derived)
   17899       286907 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   17900              :                           "PRIVATE type and cannot be a component of "
   17901              :                           "%qs, which is PUBLIC at %L", c->name,
   17902              :                           sym->name, &sym->declared_at))
   17903              :     return false;
   17904              : 
   17905       286709 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   17906              :     {
   17907            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   17908              :                  "type %s", c->name, &c->loc, sym->name);
   17909            2 :       return false;
   17910              :     }
   17911              : 
   17912       286707 :   if (sym->attr.sequence)
   17913              :     {
   17914         2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   17915              :         {
   17916            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   17917              :                      "not have the SEQUENCE attribute",
   17918              :                      c->ts.u.derived->name, &sym->declared_at);
   17919            0 :           return false;
   17920              :         }
   17921              :     }
   17922              : 
   17923       286707 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   17924            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   17925       286707 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   17926         7608 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   17927            0 :     CLASS_DATA (c)->ts.u.derived
   17928            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   17929              : 
   17930              :   /* If an allocatable component derived type is of the same type as
   17931              :      the enclosing derived type, we need a vtable generating so that
   17932              :      the __deallocate procedure is created.  */
   17933       286707 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   17934        61655 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   17935          495 :     gfc_find_vtab (&c->ts);
   17936              : 
   17937              :   /* Ensure that all the derived type components are put on the
   17938              :      derived type list; even in formal namespaces, where derived type
   17939              :      pointer components might not have been declared.  */
   17940       286707 :   if (c->ts.type == BT_DERIVED
   17941        54036 :       && c->ts.u.derived
   17942        54036 :       && c->ts.u.derived->components
   17943        50708 :       && c->attr.pointer
   17944        34467 :       && sym != c->ts.u.derived)
   17945         4361 :     add_dt_to_dt_list (c->ts.u.derived);
   17946              : 
   17947       286707 :   if (c->as && c->as->type != AS_DEFERRED
   17948         6548 :       && (c->attr.pointer || c->attr.allocatable))
   17949              :     return false;
   17950              : 
   17951       286693 :   if (!gfc_resolve_array_spec (c->as,
   17952       286693 :                                !(c->attr.pointer || c->attr.proc_pointer
   17953       233431 :                                  || c->attr.allocatable)))
   17954              :     return false;
   17955              : 
   17956       108884 :   if (c->initializer && !sym->attr.vtype
   17957        33333 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   17958       316852 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   17959              :     return false;
   17960              : 
   17961              :   return true;
   17962              : }
   17963              : 
   17964              : 
   17965              : /* Be nice about the locus for a structure expression - show the locus of the
   17966              :    first non-null sub-expression if we can.  */
   17967              : 
   17968              : static locus *
   17969            4 : cons_where (gfc_expr *struct_expr)
   17970              : {
   17971            4 :   gfc_constructor *cons;
   17972              : 
   17973            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   17974              : 
   17975            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   17976           12 :   for (; cons; cons = gfc_constructor_next (cons))
   17977              :     {
   17978            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   17979            4 :         return &cons->expr->where;
   17980              :     }
   17981              : 
   17982            0 :   return &struct_expr->where;
   17983              : }
   17984              : 
   17985              : /* Resolve the components of a structure type. Much less work than derived
   17986              :    types.  */
   17987              : 
   17988              : static bool
   17989          913 : resolve_fl_struct (gfc_symbol *sym)
   17990              : {
   17991          913 :   gfc_component *c;
   17992          913 :   gfc_expr *init = NULL;
   17993          913 :   bool success;
   17994              : 
   17995              :   /* Make sure UNIONs do not have overlapping initializers.  */
   17996          913 :   if (sym->attr.flavor == FL_UNION)
   17997              :     {
   17998          498 :       for (c = sym->components; c; c = c->next)
   17999              :         {
   18000          331 :           if (init && c->initializer)
   18001              :             {
   18002            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   18003              :                          cons_where (init), cons_where (c->initializer));
   18004            2 :               gfc_free_expr (c->initializer);
   18005            2 :               c->initializer = NULL;
   18006              :             }
   18007              :           if (init == NULL)
   18008          291 :             init = c->initializer;
   18009              :         }
   18010              :     }
   18011              : 
   18012          913 :   success = true;
   18013         2830 :   for (c = sym->components; c; c = c->next)
   18014         1917 :     if (!resolve_component (c, sym))
   18015            0 :       success = false;
   18016              : 
   18017          913 :   if (!success)
   18018              :     return false;
   18019              : 
   18020          913 :   if (sym->components)
   18021          862 :     add_dt_to_dt_list (sym);
   18022              : 
   18023              :   return true;
   18024              : }
   18025              : 
   18026              : /* Figure if the derived type is using itself directly in one of its components
   18027              :    or through referencing other derived types.  The information is required to
   18028              :    generate the __deallocate and __final type bound procedures to ensure
   18029              :    freeing larger hierarchies of derived types with allocatable objects.  */
   18030              : 
   18031              : static void
   18032       141370 : resolve_cyclic_derived_type (gfc_symbol *derived)
   18033              : {
   18034       141370 :   hash_set<gfc_symbol *> seen, to_examin;
   18035       141370 :   gfc_component *c;
   18036       141370 :   seen.add (derived);
   18037       141370 :   to_examin.add (derived);
   18038       474204 :   while (!to_examin.is_empty ())
   18039              :     {
   18040       193752 :       gfc_symbol *cand = *to_examin.begin ();
   18041       193752 :       to_examin.remove (cand);
   18042       521874 :       for (c = cand->components; c; c = c->next)
   18043       330410 :         if (c->ts.type == BT_DERIVED)
   18044              :           {
   18045        73434 :             if (c->ts.u.derived == derived)
   18046              :               {
   18047         1216 :                 derived->attr.recursive = 1;
   18048         2288 :                 return;
   18049              :               }
   18050        72218 :             else if (!seen.contains (c->ts.u.derived))
   18051              :               {
   18052        47763 :                 seen.add (c->ts.u.derived);
   18053        47763 :                 to_examin.add (c->ts.u.derived);
   18054              :               }
   18055              :           }
   18056       256976 :         else if (c->ts.type == BT_CLASS)
   18057              :           {
   18058         9876 :             if (!c->attr.class_ok)
   18059            7 :               continue;
   18060         9869 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   18061              :               {
   18062         1072 :                 derived->attr.recursive = 1;
   18063         1072 :                 return;
   18064              :               }
   18065         8797 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   18066              :               {
   18067         4937 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   18068         4937 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   18069              :               }
   18070              :           }
   18071              :     }
   18072       141370 : }
   18073              : 
   18074              : /* Resolve the components of a derived type. This does not have to wait until
   18075              :    resolution stage, but can be done as soon as the dt declaration has been
   18076              :    parsed.  */
   18077              : 
   18078              : static bool
   18079       174463 : resolve_fl_derived0 (gfc_symbol *sym)
   18080              : {
   18081       174463 :   gfc_symbol* super_type;
   18082       174463 :   gfc_component *c;
   18083       174463 :   gfc_formal_arglist *f;
   18084       174463 :   bool success;
   18085              : 
   18086       174463 :   if (sym->attr.unlimited_polymorphic)
   18087              :     return true;
   18088              : 
   18089       174463 :   super_type = gfc_get_derived_super_type (sym);
   18090              : 
   18091              :   /* F2008, C432.  */
   18092       174463 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   18093              :     {
   18094            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   18095              :                  "parent type %qs shall also have one", sym->name,
   18096              :                  &sym->declared_at, super_type->name);
   18097            2 :       return false;
   18098              :     }
   18099              : 
   18100              :   /* Ensure the extended type gets resolved before we do.  */
   18101        18017 :   if (super_type && !resolve_fl_derived0 (super_type))
   18102              :     return false;
   18103              : 
   18104              :   /* An ABSTRACT type must be extensible.  */
   18105       174455 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   18106              :     {
   18107            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   18108              :                  sym->name, &sym->declared_at);
   18109            2 :       return false;
   18110              :     }
   18111              : 
   18112              :   /* Resolving components below, may create vtabs for which the cyclic type
   18113              :      information needs to be present.  */
   18114       174453 :   if (!sym->attr.vtype)
   18115       141370 :     resolve_cyclic_derived_type (sym);
   18116              : 
   18117       174453 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   18118              :                            : sym->components;
   18119              : 
   18120       174453 :   success = true;
   18121       593124 :   for ( ; c != NULL; c = c->next)
   18122       418671 :     if (!resolve_component (c, sym))
   18123           96 :       success = false;
   18124              : 
   18125       174453 :   if (!success)
   18126              :     return false;
   18127              : 
   18128              :   /* Now add the caf token field, where needed.  */
   18129       174367 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   18130         1016 :       && !sym->attr.vtype)
   18131              :     {
   18132         2272 :       for (c = sym->components; c; c = c->next)
   18133         1459 :         if (!c->attr.dimension && !c->attr.codimension
   18134          803 :             && (c->attr.allocatable || c->attr.pointer))
   18135              :           {
   18136          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   18137          146 :             gfc_component *token;
   18138          146 :             sprintf (name, "_caf_%s", c->name);
   18139          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   18140          146 :             if (token == NULL)
   18141              :               {
   18142           82 :                 if (!gfc_add_component (sym, name, &token))
   18143            0 :                   return false;
   18144           82 :                 token->ts.type = BT_VOID;
   18145           82 :                 token->ts.kind = gfc_default_integer_kind;
   18146           82 :                 token->attr.access = ACCESS_PRIVATE;
   18147           82 :                 token->attr.artificial = 1;
   18148           82 :                 token->attr.caf_token = 1;
   18149              :               }
   18150          146 :             c->caf_token = token;
   18151              :           }
   18152              :     }
   18153              : 
   18154       174367 :   check_defined_assignments (sym);
   18155              : 
   18156       174367 :   if (!sym->attr.defined_assign_comp && super_type)
   18157        17010 :     sym->attr.defined_assign_comp
   18158        17010 :                         = super_type->attr.defined_assign_comp;
   18159              : 
   18160              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   18161              :      all DEFERRED bindings are overridden.  */
   18162        18010 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   18163         1457 :       && !sym->attr.is_class
   18164         3237 :       && !ensure_not_abstract (sym, super_type))
   18165              :     return false;
   18166              : 
   18167              :   /* Check that there is a component for every PDT parameter.  */
   18168       174361 :   if (sym->attr.pdt_template)
   18169              :     {
   18170         2502 :       for (f = sym->formal; f; f = f->next)
   18171              :         {
   18172         1444 :           if (!f->sym)
   18173            1 :             continue;
   18174         1443 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   18175         1443 :           if (c == NULL)
   18176              :             {
   18177            9 :               gfc_error ("Parameterized type %qs does not have a component "
   18178              :                          "corresponding to parameter %qs at %L", sym->name,
   18179            9 :                          f->sym->name, &sym->declared_at);
   18180            9 :               break;
   18181              :             }
   18182              :         }
   18183              :     }
   18184              : 
   18185              :   /* Add derived type to the derived type list.  */
   18186       174361 :   add_dt_to_dt_list (sym);
   18187              : 
   18188       174361 :   return true;
   18189              : }
   18190              : 
   18191              : /* The following procedure does the full resolution of a derived type,
   18192              :    including resolution of all type-bound procedures (if present). In contrast
   18193              :    to 'resolve_fl_derived0' this can only be done after the module has been
   18194              :    parsed completely.  */
   18195              : 
   18196              : static bool
   18197        90981 : resolve_fl_derived (gfc_symbol *sym)
   18198              : {
   18199        90981 :   gfc_symbol *gen_dt = NULL;
   18200              : 
   18201        90981 :   if (sym->attr.unlimited_polymorphic)
   18202              :     return true;
   18203              : 
   18204        90981 :   if (!sym->attr.is_class)
   18205        77827 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   18206        58143 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   18207         2291 :       && (!gen_dt->generic->sym->attr.use_assoc
   18208         2148 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   18209        91157 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   18210              :                           "%qs at %L being the same name as derived "
   18211              :                           "type at %L", sym->name,
   18212              :                           gen_dt->generic->sym == sym
   18213           11 :                           ? gen_dt->generic->next->sym->name
   18214              :                           : gen_dt->generic->sym->name,
   18215              :                           gen_dt->generic->sym == sym
   18216           11 :                           ? &gen_dt->generic->next->sym->declared_at
   18217              :                           : &gen_dt->generic->sym->declared_at,
   18218              :                           &sym->declared_at))
   18219              :     return false;
   18220              : 
   18221        90977 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   18222              :     {
   18223           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   18224              :                   sym->name, &sym->declared_at);
   18225           13 :       return false;
   18226              :     }
   18227              : 
   18228              :   /* Resolve the finalizer procedures.  */
   18229        90964 :   if (!gfc_resolve_finalizers (sym, NULL))
   18230              :     return false;
   18231              : 
   18232        90961 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   18233              :     {
   18234              :       /* Fix up incomplete CLASS symbols.  */
   18235        13154 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   18236        13154 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18237              : 
   18238        13154 :       if (data->ts.u.derived->attr.pdt_template)
   18239              :         {
   18240            0 :           match m;
   18241            0 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   18242              :                                     &data->param_list);
   18243            0 :           if (m != MATCH_YES
   18244            0 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   18245              :             {
   18246            0 :               gfc_error ("Failed to build PDT class component at %L",
   18247              :                          &sym->declared_at);
   18248            0 :               return false;
   18249              :             }
   18250            0 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   18251            0 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   18252              :         }
   18253              : 
   18254              :       /* Nothing more to do for unlimited polymorphic entities.  */
   18255        13154 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   18256              :         {
   18257         2145 :           add_dt_to_dt_list (sym);
   18258         2145 :           return true;
   18259              :         }
   18260        11009 :       else if (vptr->ts.u.derived == NULL)
   18261              :         {
   18262         6517 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   18263         6517 :           gcc_assert (vtab);
   18264         6517 :           vptr->ts.u.derived = vtab->ts.u.derived;
   18265         6517 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   18266              :             return false;
   18267              :         }
   18268              :     }
   18269              : 
   18270        88816 :   if (!resolve_fl_derived0 (sym))
   18271              :     return false;
   18272              : 
   18273              :   /* Resolve the type-bound procedures.  */
   18274        88732 :   if (!resolve_typebound_procedures (sym))
   18275              :     return false;
   18276              : 
   18277              :   /* Generate module vtables subject to their accessibility and their not
   18278              :      being vtables or pdt templates. If this is not done class declarations
   18279              :      in external procedures wind up with their own version and so SELECT TYPE
   18280              :      fails because the vptrs do not have the same address.  */
   18281        88691 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   18282        88630 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   18283        66457 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   18284        22339 :       && sym->attr.access != ACCESS_PRIVATE
   18285        22306 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   18286              :     {
   18287        20020 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   18288        20020 :       gfc_set_sym_referenced (vtab);
   18289              :     }
   18290              : 
   18291              :   return true;
   18292              : }
   18293              : 
   18294              : 
   18295              : static bool
   18296          875 : resolve_fl_namelist (gfc_symbol *sym)
   18297              : {
   18298          875 :   gfc_namelist *nl;
   18299          875 :   gfc_symbol *nlsym;
   18300              : 
   18301         3070 :   for (nl = sym->namelist; nl; nl = nl->next)
   18302              :     {
   18303              :       /* Check again, the check in match only works if NAMELIST comes
   18304              :          after the decl.  */
   18305         2200 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   18306              :         {
   18307            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   18308              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   18309            1 :           return false;
   18310              :         }
   18311              : 
   18312          678 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   18313         2207 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18314              :                               "with assumed shape in namelist %qs at %L",
   18315              :                               nl->sym->name, sym->name, &sym->declared_at))
   18316              :         return false;
   18317              : 
   18318         2198 :       if (is_non_constant_shape_array (nl->sym)
   18319         2248 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   18320              :                               "with nonconstant shape in namelist %qs at %L",
   18321           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   18322              :         return false;
   18323              : 
   18324         2197 :       if (nl->sym->ts.type == BT_CHARACTER
   18325          605 :           && (nl->sym->ts.u.cl->length == NULL
   18326          566 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   18327         2279 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   18328              :                               "nonconstant character length in "
   18329           82 :                               "namelist %qs at %L", nl->sym->name,
   18330              :                               sym->name, &sym->declared_at))
   18331              :         return false;
   18332              : 
   18333              :     }
   18334              : 
   18335              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   18336          870 :   if (gfc_check_symbol_access (sym))
   18337              :     {
   18338         3051 :       for (nl = sym->namelist; nl; nl = nl->next)
   18339              :         {
   18340         2194 :           if (!nl->sym->attr.use_assoc
   18341         4092 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   18342         4218 :               && !gfc_check_symbol_access (nl->sym))
   18343              :             {
   18344            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   18345              :                          "cannot be member of PUBLIC namelist %qs at %L",
   18346            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   18347            2 :               return false;
   18348              :             }
   18349              : 
   18350         2192 :           if (nl->sym->ts.type == BT_DERIVED
   18351          472 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   18352          470 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   18353              :            {
   18354            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   18355              :                                   "namelist %qs at %L with ALLOCATABLE "
   18356              :                                   "or POINTER components", nl->sym->name,
   18357              :                                   sym->name, &sym->declared_at))
   18358              :                return false;
   18359              :              return true;
   18360              :            }
   18361              : 
   18362              :           /* Types with private components that came here by USE-association.  */
   18363         2187 :           if (nl->sym->ts.type == BT_DERIVED
   18364         2187 :               && derived_inaccessible (nl->sym->ts.u.derived))
   18365              :             {
   18366            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   18367              :                          "components and cannot be member of namelist %qs at %L",
   18368              :                          nl->sym->name, sym->name, &sym->declared_at);
   18369            6 :               return false;
   18370              :             }
   18371              : 
   18372              :           /* Types with private components that are defined in the same module.  */
   18373         2181 :           if (nl->sym->ts.type == BT_DERIVED
   18374          922 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18375         2465 :               && nl->sym->ts.u.derived->attr.private_comp)
   18376              :             {
   18377            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18378              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18379              :                          nl->sym->name, sym->name, &sym->declared_at);
   18380            0 :               return false;
   18381              :             }
   18382              :         }
   18383              :     }
   18384              : 
   18385              : 
   18386              :   /* 14.1.2 A module or internal procedure represent local entities
   18387              :      of the same type as a namelist member and so are not allowed.  */
   18388         3035 :   for (nl = sym->namelist; nl; nl = nl->next)
   18389              :     {
   18390         2181 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18391         1616 :         continue;
   18392              : 
   18393          565 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18394            7 :         if ((nl->sym == sym->ns->proc_name)
   18395            1 :                ||
   18396            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18397            6 :           continue;
   18398              : 
   18399          559 :       nlsym = NULL;
   18400          559 :       if (nl->sym->name)
   18401          559 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18402          559 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18403              :         {
   18404            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18405              :                      "attribute in %qs at %L", nlsym->name,
   18406              :                      &sym->declared_at);
   18407            3 :           return false;
   18408              :         }
   18409              :     }
   18410              : 
   18411              :   return true;
   18412              : }
   18413              : 
   18414              : 
   18415              : static bool
   18416       411188 : resolve_fl_parameter (gfc_symbol *sym)
   18417              : {
   18418              :   /* A parameter array's shape needs to be constant.  */
   18419       411188 :   if (sym->as != NULL
   18420       411188 :       && (sym->as->type == AS_DEFERRED
   18421         6291 :           || is_non_constant_shape_array (sym)))
   18422              :     {
   18423           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18424              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18425           17 :       return false;
   18426              :     }
   18427              : 
   18428              :   /* Constraints on deferred type parameter.  */
   18429       411171 :   if (!deferred_requirements (sym))
   18430              :     return false;
   18431              : 
   18432              :   /* Make sure a parameter that has been implicitly typed still
   18433              :      matches the implicit type, since PARAMETER statements can precede
   18434              :      IMPLICIT statements.  */
   18435       411170 :   if (sym->attr.implicit_type
   18436       411883 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18437          713 :                                                              sym->ns)))
   18438              :     {
   18439            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18440              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18441            0 :       return false;
   18442              :     }
   18443              : 
   18444              :   /* Make sure the types of derived parameters are consistent.  This
   18445              :      type checking is deferred until resolution because the type may
   18446              :      refer to a derived type from the host.  */
   18447       411170 :   if (sym->ts.type == BT_DERIVED
   18448       411170 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18449              :     {
   18450            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18451            0 :                  &sym->value->where);
   18452            0 :       return false;
   18453              :     }
   18454              : 
   18455              :   /* F03:C509,C514.  */
   18456       411170 :   if (sym->ts.type == BT_CLASS)
   18457              :     {
   18458            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18459              :                  sym->name, &sym->declared_at);
   18460            0 :       return false;
   18461              :     }
   18462              : 
   18463              :   /* Some programmers can have a typo when using an implied-do loop to
   18464              :      initialize an array constant.  For example,
   18465              :        INTEGER I,J
   18466              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18467              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18468              :      This check catches the typo.  */
   18469       411170 :   if (sym->attr.dimension
   18470         6284 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18471       417448 :       && !gfc_is_constant_expr (sym->value))
   18472              :     {
   18473              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18474              :          the array constructor of a parameter.  This seems inconsistent with
   18475              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18476           20 :       if (sym->value->ts.type == BT_DERIVED
   18477           18 :           && sym->value->ts.u.derived
   18478           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18479              :         return true;
   18480            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18481            2 :       return false;
   18482              :     }
   18483              : 
   18484              :   return true;
   18485              : }
   18486              : 
   18487              : 
   18488              : /* Called by resolve_symbol to check PDTs.  */
   18489              : 
   18490              : static void
   18491         1462 : resolve_pdt (gfc_symbol* sym)
   18492              : {
   18493         1462 :   gfc_symbol *derived = NULL;
   18494         1462 :   gfc_actual_arglist *param;
   18495         1462 :   gfc_component *c;
   18496         1462 :   bool const_len_exprs = true;
   18497         1462 :   bool assumed_len_exprs = false;
   18498         1462 :   symbol_attribute *attr;
   18499              : 
   18500         1462 :   if (sym->ts.type == BT_DERIVED)
   18501              :     {
   18502         1223 :       derived = sym->ts.u.derived;
   18503         1223 :       attr = &(sym->attr);
   18504              :     }
   18505          239 :   else if (sym->ts.type == BT_CLASS)
   18506              :     {
   18507          239 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18508          239 :       attr = &(CLASS_DATA (sym)->attr);
   18509              :     }
   18510              :   else
   18511            0 :     gcc_unreachable ();
   18512              : 
   18513         1462 :   gcc_assert (derived->attr.pdt_type);
   18514              : 
   18515         3447 :   for (param = sym->param_list; param; param = param->next)
   18516              :     {
   18517         1985 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18518         1985 :       gcc_assert (c);
   18519         1985 :       if (c->attr.pdt_kind)
   18520         1042 :         continue;
   18521              : 
   18522          662 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18523         1039 :           && c->attr.pdt_len)
   18524              :         const_len_exprs = false;
   18525          847 :       else if (param->spec_type == SPEC_ASSUMED)
   18526          303 :         assumed_len_exprs = true;
   18527              : 
   18528          943 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18529           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18530           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18531            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18532              :                    "parameter %qs and requires either the POINTER "
   18533              :                    "or ALLOCATABLE attribute",
   18534              :                    sym->name, &sym->declared_at,
   18535              :                    param->name);
   18536              : 
   18537              :     }
   18538              : 
   18539         1462 :   if (!const_len_exprs
   18540           96 :       && (sym->ns->proc_name->attr.is_main_program
   18541           95 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18542           94 :           || sym->attr.save != SAVE_NONE))
   18543            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18544              :                "SAVE attribute or be a variable declared in the "
   18545              :                "main program, a module or a submodule(F08/C513)",
   18546              :                sym->name, &sym->declared_at);
   18547              : 
   18548         1462 :   if (assumed_len_exprs && !(sym->attr.dummy
   18549            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18550            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18551              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18552              :                sym->name, &sym->declared_at);
   18553         1462 : }
   18554              : 
   18555              : 
   18556              : /* Resolve the symbol's array spec.  */
   18557              : 
   18558              : static bool
   18559      1781618 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18560              : {
   18561      1781618 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18562      1781618 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18563              : 
   18564      1781618 :   bool saved_specification_expr = specification_expr;
   18565      1781618 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18566      1781618 :   specification_expr = true;
   18567      1781618 :   specification_expr_symbol = sym;
   18568              : 
   18569      1781618 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18570              : 
   18571      1781618 :   specification_expr = saved_specification_expr;
   18572      1781618 :   specification_expr_symbol = saved_specification_expr_symbol;
   18573      1781618 :   gfc_current_ns = orig_current_ns;
   18574              : 
   18575      1781618 :   return result;
   18576              : }
   18577              : 
   18578              : 
   18579              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18580              :    assume that an otherwise unknown symbol is a variable.  This sort
   18581              :    of thing commonly happens for symbols in module.  */
   18582              : 
   18583              : static void
   18584      1943646 : resolve_symbol (gfc_symbol *sym)
   18585              : {
   18586      1943646 :   int check_constant, mp_flag;
   18587      1943646 :   gfc_symtree *symtree;
   18588      1943646 :   gfc_symtree *this_symtree;
   18589      1943646 :   gfc_namespace *ns;
   18590      1943646 :   gfc_component *c;
   18591      1943646 :   symbol_attribute class_attr;
   18592      1943646 :   gfc_array_spec *as;
   18593      1943646 :   bool declared_has_coarray_comp = false;
   18594              : 
   18595      1943646 :   if (sym->resolve_symbol_called >= 1)
   18596       193459 :     return;
   18597      1854368 :   sym->resolve_symbol_called = 1;
   18598              : 
   18599              :   /* No symbol will ever have union type; only components can be unions.
   18600              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18601              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18602      1854368 :   gcc_assert (sym->ts.type != BT_UNION);
   18603              : 
   18604              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18605              :      yet unsupported for -fcoarray=lib.  */
   18606      1854368 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18607          112 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18608          112 :       && CLASS_DATA (sym)->attr.codimension
   18609           94 :       && CLASS_DATA (sym)->ts.u.derived
   18610           93 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18611           90 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18612              :     {
   18613            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18614              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18615            6 :       return;
   18616              :     }
   18617              : 
   18618      1854362 :   if (sym->attr.artificial)
   18619              :     return;
   18620              : 
   18621      1752966 :   if (sym->attr.unlimited_polymorphic)
   18622              :     return;
   18623              : 
   18624      1751427 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18625              :     {
   18626            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18627              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18628            4 :       return;
   18629              :     }
   18630              : 
   18631      1751423 :   if (sym->attr.flavor == FL_UNKNOWN
   18632      1729996 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18633       463435 :           && !sym->attr.generic && !sym->attr.external
   18634       183032 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18635        82419 :           && sym->ts.type == BT_UNKNOWN))
   18636              :     {
   18637              :       /* A symbol in a common block might not have been resolved yet properly.
   18638              :          Do not try to find an interface with the same name.  */
   18639        95297 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18640        21423 :           && !sym->attr.generic && !sym->attr.external
   18641        21372 :           && sym->attr.in_common)
   18642         2594 :         goto skip_interfaces;
   18643              : 
   18644              :     /* If we find that a flavorless symbol is an interface in one of the
   18645              :        parent namespaces, find its symtree in this namespace, free the
   18646              :        symbol and set the symtree to point to the interface symbol.  */
   18647       132718 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18648              :         {
   18649        40722 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   18650        40722 :           if (symtree && (symtree->n.sym->generic ||
   18651          776 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   18652          680 :                            && sym->ns->construct_entities)))
   18653              :             {
   18654          715 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   18655              :                                                sym->name);
   18656          715 :               if (this_symtree->n.sym == sym)
   18657              :                 {
   18658          707 :                   symtree->n.sym->refs++;
   18659          707 :                   gfc_release_symbol (sym);
   18660          707 :                   this_symtree->n.sym = symtree->n.sym;
   18661          707 :                   return;
   18662              :                 }
   18663              :             }
   18664              :         }
   18665              : 
   18666        91996 : skip_interfaces:
   18667              :       /* Otherwise give it a flavor according to such attributes as
   18668              :          it has.  */
   18669        94590 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   18670        21242 :           && sym->attr.intrinsic == 0)
   18671        21238 :         sym->attr.flavor = FL_VARIABLE;
   18672        73352 :       else if (sym->attr.flavor == FL_UNKNOWN)
   18673              :         {
   18674           55 :           sym->attr.flavor = FL_PROCEDURE;
   18675           55 :           if (sym->attr.dimension)
   18676            0 :             sym->attr.function = 1;
   18677              :         }
   18678              :     }
   18679              : 
   18680      1750716 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   18681         2384 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   18682              : 
   18683         1530 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   18684      1752246 :       && !resolve_procedure_interface (sym))
   18685              :     return;
   18686              : 
   18687      1750705 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   18688          130 :       && (sym->attr.procedure || sym->attr.external))
   18689              :     {
   18690            0 :       if (sym->attr.external)
   18691            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   18692              :                    "at %L", &sym->declared_at);
   18693              :       else
   18694            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   18695              :                    "at %L", &sym->declared_at);
   18696              : 
   18697              :       return;
   18698              :     }
   18699              : 
   18700              :   /* Ensure that variables of derived or class type having a finalizer are
   18701              :      marked used even when the variable is not used anything else in the scope.
   18702              :      This fixes PR118730.  */
   18703       677665 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   18704       469042 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   18705      1801402 :       && gfc_may_be_finalized (sym->ts))
   18706         8879 :     gfc_set_sym_referenced (sym);
   18707              : 
   18708      1750705 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   18709              :     return;
   18710              : 
   18711      1750560 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   18712      1750560 :            && !resolve_fl_struct (sym))
   18713              :     return;
   18714              : 
   18715              :   /* Symbols that are module procedures with results (functions) have
   18716              :      the types and array specification copied for type checking in
   18717              :      procedures that call them, as well as for saving to a module
   18718              :      file.  These symbols can't stand the scrutiny that their results
   18719              :      can.  */
   18720      1750560 :   mp_flag = (sym->result != NULL && sym->result != sym);
   18721              : 
   18722              :   /* Make sure that the intrinsic is consistent with its internal
   18723              :      representation. This needs to be done before assigning a default
   18724              :      type to avoid spurious warnings.  */
   18725      1715125 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   18726      1787786 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   18727              :     return;
   18728              : 
   18729              :   /* Resolve associate names.  */
   18730      1750524 :   if (sym->assoc)
   18731         7015 :     resolve_assoc_var (sym, true);
   18732              : 
   18733              :   /* Assign default type to symbols that need one and don't have one.  */
   18734      1750524 :   if (sym->ts.type == BT_UNKNOWN)
   18735              :     {
   18736       419048 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   18737              :         {
   18738        11842 :           gfc_set_default_type (sym, 1, NULL);
   18739              :         }
   18740              : 
   18741       272524 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   18742        65799 :           && !sym->attr.function && !sym->attr.subroutine
   18743       420721 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   18744          622 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   18745              : 
   18746       419048 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18747              :         {
   18748              :           /* The specific case of an external procedure should emit an error
   18749              :              in the case that there is no implicit type.  */
   18750       104595 :           if (!mp_flag)
   18751              :             {
   18752        98450 :               if (!sym->attr.mixed_entry_master)
   18753        98342 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   18754              :             }
   18755              :           else
   18756              :             {
   18757              :               /* Result may be in another namespace.  */
   18758         6145 :               resolve_symbol (sym->result);
   18759              : 
   18760         6145 :               if (!sym->result->attr.proc_pointer)
   18761              :                 {
   18762         5965 :                   sym->ts = sym->result->ts;
   18763         5965 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   18764         5965 :                   sym->attr.dimension = sym->result->attr.dimension;
   18765         5965 :                   sym->attr.codimension = sym->result->attr.codimension;
   18766         5965 :                   sym->attr.pointer = sym->result->attr.pointer;
   18767         5965 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   18768         5965 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   18769              :                 }
   18770              :             }
   18771              :         }
   18772              :     }
   18773      1331476 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18774        31428 :     resolve_symbol_array_spec (sym->result, false);
   18775              : 
   18776              :   /* For a CLASS-valued function with a result variable, affirm that it has
   18777              :      been resolved also when looking at the symbol 'sym'.  */
   18778       450476 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   18779          745 :     sym->attr.class_ok = sym->result->attr.class_ok;
   18780              : 
   18781      1750524 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   18782        20128 :       && CLASS_DATA (sym))
   18783              :     {
   18784        20128 :       as = CLASS_DATA (sym)->as;
   18785        20128 :       class_attr = CLASS_DATA (sym)->attr;
   18786        20128 :       class_attr.pointer = class_attr.class_pointer;
   18787        20128 :       declared_has_coarray_comp = CLASS_DATA (sym)->ts.u.derived
   18788        20128 :                                   && CLASS_DATA (sym)->ts.u.derived->attr.coarray_comp;
   18789              :     }
   18790              :   else
   18791              :     {
   18792      1730396 :       class_attr = sym->attr;
   18793      1730396 :       as = sym->as;
   18794              :     }
   18795              : 
   18796              :   /* F2008, C530.  */
   18797      1750524 :   if (sym->attr.contiguous
   18798         8546 :       && !sym->attr.associate_var
   18799         8545 :       && (!class_attr.dimension
   18800         8542 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   18801          140 :               && !class_attr.pointer)))
   18802              :     {
   18803            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   18804              :                  "array pointer or an assumed-shape or assumed-rank array",
   18805              :                  sym->name, &sym->declared_at);
   18806            7 :       return;
   18807              :     }
   18808              : 
   18809              :   /* Assumed size arrays and assumed shape arrays must be dummy
   18810              :      arguments.  Array-spec's of implied-shape should have been resolved to
   18811              :      AS_EXPLICIT already.  */
   18812              : 
   18813      1742115 :   if (as)
   18814              :     {
   18815              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   18816              :          specification expression.  */
   18817       151910 :       if (as->type == AS_IMPLIED_SHAPE)
   18818              :         {
   18819              :           int i;
   18820            1 :           for (i=0; i<as->rank; i++)
   18821              :             {
   18822            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   18823              :                 {
   18824            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   18825              :                              &as->lower[i]->where);
   18826            1 :                   return;
   18827              :                 }
   18828              :             }
   18829            0 :           gcc_unreachable();
   18830              :         }
   18831              : 
   18832       151909 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   18833       116780 :            || as->type == AS_ASSUMED_SHAPE)
   18834        47227 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   18835            8 :           && !sym->attr.associate_var)
   18836              :         {
   18837            7 :           if (as->type == AS_ASSUMED_SIZE)
   18838            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   18839              :                        &sym->declared_at);
   18840              :           else
   18841            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   18842              :                        &sym->declared_at);
   18843              :           return;
   18844              :         }
   18845              :       /* TS 29113, C535a.  */
   18846       151902 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   18847           60 :           && !sym->attr.select_type_temporary
   18848           60 :           && !(cs_base && cs_base->current
   18849           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   18850            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   18851            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   18852              :         {
   18853           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   18854              :                      &sym->declared_at);
   18855           18 :           return;
   18856              :         }
   18857       151884 :       if (as->type == AS_ASSUMED_RANK
   18858        27380 :           && (sym->attr.codimension || sym->attr.value))
   18859              :         {
   18860            2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   18861              :                      "CODIMENSION attribute", &sym->declared_at);
   18862            2 :           return;
   18863              :         }
   18864              :     }
   18865              : 
   18866              :   /* Make sure symbols with known intent or optional are really dummy
   18867              :      variable.  Because of ENTRY statement, this has to be deferred
   18868              :      until resolution time.  */
   18869              : 
   18870      1750489 :   if (!sym->attr.dummy
   18871      1257329 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   18872              :     {
   18873            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   18874            2 :       return;
   18875              :     }
   18876              : 
   18877      1750487 :   if (sym->attr.value && !sym->attr.dummy)
   18878              :     {
   18879            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   18880              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   18881            2 :       return;
   18882              :     }
   18883              : 
   18884      1750485 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   18885              :     {
   18886          616 :       gfc_charlen *cl = sym->ts.u.cl;
   18887          616 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   18888              :         {
   18889            2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   18890              :                      "attribute must have constant length",
   18891              :                      sym->name, &sym->declared_at);
   18892            2 :           return;
   18893              :         }
   18894              : 
   18895          614 :       if (sym->ts.is_c_interop
   18896          381 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   18897              :         {
   18898            1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   18899              :                      "with VALUE attribute must have length one",
   18900              :                      sym->name, &sym->declared_at);
   18901            1 :           return;
   18902              :         }
   18903              :     }
   18904              : 
   18905      1750482 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18906       126222 :       && sym->ts.u.derived->attr.generic)
   18907              :     {
   18908           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   18909           20 :       if (!sym->ts.u.derived)
   18910              :         {
   18911            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   18912              :                      "which has not been defined", sym->name,
   18913              :                      &sym->declared_at, sym->ts.u.derived->name);
   18914            0 :           sym->ts.type = BT_UNKNOWN;
   18915            0 :           return;
   18916              :         }
   18917              :     }
   18918              : 
   18919              :     /* Use the same constraints as TYPE(*), except for the type check
   18920              :        and that only scalars and assumed-size arrays are permitted.  */
   18921      1750482 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   18922              :       {
   18923        14556 :         if (!sym->attr.dummy)
   18924              :           {
   18925            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18926              :                        "a dummy argument", sym->name, &sym->declared_at);
   18927            1 :             return;
   18928              :           }
   18929              : 
   18930        14555 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   18931            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   18932            0 :             && sym->ts.type != BT_COMPLEX)
   18933              :           {
   18934            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18935              :                        "of type TYPE(*) or of an numeric intrinsic type",
   18936              :                        sym->name, &sym->declared_at);
   18937            0 :             return;
   18938              :           }
   18939              : 
   18940        14555 :       if (sym->attr.allocatable || sym->attr.codimension
   18941        14553 :           || sym->attr.pointer || sym->attr.value)
   18942              :         {
   18943            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18944              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   18945              :                      "attribute", sym->name, &sym->declared_at);
   18946            4 :           return;
   18947              :         }
   18948              : 
   18949        14551 :       if (sym->attr.intent == INTENT_OUT)
   18950              :         {
   18951            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18952              :                      "have the INTENT(OUT) attribute",
   18953              :                      sym->name, &sym->declared_at);
   18954            0 :           return;
   18955              :         }
   18956        14551 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   18957              :         {
   18958            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   18959              :                      "either be a scalar or an assumed-size array",
   18960              :                      sym->name, &sym->declared_at);
   18961            1 :           return;
   18962              :         }
   18963              : 
   18964              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   18965              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   18966              :          packing.  */
   18967        14550 :       sym->ts.type = BT_ASSUMED;
   18968        14550 :       sym->as = gfc_get_array_spec ();
   18969        14550 :       sym->as->type = AS_ASSUMED_SIZE;
   18970        14550 :       sym->as->rank = 1;
   18971        14550 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   18972              :     }
   18973      1735926 :   else if (sym->ts.type == BT_ASSUMED)
   18974              :     {
   18975              :       /* TS 29113, C407a.  */
   18976        12350 :       if (!sym->attr.dummy)
   18977              :         {
   18978            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   18979              :                      "for dummy variables", sym->name, &sym->declared_at);
   18980            7 :           return;
   18981              :         }
   18982        12343 :       if (sym->attr.allocatable || sym->attr.codimension
   18983        12339 :           || sym->attr.pointer || sym->attr.value)
   18984              :         {
   18985            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18986              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   18987              :                      sym->name, &sym->declared_at);
   18988            8 :           return;
   18989              :         }
   18990        12335 :       if (sym->attr.intent == INTENT_OUT)
   18991              :         {
   18992            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18993              :                      "INTENT(OUT) attribute",
   18994              :                      sym->name, &sym->declared_at);
   18995            2 :           return;
   18996              :         }
   18997        12333 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   18998              :         {
   18999            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   19000              :                      "explicit-shape array", sym->name, &sym->declared_at);
   19001            3 :           return;
   19002              :         }
   19003              :     }
   19004              : 
   19005              :   /* If the symbol is marked as bind(c), that it is declared at module level
   19006              :      scope and verify its type and kind.  Do not do the latter for symbols
   19007              :      that are implicitly typed because that is handled in
   19008              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   19009              :      separately.  Also, anything that is use associated is not handled here
   19010              :      but instead is handled in the module it is declared in.  Finally, derived
   19011              :      type definitions are allowed to be BIND(C) since that only implies that
   19012              :      they're interoperable, and they are checked fully for interoperability
   19013              :      when a variable is declared of that type.  */
   19014      1750456 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   19015         7812 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   19016          568 :       && sym->attr.flavor != FL_DERIVED)
   19017              :     {
   19018          168 :       bool t = true;
   19019              : 
   19020              :       /* First, make sure the variable is declared at the
   19021              :          module-level scope (J3/04-007, Section 15.3).  */
   19022          168 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   19023            7 :           && !sym->attr.in_common)
   19024              :         {
   19025            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   19026              :                      "is neither a COMMON block nor declared at the "
   19027              :                      "module level scope", sym->name, &(sym->declared_at));
   19028            6 :           t = false;
   19029              :         }
   19030          162 :       else if (sym->ts.type == BT_CHARACTER
   19031          162 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   19032            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   19033            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   19034              :         {
   19035            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   19036            1 :                      sym->name, &sym->declared_at);
   19037            1 :           t = false;
   19038              :         }
   19039          161 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   19040              :         {
   19041            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   19042              :         }
   19043          160 :       else if (sym->attr.implicit_type == 0)
   19044              :         {
   19045              :           /* If type() declaration, we need to verify that the components
   19046              :              of the given type are all C interoperable, etc.  */
   19047          158 :           if (sym->ts.type == BT_DERIVED &&
   19048           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   19049              :             {
   19050              :               /* Make sure the user marked the derived type as BIND(C).  If
   19051              :                  not, call the verify routine.  This could print an error
   19052              :                  for the derived type more than once if multiple variables
   19053              :                  of that type are declared.  */
   19054           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   19055            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   19056          158 :               t = false;
   19057              :             }
   19058              : 
   19059              :           /* Verify the variable itself as C interoperable if it
   19060              :              is BIND(C).  It is not possible for this to succeed if
   19061              :              the verify_bind_c_derived_type failed, so don't have to handle
   19062              :              any error returned by verify_bind_c_derived_type.  */
   19063          158 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   19064          158 :                                  sym->common_block);
   19065              :         }
   19066              : 
   19067          166 :       if (!t)
   19068              :         {
   19069              :           /* clear the is_bind_c flag to prevent reporting errors more than
   19070              :              once if something failed.  */
   19071           10 :           sym->attr.is_bind_c = 0;
   19072           10 :           return;
   19073              :         }
   19074              :     }
   19075              : 
   19076              :   /* If a derived type symbol has reached this point, without its
   19077              :      type being declared, we have an error.  Notice that most
   19078              :      conditions that produce undefined derived types have already
   19079              :      been dealt with.  However, the likes of:
   19080              :      implicit type(t) (t) ..... call foo (t) will get us here if
   19081              :      the type is not declared in the scope of the implicit
   19082              :      statement. Change the type to BT_UNKNOWN, both because it is so
   19083              :      and to prevent an ICE.  */
   19084      1750446 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   19085       126220 :       && sym->ts.u.derived->components == NULL
   19086         1151 :       && !sym->ts.u.derived->attr.zero_comp)
   19087              :     {
   19088            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   19089              :                  "which has not been defined", sym->name,
   19090              :                   &sym->declared_at, sym->ts.u.derived->name);
   19091            3 :       sym->ts.type = BT_UNKNOWN;
   19092            3 :       return;
   19093              :     }
   19094              : 
   19095              :   /* Make sure that the derived type has been resolved and that the
   19096              :      derived type is visible in the symbol's namespace, if it is a
   19097              :      module function and is not PRIVATE.  */
   19098      1750443 :   if (sym->ts.type == BT_DERIVED
   19099       133373 :         && sym->ts.u.derived->attr.use_assoc
   19100       115516 :         && sym->ns->proc_name
   19101       115508 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   19102      1756398 :         && !resolve_fl_derived (sym->ts.u.derived))
   19103              :     return;
   19104              : 
   19105              :   /* Unless the derived-type declaration is use associated, Fortran 95
   19106              :      does not allow public entries of private derived types.
   19107              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   19108              :      161 in 95-006r3.  */
   19109      1750443 :   if (sym->ts.type == BT_DERIVED
   19110       133373 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   19111         8099 :       && !sym->ts.u.derived->attr.use_assoc
   19112         2144 :       && gfc_check_symbol_access (sym)
   19113         1931 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   19114      1750457 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   19115              :                           "derived type %qs",
   19116           14 :                           (sym->attr.flavor == FL_PARAMETER)
   19117              :                           ? "parameter" : "variable",
   19118              :                           sym->name, &sym->declared_at,
   19119           14 :                           sym->ts.u.derived->name))
   19120              :     return;
   19121              : 
   19122              :   /* F2008, C1302.  */
   19123      1750436 :   if (sym->ts.type == BT_DERIVED
   19124       133366 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19125          160 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   19126       133335 :           || sym->ts.u.derived->attr.lock_comp)
   19127           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19128              :     {
   19129            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   19130              :                  "type LOCK_TYPE must be a coarray", sym->name,
   19131              :                  &sym->declared_at);
   19132            4 :       return;
   19133              :     }
   19134              : 
   19135              :   /* TS18508, C702/C703.  */
   19136      1750432 :   if (sym->ts.type == BT_DERIVED
   19137       133362 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   19138          159 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   19139       133345 :           || sym->ts.u.derived->attr.event_comp)
   19140           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   19141              :     {
   19142            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   19143              :                  "type EVENT_TYPE must be a coarray", sym->name,
   19144              :                  &sym->declared_at);
   19145            1 :       return;
   19146              :     }
   19147              : 
   19148              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   19149              :      default initialization is defined (5.1.2.4.4).  */
   19150      1750431 :   if (sym->ts.type == BT_DERIVED
   19151       133361 :       && sym->attr.dummy
   19152        45823 :       && sym->attr.intent == INTENT_OUT
   19153         2357 :       && sym->as
   19154          382 :       && sym->as->type == AS_ASSUMED_SIZE)
   19155              :     {
   19156            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   19157              :         {
   19158            1 :           if (c->initializer)
   19159              :             {
   19160            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   19161              :                          "ASSUMED SIZE and so cannot have a default initializer",
   19162              :                          sym->name, &sym->declared_at);
   19163            1 :               return;
   19164              :             }
   19165              :         }
   19166              :     }
   19167              : 
   19168              :   /* F2008, C542.  */
   19169      1750430 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19170        45822 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   19171              :     {
   19172            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   19173              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19174            0 :       return;
   19175              :     }
   19176              : 
   19177              :   /* TS18508.  */
   19178      1750430 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   19179        45822 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   19180              :     {
   19181            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   19182              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   19183            0 :       return;
   19184              :     }
   19185              : 
   19186              :   /* F2008, C525.  */
   19187      1750430 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19188      1750317 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19189        20131 :              && sym->ts.u.derived && CLASS_DATA (sym)
   19190        20126 :              && CLASS_DATA (sym)->attr.coarray_comp))
   19191      1750317 :        || class_attr.codimension)
   19192         1821 :       && (sym->attr.result || sym->result == sym))
   19193              :     {
   19194            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   19195              :                  "a coarray component", sym->name, &sym->declared_at);
   19196            8 :       return;
   19197              :     }
   19198              : 
   19199              :   /* F2008, C524.  */
   19200      1750422 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   19201          420 :       && sym->ts.u.derived->ts.is_iso_c)
   19202              :     {
   19203            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   19204              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   19205            3 :       return;
   19206              :     }
   19207              : 
   19208              :   /* F2008, C525.  */
   19209      1750419 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19210      1750309 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19211        20130 :             && sym->ts.u.derived && CLASS_DATA (sym)
   19212        20125 :             && CLASS_DATA (sym)->attr.coarray_comp))
   19213          110 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   19214          106 :           || class_attr.allocatable))
   19215              :     {
   19216            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   19217              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   19218              :                  sym->name, &sym->declared_at);
   19219            4 :       return;
   19220              :     }
   19221              : 
   19222              :   /* F2008, C526.  The function-result case was handled above.  */
   19223      1750415 :   if (class_attr.codimension
   19224         1700 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   19225          350 :            || sym->attr.select_type_temporary
   19226          274 :            || sym->attr.associate_var
   19227          256 :            || (sym->ns->save_all && !sym->attr.automatic)
   19228          256 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   19229          256 :            || sym->ns->proc_name->attr.is_main_program
   19230            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   19231              :     {
   19232            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   19233              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   19234            4 :       return;
   19235              :     }
   19236              :   /* F2008, C528.  */
   19237      1750411 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   19238         1620 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   19239              :     {
   19240            6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   19241              :                  "deferred shape without allocatable", sym->name,
   19242              :                  &sym->declared_at);
   19243            6 :       return;
   19244              :     }
   19245      1750405 :   else if (class_attr.codimension && class_attr.allocatable && as
   19246          626 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   19247              :     {
   19248            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   19249              :                  "deferred shape", sym->name, &sym->declared_at);
   19250            9 :       return;
   19251              :     }
   19252              : 
   19253              :   /* F2008, C541.  */
   19254      1750396 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   19255      1750290 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   19256        20125 :             && declared_has_coarray_comp))
   19257      1750283 :         || (class_attr.codimension && class_attr.allocatable))
   19258          730 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   19259              :     {
   19260            4 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   19261              :                  "allocatable coarray or have coarray components",
   19262              :                  sym->name, &sym->declared_at);
   19263            4 :       return;
   19264              :     }
   19265              : 
   19266      1750392 :   if (class_attr.codimension && sym->attr.dummy
   19267          469 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   19268              :     {
   19269            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   19270              :                  "procedure %qs", sym->name, &sym->declared_at,
   19271              :                  sym->ns->proc_name->name);
   19272            2 :       return;
   19273              :     }
   19274              : 
   19275      1750390 :   if (sym->ts.type == BT_LOGICAL
   19276       114403 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   19277       114400 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   19278        32612 :               && sym->ns->proc_name->attr.is_bind_c)))
   19279              :     {
   19280              :       int i;
   19281          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   19282          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   19283              :           break;
   19284           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   19285          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   19286              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   19287              :                               "%qs", sym->name, &sym->declared_at,
   19288           13 :                               sym->ns->proc_name->name))
   19289              :         return;
   19290          167 :       else if (!gfc_logical_kinds[i].c_bool
   19291          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   19292              :                                    "%qs at %L with non-C_Bool kind in "
   19293              :                                    "BIND(C) procedure %qs", sym->name,
   19294              :                                    &sym->declared_at,
   19295           15 :                                    sym->attr.function ? sym->name
   19296           13 :                                    : sym->ns->proc_name->name))
   19297              :         return;
   19298              :     }
   19299              : 
   19300      1750387 :   switch (sym->attr.flavor)
   19301              :     {
   19302       677547 :     case FL_VARIABLE:
   19303       677547 :       if (!resolve_fl_variable (sym, mp_flag))
   19304              :         return;
   19305              :       break;
   19306              : 
   19307       500078 :     case FL_PROCEDURE:
   19308       500078 :       if (sym->formal && !sym->formal_ns)
   19309              :         {
   19310              :           /* Check that none of the arguments are a namelist.  */
   19311              :           gfc_formal_arglist *formal = sym->formal;
   19312              : 
   19313       107094 :           for (; formal; formal = formal->next)
   19314        72566 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   19315              :               {
   19316            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   19317              :                            "subroutine or function at %L",
   19318              :                            formal->sym->name, &sym->declared_at);
   19319            1 :                 return;
   19320              :               }
   19321              :         }
   19322              : 
   19323       500077 :       if (!resolve_fl_procedure (sym, mp_flag))
   19324              :         return;
   19325              :       break;
   19326              : 
   19327          875 :     case FL_NAMELIST:
   19328          875 :       if (!resolve_fl_namelist (sym))
   19329              :         return;
   19330              :       break;
   19331              : 
   19332       411188 :     case FL_PARAMETER:
   19333       411188 :       if (!resolve_fl_parameter (sym))
   19334              :         return;
   19335              :       break;
   19336              : 
   19337              :     default:
   19338              :       break;
   19339              :     }
   19340              : 
   19341              :   /* Resolve array specifier. Check as well some constraints
   19342              :      on COMMON blocks.  */
   19343              : 
   19344      1750190 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   19345              : 
   19346      1750190 :   resolve_symbol_array_spec (sym, check_constant);
   19347              : 
   19348              :   /* Resolve formal namespaces.  */
   19349      1750190 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   19350       279205 :       && !sym->attr.contained && !sym->attr.intrinsic)
   19351       249477 :     gfc_resolve (sym->formal_ns);
   19352              : 
   19353              :   /* Make sure the formal namespace is present.  */
   19354      1750190 :   if (sym->formal && !sym->formal_ns)
   19355              :     {
   19356              :       gfc_formal_arglist *formal = sym->formal;
   19357        34997 :       while (formal && !formal->sym)
   19358           11 :         formal = formal->next;
   19359              : 
   19360        34986 :       if (formal)
   19361              :         {
   19362        34975 :           sym->formal_ns = formal->sym->ns;
   19363        34975 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   19364        26516 :             sym->formal_ns->refs++;
   19365              :         }
   19366              :     }
   19367              : 
   19368              :   /* Check threadprivate restrictions.  */
   19369      1750190 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   19370          384 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   19371           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19372           32 :       && sym->module == NULL
   19373           17 :       && (sym->ns->proc_name == NULL
   19374           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19375            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19376              :     {
   19377            2 :       if (sym->attr.threadprivate)
   19378            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19379              :       else
   19380            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19381              :                    "attribute", sym->name, &sym->declared_at);
   19382              :     }
   19383              : 
   19384      1750190 :   if (sym->attr.omp_groupprivate && sym->value)
   19385            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19386              :                "initializer", sym->name, &sym->declared_at);
   19387              : 
   19388              :   /* Check omp declare target restrictions.  */
   19389      1750190 :   if ((sym->attr.omp_declare_target
   19390      1748773 :        || sym->attr.omp_declare_target_link
   19391      1748725 :        || sym->attr.omp_declare_target_local)
   19392         1505 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19393         1458 :       && sym->attr.flavor == FL_VARIABLE
   19394          616 :       && !sym->attr.save
   19395          199 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19396          199 :       && (!sym->attr.in_common
   19397          186 :           && sym->module == NULL
   19398           96 :           && (sym->ns->proc_name == NULL
   19399           96 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19400            6 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19401            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19402              :                sym->name, &sym->declared_at);
   19403              : 
   19404              :   /* If we have come this far we can apply default-initializers, as
   19405              :      described in 14.7.5, to those variables that have not already
   19406              :      been assigned one.  */
   19407      1750190 :   if (sym->ts.type == BT_DERIVED
   19408       133331 :       && !sym->value
   19409       108023 :       && !sym->attr.allocatable
   19410       104980 :       && !sym->attr.alloc_comp)
   19411              :     {
   19412       104915 :       symbol_attribute *a = &sym->attr;
   19413              : 
   19414       104915 :       if ((!a->save && !a->dummy && !a->pointer
   19415        57705 :            && !a->in_common && !a->use_assoc
   19416        10667 :            && a->referenced
   19417         8378 :            && !((a->function || a->result)
   19418         1704 :                 && (!a->dimension
   19419          160 :                     || sym->ts.u.derived->attr.alloc_comp
   19420           95 :                     || sym->ts.u.derived->attr.pointer_comp))
   19421         6755 :            && !(a->function && sym != sym->result))
   19422        98180 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19423         1528 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19424         8164 :         apply_default_init (sym);
   19425        96751 :       else if (a->function && !a->pointer && !a->allocatable
   19426        21029 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19427              :         /* Default initialization for function results.  */
   19428         2752 :         apply_default_init (sym->result);
   19429        93999 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19430        12021 :                && (sym->ts.u.derived->attr.alloc_comp
   19431        11456 :                    || sym->ts.u.derived->attr.pointer_comp))
   19432              :         /* Mark the result symbol to be referenced, when it has allocatable
   19433              :            components.  */
   19434          624 :         sym->result->attr.referenced = 1;
   19435              :     }
   19436              : 
   19437      1750190 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19438        19612 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19439         1322 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19440         1247 :       && !CLASS_DATA (sym)->attr.class_pointer
   19441         1221 :       && !CLASS_DATA (sym)->attr.allocatable)
   19442          913 :     apply_default_init (sym);
   19443              : 
   19444              :   /* If this symbol has a type-spec, check it.  */
   19445      1750190 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19446       661565 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19447      1420713 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19448              :       return;
   19449              : 
   19450      1750187 :   if (sym->param_list)
   19451         1462 :     resolve_pdt (sym);
   19452              : }
   19453              : 
   19454              : 
   19455         4149 : void gfc_resolve_symbol (gfc_symbol *sym)
   19456              : {
   19457         4149 :   resolve_symbol (sym);
   19458         4149 :   return;
   19459              : }
   19460              : 
   19461              : 
   19462              : /************* Resolve DATA statements *************/
   19463              : 
   19464              : static struct
   19465              : {
   19466              :   gfc_data_value *vnode;
   19467              :   mpz_t left;
   19468              : }
   19469              : values;
   19470              : 
   19471              : 
   19472              : /* Advance the values structure to point to the next value in the data list.  */
   19473              : 
   19474              : static bool
   19475        10892 : next_data_value (void)
   19476              : {
   19477        16660 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19478              :     {
   19479              : 
   19480         8198 :       if (values.vnode->next == NULL)
   19481              :         return false;
   19482              : 
   19483         5768 :       values.vnode = values.vnode->next;
   19484         5768 :       mpz_set (values.left, values.vnode->repeat);
   19485              :     }
   19486              : 
   19487              :   return true;
   19488              : }
   19489              : 
   19490              : 
   19491              : static bool
   19492         3557 : check_data_variable (gfc_data_variable *var, locus *where)
   19493              : {
   19494         3557 :   gfc_expr *e;
   19495         3557 :   mpz_t size;
   19496         3557 :   mpz_t offset;
   19497         3557 :   bool t;
   19498         3557 :   ar_type mark = AR_UNKNOWN;
   19499         3557 :   int i;
   19500         3557 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19501         3557 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19502         3557 :   gfc_ref *ref;
   19503         3557 :   gfc_array_ref *ar;
   19504         3557 :   gfc_symbol *sym;
   19505         3557 :   int has_pointer;
   19506              : 
   19507         3557 :   if (!gfc_resolve_expr (var->expr))
   19508              :     return false;
   19509              : 
   19510         3557 :   ar = NULL;
   19511         3557 :   e = var->expr;
   19512              : 
   19513         3557 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19514            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19515            0 :     e = e->value.function.actual->expr;
   19516              : 
   19517         3557 :   if (e->expr_type != EXPR_VARIABLE)
   19518              :     {
   19519            0 :       gfc_error ("Expecting definable entity near %L", where);
   19520            0 :       return false;
   19521              :     }
   19522              : 
   19523         3557 :   sym = e->symtree->n.sym;
   19524              : 
   19525         3557 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19526              :     {
   19527            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19528              :                  sym->name, &sym->declared_at);
   19529            2 :       return false;
   19530              :     }
   19531              : 
   19532         3555 :   if (e->ref == NULL && sym->as)
   19533              :     {
   19534            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19535              :                  " declaration", sym->name, where);
   19536            1 :       return false;
   19537              :     }
   19538              : 
   19539         3554 :   if (gfc_is_coindexed (e))
   19540              :     {
   19541            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19542              :                  where);
   19543            7 :       return false;
   19544              :     }
   19545              : 
   19546         3547 :   has_pointer = sym->attr.pointer;
   19547              : 
   19548         5988 :   for (ref = e->ref; ref; ref = ref->next)
   19549              :     {
   19550         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19551              :         has_pointer = 1;
   19552              : 
   19553         2419 :       if (has_pointer)
   19554              :         {
   19555           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19556              :             {
   19557            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19558              :                          "be a full array", sym->name, where);
   19559            1 :               return false;
   19560              :             }
   19561              : 
   19562           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19563              :             {
   19564            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19565              :                          "and the corresponding DATA value is not a valid "
   19566              :                          "initial-data-target", where);
   19567            1 :               return false;
   19568              :             }
   19569              :         }
   19570              : 
   19571         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19572              :         {
   19573            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19574              :                      "attribute", ref->u.c.component->name, &e->where);
   19575            1 :           return false;
   19576              :         }
   19577              : 
   19578              :       /* Reject substrings of strings of non-constant length.  */
   19579         2442 :       if (ref->type == REF_SUBSTRING
   19580           73 :           && ref->u.ss.length
   19581           73 :           && ref->u.ss.length->length
   19582         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19583            1 :         goto bad_charlen;
   19584              :     }
   19585              : 
   19586              :   /* Reject strings with deferred length or non-constant length.  */
   19587         3543 :   if (e->ts.type == BT_CHARACTER
   19588         3543 :       && (e->ts.deferred
   19589          374 :           || (e->ts.u.cl->length
   19590          323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19591            5 :     goto bad_charlen;
   19592              : 
   19593         3538 :   mpz_init_set_si (offset, 0);
   19594              : 
   19595         3538 :   if (e->rank == 0 || has_pointer)
   19596              :     {
   19597         2691 :       mpz_init_set_ui (size, 1);
   19598         2691 :       ref = NULL;
   19599              :     }
   19600              :   else
   19601              :     {
   19602          847 :       ref = e->ref;
   19603              : 
   19604              :       /* Find the array section reference.  */
   19605         1030 :       for (ref = e->ref; ref; ref = ref->next)
   19606              :         {
   19607         1030 :           if (ref->type != REF_ARRAY)
   19608           92 :             continue;
   19609          938 :           if (ref->u.ar.type == AR_ELEMENT)
   19610           91 :             continue;
   19611              :           break;
   19612              :         }
   19613          847 :       gcc_assert (ref);
   19614              : 
   19615              :       /* Set marks according to the reference pattern.  */
   19616          847 :       switch (ref->u.ar.type)
   19617              :         {
   19618              :         case AR_FULL:
   19619              :           mark = AR_FULL;
   19620              :           break;
   19621              : 
   19622          151 :         case AR_SECTION:
   19623          151 :           ar = &ref->u.ar;
   19624              :           /* Get the start position of array section.  */
   19625          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   19626          151 :           mark = AR_SECTION;
   19627          151 :           break;
   19628              : 
   19629            0 :         default:
   19630            0 :           gcc_unreachable ();
   19631              :         }
   19632              : 
   19633          847 :       if (!gfc_array_size (e, &size))
   19634              :         {
   19635            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   19636              :                      where);
   19637            1 :           mpz_clear (offset);
   19638            1 :           return false;
   19639              :         }
   19640              :     }
   19641              : 
   19642         3537 :   t = true;
   19643              : 
   19644        11937 :   while (mpz_cmp_ui (size, 0) > 0)
   19645              :     {
   19646         8463 :       if (!next_data_value ())
   19647              :         {
   19648            1 :           gfc_error ("DATA statement at %L has more variables than values",
   19649              :                      where);
   19650            1 :           t = false;
   19651            1 :           break;
   19652              :         }
   19653              : 
   19654         8462 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   19655         8462 :       if (!t)
   19656              :         break;
   19657              : 
   19658              :       /* If we have more than one element left in the repeat count,
   19659              :          and we have more than one element left in the target variable,
   19660              :          then create a range assignment.  */
   19661              :       /* FIXME: Only done for full arrays for now, since array sections
   19662              :          seem tricky.  */
   19663         8443 :       if (mark == AR_FULL && ref && ref->next == NULL
   19664         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   19665              :         {
   19666          137 :           mpz_t range;
   19667              : 
   19668          137 :           if (mpz_cmp (size, values.left) >= 0)
   19669              :             {
   19670          126 :               mpz_init_set (range, values.left);
   19671          126 :               mpz_sub (size, size, values.left);
   19672          126 :               mpz_set_ui (values.left, 0);
   19673              :             }
   19674              :           else
   19675              :             {
   19676           11 :               mpz_init_set (range, size);
   19677           11 :               mpz_sub (values.left, values.left, size);
   19678           11 :               mpz_set_ui (size, 0);
   19679              :             }
   19680              : 
   19681          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19682              :                                      offset, &range);
   19683              : 
   19684          137 :           mpz_add (offset, offset, range);
   19685          137 :           mpz_clear (range);
   19686              : 
   19687          137 :           if (!t)
   19688              :             break;
   19689          129 :         }
   19690              : 
   19691              :       /* Assign initial value to symbol.  */
   19692              :       else
   19693              :         {
   19694         8306 :           mpz_sub_ui (values.left, values.left, 1);
   19695         8306 :           mpz_sub_ui (size, size, 1);
   19696              : 
   19697         8306 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19698              :                                      offset, NULL);
   19699         8306 :           if (!t)
   19700              :             break;
   19701              : 
   19702         8271 :           if (mark == AR_FULL)
   19703         5259 :             mpz_add_ui (offset, offset, 1);
   19704              : 
   19705              :           /* Modify the array section indexes and recalculate the offset
   19706              :              for next element.  */
   19707         3012 :           else if (mark == AR_SECTION)
   19708          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   19709              :         }
   19710              :     }
   19711              : 
   19712         3537 :   if (mark == AR_SECTION)
   19713              :     {
   19714          344 :       for (i = 0; i < ar->dimen; i++)
   19715          194 :         mpz_clear (section_index[i]);
   19716              :     }
   19717              : 
   19718         3537 :   mpz_clear (size);
   19719         3537 :   mpz_clear (offset);
   19720              : 
   19721         3537 :   return t;
   19722              : 
   19723            6 : bad_charlen:
   19724            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   19725              :              &e->where);
   19726            6 :   return false;
   19727              : }
   19728              : 
   19729              : 
   19730              : static bool traverse_data_var (gfc_data_variable *, locus *);
   19731              : 
   19732              : /* Iterate over a list of elements in a DATA statement.  */
   19733              : 
   19734              : static bool
   19735          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   19736              : {
   19737          237 :   mpz_t trip;
   19738          237 :   iterator_stack frame;
   19739          237 :   gfc_expr *e, *start, *end, *step;
   19740          237 :   bool retval = true;
   19741              : 
   19742          237 :   mpz_init (frame.value);
   19743          237 :   mpz_init (trip);
   19744              : 
   19745          237 :   start = gfc_copy_expr (var->iter.start);
   19746          237 :   end = gfc_copy_expr (var->iter.end);
   19747          237 :   step = gfc_copy_expr (var->iter.step);
   19748              : 
   19749          237 :   if (!gfc_simplify_expr (start, 1)
   19750          237 :       || start->expr_type != EXPR_CONSTANT)
   19751              :     {
   19752            0 :       gfc_error ("start of implied-do loop at %L could not be "
   19753              :                  "simplified to a constant value", &start->where);
   19754            0 :       retval = false;
   19755            0 :       goto cleanup;
   19756              :     }
   19757          237 :   if (!gfc_simplify_expr (end, 1)
   19758          237 :       || end->expr_type != EXPR_CONSTANT)
   19759              :     {
   19760            0 :       gfc_error ("end of implied-do loop at %L could not be "
   19761              :                  "simplified to a constant value", &end->where);
   19762            0 :       retval = false;
   19763            0 :       goto cleanup;
   19764              :     }
   19765          237 :   if (!gfc_simplify_expr (step, 1)
   19766          237 :       || step->expr_type != EXPR_CONSTANT)
   19767              :     {
   19768            0 :       gfc_error ("step of implied-do loop at %L could not be "
   19769              :                  "simplified to a constant value", &step->where);
   19770            0 :       retval = false;
   19771            0 :       goto cleanup;
   19772              :     }
   19773          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   19774              :     {
   19775            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   19776              :                  &step->where);
   19777            1 :       retval = false;
   19778            1 :       goto cleanup;
   19779              :     }
   19780              : 
   19781          236 :   mpz_set (trip, end->value.integer);
   19782          236 :   mpz_sub (trip, trip, start->value.integer);
   19783          236 :   mpz_add (trip, trip, step->value.integer);
   19784              : 
   19785          236 :   mpz_div (trip, trip, step->value.integer);
   19786              : 
   19787          236 :   mpz_set (frame.value, start->value.integer);
   19788              : 
   19789          236 :   frame.prev = iter_stack;
   19790          236 :   frame.variable = var->iter.var->symtree;
   19791          236 :   iter_stack = &frame;
   19792              : 
   19793         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   19794              :     {
   19795          905 :       if (!traverse_data_var (var->list, where))
   19796              :         {
   19797           14 :           retval = false;
   19798           14 :           goto cleanup;
   19799              :         }
   19800              : 
   19801          891 :       e = gfc_copy_expr (var->expr);
   19802          891 :       if (!gfc_simplify_expr (e, 1))
   19803              :         {
   19804            0 :           gfc_free_expr (e);
   19805            0 :           retval = false;
   19806            0 :           goto cleanup;
   19807              :         }
   19808              : 
   19809          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   19810              : 
   19811          891 :       mpz_sub_ui (trip, trip, 1);
   19812              :     }
   19813              : 
   19814          222 : cleanup:
   19815          237 :   mpz_clear (frame.value);
   19816          237 :   mpz_clear (trip);
   19817              : 
   19818          237 :   gfc_free_expr (start);
   19819          237 :   gfc_free_expr (end);
   19820          237 :   gfc_free_expr (step);
   19821              : 
   19822          237 :   iter_stack = frame.prev;
   19823          237 :   return retval;
   19824              : }
   19825              : 
   19826              : 
   19827              : /* Type resolve variables in the variable list of a DATA statement.  */
   19828              : 
   19829              : static bool
   19830         3418 : traverse_data_var (gfc_data_variable *var, locus *where)
   19831              : {
   19832         3418 :   bool t;
   19833              : 
   19834         7114 :   for (; var; var = var->next)
   19835              :     {
   19836         3794 :       if (var->expr == NULL)
   19837          237 :         t = traverse_data_list (var, where);
   19838              :       else
   19839         3557 :         t = check_data_variable (var, where);
   19840              : 
   19841         3794 :       if (!t)
   19842              :         return false;
   19843              :     }
   19844              : 
   19845              :   return true;
   19846              : }
   19847              : 
   19848              : 
   19849              : /* Resolve the expressions and iterators associated with a data statement.
   19850              :    This is separate from the assignment checking because data lists should
   19851              :    only be resolved once.  */
   19852              : 
   19853              : static bool
   19854         2668 : resolve_data_variables (gfc_data_variable *d)
   19855              : {
   19856         5707 :   for (; d; d = d->next)
   19857              :     {
   19858         3044 :       if (d->list == NULL)
   19859              :         {
   19860         2891 :           if (!gfc_resolve_expr (d->expr))
   19861              :             return false;
   19862              :         }
   19863              :       else
   19864              :         {
   19865          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   19866              :             return false;
   19867              : 
   19868          150 :           if (!resolve_data_variables (d->list))
   19869              :             return false;
   19870              :         }
   19871              :     }
   19872              : 
   19873              :   return true;
   19874              : }
   19875              : 
   19876              : 
   19877              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   19878              :    the value list into static variables, and then recursively traversing the
   19879              :    variables list, expanding iterators and such.  */
   19880              : 
   19881              : static void
   19882         2518 : resolve_data (gfc_data *d)
   19883              : {
   19884              : 
   19885         2518 :   if (!resolve_data_variables (d->var))
   19886              :     return;
   19887              : 
   19888         2513 :   values.vnode = d->value;
   19889         2513 :   if (d->value == NULL)
   19890            0 :     mpz_set_ui (values.left, 0);
   19891              :   else
   19892         2513 :     mpz_set (values.left, d->value->repeat);
   19893              : 
   19894         2513 :   if (!traverse_data_var (d->var, &d->where))
   19895              :     return;
   19896              : 
   19897              :   /* At this point, we better not have any values left.  */
   19898              : 
   19899         2429 :   if (next_data_value ())
   19900            0 :     gfc_error ("DATA statement at %L has more values than variables",
   19901              :                &d->where);
   19902              : }
   19903              : 
   19904              : 
   19905              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   19906              :    accessed by host or use association, is a dummy argument to a pure function,
   19907              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   19908              :    is storage associated with any such variable, shall not be used in the
   19909              :    following contexts: (clients of this function).  */
   19910              : 
   19911              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   19912              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   19913              :    problem.  */
   19914              : bool
   19915        56926 : gfc_impure_variable (gfc_symbol *sym)
   19916              : {
   19917        56926 :   gfc_symbol *proc;
   19918        56926 :   gfc_namespace *ns;
   19919              : 
   19920        56926 :   if (sym->attr.use_assoc || sym->attr.in_common)
   19921              :     return 1;
   19922              : 
   19923              :   /* The namespace of a module procedure interface holds the arguments and
   19924              :      symbols, and so the symbol namespace can be different to that of the
   19925              :      procedure.  */
   19926        56296 :   if (sym->ns != gfc_current_ns
   19927         6030 :       && gfc_current_ns->proc_name->abr_modproc_decl
   19928           48 :       && sym->ns->proc_name->attr.function
   19929           12 :       && sym->attr.result
   19930           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   19931              :     return 0;
   19932              : 
   19933              :   /* Check if the symbol's ns is inside the pure procedure.  */
   19934        61041 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   19935              :     {
   19936        60757 :       if (ns == sym->ns)
   19937              :         break;
   19938         6349 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   19939         5227 :           && !(sym->attr.function || sym->attr.result))
   19940              :         return 1;
   19941              :     }
   19942              : 
   19943        54692 :   proc = sym->ns->proc_name;
   19944        54692 :   if (sym->attr.dummy
   19945         6069 :       && !sym->attr.value
   19946         5947 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   19947         5741 :           || proc->attr.function))
   19948          700 :     return 1;
   19949              : 
   19950              :   /* TODO: Sort out what can be storage associated, if anything, and include
   19951              :      it here.  In principle equivalences should be scanned but it does not
   19952              :      seem to be possible to storage associate an impure variable this way.  */
   19953              :   return 0;
   19954              : }
   19955              : 
   19956              : 
   19957              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   19958              :    current namespace is inside a pure procedure.  */
   19959              : 
   19960              : bool
   19961      2383092 : gfc_pure (gfc_symbol *sym)
   19962              : {
   19963      2383092 :   symbol_attribute attr;
   19964      2383092 :   gfc_namespace *ns;
   19965              : 
   19966      2383092 :   if (sym == NULL)
   19967              :     {
   19968              :       /* Check if the current namespace or one of its parents
   19969              :         belongs to a pure procedure.  */
   19970      3214433 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19971              :         {
   19972      1899565 :           sym = ns->proc_name;
   19973      1899565 :           if (sym == NULL)
   19974              :             return 0;
   19975      1898424 :           attr = sym->attr;
   19976      1898424 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   19977              :             return 1;
   19978              :         }
   19979              :       return 0;
   19980              :     }
   19981              : 
   19982      1059575 :   attr = sym->attr;
   19983              : 
   19984      1059575 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   19985              : }
   19986              : 
   19987              : 
   19988              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   19989              :    checks if the current namespace is implicitly pure.  Note that this
   19990              :    function returns false for a PURE procedure.  */
   19991              : 
   19992              : bool
   19993       731547 : gfc_implicit_pure (gfc_symbol *sym)
   19994              : {
   19995       731547 :   gfc_namespace *ns;
   19996              : 
   19997       731547 :   if (sym == NULL)
   19998              :     {
   19999              :       /* Check if the current procedure is implicit_pure.  Walk up
   20000              :          the procedure list until we find a procedure.  */
   20001      1007897 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   20002              :         {
   20003       719538 :           sym = ns->proc_name;
   20004       719538 :           if (sym == NULL)
   20005              :             return 0;
   20006              : 
   20007       719465 :           if (sym->attr.flavor == FL_PROCEDURE)
   20008              :             break;
   20009              :         }
   20010              :     }
   20011              : 
   20012       443112 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   20013       759217 :     && !sym->attr.pure;
   20014              : }
   20015              : 
   20016              : 
   20017              : void
   20018       429479 : gfc_unset_implicit_pure (gfc_symbol *sym)
   20019              : {
   20020       429479 :   gfc_namespace *ns;
   20021              : 
   20022       429479 :   if (sym == NULL)
   20023              :     {
   20024              :       /* Check if the current procedure is implicit_pure.  Walk up
   20025              :          the procedure list until we find a procedure.  */
   20026       701978 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   20027              :         {
   20028       434249 :           sym = ns->proc_name;
   20029       434249 :           if (sym == NULL)
   20030              :             return;
   20031              : 
   20032       433416 :           if (sym->attr.flavor == FL_PROCEDURE)
   20033              :             break;
   20034              :         }
   20035              :     }
   20036              : 
   20037       428646 :   if (sym->attr.flavor == FL_PROCEDURE)
   20038       152482 :     sym->attr.implicit_pure = 0;
   20039              :   else
   20040       276164 :     sym->attr.pure = 0;
   20041              : }
   20042              : 
   20043              : 
   20044              : /* Test whether the current procedure is elemental or not.  */
   20045              : 
   20046              : bool
   20047      1430311 : gfc_elemental (gfc_symbol *sym)
   20048              : {
   20049      1430311 :   symbol_attribute attr;
   20050              : 
   20051      1430311 :   if (sym == NULL)
   20052            0 :     sym = gfc_current_ns->proc_name;
   20053            0 :   if (sym == NULL)
   20054              :     return 0;
   20055      1430311 :   attr = sym->attr;
   20056              : 
   20057      1430311 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   20058              : }
   20059              : 
   20060              : 
   20061              : /* Warn about unused labels.  */
   20062              : 
   20063              : static void
   20064         4843 : warn_unused_fortran_label (gfc_st_label *label)
   20065              : {
   20066         4869 :   if (label == NULL)
   20067              :     return;
   20068              : 
   20069           27 :   warn_unused_fortran_label (label->left);
   20070              : 
   20071           27 :   if (label->defined == ST_LABEL_UNKNOWN)
   20072              :     return;
   20073              : 
   20074           26 :   switch (label->referenced)
   20075              :     {
   20076            2 :     case ST_LABEL_UNKNOWN:
   20077            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   20078              :                    label->value, &label->where);
   20079            2 :       break;
   20080              : 
   20081            1 :     case ST_LABEL_BAD_TARGET:
   20082            1 :       gfc_warning (OPT_Wunused_label,
   20083              :                    "Label %d at %L defined but cannot be used",
   20084              :                    label->value, &label->where);
   20085            1 :       break;
   20086              : 
   20087              :     default:
   20088              :       break;
   20089              :     }
   20090              : 
   20091           26 :   warn_unused_fortran_label (label->right);
   20092              : }
   20093              : 
   20094              : 
   20095              : /* Returns the sequence type of a symbol or sequence.  */
   20096              : 
   20097              : static seq_type
   20098         1076 : sequence_type (gfc_typespec ts)
   20099              : {
   20100         1076 :   seq_type result;
   20101         1076 :   gfc_component *c;
   20102              : 
   20103         1076 :   switch (ts.type)
   20104              :   {
   20105           49 :     case BT_DERIVED:
   20106              : 
   20107           49 :       if (ts.u.derived->components == NULL)
   20108              :         return SEQ_NONDEFAULT;
   20109              : 
   20110           49 :       result = sequence_type (ts.u.derived->components->ts);
   20111          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   20112           67 :         if (sequence_type (c->ts) != result)
   20113              :           return SEQ_MIXED;
   20114              : 
   20115              :       return result;
   20116              : 
   20117          129 :     case BT_CHARACTER:
   20118          129 :       if (ts.kind != gfc_default_character_kind)
   20119            0 :           return SEQ_NONDEFAULT;
   20120              : 
   20121              :       return SEQ_CHARACTER;
   20122              : 
   20123          240 :     case BT_INTEGER:
   20124          240 :       if (ts.kind != gfc_default_integer_kind)
   20125           25 :           return SEQ_NONDEFAULT;
   20126              : 
   20127              :       return SEQ_NUMERIC;
   20128              : 
   20129          559 :     case BT_REAL:
   20130          559 :       if (!(ts.kind == gfc_default_real_kind
   20131          269 :             || ts.kind == gfc_default_double_kind))
   20132            0 :           return SEQ_NONDEFAULT;
   20133              : 
   20134              :       return SEQ_NUMERIC;
   20135              : 
   20136           81 :     case BT_COMPLEX:
   20137           81 :       if (ts.kind != gfc_default_complex_kind)
   20138           48 :           return SEQ_NONDEFAULT;
   20139              : 
   20140              :       return SEQ_NUMERIC;
   20141              : 
   20142           17 :     case BT_LOGICAL:
   20143           17 :       if (ts.kind != gfc_default_logical_kind)
   20144            0 :           return SEQ_NONDEFAULT;
   20145              : 
   20146              :       return SEQ_NUMERIC;
   20147              : 
   20148              :     default:
   20149              :       return SEQ_NONDEFAULT;
   20150              :   }
   20151              : }
   20152              : 
   20153              : 
   20154              : /* Resolve derived type EQUIVALENCE object.  */
   20155              : 
   20156              : static bool
   20157           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   20158              : {
   20159           80 :   gfc_component *c = derived->components;
   20160              : 
   20161           80 :   if (!derived)
   20162              :     return true;
   20163              : 
   20164              :   /* Shall not be an object of nonsequence derived type.  */
   20165           80 :   if (!derived->attr.sequence)
   20166              :     {
   20167            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   20168              :                  "attribute to be an EQUIVALENCE object", sym->name,
   20169              :                  &e->where);
   20170            0 :       return false;
   20171              :     }
   20172              : 
   20173              :   /* Shall not have allocatable components.  */
   20174           80 :   if (derived->attr.alloc_comp)
   20175              :     {
   20176            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   20177              :                  "components to be an EQUIVALENCE object",sym->name,
   20178              :                  &e->where);
   20179            1 :       return false;
   20180              :     }
   20181              : 
   20182           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   20183              :     {
   20184            1 :       gfc_error ("Derived type variable %qs at %L with default "
   20185              :                  "initialization cannot be in EQUIVALENCE with a variable "
   20186              :                  "in COMMON", sym->name, &e->where);
   20187            1 :       return false;
   20188              :     }
   20189              : 
   20190          245 :   for (; c ; c = c->next)
   20191              :     {
   20192          167 :       if (gfc_bt_struct (c->ts.type)
   20193          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   20194              :         return false;
   20195              : 
   20196              :       /* Shall not be an object of sequence derived type containing a pointer
   20197              :          in the structure.  */
   20198          167 :       if (c->attr.pointer)
   20199              :         {
   20200            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   20201              :                      "component(s) cannot be an EQUIVALENCE object",
   20202              :                      sym->name, &e->where);
   20203            0 :           return false;
   20204              :         }
   20205              :     }
   20206              :   return true;
   20207              : }
   20208              : 
   20209              : 
   20210              : /* Resolve equivalence object.
   20211              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   20212              :    an allocatable array, an object of nonsequence derived type, an object of
   20213              :    sequence derived type containing a pointer at any level of component
   20214              :    selection, an automatic object, a function name, an entry name, a result
   20215              :    name, a named constant, a structure component, or a subobject of any of
   20216              :    the preceding objects.  A substring shall not have length zero.  A
   20217              :    derived type shall not have components with default initialization nor
   20218              :    shall two objects of an equivalence group be initialized.
   20219              :    Either all or none of the objects shall have an protected attribute.
   20220              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   20221              :    are implemented here.  */
   20222              : 
   20223              : static void
   20224         1565 : resolve_equivalence (gfc_equiv *eq)
   20225              : {
   20226         1565 :   gfc_symbol *sym;
   20227         1565 :   gfc_symbol *first_sym;
   20228         1565 :   gfc_expr *e;
   20229         1565 :   gfc_ref *r;
   20230         1565 :   locus *last_where = NULL;
   20231         1565 :   seq_type eq_type, last_eq_type;
   20232         1565 :   gfc_typespec *last_ts;
   20233         1565 :   int object, cnt_protected;
   20234         1565 :   const char *msg;
   20235              : 
   20236         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   20237              : 
   20238         1565 :   first_sym = eq->expr->symtree->n.sym;
   20239              : 
   20240         1565 :   cnt_protected = 0;
   20241              : 
   20242         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   20243              :     {
   20244         3171 :       e = eq->expr;
   20245              : 
   20246         3171 :       e->ts = e->symtree->n.sym->ts;
   20247              :       /* match_varspec might not know yet if it is seeing
   20248              :          array reference or substring reference, as it doesn't
   20249              :          know the types.  */
   20250         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   20251              :         {
   20252         2152 :           gfc_ref *ref = e->ref;
   20253         2152 :           sym = e->symtree->n.sym;
   20254              : 
   20255         2152 :           if (sym->attr.dimension)
   20256              :             {
   20257         1855 :               ref->u.ar.as = sym->as;
   20258         1855 :               ref = ref->next;
   20259              :             }
   20260              : 
   20261              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   20262         2152 :           if (e->ts.type == BT_CHARACTER
   20263          592 :               && ref
   20264          371 :               && ref->type == REF_ARRAY
   20265          371 :               && ref->u.ar.dimen == 1
   20266          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   20267          371 :               && ref->u.ar.stride[0] == NULL)
   20268              :             {
   20269          370 :               gfc_expr *start = ref->u.ar.start[0];
   20270          370 :               gfc_expr *end = ref->u.ar.end[0];
   20271          370 :               void *mem = NULL;
   20272              : 
   20273              :               /* Optimize away the (:) reference.  */
   20274          370 :               if (start == NULL && end == NULL)
   20275              :                 {
   20276            9 :                   if (e->ref == ref)
   20277            0 :                     e->ref = ref->next;
   20278              :                   else
   20279            9 :                     e->ref->next = ref->next;
   20280              :                   mem = ref;
   20281              :                 }
   20282              :               else
   20283              :                 {
   20284          361 :                   ref->type = REF_SUBSTRING;
   20285          361 :                   if (start == NULL)
   20286            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   20287              :                                               NULL, 1);
   20288          361 :                   ref->u.ss.start = start;
   20289          361 :                   if (end == NULL && e->ts.u.cl)
   20290           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   20291          361 :                   ref->u.ss.end = end;
   20292          361 :                   ref->u.ss.length = e->ts.u.cl;
   20293          361 :                   e->ts.u.cl = NULL;
   20294              :                 }
   20295          370 :               ref = ref->next;
   20296          370 :               free (mem);
   20297              :             }
   20298              : 
   20299              :           /* Any further ref is an error.  */
   20300         1930 :           if (ref)
   20301              :             {
   20302            1 :               gcc_assert (ref->type == REF_ARRAY);
   20303            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   20304              :                          &ref->u.ar.where);
   20305            1 :               continue;
   20306              :             }
   20307              :         }
   20308              : 
   20309         3170 :       if (!gfc_resolve_expr (e))
   20310            2 :         continue;
   20311              : 
   20312         3168 :       sym = e->symtree->n.sym;
   20313              : 
   20314         3168 :       if (sym->attr.is_protected)
   20315            2 :         cnt_protected++;
   20316         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   20317              :         {
   20318            2 :               gfc_error ("Either all or none of the objects in the "
   20319              :                          "EQUIVALENCE set at %L shall have the "
   20320              :                          "PROTECTED attribute",
   20321              :                          &e->where);
   20322            2 :               break;
   20323              :         }
   20324              : 
   20325              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   20326         3166 :       if (sym->ns->proc_name
   20327         3150 :           && sym->ns->proc_name->attr.pure
   20328            7 :           && sym->attr.in_common)
   20329              :         {
   20330              :           /* Need to check for symbols that may have entered the pure
   20331              :              procedure via a USE statement.  */
   20332            7 :           bool saw_sym = false;
   20333            7 :           if (sym->ns->use_stmts)
   20334              :             {
   20335            6 :               gfc_use_rename *r;
   20336           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   20337            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   20338              :             }
   20339              :           else
   20340              :             saw_sym = true;
   20341              : 
   20342            6 :           if (saw_sym)
   20343            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   20344              :                        "EQUIVALENCE object in the pure procedure %qs",
   20345              :                        sym->name, &e->where, sym->ns->proc_name->name);
   20346              :           break;
   20347              :         }
   20348              : 
   20349              :       /* Shall not be a named constant.  */
   20350         3159 :       if (e->expr_type == EXPR_CONSTANT)
   20351              :         {
   20352            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   20353              :                      "object", sym->name, &e->where);
   20354            0 :           continue;
   20355              :         }
   20356              : 
   20357         3161 :       if (e->ts.type == BT_DERIVED
   20358         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   20359            2 :         continue;
   20360              : 
   20361              :       /* Check that the types correspond correctly:
   20362              :          Note 5.28:
   20363              :          A numeric sequence structure may be equivalenced to another sequence
   20364              :          structure, an object of default integer type, default real type, double
   20365              :          precision real type, default logical type such that components of the
   20366              :          structure ultimately only become associated to objects of the same
   20367              :          kind. A character sequence structure may be equivalenced to an object
   20368              :          of default character kind or another character sequence structure.
   20369              :          Other objects may be equivalenced only to objects of the same type and
   20370              :          kind parameters.  */
   20371              : 
   20372              :       /* Identical types are unconditionally OK.  */
   20373         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   20374         2677 :         goto identical_types;
   20375              : 
   20376          480 :       last_eq_type = sequence_type (*last_ts);
   20377          480 :       eq_type = sequence_type (sym->ts);
   20378              : 
   20379              :       /* Since the pair of objects is not of the same type, mixed or
   20380              :          non-default sequences can be rejected.  */
   20381              : 
   20382          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20383              :                "statement at %L with different type objects");
   20384          481 :       if ((object ==2
   20385          480 :            && last_eq_type == SEQ_MIXED
   20386            7 :            && last_where
   20387            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20388          486 :           || (eq_type == SEQ_MIXED
   20389            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20390            1 :         continue;
   20391              : 
   20392          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20393              :                "statement at %L with objects of different type");
   20394          483 :       if ((object ==2
   20395          479 :            && last_eq_type == SEQ_NONDEFAULT
   20396           50 :            && last_where
   20397           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20398          525 :           || (eq_type == SEQ_NONDEFAULT
   20399           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20400            4 :         continue;
   20401              : 
   20402          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20403              :                "EQUIVALENCE statement at %L");
   20404          479 :       if (last_eq_type == SEQ_CHARACTER
   20405          475 :           && eq_type != SEQ_CHARACTER
   20406          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20407            4 :                 continue;
   20408              : 
   20409          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20410              :                "EQUIVALENCE statement at %L");
   20411          473 :       if (last_eq_type == SEQ_NUMERIC
   20412          471 :           && eq_type != SEQ_NUMERIC
   20413          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20414            2 :                 continue;
   20415              : 
   20416         3146 : identical_types:
   20417              : 
   20418         3146 :       last_ts =&sym->ts;
   20419         3146 :       last_where = &e->where;
   20420              : 
   20421         3146 :       if (!e->ref)
   20422         1003 :         continue;
   20423              : 
   20424              :       /* Shall not be an automatic array.  */
   20425         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20426              :         {
   20427            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20428              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20429            3 :           continue;
   20430              :         }
   20431              : 
   20432         2140 :       r = e->ref;
   20433         4326 :       while (r)
   20434              :         {
   20435              :           /* Shall not be a structure component.  */
   20436         2187 :           if (r->type == REF_COMPONENT)
   20437              :             {
   20438            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20439              :                          "EQUIVALENCE object",
   20440            0 :                          r->u.c.component->name, &e->where);
   20441            0 :               break;
   20442              :             }
   20443              : 
   20444              :           /* A substring shall not have length zero.  */
   20445         2187 :           if (r->type == REF_SUBSTRING)
   20446              :             {
   20447          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20448              :                 {
   20449            1 :                   gfc_error ("Substring at %L has length zero",
   20450              :                              &r->u.ss.start->where);
   20451            1 :                   break;
   20452              :                 }
   20453              :             }
   20454         2186 :           r = r->next;
   20455              :         }
   20456              :     }
   20457         1565 : }
   20458              : 
   20459              : 
   20460              : /* Function called by resolve_fntype to flag other symbols used in the
   20461              :    length type parameter specification of function results.  */
   20462              : 
   20463              : static bool
   20464         4237 : flag_fn_result_spec (gfc_expr *expr,
   20465              :                      gfc_symbol *sym,
   20466              :                      int *f ATTRIBUTE_UNUSED)
   20467              : {
   20468         4237 :   gfc_namespace *ns;
   20469         4237 :   gfc_symbol *s;
   20470              : 
   20471         4237 :   if (expr->expr_type == EXPR_VARIABLE)
   20472              :     {
   20473         1384 :       s = expr->symtree->n.sym;
   20474         2171 :       for (ns = s->ns; ns; ns = ns->parent)
   20475         2171 :         if (!ns->parent)
   20476              :           break;
   20477              : 
   20478         1384 :       if (sym == s)
   20479              :         {
   20480            1 :           gfc_error ("Self reference in character length expression "
   20481              :                      "for %qs at %L", sym->name, &expr->where);
   20482            1 :           return true;
   20483              :         }
   20484              : 
   20485         1383 :       if (!s->fn_result_spec
   20486         1383 :           && s->attr.flavor == FL_PARAMETER)
   20487              :         {
   20488              :           /* Function contained in a module.... */
   20489           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20490              :             {
   20491           32 :               gfc_symtree *st;
   20492           32 :               s->fn_result_spec = 1;
   20493              :               /* Make sure that this symbol is translated as a module
   20494              :                  variable.  */
   20495           32 :               st = gfc_get_unique_symtree (ns);
   20496           32 :               st->n.sym = s;
   20497           32 :               s->refs++;
   20498           32 :             }
   20499              :           /* ... which is use associated and called.  */
   20500           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20501            0 :                         ||
   20502              :                   /* External function matched with an interface.  */
   20503            0 :                   (s->ns->proc_name
   20504            0 :                    && ((s->ns == ns
   20505            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20506            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20507            0 :                    && s->ns->proc_name->attr.function))
   20508           31 :             s->fn_result_spec = 1;
   20509              :         }
   20510              :     }
   20511              :   return false;
   20512              : }
   20513              : 
   20514              : 
   20515              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20516              : 
   20517              : static void
   20518       361488 : resolve_fntype (gfc_namespace *ns)
   20519              : {
   20520       361488 :   gfc_entry_list *el;
   20521       361488 :   gfc_symbol *sym;
   20522              : 
   20523       361488 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20524              :     return;
   20525              : 
   20526              :   /* If there are any entries, ns->proc_name is the entry master
   20527              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20528       189391 :   if (ns->entries)
   20529          596 :     sym = ns->entries->sym;
   20530              :   else
   20531              :     sym = ns->proc_name;
   20532       189391 :   if (sym->result == sym
   20533       153853 :       && sym->ts.type == BT_UNKNOWN
   20534            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20535       189395 :       && !sym->attr.untyped)
   20536              :     {
   20537            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20538              :                  sym->name, &sym->declared_at);
   20539            3 :       sym->attr.untyped = 1;
   20540              :     }
   20541              : 
   20542        14015 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20543         1862 :       && !sym->attr.contained
   20544          299 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20545       189391 :       && gfc_check_symbol_access (sym))
   20546              :     {
   20547            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20548              :                       "%L of PRIVATE type %qs", sym->name,
   20549            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20550              :     }
   20551              : 
   20552       189391 :     if (ns->entries)
   20553         1253 :     for (el = ns->entries->next; el; el = el->next)
   20554              :       {
   20555          657 :         if (el->sym->result == el->sym
   20556          445 :             && el->sym->ts.type == BT_UNKNOWN
   20557            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20558          659 :             && !el->sym->attr.untyped)
   20559              :           {
   20560            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20561              :                        el->sym->name, &el->sym->declared_at);
   20562            2 :             el->sym->attr.untyped = 1;
   20563              :           }
   20564              :       }
   20565              : 
   20566       189391 :   if (sym->ts.type == BT_CHARACTER
   20567         7086 :       && sym->ts.u.cl->length
   20568         1883 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20569         1878 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20570              : }
   20571              : 
   20572              : 
   20573              : /* 12.3.2.1.1 Defined operators.  */
   20574              : 
   20575              : static bool
   20576          508 : check_uop_procedure (gfc_symbol *sym, locus where)
   20577              : {
   20578          508 :   gfc_formal_arglist *formal;
   20579              : 
   20580          508 :   if (!sym->attr.function)
   20581              :     {
   20582            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20583              :                  sym->name, &where);
   20584            4 :       return false;
   20585              :     }
   20586              : 
   20587          504 :   if (sym->ts.type == BT_CHARACTER
   20588           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20589            2 :       && !(sym->result && ((sym->result->ts.u.cl
   20590            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   20591              :     {
   20592            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   20593              :                  "character length", sym->name, &where);
   20594            2 :       return false;
   20595              :     }
   20596              : 
   20597          502 :   formal = gfc_sym_get_dummy_args (sym);
   20598          502 :   if (!formal || !formal->sym)
   20599              :     {
   20600            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   20601              :                  "one argument", sym->name, &where);
   20602            1 :       return false;
   20603              :     }
   20604              : 
   20605          501 :   if (formal->sym->attr.intent != INTENT_IN)
   20606              :     {
   20607            0 :       gfc_error ("First argument of operator interface at %L must be "
   20608              :                  "INTENT(IN)", &where);
   20609            0 :       return false;
   20610              :     }
   20611              : 
   20612          501 :   if (formal->sym->attr.optional)
   20613              :     {
   20614            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   20615              :                  "optional", &where);
   20616            0 :       return false;
   20617              :     }
   20618              : 
   20619          501 :   formal = formal->next;
   20620          501 :   if (!formal || !formal->sym)
   20621              :     return true;
   20622              : 
   20623          297 :   if (formal->sym->attr.intent != INTENT_IN)
   20624              :     {
   20625            0 :       gfc_error ("Second argument of operator interface at %L must be "
   20626              :                  "INTENT(IN)", &where);
   20627            0 :       return false;
   20628              :     }
   20629              : 
   20630          297 :   if (formal->sym->attr.optional)
   20631              :     {
   20632            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   20633              :                  "optional", &where);
   20634            1 :       return false;
   20635              :     }
   20636              : 
   20637          296 :   if (formal->next)
   20638              :     {
   20639            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   20640              :                  "arguments", &where);
   20641            2 :       return false;
   20642              :     }
   20643              : 
   20644              :   return true;
   20645              : }
   20646              : 
   20647              : static void
   20648       362294 : gfc_resolve_uops (gfc_symtree *symtree)
   20649              : {
   20650       362294 :   gfc_interface *itr;
   20651              : 
   20652       362294 :   if (symtree == NULL)
   20653              :     return;
   20654              : 
   20655          403 :   gfc_resolve_uops (symtree->left);
   20656          403 :   gfc_resolve_uops (symtree->right);
   20657              : 
   20658          798 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   20659          395 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   20660              : }
   20661              : 
   20662              : /* Mark all lhs in assignment statement as used.  It is better to put this into
   20663              :    its own function rather than into the different switch cases in
   20664              :    gfc_resolve_code.  */
   20665              : 
   20666              : static void
   20667       698424 : mark_lhs_assignments_set (gfc_code *code)
   20668              : {
   20669              : 
   20670      1847652 :   for (; code; code = code->next)
   20671              :     {
   20672      1149228 :       gfc_expr *lvalue = code->expr1, *rvalue = code->expr2;
   20673              : 
   20674      1149228 :       if (lvalue == NULL || lvalue->symtree == NULL || rvalue == NULL)
   20675       849287 :         continue;
   20676              : 
   20677       299941 :       switch (code->op)
   20678              :         {
   20679       288273 :         case EXEC_ASSIGN:
   20680       288273 :           if (gfc_is_reallocatable_lhs (lvalue) && lvalue->rank == rvalue->rank)
   20681         8473 :             gfc_lvalue_allocated_at (lvalue->symtree->n.sym, &lvalue->where);
   20682              : 
   20683       298470 :           gcc_fallthrough();
   20684       298470 :         case EXEC_POINTER_ASSIGN:
   20685       298470 :           gfc_expr_set_at (lvalue, &rvalue->where, VALUE_VARDEF);
   20686              :         default:
   20687              :           break;
   20688              :         }
   20689              :     }
   20690       698424 : }
   20691              : 
   20692              : /* Examine all of the expressions associated with a program unit,
   20693              :    assign types to all intermediate expressions, make sure that all
   20694              :    assignments are to compatible types and figure out which names
   20695              :    refer to which functions or subroutines.  It doesn't check code
   20696              :    block, which is handled by gfc_resolve_code.  */
   20697              : 
   20698              : static void
   20699       364044 : resolve_types (gfc_namespace *ns)
   20700              : {
   20701       364044 :   gfc_namespace *n;
   20702       364044 :   gfc_charlen *cl;
   20703       364044 :   gfc_data *d;
   20704       364044 :   gfc_equiv *eq;
   20705       364044 :   gfc_namespace* old_ns = gfc_current_ns;
   20706       364044 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   20707              : 
   20708       364044 :   if (ns->types_resolved)
   20709              :     return;
   20710              : 
   20711              :   /* Check that all IMPLICIT types are ok.  */
   20712       361489 :   if (!ns->seen_implicit_none)
   20713              :     {
   20714              :       unsigned letter;
   20715      9106804 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   20716      8769515 :         if (ns->set_flag[letter]
   20717      8769515 :             && !resolve_typespec_used (&ns->default_type[letter],
   20718              :                                        &ns->implicit_loc[letter], NULL))
   20719              :           return;
   20720              :     }
   20721              : 
   20722       361488 :   gfc_current_ns = ns;
   20723              : 
   20724       361488 :   resolve_entries (ns);
   20725              : 
   20726       361488 :   resolve_common_vars (&ns->blank_common, false);
   20727       361488 :   resolve_common_blocks (ns->common_root);
   20728              : 
   20729       361488 :   resolve_contained_functions (ns);
   20730              : 
   20731       361488 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   20732       310195 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20733       206379 :     gfc_resolve_formal_arglist (ns->proc_name);
   20734              : 
   20735       361488 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   20736              : 
   20737       456648 :   for (cl = ns->cl_list; cl; cl = cl->next)
   20738        95160 :     resolve_charlen (cl);
   20739              : 
   20740       361488 :   gfc_traverse_ns (ns, resolve_symbol);
   20741              : 
   20742       361488 :   resolve_fntype (ns);
   20743              : 
   20744       410809 :   for (n = ns->contained; n; n = n->sibling)
   20745              :     {
   20746              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   20747        49321 :       if (gfc_pure (ns->proc_name)
   20748            5 :           && !gfc_pure (n->proc_name)
   20749        49321 :           && !n->proc_name->attr.artificial)
   20750            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   20751              :                    "also be PURE", n->proc_name->name,
   20752              :                    &n->proc_name->declared_at);
   20753              : 
   20754        49321 :       resolve_types (n);
   20755              :     }
   20756              : 
   20757       361488 :   forall_flag = 0;
   20758       361488 :   gfc_do_concurrent_flag = 0;
   20759       361488 :   gfc_check_interfaces (ns);
   20760              : 
   20761       361488 :   gfc_traverse_ns (ns, resolve_values);
   20762              : 
   20763       361488 :   if (ns->save_all || (!flag_automatic && !recursive))
   20764          315 :     gfc_save_all (ns);
   20765              : 
   20766       361488 :   iter_stack = NULL;
   20767       364006 :   for (d = ns->data; d; d = d->next)
   20768         2518 :     resolve_data (d);
   20769              : 
   20770       361488 :   iter_stack = NULL;
   20771       361488 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   20772              : 
   20773       361488 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   20774              : 
   20775       363053 :   for (eq = ns->equiv; eq; eq = eq->next)
   20776         1565 :     resolve_equivalence (eq);
   20777              : 
   20778              :   /* Warn about unused labels.  */
   20779       361488 :   if (warn_unused_label)
   20780         4816 :     warn_unused_fortran_label (ns->st_labels);
   20781              : 
   20782       361488 :   gfc_resolve_uops (ns->uop_root);
   20783              : 
   20784       361488 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   20785              : 
   20786       361488 :   gfc_resolve_omp_declare (ns);
   20787              : 
   20788       361488 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   20789              : 
   20790       361488 :   gfc_resolve_omp_udms (ns->omp_udm_root);
   20791              : 
   20792       361488 :   ns->types_resolved = 1;
   20793              : 
   20794       361488 :   gfc_current_ns = old_ns;
   20795              : }
   20796              : 
   20797              : 
   20798              : /* Call gfc_resolve_code recursively.  */
   20799              : 
   20800              : static void
   20801       364106 : resolve_codes (gfc_namespace *ns)
   20802              : {
   20803       364106 :   gfc_namespace *n;
   20804       364106 :   bitmap_obstack old_obstack;
   20805              : 
   20806       364106 :   if (ns->resolved == 1)
   20807        14591 :     return;
   20808              : 
   20809       398898 :   for (n = ns->contained; n; n = n->sibling)
   20810        49383 :     resolve_codes (n);
   20811              : 
   20812       349515 :   gfc_current_ns = ns;
   20813              : 
   20814              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   20815       349515 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   20816       336912 :     cs_base = NULL;
   20817              : 
   20818              :   /* Set to an out of range value.  */
   20819       349515 :   current_entry_id = -1;
   20820              : 
   20821       349515 :   old_obstack = labels_obstack;
   20822       349515 :   bitmap_obstack_initialize (&labels_obstack);
   20823              : 
   20824       349515 :   gfc_resolve_oacc_declare (ns);
   20825       349515 :   gfc_resolve_oacc_routines (ns);
   20826       349515 :   gfc_resolve_omp_local_vars (ns);
   20827       349515 :   if (ns->omp_allocate)
   20828           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   20829       349515 :   gfc_resolve_code (ns->code, ns);
   20830              : 
   20831       349514 :   bitmap_obstack_release (&labels_obstack);
   20832       349514 :   labels_obstack = old_obstack;
   20833              : }
   20834              : 
   20835              : /* Return true if the value of a variable can be considered used, either
   20836              :    through the value_used flag or because it is a suitable dummy argument.  */
   20837              : 
   20838              : static bool
   20839          453 : var_value_is_used (gfc_symbol *sym)
   20840              : {
   20841          453 :   if (sym->attr.value_used != VALUE_UNUSED)
   20842              :     return true;
   20843              : 
   20844          107 :   if (!sym->attr.dummy)
   20845              :     return false;
   20846              : 
   20847           90 :   if (sym->attr.value)
   20848              :     return false;
   20849              : 
   20850           90 :   switch (sym->attr.intent)
   20851              :     {
   20852              :     case INTENT_UNKNOWN:
   20853              :     case INTENT_INOUT:
   20854              :     case INTENT_OUT:
   20855              :       return true;
   20856              : 
   20857              :     case INTENT_IN:
   20858              :     default:
   20859              :       return false;
   20860              :     }
   20861              : }
   20862              : 
   20863              : /* Similar, see if the variable could have gotten its value from somewhere.  */
   20864              : 
   20865              : static bool
   20866         2381 : var_value_is_set (gfc_symbol *sym)
   20867              : {
   20868         2381 :   if (sym->attr.value_set != VALUE_UNSET)
   20869              :     return true;
   20870              : 
   20871         1684 :   if (sym->value)
   20872              :     return true;
   20873              : 
   20874         1669 :   if (sym->ts.type == BT_DERIVED
   20875         1669 :       && gfc_has_default_initializer (sym->ts.u.derived))
   20876              :     return true;
   20877              : 
   20878         1669 :   if (!sym->attr.dummy)
   20879              :     return false;
   20880              : 
   20881         1624 :   if (sym->attr.value)
   20882              :     return true;
   20883              : 
   20884         1591 :   if (sym->attr.intent == INTENT_OUT)
   20885            3 :     return false;
   20886              : 
   20887              :   return true;
   20888              : }
   20889              : 
   20890              : /* Callback function to catch set but never used variables.  */
   20891              : 
   20892              : static void
   20893        34280 : find_unused_vs_set (gfc_symbol *sym)
   20894              : {
   20895        34280 :   symbol_attribute *attr = &sym->attr;
   20896              : 
   20897        34280 :   if (attr->flavor != FL_VARIABLE)
   20898              :     return;
   20899              : 
   20900              :   /* Do not warn about anything too far out of the ordinary.  This might be
   20901              :      tightened later.  */
   20902         8607 :   if (attr->in_common || attr->in_equivalence || attr->artificial
   20903         8201 :       || attr->cray_pointer || attr->cray_pointee || attr->associate_var
   20904         8198 :       || attr->target || attr->fe_temp || attr->omp_declare_target
   20905         8195 :       || attr->omp_declare_target_link || attr->omp_declare_target_local
   20906         8186 :       || attr->omp_declare_target_indirect || attr->oacc_declare_create
   20907         8186 :       || attr->oacc_declare_copyin || attr->oacc_declare_deviceptr
   20908         8186 :       || attr->oacc_declare_device_resident || attr->oacc_declare_link
   20909         8186 :       || attr->result || attr->warning_emitted || attr->use_assoc
   20910         5645 :       || attr->volatile_ || attr->asynchronous || !attr->referenced)
   20911              :     return;
   20912              : 
   20913         2449 :   if (attr->host_assoc && attr->access != ACCESS_PRIVATE)
   20914              :     return;
   20915              : 
   20916              :   /* There is no allocation in sight, but the variable is used anyway.  This
   20917              :      might be hidden behind PRESENT, but issue a warning nonetheless.  If
   20918              :      people complain, we might want to make this to an extra option to be
   20919              :      included with -Wextra.  */
   20920              : 
   20921         2383 :   if (warn_undefined_vars && attr->allocatable && !attr->allocated
   20922         2435 :       && var_value_is_used (sym))
   20923              :     {
   20924            3 :       if (attr->dummy && attr->intent == INTENT_OUT)
   20925              :         {
   20926            0 :           gfc_warning (OPT_Wundefined_vars, "Unallocated INTENT(OUT) variable "
   20927              :                        "%qs referenced at %L", sym->name, &sym->other_loc);
   20928            0 :           attr->warning_emitted = 1;
   20929            0 :           return;
   20930              :         }
   20931              : 
   20932            3 :       if (!attr->dummy)
   20933              :         {
   20934            2 :           gfc_warning (OPT_Wundefined_vars, "Unallocated variable %qs "
   20935              :                        "referenced at %L", sym->name, &sym->other_loc);
   20936            2 :           attr->warning_emitted = 1;
   20937            2 :           return;
   20938              :         }
   20939              :     }
   20940              : 
   20941         2424 :   if (warn_undefined_vars && !var_value_is_set (sym))
   20942              :     {
   20943              :       /* Warn about variables which have been allocated and used, but never
   20944              :          set.  */
   20945           48 :       if (attr->allocated && sym->attr.value_used > VALUE_MAYBE_USED)
   20946              :         {
   20947            3 :           switch (sym->attr.value_used)
   20948              :             {
   20949            1 :             case VALUE_INTENT_IN:
   20950            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20951              :                            "undefined to INTENT(IN) argument at %L", sym->name,
   20952              :                            &sym->other_loc);
   20953            1 :               break;
   20954              : 
   20955            1 :             case VALUE_VALUE_ARG:
   20956            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated variable %qs passed "
   20957              :                            "undefined to VALUE argument at %L", sym->name,
   20958              :                            &sym->other_loc);
   20959            1 :               break;
   20960            1 :             case VALUE_USED:
   20961            1 :               gfc_warning (OPT_Wundefined_vars, "Allocated undefined variable "
   20962              :                            "%qs used at %L", sym->name, &sym->other_loc);
   20963            1 :               break;
   20964            0 :             default:
   20965            0 :               gfc_internal_error ("Wrong value_set");
   20966            3 :               break;
   20967              :             }
   20968            3 :           attr->warning_emitted = 1;
   20969            3 :           return;
   20970              :         }
   20971              : 
   20972              :       /* Similar, when undefined variables are passed to INTENT(IN), VALUE
   20973              :          arguments or are used in general.  */
   20974              : 
   20975           45 :       if (attr->value_used == VALUE_INTENT_IN)
   20976              :         {
   20977            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20978              :                        "to INTENT(IN) argument at %L", sym->name, &sym->other_loc);
   20979            1 :           attr->warning_emitted = 1;
   20980            1 :           return;
   20981              :         }
   20982           44 :       else if (attr->value_used == VALUE_VALUE_ARG)
   20983              :         {
   20984            1 :           gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs passed "
   20985              :                        "to VALUE argument at %L", sym->name, &sym->other_loc);
   20986            1 :           attr->warning_emitted = 1;
   20987            1 :           return;
   20988              :         }
   20989           43 :       else if (attr->value_used == VALUE_USED)
   20990              :         {
   20991            9 :           if (attr->dummy && attr->intent == INTENT_OUT)
   20992            1 :             gfc_warning (OPT_Wundefined_vars, "Undefined INTENT(OUT) variable %qs "
   20993              :                          "used at %L", sym->name, &sym->other_loc);
   20994              :           else
   20995            8 :             gfc_warning (OPT_Wundefined_vars, "Undefined variable %qs used at "
   20996              :                          "%L", sym->name, &sym->other_loc);
   20997              : 
   20998            9 :           attr->warning_emitted = 1;
   20999            9 :           return;
   21000              :         }
   21001              : 
   21002              :       /* PR 28004 - warn about INTENT(OUT) variables that are never set.  If
   21003              :          the variable or a component are allocatable, do not warn since this is
   21004              :          a frequent shortcut for deallocation.  */
   21005              : 
   21006           34 :       if (sym->attr.dummy && sym->attr.intent == INTENT_OUT
   21007            2 :           && !(attr->allocatable || attr->alloc_comp))
   21008              :         {
   21009            0 :           gfc_warning (OPT_Wundefined_vars, "INTENT(OUT) variable %qs  "
   21010              :                        "declared at %L is not assigned a value", sym->name,
   21011              :                        &sym->declared_at);
   21012            0 :           attr->warning_emitted = 1;
   21013            0 :           return;
   21014              :         }
   21015              :     }
   21016              : 
   21017              :   /* Warn for unused but defined variables.  */
   21018              : 
   21019         2410 :   if (warn_unused_but_set_variable)
   21020              :     {
   21021         2302 :       if (attr->value_set == VALUE_VARDEF && !var_value_is_used (sym))
   21022              :         {
   21023            7 :           gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs defined at "
   21024              :                        "%L but never used", sym->name, &sym->other_loc);
   21025            7 :           attr->warning_emitted = 1;
   21026            7 :           return;
   21027              :         }
   21028         2295 :       if (attr->allocatable && !var_value_is_used (sym))
   21029              :         {
   21030            2 :           if (attr->allocated == ALLOCATED_ALLOCATE_STMT)
   21031              :             {
   21032            1 :               gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs "
   21033              :                            "allocated at %L but never used", sym->name,
   21034              :                            &sym->extra_loc);
   21035            1 :               attr->warning_emitted = 1;
   21036            1 :               return;
   21037              :             }
   21038            1 :           else if (attr->allocated == ALLOCATED_ARG)
   21039              :             {
   21040            1 :               gfc_warning (OPT_Wunused_but_set_variable_, "Variable %qs maybe "
   21041              :                            "allocated as argument at %L but never used",
   21042              :                            sym->name, &sym->extra_loc);
   21043            1 :               attr->warning_emitted = 1;
   21044            1 :               return;
   21045              :             }
   21046              :         }
   21047              :     }
   21048              : 
   21049              :   /* -Wunused-intent-out and -Wunused-read are enabled with -Wextra, so
   21050              :      check for these conditions at the end.  If one of the warnings
   21051              :      with -Wall triggered, we do not want to issue a different warrning
   21052              :      for the same variable if the user supplies -Wall -Wextra instead
   21053              :      of only -Wall.  */
   21054              : 
   21055           39 :   if (warn_unused_intent_out && attr->value_set == VALUE_INTENT_OUT
   21056         2406 :       && !var_value_is_used (sym))
   21057              :     {
   21058            1 :       gfc_warning (OPT_Wunused_intent_out, "Variable %qs passed to "
   21059              :                    "INTENT(OUT) argument at %L but value never used",
   21060              :                    sym->name, &sym->other_loc);
   21061            1 :       attr->warning_emitted = 1;
   21062            1 :       return;
   21063              :     }
   21064              : 
   21065         2400 :   if (warn_unused_read && attr->value_set == VALUE_READ && !var_value_is_used (sym))
   21066              :     {
   21067            1 :       gfc_warning (OPT_Wunused_read, "Variable %qs read at %L but never "
   21068              :                    "used", sym->name, &sym->other_loc);
   21069            1 :       attr->warning_emitted = 1;
   21070            1 :       return;
   21071              :     }
   21072              : }
   21073              : 
   21074              : /* Run warn_unused_vs_set over a namespace recursively.  */
   21075              : 
   21076              : static void
   21077         4845 : warn_unused_vs_set (gfc_namespace *ns)
   21078              : {
   21079         4845 :   gfc_traverse_ns (ns, find_unused_vs_set);
   21080              : 
   21081         5368 :   for (gfc_namespace *n = ns->contained; n; n = n->sibling)
   21082          523 :     warn_unused_vs_set (n);
   21083         4845 : }
   21084              : 
   21085              : /* This function is called after a complete program unit has been compiled.
   21086              :    Its purpose is to examine all of the expressions associated with a program
   21087              :    unit, assign types to all intermediate expressions, make sure that all
   21088              :    assignments are to compatible types and figure out which names refer to
   21089              :    which functions or subroutines.  */
   21090              : 
   21091              : void
   21092       319576 : gfc_resolve (gfc_namespace *ns, gfc_association_list *a)
   21093              : {
   21094       319576 :   gfc_namespace *old_ns;
   21095       319576 :   code_stack *old_cs_base;
   21096       319576 :   struct gfc_omp_saved_state old_omp_state;
   21097              : 
   21098       319576 :   if (ns->resolved)
   21099         4853 :     return;
   21100              : 
   21101       314723 :   ns->resolved = -1;
   21102       314723 :   old_ns = gfc_current_ns;
   21103       314723 :   old_cs_base = cs_base;
   21104              : 
   21105              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   21106              :      body, we should clear any state associated to it, so that say NS's
   21107              :      DO loops are not interpreted as OpenMP loops.  */
   21108       314723 :   if (!ns->construct_entities)
   21109       302120 :     gfc_omp_save_and_clear_state (&old_omp_state);
   21110              : 
   21111       314723 :   resolve_types (ns);
   21112       314723 :   component_assignment_level = 0;
   21113       314723 :   resolve_codes (ns);
   21114       314722 :   mark_assoc_used (a);
   21115              : 
   21116       314722 :   if (warn_unused_but_set_variable || warn_unused_intent_out
   21117       310458 :       || warn_unused_read || warn_undefined_vars)
   21118              :     {
   21119         4346 :       int error_count;
   21120         4346 :       gfc_get_errors (NULL, &error_count);
   21121         4346 :       if (error_count == 0)
   21122         4322 :         warn_unused_vs_set (ns);
   21123              :     }
   21124              : 
   21125       314722 :   if (ns->omp_assumes)
   21126           13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   21127              : 
   21128       314722 :   gfc_current_ns = old_ns;
   21129       314722 :   cs_base = old_cs_base;
   21130       314722 :   ns->resolved = 1;
   21131              : 
   21132       314722 :   gfc_run_passes (ns);
   21133              : 
   21134       314722 :   if (!ns->construct_entities)
   21135       302119 :     gfc_omp_restore_state (&old_omp_state);
   21136              : }
        

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.