LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.4 % 9659 9026
Test Date: 2026-04-20 14:57:17 Functions: 99.6 % 246 245
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        52243 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        56730 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         4738 :       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      1513022 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1513022 :   if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
     138              :     {
     139            5 :       if (where)
     140              :         {
     141            5 :           if (name)
     142            4 :             gfc_error ("%qs at %L is of the ABSTRACT type %qs",
     143              :                        name, where, ts->u.derived->name);
     144              :           else
     145            1 :             gfc_error ("ABSTRACT type %qs used at %L",
     146              :                        ts->u.derived->name, where);
     147              :         }
     148              : 
     149            5 :       return false;
     150              :     }
     151              : 
     152              :   return true;
     153              : }
     154              : 
     155              : 
     156              : static bool
     157         5590 : check_proc_interface (gfc_symbol *ifc, locus *where)
     158              : {
     159              :   /* Several checks for F08:C1216.  */
     160         5590 :   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         5588 :   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         5584 :   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         5580 :   if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
     187         5580 :       || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
     188           17 :     ifc->attr.intrinsic = 1;
     189         5580 :   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         5577 :   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         2079 : resolve_procedure_interface (gfc_symbol *sym)
     211              : {
     212         2079 :   gfc_symbol *ifc = sym->ts.interface;
     213              : 
     214         2079 :   if (!ifc)
     215              :     return true;
     216              : 
     217         1919 :   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         1917 :   if (!check_proc_interface (ifc, &sym->declared_at))
     224              :     return false;
     225              : 
     226         1908 :   if (ifc->attr.if_source || ifc->attr.intrinsic)
     227              :     {
     228              :       /* Resolve interface and copy attributes.  */
     229         1629 :       resolve_symbol (ifc);
     230         1629 :       if (ifc->attr.intrinsic)
     231           14 :         gfc_resolve_intrinsic (ifc, &ifc->declared_at);
     232              : 
     233         1629 :       if (ifc->result)
     234              :         {
     235          742 :           sym->ts = ifc->result->ts;
     236          742 :           sym->attr.allocatable = ifc->result->attr.allocatable;
     237          742 :           sym->attr.pointer = ifc->result->attr.pointer;
     238          742 :           sym->attr.dimension = ifc->result->attr.dimension;
     239          742 :           sym->attr.class_ok = ifc->result->attr.class_ok;
     240          742 :           sym->as = gfc_copy_array_spec (ifc->result->as);
     241          742 :           sym->result = sym;
     242              :         }
     243              :       else
     244              :         {
     245          887 :           sym->ts = ifc->ts;
     246          887 :           sym->attr.allocatable = ifc->attr.allocatable;
     247          887 :           sym->attr.pointer = ifc->attr.pointer;
     248          887 :           sym->attr.dimension = ifc->attr.dimension;
     249          887 :           sym->attr.class_ok = ifc->attr.class_ok;
     250          887 :           sym->as = gfc_copy_array_spec (ifc->as);
     251              :         }
     252         1629 :       sym->ts.interface = ifc;
     253         1629 :       sym->attr.function = ifc->attr.function;
     254         1629 :       sym->attr.subroutine = ifc->attr.subroutine;
     255              : 
     256         1629 :       sym->attr.pure = ifc->attr.pure;
     257         1629 :       sym->attr.elemental = ifc->attr.elemental;
     258         1629 :       sym->attr.contiguous = ifc->attr.contiguous;
     259         1629 :       sym->attr.recursive = ifc->attr.recursive;
     260         1629 :       sym->attr.always_explicit = ifc->attr.always_explicit;
     261         1629 :       sym->attr.ext_attr |= ifc->attr.ext_attr;
     262         1629 :       sym->attr.is_bind_c = ifc->attr.is_bind_c;
     263              :       /* Copy char length.  */
     264         1629 :       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       519009 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       519009 :   gfc_formal_arglist *f;
     290       519009 :   gfc_symbol *sym;
     291       519009 :   bool saved_specification_expr;
     292       519009 :   int i;
     293              : 
     294       519009 :   if (proc->result != NULL)
     295       323093 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       519009 :   if (gfc_elemental (proc)
     300       356839 :       || sym->attr.pointer || sym->attr.allocatable
     301       863809 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       176539 :       proc->attr.always_explicit = 1;
     304       176539 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       519009 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       519009 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1342262 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       823255 :       gfc_array_spec *as;
     313       823255 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       823255 :       sym = f->sym;
     316              : 
     317       823255 :       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          587 :       if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
     332       823671 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       823084 :       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       823082 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          855 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       823082 :       if (sym->attr.subroutine || sym->attr.external)
     347              :         {
     348          859 :           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       822223 :           if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
     354         3663 :               && (!sym->attr.function || sym->result == sym))
     355         3625 :             gfc_set_default_type (sym, 1, sym->ns);
     356              :         }
     357              : 
     358       823082 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       836864 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       823082 :       saved_specification_expr = specification_expr;
     362       823082 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       823082 :       specification_expr = true;
     364       823082 :       specification_expr_symbol = sym;
     365       823082 :       gfc_resolve_array_spec (as, 0);
     366       823082 :       specification_expr = saved_specification_expr;
     367       823082 :       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       823082 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        12217 :           && ((sym->ts.type != BT_CLASS
     374        11097 :                && !(sym->attr.pointer || sym->attr.allocatable))
     375         5344 :               || (sym->ts.type == BT_CLASS
     376         1120 :                   && !(CLASS_DATA (sym)->attr.class_pointer
     377          920 :                        || CLASS_DATA (sym)->attr.allocatable)))
     378         7378 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         7377 :           as->type = AS_ASSUMED_SHAPE;
     381        17115 :           for (i = 0; i < as->rank; i++)
     382         9738 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       128016 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       114427 :           || (as && as->type == AS_ASSUMED_RANK)
     387       772041 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       761933 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        11603 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11120 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10222 :                   || CLASS_DATA (sym)->attr.target))
     392       760552 :           || sym->attr.optional)
     393              :         {
     394        77692 :           proc->attr.always_explicit = 1;
     395        77692 :           if (proc->result)
     396        36119 :             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       823082 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        50362 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       823082 :       if (gfc_pure (proc))
     406              :         {
     407       327151 :           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       327122 :           else if (!sym->attr.pointer)
     418              :             {
     419       327108 :               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       327108 :               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       327150 :           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       823080 :       if (proc->attr.implicit_pure)
     458              :         {
     459        24727 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          313 :               if (!gfc_pure (sym))
     462          293 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        24414 :           else if (!sym->attr.pointer)
     465              :             {
     466        23634 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2739 :                   && !sym->value)
     468         2739 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        23634 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4196 :                   && !sym->value)
     472         4196 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       823080 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       301668 :           if (sym->attr.codimension
     480       301667 :               || (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       301665 :           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       301663 :           if (sym->attr.allocatable
     497       301662 :               || (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       301661 :           if (sym->attr.pointer
     507       301660 :               || (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       301659 :           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       301657 :           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       823067 :       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       519009 :   if (sym)
     562       518917 :     sym->formal_resolved = 1;
     563       519009 :   gfc_current_ns = orig_current_ns;
     564       519009 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1816104 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1816104 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       328831 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       326794 :   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       342788 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       342788 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        36938 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        36938 :   bool t;
     598              : 
     599        36938 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        36938 :       && sym->ns->parent
     601         1070 :       && sym->ns->parent->proc_name
     602         1070 :       && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
     603            1 :       && !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        36938 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        10849 :       || sym->attr.entry_master)
     611        26278 :     return;
     612              : 
     613        10660 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        10660 :   if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
     618              :     {
     619           57 :       t = gfc_set_default_type (sym->result, 0, ns);
     620              : 
     621           57 :       if (!t && !sym->result->attr.untyped)
     622              :         {
     623           19 :           if (sym->result == sym)
     624            1 :             gfc_error ("Contained function %qs at %L has no IMPLICIT type",
     625              :                        sym->name, &sym->declared_at);
     626           18 :           else if (!sym->result->attr.proc_pointer)
     627            0 :             gfc_error ("Result %qs of contained function %qs at %L has "
     628              :                        "no IMPLICIT type", sym->result->name, sym->name,
     629              :                        &sym->result->declared_at);
     630           19 :           sym->result->attr.untyped = 1;
     631              :         }
     632              :     }
     633              : 
     634              :   /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
     635              :      type, lists the only ways a character length value of * can be used:
     636              :      dummy arguments of procedures, named constants, function results and
     637              :      in allocate statements if the allocate_object is an assumed length dummy
     638              :      in external functions.  Internal function results and results of module
     639              :      procedures are not on this list, ergo, not permitted.  */
     640              : 
     641        10660 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1187 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1187 :       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       379219 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       379219 :   gfc_namespace *old_ns;
     729       379219 :   gfc_code *c;
     730       379219 :   gfc_symbol *proc;
     731       379219 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       379219 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       379219 :   static int master_count = 0;
     735              : 
     736       379219 :   if (ns->proc_name == NULL)
     737       378516 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       379170 :   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       344765 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       344765 :   gfc_symbol *csym = common_block->head;
     998       344765 :   gfc_gsymbol *gsym;
     999              : 
    1000       350816 :   for (; csym; csym = csym->common_next)
    1001              :     {
    1002         6051 :       gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
    1003         6051 :       if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
    1004              :         {
    1005            3 :           if (csym->common_block)
    1006            2 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
    1007              :                            "COMMON block at %L", gsym->name,
    1008              :                            &gsym->where, &csym->common_block->where);
    1009              :           else
    1010            1 :             gfc_error_now ("Global entity %qs at %L cannot appear in a "
    1011              :                            "COMMON block", gsym->name, &gsym->where);
    1012              :         }
    1013              : 
    1014              :       /* gfc_add_in_common may have been called before, but the reported errors
    1015              :          have been ignored to continue parsing.
    1016              :          We do the checks again here, unless the symbol is USE associated.  */
    1017         6051 :       if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
    1018              :         {
    1019         5778 :           gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
    1020         5778 :           gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
    1021              :                           &common_block->where);
    1022              :         }
    1023              : 
    1024         6051 :       if (csym->value || csym->attr.data)
    1025              :         {
    1026          149 :           if (!csym->ns->is_block_data)
    1027           33 :             gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
    1028              :                             "but only in BLOCK DATA initialization is "
    1029              :                             "allowed", csym->name, &csym->declared_at);
    1030          116 :           else if (!named_common)
    1031            8 :             gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
    1032              :                             "in a blank COMMON but initialization is only "
    1033              :                             "allowed in named common blocks", csym->name,
    1034              :                             &csym->declared_at);
    1035              :         }
    1036              : 
    1037         6051 :       if (UNLIMITED_POLY (csym))
    1038            1 :         gfc_error_now ("%qs at %L cannot appear in COMMON "
    1039              :                        "[F2008:C5100]", csym->name, &csym->declared_at);
    1040              : 
    1041         6051 :       if (csym->attr.dimension && is_non_constant_shape_array (csym))
    1042              :         {
    1043            1 :           gfc_error_now ("Automatic object %qs at %L cannot appear in "
    1044              :                          "COMMON at %L", csym->name, &csym->declared_at,
    1045              :                          &common_block->where);
    1046              :           /* Avoid confusing follow-on error.  */
    1047            1 :           csym->error = 1;
    1048              :         }
    1049              : 
    1050         6051 :       if (csym->ts.type != BT_DERIVED)
    1051         6004 :         continue;
    1052              : 
    1053           47 :       if (!(csym->ts.u.derived->attr.sequence
    1054            3 :             || csym->ts.u.derived->attr.is_bind_c))
    1055            2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1056              :                        "has neither the SEQUENCE nor the BIND(C) "
    1057              :                        "attribute", csym->name, &csym->declared_at);
    1058           47 :       if (csym->ts.u.derived->attr.alloc_comp)
    1059            3 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1060              :                        "has an ultimate component that is "
    1061              :                        "allocatable", csym->name, &csym->declared_at);
    1062           47 :       if (gfc_has_default_initializer (csym->ts.u.derived))
    1063            2 :         gfc_error_now ("Derived type variable %qs in COMMON at %L "
    1064              :                        "may not have default initializer", csym->name,
    1065              :                        &csym->declared_at);
    1066              : 
    1067           47 :       if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
    1068           16 :         gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
    1069              :     }
    1070       344765 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       343318 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       343318 :   gfc_symbol *sym = NULL;
    1077       343318 :   gfc_gsymbol * gsym;
    1078              : 
    1079       343318 :   if (common_root == NULL)
    1080       343196 :     return;
    1081              : 
    1082         1977 :   if (common_root->left)
    1083          246 :     resolve_common_blocks (common_root->left);
    1084         1977 :   if (common_root->right)
    1085          284 :     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       342788 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       342788 :   gfc_namespace *child;
    1208       342788 :   gfc_entry_list *el;
    1209              : 
    1210       342788 :   resolve_formal_arglists (ns);
    1211              : 
    1212       379219 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        36431 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        36431 :       resolve_contained_fntype (child->proc_name, child);
    1219        36938 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       342788 : }
    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          296 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
    1236              : {
    1237          296 :   param = gfc_get_actual_arglist ();
    1238          296 :   if (!param_list)
    1239          240 :     param_list = param_tail = param;
    1240              :   else
    1241              :     {
    1242           56 :       param_tail->next = param;
    1243           56 :       param_tail = param_tail->next;
    1244              :     }
    1245              : 
    1246          296 :   param_tail->name = c->name;
    1247          296 :   if (expr)
    1248          296 :     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          276 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
    1267              :                      gfc_symbol *derived)
    1268              : {
    1269          276 :   gfc_constructor *cons = NULL;
    1270          276 :   gfc_component *comp;
    1271          276 :   bool t = true;
    1272              : 
    1273          276 :   if (expr && expr->expr_type == EXPR_STRUCTURE)
    1274          240 :     cons = gfc_constructor_first (expr->value.constructor);
    1275           36 :   else if (constr)
    1276           36 :     cons = *constr;
    1277          276 :   gcc_assert (cons);
    1278              : 
    1279          276 :   comp = derived->components;
    1280              : 
    1281          844 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1282              :     {
    1283          568 :       if (cons->expr
    1284          568 :           && 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          568 :       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          532 :      else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
    1298          296 :                && derived->attr.pdt_template)
    1299              :         {
    1300          296 :           t = get_pdt_spec_expr (comp, cons->expr);
    1301          296 :           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        62695 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        62695 :   gfc_constructor *cons;
    1321        62695 :   gfc_component *comp;
    1322        62695 :   bool t;
    1323        62695 :   symbol_attribute a;
    1324              : 
    1325        62695 :   t = true;
    1326              : 
    1327        62695 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        59834 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        59684 :         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        59834 :       if (expr->ts.u.derived->attr.pdt_template)
    1337              :         {
    1338          240 :           param_list = NULL;
    1339          240 :           t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
    1340          240 :           if (!t)
    1341              :             return t;
    1342          240 :           gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
    1343              : 
    1344          240 :           expr->param_list = gfc_copy_actual_arglist (param_list);
    1345              : 
    1346          240 :           if (param_list)
    1347          240 :             gfc_free_actual_arglist (param_list);
    1348              : 
    1349          240 :           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        62695 :   if (expr->ref)
    1358          160 :     comp = expr->ref->u.c.sym->components;
    1359        62535 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        62533 :            && expr->ts.u.derived)
    1362        62533 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        62693 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       208540 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       145849 :       int rank;
    1371              : 
    1372       145849 :       if (!cons->expr)
    1373         9691 :         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       136158 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       136143 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       136143 :       rank = comp->as ? comp->as->rank : 0;
    1388       136143 :       if (comp->ts.type == BT_CLASS
    1389         1763 :           && !comp->ts.u.derived->attr.unlimited_polymorphic
    1390         1762 :           && CLASS_DATA (comp)->as)
    1391          519 :         rank = CLASS_DATA (comp)->as->rank;
    1392              : 
    1393       136143 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          228 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       136143 :       if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
    1397          477 :           && (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       238070 :       if (!comp->attr.proc_pointer &&
    1409       101927 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        12410 :           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         9070 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3340 :           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         3338 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3275 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3275 :               if (t)
    1431       136143 :                 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       136143 :       if (cons->expr->ts.type == BT_CHARACTER
    1440         3889 :           && comp->ts.type == BT_CHARACTER
    1441         3863 :           && comp->ts.u.cl && comp->ts.u.cl->length
    1442         2498 :           && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
    1443         2463 :           && 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       136143 :       if (cons->expr->expr_type == EXPR_NULL
    1494        40795 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        20321 :                || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
    1496         1112 :                || (comp->ts.type == BT_CLASS
    1497         1110 :                    && (CLASS_DATA (comp)->attr.class_pointer
    1498          893 :                        || 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       136143 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        15327 :           gfc_symbol *s2 = NULL;
    1511        15327 :           gfc_component *c2;
    1512        15327 :           const char *name;
    1513        15327 :           char err[200];
    1514              : 
    1515        15327 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        15327 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        15315 :           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        15315 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        14902 :               s2 = cons->expr->symtree->n.sym;
    1529        14902 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        14914 :           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       136141 :       if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
    1544         2251 :           && comp->as && !comp->attr.allocatable && !comp->attr.pointer
    1545         1520 :           && !comp->attr.pdt_array)
    1546              :         {
    1547         1273 :           mpz_t len;
    1548         1273 :           mpz_init (len);
    1549         2639 :           for (int n = 0; n < rank; n++)
    1550              :             {
    1551         1371 :               if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
    1552         1366 :                   || 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         1366 :                 };
    1560         1366 :               if (cons->expr->shape == NULL)
    1561           12 :                 continue;
    1562         1354 :               mpz_set_ui (len, 1);
    1563         1354 :               mpz_add (len, len, comp->as->upper[n]->value.integer);
    1564         1354 :               mpz_sub (len, len, comp->as->lower[n]->value.integer);
    1565         1354 :               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         1273 :           mpz_clear (len);
    1577              :         }
    1578              : 
    1579       136141 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        21869 :           || cons->expr->expr_type == EXPR_NULL)
    1581       126095 :         continue;
    1582              : 
    1583        10046 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        10046 :       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        10046 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596         9978 :           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         9978 :           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        10046 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        20091 :           && 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        10046 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        10046 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        10010 :                         || gfc_is_coindexed (cons->expr));
    1624           33 :       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        10046 :       if (impure)
    1633           33 :         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       741806 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       741806 :   symbol_attribute a;
    1649              : 
    1650       741806 :   a = sym->attr;
    1651              : 
    1652       741806 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       628740 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       620160 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       620158 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       620104 :       || a.asynchronous || a.codimension || a.subroutine)
    1659        94666 :     return 1;
    1660              : 
    1661              :   return 0;
    1662              : }
    1663              : 
    1664              : 
    1665              : /* Determine if a symbol is generic or not.  */
    1666              : 
    1667              : static int
    1668       411978 : generic_sym (gfc_symbol *sym)
    1669              : {
    1670       411978 :   gfc_symbol *s;
    1671              : 
    1672       411978 :   if (sym->attr.generic ||
    1673       382781 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1674        30260 :     return 1;
    1675              : 
    1676       381718 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1677              :     return 0;
    1678              : 
    1679        76874 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1680              : 
    1681        76874 :   if (s != NULL)
    1682              :     {
    1683          133 :       if (s == sym)
    1684              :         return 0;
    1685              :       else
    1686          132 :         return generic_sym (s);
    1687              :     }
    1688              : 
    1689              :   return 0;
    1690              : }
    1691              : 
    1692              : 
    1693              : /* Determine if a symbol is specific or not.  */
    1694              : 
    1695              : static int
    1696       381630 : specific_sym (gfc_symbol *sym)
    1697              : {
    1698       381630 :   gfc_symbol *s;
    1699              : 
    1700       381630 :   if (sym->attr.if_source == IFSRC_IFBODY
    1701       370377 :       || sym->attr.proc == PROC_MODULE
    1702              :       || sym->attr.proc == PROC_INTERNAL
    1703              :       || sym->attr.proc == PROC_ST_FUNCTION
    1704       294201 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1705       675100 :       || sym->attr.external)
    1706        90545 :     return 1;
    1707              : 
    1708       291085 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1709              :     return 0;
    1710              : 
    1711        76772 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1712              : 
    1713        76772 :   return (s == NULL) ? 0 : specific_sym (s);
    1714              : }
    1715              : 
    1716              : 
    1717              : /* Figure out if the procedure is specific, generic or unknown.  */
    1718              : 
    1719              : enum proc_type
    1720              : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
    1721              : 
    1722              : static proc_type
    1723       411700 : procedure_kind (gfc_symbol *sym)
    1724              : {
    1725       411700 :   if (generic_sym (sym))
    1726              :     return PTYPE_GENERIC;
    1727              : 
    1728       381583 :   if (specific_sym (sym))
    1729        90545 :     return PTYPE_SPECIFIC;
    1730              : 
    1731              :   return PTYPE_UNKNOWN;
    1732              : }
    1733              : 
    1734              : /* Check references to assumed size arrays.  The flag need_full_assumed_size
    1735              :    is nonzero when matching actual arguments.  */
    1736              : 
    1737              : static int need_full_assumed_size = 0;
    1738              : 
    1739              : static bool
    1740      1418949 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1741              : {
    1742      1418949 :   if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
    1743              :       return false;
    1744              : 
    1745              :   /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
    1746              :      What should it be?  */
    1747         3788 :   if (e->ref
    1748         3786 :       && e->ref->u.ar.as
    1749         3785 :       && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
    1750         3290 :       && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
    1751         3290 :       && (e->ref->u.ar.type == AR_FULL))
    1752              :     {
    1753           25 :       gfc_error ("The upper bound in the last dimension must "
    1754              :                  "appear in the reference to the assumed size "
    1755              :                  "array %qs at %L", sym->name, &e->where);
    1756           25 :       return true;
    1757              :     }
    1758              :   return false;
    1759              : }
    1760              : 
    1761              : 
    1762              : /* Look for bad assumed size array references in argument expressions
    1763              :   of elemental and array valued intrinsic procedures.  Since this is
    1764              :   called from procedure resolution functions, it only recurses at
    1765              :   operators.  */
    1766              : 
    1767              : static bool
    1768       228479 : resolve_assumed_size_actual (gfc_expr *e)
    1769              : {
    1770       228479 :   if (e == NULL)
    1771              :    return false;
    1772              : 
    1773       227912 :   switch (e->expr_type)
    1774              :     {
    1775       110000 :     case EXPR_VARIABLE:
    1776       110000 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1777              :         return true;
    1778              :       break;
    1779              : 
    1780        48234 :     case EXPR_OP:
    1781        48234 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1782        48234 :           || resolve_assumed_size_actual (e->value.op.op2))
    1783            0 :         return true;
    1784              :       break;
    1785              : 
    1786              :     default:
    1787              :       break;
    1788              :     }
    1789              :   return false;
    1790              : }
    1791              : 
    1792              : 
    1793              : /* Check a generic procedure, passed as an actual argument, to see if
    1794              :    there is a matching specific name.  If none, it is an error, and if
    1795              :    more than one, the reference is ambiguous.  */
    1796              : static int
    1797            8 : count_specific_procs (gfc_expr *e)
    1798              : {
    1799            8 :   int n;
    1800            8 :   gfc_interface *p;
    1801            8 :   gfc_symbol *sym;
    1802              : 
    1803            8 :   n = 0;
    1804            8 :   sym = e->symtree->n.sym;
    1805              : 
    1806           22 :   for (p = sym->generic; p; p = p->next)
    1807           14 :     if (strcmp (sym->name, p->sym->name) == 0)
    1808              :       {
    1809            8 :         e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
    1810              :                                        sym->name);
    1811            8 :         n++;
    1812              :       }
    1813              : 
    1814            8 :   if (n > 1)
    1815            1 :     gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
    1816              :                &e->where);
    1817              : 
    1818            8 :   if (n == 0)
    1819            1 :     gfc_error ("GENERIC procedure %qs is not allowed as an actual "
    1820              :                "argument at %L", sym->name, &e->where);
    1821              : 
    1822            8 :   return n;
    1823              : }
    1824              : 
    1825              : 
    1826              : /* See if a call to sym could possibly be a not allowed RECURSION because of
    1827              :    a missing RECURSIVE declaration.  This means that either sym is the current
    1828              :    context itself, or sym is the parent of a contained procedure calling its
    1829              :    non-RECURSIVE containing procedure.
    1830              :    This also works if sym is an ENTRY.  */
    1831              : 
    1832              : static bool
    1833       151243 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1834              : {
    1835       151243 :   gfc_symbol* proc_sym;
    1836       151243 :   gfc_symbol* context_proc;
    1837       151243 :   gfc_namespace* real_context;
    1838              : 
    1839       151243 :   if (sym->attr.flavor == FL_PROGRAM
    1840              :       || gfc_fl_struct (sym->attr.flavor))
    1841              :     return false;
    1842              : 
    1843              :   /* If we've got an ENTRY, find real procedure.  */
    1844       151242 :   if (sym->attr.entry && sym->ns->entries)
    1845           45 :     proc_sym = sym->ns->entries->sym;
    1846              :   else
    1847              :     proc_sym = sym;
    1848              : 
    1849              :   /* If sym is RECURSIVE, all is well of course.  */
    1850       151242 :   if (proc_sym->attr.recursive || flag_recursive)
    1851              :     return false;
    1852              : 
    1853              :   /* Find the context procedure's "real" symbol if it has entries.
    1854              :      We look for a procedure symbol, so recurse on the parents if we don't
    1855              :      find one (like in case of a BLOCK construct).  */
    1856         1838 :   for (real_context = context; ; real_context = real_context->parent)
    1857              :     {
    1858              :       /* We should find something, eventually!  */
    1859       128304 :       gcc_assert (real_context);
    1860              : 
    1861       128304 :       context_proc = (real_context->entries ? real_context->entries->sym
    1862              :                                             : real_context->proc_name);
    1863              : 
    1864              :       /* In some special cases, there may not be a proc_name, like for this
    1865              :          invalid code:
    1866              :          real(bad_kind()) function foo () ...
    1867              :          when checking the call to bad_kind ().
    1868              :          In these cases, we simply return here and assume that the
    1869              :          call is ok.  */
    1870       128304 :       if (!context_proc)
    1871              :         return false;
    1872              : 
    1873       128040 :       if (context_proc->attr.flavor != FL_LABEL)
    1874              :         break;
    1875              :     }
    1876              : 
    1877              :   /* A call from sym's body to itself is recursion, of course.  */
    1878       126202 :   if (context_proc == proc_sym)
    1879              :     return true;
    1880              : 
    1881              :   /* The same is true if context is a contained procedure and sym the
    1882              :      containing one.  */
    1883       126187 :   if (context_proc->attr.contained)
    1884              :     {
    1885        21169 :       gfc_symbol* parent_proc;
    1886              : 
    1887        21169 :       gcc_assert (context->parent);
    1888        21169 :       parent_proc = (context->parent->entries ? context->parent->entries->sym
    1889              :                                               : context->parent->proc_name);
    1890              : 
    1891        21169 :       if (parent_proc == proc_sym)
    1892            9 :         return true;
    1893              :     }
    1894              : 
    1895              :   return false;
    1896              : }
    1897              : 
    1898              : 
    1899              : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
    1900              :    its typespec and formal argument list.  */
    1901              : 
    1902              : bool
    1903        42413 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1904              : {
    1905        42413 :   gfc_intrinsic_sym* isym = NULL;
    1906        42413 :   const char* symstd;
    1907              : 
    1908        42413 :   if (sym->resolve_symbol_called >= 2)
    1909              :     return true;
    1910              : 
    1911        32654 :   sym->resolve_symbol_called = 2;
    1912              : 
    1913              :   /* Already resolved.  */
    1914        32654 :   if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
    1915              :     return true;
    1916              : 
    1917              :   /* We already know this one is an intrinsic, so we don't call
    1918              :      gfc_is_intrinsic for full checking but rather use gfc_find_function and
    1919              :      gfc_find_subroutine directly to check whether it is a function or
    1920              :      subroutine.  */
    1921              : 
    1922        24815 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1923              :     {
    1924         8922 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1925         8922 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1926         8922 :     }
    1927        15893 :   else if (sym->intmod_sym_id)
    1928              :     {
    1929        12213 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1930        12213 :       isym = gfc_intrinsic_function_by_id (id);
    1931              :     }
    1932         3680 :   else if (!sym->attr.subroutine)
    1933         3593 :     isym = gfc_find_function (sym->name);
    1934              : 
    1935        24728 :   if (isym && !sym->attr.subroutine)
    1936              :     {
    1937        15761 :       if (sym->ts.type != BT_UNKNOWN && warn_surprising
    1938           24 :           && !sym->attr.implicit_type)
    1939           10 :         gfc_warning (OPT_Wsurprising,
    1940              :                      "Type specified for intrinsic function %qs at %L is"
    1941              :                       " ignored", sym->name, &sym->declared_at);
    1942              : 
    1943        19944 :       if (!sym->attr.function &&
    1944         4183 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1945              :         return false;
    1946              : 
    1947        15761 :       sym->ts = isym->ts;
    1948              :     }
    1949         9054 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1950              :     {
    1951         9051 :       if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
    1952              :         {
    1953            1 :           gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
    1954              :                       " specifier", sym->name, &sym->declared_at);
    1955            1 :           return false;
    1956              :         }
    1957              : 
    1958         9091 :       if (!sym->attr.subroutine &&
    1959           41 :           !gfc_add_subroutine(&sym->attr, sym->name, loc))
    1960              :         return false;
    1961              :     }
    1962              :   else
    1963              :     {
    1964            3 :       gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
    1965              :                  &sym->declared_at);
    1966            3 :       return false;
    1967              :     }
    1968              : 
    1969        24810 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1970              : 
    1971        24810 :   sym->attr.pure = isym->pure;
    1972        24810 :   sym->attr.elemental = isym->elemental;
    1973              : 
    1974              :   /* Check it is actually available in the standard settings.  */
    1975        24810 :   if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
    1976              :     {
    1977           31 :       gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
    1978              :                  "available in the current standard settings but %s. Use "
    1979              :                  "an appropriate %<-std=*%> option or enable "
    1980              :                  "%<-fall-intrinsics%> in order to use it.",
    1981              :                  sym->name, &sym->declared_at, symstd);
    1982           31 :       return false;
    1983              :     }
    1984              : 
    1985              :   return true;
    1986              : }
    1987              : 
    1988              : 
    1989              : /* Resolve a procedure expression, like passing it to a called procedure or as
    1990              :    RHS for a procedure pointer assignment.  */
    1991              : 
    1992              : static bool
    1993      1321959 : resolve_procedure_expression (gfc_expr* expr)
    1994              : {
    1995      1321959 :   gfc_symbol* sym;
    1996              : 
    1997      1321959 :   if (expr->expr_type != EXPR_VARIABLE)
    1998              :     return true;
    1999      1321942 :   gcc_assert (expr->symtree);
    2000              : 
    2001      1321942 :   sym = expr->symtree->n.sym;
    2002              : 
    2003      1321942 :   if (sym->attr.intrinsic)
    2004         1346 :     gfc_resolve_intrinsic (sym, &expr->where);
    2005              : 
    2006      1321942 :   if (sym->attr.flavor != FL_PROCEDURE
    2007        31580 :       || (sym->attr.function && sym->result == sym))
    2008              :     return true;
    2009              : 
    2010              :    /* A non-RECURSIVE procedure that is used as procedure expression within its
    2011              :      own body is in danger of being called recursively.  */
    2012        17038 :   if (is_illegal_recursion (sym, gfc_current_ns))
    2013              :     {
    2014           10 :       if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
    2015            0 :         gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
    2016              :                      " possibly calling itself recursively in procedure %qs. "
    2017              :                      " Declare it RECURSIVE or use %<-frecursive%>",
    2018            0 :                      sym->name, sym->module, gfc_current_ns->proc_name->name);
    2019              :       else
    2020           10 :         gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    2021              :                      " itself recursively.  Declare it RECURSIVE or use"
    2022              :                      " %<-frecursive%>", sym->name, &expr->where);
    2023              :     }
    2024              : 
    2025              :   return true;
    2026              : }
    2027              : 
    2028              : 
    2029              : /* Check that name is not a derived type.  */
    2030              : 
    2031              : static bool
    2032         3403 : is_dt_name (const char *name)
    2033              : {
    2034         3403 :   gfc_symbol *dt_list, *dt_first;
    2035              : 
    2036         3403 :   dt_list = dt_first = gfc_derived_types;
    2037         5850 :   for (; dt_list; dt_list = dt_list->dt_next)
    2038              :     {
    2039         3563 :       if (strcmp(dt_list->name, name) == 0)
    2040              :         return true;
    2041         3560 :       if (dt_first == dt_list->dt_next)
    2042              :         break;
    2043              :     }
    2044              :   return false;
    2045              : }
    2046              : 
    2047              : 
    2048              : /* Resolve an actual argument list.  Most of the time, this is just
    2049              :    resolving the expressions in the list.
    2050              :    The exception is that we sometimes have to decide whether arguments
    2051              :    that look like procedure arguments are really simple variable
    2052              :    references.  */
    2053              : 
    2054              : static bool
    2055       425814 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2056              :                         bool no_formal_args)
    2057              : {
    2058       425814 :   gfc_symbol *sym = NULL;
    2059       425814 :   gfc_symtree *parent_st;
    2060       425814 :   gfc_expr *e;
    2061       425814 :   gfc_component *comp;
    2062       425814 :   int save_need_full_assumed_size;
    2063       425814 :   bool return_value = false;
    2064       425814 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2065              : 
    2066       425814 :   actual_arg = true;
    2067       425814 :   first_actual_arg = true;
    2068              : 
    2069      1093950 :   for (; arg; arg = arg->next)
    2070              :     {
    2071       668237 :       e = arg->expr;
    2072       668237 :       if (e == NULL)
    2073              :         {
    2074              :           /* Check the label is a valid branching target.  */
    2075         2436 :           if (arg->label)
    2076              :             {
    2077          236 :               if (arg->label->defined == ST_LABEL_UNKNOWN)
    2078              :                 {
    2079            0 :                   gfc_error ("Label %d referenced at %L is never defined",
    2080              :                              arg->label->value, &arg->label->where);
    2081            0 :                   goto cleanup;
    2082              :                 }
    2083              :             }
    2084         2436 :           first_actual_arg = false;
    2085         2436 :           continue;
    2086              :         }
    2087              : 
    2088       665801 :       if (e->expr_type == EXPR_VARIABLE
    2089       293766 :             && e->symtree->n.sym->attr.generic
    2090            8 :             && no_formal_args
    2091       665806 :             && count_specific_procs (e) != 1)
    2092            2 :         goto cleanup;
    2093              : 
    2094       665799 :       if (e->ts.type != BT_PROCEDURE)
    2095              :         {
    2096       593393 :           save_need_full_assumed_size = need_full_assumed_size;
    2097       593393 :           if (e->expr_type != EXPR_VARIABLE)
    2098       372035 :             need_full_assumed_size = 0;
    2099       593393 :           if (!gfc_resolve_expr (e))
    2100           60 :             goto cleanup;
    2101       593333 :           need_full_assumed_size = save_need_full_assumed_size;
    2102       593333 :           goto argument_list;
    2103              :         }
    2104              : 
    2105              :       /* See if the expression node should really be a variable reference.  */
    2106              : 
    2107        72406 :       sym = e->symtree->n.sym;
    2108              : 
    2109        72406 :       if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
    2110              :         {
    2111            3 :           gfc_error ("Derived type %qs is used as an actual "
    2112              :                      "argument at %L", sym->name, &e->where);
    2113            3 :           goto cleanup;
    2114              :         }
    2115              : 
    2116        72403 :       if (sym->attr.flavor == FL_PROCEDURE
    2117        69003 :           || sym->attr.intrinsic
    2118        69003 :           || sym->attr.external)
    2119              :         {
    2120         3400 :           int actual_ok;
    2121              : 
    2122              :           /* If a procedure is not already determined to be something else
    2123              :              check if it is intrinsic.  */
    2124         3400 :           if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
    2125         1254 :             sym->attr.intrinsic = 1;
    2126              : 
    2127         3400 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    2128              :             {
    2129            2 :               gfc_error ("Statement function %qs at %L is not allowed as an "
    2130              :                          "actual argument", sym->name, &e->where);
    2131              :             }
    2132              : 
    2133         6800 :           actual_ok = gfc_intrinsic_actual_ok (sym->name,
    2134         3400 :                                                sym->attr.subroutine);
    2135         3400 :           if (sym->attr.intrinsic && actual_ok == 0)
    2136              :             {
    2137            0 :               gfc_error ("Intrinsic %qs at %L is not allowed as an "
    2138              :                          "actual argument", sym->name, &e->where);
    2139              :             }
    2140              : 
    2141         3400 :           if (sym->attr.contained && !sym->attr.use_assoc
    2142          432 :               && sym->ns->proc_name->attr.flavor != FL_MODULE)
    2143              :             {
    2144          244 :               if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
    2145              :                                    " used as actual argument at %L",
    2146              :                                    sym->name, &e->where))
    2147            3 :                 goto cleanup;
    2148              :             }
    2149              : 
    2150         3397 :           if (sym->attr.elemental && !sym->attr.intrinsic)
    2151              :             {
    2152            2 :               gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
    2153              :                          "allowed as an actual argument at %L", sym->name,
    2154              :                          &e->where);
    2155              :             }
    2156              : 
    2157              :           /* Check if a generic interface has a specific procedure
    2158              :             with the same name before emitting an error.  */
    2159         3397 :           if (sym->attr.generic && count_specific_procs (e) != 1)
    2160            0 :             goto cleanup;
    2161              : 
    2162              :           /* Just in case a specific was found for the expression.  */
    2163         3397 :           sym = e->symtree->n.sym;
    2164              : 
    2165              :           /* If the symbol is the function that names the current (or
    2166              :              parent) scope, then we really have a variable reference.  */
    2167              : 
    2168         3397 :           if (gfc_is_function_return_value (sym, sym->ns))
    2169            0 :             goto got_variable;
    2170              : 
    2171              :           /* If all else fails, see if we have a specific intrinsic.  */
    2172         3397 :           if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
    2173              :             {
    2174            0 :               gfc_intrinsic_sym *isym;
    2175              : 
    2176            0 :               isym = gfc_find_function (sym->name);
    2177            0 :               if (isym == NULL || !isym->specific)
    2178              :                 {
    2179            0 :                   gfc_error ("Unable to find a specific INTRINSIC procedure "
    2180              :                              "for the reference %qs at %L", sym->name,
    2181              :                              &e->where);
    2182            0 :                   goto cleanup;
    2183              :                 }
    2184            0 :               sym->ts = isym->ts;
    2185            0 :               sym->attr.intrinsic = 1;
    2186            0 :               sym->attr.function = 1;
    2187              :             }
    2188              : 
    2189         3397 :           if (!gfc_resolve_expr (e))
    2190            0 :             goto cleanup;
    2191         3397 :           goto argument_list;
    2192              :         }
    2193              : 
    2194              :       /* See if the name is a module procedure in a parent unit.  */
    2195              : 
    2196        69003 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2197        68910 :         goto got_variable;
    2198              : 
    2199           93 :       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
    2200              :         {
    2201            0 :           gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
    2202            0 :           goto cleanup;
    2203              :         }
    2204              : 
    2205           93 :       if (parent_st == NULL)
    2206           93 :         goto got_variable;
    2207              : 
    2208            0 :       sym = parent_st->n.sym;
    2209            0 :       e->symtree = parent_st;                /* Point to the right thing.  */
    2210              : 
    2211            0 :       if (sym->attr.flavor == FL_PROCEDURE
    2212            0 :           || sym->attr.intrinsic
    2213            0 :           || sym->attr.external)
    2214              :         {
    2215            0 :           if (!gfc_resolve_expr (e))
    2216            0 :             goto cleanup;
    2217            0 :           goto argument_list;
    2218              :         }
    2219              : 
    2220            0 :     got_variable:
    2221        69003 :       e->expr_type = EXPR_VARIABLE;
    2222        69003 :       e->ts = sym->ts;
    2223        69003 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2224        35766 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2225         3834 :               && CLASS_DATA (sym)->as))
    2226              :         {
    2227        38777 :           gfc_array_spec *as
    2228        36007 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2229        36007 :           e->rank = as->rank;
    2230        36007 :           e->corank = as->corank;
    2231        36007 :           e->ref = gfc_get_ref ();
    2232        36007 :           e->ref->type = REF_ARRAY;
    2233        36007 :           e->ref->u.ar.type = AR_FULL;
    2234        36007 :           e->ref->u.ar.as = as;
    2235              :         }
    2236              : 
    2237              :       /* These symbols are set untyped by calls to gfc_set_default_type
    2238              :          with 'error_flag' = false.  Reset the untyped attribute so that
    2239              :          the error will be generated in gfc_resolve_expr.  */
    2240        69003 :       if (e->expr_type == EXPR_VARIABLE
    2241        69003 :           && sym->ts.type == BT_UNKNOWN
    2242           36 :           && sym->attr.untyped)
    2243            5 :         sym->attr.untyped = 0;
    2244              : 
    2245              :       /* Expressions are assigned a default ts.type of BT_PROCEDURE in
    2246              :          primary.cc (match_actual_arg). If above code determines that it
    2247              :          is a  variable instead, it needs to be resolved as it was not
    2248              :          done at the beginning of this function.  */
    2249        69003 :       save_need_full_assumed_size = need_full_assumed_size;
    2250        69003 :       if (e->expr_type != EXPR_VARIABLE)
    2251            0 :         need_full_assumed_size = 0;
    2252        69003 :       if (!gfc_resolve_expr (e))
    2253           22 :         goto cleanup;
    2254        68981 :       need_full_assumed_size = save_need_full_assumed_size;
    2255              : 
    2256       665711 :     argument_list:
    2257              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2258              :          nothing to do for %REF.  */
    2259       665711 :       if (arg->name && arg->name[0] == '%')
    2260              :         {
    2261           42 :           if (strcmp ("%VAL", arg->name) == 0)
    2262              :             {
    2263           28 :               if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
    2264              :                 {
    2265            2 :                   gfc_error ("By-value argument at %L is not of numeric "
    2266              :                              "type", &e->where);
    2267            2 :                   goto cleanup;
    2268              :                 }
    2269              : 
    2270           26 :               if (e->rank)
    2271              :                 {
    2272            1 :                   gfc_error ("By-value argument at %L cannot be an array or "
    2273              :                              "an array section", &e->where);
    2274            1 :                   goto cleanup;
    2275              :                 }
    2276              : 
    2277              :               /* Intrinsics are still PROC_UNKNOWN here.  However,
    2278              :                  since same file external procedures are not resolvable
    2279              :                  in gfortran, it is a good deal easier to leave them to
    2280              :                  intrinsic.cc.  */
    2281           25 :               if (ptype != PROC_UNKNOWN
    2282           25 :                   && ptype != PROC_DUMMY
    2283            9 :                   && ptype != PROC_EXTERNAL
    2284            9 :                   && ptype != PROC_MODULE)
    2285              :                 {
    2286            3 :                   gfc_error ("By-value argument at %L is not allowed "
    2287              :                              "in this context", &e->where);
    2288            3 :                   goto cleanup;
    2289              :                 }
    2290              :             }
    2291              : 
    2292              :           /* Statement functions have already been excluded above.  */
    2293           14 :           else if (strcmp ("%LOC", arg->name) == 0
    2294            8 :                    && e->ts.type == BT_PROCEDURE)
    2295              :             {
    2296            0 :               if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
    2297              :                 {
    2298            0 :                   gfc_error ("Passing internal procedure at %L by location "
    2299              :                              "not allowed", &e->where);
    2300            0 :                   goto cleanup;
    2301              :                 }
    2302              :             }
    2303              :         }
    2304              : 
    2305       665705 :       comp = gfc_get_proc_ptr_comp(e);
    2306       665705 :       if (e->expr_type == EXPR_VARIABLE
    2307       292388 :           && comp && comp->attr.elemental)
    2308              :         {
    2309            1 :             gfc_error ("ELEMENTAL procedure pointer component %qs is not "
    2310              :                        "allowed as an actual argument at %L", comp->name,
    2311              :                        &e->where);
    2312              :         }
    2313              : 
    2314              :       /* Fortran 2008, C1237.  */
    2315       292388 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2316       666150 :           && gfc_has_ultimate_pointer (e))
    2317              :         {
    2318            3 :           gfc_error ("Coindexed actual argument at %L with ultimate pointer "
    2319              :                      "component", &e->where);
    2320            3 :           goto cleanup;
    2321              :         }
    2322              : 
    2323       665702 :       if (e->expr_type == EXPR_VARIABLE
    2324       292385 :           && e->ts.type == BT_PROCEDURE
    2325         3397 :           && no_formal_args
    2326         1505 :           && sym->attr.flavor == FL_PROCEDURE
    2327         1505 :           && sym->attr.if_source == IFSRC_UNKNOWN
    2328          142 :           && !sym->attr.external
    2329            2 :           && !sym->attr.intrinsic
    2330            2 :           && !sym->attr.artificial
    2331            2 :           && !sym->ts.interface)
    2332              :         {
    2333              :           /* Emit a warning for -std=legacy and an error otherwise. */
    2334            2 :           if (gfc_option.warn_std == 0)
    2335            0 :             gfc_warning (0, "Procedure %qs at %L used as actual argument but "
    2336              :                          "does neither have an explicit interface nor the "
    2337              :                          "EXTERNAL attribute", sym->name, &e->where);
    2338              :           else
    2339              :             {
    2340            2 :               gfc_error ("Procedure %qs at %L used as actual argument but "
    2341              :                          "does neither have an explicit interface nor the "
    2342              :                          "EXTERNAL attribute", sym->name, &e->where);
    2343            2 :               goto cleanup;
    2344              :             }
    2345              :         }
    2346              : 
    2347       665700 :       first_actual_arg = false;
    2348              :     }
    2349              : 
    2350              :   return_value = true;
    2351              : 
    2352       425814 : cleanup:
    2353       425814 :   actual_arg = actual_arg_sav;
    2354       425814 :   first_actual_arg = first_actual_arg_sav;
    2355              : 
    2356       425814 :   return return_value;
    2357              : }
    2358              : 
    2359              : 
    2360              : /* Do the checks of the actual argument list that are specific to elemental
    2361              :    procedures.  If called with c == NULL, we have a function, otherwise if
    2362              :    expr == NULL, we have a subroutine.  */
    2363              : 
    2364              : static bool
    2365       324099 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2366              : {
    2367       324099 :   gfc_actual_arglist *arg0;
    2368       324099 :   gfc_actual_arglist *arg;
    2369       324099 :   gfc_symbol *esym = NULL;
    2370       324099 :   gfc_intrinsic_sym *isym = NULL;
    2371       324099 :   gfc_expr *e = NULL;
    2372       324099 :   gfc_intrinsic_arg *iformal = NULL;
    2373       324099 :   gfc_formal_arglist *eformal = NULL;
    2374       324099 :   bool formal_optional = false;
    2375       324099 :   bool set_by_optional = false;
    2376       324099 :   int i;
    2377       324099 :   int rank = 0;
    2378              : 
    2379              :   /* Is this an elemental procedure?  */
    2380       324099 :   if (expr && expr->value.function.actual != NULL)
    2381              :     {
    2382       234879 :       if (expr->value.function.esym != NULL
    2383        43792 :           && expr->value.function.esym->attr.elemental)
    2384              :         {
    2385              :           arg0 = expr->value.function.actual;
    2386              :           esym = expr->value.function.esym;
    2387              :         }
    2388       218571 :       else if (expr->value.function.isym != NULL
    2389       190033 :                && expr->value.function.isym->elemental)
    2390              :         {
    2391              :           arg0 = expr->value.function.actual;
    2392              :           isym = expr->value.function.isym;
    2393              :         }
    2394              :       else
    2395              :         return true;
    2396              :     }
    2397        89220 :   else if (c && c->ext.actual != NULL)
    2398              :     {
    2399        70753 :       arg0 = c->ext.actual;
    2400              : 
    2401        70753 :       if (c->resolved_sym)
    2402              :         esym = c->resolved_sym;
    2403              :       else
    2404          313 :         esym = c->symtree->n.sym;
    2405        70753 :       gcc_assert (esym);
    2406              : 
    2407        70753 :       if (!esym->attr.elemental)
    2408              :         return true;
    2409              :     }
    2410              :   else
    2411              :     return true;
    2412              : 
    2413              :   /* The rank of an elemental is the rank of its array argument(s).  */
    2414       173713 :   for (arg = arg0; arg; arg = arg->next)
    2415              :     {
    2416       112624 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2417              :         {
    2418        10692 :           rank = arg->expr->rank;
    2419        10692 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2420         5484 :               && arg->expr->symtree->n.sym->attr.optional)
    2421        10692 :             set_by_optional = true;
    2422              : 
    2423              :           /* Function specific; set the result rank and shape.  */
    2424        10692 :           if (expr)
    2425              :             {
    2426         8290 :               expr->rank = rank;
    2427         8290 :               expr->corank = arg->expr->corank;
    2428         8290 :               if (!expr->shape && arg->expr->shape)
    2429              :                 {
    2430         3944 :                   expr->shape = gfc_get_shape (rank);
    2431         8683 :                   for (i = 0; i < rank; i++)
    2432         4739 :                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
    2433              :                 }
    2434              :             }
    2435              :           break;
    2436              :         }
    2437              :     }
    2438              : 
    2439              :   /* If it is an array, it shall not be supplied as an actual argument
    2440              :      to an elemental procedure unless an array of the same rank is supplied
    2441              :      as an actual argument corresponding to a nonoptional dummy argument of
    2442              :      that elemental procedure(12.4.1.5).  */
    2443        71781 :   formal_optional = false;
    2444        71781 :   if (isym)
    2445        49266 :     iformal = isym->formal;
    2446              :   else
    2447        22515 :     eformal = esym->formal;
    2448              : 
    2449       189921 :   for (arg = arg0; arg; arg = arg->next)
    2450              :     {
    2451       118140 :       if (eformal)
    2452              :         {
    2453        40405 :           if (eformal->sym && eformal->sym->attr.optional)
    2454        40405 :             formal_optional = true;
    2455        40405 :           eformal = eformal->next;
    2456              :         }
    2457        77735 :       else if (isym && iformal)
    2458              :         {
    2459        67503 :           if (iformal->optional)
    2460        13412 :             formal_optional = true;
    2461        67503 :           iformal = iformal->next;
    2462              :         }
    2463        10232 :       else if (isym)
    2464        10224 :         formal_optional = true;
    2465              : 
    2466       118140 :       if (pedantic && arg->expr != NULL
    2467        68935 :           && arg->expr->expr_type == EXPR_VARIABLE
    2468        32586 :           && arg->expr->symtree->n.sym->attr.optional
    2469          572 :           && formal_optional
    2470          479 :           && arg->expr->rank
    2471          153 :           && (set_by_optional || arg->expr->rank != rank)
    2472           42 :           && !(isym && isym->id == GFC_ISYM_CONVERSION))
    2473              :         {
    2474          114 :           bool t = false;
    2475              :           gfc_actual_arglist *a;
    2476              : 
    2477              :           /* Scan the argument list for a non-optional argument with the
    2478              :              same rank as arg.  */
    2479          114 :           for (a = arg0; a; a = a->next)
    2480           87 :             if (a != arg
    2481           45 :                 && a->expr->rank == arg->expr->rank
    2482           39 :                 && (a->expr->expr_type != EXPR_VARIABLE
    2483           37 :                     || (a->expr->expr_type == EXPR_VARIABLE
    2484           37 :                         && !a->expr->symtree->n.sym->attr.optional)))
    2485              :               {
    2486              :                 t = true;
    2487              :                 break;
    2488              :               }
    2489              : 
    2490           42 :           if (!t)
    2491           27 :             gfc_warning (OPT_Wpedantic,
    2492              :                          "%qs at %L is an array and OPTIONAL; If it is not "
    2493              :                          "present, then it cannot be the actual argument of "
    2494              :                          "an ELEMENTAL procedure unless there is a non-optional"
    2495              :                          " argument with the same rank "
    2496              :                          "(Fortran 2018, 15.5.2.12)",
    2497              :                          arg->expr->symtree->n.sym->name, &arg->expr->where);
    2498              :         }
    2499              :     }
    2500              : 
    2501       189910 :   for (arg = arg0; arg; arg = arg->next)
    2502              :     {
    2503       118138 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2504       104558 :         continue;
    2505              : 
    2506              :       /* Being elemental, the last upper bound of an assumed size array
    2507              :          argument must be present.  */
    2508        13580 :       if (resolve_assumed_size_actual (arg->expr))
    2509              :         return false;
    2510              : 
    2511              :       /* Elemental procedure's array actual arguments must conform.  */
    2512        13577 :       if (e != NULL)
    2513              :         {
    2514         2888 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2515              :             return false;
    2516              :         }
    2517              :       else
    2518        10689 :         e = arg->expr;
    2519              :     }
    2520              : 
    2521              :   /* INTENT(OUT) is only allowed for subroutines; if any actual argument
    2522              :      is an array, the intent inout/out variable needs to be also an array.  */
    2523        71772 :   if (rank > 0 && esym && expr == NULL)
    2524         7321 :     for (eformal = esym->formal, arg = arg0; arg && eformal;
    2525         4925 :          arg = arg->next, eformal = eformal->next)
    2526         4927 :       if (eformal->sym
    2527         4926 :           && (eformal->sym->attr.intent == INTENT_OUT
    2528         3844 :               || eformal->sym->attr.intent == INTENT_INOUT)
    2529         1710 :           && arg->expr && arg->expr->rank == 0)
    2530              :         {
    2531            2 :           gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
    2532              :                      "ELEMENTAL subroutine %qs is a scalar, but another "
    2533              :                      "actual argument is an array", &arg->expr->where,
    2534              :                      (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
    2535              :                      : "INOUT", eformal->sym->name, esym->name);
    2536            2 :           return false;
    2537              :         }
    2538              :   return true;
    2539              : }
    2540              : 
    2541              : 
    2542              : /* This function does the checking of references to global procedures
    2543              :    as defined in sections 18.1 and 14.1, respectively, of the Fortran
    2544              :    77 and 95 standards.  It checks for a gsymbol for the name, making
    2545              :    one if it does not already exist.  If it already exists, then the
    2546              :    reference being resolved must correspond to the type of gsymbol.
    2547              :    Otherwise, the new symbol is equipped with the attributes of the
    2548              :    reference.  The corresponding code that is called in creating
    2549              :    global entities is parse.cc.
    2550              : 
    2551              :    In addition, for all but -std=legacy, the gsymbols are used to
    2552              :    check the interfaces of external procedures from the same file.
    2553              :    The namespace of the gsymbol is resolved and then, once this is
    2554              :    done the interface is checked.  */
    2555              : 
    2556              : 
    2557              : static bool
    2558        14930 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2559              : {
    2560        14930 :   if (!gsym_ns->proc_name->attr.recursive)
    2561              :     return true;
    2562              : 
    2563          151 :   if (sym->ns == gsym_ns)
    2564              :     return false;
    2565              : 
    2566          151 :   if (sym->ns->parent && sym->ns->parent == gsym_ns)
    2567            0 :     return false;
    2568              : 
    2569              :   return true;
    2570              : }
    2571              : 
    2572              : static bool
    2573        14930 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2574              : {
    2575        14930 :   if (gsym_ns->entries)
    2576              :     {
    2577              :       gfc_entry_list *entry = gsym_ns->entries;
    2578              : 
    2579         3312 :       for (; entry; entry = entry->next)
    2580              :         {
    2581         2333 :           if (strcmp (sym->name, entry->sym->name) == 0)
    2582              :             {
    2583          971 :               if (strcmp (gsym_ns->proc_name->name,
    2584          971 :                           sym->ns->proc_name->name) == 0)
    2585              :                 return false;
    2586              : 
    2587          971 :               if (sym->ns->parent
    2588            0 :                   && strcmp (gsym_ns->proc_name->name,
    2589            0 :                              sym->ns->parent->proc_name->name) == 0)
    2590              :                 return false;
    2591              :             }
    2592              :         }
    2593              :     }
    2594              :   return true;
    2595              : }
    2596              : 
    2597              : 
    2598              : /* Check for the requirement of an explicit interface. F08:12.4.2.2.  */
    2599              : 
    2600              : bool
    2601        15718 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2602              : {
    2603        15718 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2604              : 
    2605        58818 :   for ( ; arg; arg = arg->next)
    2606              :     {
    2607        27766 :       if (!arg->sym)
    2608          157 :         continue;
    2609              : 
    2610        27609 :       if (arg->sym->attr.allocatable)  /* (2a)  */
    2611              :         {
    2612            0 :           strncpy (errmsg, _("allocatable argument"), err_len);
    2613            0 :           return true;
    2614              :         }
    2615        27609 :       else if (arg->sym->attr.asynchronous)
    2616              :         {
    2617            0 :           strncpy (errmsg, _("asynchronous argument"), err_len);
    2618            0 :           return true;
    2619              :         }
    2620        27609 :       else if (arg->sym->attr.optional)
    2621              :         {
    2622           75 :           strncpy (errmsg, _("optional argument"), err_len);
    2623           75 :           return true;
    2624              :         }
    2625        27534 :       else if (arg->sym->attr.pointer)
    2626              :         {
    2627           12 :           strncpy (errmsg, _("pointer argument"), err_len);
    2628           12 :           return true;
    2629              :         }
    2630        27522 :       else if (arg->sym->attr.target)
    2631              :         {
    2632           72 :           strncpy (errmsg, _("target argument"), err_len);
    2633           72 :           return true;
    2634              :         }
    2635        27450 :       else if (arg->sym->attr.value)
    2636              :         {
    2637           12 :           strncpy (errmsg, _("value argument"), err_len);
    2638           12 :           return true;
    2639              :         }
    2640        27438 :       else if (arg->sym->attr.volatile_)
    2641              :         {
    2642            1 :           strncpy (errmsg, _("volatile argument"), err_len);
    2643            1 :           return true;
    2644              :         }
    2645        27437 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE)  /* (2b)  */
    2646              :         {
    2647           45 :           strncpy (errmsg, _("assumed-shape argument"), err_len);
    2648           45 :           return true;
    2649              :         }
    2650        27392 :       else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK)  /* TS 29113, 6.2.  */
    2651              :         {
    2652            1 :           strncpy (errmsg, _("assumed-rank argument"), err_len);
    2653            1 :           return true;
    2654              :         }
    2655        27391 :       else if (arg->sym->attr.codimension)  /* (2c)  */
    2656              :         {
    2657            1 :           strncpy (errmsg, _("coarray argument"), err_len);
    2658            1 :           return true;
    2659              :         }
    2660        27390 :       else if (false)  /* (2d) TODO: parametrized derived type  */
    2661              :         {
    2662              :           strncpy (errmsg, _("parametrized derived type argument"), err_len);
    2663              :           return true;
    2664              :         }
    2665        27390 :       else if (arg->sym->ts.type == BT_CLASS)  /* (2e)  */
    2666              :         {
    2667          164 :           strncpy (errmsg, _("polymorphic argument"), err_len);
    2668          164 :           return true;
    2669              :         }
    2670        27226 :       else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    2671              :         {
    2672            0 :           strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
    2673            0 :           return true;
    2674              :         }
    2675        27226 :       else if (arg->sym->ts.type == BT_ASSUMED)
    2676              :         {
    2677              :           /* As assumed-type is unlimited polymorphic (cf. above).
    2678              :              See also TS 29113, Note 6.1.  */
    2679            1 :           strncpy (errmsg, _("assumed-type argument"), err_len);
    2680            1 :           return true;
    2681              :         }
    2682              :     }
    2683              : 
    2684        15334 :   if (sym->attr.function)
    2685              :     {
    2686         3457 :       gfc_symbol *res = sym->result ? sym->result : sym;
    2687              : 
    2688         3457 :       if (res->attr.dimension)  /* (3a)  */
    2689              :         {
    2690           93 :           strncpy (errmsg, _("array result"), err_len);
    2691           93 :           return true;
    2692              :         }
    2693         3364 :       else if (res->attr.pointer || res->attr.allocatable)  /* (3b)  */
    2694              :         {
    2695           38 :           strncpy (errmsg, _("pointer or allocatable result"), err_len);
    2696           38 :           return true;
    2697              :         }
    2698         3326 :       else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
    2699          347 :                && res->ts.u.cl->length
    2700          166 :                && res->ts.u.cl->length->expr_type != EXPR_CONSTANT)  /* (3c)  */
    2701              :         {
    2702           12 :           strncpy (errmsg, _("result with non-constant character length"), err_len);
    2703           12 :           return true;
    2704              :         }
    2705              :     }
    2706              : 
    2707        15191 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2708              :     {
    2709            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2710            7 :       return true;
    2711              :     }
    2712        15184 :   else if (sym->attr.is_bind_c)  /* (5)  */
    2713              :     {
    2714            0 :       strncpy (errmsg, _("bind(c) procedure"), err_len);
    2715            0 :       return true;
    2716              :     }
    2717              : 
    2718              :   return false;
    2719              : }
    2720              : 
    2721              : 
    2722              : static void
    2723        29412 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2724              : {
    2725        29412 :   gfc_gsymbol * gsym;
    2726        29412 :   gfc_namespace *ns;
    2727        29412 :   enum gfc_symbol_type type;
    2728        29412 :   char reason[200];
    2729              : 
    2730        29412 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2731              : 
    2732        29412 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2733        29412 :                           sym->binding_label != NULL);
    2734              : 
    2735        29412 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2736           10 :     gfc_global_used (gsym, where);
    2737              : 
    2738        29412 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2739         9274 :        || sym->attr.if_source == IFSRC_IFBODY)
    2740        25023 :       && gsym->type != GSYM_UNKNOWN
    2741        22857 :       && !gsym->binding_label
    2742        20558 :       && gsym->ns
    2743        14930 :       && gsym->ns->proc_name
    2744        14930 :       && not_in_recursive (sym, gsym->ns)
    2745        44342 :       && not_entry_self_reference (sym, gsym->ns))
    2746              :     {
    2747        14930 :       gfc_symbol *def_sym;
    2748        14930 :       def_sym = gsym->ns->proc_name;
    2749              : 
    2750        14930 :       if (gsym->ns->resolved != -1)
    2751              :         {
    2752              : 
    2753              :           /* Resolve the gsymbol namespace if needed.  */
    2754        14908 :           if (!gsym->ns->resolved)
    2755              :             {
    2756         2775 :               gfc_symbol *old_dt_list;
    2757              : 
    2758              :               /* Stash away derived types so that the backend_decls
    2759              :                  do not get mixed up.  */
    2760         2775 :               old_dt_list = gfc_derived_types;
    2761         2775 :               gfc_derived_types = NULL;
    2762              : 
    2763         2775 :               gfc_resolve (gsym->ns);
    2764              : 
    2765              :               /* Store the new derived types with the global namespace.  */
    2766         2775 :               if (gfc_derived_types)
    2767          306 :                 gsym->ns->derived_types = gfc_derived_types;
    2768              : 
    2769              :               /* Restore the derived types of this namespace.  */
    2770         2775 :               gfc_derived_types = old_dt_list;
    2771              :             }
    2772              : 
    2773              :           /* Make sure that translation for the gsymbol occurs before
    2774              :              the procedure currently being resolved.  */
    2775        14908 :           ns = gfc_global_ns_list;
    2776        25291 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2777              :             {
    2778        16901 :               if (ns->sibling == gsym->ns)
    2779              :                 {
    2780         6518 :                   ns->sibling = gsym->ns->sibling;
    2781         6518 :                   gsym->ns->sibling = gfc_global_ns_list;
    2782         6518 :                   gfc_global_ns_list = gsym->ns;
    2783         6518 :                   break;
    2784              :                 }
    2785              :             }
    2786              : 
    2787              :           /* This can happen if a binding name has been specified.  */
    2788        14908 :           if (gsym->binding_label && gsym->sym_name != def_sym->name)
    2789            0 :             gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
    2790              :         }
    2791              : 
    2792              :       /* Look up the specific entry symbol so that interface checks use
    2793              :          the entry's own formal argument list, not the entry master's.
    2794              :          This must run even when resolved == -1 (recursive resolution in
    2795              :          progress), because def_sym starts as the namespace proc_name
    2796              :          which is the entry master with the combined formals.  */
    2797        14930 :       if (def_sym->attr.entry_master || def_sym->attr.entry)
    2798              :         {
    2799          979 :           gfc_entry_list *entry;
    2800         1699 :           for (entry = gsym->ns->entries; entry; entry = entry->next)
    2801         1699 :             if (strcmp (entry->sym->name, sym->name) == 0)
    2802              :               {
    2803          979 :                 def_sym = entry->sym;
    2804          979 :                 break;
    2805              :               }
    2806              :         }
    2807              : 
    2808        14930 :       if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
    2809              :         {
    2810            6 :           gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
    2811              :                      sym->name, &sym->declared_at, gfc_typename (&sym->ts),
    2812            6 :                      gfc_typename (&def_sym->ts));
    2813           28 :           goto done;
    2814              :         }
    2815              : 
    2816        14924 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2817        14924 :           && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
    2818              :         {
    2819            8 :           gfc_error ("Explicit interface required for %qs at %L: %s",
    2820              :                      sym->name, &sym->declared_at, reason);
    2821            8 :           goto done;
    2822              :         }
    2823              : 
    2824        14916 :       bool bad_result_characteristics;
    2825        14916 :       if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
    2826              :                                    reason, sizeof(reason), NULL, NULL,
    2827              :                                    &bad_result_characteristics))
    2828              :         {
    2829              :           /* Turn erros into warnings with -std=gnu and -std=legacy,
    2830              :              unless a function returns a wrong type, which can lead
    2831              :              to all kinds of ICEs and wrong code.  */
    2832              : 
    2833           14 :           if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
    2834            2 :               && !bad_result_characteristics)
    2835            2 :             gfc_errors_to_warnings (true);
    2836              : 
    2837           14 :           gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
    2838              :                      sym->name, &sym->declared_at, reason);
    2839           14 :           sym->error = 1;
    2840           14 :           gfc_errors_to_warnings (false);
    2841           14 :           goto done;
    2842              :         }
    2843              :     }
    2844              : 
    2845        29412 : done:
    2846              : 
    2847        29412 :   if (gsym->type == GSYM_UNKNOWN)
    2848              :     {
    2849         3956 :       gsym->type = type;
    2850         3956 :       gsym->where = *where;
    2851              :     }
    2852              : 
    2853        29412 :   gsym->used = 1;
    2854        29412 : }
    2855              : 
    2856              : 
    2857              : /************* Function resolution *************/
    2858              : 
    2859              : /* Resolve a function call known to be generic.
    2860              :    Section 14.1.2.4.1.  */
    2861              : 
    2862              : static match
    2863        27447 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2864              : {
    2865        27447 :   gfc_symbol *s;
    2866              : 
    2867        27447 :   if (sym->attr.generic)
    2868              :     {
    2869        26342 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2870        26342 :       if (s != NULL)
    2871              :         {
    2872        19774 :           expr->value.function.name = s->name;
    2873        19774 :           expr->value.function.esym = s;
    2874              : 
    2875        19774 :           if (s->ts.type != BT_UNKNOWN)
    2876        19757 :             expr->ts = s->ts;
    2877           17 :           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
    2878           15 :             expr->ts = s->result->ts;
    2879              : 
    2880        19774 :           if (s->as != NULL)
    2881              :             {
    2882           55 :               expr->rank = s->as->rank;
    2883           55 :               expr->corank = s->as->corank;
    2884              :             }
    2885        19719 :           else if (s->result != NULL && s->result->as != NULL)
    2886              :             {
    2887            0 :               expr->rank = s->result->as->rank;
    2888            0 :               expr->corank = s->result->as->corank;
    2889              :             }
    2890              : 
    2891        19774 :           gfc_set_sym_referenced (expr->value.function.esym);
    2892              : 
    2893        19774 :           return MATCH_YES;
    2894              :         }
    2895              : 
    2896              :       /* TODO: Need to search for elemental references in generic
    2897              :          interface.  */
    2898              :     }
    2899              : 
    2900         7673 :   if (sym->attr.intrinsic)
    2901         1062 :     return gfc_intrinsic_func_interface (expr, 0);
    2902              : 
    2903              :   return MATCH_NO;
    2904              : }
    2905              : 
    2906              : 
    2907              : static bool
    2908        27306 : resolve_generic_f (gfc_expr *expr)
    2909              : {
    2910        27306 :   gfc_symbol *sym;
    2911        27306 :   match m;
    2912        27306 :   gfc_interface *intr = NULL;
    2913              : 
    2914        27306 :   sym = expr->symtree->n.sym;
    2915              : 
    2916        27447 :   for (;;)
    2917              :     {
    2918        27447 :       m = resolve_generic_f0 (expr, sym);
    2919        27447 :       if (m == MATCH_YES)
    2920              :         return true;
    2921         6613 :       else if (m == MATCH_ERROR)
    2922              :         return false;
    2923              : 
    2924         6613 : generic:
    2925         6616 :       if (!intr)
    2926         6587 :         for (intr = sym->generic; intr; intr = intr->next)
    2927         6503 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2928              :             break;
    2929              : 
    2930         6616 :       if (sym->ns->parent == NULL)
    2931              :         break;
    2932          283 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2933              : 
    2934          283 :       if (sym == NULL)
    2935              :         break;
    2936          144 :       if (!generic_sym (sym))
    2937            3 :         goto generic;
    2938              :     }
    2939              : 
    2940              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    2941              :      that possesses a matching interface.  14.1.2.4  */
    2942         6472 :   if (sym  && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
    2943              :     {
    2944            5 :       if (gfc_init_expr_flag)
    2945            1 :         gfc_error ("Function %qs in initialization expression at %L "
    2946              :                    "must be an intrinsic function",
    2947            1 :                    expr->symtree->n.sym->name, &expr->where);
    2948              :       else
    2949            4 :         gfc_error ("There is no specific function for the generic %qs "
    2950            4 :                    "at %L", expr->symtree->n.sym->name, &expr->where);
    2951            5 :       return false;
    2952              :     }
    2953              : 
    2954         6467 :   if (intr)
    2955              :     {
    2956         6432 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2957              :                                                  NULL, false))
    2958              :         return false;
    2959         6405 :       if (!gfc_use_derived (expr->ts.u.derived))
    2960              :         return false;
    2961         6405 :       return resolve_structure_cons (expr, 0);
    2962              :     }
    2963              : 
    2964           35 :   m = gfc_intrinsic_func_interface (expr, 0);
    2965           35 :   if (m == MATCH_YES)
    2966              :     return true;
    2967              : 
    2968            3 :   if (m == MATCH_NO)
    2969            3 :     gfc_error ("Generic function %qs at %L is not consistent with a "
    2970            3 :                "specific intrinsic interface", expr->symtree->n.sym->name,
    2971              :                &expr->where);
    2972              : 
    2973              :   return false;
    2974              : }
    2975              : 
    2976              : 
    2977              : /* Resolve a function call known to be specific.  */
    2978              : 
    2979              : static match
    2980        27986 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2981              : {
    2982        27986 :   match m;
    2983              : 
    2984        27986 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2985              :     {
    2986         8063 :       if (sym->attr.dummy)
    2987              :         {
    2988          276 :           sym->attr.proc = PROC_DUMMY;
    2989          276 :           goto found;
    2990              :         }
    2991              : 
    2992         7787 :       sym->attr.proc = PROC_EXTERNAL;
    2993         7787 :       goto found;
    2994              :     }
    2995              : 
    2996        19923 :   if (sym->attr.proc == PROC_MODULE
    2997              :       || sym->attr.proc == PROC_ST_FUNCTION
    2998              :       || sym->attr.proc == PROC_INTERNAL)
    2999        19185 :     goto found;
    3000              : 
    3001          738 :   if (sym->attr.intrinsic)
    3002              :     {
    3003          731 :       m = gfc_intrinsic_func_interface (expr, 1);
    3004          731 :       if (m == MATCH_YES)
    3005              :         return MATCH_YES;
    3006            0 :       if (m == MATCH_NO)
    3007            0 :         gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
    3008              :                    "with an intrinsic", sym->name, &expr->where);
    3009              : 
    3010            0 :       return MATCH_ERROR;
    3011              :     }
    3012              : 
    3013              :   return MATCH_NO;
    3014              : 
    3015        27248 : found:
    3016        27248 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3017              : 
    3018        27248 :   if (sym->result)
    3019        27248 :     expr->ts = sym->result->ts;
    3020              :   else
    3021            0 :     expr->ts = sym->ts;
    3022        27248 :   expr->value.function.name = sym->name;
    3023        27248 :   expr->value.function.esym = sym;
    3024              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3025              :      error(s).  */
    3026        27248 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3027              :     return MATCH_ERROR;
    3028        27247 :   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
    3029              :     {
    3030          322 :       expr->rank = CLASS_DATA (sym)->as->rank;
    3031          322 :       expr->corank = CLASS_DATA (sym)->as->corank;
    3032              :     }
    3033        26925 :   else if (sym->as != NULL)
    3034              :     {
    3035         2323 :       expr->rank = sym->as->rank;
    3036         2323 :       expr->corank = sym->as->corank;
    3037              :     }
    3038              : 
    3039              :   return MATCH_YES;
    3040              : }
    3041              : 
    3042              : 
    3043              : static bool
    3044        27979 : resolve_specific_f (gfc_expr *expr)
    3045              : {
    3046        27979 :   gfc_symbol *sym;
    3047        27979 :   match m;
    3048              : 
    3049        27979 :   sym = expr->symtree->n.sym;
    3050              : 
    3051        27986 :   for (;;)
    3052              :     {
    3053        27986 :       m = resolve_specific_f0 (sym, expr);
    3054        27986 :       if (m == MATCH_YES)
    3055              :         return true;
    3056            8 :       if (m == MATCH_ERROR)
    3057              :         return false;
    3058              : 
    3059            7 :       if (sym->ns->parent == NULL)
    3060              :         break;
    3061              : 
    3062            7 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3063              : 
    3064            7 :       if (sym == NULL)
    3065              :         break;
    3066              :     }
    3067              : 
    3068            0 :   gfc_error ("Unable to resolve the specific function %qs at %L",
    3069            0 :              expr->symtree->n.sym->name, &expr->where);
    3070              : 
    3071            0 :   return true;
    3072              : }
    3073              : 
    3074              : /* Recursively append candidate SYM to CANDIDATES.  Store the number of
    3075              :    candidates in CANDIDATES_LEN.  */
    3076              : 
    3077              : static void
    3078          212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
    3079              :                                        char **&candidates,
    3080              :                                        size_t &candidates_len)
    3081              : {
    3082          388 :   gfc_symtree *p;
    3083              : 
    3084          388 :   if (sym == NULL)
    3085              :     return;
    3086          388 :   if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
    3087          126 :       && sym->n.sym->attr.flavor == FL_PROCEDURE)
    3088           51 :     vec_push (candidates, candidates_len, sym->name);
    3089              : 
    3090          388 :   p = sym->left;
    3091          388 :   if (p)
    3092          155 :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3093              : 
    3094          388 :   p = sym->right;
    3095          388 :   if (p)
    3096              :     lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
    3097              : }
    3098              : 
    3099              : 
    3100              : /* Lookup function FN fuzzily, taking names in SYMROOT into account.  */
    3101              : 
    3102              : const char*
    3103           57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
    3104              : {
    3105           57 :   char **candidates = NULL;
    3106           57 :   size_t candidates_len = 0;
    3107           57 :   lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
    3108           57 :   return gfc_closest_fuzzy_match (fn, candidates);
    3109              : }
    3110              : 
    3111              : 
    3112              : /* Resolve a procedure call not known to be generic nor specific.  */
    3113              : 
    3114              : static bool
    3115       275270 : resolve_unknown_f (gfc_expr *expr)
    3116              : {
    3117       275270 :   gfc_symbol *sym;
    3118       275270 :   gfc_typespec *ts;
    3119              : 
    3120       275270 :   sym = expr->symtree->n.sym;
    3121              : 
    3122       275270 :   if (sym->attr.dummy)
    3123              :     {
    3124          289 :       sym->attr.proc = PROC_DUMMY;
    3125          289 :       expr->value.function.name = sym->name;
    3126          289 :       goto set_type;
    3127              :     }
    3128              : 
    3129              :   /* See if we have an intrinsic function reference.  */
    3130              : 
    3131       274981 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3132              :     {
    3133       272724 :       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
    3134              :         return true;
    3135              :       return false;
    3136              :     }
    3137              : 
    3138              :   /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr.  */
    3139              :   /* Intrinsics were handled above, only non-intrinsics left here.  */
    3140         2257 :   if (sym->attr.flavor == FL_PROCEDURE
    3141         2254 :       && sym->attr.implicit_type
    3142          371 :       && sym->ns
    3143          371 :       && sym->ns->has_implicit_none_export)
    3144              :     {
    3145            3 :           gfc_error ("Missing explicit declaration with EXTERNAL attribute "
    3146              :               "for symbol %qs at %L", sym->name, &sym->declared_at);
    3147            3 :           sym->error = 1;
    3148            3 :           return false;
    3149              :     }
    3150              : 
    3151              :   /* The reference is to an external name.  */
    3152              : 
    3153         2254 :   sym->attr.proc = PROC_EXTERNAL;
    3154         2254 :   expr->value.function.name = sym->name;
    3155         2254 :   expr->value.function.esym = expr->symtree->n.sym;
    3156              : 
    3157         2254 :   if (sym->as != NULL)
    3158              :     {
    3159            1 :       expr->rank = sym->as->rank;
    3160            1 :       expr->corank = sym->as->corank;
    3161              :     }
    3162              : 
    3163              :   /* Type of the expression is either the type of the symbol or the
    3164              :      default type of the symbol.  */
    3165              : 
    3166         2253 : set_type:
    3167         2543 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3168              : 
    3169         2543 :   if (sym->ts.type != BT_UNKNOWN)
    3170         2492 :     expr->ts = sym->ts;
    3171              :   else
    3172              :     {
    3173           51 :       ts = gfc_get_default_type (sym->name, sym->ns);
    3174              : 
    3175           51 :       if (ts->type == BT_UNKNOWN)
    3176              :         {
    3177           41 :           const char *guessed
    3178           41 :             = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
    3179           41 :           if (guessed)
    3180            3 :             gfc_error ("Function %qs at %L has no IMPLICIT type"
    3181              :                        "; did you mean %qs?",
    3182              :                        sym->name, &expr->where, guessed);
    3183              :           else
    3184           38 :             gfc_error ("Function %qs at %L has no IMPLICIT type",
    3185              :                        sym->name, &expr->where);
    3186           41 :           return false;
    3187              :         }
    3188              :       else
    3189           10 :         expr->ts = *ts;
    3190              :     }
    3191              : 
    3192              :   return true;
    3193              : }
    3194              : 
    3195              : 
    3196              : /* Return true, if the symbol is an external procedure.  */
    3197              : static bool
    3198       849036 : is_external_proc (gfc_symbol *sym)
    3199              : {
    3200       847345 :   if (!sym->attr.dummy && !sym->attr.contained
    3201       739363 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3202       161151 :         && sym->attr.proc != PROC_ST_FUNCTION
    3203       160556 :         && !sym->attr.proc_pointer
    3204       159362 :         && !sym->attr.use_assoc
    3205       907891 :         && sym->name)
    3206              :     return true;
    3207              : 
    3208              :   return false;
    3209              : }
    3210              : 
    3211              : 
    3212              : /* Figure out if a function reference is pure or not.  Also set the name
    3213              :    of the function for a potential error message.  Return nonzero if the
    3214              :    function is PURE, zero if not.  */
    3215              : static bool
    3216              : pure_stmt_function (gfc_expr *, gfc_symbol *);
    3217              : 
    3218              : bool
    3219       255111 : gfc_pure_function (gfc_expr *e, const char **name)
    3220              : {
    3221       255111 :   bool pure;
    3222       255111 :   gfc_component *comp;
    3223              : 
    3224       255111 :   *name = NULL;
    3225              : 
    3226       255111 :   if (e->symtree != NULL
    3227       254757 :         && e->symtree->n.sym != NULL
    3228       254757 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3229          305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3230              : 
    3231       254806 :   comp = gfc_get_proc_ptr_comp (e);
    3232       254806 :   if (comp)
    3233              :     {
    3234          465 :       pure = gfc_pure (comp->ts.interface);
    3235          465 :       *name = comp->name;
    3236              :     }
    3237       254341 :   else if (e->value.function.esym)
    3238              :     {
    3239        52557 :       pure = gfc_pure (e->value.function.esym);
    3240        52557 :       *name = e->value.function.esym->name;
    3241              :     }
    3242       201784 :   else if (e->value.function.isym)
    3243              :     {
    3244       401430 :       pure = e->value.function.isym->pure
    3245       200715 :              || e->value.function.isym->elemental;
    3246       200715 :       *name = e->value.function.isym->name;
    3247              :     }
    3248         1069 :   else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
    3249              :     {
    3250              :       /* The function has been resolved, but esym is not yet set.
    3251              :          This can happen with functions as dummy argument.  */
    3252          287 :       pure = e->symtree->n.sym->attr.pure;
    3253          287 :       *name = e->symtree->n.sym->name;
    3254              :     }
    3255              :   else
    3256              :     {
    3257              :       /* Implicit functions are not pure.  */
    3258          782 :       pure = 0;
    3259          782 :       *name = e->value.function.name;
    3260              :     }
    3261              : 
    3262              :   return pure;
    3263              : }
    3264              : 
    3265              : 
    3266              : /* Check if the expression is a reference to an implicitly pure function.  */
    3267              : 
    3268              : bool
    3269        37968 : gfc_implicit_pure_function (gfc_expr *e)
    3270              : {
    3271        37968 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3272        37968 :   if (comp)
    3273          449 :     return gfc_implicit_pure (comp->ts.interface);
    3274        37519 :   else if (e->value.function.esym)
    3275        32116 :     return gfc_implicit_pure (e->value.function.esym);
    3276              :   else
    3277              :     return 0;
    3278              : }
    3279              : 
    3280              : 
    3281              : static bool
    3282          981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
    3283              :                  int *f ATTRIBUTE_UNUSED)
    3284              : {
    3285          981 :   const char *name;
    3286              : 
    3287              :   /* Don't bother recursing into other statement functions
    3288              :      since they will be checked individually for purity.  */
    3289          981 :   if (e->expr_type != EXPR_FUNCTION
    3290          343 :         || !e->symtree
    3291          343 :         || e->symtree->n.sym == sym
    3292           20 :         || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3293              :     return false;
    3294              : 
    3295           19 :   return gfc_pure_function (e, &name) ? false : true;
    3296              : }
    3297              : 
    3298              : 
    3299              : static bool
    3300          305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
    3301              : {
    3302          305 :   return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
    3303              : }
    3304              : 
    3305              : 
    3306              : /* Check if an impure function is allowed in the current context. */
    3307              : 
    3308       243292 : static bool check_pure_function (gfc_expr *e)
    3309              : {
    3310       243292 :   const char *name = NULL;
    3311       243292 :   code_stack *stack;
    3312       243292 :   bool saw_block = false;
    3313              : 
    3314              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3315              :      gfc_do_concurrent_flag = 0 when the check for an impure function
    3316              :      occurs.  Check the stack to see if the source code has a nested
    3317              :      BLOCK construct.  */
    3318              : 
    3319       562806 :   for (stack = cs_base; stack; stack = stack->prev)
    3320              :     {
    3321       319516 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3322              :         {
    3323         7198 :           saw_block = true;
    3324         7198 :           continue;
    3325              :         }
    3326              : 
    3327         5221 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3328              :         {
    3329           10 :           bool is_pure;
    3330       319514 :           is_pure = (e->value.function.isym
    3331            9 :                      && (e->value.function.isym->pure
    3332            1 :                          || e->value.function.isym->elemental))
    3333           11 :                     || (e->value.function.esym
    3334            1 :                         && (e->value.function.esym->attr.pure
    3335            1 :                             || e->value.function.esym->attr.elemental));
    3336            2 :           if (!is_pure)
    3337              :             {
    3338            2 :               gfc_error ("Reference to impure function at %L inside a "
    3339              :                          "DO CONCURRENT", &e->where);
    3340            2 :               return false;
    3341              :             }
    3342              :         }
    3343              :     }
    3344              : 
    3345       243290 :   if (!gfc_pure_function (e, &name) && name)
    3346              :     {
    3347        36699 :       if (forall_flag)
    3348              :         {
    3349            4 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3350              :                      "FORALL %s", name, &e->where,
    3351              :                      forall_flag == 2 ? "mask" : "block");
    3352            4 :           return false;
    3353              :         }
    3354        36695 :       else if (gfc_do_concurrent_flag)
    3355              :         {
    3356            2 :           gfc_error ("Reference to impure function %qs at %L inside a "
    3357              :                      "DO CONCURRENT %s", name, &e->where,
    3358              :                      gfc_do_concurrent_flag == 2 ? "mask" : "block");
    3359            2 :           return false;
    3360              :         }
    3361        36693 :       else if (gfc_pure (NULL))
    3362              :         {
    3363            5 :           gfc_error ("Reference to impure function %qs at %L "
    3364              :                      "within a PURE procedure", name, &e->where);
    3365            5 :           return false;
    3366              :         }
    3367        36688 :       if (!gfc_implicit_pure_function (e))
    3368        30274 :         gfc_unset_implicit_pure (NULL);
    3369              :     }
    3370              :   return true;
    3371              : }
    3372              : 
    3373              : 
    3374              : /* Update current procedure's array_outer_dependency flag, considering
    3375              :    a call to procedure SYM.  */
    3376              : 
    3377              : static void
    3378       132223 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
    3379              : {
    3380              :   /* Check to see if this is a sibling function that has not yet
    3381              :      been resolved.  */
    3382       132223 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3383       249803 :   for (; sibling; sibling = sibling->sibling)
    3384              :     {
    3385       124611 :       if (sibling->proc_name == sym)
    3386              :         {
    3387         7031 :           gfc_resolve (sibling);
    3388         7031 :           break;
    3389              :         }
    3390              :     }
    3391              : 
    3392              :   /* If SYM has references to outer arrays, so has the procedure calling
    3393              :      SYM.  If SYM is a procedure pointer, we can assume the worst.  */
    3394       132223 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3395        67933 :       && gfc_current_ns->proc_name)
    3396        67889 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3397       132223 : }
    3398              : 
    3399              : 
    3400              : /* Resolve a function call, which means resolving the arguments, then figuring
    3401              :    out which entity the name refers to.  */
    3402              : 
    3403              : static bool
    3404       343725 : resolve_function (gfc_expr *expr)
    3405              : {
    3406       343725 :   gfc_actual_arglist *arg;
    3407       343725 :   gfc_symbol *sym;
    3408       343725 :   bool t;
    3409       343725 :   int temp;
    3410       343725 :   procedure_type p = PROC_INTRINSIC;
    3411       343725 :   bool no_formal_args;
    3412              : 
    3413       343725 :   sym = NULL;
    3414       343725 :   if (expr->symtree)
    3415       343371 :     sym = expr->symtree->n.sym;
    3416              : 
    3417              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3418       343725 :   if (gfc_is_proc_ptr_comp (expr))
    3419              :     return true;
    3420              : 
    3421              :   /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
    3422              :      another caf_get.  */
    3423       343327 :   if (sym && sym->attr.intrinsic
    3424         8486 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3425         8486 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3426              :     return true;
    3427              : 
    3428       343327 :   if (expr->ref)
    3429              :     {
    3430            1 :       gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
    3431              :                  &expr->where);
    3432            1 :       return false;
    3433              :     }
    3434              : 
    3435       342972 :   if (sym && sym->attr.intrinsic
    3436       351812 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3437              :     return false;
    3438              : 
    3439       343326 :   if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
    3440              :     {
    3441            4 :       gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
    3442            4 :       return false;
    3443              :     }
    3444              : 
    3445              :   /* If this is a deferred TBP with an abstract interface (which may
    3446              :      of course be referenced), expr->value.function.esym will be set.  */
    3447       342968 :   if (sym && sym->attr.abstract && !expr->value.function.esym)
    3448              :     {
    3449            1 :       gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    3450              :                  sym->name, &expr->where);
    3451            1 :       return false;
    3452              :     }
    3453              : 
    3454              :   /* If this is a deferred TBP with an abstract interface, its result
    3455              :      cannot be an assumed length character (F2003: C418).  */
    3456       342967 :   if (sym && sym->attr.abstract && sym->attr.function
    3457          192 :       && sym->result->ts.u.cl
    3458          158 :       && sym->result->ts.u.cl->length == NULL
    3459            2 :       && !sym->result->ts.deferred)
    3460              :     {
    3461            1 :       gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
    3462              :                  "character length result (F2008: C418)", sym->name,
    3463              :                  &sym->declared_at);
    3464            1 :       return false;
    3465              :     }
    3466              : 
    3467              :   /* Switch off assumed size checking and do this again for certain kinds
    3468              :      of procedure, once the procedure itself is resolved.  */
    3469       343320 :   need_full_assumed_size++;
    3470              : 
    3471       343320 :   if (expr->symtree && expr->symtree->n.sym)
    3472       342966 :     p = expr->symtree->n.sym->attr.proc;
    3473              : 
    3474       343320 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3475         1105 :     inquiry_argument = true;
    3476       342966 :   no_formal_args = sym && is_external_proc (sym)
    3477       357120 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3478              : 
    3479       343320 :   if (!resolve_actual_arglist (expr->value.function.actual,
    3480              :                                p, no_formal_args))
    3481              :     {
    3482           67 :       inquiry_argument = false;
    3483           67 :       return false;
    3484              :     }
    3485              : 
    3486       343253 :   inquiry_argument = false;
    3487              : 
    3488              :   /* Resume assumed_size checking.  */
    3489       343253 :   need_full_assumed_size--;
    3490              : 
    3491              :   /* If the procedure is external, check for usage.  */
    3492       343253 :   if (sym && is_external_proc (sym))
    3493        13780 :     resolve_global_procedure (sym, &expr->where, 0);
    3494              : 
    3495       343253 :   if (sym && sym->ts.type == BT_CHARACTER
    3496         3290 :       && sym->ts.u.cl
    3497         3230 :       && sym->ts.u.cl->length == NULL
    3498          670 :       && !sym->attr.dummy
    3499          663 :       && !sym->ts.deferred
    3500            2 :       && expr->value.function.esym == NULL
    3501            2 :       && !sym->attr.contained)
    3502              :     {
    3503              :       /* Internal procedures are taken care of in resolve_contained_fntype.  */
    3504            1 :       gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
    3505              :                  "be used at %L since it is not a dummy argument",
    3506              :                  sym->name, &expr->where);
    3507            1 :       return false;
    3508              :     }
    3509              : 
    3510              :   /* Add and check formal interface when -fc-prototypes-external is in
    3511              :      force, see comment in resolve_call().  */
    3512              : 
    3513       343252 :   if (warn_external_argument_mismatch && sym && sym->attr.dummy
    3514           18 :       && sym->attr.external)
    3515              :     {
    3516           18 :       if (sym->formal)
    3517              :         {
    3518            6 :           bool conflict;
    3519            6 :           conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
    3520              :                                                  sym->formal, 0, 0, 0, NULL);
    3521            6 :           if (conflict)
    3522              :             {
    3523            6 :               sym->ext_dummy_arglist_mismatch = 1;
    3524            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    3525              :                            "Different argument lists in external dummy "
    3526              :                            "function %s at %L and %L", sym->name,
    3527              :                            &expr->where, &sym->formal_at);
    3528              :             }
    3529              :         }
    3530           12 :       else if (!sym->formal_resolved)
    3531              :         {
    3532            6 :           gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
    3533            6 :           sym->formal_at = expr->where;
    3534              :         }
    3535              :     }
    3536              :   /* See if function is already resolved.  */
    3537              : 
    3538       343252 :   if (expr->value.function.name != NULL
    3539       331353 :       || expr->value.function.isym != NULL)
    3540              :     {
    3541        12697 :       if (expr->ts.type == BT_UNKNOWN)
    3542            3 :         expr->ts = sym->ts;
    3543              :       t = true;
    3544              :     }
    3545              :   else
    3546              :     {
    3547              :       /* Apply the rules of section 14.1.2.  */
    3548              : 
    3549       330555 :       switch (procedure_kind (sym))
    3550              :         {
    3551        27306 :         case PTYPE_GENERIC:
    3552        27306 :           t = resolve_generic_f (expr);
    3553        27306 :           break;
    3554              : 
    3555        27979 :         case PTYPE_SPECIFIC:
    3556        27979 :           t = resolve_specific_f (expr);
    3557        27979 :           break;
    3558              : 
    3559       275270 :         case PTYPE_UNKNOWN:
    3560       275270 :           t = resolve_unknown_f (expr);
    3561       275270 :           break;
    3562              : 
    3563              :         default:
    3564              :           gfc_internal_error ("resolve_function(): bad function type");
    3565              :         }
    3566              :     }
    3567              : 
    3568              :   /* If the expression is still a function (it might have simplified),
    3569              :      then we check to see if we are calling an elemental function.  */
    3570              : 
    3571       343252 :   if (expr->expr_type != EXPR_FUNCTION)
    3572              :     return t;
    3573              : 
    3574              :   /* Walk the argument list looking for invalid BOZ.  */
    3575       737050 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3576       494200 :     if (arg->expr && arg->expr->ts.type == BT_BOZ)
    3577              :       {
    3578            5 :         gfc_error ("A BOZ literal constant at %L cannot appear as an "
    3579              :                    "actual argument in a function reference",
    3580              :                    &arg->expr->where);
    3581            5 :         return false;
    3582              :       }
    3583              : 
    3584       242850 :   temp = need_full_assumed_size;
    3585       242850 :   need_full_assumed_size = 0;
    3586              : 
    3587       242850 :   if (!resolve_elemental_actual (expr, NULL))
    3588              :     return false;
    3589              : 
    3590       242847 :   if (omp_workshare_flag
    3591           32 :       && expr->value.function.esym
    3592       242852 :       && ! gfc_elemental (expr->value.function.esym))
    3593              :     {
    3594            4 :       gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
    3595            4 :                  "in WORKSHARE construct", expr->value.function.esym->name,
    3596              :                  &expr->where);
    3597            4 :       t = false;
    3598              :     }
    3599              : 
    3600              : #define GENERIC_ID expr->value.function.isym->id
    3601       242843 :   else if (expr->value.function.actual != NULL
    3602       234876 :            && expr->value.function.isym != NULL
    3603       190032 :            && GENERIC_ID != GFC_ISYM_LBOUND
    3604              :            && GENERIC_ID != GFC_ISYM_LCOBOUND
    3605              :            && GENERIC_ID != GFC_ISYM_UCOBOUND
    3606              :            && GENERIC_ID != GFC_ISYM_LEN
    3607              :            && GENERIC_ID != GFC_ISYM_LOC
    3608              :            && GENERIC_ID != GFC_ISYM_C_LOC
    3609              :            && GENERIC_ID != GFC_ISYM_PRESENT)
    3610              :     {
    3611              :       /* Array intrinsics must also have the last upper bound of an
    3612              :          assumed size array argument.  UBOUND and SIZE have to be
    3613              :          excluded from the check if the second argument is anything
    3614              :          than a constant.  */
    3615              : 
    3616       535374 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3617              :         {
    3618       371046 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3619        45359 :               && arg == expr->value.function.actual
    3620        16723 :               && arg->next != NULL && arg->next->expr)
    3621              :             {
    3622         8236 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3623              :                 break;
    3624              : 
    3625         8012 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3626              :                 break;
    3627              : 
    3628         8012 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3629         8012 :                         < arg->expr->rank)
    3630              :                 break;
    3631              :             }
    3632              : 
    3633       368643 :           if (arg->expr != NULL
    3634       245953 :               && arg->expr->rank > 0
    3635       487074 :               && resolve_assumed_size_actual (arg->expr))
    3636              :             return false;
    3637              :         }
    3638              :     }
    3639              : #undef GENERIC_ID
    3640              : 
    3641       242844 :   need_full_assumed_size = temp;
    3642              : 
    3643       242844 :   if (!check_pure_function(expr))
    3644           12 :     t = false;
    3645              : 
    3646              :   /* Functions without the RECURSIVE attribution are not allowed to
    3647              :    * call themselves.  */
    3648       242844 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3649              :     {
    3650        51310 :       gfc_symbol *esym;
    3651        51310 :       esym = expr->value.function.esym;
    3652              : 
    3653        51310 :       if (is_illegal_recursion (esym, gfc_current_ns))
    3654              :       {
    3655            5 :         if (esym->attr.entry && esym->ns->entries)
    3656            3 :           gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
    3657              :                      " function %qs is not RECURSIVE",
    3658            3 :                      esym->name, &expr->where, esym->ns->entries->sym->name);
    3659              :         else
    3660            2 :           gfc_error ("Function %qs at %L cannot be called recursively, as it"
    3661              :                      " is not RECURSIVE", esym->name, &expr->where);
    3662              : 
    3663              :         t = false;
    3664              :       }
    3665              :     }
    3666              : 
    3667              :   /* Character lengths of use associated functions may contains references to
    3668              :      symbols not referenced from the current program unit otherwise.  Make sure
    3669              :      those symbols are marked as referenced.  */
    3670              : 
    3671       242844 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3672         3428 :       && expr->value.function.esym->attr.use_assoc)
    3673              :     {
    3674         1238 :       gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
    3675              :     }
    3676              : 
    3677              :   /* Make sure that the expression has a typespec that works.  */
    3678       242844 :   if (expr->ts.type == BT_UNKNOWN)
    3679              :     {
    3680          921 :       if (expr->symtree->n.sym->result
    3681          912 :             && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
    3682          560 :             && !expr->symtree->n.sym->result->attr.proc_pointer)
    3683          560 :         expr->ts = expr->symtree->n.sym->result->ts;
    3684              :     }
    3685              : 
    3686              :   /* These derived types with an incomplete namespace, arising from use
    3687              :      association, cause gfc_get_derived_vtab to segfault. If the function
    3688              :      namespace does not suffice, something is badly wrong.  */
    3689       242844 :   if (expr->ts.type == BT_DERIVED
    3690         9308 :       && !expr->ts.u.derived->ns->proc_name)
    3691              :     {
    3692            3 :       gfc_symbol *der;
    3693            3 :       gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
    3694            3 :       if (der)
    3695              :         {
    3696            3 :           expr->ts.u.derived->refs--;
    3697            3 :           expr->ts.u.derived = der;
    3698            3 :           der->refs++;
    3699              :         }
    3700              :       else
    3701            0 :         expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
    3702              :     }
    3703              : 
    3704       242844 :   if (!expr->ref && !expr->value.function.isym)
    3705              :     {
    3706        52691 :       if (expr->value.function.esym)
    3707        51622 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3708              :       else
    3709         1069 :         update_current_proc_array_outer_dependency (sym);
    3710              :     }
    3711       190153 :   else if (expr->ref)
    3712              :     /* typebound procedure: Assume the worst.  */
    3713            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3714              : 
    3715       242844 :   if (expr->value.function.esym
    3716        51622 :       && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    3717           26 :     gfc_warning (OPT_Wdeprecated_declarations,
    3718              :                  "Using function %qs at %L is deprecated",
    3719              :                  sym->name, &expr->where);
    3720              : 
    3721              :   /* Check an external function supplied as a dummy argument has an external
    3722              :      attribute when a program unit uses 'implicit none (external)'.  */
    3723       242844 :   if (expr->expr_type == EXPR_FUNCTION
    3724       242844 :       && expr->symtree
    3725       242490 :       && expr->symtree->n.sym->attr.dummy
    3726          564 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3727       242845 :       && !gfc_is_intrinsic(expr->symtree->n.sym, 0, expr->where))
    3728              :     {
    3729            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    3730              :                  sym->name, &expr->where);
    3731            1 :       return false;
    3732              :     }
    3733              : 
    3734              :   return t;
    3735              : }
    3736              : 
    3737              : 
    3738              : /************* Subroutine resolution *************/
    3739              : 
    3740              : static bool
    3741        77050 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3742              : {
    3743        77050 :   code_stack *stack;
    3744        77050 :   bool saw_block = false;
    3745              : 
    3746        77050 :   if (gfc_pure (sym))
    3747              :     return true;
    3748              : 
    3749              :   /* A BLOCK construct within a DO CONCURRENT construct leads to
    3750              :      gfc_do_concurrent_flag = 0 when the check for an impure subroutine
    3751              :      occurs.  Walk up the stack to see if the source code has a nested
    3752              :      construct.  */
    3753              : 
    3754       158892 :   for (stack = cs_base; stack; stack = stack->prev)
    3755              :     {
    3756        87487 :       if (stack->current->op == EXEC_BLOCK)
    3757              :         {
    3758         1896 :           saw_block = true;
    3759         1896 :           continue;
    3760              :         }
    3761              : 
    3762        85591 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3763              :         {
    3764              : 
    3765            2 :           bool is_pure = true;
    3766        87487 :           is_pure = sym->attr.pure || sym->attr.elemental;
    3767              : 
    3768            2 :           if (!is_pure)
    3769              :             {
    3770            2 :               gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
    3771              :                          "is not PURE", loc);
    3772            2 :               return false;
    3773              :             }
    3774              :         }
    3775              :     }
    3776              : 
    3777        71405 :   if (forall_flag)
    3778              :     {
    3779            0 :       gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
    3780              :                  name, loc);
    3781            0 :       return false;
    3782              :     }
    3783        71405 :   else if (gfc_do_concurrent_flag)
    3784              :     {
    3785            6 :       gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
    3786              :                  "PURE", name, loc);
    3787            6 :       return false;
    3788              :     }
    3789        71399 :   else if (gfc_pure (NULL))
    3790              :     {
    3791            4 :       gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
    3792            4 :       return false;
    3793              :     }
    3794              : 
    3795        71395 :   gfc_unset_implicit_pure (NULL);
    3796        71395 :   return true;
    3797              : }
    3798              : 
    3799              : 
    3800              : static match
    3801         2813 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
    3802              : {
    3803         2813 :   gfc_symbol *s;
    3804              : 
    3805         2813 :   if (sym->attr.generic)
    3806              :     {
    3807         2812 :       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
    3808         2812 :       if (s != NULL)
    3809              :         {
    3810         2803 :           c->resolved_sym = s;
    3811         2803 :           if (!pure_subroutine (s, s->name, &c->loc))
    3812              :             return MATCH_ERROR;
    3813         2803 :           return MATCH_YES;
    3814              :         }
    3815              : 
    3816              :       /* TODO: Need to search for elemental references in generic interface.  */
    3817              :     }
    3818              : 
    3819           10 :   if (sym->attr.intrinsic)
    3820            1 :     return gfc_intrinsic_sub_interface (c, 0);
    3821              : 
    3822              :   return MATCH_NO;
    3823              : }
    3824              : 
    3825              : 
    3826              : static bool
    3827         2811 : resolve_generic_s (gfc_code *c)
    3828              : {
    3829         2811 :   gfc_symbol *sym;
    3830         2811 :   match m;
    3831              : 
    3832         2811 :   sym = c->symtree->n.sym;
    3833              : 
    3834         2813 :   for (;;)
    3835              :     {
    3836         2813 :       m = resolve_generic_s0 (c, sym);
    3837         2813 :       if (m == MATCH_YES)
    3838              :         return true;
    3839            9 :       else if (m == MATCH_ERROR)
    3840              :         return false;
    3841              : 
    3842            9 : generic:
    3843            9 :       if (sym->ns->parent == NULL)
    3844              :         break;
    3845            3 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3846              : 
    3847            3 :       if (sym == NULL)
    3848              :         break;
    3849            2 :       if (!generic_sym (sym))
    3850            0 :         goto generic;
    3851              :     }
    3852              : 
    3853              :   /* Last ditch attempt.  See if the reference is to an intrinsic
    3854              :      that possesses a matching interface.  14.1.2.4  */
    3855            7 :   sym = c->symtree->n.sym;
    3856              : 
    3857            7 :   if (!gfc_is_intrinsic (sym, 1, c->loc))
    3858              :     {
    3859            4 :       gfc_error ("There is no specific subroutine for the generic %qs at %L",
    3860              :                  sym->name, &c->loc);
    3861            4 :       return false;
    3862              :     }
    3863              : 
    3864            3 :   m = gfc_intrinsic_sub_interface (c, 0);
    3865            3 :   if (m == MATCH_YES)
    3866              :     return true;
    3867            1 :   if (m == MATCH_NO)
    3868            1 :     gfc_error ("Generic subroutine %qs at %L is not consistent with an "
    3869              :                "intrinsic subroutine interface", sym->name, &c->loc);
    3870              : 
    3871              :   return false;
    3872              : }
    3873              : 
    3874              : 
    3875              : /* Resolve a subroutine call known to be specific.  */
    3876              : 
    3877              : static match
    3878        62566 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3879              : {
    3880        62566 :   match m;
    3881              : 
    3882        62566 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    3883              :     {
    3884         5661 :       if (sym->attr.dummy)
    3885              :         {
    3886          257 :           sym->attr.proc = PROC_DUMMY;
    3887          257 :           goto found;
    3888              :         }
    3889              : 
    3890         5404 :       sym->attr.proc = PROC_EXTERNAL;
    3891         5404 :       goto found;
    3892              :     }
    3893              : 
    3894        56905 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3895        56905 :     goto found;
    3896              : 
    3897            0 :   if (sym->attr.intrinsic)
    3898              :     {
    3899            0 :       m = gfc_intrinsic_sub_interface (c, 1);
    3900            0 :       if (m == MATCH_YES)
    3901              :         return MATCH_YES;
    3902            0 :       if (m == MATCH_NO)
    3903            0 :         gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
    3904              :                    "with an intrinsic", sym->name, &c->loc);
    3905              : 
    3906            0 :       return MATCH_ERROR;
    3907              :     }
    3908              : 
    3909              :   return MATCH_NO;
    3910              : 
    3911        62566 : found:
    3912        62566 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3913              : 
    3914        62566 :   c->resolved_sym = sym;
    3915        62566 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3916              :     return MATCH_ERROR;
    3917              : 
    3918              :   return MATCH_YES;
    3919              : }
    3920              : 
    3921              : 
    3922              : static bool
    3923        62566 : resolve_specific_s (gfc_code *c)
    3924              : {
    3925        62566 :   gfc_symbol *sym;
    3926        62566 :   match m;
    3927              : 
    3928        62566 :   sym = c->symtree->n.sym;
    3929              : 
    3930        62566 :   for (;;)
    3931              :     {
    3932        62566 :       m = resolve_specific_s0 (c, sym);
    3933        62566 :       if (m == MATCH_YES)
    3934              :         return true;
    3935            7 :       if (m == MATCH_ERROR)
    3936              :         return false;
    3937              : 
    3938            0 :       if (sym->ns->parent == NULL)
    3939              :         break;
    3940              : 
    3941            0 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    3942              : 
    3943            0 :       if (sym == NULL)
    3944              :         break;
    3945              :     }
    3946              : 
    3947            0 :   sym = c->symtree->n.sym;
    3948            0 :   gfc_error ("Unable to resolve the specific subroutine %qs at %L",
    3949              :              sym->name, &c->loc);
    3950              : 
    3951            0 :   return false;
    3952              : }
    3953              : 
    3954              : 
    3955              : /* Resolve a subroutine call not known to be generic nor specific.  */
    3956              : 
    3957              : static bool
    3958        15768 : resolve_unknown_s (gfc_code *c)
    3959              : {
    3960        15768 :   gfc_symbol *sym;
    3961              : 
    3962        15768 :   sym = c->symtree->n.sym;
    3963              : 
    3964        15768 :   if (sym->attr.dummy)
    3965              :     {
    3966           20 :       sym->attr.proc = PROC_DUMMY;
    3967           20 :       goto found;
    3968              :     }
    3969              : 
    3970              :   /* See if we have an intrinsic function reference.  */
    3971              : 
    3972        15748 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3973              :     {
    3974         4210 :       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
    3975              :         return true;
    3976          309 :       return false;
    3977              :     }
    3978              : 
    3979              :   /* The reference is to an external name.  */
    3980              : 
    3981        11538 : found:
    3982        11558 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3983              : 
    3984        11558 :   c->resolved_sym = sym;
    3985              : 
    3986        11558 :   return pure_subroutine (sym, sym->name, &c->loc);
    3987              : }
    3988              : 
    3989              : 
    3990              : 
    3991              : static bool
    3992          805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
    3993              :                          gfc_code *c, gfc_namespace *ns)
    3994              : {
    3995          805 :   locus *here;
    3996              : 
    3997              :   /* If the type has been imported then its vtype functions are OK.  */
    3998          805 :   if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
    3999              :     return true;
    4000              : 
    4001              :   if (e)
    4002          791 :     here = &e->where;
    4003              :   else
    4004            7 :     here = &c->loc;
    4005              : 
    4006          798 :   if (s && !s->import_only)
    4007          705 :     s = gfc_find_symtree (ns->sym_root, sym->name);
    4008              : 
    4009          798 :   if (ns->import_state == IMPORT_ONLY
    4010           75 :       && sym->ns != ns
    4011           58 :       && (!s || !s->import_only))
    4012              :     {
    4013           21 :       gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
    4014              :                  "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
    4015           21 :       return false;
    4016              :     }
    4017          777 :   else if (ns->import_state == IMPORT_NONE
    4018           27 :            && sym->ns != ns)
    4019              :     {
    4020           12 :       gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
    4021              :                  "has IMPORT, NONE", sym->name, here);
    4022           12 :       return false;
    4023              :     }
    4024              :   return true;
    4025              : }
    4026              : 
    4027              : 
    4028              : static bool
    4029         6919 : check_import_status (gfc_expr *e)
    4030              : {
    4031         6919 :   gfc_symtree *st;
    4032         6919 :   gfc_ref *ref;
    4033         6919 :   gfc_symbol *sym, *der;
    4034         6919 :   gfc_namespace *ns = gfc_current_ns;
    4035              : 
    4036         6919 :   switch (e->expr_type)
    4037              :     {
    4038          727 :       case EXPR_VARIABLE:
    4039          727 :       case EXPR_FUNCTION:
    4040          727 :       case EXPR_SUBSTRING:
    4041          727 :         sym = e->symtree ? e->symtree->n.sym : NULL;
    4042              : 
    4043              :         /* Check the symbol itself.  */
    4044          727 :         if (sym
    4045          727 :             && !(ns->proc_name
    4046              :                  && (sym == ns->proc_name))
    4047         1450 :             && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
    4048              :           return false;
    4049              : 
    4050              :         /* Check the declared derived type.  */
    4051          717 :         if (sym->ts.type == BT_DERIVED)
    4052              :           {
    4053           16 :             der = sym->ts.u.derived;
    4054           16 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4055              : 
    4056           16 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4057              :               return false;
    4058              :           }
    4059          701 :         else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
    4060              :           {
    4061           44 :             der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
    4062              :                                    : sym->ts.u.derived;
    4063           44 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4064              : 
    4065           44 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4066              :               return false;
    4067              :           }
    4068              : 
    4069              :         /* Check the declared derived types of component references.  */
    4070          724 :         for (ref = e->ref; ref; ref = ref->next)
    4071           20 :           if (ref->type == REF_COMPONENT)
    4072              :             {
    4073           19 :               gfc_component *c = ref->u.c.component;
    4074           19 :               if (c->ts.type == BT_DERIVED)
    4075              :                 {
    4076            7 :                   der = c->ts.u.derived;
    4077            7 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4078            7 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4079              :                     return false;
    4080              :                 }
    4081           12 :               else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
    4082              :                 {
    4083            0 :                   der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
    4084              :                                        : c->ts.u.derived;
    4085            0 :                   st = gfc_find_symtree (ns->sym_root, der->name);
    4086            0 :                   if (!check_sym_import_status (der, st, e, NULL, ns))
    4087              :                     return false;
    4088              :                 }
    4089              :             }
    4090              : 
    4091              :         break;
    4092              : 
    4093            8 :       case EXPR_ARRAY:
    4094            8 :       case EXPR_STRUCTURE:
    4095              :         /* Check the declared derived type.  */
    4096            8 :         if (e->ts.type == BT_DERIVED)
    4097              :           {
    4098            8 :             der = e->ts.u.derived;
    4099            8 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4100              : 
    4101            8 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4102              :               return false;
    4103              :           }
    4104            0 :         else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
    4105              :           {
    4106            0 :             der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
    4107              :                                    : e->ts.u.derived;
    4108            0 :             st = gfc_find_symtree (ns->sym_root, der->name);
    4109              : 
    4110            0 :             if (!check_sym_import_status (der, st, e, NULL, ns))
    4111              :               return false;
    4112              :           }
    4113              : 
    4114              :         break;
    4115              : 
    4116              : /* Either not applicable or resolved away
    4117              :       case EXPR_OP:
    4118              :       case EXPR_UNKNOWN:
    4119              :       case EXPR_CONSTANT:
    4120              :       case EXPR_NULL:
    4121              :       case EXPR_COMPCALL:
    4122              :       case EXPR_PPC: */
    4123              : 
    4124              :       default:
    4125              :         break;
    4126              :     }
    4127              : 
    4128              :   return true;
    4129              : }
    4130              : 
    4131              : 
    4132              : /* If an elemental call has an INTENT_IN argument that has a dependency on an
    4133              :    argument which is not INTENT_IN and requires a temporary, build a temporary
    4134              :    for the INTENT_IN actual argument as well.  */
    4135              : 
    4136              : static void
    4137              : add_temp_assign_before_call (gfc_code *, gfc_namespace *, gfc_expr **);
    4138              : 
    4139              : static void
    4140         5257 : resolve_elemental_dependencies (gfc_code *c)
    4141              : {
    4142         5257 :   gfc_actual_arglist *arg1 = c->ext.actual;
    4143         5257 :   gfc_actual_arglist *arg2 = NULL;
    4144         5257 :   gfc_formal_arglist *formal1 = c->resolved_sym->formal;
    4145         5257 :   gfc_formal_arglist *formal2 = NULL;
    4146         5257 :   gfc_expr *expr1;
    4147         5257 :   gfc_expr **expr2;
    4148              : 
    4149        16645 :   for (; arg1 && formal1; arg1 = arg1->next, formal1 = formal1->next)
    4150              :     {
    4151        11388 :       if (formal1->sym
    4152        11388 :           && (formal1->sym->attr.intent == INTENT_IN
    4153         3536 :               || formal1->sym->attr.value))
    4154         8110 :         continue;
    4155              : 
    4156         3278 :       if (!arg1->expr || arg1->expr->expr_type != EXPR_VARIABLE)
    4157            0 :         continue;
    4158              : 
    4159         3278 :       arg2 = c->ext.actual;
    4160         3278 :       formal2 = c->resolved_sym->formal;
    4161        10696 :       for (; arg2 && formal2; arg2 = arg2->next, formal2 = formal2->next)
    4162              :         {
    4163         7418 :           if (arg2 == arg1 || !arg2->expr
    4164         4128 :               || !(formal2->sym && formal2->sym->attr.intent == INTENT_IN))
    4165         3304 :             continue;
    4166              : 
    4167         4114 :           expr1 = arg1->expr;
    4168         4114 :           expr2 = &arg2->expr;
    4169              : 
    4170              :           /* If the arg1 has something horrible like a vector index and
    4171              :              there is a dependency between arg1 and arg2, build a
    4172              :              temporary from arg2, assign the arg2 to it and use the
    4173              :              temporary in the call expression.  */
    4174         2009 :           if (expr1->rank && gfc_ref_needs_temporary_p (expr1->ref)
    4175         4234 :               && gfc_check_dependency (expr1, *expr2, false))
    4176           36 :             add_temp_assign_before_call (c, gfc_current_ns, expr2);
    4177              :         }
    4178              :     }
    4179         5257 : }
    4180              : 
    4181              : /* Resolve a subroutine call.  Although it was tempting to use the same code
    4182              :    for functions, subroutines and functions are stored differently and this
    4183              :    makes things awkward.  */
    4184              : 
    4185              : 
    4186              : static bool
    4187        81290 : resolve_call (gfc_code *c)
    4188              : {
    4189        81290 :   bool t;
    4190        81290 :   procedure_type ptype = PROC_INTRINSIC;
    4191        81290 :   gfc_symbol *csym, *sym;
    4192        81290 :   bool no_formal_args;
    4193              : 
    4194        81290 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4195              : 
    4196        81290 :   if (csym && csym->ts.type != BT_UNKNOWN)
    4197              :     {
    4198            4 :       gfc_error ("%qs at %L has a type, which is not consistent with "
    4199              :                  "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
    4200            4 :       return false;
    4201              :     }
    4202              : 
    4203        81286 :   if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
    4204              :     {
    4205        17243 :       gfc_symtree *st;
    4206        17243 :       gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
    4207        17243 :       sym = st ? st->n.sym : NULL;
    4208        17243 :       if (sym && csym != sym
    4209            3 :               && sym->ns == gfc_current_ns
    4210            3 :               && sym->attr.flavor == FL_PROCEDURE
    4211            3 :               && sym->attr.contained)
    4212              :         {
    4213            3 :           sym->refs++;
    4214            3 :           if (csym->attr.generic)
    4215            2 :             c->symtree->n.sym = sym;
    4216              :           else
    4217            1 :             c->symtree = st;
    4218            3 :           csym = c->symtree->n.sym;
    4219              :         }
    4220              :     }
    4221              : 
    4222              :   /* If this ia a deferred TBP, c->expr1 will be set.  */
    4223        81286 :   if (!c->expr1 && csym)
    4224              :     {
    4225        79577 :       if (csym->attr.abstract)
    4226              :         {
    4227            1 :           gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
    4228              :                     csym->name, &c->loc);
    4229            1 :           return false;
    4230              :         }
    4231              : 
    4232              :       /* Subroutines without the RECURSIVE attribution are not allowed to
    4233              :          call themselves.  */
    4234        79576 :       if (is_illegal_recursion (csym, gfc_current_ns))
    4235              :         {
    4236            4 :           if (csym->attr.entry && csym->ns->entries)
    4237            2 :             gfc_error ("ENTRY %qs at %L cannot be called recursively, "
    4238              :                        "as subroutine %qs is not RECURSIVE",
    4239            2 :                        csym->name, &c->loc, csym->ns->entries->sym->name);
    4240              :           else
    4241            2 :             gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
    4242              :                        "as it is not RECURSIVE", csym->name, &c->loc);
    4243              : 
    4244        81285 :           t = false;
    4245              :         }
    4246              :     }
    4247              : 
    4248              :   /* Switch off assumed size checking and do this again for certain kinds
    4249              :      of procedure, once the procedure itself is resolved.  */
    4250        81285 :   need_full_assumed_size++;
    4251              : 
    4252        81285 :   if (csym)
    4253        81285 :     ptype = csym->attr.proc;
    4254              : 
    4255        81285 :   no_formal_args = csym && is_external_proc (csym)
    4256        15638 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4257        81285 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4258              :     return false;
    4259              : 
    4260              :   /* Resume assumed_size checking.  */
    4261        81251 :   need_full_assumed_size--;
    4262              : 
    4263              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4264              :      check for an 'external' attribute.  */
    4265        81251 :   if (csym->ns->has_implicit_none_export
    4266         4423 :       && csym->attr.external == 0 && csym->attr.dummy == 1)
    4267              :     {
    4268            1 :       gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
    4269              :                  csym->name, &c->loc);
    4270            1 :       return false;
    4271              :     }
    4272              : 
    4273              :   /* If external, check for usage.  */
    4274        81250 :   if (csym && is_external_proc (csym))
    4275        15632 :     resolve_global_procedure (csym, &c->loc, 1);
    4276              : 
    4277              :   /* If we have an external dummy argument, we want to write out its arguments
    4278              :      with -fc-prototypes-external.  Code like
    4279              : 
    4280              :      subroutine foo(a,n)
    4281              :        external a
    4282              :        if (n == 1) call a(1)
    4283              :        if (n == 2) call a(2,3)
    4284              :      end subroutine foo
    4285              : 
    4286              :      is actually legal Fortran, but it is not possible to generate a C23-
    4287              :      compliant prototype for this, so we just record the fact here and
    4288              :      handle that during -fc-prototypes-external processing.  */
    4289              : 
    4290        81250 :   if (warn_external_argument_mismatch && csym && csym->attr.dummy
    4291           14 :       && csym->attr.external)
    4292              :     {
    4293           14 :       if (csym->formal)
    4294              :         {
    4295            6 :           bool conflict;
    4296            6 :           conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
    4297              :                                                  0, 0, 0, NULL);
    4298            6 :           if (conflict)
    4299              :             {
    4300            6 :               csym->ext_dummy_arglist_mismatch = 1;
    4301            6 :               gfc_warning (OPT_Wexternal_argument_mismatch,
    4302              :                            "Different argument lists in external dummy "
    4303              :                            "subroutine %s at %L and %L", csym->name,
    4304              :                            &c->loc, &csym->formal_at);
    4305              :             }
    4306              :         }
    4307            8 :       else if (!csym->formal_resolved)
    4308              :         {
    4309            7 :           gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
    4310            7 :           csym->formal_at = c->loc;
    4311              :         }
    4312              :     }
    4313              : 
    4314        81250 :   t = true;
    4315        81250 :   if (c->resolved_sym == NULL)
    4316              :     {
    4317        81145 :       c->resolved_isym = NULL;
    4318        81145 :       switch (procedure_kind (csym))
    4319              :         {
    4320         2811 :         case PTYPE_GENERIC:
    4321         2811 :           t = resolve_generic_s (c);
    4322         2811 :           break;
    4323              : 
    4324        62566 :         case PTYPE_SPECIFIC:
    4325        62566 :           t = resolve_specific_s (c);
    4326        62566 :           break;
    4327              : 
    4328        15768 :         case PTYPE_UNKNOWN:
    4329        15768 :           t = resolve_unknown_s (c);
    4330        15768 :           break;
    4331              : 
    4332              :         default:
    4333              :           gfc_internal_error ("resolve_subroutine(): bad function type");
    4334              :         }
    4335              :     }
    4336              : 
    4337              :   /* Some checks of elemental subroutine actual arguments.  */
    4338        81249 :   if (!resolve_elemental_actual (NULL, c))
    4339              :     return false;
    4340              : 
    4341              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4342        81241 :   if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args
    4343         6200 :       && c->ext.actual && c->ext.actual->next)
    4344         5257 :     resolve_elemental_dependencies (c);
    4345              : 
    4346        81241 :   if (!c->expr1)
    4347        79532 :     update_current_proc_array_outer_dependency (csym);
    4348              :   else
    4349              :     /* Typebound procedure: Assume the worst.  */
    4350         1709 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4351              : 
    4352        81241 :   if (c->resolved_sym
    4353        80928 :       && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
    4354           34 :     gfc_warning (OPT_Wdeprecated_declarations,
    4355              :                  "Using subroutine %qs at %L is deprecated",
    4356              :                  c->resolved_sym->name, &c->loc);
    4357              : 
    4358        81241 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4359        81241 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
    4360            2 :       && csym != gfc_current_ns->proc_name)
    4361            1 :     return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
    4362              : 
    4363              :   return t;
    4364              : }
    4365              : 
    4366              : 
    4367              : /* Compare the shapes of two arrays that have non-NULL shapes.  If both
    4368              :    op1->shape and op2->shape are non-NULL return true if their shapes
    4369              :    match.  If both op1->shape and op2->shape are non-NULL return false
    4370              :    if their shapes do not match.  If either op1->shape or op2->shape is
    4371              :    NULL, return true.  */
    4372              : 
    4373              : static bool
    4374        32289 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4375              : {
    4376        32289 :   bool t;
    4377        32289 :   int i;
    4378              : 
    4379        32289 :   t = true;
    4380              : 
    4381        32289 :   if (op1->shape != NULL && op2->shape != NULL)
    4382              :     {
    4383        42886 :       for (i = 0; i < op1->rank; i++)
    4384              :         {
    4385        22880 :           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
    4386              :            {
    4387            3 :              gfc_error ("Shapes for operands at %L and %L are not conformable",
    4388              :                         &op1->where, &op2->where);
    4389            3 :              t = false;
    4390            3 :              break;
    4391              :            }
    4392              :         }
    4393              :     }
    4394              : 
    4395        32289 :   return t;
    4396              : }
    4397              : 
    4398              : /* Convert a logical operator to the corresponding bitwise intrinsic call.
    4399              :    For example A .AND. B becomes IAND(A, B).  */
    4400              : static gfc_expr *
    4401          668 : logical_to_bitwise (gfc_expr *e)
    4402              : {
    4403          668 :   gfc_expr *tmp, *op1, *op2;
    4404          668 :   gfc_isym_id isym;
    4405          668 :   gfc_actual_arglist *args = NULL;
    4406              : 
    4407          668 :   gcc_assert (e->expr_type == EXPR_OP);
    4408              : 
    4409          668 :   isym = GFC_ISYM_NONE;
    4410          668 :   op1 = e->value.op.op1;
    4411          668 :   op2 = e->value.op.op2;
    4412              : 
    4413          668 :   switch (e->value.op.op)
    4414              :     {
    4415              :     case INTRINSIC_NOT:
    4416              :       isym = GFC_ISYM_NOT;
    4417              :       break;
    4418          126 :     case INTRINSIC_AND:
    4419          126 :       isym = GFC_ISYM_IAND;
    4420          126 :       break;
    4421          127 :     case INTRINSIC_OR:
    4422          127 :       isym = GFC_ISYM_IOR;
    4423          127 :       break;
    4424          270 :     case INTRINSIC_NEQV:
    4425          270 :       isym = GFC_ISYM_IEOR;
    4426          270 :       break;
    4427          126 :     case INTRINSIC_EQV:
    4428              :       /* "Bitwise eqv" is just the complement of NEQV === IEOR.
    4429              :          Change the old expression to NEQV, which will get replaced by IEOR,
    4430              :          and wrap it in NOT.  */
    4431          126 :       tmp = gfc_copy_expr (e);
    4432          126 :       tmp->value.op.op = INTRINSIC_NEQV;
    4433          126 :       tmp = logical_to_bitwise (tmp);
    4434          126 :       isym = GFC_ISYM_NOT;
    4435          126 :       op1 = tmp;
    4436          126 :       op2 = NULL;
    4437          126 :       break;
    4438            0 :     default:
    4439            0 :       gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
    4440              :     }
    4441              : 
    4442              :   /* Inherit the original operation's operands as arguments.  */
    4443          668 :   args = gfc_get_actual_arglist ();
    4444          668 :   args->expr = op1;
    4445          668 :   if (op2)
    4446              :     {
    4447          523 :       args->next = gfc_get_actual_arglist ();
    4448          523 :       args->next->expr = op2;
    4449              :     }
    4450              : 
    4451              :   /* Convert the expression to a function call.  */
    4452          668 :   e->expr_type = EXPR_FUNCTION;
    4453          668 :   e->value.function.actual = args;
    4454          668 :   e->value.function.isym = gfc_intrinsic_function_by_id (isym);
    4455          668 :   e->value.function.name = e->value.function.isym->name;
    4456          668 :   e->value.function.esym = NULL;
    4457              : 
    4458              :   /* Make up a pre-resolved function call symtree if we need to.  */
    4459          668 :   if (!e->symtree || !e->symtree->n.sym)
    4460              :     {
    4461          668 :       gfc_symbol *sym;
    4462          668 :       gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
    4463          668 :       sym = e->symtree->n.sym;
    4464          668 :       sym->result = sym;
    4465          668 :       sym->attr.flavor = FL_PROCEDURE;
    4466          668 :       sym->attr.function = 1;
    4467          668 :       sym->attr.elemental = 1;
    4468          668 :       sym->attr.pure = 1;
    4469          668 :       sym->attr.referenced = 1;
    4470          668 :       gfc_intrinsic_symbol (sym);
    4471          668 :       gfc_commit_symbol (sym);
    4472              :     }
    4473              : 
    4474          668 :   args->name = e->value.function.isym->formal->name;
    4475          668 :   if (e->value.function.isym->formal->next)
    4476          523 :     args->next->name = e->value.function.isym->formal->next->name;
    4477              : 
    4478          668 :   return e;
    4479              : }
    4480              : 
    4481              : /* Recursively append candidate UOP to CANDIDATES.  Store the number of
    4482              :    candidates in CANDIDATES_LEN.  */
    4483              : static void
    4484           57 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
    4485              :                                   char **&candidates,
    4486              :                                   size_t &candidates_len)
    4487              : {
    4488           59 :   gfc_symtree *p;
    4489              : 
    4490           59 :   if (uop == NULL)
    4491              :     return;
    4492              : 
    4493              :   /* Not sure how to properly filter here.  Use all for a start.
    4494              :      n.uop.op is NULL for empty interface operators (is that legal?) disregard
    4495              :      these as i suppose they don't make terribly sense.  */
    4496              : 
    4497           59 :   if (uop->n.uop->op != NULL)
    4498            2 :     vec_push (candidates, candidates_len, uop->name);
    4499              : 
    4500           59 :   p = uop->left;
    4501           59 :   if (p)
    4502            0 :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4503              : 
    4504           59 :   p = uop->right;
    4505           59 :   if (p)
    4506              :     lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
    4507              : }
    4508              : 
    4509              : /* Lookup user-operator OP fuzzily, taking names in UOP into account.  */
    4510              : 
    4511              : static const char*
    4512           57 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
    4513              : {
    4514           57 :   char **candidates = NULL;
    4515           57 :   size_t candidates_len = 0;
    4516           57 :   lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
    4517           57 :   return gfc_closest_fuzzy_match (op, candidates);
    4518              : }
    4519              : 
    4520              : 
    4521              : /* Callback finding an impure function as an operand to an .and. or
    4522              :    .or.  expression.  Remember the last function warned about to
    4523              :    avoid double warnings when recursing.  */
    4524              : 
    4525              : static int
    4526       192821 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4527              :                           void *data)
    4528              : {
    4529       192821 :   gfc_expr *f = *e;
    4530       192821 :   const char *name;
    4531       192821 :   static gfc_expr *last = NULL;
    4532       192821 :   bool *found = (bool *) data;
    4533              : 
    4534       192821 :   if (f->expr_type == EXPR_FUNCTION)
    4535              :     {
    4536        11790 :       *found = 1;
    4537        11790 :       if (f != last && !gfc_pure_function (f, &name)
    4538        13065 :           && !gfc_implicit_pure_function (f))
    4539              :         {
    4540         1136 :           if (name)
    4541         1136 :             gfc_warning (OPT_Wfunction_elimination,
    4542              :                          "Impure function %qs at %L might not be evaluated",
    4543              :                          name, &f->where);
    4544              :           else
    4545            0 :             gfc_warning (OPT_Wfunction_elimination,
    4546              :                          "Impure function at %L might not be evaluated",
    4547              :                          &f->where);
    4548              :         }
    4549        11790 :       last = f;
    4550              :     }
    4551              : 
    4552       192821 :   return 0;
    4553              : }
    4554              : 
    4555              : /* Return true if TYPE is character based, false otherwise.  */
    4556              : 
    4557              : static int
    4558         1373 : is_character_based (bt type)
    4559              : {
    4560         1373 :   return type == BT_CHARACTER || type == BT_HOLLERITH;
    4561              : }
    4562              : 
    4563              : 
    4564              : /* If expression is a hollerith, convert it to character and issue a warning
    4565              :    for the conversion.  */
    4566              : 
    4567              : static void
    4568          408 : convert_hollerith_to_character (gfc_expr *e)
    4569              : {
    4570          408 :   if (e->ts.type == BT_HOLLERITH)
    4571              :     {
    4572          108 :       gfc_typespec t;
    4573          108 :       gfc_clear_ts (&t);
    4574          108 :       t.type = BT_CHARACTER;
    4575          108 :       t.kind = e->ts.kind;
    4576          108 :       gfc_convert_type_warn (e, &t, 2, 1);
    4577              :     }
    4578          408 : }
    4579              : 
    4580              : /* Convert to numeric and issue a warning for the conversion.  */
    4581              : 
    4582              : static void
    4583          240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
    4584              : {
    4585          240 :   gfc_typespec t;
    4586          240 :   gfc_clear_ts (&t);
    4587          240 :   t.type = b->ts.type;
    4588          240 :   t.kind = b->ts.kind;
    4589          240 :   gfc_convert_type_warn (a, &t, 2, 1);
    4590          240 : }
    4591              : 
    4592              : /* Resolve an operator expression node.  This can involve replacing the
    4593              :    operation with a user defined function call.  CHECK_INTERFACES is a
    4594              :    helper macro.  */
    4595              : 
    4596              : #define CHECK_INTERFACES \
    4597              :   { \
    4598              :     match m = gfc_extend_expr (e); \
    4599              :     if (m == MATCH_YES) \
    4600              :       return true; \
    4601              :     if (m == MATCH_ERROR) \
    4602              :       return false; \
    4603              :   }
    4604              : 
    4605              : static bool
    4606       531162 : resolve_operator (gfc_expr *e)
    4607              : {
    4608       531162 :   gfc_expr *op1, *op2;
    4609              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4610       531162 :   bool t = true;
    4611              : 
    4612              :   /* Reduce stacked parentheses to single pair  */
    4613       531162 :   while (e->expr_type == EXPR_OP
    4614       531320 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4615        23499 :          && e->value.op.op1->expr_type == EXPR_OP
    4616       548152 :          && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
    4617              :     {
    4618          158 :       gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
    4619          158 :       gfc_replace_expr (e, tmp);
    4620              :     }
    4621              : 
    4622              :   /* Resolve all subnodes-- give them types.  */
    4623              : 
    4624       531162 :   switch (e->value.op.op)
    4625              :     {
    4626       479221 :     default:
    4627       479221 :       if (!gfc_resolve_expr (e->value.op.op2))
    4628       531162 :         t = false;
    4629              : 
    4630              :     /* Fall through.  */
    4631              : 
    4632       531162 :     case INTRINSIC_NOT:
    4633       531162 :     case INTRINSIC_UPLUS:
    4634       531162 :     case INTRINSIC_UMINUS:
    4635       531162 :     case INTRINSIC_PARENTHESES:
    4636       531162 :       if (!gfc_resolve_expr (e->value.op.op1))
    4637              :         return false;
    4638       531001 :       if (e->value.op.op1
    4639       530992 :           && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
    4640              :         {
    4641            0 :           gfc_error ("BOZ literal constant at %L cannot be an operand of "
    4642            0 :                      "unary operator %qs", &e->value.op.op1->where,
    4643              :                      gfc_op2string (e->value.op.op));
    4644            0 :           return false;
    4645              :         }
    4646       531001 :       if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
    4647            6 :           && e->value.op.op == INTRINSIC_UMINUS)
    4648              :         {
    4649            2 :           gfc_error ("Negation of unsigned expression at %L not permitted ",
    4650              :                      &e->value.op.op1->where);
    4651            2 :           return false;
    4652              :         }
    4653       530999 :       break;
    4654              :     }
    4655              : 
    4656              :   /* Typecheck the new node.  */
    4657              : 
    4658       530999 :   op1 = e->value.op.op1;
    4659       530999 :   op2 = e->value.op.op2;
    4660       530999 :   if (op1 == NULL && op2 == NULL)
    4661              :     return false;
    4662              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4663       530990 :   if (t == false)
    4664              :     return false;
    4665              : 
    4666              :   /* op1 and op2 cannot both be BOZ.  */
    4667       530924 :   if (op1 && op1->ts.type == BT_BOZ
    4668            0 :       && op2 && op2->ts.type == BT_BOZ)
    4669              :     {
    4670            0 :       gfc_error ("Operands at %L and %L cannot appear as operands of "
    4671            0 :                  "binary operator %qs", &op1->where, &op2->where,
    4672              :                  gfc_op2string (e->value.op.op));
    4673            0 :       return false;
    4674              :     }
    4675              : 
    4676       530924 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4677       530922 :       || (op2 && op2->expr_type == EXPR_NULL))
    4678              :     {
    4679            3 :       CHECK_INTERFACES
    4680            3 :       gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
    4681            3 :       return false;
    4682              :     }
    4683              : 
    4684       530921 :   switch (e->value.op.op)
    4685              :     {
    4686         8106 :     case INTRINSIC_UPLUS:
    4687         8106 :     case INTRINSIC_UMINUS:
    4688         8106 :       if (op1->ts.type == BT_INTEGER
    4689              :           || op1->ts.type == BT_REAL
    4690              :           || op1->ts.type == BT_COMPLEX
    4691              :           || op1->ts.type == BT_UNSIGNED)
    4692              :         {
    4693         8037 :           e->ts = op1->ts;
    4694         8037 :           break;
    4695              :         }
    4696              : 
    4697           69 :       CHECK_INTERFACES
    4698           43 :       gfc_error ("Operand of unary numeric operator %qs at %L is %s",
    4699              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
    4700           43 :       return false;
    4701              : 
    4702       155025 :     case INTRINSIC_POWER:
    4703       155025 :     case INTRINSIC_PLUS:
    4704       155025 :     case INTRINSIC_MINUS:
    4705       155025 :     case INTRINSIC_TIMES:
    4706       155025 :     case INTRINSIC_DIVIDE:
    4707              : 
    4708              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4709              :              conversion.  */
    4710       155025 :       if (flag_unsigned &&  gfc_invalid_unsigned_ops (op1, op2))
    4711              :         {
    4712            3 :           CHECK_INTERFACES
    4713            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are "
    4714              :                      "%s/%s", gfc_op2string (e->value.op.op), &e->where,
    4715              :                      gfc_typename (op1), gfc_typename (op2));
    4716            3 :           return false;
    4717              :         }
    4718              : 
    4719       155022 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4720              :         {
    4721              :           /* Do not perform conversions if operands are not conformable as
    4722              :              required for the binary intrinsic operators (F2018:10.1.5).
    4723              :              Defer to a possibly overloading user-defined operator.  */
    4724       154568 :           if (!gfc_op_rank_conformable (op1, op2))
    4725              :             {
    4726           36 :               CHECK_INTERFACES
    4727            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4728            0 :                          &op1->where, &op2->where);
    4729            0 :               return false;
    4730              :             }
    4731              : 
    4732       154532 :           gfc_type_convert_binary (e, 1);
    4733       154532 :           break;
    4734              :         }
    4735              : 
    4736          454 :       if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
    4737              :         {
    4738          225 :           CHECK_INTERFACES
    4739            2 :           gfc_error ("Unexpected derived-type entities in binary intrinsic "
    4740              :                      "numeric operator %qs at %L",
    4741              :                      gfc_op2string (e->value.op.op), &e->where);
    4742            2 :           return false;
    4743              :         }
    4744              :       else
    4745              :         {
    4746          229 :           CHECK_INTERFACES
    4747            3 :           gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
    4748              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4749              :                      gfc_typename (op2));
    4750            3 :           return false;
    4751              :         }
    4752              : 
    4753         2268 :     case INTRINSIC_CONCAT:
    4754         2268 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4755         2243 :           && op1->ts.kind == op2->ts.kind)
    4756              :         {
    4757         2234 :           e->ts.type = BT_CHARACTER;
    4758         2234 :           e->ts.kind = op1->ts.kind;
    4759         2234 :           break;
    4760              :         }
    4761              : 
    4762           34 :       CHECK_INTERFACES
    4763           10 :       gfc_error ("Operands of string concatenation operator at %L are %s/%s",
    4764              :                  &e->where, gfc_typename (op1), gfc_typename (op2));
    4765           10 :       return false;
    4766              : 
    4767        69521 :     case INTRINSIC_AND:
    4768        69521 :     case INTRINSIC_OR:
    4769        69521 :     case INTRINSIC_EQV:
    4770        69521 :     case INTRINSIC_NEQV:
    4771        69521 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4772              :         {
    4773        68970 :           e->ts.type = BT_LOGICAL;
    4774        68970 :           e->ts.kind = gfc_kind_max (op1, op2);
    4775        68970 :           if (op1->ts.kind < e->ts.kind)
    4776          140 :             gfc_convert_type (op1, &e->ts, 2);
    4777        68830 :           else if (op2->ts.kind < e->ts.kind)
    4778          117 :             gfc_convert_type (op2, &e->ts, 2);
    4779              : 
    4780        68970 :           if (flag_frontend_optimize &&
    4781        57936 :             (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
    4782              :             {
    4783              :               /* Warn about short-circuiting
    4784              :                  with impure function as second operand.  */
    4785        51935 :               bool op2_f = false;
    4786        51935 :               gfc_expr_walker (&op2, impure_function_callback, &op2_f);
    4787              :             }
    4788              :           break;
    4789              :         }
    4790              : 
    4791              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4792          551 :       else if (flag_dec
    4793          523 :                && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
    4794              :         {
    4795          523 :           e->ts.type = BT_INTEGER;
    4796          523 :           e->ts.kind = gfc_kind_max (op1, op2);
    4797          523 :           if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
    4798          289 :             gfc_convert_type (op1, &e->ts, 1);
    4799          523 :           if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
    4800          144 :             gfc_convert_type (op2, &e->ts, 1);
    4801          523 :           e = logical_to_bitwise (e);
    4802          523 :           goto simplify_op;
    4803              :         }
    4804              : 
    4805           28 :       CHECK_INTERFACES
    4806           16 :       gfc_error ("Operands of logical operator %qs at %L are %s/%s",
    4807              :                  gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4808              :                  gfc_typename (op2));
    4809           16 :       return false;
    4810              : 
    4811        20478 :     case INTRINSIC_NOT:
    4812              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4813        20478 :       if (flag_dec && op1->ts.type == BT_INTEGER)
    4814              :         {
    4815           19 :           e->ts.type = BT_INTEGER;
    4816           19 :           e->ts.kind = op1->ts.kind;
    4817           19 :           e = logical_to_bitwise (e);
    4818           19 :           goto simplify_op;
    4819              :         }
    4820              : 
    4821        20459 :       if (op1->ts.type == BT_LOGICAL)
    4822              :         {
    4823        20453 :           e->ts.type = BT_LOGICAL;
    4824        20453 :           e->ts.kind = op1->ts.kind;
    4825        20453 :           break;
    4826              :         }
    4827              : 
    4828            6 :       CHECK_INTERFACES
    4829            3 :       gfc_error ("Operand of .not. operator at %L is %s", &e->where,
    4830              :                  gfc_typename (op1));
    4831            3 :       return false;
    4832              : 
    4833        21281 :     case INTRINSIC_GT:
    4834        21281 :     case INTRINSIC_GT_OS:
    4835        21281 :     case INTRINSIC_GE:
    4836        21281 :     case INTRINSIC_GE_OS:
    4837        21281 :     case INTRINSIC_LT:
    4838        21281 :     case INTRINSIC_LT_OS:
    4839        21281 :     case INTRINSIC_LE:
    4840        21281 :     case INTRINSIC_LE_OS:
    4841        21281 :       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
    4842              :         {
    4843           18 :           CHECK_INTERFACES
    4844            0 :           gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
    4845            0 :           return false;
    4846              :         }
    4847              : 
    4848              :       /* Fall through.  */
    4849              : 
    4850       251921 :     case INTRINSIC_EQ:
    4851       251921 :     case INTRINSIC_EQ_OS:
    4852       251921 :     case INTRINSIC_NE:
    4853       251921 :     case INTRINSIC_NE_OS:
    4854              : 
    4855       251921 :       if (flag_dec
    4856         1038 :           && is_character_based (op1->ts.type)
    4857       252256 :           && is_character_based (op2->ts.type))
    4858              :         {
    4859          204 :           convert_hollerith_to_character (op1);
    4860          204 :           convert_hollerith_to_character (op2);
    4861              :         }
    4862              : 
    4863       251921 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4864        37930 :           && op1->ts.kind == op2->ts.kind)
    4865              :         {
    4866        37893 :           e->ts.type = BT_LOGICAL;
    4867        37893 :           e->ts.kind = gfc_default_logical_kind;
    4868        37893 :           break;
    4869              :         }
    4870              : 
    4871              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4872       214028 :       if (op1->ts.type == BT_BOZ)
    4873              :         {
    4874            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
    4875              :                                "as an operand of a relational operator"),
    4876              :                                &op1->where))
    4877              :             return false;
    4878              : 
    4879            0 :           if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
    4880              :             return false;
    4881              : 
    4882            0 :           if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
    4883              :             return false;
    4884              :         }
    4885              : 
    4886              :       /* If op2 is BOZ, then op1 is not!.  Try to convert to type of op2. */
    4887       214028 :       if (op2->ts.type == BT_BOZ)
    4888              :         {
    4889            0 :           if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
    4890              :                                " as an operand of a relational operator"),
    4891              :                                 &op2->where))
    4892              :             return false;
    4893              : 
    4894            0 :           if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
    4895              :             return false;
    4896              : 
    4897            0 :           if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
    4898              :             return false;
    4899              :         }
    4900       214028 :       if (flag_dec
    4901       214028 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4902          120 :         convert_to_numeric (op1, op2);
    4903              : 
    4904       214028 :       if (flag_dec
    4905       214028 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4906          120 :         convert_to_numeric (op2, op1);
    4907              : 
    4908       214028 :       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
    4909              :         {
    4910              :           /* Do not perform conversions if operands are not conformable as
    4911              :              required for the binary intrinsic operators (F2018:10.1.5).
    4912              :              Defer to a possibly overloading user-defined operator.  */
    4913       212899 :           if (!gfc_op_rank_conformable (op1, op2))
    4914              :             {
    4915           70 :               CHECK_INTERFACES
    4916            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    4917            0 :                          &op1->where, &op2->where);
    4918            0 :               return false;
    4919              :             }
    4920              : 
    4921       212829 :           if (flag_unsigned  && gfc_invalid_unsigned_ops (op1, op2))
    4922              :             {
    4923            1 :               CHECK_INTERFACES
    4924            1 :               gfc_error ("Inconsistent types for operator at %L and %L: "
    4925            1 :                          "%s and %s", &op1->where, &op2->where,
    4926              :                          gfc_typename (op1), gfc_typename (op2));
    4927            1 :               return false;
    4928              :             }
    4929              : 
    4930       212828 :           gfc_type_convert_binary (e, 1);
    4931              : 
    4932       212828 :           e->ts.type = BT_LOGICAL;
    4933       212828 :           e->ts.kind = gfc_default_logical_kind;
    4934              : 
    4935       212828 :           if (warn_compare_reals)
    4936              :             {
    4937           69 :               gfc_intrinsic_op op = e->value.op.op;
    4938              : 
    4939              :               /* Type conversion has made sure that the types of op1 and op2
    4940              :                  agree, so it is only necessary to check the first one.   */
    4941           69 :               if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
    4942           13 :                   && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
    4943            6 :                       || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
    4944              :                 {
    4945           13 :                   const char *msg;
    4946              : 
    4947           13 :                   if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
    4948              :                     msg = G_("Equality comparison for %s at %L");
    4949              :                   else
    4950            6 :                     msg = G_("Inequality comparison for %s at %L");
    4951              : 
    4952           13 :                   gfc_warning (OPT_Wcompare_reals, msg,
    4953              :                                gfc_typename (op1), &op1->where);
    4954              :                 }
    4955              :             }
    4956              : 
    4957              :           break;
    4958              :         }
    4959              : 
    4960         1129 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4961              :         {
    4962            2 :           CHECK_INTERFACES
    4963            4 :           gfc_error ("Logicals at %L must be compared with %s instead of %s",
    4964              :                      &e->where,
    4965            2 :                      (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
    4966              :                       ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
    4967            2 :         }
    4968              :       else
    4969              :         {
    4970         1127 :           CHECK_INTERFACES
    4971          113 :           gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
    4972              :                      gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
    4973              :                      gfc_typename (op2));
    4974              :         }
    4975              : 
    4976              :       return false;
    4977              : 
    4978          282 :     case INTRINSIC_USER:
    4979          282 :       if (e->value.op.uop->op == NULL)
    4980              :         {
    4981           57 :           const char *name = e->value.op.uop->name;
    4982           57 :           const char *guessed;
    4983           57 :           guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
    4984           57 :           CHECK_INTERFACES
    4985            5 :           if (guessed)
    4986            1 :             gfc_error ("Unknown operator %qs at %L; did you mean "
    4987              :                         "%qs?", name, &e->where, guessed);
    4988              :           else
    4989            4 :             gfc_error ("Unknown operator %qs at %L", name, &e->where);
    4990              :         }
    4991          225 :       else if (op2 == NULL)
    4992              :         {
    4993           48 :           CHECK_INTERFACES
    4994            0 :           gfc_error ("Operand of user operator %qs at %L is %s",
    4995            0 :                   e->value.op.uop->name, &e->where, gfc_typename (op1));
    4996              :         }
    4997              :       else
    4998              :         {
    4999          177 :           e->value.op.uop->op->sym->attr.referenced = 1;
    5000          177 :           CHECK_INTERFACES
    5001            5 :           gfc_error ("Operands of user operator %qs at %L are %s/%s",
    5002            5 :                     e->value.op.uop->name, &e->where, gfc_typename (op1),
    5003              :                     gfc_typename (op2));
    5004              :         }
    5005              : 
    5006              :       return false;
    5007              : 
    5008        23302 :     case INTRINSIC_PARENTHESES:
    5009        23302 :       e->ts = op1->ts;
    5010        23302 :       if (e->ts.type == BT_CHARACTER)
    5011          321 :         e->ts.u.cl = op1->ts.u.cl;
    5012              :       break;
    5013              : 
    5014            0 :     default:
    5015            0 :       gfc_internal_error ("resolve_operator(): Bad intrinsic");
    5016              :     }
    5017              : 
    5018              :   /* Deal with arrayness of an operand through an operator.  */
    5019              : 
    5020       528249 :   switch (e->value.op.op)
    5021              :     {
    5022       476457 :     case INTRINSIC_PLUS:
    5023       476457 :     case INTRINSIC_MINUS:
    5024       476457 :     case INTRINSIC_TIMES:
    5025       476457 :     case INTRINSIC_DIVIDE:
    5026       476457 :     case INTRINSIC_POWER:
    5027       476457 :     case INTRINSIC_CONCAT:
    5028       476457 :     case INTRINSIC_AND:
    5029       476457 :     case INTRINSIC_OR:
    5030       476457 :     case INTRINSIC_EQV:
    5031       476457 :     case INTRINSIC_NEQV:
    5032       476457 :     case INTRINSIC_EQ:
    5033       476457 :     case INTRINSIC_EQ_OS:
    5034       476457 :     case INTRINSIC_NE:
    5035       476457 :     case INTRINSIC_NE_OS:
    5036       476457 :     case INTRINSIC_GT:
    5037       476457 :     case INTRINSIC_GT_OS:
    5038       476457 :     case INTRINSIC_GE:
    5039       476457 :     case INTRINSIC_GE_OS:
    5040       476457 :     case INTRINSIC_LT:
    5041       476457 :     case INTRINSIC_LT_OS:
    5042       476457 :     case INTRINSIC_LE:
    5043       476457 :     case INTRINSIC_LE_OS:
    5044              : 
    5045       476457 :       if (op1->rank == 0 && op2->rank == 0)
    5046       424671 :         e->rank = 0;
    5047              : 
    5048       476457 :       if (op1->rank == 0 && op2->rank != 0)
    5049              :         {
    5050         2529 :           e->rank = op2->rank;
    5051              : 
    5052         2529 :           if (e->shape == NULL)
    5053         2499 :             e->shape = gfc_copy_shape (op2->shape, op2->rank);
    5054              :         }
    5055              : 
    5056       476457 :       if (op1->rank != 0 && op2->rank == 0)
    5057              :         {
    5058        16907 :           e->rank = op1->rank;
    5059              : 
    5060        16907 :           if (e->shape == NULL)
    5061        16889 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5062              :         }
    5063              : 
    5064       476457 :       if (op1->rank != 0 && op2->rank != 0)
    5065              :         {
    5066        32350 :           if (op1->rank == op2->rank)
    5067              :             {
    5068        32350 :               e->rank = op1->rank;
    5069        32350 :               if (e->shape == NULL)
    5070              :                 {
    5071        32289 :                   t = compare_shapes (op1, op2);
    5072        32289 :                   if (!t)
    5073            3 :                     e->shape = NULL;
    5074              :                   else
    5075        32286 :                     e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5076              :                 }
    5077              :             }
    5078              :           else
    5079              :             {
    5080              :               /* Allow higher level expressions to work.  */
    5081            0 :               e->rank = 0;
    5082              : 
    5083              :               /* Try user-defined operators, and otherwise throw an error.  */
    5084            0 :               CHECK_INTERFACES
    5085            0 :               gfc_error ("Inconsistent ranks for operator at %L and %L",
    5086            0 :                          &op1->where, &op2->where);
    5087            0 :               return false;
    5088              :             }
    5089              :         }
    5090              :       break;
    5091              : 
    5092        51792 :     case INTRINSIC_PARENTHESES:
    5093        51792 :     case INTRINSIC_NOT:
    5094        51792 :     case INTRINSIC_UPLUS:
    5095        51792 :     case INTRINSIC_UMINUS:
    5096              :       /* Simply copy arrayness attribute */
    5097        51792 :       e->rank = op1->rank;
    5098        51792 :       e->corank = op1->corank;
    5099              : 
    5100        51792 :       if (e->shape == NULL)
    5101        51785 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5102              : 
    5103              :       break;
    5104              : 
    5105              :     default:
    5106              :       break;
    5107              :     }
    5108              : 
    5109       528791 : simplify_op:
    5110              : 
    5111              :   /* Attempt to simplify the expression.  */
    5112            3 :   if (t)
    5113              :     {
    5114       528788 :       t = gfc_simplify_expr (e, 0);
    5115              :       /* Some calls do not succeed in simplification and return false
    5116              :          even though there is no error; e.g. variable references to
    5117              :          PARAMETER arrays.  */
    5118       528788 :       if (!gfc_is_constant_expr (e))
    5119       483111 :         t = true;
    5120              :     }
    5121              :   return t;
    5122              : }
    5123              : 
    5124              : static bool
    5125          150 : resolve_conditional (gfc_expr *expr)
    5126              : {
    5127          150 :   gfc_expr *condition, *true_expr, *false_expr;
    5128              : 
    5129          150 :   condition = expr->value.conditional.condition;
    5130          150 :   true_expr = expr->value.conditional.true_expr;
    5131          150 :   false_expr = expr->value.conditional.false_expr;
    5132              : 
    5133          300 :   if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
    5134          300 :       || !gfc_resolve_expr (false_expr))
    5135            0 :     return false;
    5136              : 
    5137          150 :   if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
    5138              :     {
    5139            2 :       gfc_error (
    5140              :         "Condition in conditional expression must be a scalar logical at %L",
    5141              :         &condition->where);
    5142            2 :       return false;
    5143              :     }
    5144              : 
    5145          148 :   if (true_expr->ts.type != false_expr->ts.type)
    5146              :     {
    5147            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5148              :                  "must have the same declared type",
    5149              :                  &true_expr->where, &false_expr->where);
    5150            1 :       return false;
    5151              :     }
    5152              : 
    5153          147 :   if (true_expr->ts.kind != false_expr->ts.kind)
    5154              :     {
    5155            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5156              :                  "must have the same kind parameter",
    5157              :                  &true_expr->where, &false_expr->where);
    5158            1 :       return false;
    5159              :     }
    5160              : 
    5161          146 :   if (true_expr->rank != false_expr->rank)
    5162              :     {
    5163            1 :       gfc_error ("expr at %L and expr at %L in conditional expression "
    5164              :                  "must have the same rank",
    5165              :                  &true_expr->where, &false_expr->where);
    5166            1 :       return false;
    5167              :     }
    5168              : 
    5169              :   /* TODO: support more data types for conditional expressions  */
    5170          145 :   if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
    5171          145 :       && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
    5172           55 :       && true_expr->ts.type != BT_CHARACTER)
    5173              :     {
    5174            1 :       gfc_error (
    5175              :         "Sorry, only integer, logical, real, complex and character types are "
    5176              :         "currently supported for conditional expressions at %L",
    5177              :         &expr->where);
    5178            1 :       return false;
    5179              :     }
    5180              : 
    5181              :   /* TODO: support arrays in conditional expressions  */
    5182          144 :   if (true_expr->rank > 0)
    5183              :     {
    5184            1 :       gfc_error ("Sorry, array is currently unsupported for conditional "
    5185              :                  "expressions at %L",
    5186              :                  &expr->where);
    5187            1 :       return false;
    5188              :     }
    5189              : 
    5190          143 :   expr->ts = true_expr->ts;
    5191          143 :   expr->rank = true_expr->rank;
    5192          143 :   return true;
    5193              : }
    5194              : 
    5195              : /************** Array resolution subroutines **************/
    5196              : 
    5197              : enum compare_result
    5198              : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
    5199              : 
    5200              : /* Compare two integer expressions.  */
    5201              : 
    5202              : static compare_result
    5203       464581 : compare_bound (gfc_expr *a, gfc_expr *b)
    5204              : {
    5205       464581 :   int i;
    5206              : 
    5207       464581 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5208       305162 :       || b == NULL || b->expr_type != EXPR_CONSTANT)
    5209              :     return CMP_UNKNOWN;
    5210              : 
    5211              :   /* If either of the types isn't INTEGER, we must have
    5212              :      raised an error earlier.  */
    5213              : 
    5214       210878 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5215              :     return CMP_UNKNOWN;
    5216              : 
    5217       210874 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5218              : 
    5219       210874 :   if (i < 0)
    5220              :     return CMP_LT;
    5221        99431 :   if (i > 0)
    5222        39563 :     return CMP_GT;
    5223              :   return CMP_EQ;
    5224              : }
    5225              : 
    5226              : 
    5227              : /* Compare an integer expression with an integer.  */
    5228              : 
    5229              : static compare_result
    5230        74329 : compare_bound_int (gfc_expr *a, int b)
    5231              : {
    5232        74329 :   int i;
    5233              : 
    5234        74329 :   if (a == NULL
    5235        31933 :       || a->expr_type != EXPR_CONSTANT
    5236        28985 :       || a->ts.type != BT_INTEGER)
    5237              :     return CMP_UNKNOWN;
    5238              : 
    5239        28985 :   i = mpz_cmp_si (a->value.integer, b);
    5240              : 
    5241        28985 :   if (i < 0)
    5242              :     return CMP_LT;
    5243        24511 :   if (i > 0)
    5244        21431 :     return CMP_GT;
    5245              :   return CMP_EQ;
    5246              : }
    5247              : 
    5248              : 
    5249              : /* Compare an integer expression with a mpz_t.  */
    5250              : 
    5251              : static compare_result
    5252        69059 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5253              : {
    5254        69059 :   int i;
    5255              : 
    5256        69059 :   if (a == NULL
    5257        56405 :       || a->expr_type != EXPR_CONSTANT
    5258        54282 :       || a->ts.type != BT_INTEGER)
    5259              :     return CMP_UNKNOWN;
    5260              : 
    5261        54279 :   i = mpz_cmp (a->value.integer, b);
    5262              : 
    5263        54279 :   if (i < 0)
    5264              :     return CMP_LT;
    5265        24904 :   if (i > 0)
    5266        10696 :     return CMP_GT;
    5267              :   return CMP_EQ;
    5268              : }
    5269              : 
    5270              : 
    5271              : /* Compute the last value of a sequence given by a triplet.
    5272              :    Return 0 if it wasn't able to compute the last value, or if the
    5273              :    sequence if empty, and 1 otherwise.  */
    5274              : 
    5275              : static int
    5276        51828 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5277              :                                 gfc_expr *stride, mpz_t last)
    5278              : {
    5279        51828 :   mpz_t rem;
    5280              : 
    5281        51828 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5282        36767 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5283        32172 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5284              :     return 0;
    5285              : 
    5286        31853 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5287        31852 :       || (stride != NULL && stride->ts.type != BT_INTEGER))
    5288              :     return 0;
    5289              : 
    5290         6646 :   if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
    5291              :     {
    5292        25332 :       if (compare_bound (start, end) == CMP_GT)
    5293              :         return 0;
    5294        23943 :       mpz_set (last, end->value.integer);
    5295        23943 :       return 1;
    5296              :     }
    5297              : 
    5298         6520 :   if (compare_bound_int (stride, 0) == CMP_GT)
    5299              :     {
    5300              :       /* Stride is positive */
    5301         5155 :       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
    5302              :         return 0;
    5303              :     }
    5304              :   else
    5305              :     {
    5306              :       /* Stride is negative */
    5307         1365 :       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
    5308              :         return 0;
    5309              :     }
    5310              : 
    5311         6500 :   mpz_init (rem);
    5312         6500 :   mpz_sub (rem, end->value.integer, start->value.integer);
    5313         6500 :   mpz_tdiv_r (rem, rem, stride->value.integer);
    5314         6500 :   mpz_sub (last, end->value.integer, rem);
    5315         6500 :   mpz_clear (rem);
    5316              : 
    5317         6500 :   return 1;
    5318              : }
    5319              : 
    5320              : 
    5321              : /* Compare a single dimension of an array reference to the array
    5322              :    specification.  */
    5323              : 
    5324              : static bool
    5325       215681 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5326              : {
    5327       215681 :   mpz_t last_value;
    5328              : 
    5329       215681 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5330              :     {
    5331          495 :       gcc_assert (ar->stride[i] == NULL);
    5332              :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5333          495 :       if (ar->start[i] == NULL)
    5334              :         {
    5335          403 :           gcc_assert (ar->end[i] == NULL);
    5336              :           return true;
    5337              :         }
    5338              :     }
    5339              : 
    5340              : /* Given start, end and stride values, calculate the minimum and
    5341              :    maximum referenced indexes.  */
    5342              : 
    5343       215278 :   switch (ar->dimen_type[i])
    5344              :     {
    5345              :     case DIMEN_VECTOR:
    5346              :     case DIMEN_THIS_IMAGE:
    5347              :       break;
    5348              : 
    5349       155175 :     case DIMEN_STAR:
    5350       155175 :     case DIMEN_ELEMENT:
    5351       155175 :       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
    5352              :         {
    5353            2 :           if (i < as->rank)
    5354            2 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5355              :                          "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5356            2 :                          mpz_get_si (ar->start[i]->value.integer),
    5357            2 :                          mpz_get_si (as->lower[i]->value.integer), i+1);
    5358              :           else
    5359            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5360              :                          "(%ld < %ld) in codimension %d", &ar->c_where[i],
    5361            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5362            0 :                          mpz_get_si (as->lower[i]->value.integer),
    5363            0 :                          i + 1 - as->rank);
    5364            2 :           return true;
    5365              :         }
    5366       155173 :       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
    5367              :         {
    5368           39 :           if (i < as->rank)
    5369           39 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5370              :                          "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5371           39 :                          mpz_get_si (ar->start[i]->value.integer),
    5372           39 :                          mpz_get_si (as->upper[i]->value.integer), i+1);
    5373              :           else
    5374            0 :             gfc_warning (0, "Array reference at %L is out of bounds "
    5375              :                          "(%ld > %ld) in codimension %d", &ar->c_where[i],
    5376            0 :                          mpz_get_si (ar->start[i]->value.integer),
    5377            0 :                          mpz_get_si (as->upper[i]->value.integer),
    5378            0 :                          i + 1 - as->rank);
    5379           39 :           return true;
    5380              :         }
    5381              : 
    5382              :       break;
    5383              : 
    5384        51873 :     case DIMEN_RANGE:
    5385        51873 :       {
    5386              : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
    5387              : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
    5388              : 
    5389        51873 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5390        51873 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5391              : 
    5392              :         /* Check for zero stride, which is not allowed.  */
    5393        51873 :         if (comp_stride_zero == CMP_EQ)
    5394              :           {
    5395            1 :             gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
    5396            1 :             return false;
    5397              :           }
    5398              : 
    5399              :         /* if start == end || (stride > 0 && start < end)
    5400              :                            || (stride < 0 && start > end),
    5401              :            then the array section contains at least one element.  In this
    5402              :            case, there is an out-of-bounds access if
    5403              :            (start < lower || start > upper).  */
    5404        51872 :         if (comp_start_end == CMP_EQ
    5405        51110 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5406        48321 :                 && comp_start_end == CMP_LT)
    5407        22728 :             || (comp_stride_zero == CMP_LT
    5408        22728 :                 && comp_start_end == CMP_GT))
    5409              :           {
    5410        30489 :             if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
    5411              :               {
    5412           27 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5413              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5414           27 :                        mpz_get_si (AR_START->value.integer),
    5415           27 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5416           27 :                 return true;
    5417              :               }
    5418        30462 :             if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
    5419              :               {
    5420           17 :                 gfc_warning (0, "Lower array reference at %L is out of bounds "
    5421              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5422           17 :                        mpz_get_si (AR_START->value.integer),
    5423           17 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5424           17 :                 return true;
    5425              :               }
    5426              :           }
    5427              : 
    5428              :         /* If we can compute the highest index of the array section,
    5429              :            then it also has to be between lower and upper.  */
    5430        51828 :         mpz_init (last_value);
    5431        51828 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5432              :                                             last_value))
    5433              :           {
    5434        30443 :             if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
    5435              :               {
    5436            3 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5437              :                        "(%ld < %ld) in dimension %d", &ar->c_where[i],
    5438              :                        mpz_get_si (last_value),
    5439            3 :                        mpz_get_si (as->lower[i]->value.integer), i+1);
    5440            3 :                 mpz_clear (last_value);
    5441            3 :                 return true;
    5442              :               }
    5443        30440 :             if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
    5444              :               {
    5445            7 :                 gfc_warning (0, "Upper array reference at %L is out of bounds "
    5446              :                        "(%ld > %ld) in dimension %d", &ar->c_where[i],
    5447              :                        mpz_get_si (last_value),
    5448            7 :                        mpz_get_si (as->upper[i]->value.integer), i+1);
    5449            7 :                 mpz_clear (last_value);
    5450            7 :                 return true;
    5451              :               }
    5452              :           }
    5453        51818 :         mpz_clear (last_value);
    5454              : 
    5455              : #undef AR_START
    5456              : #undef AR_END
    5457              :       }
    5458        51818 :       break;
    5459              : 
    5460            0 :     default:
    5461            0 :       gfc_internal_error ("check_dimension(): Bad array reference");
    5462              :     }
    5463              : 
    5464              :   return true;
    5465              : }
    5466              : 
    5467              : 
    5468              : /* Compare an array reference with an array specification.  */
    5469              : 
    5470              : static bool
    5471       424537 : compare_spec_to_ref (gfc_array_ref *ar)
    5472              : {
    5473       424537 :   gfc_array_spec *as;
    5474       424537 :   int i;
    5475              : 
    5476       424537 :   as = ar->as;
    5477       424537 :   i = as->rank - 1;
    5478              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5479       424537 :   if (as->type == AS_ASSUMED_SIZE
    5480         5768 :       && (/*ar->type == AR_FULL
    5481         5768 :           ||*/ (ar->type == AR_SECTION
    5482          514 :               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
    5483              :     {
    5484            5 :       gfc_error ("Rightmost upper bound of assumed size array section "
    5485              :                  "not specified at %L", &ar->where);
    5486            5 :       return false;
    5487              :     }
    5488              : 
    5489       424532 :   if (ar->type == AR_FULL)
    5490              :     return true;
    5491              : 
    5492       163667 :   if (as->rank != ar->dimen)
    5493              :     {
    5494           28 :       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
    5495              :                  &ar->where, ar->dimen, as->rank);
    5496           28 :       return false;
    5497              :     }
    5498              : 
    5499              :   /* ar->codimen == 0 is a local array.  */
    5500       163639 :   if (as->corank != ar->codimen && ar->codimen != 0)
    5501              :     {
    5502            0 :       gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
    5503              :                  &ar->where, ar->codimen, as->corank);
    5504            0 :       return false;
    5505              :     }
    5506              : 
    5507       369530 :   for (i = 0; i < as->rank; i++)
    5508       205892 :     if (!check_dimension (i, ar, as))
    5509              :       return false;
    5510              : 
    5511              :   /* Local access has no coarray spec.  */
    5512       163638 :   if (ar->codimen != 0)
    5513        18820 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5514              :       {
    5515         9791 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5516         6818 :             && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
    5517              :           {
    5518            2 :             gfc_error ("Coindex of codimension %d must be a scalar at %L",
    5519            2 :                        i + 1 - as->rank, &ar->where);
    5520            2 :             return false;
    5521              :           }
    5522         9789 :         if (!check_dimension (i, ar, as))
    5523              :           return false;
    5524              :       }
    5525              : 
    5526              :   return true;
    5527              : }
    5528              : 
    5529              : 
    5530              : /* Resolve one part of an array index.  */
    5531              : 
    5532              : static bool
    5533       731971 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5534              :                      int force_index_integer_kind)
    5535              : {
    5536       731971 :   gfc_typespec ts;
    5537              : 
    5538       731971 :   if (index == NULL)
    5539              :     return true;
    5540              : 
    5541       217189 :   if (!gfc_resolve_expr (index))
    5542              :     return false;
    5543              : 
    5544       217166 :   if (check_scalar && index->rank != 0)
    5545              :     {
    5546            2 :       gfc_error ("Array index at %L must be scalar", &index->where);
    5547            2 :       return false;
    5548              :     }
    5549              : 
    5550       217164 :   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
    5551              :     {
    5552            4 :       gfc_error ("Array index at %L must be of INTEGER type, found %s",
    5553              :                  &index->where, gfc_basic_typename (index->ts.type));
    5554            4 :       return false;
    5555              :     }
    5556              : 
    5557       217160 :   if (index->ts.type == BT_REAL)
    5558          337 :     if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
    5559              :                          &index->where))
    5560              :       return false;
    5561              : 
    5562       217160 :   if ((index->ts.kind != gfc_index_integer_kind
    5563       212221 :        && force_index_integer_kind)
    5564       185966 :       || (index->ts.type != BT_INTEGER
    5565              :           && index->ts.type != BT_UNKNOWN))
    5566              :     {
    5567        31530 :       gfc_clear_ts (&ts);
    5568        31530 :       ts.type = BT_INTEGER;
    5569        31530 :       ts.kind = gfc_index_integer_kind;
    5570              : 
    5571        31530 :       gfc_convert_type_warn (index, &ts, 2, 0);
    5572              :     }
    5573              : 
    5574              :   return true;
    5575              : }
    5576              : 
    5577              : /* Resolve one part of an array index.  */
    5578              : 
    5579              : bool
    5580       488231 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5581              : {
    5582       488231 :   return gfc_resolve_index_1 (index, check_scalar, 1);
    5583              : }
    5584              : 
    5585              : /* Resolve a dim argument to an intrinsic function.  */
    5586              : 
    5587              : bool
    5588        23915 : gfc_resolve_dim_arg (gfc_expr *dim)
    5589              : {
    5590        23915 :   if (dim == NULL)
    5591              :     return true;
    5592              : 
    5593        23915 :   if (!gfc_resolve_expr (dim))
    5594              :     return false;
    5595              : 
    5596        23915 :   if (dim->rank != 0)
    5597              :     {
    5598            0 :       gfc_error ("Argument dim at %L must be scalar", &dim->where);
    5599            0 :       return false;
    5600              : 
    5601              :     }
    5602              : 
    5603        23915 :   if (dim->ts.type != BT_INTEGER)
    5604              :     {
    5605            0 :       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
    5606            0 :       return false;
    5607              :     }
    5608              : 
    5609        23915 :   if (dim->ts.kind != gfc_index_integer_kind)
    5610              :     {
    5611        15306 :       gfc_typespec ts;
    5612              : 
    5613        15306 :       gfc_clear_ts (&ts);
    5614        15306 :       ts.type = BT_INTEGER;
    5615        15306 :       ts.kind = gfc_index_integer_kind;
    5616              : 
    5617        15306 :       gfc_convert_type_warn (dim, &ts, 2, 0);
    5618              :     }
    5619              : 
    5620              :   return true;
    5621              : }
    5622              : 
    5623              : /* Given an expression that contains array references, update those array
    5624              :    references to point to the right array specifications.  While this is
    5625              :    filled in during matching, this information is difficult to save and load
    5626              :    in a module, so we take care of it here.
    5627              : 
    5628              :    The idea here is that the original array reference comes from the
    5629              :    base symbol.  We traverse the list of reference structures, setting
    5630              :    the stored reference to references.  Component references can
    5631              :    provide an additional array specification.  */
    5632              : static void
    5633              : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
    5634              : 
    5635              : static bool
    5636          909 : find_array_spec (gfc_expr *e)
    5637              : {
    5638          909 :   gfc_array_spec *as;
    5639          909 :   gfc_component *c;
    5640          909 :   gfc_ref *ref;
    5641          909 :   bool class_as = false;
    5642              : 
    5643          909 :   if (e->symtree->n.sym->assoc)
    5644              :     {
    5645          217 :       if (e->symtree->n.sym->assoc->target)
    5646          217 :         gfc_resolve_expr (e->symtree->n.sym->assoc->target);
    5647          217 :       resolve_assoc_var (e->symtree->n.sym, false);
    5648              :     }
    5649              : 
    5650          909 :   if (e->symtree->n.sym->ts.type == BT_CLASS)
    5651              :     {
    5652          124 :       as = CLASS_DATA (e->symtree->n.sym)->as;
    5653          124 :       class_as = true;
    5654              :     }
    5655              :   else
    5656          785 :     as = e->symtree->n.sym->as;
    5657              : 
    5658         2070 :   for (ref = e->ref; ref; ref = ref->next)
    5659         1168 :     switch (ref->type)
    5660              :       {
    5661          911 :       case REF_ARRAY:
    5662          911 :         if (as == NULL)
    5663              :           {
    5664            7 :             locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
    5665           14 :                          ? ref->u.ar.where : e->where);
    5666            7 :             gfc_error ("Invalid array reference of a non-array entity at %L",
    5667              :                        &loc);
    5668            7 :             return false;
    5669              :           }
    5670              : 
    5671          904 :         ref->u.ar.as = as;
    5672          904 :         if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
    5673              :         as = NULL;
    5674              :         break;
    5675              : 
    5676          233 :       case REF_COMPONENT:
    5677          233 :         c = ref->u.c.component;
    5678          233 :         if (c->attr.dimension)
    5679              :           {
    5680          102 :             if (as != NULL && !(class_as && as == c->as))
    5681            0 :               gfc_internal_error ("find_array_spec(): unused as(1)");
    5682          102 :             as = c->as;
    5683              :           }
    5684              : 
    5685              :         break;
    5686              : 
    5687              :       case REF_SUBSTRING:
    5688              :       case REF_INQUIRY:
    5689              :         break;
    5690              :       }
    5691              : 
    5692          902 :   if (as != NULL)
    5693            0 :     gfc_internal_error ("find_array_spec(): unused as(2)");
    5694              : 
    5695              :   return true;
    5696              : }
    5697              : 
    5698              : 
    5699              : /* Resolve an array reference.  */
    5700              : 
    5701              : static bool
    5702       425263 : resolve_array_ref (gfc_array_ref *ar)
    5703              : {
    5704       425263 :   int i, check_scalar;
    5705       425263 :   gfc_expr *e;
    5706              : 
    5707       668974 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5708              :     {
    5709       243740 :       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
    5710              : 
    5711              :       /* Do not force gfc_index_integer_kind for the start.  We can
    5712              :          do fine with any integer kind.  This avoids temporary arrays
    5713              :          created for indexing with a vector.  */
    5714       243740 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5715              :         return false;
    5716       243713 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5717              :         return false;
    5718       243711 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5719              :         return false;
    5720              : 
    5721       243711 :       e = ar->start[i];
    5722              : 
    5723       243711 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5724       145314 :         switch (e->rank)
    5725              :           {
    5726       144222 :           case 0:
    5727       144222 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5728       144222 :             break;
    5729              : 
    5730         1092 :           case 1:
    5731         1092 :             ar->dimen_type[i] = DIMEN_VECTOR;
    5732         1092 :             if (e->expr_type == EXPR_VARIABLE
    5733          470 :                 && e->symtree->n.sym->ts.type == BT_DERIVED)
    5734           13 :               ar->start[i] = gfc_get_parentheses (e);
    5735              :             break;
    5736              : 
    5737            0 :           default:
    5738            0 :             gfc_error ("Array index at %L is an array of rank %d",
    5739              :                        &ar->c_where[i], e->rank);
    5740            0 :             return false;
    5741              :           }
    5742              : 
    5743              :       /* Fill in the upper bound, which may be lower than the
    5744              :          specified one for something like a(2:10:5), which is
    5745              :          identical to a(2:7:5).  Only relevant for strides not equal
    5746              :          to one.  Don't try a division by zero.  */
    5747       243711 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5748        71560 :           && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
    5749         8383 :           && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
    5750         8236 :           && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
    5751              :         {
    5752         8235 :           mpz_t size, end;
    5753              : 
    5754         8235 :           if (gfc_ref_dimen_size (ar, i, &size, &end))
    5755              :             {
    5756         6530 :               if (ar->end[i] == NULL)
    5757              :                 {
    5758         7926 :                   ar->end[i] =
    5759         3963 :                     gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
    5760              :                                            &ar->where);
    5761         3963 :                   mpz_set (ar->end[i]->value.integer, end);
    5762              :                 }
    5763         2567 :               else if (ar->end[i]->ts.type == BT_INTEGER
    5764         2567 :                        && ar->end[i]->expr_type == EXPR_CONSTANT)
    5765              :                 {
    5766         2567 :                   mpz_set (ar->end[i]->value.integer, end);
    5767              :                 }
    5768              :               else
    5769            0 :                 gcc_unreachable ();
    5770              : 
    5771         6530 :               mpz_clear (size);
    5772         6530 :               mpz_clear (end);
    5773              :             }
    5774              :         }
    5775              :     }
    5776              : 
    5777       425234 :   if (ar->type == AR_FULL)
    5778              :     {
    5779       264301 :       if (ar->as->rank == 0)
    5780         3402 :         ar->type = AR_ELEMENT;
    5781              : 
    5782              :       /* Make sure array is the same as array(:,:), this way
    5783              :          we don't need to special case all the time.  */
    5784       264301 :       ar->dimen = ar->as->rank;
    5785       630580 :       for (i = 0; i < ar->dimen; i++)
    5786              :         {
    5787       366279 :           ar->dimen_type[i] = DIMEN_RANGE;
    5788              : 
    5789       366279 :           gcc_assert (ar->start[i] == NULL);
    5790       366279 :           gcc_assert (ar->end[i] == NULL);
    5791       366279 :           gcc_assert (ar->stride[i] == NULL);
    5792              :         }
    5793              :     }
    5794              : 
    5795              :   /* If the reference type is unknown, figure out what kind it is.  */
    5796              : 
    5797       425234 :   if (ar->type == AR_UNKNOWN)
    5798              :     {
    5799       147879 :       ar->type = AR_ELEMENT;
    5800       286481 :       for (i = 0; i < ar->dimen; i++)
    5801       176389 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5802       176389 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5803              :           {
    5804        37787 :             ar->type = AR_SECTION;
    5805        37787 :             break;
    5806              :           }
    5807              :     }
    5808              : 
    5809       425234 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5810              :     return false;
    5811              : 
    5812       425198 :   if (ar->as->corank && ar->codimen == 0)
    5813              :     {
    5814         2075 :       int n;
    5815         2075 :       ar->codimen = ar->as->corank;
    5816         5916 :       for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
    5817         3841 :         ar->dimen_type[n] = DIMEN_THIS_IMAGE;
    5818              :     }
    5819              : 
    5820       425198 :   if (ar->codimen)
    5821              :     {
    5822        13606 :       if (ar->team_type == TEAM_NUMBER)
    5823              :         {
    5824           60 :           if (!gfc_resolve_expr (ar->team))
    5825              :             return false;
    5826              : 
    5827           60 :           if (ar->team->rank != 0)
    5828              :             {
    5829            0 :               gfc_error ("TEAM_NUMBER argument at %L must be scalar",
    5830              :                          &ar->team->where);
    5831            0 :               return false;
    5832              :             }
    5833              : 
    5834           60 :           if (ar->team->ts.type != BT_INTEGER)
    5835              :             {
    5836            6 :               gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
    5837              :                          "type, found %s",
    5838            6 :                          &ar->team->where,
    5839              :                          gfc_basic_typename (ar->team->ts.type));
    5840            6 :               return false;
    5841              :             }
    5842              :         }
    5843        13546 :       else if (ar->team_type == TEAM_TEAM)
    5844              :         {
    5845           42 :           if (!gfc_resolve_expr (ar->team))
    5846              :             return false;
    5847              : 
    5848           42 :           if (ar->team->rank != 0)
    5849              :             {
    5850            3 :               gfc_error ("TEAM argument at %L must be scalar",
    5851              :                          &ar->team->where);
    5852            3 :               return false;
    5853              :             }
    5854              : 
    5855           39 :           if (ar->team->ts.type != BT_DERIVED
    5856           36 :               || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
    5857           36 :               || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
    5858              :             {
    5859            3 :               gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
    5860              :                          "the intrinsic module ISO_FORTRAN_ENV, found %s",
    5861            3 :                          &ar->team->where,
    5862              :                          gfc_basic_typename (ar->team->ts.type));
    5863            3 :               return false;
    5864              :             }
    5865              :         }
    5866        13594 :       if (ar->stat)
    5867              :         {
    5868           62 :           if (!gfc_resolve_expr (ar->stat))
    5869              :             return false;
    5870              : 
    5871           62 :           if (ar->stat->rank != 0)
    5872              :             {
    5873            3 :               gfc_error ("STAT argument at %L must be scalar",
    5874              :                          &ar->stat->where);
    5875            3 :               return false;
    5876              :             }
    5877              : 
    5878           59 :           if (ar->stat->ts.type != BT_INTEGER)
    5879              :             {
    5880            3 :               gfc_error ("STAT argument at %L must be of INTEGER "
    5881              :                          "type, found %s",
    5882            3 :                          &ar->stat->where,
    5883              :                          gfc_basic_typename (ar->stat->ts.type));
    5884            3 :               return false;
    5885              :             }
    5886              : 
    5887           56 :           if (ar->stat->expr_type != EXPR_VARIABLE)
    5888              :             {
    5889            0 :               gfc_error ("STAT's expression at %L must be a variable",
    5890              :                          &ar->stat->where);
    5891            0 :               return false;
    5892              :             }
    5893              :         }
    5894              :     }
    5895              :   return true;
    5896              : }
    5897              : 
    5898              : 
    5899              : bool
    5900         8377 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
    5901              : {
    5902         8377 :   int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
    5903              : 
    5904         8377 :   if (ref->u.ss.start != NULL)
    5905              :     {
    5906         8377 :       if (!gfc_resolve_expr (ref->u.ss.start))
    5907              :         return false;
    5908              : 
    5909         8377 :       if (ref->u.ss.start->ts.type != BT_INTEGER)
    5910              :         {
    5911            1 :           gfc_error ("Substring start index at %L must be of type INTEGER",
    5912              :                      &ref->u.ss.start->where);
    5913            1 :           return false;
    5914              :         }
    5915              : 
    5916         8376 :       if (ref->u.ss.start->rank != 0)
    5917              :         {
    5918            0 :           gfc_error ("Substring start index at %L must be scalar",
    5919              :                      &ref->u.ss.start->where);
    5920            0 :           return false;
    5921              :         }
    5922              : 
    5923         8376 :       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
    5924         8376 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5925           37 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5926              :         {
    5927            1 :           gfc_error ("Substring start index at %L is less than one",
    5928              :                      &ref->u.ss.start->where);
    5929            1 :           return false;
    5930              :         }
    5931              :     }
    5932              : 
    5933         8375 :   if (ref->u.ss.end != NULL)
    5934              :     {
    5935         8181 :       if (!gfc_resolve_expr (ref->u.ss.end))
    5936              :         return false;
    5937              : 
    5938         8181 :       if (ref->u.ss.end->ts.type != BT_INTEGER)
    5939              :         {
    5940            1 :           gfc_error ("Substring end index at %L must be of type INTEGER",
    5941              :                      &ref->u.ss.end->where);
    5942            1 :           return false;
    5943              :         }
    5944              : 
    5945         8180 :       if (ref->u.ss.end->rank != 0)
    5946              :         {
    5947            0 :           gfc_error ("Substring end index at %L must be scalar",
    5948              :                      &ref->u.ss.end->where);
    5949            0 :           return false;
    5950              :         }
    5951              : 
    5952         8180 :       if (ref->u.ss.length != NULL
    5953         7844 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
    5954         8192 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5955           12 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5956              :         {
    5957            4 :           gfc_error ("Substring end index at %L exceeds the string length",
    5958              :                      &ref->u.ss.start->where);
    5959            4 :           return false;
    5960              :         }
    5961              : 
    5962         8176 :       if (compare_bound_mpz_t (ref->u.ss.end,
    5963         8176 :                                gfc_integer_kinds[k].huge) == CMP_GT
    5964         8176 :           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
    5965            7 :               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
    5966              :         {
    5967            4 :           gfc_error ("Substring end index at %L is too large",
    5968              :                      &ref->u.ss.end->where);
    5969            4 :           return false;
    5970              :         }
    5971              :       /*  If the substring has the same length as the original
    5972              :           variable, the reference itself can be deleted.  */
    5973              : 
    5974         8172 :       if (ref->u.ss.length != NULL
    5975         7836 :           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
    5976         9086 :           && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
    5977          228 :         *equal_length = true;
    5978              :     }
    5979              : 
    5980              :   return true;
    5981              : }
    5982              : 
    5983              : 
    5984              : /* This function supplies missing substring charlens.  */
    5985              : 
    5986              : void
    5987         4563 : gfc_resolve_substring_charlen (gfc_expr *e)
    5988              : {
    5989         4563 :   gfc_ref *char_ref;
    5990         4563 :   gfc_expr *start, *end;
    5991         4563 :   gfc_typespec *ts = NULL;
    5992         4563 :   mpz_t diff;
    5993              : 
    5994         8887 :   for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
    5995              :     {
    5996         7041 :       if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
    5997              :         break;
    5998         4324 :       if (char_ref->type == REF_COMPONENT)
    5999          328 :         ts = &char_ref->u.c.component->ts;
    6000              :     }
    6001              : 
    6002         4563 :   if (!char_ref || char_ref->type == REF_INQUIRY)
    6003         1908 :     return;
    6004              : 
    6005         2717 :   gcc_assert (char_ref->next == NULL);
    6006              : 
    6007         2717 :   if (e->ts.u.cl)
    6008              :     {
    6009          120 :       if (e->ts.u.cl->length)
    6010          108 :         gfc_free_expr (e->ts.u.cl->length);
    6011           12 :       else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
    6012              :         return;
    6013              :     }
    6014              : 
    6015         2705 :   if (!e->ts.u.cl)
    6016         2597 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    6017              : 
    6018         2705 :   if (char_ref->u.ss.start)
    6019         2705 :     start = gfc_copy_expr (char_ref->u.ss.start);
    6020              :   else
    6021            0 :     start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
    6022              : 
    6023         2705 :   if (char_ref->u.ss.end)
    6024         2655 :     end = gfc_copy_expr (char_ref->u.ss.end);
    6025           50 :   else if (e->expr_type == EXPR_VARIABLE)
    6026              :     {
    6027           50 :       if (!ts)
    6028           32 :         ts = &e->symtree->n.sym->ts;
    6029           50 :       end = gfc_copy_expr (ts->u.cl->length);
    6030              :     }
    6031              :   else
    6032              :     end = NULL;
    6033              : 
    6034         2705 :   if (!start || !end)
    6035              :     {
    6036           50 :       gfc_free_expr (start);
    6037           50 :       gfc_free_expr (end);
    6038           50 :       return;
    6039              :     }
    6040              : 
    6041              :   /* Length = (end - start + 1).
    6042              :      Check first whether it has a constant length.  */
    6043         2655 :   if (gfc_dep_difference (end, start, &diff))
    6044              :     {
    6045         2539 :       gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
    6046              :                                              &e->where);
    6047              : 
    6048         2539 :       mpz_add_ui (len->value.integer, diff, 1);
    6049         2539 :       mpz_clear (diff);
    6050         2539 :       e->ts.u.cl->length = len;
    6051              :       /* The check for length < 0 is handled below */
    6052              :     }
    6053              :   else
    6054              :     {
    6055          116 :       e->ts.u.cl->length = gfc_subtract (end, start);
    6056          116 :       e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
    6057              :                                     gfc_get_int_expr (gfc_charlen_int_kind,
    6058              :                                                       NULL, 1));
    6059              :     }
    6060              : 
    6061              :   /* F2008, 6.4.1:  Both the starting point and the ending point shall
    6062              :      be within the range 1, 2, ..., n unless the starting point exceeds
    6063              :      the ending point, in which case the substring has length zero.  */
    6064              : 
    6065         2655 :   if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
    6066           15 :     mpz_set_si (e->ts.u.cl->length->value.integer, 0);
    6067              : 
    6068         2655 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    6069         2655 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    6070              : 
    6071              :   /* Make sure that the length is simplified.  */
    6072         2655 :   gfc_simplify_expr (e->ts.u.cl->length, 1);
    6073         2655 :   gfc_resolve_expr (e->ts.u.cl->length);
    6074              : }
    6075              : 
    6076              : 
    6077              : /* Convert an array reference to an array element so that PDT KIND and LEN
    6078              :    or inquiry references are always scalar.  */
    6079              : 
    6080              : static void
    6081           21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
    6082              : {
    6083           21 :   gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
    6084           21 :   int dim;
    6085              : 
    6086           21 :   array_ref->u.ar.type = AR_ELEMENT;
    6087           21 :   expr->rank = 0;
    6088              :   /* Suppress the runtime bounds check.  */
    6089           21 :   expr->no_bounds_check = 1;
    6090           42 :   for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
    6091              :     {
    6092           21 :       array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
    6093           21 :       if (array_ref->u.ar.start[dim])
    6094            0 :         gfc_free_expr (array_ref->u.ar.start[dim]);
    6095              : 
    6096           21 :       if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
    6097            9 :         array_ref->u.ar.start[dim]
    6098            9 :                         = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
    6099              :       else
    6100           12 :         array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
    6101              : 
    6102           21 :       if (array_ref->u.ar.end[dim])
    6103            0 :         gfc_free_expr (array_ref->u.ar.end[dim]);
    6104           21 :       if (array_ref->u.ar.stride[dim])
    6105            0 :         gfc_free_expr (array_ref->u.ar.stride[dim]);
    6106              :     }
    6107           21 :   gfc_free_expr (unity);
    6108           21 : }
    6109              : 
    6110              : 
    6111              : /* Resolve subtype references.  */
    6112              : 
    6113              : bool
    6114       540928 : gfc_resolve_ref (gfc_expr *expr)
    6115              : {
    6116       540928 :   int current_part_dimension, n_components, seen_part_dimension;
    6117       540928 :   gfc_ref *ref, **prev, *array_ref;
    6118       540928 :   bool equal_length;
    6119       540928 :   gfc_symbol *last_pdt = NULL;
    6120              : 
    6121      1062374 :   for (ref = expr->ref; ref; ref = ref->next)
    6122       522355 :     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
    6123              :       {
    6124          909 :         if (!find_array_spec (expr))
    6125              :           return false;
    6126              :         break;
    6127              :       }
    6128              : 
    6129      1585536 :   for (prev = &expr->ref; *prev != NULL;
    6130       522409 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6131       522500 :     switch ((*prev)->type)
    6132              :       {
    6133       425263 :       case REF_ARRAY:
    6134       425263 :         if (!resolve_array_ref (&(*prev)->u.ar))
    6135              :             return false;
    6136              :         break;
    6137              : 
    6138              :       case REF_COMPONENT:
    6139              :       case REF_INQUIRY:
    6140              :         break;
    6141              : 
    6142         8096 :       case REF_SUBSTRING:
    6143         8096 :         equal_length = false;
    6144         8096 :         if (!gfc_resolve_substring (*prev, &equal_length))
    6145              :             return false;
    6146              : 
    6147         8088 :         if (expr->expr_type != EXPR_SUBSTRING && equal_length)
    6148              :           {
    6149              :             /* Remove the reference and move the charlen, if any.  */
    6150          203 :             ref = *prev;
    6151          203 :             *prev = ref->next;
    6152          203 :             ref->next = NULL;
    6153          203 :             expr->ts.u.cl = ref->u.ss.length;
    6154          203 :             ref->u.ss.length = NULL;
    6155          203 :             gfc_free_ref_list (ref);
    6156              :           }
    6157              :         break;
    6158              :       }
    6159              : 
    6160              :   /* Check constraints on part references.  */
    6161              : 
    6162       540830 :   current_part_dimension = 0;
    6163       540830 :   seen_part_dimension = 0;
    6164       540830 :   n_components = 0;
    6165       540830 :   array_ref = NULL;
    6166              : 
    6167       540830 :   if (expr->expr_type == EXPR_VARIABLE && IS_PDT (expr))
    6168          534 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6169              : 
    6170      1063011 :   for (ref = expr->ref; ref; ref = ref->next)
    6171              :     {
    6172       522192 :       switch (ref->type)
    6173              :         {
    6174       425173 :         case REF_ARRAY:
    6175       425173 :           array_ref = ref;
    6176       425173 :           switch (ref->u.ar.type)
    6177              :             {
    6178       260897 :             case AR_FULL:
    6179              :               /* Coarray scalar.  */
    6180       260897 :               if (ref->u.ar.as->rank == 0)
    6181              :                 {
    6182              :                   current_part_dimension = 0;
    6183              :                   break;
    6184              :                 }
    6185              :               /* Fall through.  */
    6186       301748 :             case AR_SECTION:
    6187       301748 :               current_part_dimension = 1;
    6188       301748 :               break;
    6189              : 
    6190       123425 :             case AR_ELEMENT:
    6191       123425 :               array_ref = NULL;
    6192       123425 :               current_part_dimension = 0;
    6193       123425 :               break;
    6194              : 
    6195            0 :             case AR_UNKNOWN:
    6196            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6197              :             }
    6198              : 
    6199              :           break;
    6200              : 
    6201        88337 :         case REF_COMPONENT:
    6202        88337 :           if (current_part_dimension || seen_part_dimension)
    6203              :             {
    6204              :               /* F03:C614.  */
    6205         6846 :               if (ref->u.c.component->attr.pointer
    6206         6843 :                   || ref->u.c.component->attr.proc_pointer
    6207         6842 :                   || (ref->u.c.component->ts.type == BT_CLASS
    6208            1 :                         && CLASS_DATA (ref->u.c.component)->attr.pointer))
    6209              :                 {
    6210            4 :                   gfc_error ("Component to the right of a part reference "
    6211              :                              "with nonzero rank must not have the POINTER "
    6212              :                              "attribute at %L", &expr->where);
    6213            4 :                   return false;
    6214              :                 }
    6215         6842 :               else if (ref->u.c.component->attr.allocatable
    6216         6836 :                         || (ref->u.c.component->ts.type == BT_CLASS
    6217            1 :                             && CLASS_DATA (ref->u.c.component)->attr.allocatable))
    6218              : 
    6219              :                 {
    6220            7 :                   gfc_error ("Component to the right of a part reference "
    6221              :                              "with nonzero rank must not have the ALLOCATABLE "
    6222              :                              "attribute at %L", &expr->where);
    6223            7 :                   return false;
    6224              :                 }
    6225              :             }
    6226              : 
    6227              :           /* Sometimes the component in a component reference is that of the
    6228              :              pdt_template. Point to the component of pdt_type instead. This
    6229              :              ensures that the component gets a backend_decl in translation.  */
    6230        88326 :           if (last_pdt)
    6231              :             {
    6232          501 :               gfc_component *cmp = last_pdt->components;
    6233         1207 :               for (; cmp; cmp = cmp->next)
    6234         1202 :                 if (!strcmp (cmp->name, ref->u.c.component->name))
    6235              :                   {
    6236          496 :                     ref->u.c.component = cmp;
    6237          496 :                     break;
    6238              :                   }
    6239          501 :               ref->u.c.sym = last_pdt;
    6240              :             }
    6241              : 
    6242              :           /* Convert pdt_templates, if necessary, and update 'last_pdt'.  */
    6243        88326 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6244              :             {
    6245        20671 :               if (ref->u.c.component->ts.u.derived->attr.pdt_template)
    6246              :                 {
    6247            0 :                   if (gfc_get_pdt_instance (ref->u.c.component->param_list,
    6248              :                                             &ref->u.c.component->ts.u.derived,
    6249              :                                             NULL) != MATCH_YES)
    6250              :                     return false;
    6251            0 :                   last_pdt = ref->u.c.component->ts.u.derived;
    6252              :                 }
    6253        20671 :               else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
    6254          521 :                 last_pdt = ref->u.c.component->ts.u.derived;
    6255              :               else
    6256              :                 last_pdt = NULL;
    6257              :             }
    6258              : 
    6259              :           /* The F08 standard requires(See R425, R431, R435, and in particular
    6260              :              Note 6.7) that a PDT parameter reference be a scalar even if
    6261              :              the designator is an array."  */
    6262        88326 :           if (array_ref && last_pdt && last_pdt->attr.pdt_type
    6263           83 :               && (ref->u.c.component->attr.pdt_kind
    6264           83 :                   || ref->u.c.component->attr.pdt_len))
    6265            7 :             reset_array_ref_to_scalar (expr, array_ref);
    6266              : 
    6267        88326 :           n_components++;
    6268        88326 :           break;
    6269              : 
    6270              :         case REF_SUBSTRING:
    6271              :           break;
    6272              : 
    6273          797 :         case REF_INQUIRY:
    6274              :           /* Implement requirement in note 9.7 of F2018 that the result of the
    6275              :              LEN inquiry be a scalar.  */
    6276          797 :           if (ref->u.i == INQUIRY_LEN && array_ref
    6277           40 :               && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
    6278           40 :                   || expr->ts.type == BT_INTEGER))
    6279           14 :             reset_array_ref_to_scalar (expr, array_ref);
    6280              :           break;
    6281              :         }
    6282              : 
    6283       522181 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6284       508940 :            || ref->next == NULL)
    6285              :           && current_part_dimension
    6286       458331 :           && seen_part_dimension)
    6287              :         {
    6288            0 :           gfc_error ("Two or more part references with nonzero rank must "
    6289              :                      "not be specified at %L", &expr->where);
    6290            0 :           return false;
    6291              :         }
    6292              : 
    6293       522181 :       if (ref->type == REF_COMPONENT)
    6294              :         {
    6295        88326 :           if (current_part_dimension)
    6296         6648 :             seen_part_dimension = 1;
    6297              : 
    6298              :           /* reset to make sure */
    6299              :           current_part_dimension = 0;
    6300              :         }
    6301              :     }
    6302              : 
    6303              :   return true;
    6304              : }
    6305              : 
    6306              : 
    6307              : /* Given an expression, determine its shape.  This is easier than it sounds.
    6308              :    Leaves the shape array NULL if it is not possible to determine the shape.  */
    6309              : 
    6310              : static void
    6311      2589695 : expression_shape (gfc_expr *e)
    6312              : {
    6313      2589695 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6314      2589695 :   int i;
    6315              : 
    6316      2589695 :   if (e->rank <= 0 || e->shape != NULL)
    6317      2414800 :     return;
    6318              : 
    6319       702172 :   for (i = 0; i < e->rank; i++)
    6320       474113 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6321       174895 :       goto fail;
    6322              : 
    6323       228059 :   e->shape = gfc_get_shape (e->rank);
    6324              : 
    6325       228059 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6326              : 
    6327       228059 :   return;
    6328              : 
    6329       174895 : fail:
    6330       176566 :   for (i--; i >= 0; i--)
    6331         1671 :     mpz_clear (array[i]);
    6332              : }
    6333              : 
    6334              : 
    6335              : /* Given a variable expression node, compute the rank of the expression by
    6336              :    examining the base symbol and any reference structures it may have.  */
    6337              : 
    6338              : void
    6339      2589695 : gfc_expression_rank (gfc_expr *e)
    6340              : {
    6341      2589695 :   gfc_ref *ref, *last_arr_ref = nullptr;
    6342      2589695 :   int i, rank, corank;
    6343              : 
    6344              :   /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
    6345              :      could lead to serious confusion...  */
    6346      2589695 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6347              : 
    6348      2589695 :   if (e->ref == NULL)
    6349              :     {
    6350      1910300 :       if (e->expr_type == EXPR_ARRAY)
    6351        71379 :         goto done;
    6352              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6353              : 
    6354      1838921 :       if (e->symtree != NULL)
    6355              :         {
    6356              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6357      1838909 :           gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
    6358        13805 :                                 && e->symtree->n.sym->ts.u.derived
    6359        13800 :                                 && CLASS_DATA (e->symtree->n.sym))
    6360      1838909 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6361              :                                  : e->symtree->n.sym->as;
    6362      1838909 :           if (as)
    6363              :             {
    6364          589 :               e->rank = as->rank;
    6365          589 :               e->corank = as->corank;
    6366          589 :               goto done;
    6367              :             }
    6368              :         }
    6369      1838332 :       e->rank = 0;
    6370      1838332 :       e->corank = 0;
    6371      1838332 :       goto done;
    6372              :     }
    6373              : 
    6374              :   rank = 0;
    6375              :   corank = 0;
    6376              : 
    6377      1072729 :   for (ref = e->ref; ref; ref = ref->next)
    6378              :     {
    6379       784620 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
    6380          553 :           && ref->u.c.component->attr.function && !ref->next)
    6381              :         {
    6382          357 :           rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
    6383          357 :           corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
    6384              :         }
    6385              : 
    6386       784620 :       if (ref->type != REF_ARRAY)
    6387       155489 :         continue;
    6388              : 
    6389       629131 :       last_arr_ref = ref;
    6390       629131 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6391              :         {
    6392       345815 :           rank = ref->u.ar.as->rank;
    6393       345815 :           break;
    6394              :         }
    6395              : 
    6396       283316 :       if (ref->u.ar.type == AR_SECTION)
    6397              :         {
    6398              :           /* Figure out the rank of the section.  */
    6399        45471 :           if (rank != 0)
    6400            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6401              : 
    6402       113440 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6403        67969 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6404        67969 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6405        59263 :               rank++;
    6406              : 
    6407              :           break;
    6408              :         }
    6409              :     }
    6410       679395 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6411       609720 :       && last_arr_ref->u.ar.as->rank != -1)
    6412              :     {
    6413        19265 :       for (i = last_arr_ref->u.ar.as->rank;
    6414       620847 :            i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
    6415              :         {
    6416              :           /* For unknown dimen in non-resolved as assume full corank.  */
    6417        20152 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6418        19588 :               || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
    6419          323 :                   && !last_arr_ref->u.ar.as->resolved))
    6420              :             {
    6421              :               corank = last_arr_ref->u.ar.as->corank;
    6422              :               break;
    6423              :             }
    6424        19265 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6425        19265 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6426        19167 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6427        16676 :             corank++;
    6428         2589 :           else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
    6429            0 :             gfc_internal_error ("Illegal coarray index");
    6430              :         }
    6431              :     }
    6432              : 
    6433       679395 :   e->rank = rank;
    6434       679395 :   e->corank = corank;
    6435              : 
    6436      2589695 : done:
    6437      2589695 :   expression_shape (e);
    6438      2589695 : }
    6439              : 
    6440              : 
    6441              : /* Given two expressions, check that their rank is conformable, i.e. either
    6442              :    both have the same rank or at least one is a scalar.  */
    6443              : 
    6444              : bool
    6445     12197360 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6446              : {
    6447     12197360 :   if (op1->expr_type == EXPR_VARIABLE)
    6448       730621 :     gfc_expression_rank (op1);
    6449     12197360 :   if (op2->expr_type == EXPR_VARIABLE)
    6450       446242 :     gfc_expression_rank (op2);
    6451              : 
    6452        76074 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6453     12273108 :          && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
    6454           30 :              || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
    6455              : }
    6456              : 
    6457              : /* Resolve a variable expression.  */
    6458              : 
    6459              : static bool
    6460      1322842 : resolve_variable (gfc_expr *e)
    6461              : {
    6462      1322842 :   gfc_symbol *sym;
    6463      1322842 :   bool t;
    6464              : 
    6465      1322842 :   t = true;
    6466              : 
    6467      1322842 :   if (e->symtree == NULL)
    6468              :     return false;
    6469      1322397 :   sym = e->symtree->n.sym;
    6470              : 
    6471              :   /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
    6472              :      as ts.type is set to BT_ASSUMED in resolve_symbol.  */
    6473      1322397 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    6474              :     {
    6475          183 :       if (!actual_arg || inquiry_argument)
    6476              :         {
    6477            2 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
    6478              :                      "be used as actual argument", sym->name, &e->where);
    6479            2 :           return false;
    6480              :         }
    6481              :     }
    6482              :   /* TS 29113, 407b.  */
    6483      1322214 :   else if (e->ts.type == BT_ASSUMED)
    6484              :     {
    6485          571 :       if (!actual_arg)
    6486              :         {
    6487           20 :           gfc_error ("Assumed-type variable %s at %L may only be used "
    6488              :                      "as actual argument", sym->name, &e->where);
    6489           20 :           return false;
    6490              :         }
    6491          551 :       else if (inquiry_argument && !first_actual_arg)
    6492              :         {
    6493              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6494              :              for all inquiry functions in resolve_function; the reason is
    6495              :              that the function-name resolution happens too late in that
    6496              :              function.  */
    6497            0 :           gfc_error ("Assumed-type variable %s at %L as actual argument to "
    6498              :                      "an inquiry function shall be the first argument",
    6499              :                      sym->name, &e->where);
    6500            0 :           return false;
    6501              :         }
    6502              :     }
    6503              :   /* TS 29113, C535b.  */
    6504      1321643 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6505        37146 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6506        37141 :              && CLASS_DATA (sym)->as
    6507        14516 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6508      1320733 :             || (sym->ts.type != BT_CLASS && sym->as
    6509       362500 :                 && sym->as->type == AS_ASSUMED_RANK))
    6510         7930 :            && !sym->attr.select_rank_temporary
    6511         7930 :            && !(sym->assoc && sym->assoc->ar))
    6512              :     {
    6513         7930 :       if (!actual_arg
    6514         1253 :           && !(cs_base && cs_base->current
    6515         1252 :                && (cs_base->current->op == EXEC_SELECT_RANK
    6516          188 :                    || sym->attr.target)))
    6517              :         {
    6518          144 :           gfc_error ("Assumed-rank variable %s at %L may only be used as "
    6519              :                      "actual argument", sym->name, &e->where);
    6520          144 :           return false;
    6521              :         }
    6522         7786 :       else if (inquiry_argument && !first_actual_arg)
    6523              :         {
    6524              :           /* FIXME: It doesn't work reliably as inquiry_argument is not set
    6525              :              for all inquiry functions in resolve_function; the reason is
    6526              :              that the function-name resolution happens too late in that
    6527              :              function.  */
    6528            0 :           gfc_error ("Assumed-rank variable %s at %L as actual argument "
    6529              :                      "to an inquiry function shall be the first argument",
    6530              :                      sym->name, &e->where);
    6531            0 :           return false;
    6532              :         }
    6533              :     }
    6534              : 
    6535      1322231 :   if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
    6536          181 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6537          180 :            && e->ref->next == NULL))
    6538              :     {
    6539            1 :       gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
    6540              :                  "a subobject reference", sym->name, &e->ref->u.ar.where);
    6541            1 :       return false;
    6542              :     }
    6543              :   /* TS 29113, 407b.  */
    6544      1322230 :   else if (e->ts.type == BT_ASSUMED && e->ref
    6545          687 :            && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6546          680 :                 && e->ref->next == NULL))
    6547              :     {
    6548            7 :       gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
    6549              :                  "reference", sym->name, &e->ref->u.ar.where);
    6550            7 :       return false;
    6551              :     }
    6552              : 
    6553              :   /* TS 29113, C535b.  */
    6554      1322223 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6555        37146 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6556        37141 :         && CLASS_DATA (sym)->as
    6557        14516 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6558      1321313 :        || (sym->ts.type != BT_CLASS && sym->as
    6559       363036 :            && sym->as->type == AS_ASSUMED_RANK))
    6560         8070 :       && !(sym->assoc && sym->assoc->ar)
    6561         8070 :       && e->ref
    6562         8070 :       && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
    6563         8066 :            && e->ref->next == NULL))
    6564              :     {
    6565            4 :       gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
    6566              :                  "reference", sym->name, &e->ref->u.ar.where);
    6567            4 :       return false;
    6568              :     }
    6569              : 
    6570              :   /* Guessed type variables are associate_names whose selector had not been
    6571              :      parsed at the time that the construct was parsed. Now the namespace is
    6572              :      being resolved, the TKR of the selector will be available for fixup of
    6573              :      the associate_name.  */
    6574      1322219 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6575              :     {
    6576          384 :       gfc_fixup_inferred_type_refs (e);
    6577              :       /* KIND inquiry ref returns the kind of the target.  */
    6578          384 :       if (e->expr_type == EXPR_CONSTANT)
    6579              :         return true;
    6580              :     }
    6581      1321835 :   else if (sym->attr.select_type_temporary
    6582         8936 :            && sym->ns->assoc_name_inferred)
    6583           92 :     gfc_fixup_inferred_type_refs (e);
    6584              : 
    6585              :   /* For variables that are used in an associate (target => object) where
    6586              :      the object's basetype is array valued while the target is scalar,
    6587              :      the ts' type of the component refs is still array valued, which
    6588              :      can't be translated that way.  */
    6589      1322207 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6590          603 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6591          603 :       && sym->assoc->target->ts.u.derived
    6592          603 :       && CLASS_DATA (sym->assoc->target)
    6593          603 :       && CLASS_DATA (sym->assoc->target)->as)
    6594              :     {
    6595              :       gfc_ref *ref = e->ref;
    6596          697 :       while (ref)
    6597              :         {
    6598          539 :           switch (ref->type)
    6599              :             {
    6600          236 :             case REF_COMPONENT:
    6601          236 :               ref->u.c.sym = sym->ts.u.derived;
    6602              :               /* Stop the loop.  */
    6603          236 :               ref = NULL;
    6604          236 :               break;
    6605          303 :             default:
    6606          303 :               ref = ref->next;
    6607          303 :               break;
    6608              :             }
    6609              :         }
    6610              :     }
    6611              : 
    6612              :   /* If this is an associate-name, it may be parsed with an array reference
    6613              :      in error even though the target is scalar.  Fail directly in this case.
    6614              :      TODO Understand why class scalar expressions must be excluded.  */
    6615      1322207 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6616              :     {
    6617        11394 :       if (sym->ts.type == BT_CLASS)
    6618          242 :         gfc_fix_class_refs (e);
    6619        11394 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6620         2085 :           && e->ref->type == REF_ARRAY)
    6621              :         {
    6622              :           /* Unambiguously scalar!  */
    6623            3 :           if (sym->assoc->target
    6624            3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6625            1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6626            2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6627              :                        sym->name, &e->where);
    6628            3 :           return false;
    6629              :         }
    6630        11391 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6631         6977 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6632              :         {
    6633              :           /* This can happen because the parser did not detect that the
    6634              :              associate name is an array and the expression had no array
    6635              :              part_ref.  */
    6636          147 :           gfc_ref *ref = gfc_get_ref ();
    6637          147 :           ref->type = REF_ARRAY;
    6638          147 :           ref->u.ar.type = AR_FULL;
    6639          147 :           if (sym->as)
    6640              :             {
    6641          146 :               ref->u.ar.as = sym->as;
    6642          146 :               ref->u.ar.dimen = sym->as->rank;
    6643              :             }
    6644          147 :           ref->next = e->ref;
    6645          147 :           e->ref = ref;
    6646              :         }
    6647              :     }
    6648              : 
    6649      1322204 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6650            0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6651              : 
    6652              :   /* On the other hand, the parser may not have known this is an array;
    6653              :      in this case, we have to add a FULL reference.  */
    6654      1322204 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6655              :     {
    6656            0 :       e->ref = gfc_get_ref ();
    6657            0 :       e->ref->type = REF_ARRAY;
    6658            0 :       e->ref->u.ar.type = AR_FULL;
    6659            0 :       e->ref->u.ar.dimen = 0;
    6660              :     }
    6661              : 
    6662              :   /* Like above, but for class types, where the checking whether an array
    6663              :      ref is present is more complicated.  Furthermore make sure not to add
    6664              :      the full array ref to _vptr or _len refs.  */
    6665      1322204 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6666         1012 :       && CLASS_DATA (sym)
    6667         1012 :       && (CLASS_DATA (sym)->attr.dimension
    6668          443 :           || CLASS_DATA (sym)->attr.codimension)
    6669          575 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6670              :     {
    6671          551 :       gfc_ref *ref, *newref;
    6672              : 
    6673          551 :       newref = gfc_get_ref ();
    6674          551 :       newref->type = REF_ARRAY;
    6675          551 :       newref->u.ar.type = AR_FULL;
    6676          551 :       newref->u.ar.dimen = 0;
    6677              : 
    6678              :       /* Because this is an associate var and the first ref either is a ref to
    6679              :          the _data component or not, no traversal of the ref chain is
    6680              :          needed.  The array ref needs to be inserted after the _data ref,
    6681              :          or when that is not present, which may happened for polymorphic
    6682              :          types, then at the first position.  */
    6683          551 :       ref = e->ref;
    6684          551 :       if (!ref)
    6685           18 :         e->ref = newref;
    6686          533 :       else if (ref->type == REF_COMPONENT
    6687          230 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6688              :         {
    6689          230 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6690              :             {
    6691           12 :               newref->next = ref->next;
    6692           12 :               ref->next = newref;
    6693              :             }
    6694              :           else
    6695              :             /* Array ref present already.  */
    6696          218 :             gfc_free_ref_list (newref);
    6697              :         }
    6698          303 :       else if (ref->type == REF_ARRAY)
    6699              :         /* Array ref present already.  */
    6700          303 :         gfc_free_ref_list (newref);
    6701              :       else
    6702              :         {
    6703            0 :           newref->next = ref;
    6704            0 :           e->ref = newref;
    6705              :         }
    6706              :     }
    6707      1321653 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6708              :     {
    6709          486 :       gfc_ref *ref;
    6710          910 :       for (ref = e->ref; ref; ref = ref->next)
    6711          454 :         if (ref->type == REF_SUBSTRING)
    6712              :           break;
    6713          486 :       if (ref == NULL)
    6714          456 :         e->ts = sym->ts;
    6715              :     }
    6716              : 
    6717      1322204 :   if (e->ref && !gfc_resolve_ref (e))
    6718              :     return false;
    6719              : 
    6720      1322111 :   if (sym->attr.flavor == FL_PROCEDURE
    6721        31598 :       && (!sym->attr.function
    6722        18562 :           || (sym->attr.function && sym->result
    6723        18114 :               && sym->result->attr.proc_pointer
    6724          713 :               && !sym->result->attr.function)))
    6725              :     {
    6726        13036 :       e->ts.type = BT_PROCEDURE;
    6727        13036 :       goto resolve_procedure;
    6728              :     }
    6729              : 
    6730      1309075 :   if (sym->ts.type != BT_UNKNOWN)
    6731      1308430 :     gfc_variable_attr (e, &e->ts);
    6732          645 :   else if (sym->attr.flavor == FL_PROCEDURE
    6733           12 :            && sym->attr.function && sym->result
    6734           12 :            && sym->result->ts.type != BT_UNKNOWN
    6735           10 :            && sym->result->attr.proc_pointer)
    6736           10 :     e->ts = sym->result->ts;
    6737              :   else
    6738              :     {
    6739              :       /* Must be a simple variable reference.  */
    6740          635 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6741              :         return false;
    6742          509 :       e->ts = sym->ts;
    6743              :     }
    6744              : 
    6745      1308949 :   if (check_assumed_size_reference (sym, e))
    6746              :     return false;
    6747              : 
    6748              :   /* Deal with forward references to entries during gfc_resolve_code, to
    6749              :      satisfy, at least partially, 12.5.2.5.  */
    6750      1308930 :   if (gfc_current_ns->entries
    6751         3181 :       && current_entry_id == sym->entry_id
    6752         1050 :       && cs_base
    6753          964 :       && cs_base->current
    6754          964 :       && cs_base->current->op != EXEC_ENTRY)
    6755              :     {
    6756          964 :       int n;
    6757          964 :       bool saved_specification_expr;
    6758          964 :       gfc_symbol *saved_specification_expr_symbol;
    6759              : 
    6760              :       /* If the symbol is a dummy...  */
    6761          964 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6762              :         {
    6763              :           /*  If it has not been seen as a dummy, this is an error.  */
    6764          462 :           if (!entry_dummy_seen_p (sym))
    6765              :             {
    6766            5 :               if (specification_expr
    6767            4 :                   && specification_expr_symbol
    6768            4 :                   && specification_expr_symbol->attr.dummy
    6769            2 :                   && specification_expr_symbol->ns == gfc_current_ns
    6770            7 :                   && !entry_dummy_seen_p (specification_expr_symbol))
    6771              :                 ;
    6772            3 :               else if (specification_expr)
    6773            2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6774              :                            ", is referenced at %L before the ENTRY statement "
    6775              :                            "in which it is a parameter",
    6776              :                            sym->name, &cs_base->current->loc);
    6777              :               else
    6778            1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6779              :                            "statement in which it is a parameter",
    6780              :                            sym->name, &cs_base->current->loc);
    6781              :               t = false;
    6782              :             }
    6783              :         }
    6784              : 
    6785              :       /* Now do the same check on the specification expressions.  */
    6786          964 :       saved_specification_expr = specification_expr;
    6787          964 :       saved_specification_expr_symbol = specification_expr_symbol;
    6788          964 :       specification_expr = true;
    6789          964 :       specification_expr_symbol = sym;
    6790          964 :       if (sym->ts.type == BT_CHARACTER
    6791          964 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6792              :         t = false;
    6793              : 
    6794          964 :       if (sym->as)
    6795              :         {
    6796          279 :           for (n = 0; n < sym->as->rank; n++)
    6797              :             {
    6798          164 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6799            0 :                 t = false;
    6800          164 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6801            1 :                 t = false;
    6802              :             }
    6803              :         }
    6804          964 :       specification_expr = saved_specification_expr;
    6805          964 :       specification_expr_symbol = saved_specification_expr_symbol;
    6806              : 
    6807          964 :       if (t)
    6808              :         /* Update the symbol's entry level.  */
    6809          957 :         sym->entry_id = current_entry_id + 1;
    6810              :     }
    6811              : 
    6812              :   /* If a symbol has been host_associated mark it.  This is used latter,
    6813              :      to identify if aliasing is possible via host association.  */
    6814      1308930 :   if (sym->attr.flavor == FL_VARIABLE
    6815      1270848 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6816         6040 :           || !sym->ns->code->ext.block.assoc)
    6817      1268872 :       && gfc_current_ns->parent
    6818       603342 :       && (gfc_current_ns->parent == sym->ns
    6819       565415 :           || (gfc_current_ns->parent->parent
    6820        11288 :               && gfc_current_ns->parent->parent == sym->ns)))
    6821        44551 :     sym->attr.host_assoc = 1;
    6822              : 
    6823      1308930 :   if (gfc_current_ns->proc_name
    6824      1304882 :       && sym->attr.dimension
    6825       356517 :       && (sym->ns != gfc_current_ns
    6826       332346 :           || sym->attr.use_assoc
    6827       328359 :           || sym->attr.in_common))
    6828        32946 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6829              : 
    6830      1321966 : resolve_procedure:
    6831      1321966 :   if (t && !resolve_procedure_expression (e))
    6832              :     t = false;
    6833              : 
    6834              :   /* F2008, C617 and C1229.  */
    6835      1320926 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6836      1419862 :       && gfc_is_coindexed (e))
    6837              :     {
    6838          356 :       gfc_ref *ref, *ref2 = NULL;
    6839              : 
    6840          439 :       for (ref = e->ref; ref; ref = ref->next)
    6841              :         {
    6842          439 :           if (ref->type == REF_COMPONENT)
    6843           83 :             ref2 = ref;
    6844          439 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6845              :             break;
    6846              :         }
    6847              : 
    6848          712 :       for ( ; ref; ref = ref->next)
    6849          368 :         if (ref->type == REF_COMPONENT)
    6850              :           break;
    6851              : 
    6852              :       /* Expression itself is not coindexed object.  */
    6853          356 :       if (ref && e->ts.type == BT_CLASS)
    6854              :         {
    6855            3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6856              :                      &e->where);
    6857            3 :           t = false;
    6858              :         }
    6859              : 
    6860              :       /* Expression itself is coindexed object.  */
    6861          344 :       if (ref == NULL)
    6862              :         {
    6863          344 :           gfc_component *c;
    6864          344 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6865          464 :           for ( ; c; c = c->next)
    6866          120 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6867              :               {
    6868            0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6869              :                          "subcomponent at %L", &e->where);
    6870            0 :                 t = false;
    6871            0 :                 break;
    6872              :               }
    6873              :         }
    6874              :     }
    6875              : 
    6876      1321966 :   if (t)
    6877      1321956 :     gfc_expression_rank (e);
    6878              : 
    6879      1321966 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6880            3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6881              :                  "Using variable %qs at %L is deprecated",
    6882              :                  sym->name, &e->where);
    6883              :   /* Simplify cases where access to a parameter array results in a
    6884              :      single constant.  Suppress errors since those will have been
    6885              :      issued before, as warnings.  */
    6886      1321966 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    6887              :     {
    6888         2727 :       gfc_push_suppress_errors ();
    6889         2727 :       gfc_simplify_expr (e, 1);
    6890         2727 :       gfc_pop_suppress_errors ();
    6891              :     }
    6892              : 
    6893              :   return t;
    6894              : }
    6895              : 
    6896              : 
    6897              : /* 'sym' was initially guessed to be derived type but has been corrected
    6898              :    in resolve_assoc_var to be a class entity or the derived type correcting.
    6899              :    If a class entity it will certainly need the _data reference or the
    6900              :    reference derived type symbol correcting in the first component ref if
    6901              :    a derived type.  */
    6902              : 
    6903              : void
    6904          880 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6905              : {
    6906          880 :   gfc_ref *ref, *new_ref;
    6907          880 :   gfc_symbol *sym, *derived;
    6908          880 :   gfc_expr *target;
    6909          880 :   sym = e->symtree->n.sym;
    6910              : 
    6911              :   /* An associate_name whose selector is (i) a component ref of a selector
    6912              :      that is a inferred type associate_name; or (ii) an intrinsic type that
    6913              :      has been inferred from an inquiry ref.  */
    6914          880 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    6915              :     {
    6916          282 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    6917          282 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    6918          282 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    6919              :         {
    6920           60 :           ref = e->ref;
    6921              :           /* A substring misidentified as an array section.  */
    6922           60 :           if (sym->ts.type == BT_CHARACTER
    6923           30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    6924            6 :               && !ref->u.ar.stride[0])
    6925              :             {
    6926            6 :               new_ref = gfc_get_ref ();
    6927            6 :               new_ref->type = REF_SUBSTRING;
    6928            6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    6929            6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    6930            6 :               new_ref->u.ss.length = sym->ts.u.cl;
    6931            6 :               *ref = *new_ref;
    6932            6 :               free (new_ref);
    6933              :             }
    6934              :           else
    6935              :             {
    6936           54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    6937           24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    6938           54 :               e->ref = ref->next;
    6939           54 :               free (ref);
    6940              :             }
    6941              :         }
    6942              : 
    6943              :       /* It is possible for an inquiry reference to be mistaken for a
    6944              :          component reference. Correct this now.  */
    6945          282 :       ref = e->ref;
    6946          282 :       if (ref && ref->type == REF_ARRAY)
    6947          138 :         ref = ref->next;
    6948          150 :       if (ref && ref->type == REF_COMPONENT
    6949          150 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    6950              :         {
    6951           12 :           e->symtree->n.sym = sym;
    6952           12 :           *ref = *new_ref;
    6953           12 :           gfc_free_ref_list (new_ref);
    6954              :         }
    6955              : 
    6956              :       /* The kind of the associate name is best evaluated directly from the
    6957              :          selector because of the guesses made in primary.cc, when the type
    6958              :          is still unknown.  */
    6959          282 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    6960              :         {
    6961           24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    6962           12 :                                            sym->assoc->target->ts.kind);
    6963           12 :           gfc_replace_expr (e, ne);
    6964              :         }
    6965              : 
    6966              :       /* Now that the references are all sorted out, set the expression rank
    6967              :          and return.  */
    6968          282 :       gfc_expression_rank (e);
    6969          282 :       return;
    6970              :     }
    6971              : 
    6972          598 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    6973              :                                      : sym->ts.u.derived;
    6974              : 
    6975              :   /* Ensure that class symbols have an array spec and ensure that there
    6976              :      is a _data field reference following class type references.  */
    6977          598 :   if (sym->ts.type == BT_CLASS
    6978          196 :       && sym->assoc->target->ts.type == BT_CLASS)
    6979              :     {
    6980          196 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    6981          196 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    6982          196 :       sym->attr.dimension = 0;
    6983          196 :       sym->attr.codimension = 0;
    6984          196 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    6985          196 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    6986          196 :       if (e->ref && (e->ref->type != REF_COMPONENT
    6987          160 :                      || e->ref->u.c.component->name[0] != '_'))
    6988              :         {
    6989           82 :           ref = gfc_get_ref ();
    6990           82 :           ref->type = REF_COMPONENT;
    6991           82 :           ref->next = e->ref;
    6992           82 :           e->ref = ref;
    6993           82 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    6994              :                                                    true, true, NULL);
    6995           82 :           ref->u.c.sym = sym->ts.u.derived;
    6996              :         }
    6997              :     }
    6998              : 
    6999              :   /* Proceed as far as the first component reference and ensure that the
    7000              :      correct derived type is being used.  */
    7001          861 :   for (ref = e->ref; ref; ref = ref->next)
    7002          825 :     if (ref->type == REF_COMPONENT)
    7003              :       {
    7004          562 :         if (ref->u.c.component->name[0] != '_')
    7005          366 :           ref->u.c.sym = derived;
    7006              :         else
    7007          196 :           ref->u.c.sym = sym->ts.u.derived;
    7008              :         break;
    7009              :       }
    7010              : 
    7011              :   /* Verify that the type inferrence mechanism has not introduced a spurious
    7012              :      array reference.  This can happen with an associate name, whose selector
    7013              :      is an element of another inferred type.  */
    7014          598 :   target = e->symtree->n.sym->assoc->target;
    7015          598 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7016          186 :       && e != target && !target->rank)
    7017              :     {
    7018              :       /* First case: array ref after the scalar class or derived
    7019              :          associate_name.  */
    7020          186 :       if (e->ref && e->ref->type == REF_ARRAY
    7021            7 :           && e->ref->u.ar.type != AR_ELEMENT)
    7022              :         {
    7023            7 :           ref = e->ref;
    7024            7 :           if (ref->u.ar.type == AR_UNKNOWN)
    7025            1 :             gfc_error ("Invalid array reference at %L", &e->where);
    7026            7 :           e->ref = ref->next;
    7027            7 :           free (ref);
    7028              : 
    7029              :           /* If it hasn't a ref to the '_data' field supply one.  */
    7030            7 :           if (sym->ts.type == BT_CLASS
    7031            0 :               && !(e->ref->type == REF_COMPONENT
    7032            0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    7033              :             {
    7034            0 :               gfc_ref *new_ref;
    7035            0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    7036              :                                   "_data", true, true, &new_ref);
    7037            0 :               new_ref->next = e->ref;
    7038            0 :               e->ref = new_ref;
    7039              :             }
    7040              :         }
    7041              :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    7042          179 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7043          179 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    7044           64 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    7045            0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    7046              :         {
    7047            0 :           ref = e->ref->next;
    7048            0 :           if (ref->u.ar.type == AR_UNKNOWN)
    7049            0 :             gfc_error ("Invalid array reference at %L", &e->where);
    7050            0 :           e->ref->next = e->ref->next->next;
    7051            0 :           free (ref);
    7052              :         }
    7053              :     }
    7054              : 
    7055              :   /* Now that all the references are OK, get the expression rank.  */
    7056          598 :   gfc_expression_rank (e);
    7057              : }
    7058              : 
    7059              : 
    7060              : /* Checks to see that the correct symbol has been host associated.
    7061              :    The only situations where this arises are:
    7062              :         (i)  That in which a twice contained function is parsed after
    7063              :              the host association is made. On detecting this, change
    7064              :              the symbol in the expression and convert the array reference
    7065              :              into an actual arglist if the old symbol is a variable; or
    7066              :         (ii) That in which an external function is typed but not declared
    7067              :              explicitly to be external. Here, the old symbol is changed
    7068              :              from a variable to an external function.  */
    7069              : static bool
    7070      1666567 : check_host_association (gfc_expr *e)
    7071              : {
    7072      1666567 :   gfc_symbol *sym, *old_sym;
    7073      1666567 :   gfc_symtree *st;
    7074      1666567 :   int n;
    7075      1666567 :   gfc_ref *ref;
    7076      1666567 :   gfc_actual_arglist *arg, *tail = NULL;
    7077      1666567 :   bool retval = e->expr_type == EXPR_FUNCTION;
    7078              : 
    7079              :   /*  If the expression is the result of substitution in
    7080              :       interface.cc(gfc_extend_expr) because there is no way in
    7081              :       which the host association can be wrong.  */
    7082      1666567 :   if (e->symtree == NULL
    7083      1665768 :         || e->symtree->n.sym == NULL
    7084      1665768 :         || e->user_operator)
    7085              :     return retval;
    7086              : 
    7087      1664003 :   old_sym = e->symtree->n.sym;
    7088              : 
    7089      1664003 :   if (gfc_current_ns->parent
    7090       728061 :         && old_sym->ns != gfc_current_ns)
    7091              :     {
    7092              :       /* Use the 'USE' name so that renamed module symbols are
    7093              :          correctly handled.  */
    7094        90733 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7095              : 
    7096        90733 :       if (sym && old_sym != sym
    7097          679 :               && sym->attr.flavor == FL_PROCEDURE
    7098          105 :               && sym->attr.contained)
    7099              :         {
    7100              :           /* Clear the shape, since it might not be valid.  */
    7101           83 :           gfc_free_shape (&e->shape, e->rank);
    7102              : 
    7103              :           /* Give the expression the right symtree!  */
    7104           83 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    7105           83 :           gcc_assert (st != NULL);
    7106              : 
    7107           83 :           if (old_sym->attr.flavor == FL_PROCEDURE
    7108           59 :                 || e->expr_type == EXPR_FUNCTION)
    7109              :             {
    7110              :               /* Original was function so point to the new symbol, since
    7111              :                  the actual argument list is already attached to the
    7112              :                  expression.  */
    7113           30 :               e->value.function.esym = NULL;
    7114           30 :               e->symtree = st;
    7115              :             }
    7116              :           else
    7117              :             {
    7118              :               /* Original was variable so convert array references into
    7119              :                  an actual arglist. This does not need any checking now
    7120              :                  since resolve_function will take care of it.  */
    7121           53 :               e->value.function.actual = NULL;
    7122           53 :               e->expr_type = EXPR_FUNCTION;
    7123           53 :               e->symtree = st;
    7124              : 
    7125              :               /* Ambiguity will not arise if the array reference is not
    7126              :                  the last reference.  */
    7127           55 :               for (ref = e->ref; ref; ref = ref->next)
    7128           38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    7129              :                   break;
    7130              : 
    7131           53 :               if ((ref == NULL || ref->type != REF_ARRAY)
    7132           17 :                   && sym->attr.proc == PROC_INTERNAL)
    7133              :                 {
    7134            4 :                   gfc_error ("%qs at %L is host associated at %L into "
    7135              :                              "a contained procedure with an internal "
    7136              :                              "procedure of the same name", sym->name,
    7137              :                               &old_sym->declared_at, &e->where);
    7138            4 :                   return false;
    7139              :                 }
    7140              : 
    7141           13 :               if (ref == NULL)
    7142              :                 return false;
    7143              : 
    7144           36 :               gcc_assert (ref->type == REF_ARRAY);
    7145              : 
    7146              :               /* Grab the start expressions from the array ref and
    7147              :                  copy them into actual arguments.  */
    7148           84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7149              :                 {
    7150           48 :                   arg = gfc_get_actual_arglist ();
    7151           48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    7152           48 :                   if (e->value.function.actual == NULL)
    7153           36 :                     tail = e->value.function.actual = arg;
    7154              :                   else
    7155              :                     {
    7156           12 :                       tail->next = arg;
    7157           12 :                       tail = arg;
    7158              :                     }
    7159              :                 }
    7160              : 
    7161              :               /* Dump the reference list and set the rank.  */
    7162           36 :               gfc_free_ref_list (e->ref);
    7163           36 :               e->ref = NULL;
    7164           36 :               e->rank = sym->as ? sym->as->rank : 0;
    7165           36 :               e->corank = sym->as ? sym->as->corank : 0;
    7166              :             }
    7167              : 
    7168           66 :           gfc_resolve_expr (e);
    7169           66 :           sym->refs++;
    7170              :         }
    7171              :       /* This case corresponds to a call, from a block or a contained
    7172              :          procedure, to an external function, which has not been declared
    7173              :          as being external in the main program but has been typed.  */
    7174        90650 :       else if (sym && old_sym != sym
    7175          596 :                && !e->ref
    7176          328 :                && sym->ts.type == BT_UNKNOWN
    7177           21 :                && old_sym->ts.type != BT_UNKNOWN
    7178           19 :                && sym->attr.flavor == FL_PROCEDURE
    7179           19 :                && old_sym->attr.flavor == FL_VARIABLE
    7180            7 :                && sym->ns->parent == old_sym->ns
    7181            7 :                && sym->ns->proc_name
    7182            7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    7183            6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    7184            6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    7185              :         {
    7186            6 :           old_sym->attr.flavor = FL_PROCEDURE;
    7187            6 :           old_sym->attr.external = 1;
    7188            6 :           old_sym->attr.function = 1;
    7189            6 :           old_sym->result = old_sym;
    7190            6 :           gfc_resolve_expr (e);
    7191              :         }
    7192              :     }
    7193              :   /* This might have changed!  */
    7194      1663986 :   return e->expr_type == EXPR_FUNCTION;
    7195              : }
    7196              : 
    7197              : 
    7198              : static void
    7199         1442 : gfc_resolve_character_operator (gfc_expr *e)
    7200              : {
    7201         1442 :   gfc_expr *op1 = e->value.op.op1;
    7202         1442 :   gfc_expr *op2 = e->value.op.op2;
    7203         1442 :   gfc_expr *e1 = NULL;
    7204         1442 :   gfc_expr *e2 = NULL;
    7205              : 
    7206         1442 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7207              : 
    7208         1442 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7209          761 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7210          681 :   else if (op1->expr_type == EXPR_CONSTANT)
    7211          268 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7212          268 :                            op1->value.character.length);
    7213              : 
    7214         1442 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7215          749 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7216          693 :   else if (op2->expr_type == EXPR_CONSTANT)
    7217          462 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7218          462 :                            op2->value.character.length);
    7219              : 
    7220         1442 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7221              : 
    7222         1442 :   if (!e1 || !e2)
    7223              :     {
    7224          541 :       gfc_free_expr (e1);
    7225          541 :       gfc_free_expr (e2);
    7226              : 
    7227          541 :       return;
    7228              :     }
    7229              : 
    7230          901 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7231          901 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7232          901 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7233          901 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7234          901 :   gfc_resolve_expr (e->ts.u.cl->length);
    7235              : 
    7236          901 :   return;
    7237              : }
    7238              : 
    7239              : 
    7240              : /*  Ensure that an character expression has a charlen and, if possible, a
    7241              :     length expression.  */
    7242              : 
    7243              : static void
    7244       180450 : fixup_charlen (gfc_expr *e)
    7245              : {
    7246              :   /* The cases fall through so that changes in expression type and the need
    7247              :      for multiple fixes are picked up.  In all circumstances, a charlen should
    7248              :      be available for the middle end to hang a backend_decl on.  */
    7249       180450 :   switch (e->expr_type)
    7250              :     {
    7251         1442 :     case EXPR_OP:
    7252         1442 :       gfc_resolve_character_operator (e);
    7253              :       /* FALLTHRU */
    7254              : 
    7255         1509 :     case EXPR_ARRAY:
    7256         1509 :       if (e->expr_type == EXPR_ARRAY)
    7257           67 :         gfc_resolve_character_array_constructor (e);
    7258              :       /* FALLTHRU */
    7259              : 
    7260         1966 :     case EXPR_SUBSTRING:
    7261         1966 :       if (!e->ts.u.cl && e->ref)
    7262          453 :         gfc_resolve_substring_charlen (e);
    7263              :       /* FALLTHRU */
    7264              : 
    7265       180450 :     default:
    7266       180450 :       if (!e->ts.u.cl)
    7267       178488 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7268              : 
    7269       180450 :       break;
    7270              :     }
    7271       180450 : }
    7272              : 
    7273              : 
    7274              : /* Update an actual argument to include the passed-object for type-bound
    7275              :    procedures at the right position.  */
    7276              : 
    7277              : static gfc_actual_arglist*
    7278         2962 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7279              :                      const char *name)
    7280              : {
    7281         2986 :   gcc_assert (argpos > 0);
    7282              : 
    7283         2986 :   if (argpos == 1)
    7284              :     {
    7285         2837 :       gfc_actual_arglist* result;
    7286              : 
    7287         2837 :       result = gfc_get_actual_arglist ();
    7288         2837 :       result->expr = po;
    7289         2837 :       result->next = lst;
    7290         2837 :       if (name)
    7291          514 :         result->name = name;
    7292              : 
    7293         2837 :       return result;
    7294              :     }
    7295              : 
    7296          149 :   if (lst)
    7297          125 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    7298              :   else
    7299           24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    7300              :   return lst;
    7301              : }
    7302              : 
    7303              : 
    7304              : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    7305              : 
    7306              : static gfc_expr*
    7307         7202 : extract_compcall_passed_object (gfc_expr* e)
    7308              : {
    7309         7202 :   gfc_expr* po;
    7310              : 
    7311         7202 :   if (e->expr_type == EXPR_UNKNOWN)
    7312              :     {
    7313            0 :       gfc_error ("Error in typebound call at %L",
    7314              :                  &e->where);
    7315            0 :       return NULL;
    7316              :     }
    7317              : 
    7318         7202 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7319              : 
    7320         7202 :   if (e->value.compcall.base_object)
    7321         1584 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7322              :   else
    7323              :     {
    7324         5618 :       po = gfc_get_expr ();
    7325         5618 :       po->expr_type = EXPR_VARIABLE;
    7326         5618 :       po->symtree = e->symtree;
    7327         5618 :       po->ref = gfc_copy_ref (e->ref);
    7328         5618 :       po->where = e->where;
    7329              :     }
    7330              : 
    7331         7202 :   if (!gfc_resolve_expr (po))
    7332              :     return NULL;
    7333              : 
    7334              :   return po;
    7335              : }
    7336              : 
    7337              : 
    7338              : /* Update the arglist of an EXPR_COMPCALL expression to include the
    7339              :    passed-object.  */
    7340              : 
    7341              : static bool
    7342         3321 : update_compcall_arglist (gfc_expr* e)
    7343              : {
    7344         3321 :   gfc_expr* po;
    7345         3321 :   gfc_typebound_proc* tbp;
    7346              : 
    7347         3321 :   tbp = e->value.compcall.tbp;
    7348              : 
    7349         3321 :   if (tbp->error)
    7350              :     return false;
    7351              : 
    7352         3320 :   po = extract_compcall_passed_object (e);
    7353         3320 :   if (!po)
    7354              :     return false;
    7355              : 
    7356         3320 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7357              :     {
    7358         1116 :       gfc_free_expr (po);
    7359         1116 :       return true;
    7360              :     }
    7361              : 
    7362         2204 :   if (tbp->pass_arg_num <= 0)
    7363              :     return false;
    7364              : 
    7365         2203 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7366              :                                                   tbp->pass_arg_num,
    7367              :                                                   tbp->pass_arg);
    7368              : 
    7369         2203 :   return true;
    7370              : }
    7371              : 
    7372              : 
    7373              : /* Extract the passed object from a PPC call (a copy of it).  */
    7374              : 
    7375              : static gfc_expr*
    7376           85 : extract_ppc_passed_object (gfc_expr *e)
    7377              : {
    7378           85 :   gfc_expr *po;
    7379           85 :   gfc_ref **ref;
    7380              : 
    7381           85 :   po = gfc_get_expr ();
    7382           85 :   po->expr_type = EXPR_VARIABLE;
    7383           85 :   po->symtree = e->symtree;
    7384           85 :   po->ref = gfc_copy_ref (e->ref);
    7385           85 :   po->where = e->where;
    7386              : 
    7387              :   /* Remove PPC reference.  */
    7388           85 :   ref = &po->ref;
    7389           91 :   while ((*ref)->next)
    7390            6 :     ref = &(*ref)->next;
    7391           85 :   gfc_free_ref_list (*ref);
    7392           85 :   *ref = NULL;
    7393              : 
    7394           85 :   if (!gfc_resolve_expr (po))
    7395            0 :     return NULL;
    7396              : 
    7397              :   return po;
    7398              : }
    7399              : 
    7400              : 
    7401              : /* Update the actual arglist of a procedure pointer component to include the
    7402              :    passed-object.  */
    7403              : 
    7404              : static bool
    7405          574 : update_ppc_arglist (gfc_expr* e)
    7406              : {
    7407          574 :   gfc_expr* po;
    7408          574 :   gfc_component *ppc;
    7409          574 :   gfc_typebound_proc* tb;
    7410              : 
    7411          574 :   ppc = gfc_get_proc_ptr_comp (e);
    7412          574 :   if (!ppc)
    7413              :     return false;
    7414              : 
    7415          574 :   tb = ppc->tb;
    7416              : 
    7417          574 :   if (tb->error)
    7418              :     return false;
    7419          572 :   else if (tb->nopass)
    7420              :     return true;
    7421              : 
    7422           85 :   po = extract_ppc_passed_object (e);
    7423           85 :   if (!po)
    7424              :     return false;
    7425              : 
    7426              :   /* F08:R739.  */
    7427           85 :   if (po->rank != 0)
    7428              :     {
    7429            0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7430            0 :       return false;
    7431              :     }
    7432              : 
    7433              :   /* F08:C611.  */
    7434           85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7435              :     {
    7436            1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7437              :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7438            1 :       return false;
    7439              :     }
    7440              : 
    7441           84 :   gcc_assert (tb->pass_arg_num > 0);
    7442           84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7443              :                                                   tb->pass_arg_num,
    7444              :                                                   tb->pass_arg);
    7445              : 
    7446           84 :   return true;
    7447              : }
    7448              : 
    7449              : 
    7450              : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7451              :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7452              : 
    7453              : static bool
    7454         3332 : check_typebound_baseobject (gfc_expr* e)
    7455              : {
    7456         3332 :   gfc_expr* base;
    7457         3332 :   bool return_value = false;
    7458              : 
    7459         3332 :   base = extract_compcall_passed_object (e);
    7460         3332 :   if (!base)
    7461              :     return false;
    7462              : 
    7463         3329 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7464              :     {
    7465            1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7466            1 :       goto cleanup;
    7467              :     }
    7468              : 
    7469         3328 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7470            1 :     return false;
    7471              : 
    7472              :   /* F08:C611.  */
    7473         3327 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7474              :     {
    7475            3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7476              :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7477            3 :       goto cleanup;
    7478              :     }
    7479              : 
    7480              :   /* F08:C1230. If the procedure called is NOPASS,
    7481              :      the base object must be scalar.  */
    7482         3324 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7483              :     {
    7484            1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7485              :                  " be scalar", &e->where);
    7486            1 :       goto cleanup;
    7487              :     }
    7488              : 
    7489              :   return_value = true;
    7490              : 
    7491         3328 : cleanup:
    7492         3328 :   gfc_free_expr (base);
    7493         3328 :   return return_value;
    7494              : }
    7495              : 
    7496              : 
    7497              : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7498              :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7499              :    arglist and the target-procedure symtree are returned.  */
    7500              : 
    7501              : static bool
    7502         3321 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7503              :                           gfc_actual_arglist** actual)
    7504              : {
    7505         3321 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7506         3321 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7507              : 
    7508              :   /* Update the actual arglist for PASS.  */
    7509         3321 :   if (!update_compcall_arglist (e))
    7510              :     return false;
    7511              : 
    7512         3319 :   *actual = e->value.compcall.actual;
    7513         3319 :   *target = e->value.compcall.tbp->u.specific;
    7514              : 
    7515         3319 :   gfc_free_ref_list (e->ref);
    7516         3319 :   e->ref = NULL;
    7517         3319 :   e->value.compcall.actual = NULL;
    7518              : 
    7519              :   /* If we find a deferred typebound procedure, check for derived types
    7520              :      that an overriding typebound procedure has not been missed.  */
    7521         3319 :   if (e->value.compcall.name
    7522         3319 :       && !e->value.compcall.tbp->non_overridable
    7523         3301 :       && e->value.compcall.base_object
    7524          792 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7525              :     {
    7526          499 :       gfc_symtree *st;
    7527          499 :       gfc_symbol *derived;
    7528              : 
    7529              :       /* Use the derived type of the base_object.  */
    7530          499 :       derived = e->value.compcall.base_object->ts.u.derived;
    7531          499 :       st = NULL;
    7532              : 
    7533              :       /* If necessary, go through the inheritance chain.  */
    7534         1505 :       while (!st && derived)
    7535              :         {
    7536              :           /* Look for the typebound procedure 'name'.  */
    7537          507 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7538          499 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7539              :                                    e->value.compcall.name);
    7540          507 :           if (!st)
    7541            8 :             derived = gfc_get_derived_super_type (derived);
    7542              :         }
    7543              : 
    7544              :       /* Now find the specific name in the derived type namespace.  */
    7545          499 :       if (st && st->n.tb && st->n.tb->u.specific)
    7546          499 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7547          499 :                            derived->ns, 1, &st);
    7548          499 :       if (st)
    7549          499 :         *target = st;
    7550              :     }
    7551              : 
    7552         3319 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7553         3319 :       && !e->value.compcall.tbp->deferred)
    7554            1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7555              :                  " itself recursively.  Declare it RECURSIVE or use"
    7556              :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7557              : 
    7558              :   return true;
    7559              : }
    7560              : 
    7561              : 
    7562              : /* Get the ultimate declared type from an expression.  In addition,
    7563              :    return the last class/derived type reference and the copy of the
    7564              :    reference list.  If check_types is set true, derived types are
    7565              :    identified as well as class references.  */
    7566              : static gfc_symbol*
    7567         3263 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7568              :                         gfc_expr *e, bool check_types)
    7569              : {
    7570         3263 :   gfc_symbol *declared;
    7571         3263 :   gfc_ref *ref;
    7572              : 
    7573         3263 :   declared = NULL;
    7574         3263 :   if (class_ref)
    7575         2855 :     *class_ref = NULL;
    7576         3263 :   if (new_ref)
    7577         2562 :     *new_ref = gfc_copy_ref (e->ref);
    7578              : 
    7579         4058 :   for (ref = e->ref; ref; ref = ref->next)
    7580              :     {
    7581          795 :       if (ref->type != REF_COMPONENT)
    7582          292 :         continue;
    7583              : 
    7584          503 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7585          256 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7586          428 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7587              :         {
    7588          354 :           declared = ref->u.c.component->ts.u.derived;
    7589          354 :           if (class_ref)
    7590          332 :             *class_ref = ref;
    7591              :         }
    7592              :     }
    7593              : 
    7594         3263 :   if (declared == NULL)
    7595         2935 :     declared = e->symtree->n.sym->ts.u.derived;
    7596              : 
    7597         3263 :   return declared;
    7598              : }
    7599              : 
    7600              : 
    7601              : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7602              :    which of the specific bindings (if any) matches the arglist and transform
    7603              :    the expression into a call of that binding.  */
    7604              : 
    7605              : static bool
    7606         3323 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7607              : {
    7608         3323 :   gfc_typebound_proc* genproc;
    7609         3323 :   const char* genname;
    7610         3323 :   gfc_symtree *st;
    7611         3323 :   gfc_symbol *derived;
    7612              : 
    7613         3323 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7614         3323 :   genname = e->value.compcall.name;
    7615         3323 :   genproc = e->value.compcall.tbp;
    7616              : 
    7617         3323 :   if (!genproc->is_generic)
    7618              :     return true;
    7619              : 
    7620              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7621          420 :   for (; genproc; genproc = genproc->overridden)
    7622              :     {
    7623          418 :       gfc_tbp_generic* g;
    7624              : 
    7625          418 :       gcc_assert (genproc->is_generic);
    7626          646 :       for (g = genproc->u.generic; g; g = g->next)
    7627              :         {
    7628          636 :           gfc_symbol* target;
    7629          636 :           gfc_actual_arglist* args;
    7630          636 :           bool matches;
    7631              : 
    7632          636 :           gcc_assert (g->specific);
    7633              : 
    7634          636 :           if (g->specific->error)
    7635            0 :             continue;
    7636              : 
    7637          636 :           target = g->specific->u.specific->n.sym;
    7638              : 
    7639              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7640          636 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7641          636 :           if (!g->specific->nopass)
    7642              :             {
    7643          550 :               gfc_expr* po;
    7644          550 :               po = extract_compcall_passed_object (e);
    7645          550 :               if (!po)
    7646              :                 {
    7647            0 :                   gfc_free_actual_arglist (args);
    7648            0 :                   return false;
    7649              :                 }
    7650              : 
    7651          550 :               gcc_assert (g->specific->pass_arg_num > 0);
    7652          550 :               gcc_assert (!g->specific->error);
    7653          550 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7654              :                                           g->specific->pass_arg);
    7655              :             }
    7656          636 :           resolve_actual_arglist (args, target->attr.proc,
    7657          636 :                                   is_external_proc (target)
    7658          636 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7659              : 
    7660              :           /* Check if this arglist matches the formal.  */
    7661          636 :           matches = gfc_arglist_matches_symbol (&args, target);
    7662              : 
    7663              :           /* Clean up and break out of the loop if we've found it.  */
    7664          636 :           gfc_free_actual_arglist (args);
    7665          636 :           if (matches)
    7666              :             {
    7667          408 :               e->value.compcall.tbp = g->specific;
    7668          408 :               genname = g->specific_st->name;
    7669              :               /* Pass along the name for CLASS methods, where the vtab
    7670              :                  procedure pointer component has to be referenced.  */
    7671          408 :               if (name)
    7672          161 :                 *name = genname;
    7673          408 :               goto success;
    7674              :             }
    7675              :         }
    7676              :     }
    7677              : 
    7678              :   /* Nothing matching found!  */
    7679            2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7680              :              " %qs at %L", genname, &e->where);
    7681            2 :   return false;
    7682              : 
    7683          408 : success:
    7684              :   /* Make sure that we have the right specific instance for the name.  */
    7685          408 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7686              : 
    7687          408 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7688          408 :   if (st)
    7689          408 :     e->value.compcall.tbp = st->n.tb;
    7690              : 
    7691              :   return true;
    7692              : }
    7693              : 
    7694              : 
    7695              : /* Resolve a call to a type-bound subroutine.  */
    7696              : 
    7697              : static bool
    7698         1724 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7699              : {
    7700         1724 :   gfc_actual_arglist* newactual;
    7701         1724 :   gfc_symtree* target;
    7702              : 
    7703              :   /* Check that's really a SUBROUTINE.  */
    7704         1724 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7705              :     {
    7706           17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7707           15 :           && c->expr1->value.compcall.tbp->u.specific
    7708           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7709           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7710           12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7711              :       else
    7712              :         {
    7713            5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7714              :                      c->expr1->value.compcall.name, &c->loc);
    7715            5 :           return false;
    7716              :         }
    7717              :     }
    7718              : 
    7719         1719 :   if (!check_typebound_baseobject (c->expr1))
    7720              :     return false;
    7721              : 
    7722              :   /* Pass along the name for CLASS methods, where the vtab
    7723              :      procedure pointer component has to be referenced.  */
    7724         1712 :   if (name)
    7725          480 :     *name = c->expr1->value.compcall.name;
    7726              : 
    7727         1712 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7728              :     return false;
    7729              : 
    7730              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7731         1711 :   if (overridable)
    7732          371 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7733              : 
    7734              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7735              : 
    7736         1711 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7737              :     return false;
    7738              : 
    7739         1709 :   c->ext.actual = newactual;
    7740         1709 :   c->symtree = target;
    7741         1709 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7742              : 
    7743         1709 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7744              : 
    7745         1709 :   gfc_free_expr (c->expr1);
    7746         1709 :   c->expr1 = gfc_get_expr ();
    7747         1709 :   c->expr1->expr_type = EXPR_FUNCTION;
    7748         1709 :   c->expr1->symtree = target;
    7749         1709 :   c->expr1->where = c->loc;
    7750              : 
    7751         1709 :   return resolve_call (c);
    7752              : }
    7753              : 
    7754              : 
    7755              : /* Resolve a component-call expression.  */
    7756              : static bool
    7757         1632 : resolve_compcall (gfc_expr* e, const char **name)
    7758              : {
    7759         1632 :   gfc_actual_arglist* newactual;
    7760         1632 :   gfc_symtree* target;
    7761              : 
    7762              :   /* Check that's really a FUNCTION.  */
    7763         1632 :   if (!e->value.compcall.tbp->function)
    7764              :     {
    7765           19 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    7766            5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    7767              :                    &e->where);
    7768           19 :       return false;
    7769              :     }
    7770              : 
    7771              : 
    7772              :   /* These must not be assign-calls!  */
    7773         1613 :   gcc_assert (!e->value.compcall.assign);
    7774              : 
    7775         1613 :   if (!check_typebound_baseobject (e))
    7776              :     return false;
    7777              : 
    7778              :   /* Pass along the name for CLASS methods, where the vtab
    7779              :      procedure pointer component has to be referenced.  */
    7780         1611 :   if (name)
    7781          864 :     *name = e->value.compcall.name;
    7782              : 
    7783         1611 :   if (!resolve_typebound_generic_call (e, name))
    7784              :     return false;
    7785         1610 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7786              : 
    7787              :   /* Take the rank from the function's symbol.  */
    7788         1610 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    7789              :     {
    7790          155 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    7791          155 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    7792              :     }
    7793              : 
    7794              :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    7795              :      arglist to the TBP's binding target.  */
    7796              : 
    7797         1610 :   if (!resolve_typebound_static (e, &target, &newactual))
    7798              :     return false;
    7799              : 
    7800         1610 :   e->value.function.actual = newactual;
    7801         1610 :   e->value.function.name = NULL;
    7802         1610 :   e->value.function.esym = target->n.sym;
    7803         1610 :   e->value.function.isym = NULL;
    7804         1610 :   e->symtree = target;
    7805         1610 :   e->ts = target->n.sym->ts;
    7806         1610 :   e->expr_type = EXPR_FUNCTION;
    7807              : 
    7808              :   /* Resolution is not necessary if this is a class subroutine; this
    7809              :      function only has to identify the specific proc. Resolution of
    7810              :      the call will be done next in resolve_typebound_call.  */
    7811         1610 :   return gfc_resolve_expr (e);
    7812              : }
    7813              : 
    7814              : 
    7815              : static bool resolve_fl_derived (gfc_symbol *sym);
    7816              : 
    7817              : 
    7818              : /* Resolve a typebound function, or 'method'. First separate all
    7819              :    the non-CLASS references by calling resolve_compcall directly.  */
    7820              : 
    7821              : static bool
    7822         1632 : resolve_typebound_function (gfc_expr* e)
    7823              : {
    7824         1632 :   gfc_symbol *declared;
    7825         1632 :   gfc_component *c;
    7826         1632 :   gfc_ref *new_ref;
    7827         1632 :   gfc_ref *class_ref;
    7828         1632 :   gfc_symtree *st;
    7829         1632 :   const char *name;
    7830         1632 :   gfc_typespec ts;
    7831         1632 :   gfc_expr *expr;
    7832         1632 :   bool overridable;
    7833              : 
    7834         1632 :   st = e->symtree;
    7835              : 
    7836              :   /* Deal with typebound operators for CLASS objects.  */
    7837         1632 :   expr = e->value.compcall.base_object;
    7838         1632 :   overridable = !e->value.compcall.tbp->non_overridable;
    7839         1632 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    7840              :     {
    7841              :       /* Since the typebound operators are generic, we have to ensure
    7842              :          that any delays in resolution are corrected and that the vtab
    7843              :          is present.  */
    7844          184 :       ts = expr->ts;
    7845          184 :       declared = ts.u.derived;
    7846          184 :       if (!resolve_fl_derived (declared))
    7847              :         return false;
    7848              : 
    7849          184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7850          184 :       if (c->ts.u.derived == NULL)
    7851            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7852              : 
    7853          184 :       if (!resolve_compcall (e, &name))
    7854              :         return false;
    7855              : 
    7856              :       /* Use the generic name if it is there.  */
    7857          184 :       name = name ? name : e->value.function.esym->name;
    7858          184 :       e->symtree = expr->symtree;
    7859          184 :       e->ref = gfc_copy_ref (expr->ref);
    7860          184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    7861              : 
    7862              :       /* Trim away the extraneous references that emerge from nested
    7863              :          use of interface.cc (extend_expr).  */
    7864          184 :       if (class_ref && class_ref->next)
    7865              :         {
    7866            0 :           gfc_free_ref_list (class_ref->next);
    7867            0 :           class_ref->next = NULL;
    7868              :         }
    7869          184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    7870              :         {
    7871            0 :           gfc_free_ref_list (e->ref);
    7872            0 :           e->ref = NULL;
    7873              :         }
    7874              : 
    7875          184 :       gfc_add_vptr_component (e);
    7876          184 :       gfc_add_component_ref (e, name);
    7877          184 :       e->value.function.esym = NULL;
    7878          184 :       if (expr->expr_type != EXPR_VARIABLE)
    7879           80 :         e->base_expr = expr;
    7880          184 :       return true;
    7881              :     }
    7882              : 
    7883         1448 :   if (st == NULL)
    7884          159 :     return resolve_compcall (e, NULL);
    7885              : 
    7886         1289 :   if (!gfc_resolve_ref (e))
    7887              :     return false;
    7888              : 
    7889              :   /* It can happen that a generic, typebound procedure is marked as overridable
    7890              :      with all of the specific procedures being non-overridable. If this is the
    7891              :      case, it is safe to resolve the compcall.  */
    7892         1289 :   if (!expr && overridable
    7893         1281 :       && e->value.compcall.tbp->is_generic
    7894          186 :       && e->value.compcall.tbp->u.generic->specific
    7895          185 :       && e->value.compcall.tbp->u.generic->specific->non_overridable)
    7896              :     {
    7897              :       gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
    7898            6 :       for (; g; g = g->next)
    7899            4 :         if (!g->specific->non_overridable)
    7900              :           break;
    7901            2 :       if (g == NULL && resolve_compcall (e, &name))
    7902              :         return true;
    7903              :     }
    7904              : 
    7905              :   /* Get the CLASS declared type.  */
    7906         1287 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7907              : 
    7908         1287 :   if (!resolve_fl_derived (declared))
    7909              :     return false;
    7910              : 
    7911              :   /* Weed out cases of the ultimate component being a derived type.  */
    7912         1287 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7913         1193 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7914              :     {
    7915          595 :       gfc_free_ref_list (new_ref);
    7916          595 :       return resolve_compcall (e, NULL);
    7917              :     }
    7918              : 
    7919          692 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    7920              : 
    7921              :   /* Treat the call as if it is a typebound procedure, in order to roll
    7922              :      out the correct name for the specific function.  */
    7923          692 :   if (!resolve_compcall (e, &name))
    7924              :     {
    7925           15 :       gfc_free_ref_list (new_ref);
    7926           15 :       return false;
    7927              :     }
    7928          677 :   ts = e->ts;
    7929              : 
    7930          677 :   if (overridable)
    7931              :     {
    7932              :       /* Convert the expression to a procedure pointer component call.  */
    7933          675 :       e->value.function.esym = NULL;
    7934          675 :       e->symtree = st;
    7935              : 
    7936          675 :       if (new_ref)
    7937          125 :         e->ref = new_ref;
    7938              : 
    7939              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7940          675 :       gfc_add_vptr_component (e);
    7941          675 :       gfc_add_component_ref (e, name);
    7942              : 
    7943              :       /* Recover the typespec for the expression.  This is really only
    7944              :         necessary for generic procedures, where the additional call
    7945              :         to gfc_add_component_ref seems to throw the collection of the
    7946              :         correct typespec.  */
    7947          675 :       e->ts = ts;
    7948              :     }
    7949            2 :   else if (new_ref)
    7950            0 :     gfc_free_ref_list (new_ref);
    7951              : 
    7952              :   return true;
    7953              : }
    7954              : 
    7955              : /* Resolve a typebound subroutine, or 'method'. First separate all
    7956              :    the non-CLASS references by calling resolve_typebound_call
    7957              :    directly.  */
    7958              : 
    7959              : static bool
    7960         1724 : resolve_typebound_subroutine (gfc_code *code)
    7961              : {
    7962         1724 :   gfc_symbol *declared;
    7963         1724 :   gfc_component *c;
    7964         1724 :   gfc_ref *new_ref;
    7965         1724 :   gfc_ref *class_ref;
    7966         1724 :   gfc_symtree *st;
    7967         1724 :   const char *name;
    7968         1724 :   gfc_typespec ts;
    7969         1724 :   gfc_expr *expr;
    7970         1724 :   bool overridable;
    7971              : 
    7972         1724 :   st = code->expr1->symtree;
    7973              : 
    7974              :   /* Deal with typebound operators for CLASS objects.  */
    7975         1724 :   expr = code->expr1->value.compcall.base_object;
    7976         1724 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7977         1724 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    7978              :     {
    7979              :       /* If the base_object is not a variable, the corresponding actual
    7980              :          argument expression must be stored in e->base_expression so
    7981              :          that the corresponding tree temporary can be used as the base
    7982              :          object in gfc_conv_procedure_call.  */
    7983          109 :       if (expr->expr_type != EXPR_VARIABLE)
    7984              :         {
    7985              :           gfc_actual_arglist *args;
    7986              : 
    7987              :           args= code->expr1->value.function.actual;
    7988              :           for (; args; args = args->next)
    7989              :             if (expr == args->expr)
    7990              :               expr = args->expr;
    7991              :         }
    7992              : 
    7993              :       /* Since the typebound operators are generic, we have to ensure
    7994              :          that any delays in resolution are corrected and that the vtab
    7995              :          is present.  */
    7996          109 :       declared = expr->ts.u.derived;
    7997          109 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7998          109 :       if (c->ts.u.derived == NULL)
    7999            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8000              : 
    8001          109 :       if (!resolve_typebound_call (code, &name, NULL))
    8002              :         return false;
    8003              : 
    8004              :       /* Use the generic name if it is there.  */
    8005          109 :       name = name ? name : code->expr1->value.function.esym->name;
    8006          109 :       code->expr1->symtree = expr->symtree;
    8007          109 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    8008              : 
    8009              :       /* Trim away the extraneous references that emerge from nested
    8010              :          use of interface.cc (extend_expr).  */
    8011          109 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    8012          109 :       if (class_ref && class_ref->next)
    8013              :         {
    8014            0 :           gfc_free_ref_list (class_ref->next);
    8015            0 :           class_ref->next = NULL;
    8016              :         }
    8017          109 :       else if (code->expr1->ref && !class_ref)
    8018              :         {
    8019           18 :           gfc_free_ref_list (code->expr1->ref);
    8020           18 :           code->expr1->ref = NULL;
    8021              :         }
    8022              : 
    8023              :       /* Now use the procedure in the vtable.  */
    8024          109 :       gfc_add_vptr_component (code->expr1);
    8025          109 :       gfc_add_component_ref (code->expr1, name);
    8026          109 :       code->expr1->value.function.esym = NULL;
    8027          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8028            0 :         code->expr1->base_expr = expr;
    8029          109 :       return true;
    8030              :     }
    8031              : 
    8032         1615 :   if (st == NULL)
    8033          340 :     return resolve_typebound_call (code, NULL, NULL);
    8034              : 
    8035         1275 :   if (!gfc_resolve_ref (code->expr1))
    8036              :     return false;
    8037              : 
    8038              :   /* Get the CLASS declared type.  */
    8039         1275 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    8040              : 
    8041              :   /* Weed out cases of the ultimate component being a derived type.  */
    8042         1275 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8043         1210 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8044              :     {
    8045          899 :       gfc_free_ref_list (new_ref);
    8046          899 :       return resolve_typebound_call (code, NULL, NULL);
    8047              :     }
    8048              : 
    8049          376 :   if (!resolve_typebound_call (code, &name, &overridable))
    8050              :     {
    8051            5 :       gfc_free_ref_list (new_ref);
    8052            5 :       return false;
    8053              :     }
    8054          371 :   ts = code->expr1->ts;
    8055              : 
    8056          371 :   if (overridable)
    8057              :     {
    8058              :       /* Convert the expression to a procedure pointer component call.  */
    8059          369 :       code->expr1->value.function.esym = NULL;
    8060          369 :       code->expr1->symtree = st;
    8061              : 
    8062          369 :       if (new_ref)
    8063           93 :         code->expr1->ref = new_ref;
    8064              : 
    8065              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8066          369 :       gfc_add_vptr_component (code->expr1);
    8067          369 :       gfc_add_component_ref (code->expr1, name);
    8068              : 
    8069              :       /* Recover the typespec for the expression.  This is really only
    8070              :         necessary for generic procedures, where the additional call
    8071              :         to gfc_add_component_ref seems to throw the collection of the
    8072              :         correct typespec.  */
    8073          369 :       code->expr1->ts = ts;
    8074              :     }
    8075            2 :   else if (new_ref)
    8076            0 :     gfc_free_ref_list (new_ref);
    8077              : 
    8078              :   return true;
    8079              : }
    8080              : 
    8081              : 
    8082              : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    8083              : 
    8084              : static bool
    8085          124 : resolve_ppc_call (gfc_code* c)
    8086              : {
    8087          124 :   gfc_component *comp;
    8088              : 
    8089          124 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    8090          124 :   gcc_assert (comp != NULL);
    8091              : 
    8092          124 :   c->resolved_sym = c->expr1->symtree->n.sym;
    8093          124 :   c->expr1->expr_type = EXPR_VARIABLE;
    8094              : 
    8095          124 :   if (!comp->attr.subroutine)
    8096            1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    8097              : 
    8098          124 :   if (!gfc_resolve_ref (c->expr1))
    8099              :     return false;
    8100              : 
    8101          124 :   if (!update_ppc_arglist (c->expr1))
    8102              :     return false;
    8103              : 
    8104          123 :   c->ext.actual = c->expr1->value.compcall.actual;
    8105              : 
    8106          123 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    8107          123 :                                !(comp->ts.interface
    8108           93 :                                  && comp->ts.interface->formal)))
    8109              :     return false;
    8110              : 
    8111          123 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    8112              :     return false;
    8113              : 
    8114          122 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    8115              : 
    8116          122 :   return true;
    8117              : }
    8118              : 
    8119              : 
    8120              : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    8121              : 
    8122              : static bool
    8123          450 : resolve_expr_ppc (gfc_expr* e)
    8124              : {
    8125          450 :   gfc_component *comp;
    8126              : 
    8127          450 :   comp = gfc_get_proc_ptr_comp (e);
    8128          450 :   gcc_assert (comp != NULL);
    8129              : 
    8130              :   /* Convert to EXPR_FUNCTION.  */
    8131          450 :   e->expr_type = EXPR_FUNCTION;
    8132          450 :   e->value.function.isym = NULL;
    8133          450 :   e->value.function.actual = e->value.compcall.actual;
    8134          450 :   e->ts = comp->ts;
    8135          450 :   if (comp->as != NULL)
    8136              :     {
    8137           28 :       e->rank = comp->as->rank;
    8138           28 :       e->corank = comp->as->corank;
    8139              :     }
    8140              : 
    8141          450 :   if (!comp->attr.function)
    8142            3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    8143              : 
    8144          450 :   if (!gfc_resolve_ref (e))
    8145              :     return false;
    8146              : 
    8147          450 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    8148          450 :                                !(comp->ts.interface
    8149          449 :                                  && comp->ts.interface->formal)))
    8150              :     return false;
    8151              : 
    8152          450 :   if (!update_ppc_arglist (e))
    8153              :     return false;
    8154              : 
    8155          448 :   if (!check_pure_function(e))
    8156              :     return false;
    8157              : 
    8158          447 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    8159              : 
    8160          447 :   return true;
    8161              : }
    8162              : 
    8163              : 
    8164              : static bool
    8165        11409 : gfc_is_expandable_expr (gfc_expr *e)
    8166              : {
    8167        11409 :   gfc_constructor *con;
    8168              : 
    8169        11409 :   if (e->expr_type == EXPR_ARRAY)
    8170              :     {
    8171              :       /* Traverse the constructor looking for variables that are flavor
    8172              :          parameter.  Parameters must be expanded since they are fully used at
    8173              :          compile time.  */
    8174        11409 :       con = gfc_constructor_first (e->value.constructor);
    8175        30227 :       for (; con; con = gfc_constructor_next (con))
    8176              :         {
    8177        13314 :           if (con->expr->expr_type == EXPR_VARIABLE
    8178         5181 :               && con->expr->symtree
    8179         5181 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8180         5099 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8181              :             return true;
    8182         8133 :           if (con->expr->expr_type == EXPR_ARRAY
    8183         8133 :               && gfc_is_expandable_expr (con->expr))
    8184              :             return true;
    8185              :         }
    8186              :     }
    8187              : 
    8188              :   return false;
    8189              : }
    8190              : 
    8191              : 
    8192              : /* Sometimes variables in specification expressions of the result
    8193              :    of module procedures in submodules wind up not being the 'real'
    8194              :    dummy.  Find this, if possible, in the namespace of the first
    8195              :    formal argument.  */
    8196              : 
    8197              : static void
    8198         3453 : fixup_unique_dummy (gfc_expr *e)
    8199              : {
    8200         3453 :   gfc_symtree *st = NULL;
    8201         3453 :   gfc_symbol *s = NULL;
    8202              : 
    8203         3453 :   if (e->symtree->n.sym->ns->proc_name
    8204         3423 :       && e->symtree->n.sym->ns->proc_name->formal)
    8205         3423 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8206              : 
    8207         3423 :   if (s != NULL)
    8208         3423 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8209              : 
    8210         3453 :   if (st != NULL
    8211           14 :       && st->n.sym != NULL
    8212           14 :       && st->n.sym->attr.dummy)
    8213           14 :     e->symtree = st;
    8214         3453 : }
    8215              : 
    8216              : 
    8217              : /* Resolve an expression.  That is, make sure that types of operands agree
    8218              :    with their operators, intrinsic operators are converted to function calls
    8219              :    for overloaded types and unresolved function references are resolved.  */
    8220              : 
    8221              : bool
    8222      7115244 : gfc_resolve_expr (gfc_expr *e)
    8223              : {
    8224      7115244 :   bool t;
    8225      7115244 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8226              : 
    8227      7115244 :   if (e == NULL || e->do_not_resolve_again)
    8228              :     return true;
    8229              : 
    8230              :   /* inquiry_argument only applies to variables.  */
    8231      5203493 :   inquiry_save = inquiry_argument;
    8232      5203493 :   actual_arg_save = actual_arg;
    8233      5203493 :   first_actual_arg_save = first_actual_arg;
    8234              : 
    8235      5203493 :   if (e->expr_type != EXPR_VARIABLE)
    8236              :     {
    8237      3880615 :       inquiry_argument = false;
    8238      3880615 :       actual_arg = false;
    8239      3880615 :       first_actual_arg = false;
    8240              :     }
    8241      1322878 :   else if (e->symtree != NULL
    8242      1322433 :            && *e->symtree->name == '@'
    8243         4160 :            && e->symtree->n.sym->attr.dummy)
    8244              :     {
    8245              :       /* Deal with submodule specification expressions that are not
    8246              :          found to be referenced in module.cc(read_cleanup).  */
    8247         3453 :       fixup_unique_dummy (e);
    8248              :     }
    8249              : 
    8250      5203493 :   switch (e->expr_type)
    8251              :     {
    8252       531162 :     case EXPR_OP:
    8253       531162 :       t = resolve_operator (e);
    8254       531162 :       break;
    8255              : 
    8256          150 :     case EXPR_CONDITIONAL:
    8257          150 :       t = resolve_conditional (e);
    8258          150 :       break;
    8259              : 
    8260      1666567 :     case EXPR_FUNCTION:
    8261      1666567 :     case EXPR_VARIABLE:
    8262              : 
    8263      1666567 :       if (check_host_association (e))
    8264       343725 :         t = resolve_function (e);
    8265              :       else
    8266      1322842 :         t = resolve_variable (e);
    8267              : 
    8268      1666567 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    8269         6911 :           && e->ref->type != REF_SUBSTRING)
    8270         2162 :         gfc_resolve_substring_charlen (e);
    8271              : 
    8272              :       break;
    8273              : 
    8274         1632 :     case EXPR_COMPCALL:
    8275         1632 :       t = resolve_typebound_function (e);
    8276         1632 :       break;
    8277              : 
    8278          508 :     case EXPR_SUBSTRING:
    8279          508 :       t = gfc_resolve_ref (e);
    8280          508 :       break;
    8281              : 
    8282              :     case EXPR_CONSTANT:
    8283              :     case EXPR_NULL:
    8284              :       t = true;
    8285              :       break;
    8286              : 
    8287          450 :     case EXPR_PPC:
    8288          450 :       t = resolve_expr_ppc (e);
    8289          450 :       break;
    8290              : 
    8291        71608 :     case EXPR_ARRAY:
    8292        71608 :       t = false;
    8293        71608 :       if (!gfc_resolve_ref (e))
    8294              :         break;
    8295              : 
    8296        71608 :       t = gfc_resolve_array_constructor (e);
    8297              :       /* Also try to expand a constructor.  */
    8298        71608 :       if (t)
    8299              :         {
    8300        71506 :           gfc_expression_rank (e);
    8301        71506 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8302        67128 :             gfc_expand_constructor (e, false);
    8303              :         }
    8304              : 
    8305              :       /* This provides the opportunity for the length of constructors with
    8306              :          character valued function elements to propagate the string length
    8307              :          to the expression.  */
    8308        71506 :       if (t && e->ts.type == BT_CHARACTER)
    8309              :         {
    8310              :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    8311              :              here rather then add a duplicate test for it above.  */
    8312        10729 :           gfc_expand_constructor (e, false);
    8313        10729 :           t = gfc_resolve_character_array_constructor (e);
    8314              :         }
    8315              : 
    8316              :       break;
    8317              : 
    8318        16555 :     case EXPR_STRUCTURE:
    8319        16555 :       t = gfc_resolve_ref (e);
    8320        16555 :       if (!t)
    8321              :         break;
    8322              : 
    8323        16555 :       t = resolve_structure_cons (e, 0);
    8324        16555 :       if (!t)
    8325              :         break;
    8326              : 
    8327        16543 :       t = gfc_simplify_expr (e, 0);
    8328        16543 :       break;
    8329              : 
    8330            0 :     default:
    8331            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8332              :     }
    8333              : 
    8334      5203493 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8335       180450 :     fixup_charlen (e);
    8336              : 
    8337      5203493 :   inquiry_argument = inquiry_save;
    8338      5203493 :   actual_arg = actual_arg_save;
    8339      5203493 :   first_actual_arg = first_actual_arg_save;
    8340              : 
    8341              :   /* For some reason, resolving these expressions a second time mangles
    8342              :      the typespec of the expression itself.  */
    8343      5203493 :   if (t && e->expr_type == EXPR_VARIABLE
    8344      1319999 :       && e->symtree->n.sym->attr.select_rank_temporary
    8345         3428 :       && UNLIMITED_POLY (e->symtree->n.sym))
    8346           83 :     e->do_not_resolve_again = 1;
    8347              : 
    8348      5200953 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8349         6919 :     t = check_import_status (e);
    8350              : 
    8351              :   return t;
    8352              : }
    8353              : 
    8354              : 
    8355              : /* Resolve an expression from an iterator.  They must be scalar and have
    8356              :    INTEGER or (optionally) REAL type.  */
    8357              : 
    8358              : static bool
    8359       151265 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8360              :                            const char *name_msgid)
    8361              : {
    8362       151265 :   if (!gfc_resolve_expr (expr))
    8363              :     return false;
    8364              : 
    8365       151260 :   if (expr->rank != 0)
    8366              :     {
    8367            0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    8368            0 :       return false;
    8369              :     }
    8370              : 
    8371       151260 :   if (expr->ts.type != BT_INTEGER)
    8372              :     {
    8373          274 :       if (expr->ts.type == BT_REAL)
    8374              :         {
    8375          274 :           if (real_ok)
    8376          271 :             return gfc_notify_std (GFC_STD_F95_DEL,
    8377              :                                    "%s at %L must be integer",
    8378          271 :                                    _(name_msgid), &expr->where);
    8379              :           else
    8380              :             {
    8381            3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    8382              :                          &expr->where);
    8383            3 :               return false;
    8384              :             }
    8385              :         }
    8386              :       else
    8387              :         {
    8388            0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    8389            0 :           return false;
    8390              :         }
    8391              :     }
    8392              :   return true;
    8393              : }
    8394              : 
    8395              : 
    8396              : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    8397              :    false allow only INTEGER type iterators, otherwise allow REAL types.
    8398              :    Set own_scope to true for ac-implied-do and data-implied-do as those
    8399              :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    8400              : 
    8401              : bool
    8402        37825 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8403              : {
    8404        37825 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8405              :     return false;
    8406              : 
    8407        37821 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8408        37821 :                                  _("iterator variable")))
    8409              :     return false;
    8410              : 
    8411        37815 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8412              :                                   "Start expression in DO loop"))
    8413              :     return false;
    8414              : 
    8415        37814 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8416              :                                   "End expression in DO loop"))
    8417              :     return false;
    8418              : 
    8419        37811 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8420              :                                   "Step expression in DO loop"))
    8421              :     return false;
    8422              : 
    8423              :   /* Convert start, end, and step to the same type as var.  */
    8424        37810 :   if (iter->start->ts.kind != iter->var->ts.kind
    8425        37530 :       || iter->start->ts.type != iter->var->ts.type)
    8426          315 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8427              : 
    8428        37810 :   if (iter->end->ts.kind != iter->var->ts.kind
    8429        37557 :       || iter->end->ts.type != iter->var->ts.type)
    8430          278 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8431              : 
    8432        37810 :   if (iter->step->ts.kind != iter->var->ts.kind
    8433        37566 :       || iter->step->ts.type != iter->var->ts.type)
    8434          280 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8435              : 
    8436        37810 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8437              :     {
    8438        36688 :       if ((iter->step->ts.type == BT_INTEGER
    8439        36605 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8440        73291 :           || (iter->step->ts.type == BT_REAL
    8441           83 :               && mpfr_sgn (iter->step->value.real) == 0))
    8442              :         {
    8443            3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8444            3 :                      &iter->step->where);
    8445            3 :           return false;
    8446              :         }
    8447              :     }
    8448              : 
    8449        37807 :   if (iter->start->expr_type == EXPR_CONSTANT
    8450        34675 :       && iter->end->expr_type == EXPR_CONSTANT
    8451        27127 :       && iter->step->expr_type == EXPR_CONSTANT)
    8452              :     {
    8453        26860 :       int sgn, cmp;
    8454        26860 :       if (iter->start->ts.type == BT_INTEGER)
    8455              :         {
    8456        26806 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8457        26806 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8458              :         }
    8459              :       else
    8460              :         {
    8461           54 :           sgn = mpfr_sgn (iter->step->value.real);
    8462           54 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8463              :         }
    8464        26860 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8465          146 :         gfc_warning (OPT_Wzerotrip,
    8466              :                      "DO loop at %L will be executed zero times",
    8467          146 :                      &iter->step->where);
    8468              :     }
    8469              : 
    8470        37807 :   if (iter->end->expr_type == EXPR_CONSTANT
    8471        27495 :       && iter->end->ts.type == BT_INTEGER
    8472        27441 :       && iter->step->expr_type == EXPR_CONSTANT
    8473        27131 :       && iter->step->ts.type == BT_INTEGER
    8474        27131 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8475        26760 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8476              :     {
    8477        25974 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8478        25974 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8479              : 
    8480        25974 :       if (is_step_positive
    8481        25603 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8482            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8483              :                      "DO loop at %L is undefined as it overflows",
    8484            7 :                      &iter->step->where);
    8485              :       else if (!is_step_positive
    8486          371 :                && mpz_cmp (iter->end->value.integer,
    8487          371 :                            gfc_integer_kinds[k].min_int) == 0)
    8488            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8489              :                      "DO loop at %L is undefined as it underflows",
    8490            7 :                      &iter->step->where);
    8491              :     }
    8492              : 
    8493              :   return true;
    8494              : }
    8495              : 
    8496              : 
    8497              : /* Traversal function for find_forall_index.  f == 2 signals that
    8498              :    that variable itself is not to be checked - only the references.  */
    8499              : 
    8500              : static bool
    8501        42620 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8502              : {
    8503        42620 :   if (expr->expr_type != EXPR_VARIABLE)
    8504              :     return false;
    8505              : 
    8506              :   /* A scalar assignment  */
    8507        18188 :   if (!expr->ref || *f == 1)
    8508              :     {
    8509        12128 :       if (expr->symtree->n.sym == sym)
    8510              :         return true;
    8511              :       else
    8512              :         return false;
    8513              :     }
    8514              : 
    8515         6060 :   if (*f == 2)
    8516         1731 :     *f = 1;
    8517              :   return false;
    8518              : }
    8519              : 
    8520              : 
    8521              : /* Check whether the FORALL index appears in the expression or not.
    8522              :    Returns true if SYM is found in EXPR.  */
    8523              : 
    8524              : bool
    8525        27001 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8526              : {
    8527        27001 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8528              :     return true;
    8529              :   else
    8530              :     return false;
    8531              : }
    8532              : 
    8533              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8534              :    This constraint specifies rules for variables in locality-specs.  */
    8535              : 
    8536              : static int
    8537          717 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8538              : {
    8539          717 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8540              : 
    8541          717 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8542              :     {
    8543           22 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8544           22 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8545           24 :            list; list = list->next)
    8546              :         {
    8547            5 :           if (list->expr->symtree->n.sym == sym)
    8548              :             {
    8549            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8550              :                          "must not appear in LOCAL locality-spec at %L",
    8551              :                          sym->name, &(*expr)->where, &list->expr->where);
    8552            3 :               *walk_subtrees = 0;
    8553            3 :               return 1;
    8554              :             }
    8555              :         }
    8556              :     }
    8557              : 
    8558          714 :     *walk_subtrees = 1;
    8559          714 :     return 0;
    8560              : }
    8561              : 
    8562              : static int
    8563         3969 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8564              : {
    8565         3969 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8566              : 
    8567         3969 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8568              :     {
    8569         1798 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8570              : 
    8571         1798 :       if (d->sym_hash->contains (sym))
    8572         1263 :         sym->mark = 1;
    8573              : 
    8574          535 :       else if (d->default_none)
    8575              :         {
    8576            6 :           gfc_namespace *ns2 = d->ns;
    8577           10 :           while (ns2)
    8578              :             {
    8579            6 :               if (ns2 == sym->ns)
    8580              :                 break;
    8581            4 :               ns2 = ns2->parent;
    8582              :             }
    8583              : 
    8584              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.  */
    8585            6 :           if (sym->ns->code->ext.concur.forall_iterator)
    8586              :             {
    8587              :               gfc_forall_iterator *iter
    8588              :                 = sym->ns->code->ext.concur.forall_iterator;
    8589            5 :               for (; iter; iter = iter->next)
    8590            3 :                 if (iter->var->symtree
    8591            1 :                     && strcmp(sym->name, iter->var->symtree->name) == 0)
    8592            1 :                   return 0;
    8593              :             }
    8594              : 
    8595              :           /* A named constant is not a variable, so skip test.  */
    8596            5 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8597              :             {
    8598            1 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8599              :                         "of DO CONCURRENT at %L but required due to "
    8600              :                         "DEFAULT (NONE)",
    8601            1 :                         sym->name, &(*e)->where, &d->code->loc);
    8602            1 :               d->sym_hash->add (sym);
    8603              :             }
    8604              :         }
    8605              :     }
    8606              :   return 0;
    8607              : }
    8608              : 
    8609              : static void
    8610          210 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8611              : {
    8612          210 :   struct check_default_none_data data;
    8613          210 :   data.code = code;
    8614          210 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8615          210 :   data.ns = ns;
    8616          210 :   data.default_none = code->ext.concur.default_none;
    8617              : 
    8618         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8619              :     {
    8620          840 :       const char *name;
    8621          840 :       switch (locality)
    8622              :         {
    8623              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8624          210 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8625          210 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8626          210 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8627              :           default: gcc_unreachable ();
    8628              :         }
    8629              : 
    8630         1227 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8631          387 :            list = list->next)
    8632              :         {
    8633          387 :           gfc_expr *expr = list->expr;
    8634              : 
    8635          387 :           if (locality == LOCALITY_REDUCE
    8636           72 :               && (expr->expr_type == EXPR_FUNCTION
    8637           48 :                   || expr->expr_type == EXPR_OP))
    8638           35 :             continue;
    8639              : 
    8640          363 :           if (!gfc_resolve_expr (expr))
    8641            3 :             continue;
    8642              : 
    8643          360 :           if (expr->expr_type != EXPR_VARIABLE
    8644          360 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8645          360 :               || (expr->ref
    8646          147 :                   && (expr->ref->type != REF_ARRAY
    8647          147 :                       || expr->ref->u.ar.type != AR_FULL
    8648          143 :                       || expr->ref->next)))
    8649              :             {
    8650            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8651              :                          name, &expr->where);
    8652            4 :                 continue;
    8653              :             }
    8654              : 
    8655          356 :           gfc_symbol *sym = expr->symtree->n.sym;
    8656              : 
    8657          356 :           if (data.sym_hash->contains (sym))
    8658              :             {
    8659            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8660              :                          "locality-spec", sym->name, &expr->where);
    8661            4 :               continue;
    8662              :             }
    8663              : 
    8664          352 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8665          704 :                iter; iter = iter->next)
    8666              :             {
    8667          352 :               if (iter->var->symtree->n.sym == sym)
    8668              :                 {
    8669            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8670              :                              "locality-spec", sym->name, &expr->where);
    8671            1 :                   continue;
    8672              :                 }
    8673              : 
    8674          351 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8675              :             }
    8676              : 
    8677          352 :           if (locality == LOCALITY_LOCAL
    8678          352 :               || locality == LOCALITY_LOCAL_INIT
    8679          352 :               || locality == LOCALITY_REDUCE)
    8680              :             {
    8681          198 :               if (sym->attr.optional)
    8682            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8683              :                            "locality-spec at %L",
    8684              :                            sym->name, name, &expr->where);
    8685              : 
    8686          198 :               if (sym->attr.dimension
    8687           66 :                   && sym->as
    8688           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8689            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8690              :                            "locality-spec at %L",
    8691              :                            sym->name, name, &expr->where);
    8692              : 
    8693          198 :               gfc_check_vardef_context (expr, false, false, false, name);
    8694              :             }
    8695              : 
    8696          198 :           if (locality == LOCALITY_LOCAL
    8697              :               || locality == LOCALITY_LOCAL_INIT)
    8698              :             {
    8699          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    8700              : 
    8701          181 :               if (attr.allocatable)
    8702            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8703              :                            "locality-spec at %L",
    8704              :                            sym->name, name, &expr->where);
    8705              : 
    8706          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8707            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8708              :                            " for %qs in %s locality-spec at %L",
    8709              :                            sym->name, name, &expr->where);
    8710              : 
    8711          177 :               else if (attr.codimension)
    8712            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8713              :                            "at %L",
    8714              :                            sym->name, name, &expr->where);
    8715              : 
    8716          177 :               else if (expr->ts.type == BT_DERIVED
    8717          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8718            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8719              :                            "locality-spec at %L",
    8720              :                            sym->name, name, &expr->where);
    8721              : 
    8722          177 :               else if (gfc_has_ultimate_allocatable (expr))
    8723            4 :                 gfc_error ("Type with ultimate allocatable component not "
    8724              :                            "permitted for %qs in %s locality-spec at %L",
    8725              :                            sym->name, name, &expr->where);
    8726              :             }
    8727              : 
    8728          171 :           else if (locality == LOCALITY_REDUCE)
    8729              :             {
    8730           17 :               if (sym->attr.asynchronous)
    8731            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8732              :                            "REDUCE locality-spec at %L",
    8733              :                            sym->name, &expr->where);
    8734           17 :               if (sym->attr.volatile_)
    8735            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8736              :                            "locality-spec at %L", sym->name, &expr->where);
    8737              :             }
    8738              : 
    8739          352 :           data.sym_hash->add (sym);
    8740              :         }
    8741              : 
    8742          840 :       if (locality == LOCALITY_LOCAL)
    8743              :         {
    8744          210 :           gcc_assert (locality == 0);
    8745              : 
    8746          210 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8747          437 :                iter; iter = iter->next)
    8748              :             {
    8749          227 :               gfc_expr_walker (&iter->start,
    8750              :                                do_concur_locality_specs_f2023,
    8751              :                                &data);
    8752              : 
    8753          227 :               gfc_expr_walker (&iter->end,
    8754              :                                do_concur_locality_specs_f2023,
    8755              :                                &data);
    8756              : 
    8757          227 :               gfc_expr_walker (&iter->stride,
    8758              :                                do_concur_locality_specs_f2023,
    8759              :                                &data);
    8760              :             }
    8761              : 
    8762          210 :           if (code->expr1)
    8763            7 :             gfc_expr_walker (&code->expr1,
    8764              :                              do_concur_locality_specs_f2023,
    8765              :                              &data);
    8766              :         }
    8767              :     }
    8768              : 
    8769          210 :   gfc_expr *reduce_op = NULL;
    8770              : 
    8771          210 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8772          258 :        list; list = list->next)
    8773              :     {
    8774           48 :       gfc_expr *expr = list->expr;
    8775              : 
    8776           48 :       if (expr->expr_type != EXPR_VARIABLE)
    8777              :         {
    8778           24 :           reduce_op = expr;
    8779           24 :           continue;
    8780              :         }
    8781              : 
    8782           24 :       if (reduce_op->expr_type == EXPR_OP)
    8783              :         {
    8784           17 :           switch (reduce_op->value.op.op)
    8785              :             {
    8786           17 :               case INTRINSIC_PLUS:
    8787           17 :               case INTRINSIC_TIMES:
    8788           17 :                 if (!gfc_numeric_ts (&expr->ts))
    8789            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8790            3 :                              "got %s", expr->symtree->n.sym->name,
    8791              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8792              :                 break;
    8793            0 :               case INTRINSIC_AND:
    8794            0 :               case INTRINSIC_OR:
    8795            0 :               case INTRINSIC_EQV:
    8796            0 :               case INTRINSIC_NEQV:
    8797            0 :                 if (expr->ts.type != BT_LOGICAL)
    8798            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8799            0 :                              "got %qs", expr->symtree->n.sym->name,
    8800              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8801              :                 break;
    8802            0 :               default:
    8803            0 :                 gcc_unreachable ();
    8804              :             }
    8805              :         }
    8806              : 
    8807            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8808              :         {
    8809            7 :           switch (reduce_op->value.function.isym->id)
    8810              :             {
    8811            6 :               case GFC_ISYM_MIN:
    8812            6 :               case GFC_ISYM_MAX:
    8813            6 :                 if (expr->ts.type != BT_INTEGER
    8814              :                     && expr->ts.type != BT_REAL
    8815              :                     && expr->ts.type != BT_CHARACTER)
    8816            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8817              :                              "in REDUCE with MIN/MAX at %L, got %s",
    8818            2 :                              expr->symtree->n.sym->name, &expr->where,
    8819              :                              gfc_basic_typename (expr->ts.type));
    8820              :                 break;
    8821            1 :               case GFC_ISYM_IAND:
    8822            1 :               case GFC_ISYM_IOR:
    8823            1 :               case GFC_ISYM_IEOR:
    8824            1 :                 if (expr->ts.type != BT_INTEGER)
    8825            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8826              :                              "IAND/IOR/IEOR at %L, got %s",
    8827            1 :                              expr->symtree->n.sym->name, &expr->where,
    8828              :                              gfc_basic_typename (expr->ts.type));
    8829              :                 break;
    8830            0 :               default:
    8831            0 :                 gcc_unreachable ();
    8832              :             }
    8833              :         }
    8834              : 
    8835              :       else
    8836            0 :         gcc_unreachable ();
    8837              :     }
    8838              : 
    8839         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8840              :     {
    8841         1227 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8842          387 :            list = list->next)
    8843              :         {
    8844          387 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8845          363 :             list->expr->symtree->n.sym->mark = 0;
    8846              :         }
    8847              :     }
    8848              : 
    8849          210 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8850              :                    check_default_none_expr, &data);
    8851              : 
    8852         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8853              :     {
    8854          840 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8855         1227 :       while (*plist)
    8856              :         {
    8857          387 :           gfc_expr *expr = (*plist)->expr;
    8858          387 :           if (expr->expr_type == EXPR_VARIABLE)
    8859              :             {
    8860          363 :               gfc_symbol *sym = expr->symtree->n.sym;
    8861          363 :               if (sym->mark == 0)
    8862              :                 {
    8863           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8864              :                                "locality-spec at %L is not used",
    8865              :                                sym->name, &expr->where);
    8866           70 :                   gfc_expr_list *tmp = *plist;
    8867           70 :                   *plist = (*plist)->next;
    8868           70 :                   gfc_free_expr (tmp->expr);
    8869           70 :                   free (tmp);
    8870           70 :                   continue;
    8871           70 :                 }
    8872              :             }
    8873          317 :           plist = &((*plist)->next);
    8874              :         }
    8875              :     }
    8876              : 
    8877          420 :   delete data.sym_hash;
    8878          210 : }
    8879              : 
    8880              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8881              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8882              :    INTEGERs, and if stride is a constant it must be nonzero.
    8883              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8884              :    not contain a reference to any index-name in the
    8885              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8886              : 
    8887              : static void
    8888         2202 : resolve_forall_iterators (gfc_forall_iterator *it)
    8889              : {
    8890         2202 :   gfc_forall_iterator *iter, *iter2;
    8891              : 
    8892         6320 :   for (iter = it; iter; iter = iter->next)
    8893              :     {
    8894         4118 :       if (gfc_resolve_expr (iter->var)
    8895         4118 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8896            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8897              :                    &iter->var->where);
    8898              : 
    8899         4118 :       if (gfc_resolve_expr (iter->start)
    8900         4118 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8901            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8902              :                    &iter->start->where);
    8903         4118 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8904            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8905              : 
    8906         4118 :       if (gfc_resolve_expr (iter->end)
    8907         4118 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8908            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8909              :                    &iter->end->where);
    8910         4118 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8911            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8912              : 
    8913         4118 :       if (gfc_resolve_expr (iter->stride))
    8914              :         {
    8915         4118 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8916            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8917              :                        &iter->stride->where, "INTEGER");
    8918              : 
    8919         4118 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8920         4115 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8921            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8922              :                        &iter->stride->where);
    8923              :         }
    8924         4118 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8925            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8926              :     }
    8927              : 
    8928         6320 :   for (iter = it; iter; iter = iter->next)
    8929        11078 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8930              :       {
    8931         6960 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8932         6958 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8933        13916 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8934            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8935            6 :                      "specification at %L", iter->var->symtree->name,
    8936            6 :                      &iter2->start->where);
    8937              :       }
    8938         2202 : }
    8939              : 
    8940              : 
    8941              : /* Given a pointer to a symbol that is a derived type, see if it's
    8942              :    inaccessible, i.e. if it's defined in another module and the components are
    8943              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8944              :    inaccessible components are found, nonzero otherwise.  */
    8945              : 
    8946              : static bool
    8947         1351 : derived_inaccessible (gfc_symbol *sym)
    8948              : {
    8949         1351 :   gfc_component *c;
    8950              : 
    8951         1351 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8952              :     return 1;
    8953              : 
    8954         3999 :   for (c = sym->components; c; c = c->next)
    8955              :     {
    8956              :         /* Prevent an infinite loop through this function.  */
    8957         2661 :         if (c->ts.type == BT_DERIVED
    8958          289 :             && (c->attr.pointer || c->attr.allocatable)
    8959           72 :             && sym == c->ts.u.derived)
    8960           72 :           continue;
    8961              : 
    8962         2589 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    8963              :           return 1;
    8964              :     }
    8965              : 
    8966              :   return 0;
    8967              : }
    8968              : 
    8969              : 
    8970              : /* Resolve the argument of a deallocate expression.  The expression must be
    8971              :    a pointer or a full array.  */
    8972              : 
    8973              : static bool
    8974         8336 : resolve_deallocate_expr (gfc_expr *e)
    8975              : {
    8976         8336 :   symbol_attribute attr;
    8977         8336 :   int allocatable, pointer;
    8978         8336 :   gfc_ref *ref;
    8979         8336 :   gfc_symbol *sym;
    8980         8336 :   gfc_component *c;
    8981         8336 :   bool unlimited;
    8982              : 
    8983         8336 :   if (!gfc_resolve_expr (e))
    8984              :     return false;
    8985              : 
    8986         8336 :   if (e->expr_type != EXPR_VARIABLE)
    8987            0 :     goto bad;
    8988              : 
    8989         8336 :   sym = e->symtree->n.sym;
    8990         8336 :   unlimited = UNLIMITED_POLY(sym);
    8991              : 
    8992         8336 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    8993              :     {
    8994         1574 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    8995         1574 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    8996              :     }
    8997              :   else
    8998              :     {
    8999         6762 :       allocatable = sym->attr.allocatable;
    9000         6762 :       pointer = sym->attr.pointer;
    9001              :     }
    9002        16737 :   for (ref = e->ref; ref; ref = ref->next)
    9003              :     {
    9004         8401 :       switch (ref->type)
    9005              :         {
    9006         6269 :         case REF_ARRAY:
    9007         6269 :           if (ref->u.ar.type != AR_FULL
    9008         6477 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9009          208 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9010              :             allocatable = 0;
    9011              :           break;
    9012              : 
    9013         2132 :         case REF_COMPONENT:
    9014         2132 :           c = ref->u.c.component;
    9015         2132 :           if (c->ts.type == BT_CLASS)
    9016              :             {
    9017          297 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9018          297 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9019              :             }
    9020              :           else
    9021              :             {
    9022         1835 :               allocatable = c->attr.allocatable;
    9023         1835 :               pointer = c->attr.pointer;
    9024              :             }
    9025              :           break;
    9026              : 
    9027              :         case REF_SUBSTRING:
    9028              :         case REF_INQUIRY:
    9029          513 :           allocatable = 0;
    9030              :           break;
    9031              :         }
    9032              :     }
    9033              : 
    9034         8336 :   attr = gfc_expr_attr (e);
    9035              : 
    9036         8336 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9037              :     {
    9038            3 :     bad:
    9039            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9040              :                  &e->where);
    9041            3 :       return false;
    9042              :     }
    9043              : 
    9044              :   /* F2008, C644.  */
    9045         8333 :   if (gfc_is_coindexed (e))
    9046              :     {
    9047            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9048            1 :       return false;
    9049              :     }
    9050              : 
    9051         8332 :   if (pointer
    9052        10700 :       && !gfc_check_vardef_context (e, true, true, false,
    9053         2368 :                                     _("DEALLOCATE object")))
    9054              :     return false;
    9055         8330 :   if (!gfc_check_vardef_context (e, false, true, false,
    9056         8330 :                                  _("DEALLOCATE object")))
    9057              :     return false;
    9058              : 
    9059              :   return true;
    9060              : }
    9061              : 
    9062              : 
    9063              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9064              : static bool
    9065        47360 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9066              : {
    9067        47360 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9068         2081 :     return true;
    9069              : 
    9070              :   return false;
    9071              : }
    9072              : 
    9073              : bool
    9074        20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9075              : {
    9076        20080 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9077              : }
    9078              : 
    9079              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9080              :    of character expressions.  */
    9081              : static bool
    9082        20457 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9083              : {
    9084            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9085              : }
    9086              : 
    9087              : 
    9088              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9089              :    allocated, get the expression node to be initialized afterwards (needed for
    9090              :    derived types with default initializers, and derived types with allocatable
    9091              :    components that need nullification.)  */
    9092              : 
    9093              : gfc_expr *
    9094         5767 : gfc_expr_to_initialize (gfc_expr *e)
    9095              : {
    9096         5767 :   gfc_expr *result;
    9097         5767 :   gfc_ref *ref;
    9098         5767 :   int i;
    9099              : 
    9100         5767 :   result = gfc_copy_expr (e);
    9101              : 
    9102              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9103        11406 :   for (ref = result->ref; ref; ref = ref->next)
    9104         8989 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9105              :       {
    9106         3350 :         if (ref->u.ar.dimen == 0
    9107           74 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9108              :           return result;
    9109              : 
    9110         3276 :         ref->u.ar.type = AR_FULL;
    9111              : 
    9112         7398 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9113         4122 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9114              : 
    9115              :         break;
    9116              :       }
    9117              : 
    9118         5693 :   gfc_free_shape (&result->shape, result->rank);
    9119              : 
    9120              :   /* Recalculate rank, shape, etc.  */
    9121         5693 :   gfc_resolve_expr (result);
    9122         5693 :   return result;
    9123              : }
    9124              : 
    9125              : 
    9126              : /* If the last ref of an expression is an array ref, return a copy of the
    9127              :    expression with that one removed.  Otherwise, a copy of the original
    9128              :    expression.  This is used for allocate-expressions and pointer assignment
    9129              :    LHS, where there may be an array specification that needs to be stripped
    9130              :    off when using gfc_check_vardef_context.  */
    9131              : 
    9132              : static gfc_expr*
    9133        27640 : remove_last_array_ref (gfc_expr* e)
    9134              : {
    9135        27640 :   gfc_expr* e2;
    9136        27640 :   gfc_ref** r;
    9137              : 
    9138        27640 :   e2 = gfc_copy_expr (e);
    9139        35662 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9140        24331 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9141              :       {
    9142        16309 :         gfc_free_ref_list (*r);
    9143        16309 :         *r = NULL;
    9144        16309 :         break;
    9145              :       }
    9146              : 
    9147        27640 :   return e2;
    9148              : }
    9149              : 
    9150              : 
    9151              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9152              :    a source-expr are conformable.  This does not catch all possible
    9153              :    cases; in particular a runtime checking is needed.  */
    9154              : 
    9155              : static bool
    9156         1909 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9157              : {
    9158         1909 :   gfc_ref *tail;
    9159         1909 :   bool scalar;
    9160              : 
    9161         2641 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9162              : 
    9163              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9164              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9165              :      so let's emit a warning if -Wsurprising is given.  */
    9166         1909 :   scalar = !tail || tail->type == REF_COMPONENT;
    9167         1909 :   if (e1->mold && e1->rank > 0
    9168          165 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9169              :     {
    9170           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9171           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9172              :                      "but MOLD= expression at %L has rank %d",
    9173            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9174              :                      &e1->where, e1->rank);
    9175           30 :       return true;
    9176              :     }
    9177              : 
    9178              :   /* First compare rank.  */
    9179         1879 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9180            2 :       || (!tail && e1->rank != e2->rank))
    9181              :     {
    9182            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9183              :                  "same rank as the allocate-object at %L",
    9184              :                  &e1->where, &e2->where);
    9185            7 :       return false;
    9186              :     }
    9187              : 
    9188         1872 :   if (e1->shape)
    9189              :     {
    9190         1373 :       int i;
    9191         1373 :       mpz_t s;
    9192              : 
    9193         1373 :       mpz_init (s);
    9194              : 
    9195         3165 :       for (i = 0; i < e1->rank; i++)
    9196              :         {
    9197         1379 :           if (tail->u.ar.start[i] == NULL)
    9198              :             break;
    9199              : 
    9200          419 :           if (tail->u.ar.end[i])
    9201              :             {
    9202           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9203           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9204           54 :               mpz_add_ui (s, s, 1);
    9205              :             }
    9206              :           else
    9207              :             {
    9208          365 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9209              :             }
    9210              : 
    9211          419 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9212              :             {
    9213            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9214              :                          "have the same shape", &e1->where, &e2->where);
    9215            0 :               mpz_clear (s);
    9216            0 :               return false;
    9217              :             }
    9218              :         }
    9219              : 
    9220         1373 :       mpz_clear (s);
    9221              :     }
    9222              : 
    9223              :   return true;
    9224              : }
    9225              : 
    9226              : 
    9227              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9228              :    checks to see whether the expression is OK or not.  The expression must
    9229              :    have a trailing array reference that gives the size of the array.  */
    9230              : 
    9231              : static bool
    9232        17283 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9233              : {
    9234        17283 :   int i, pointer, allocatable, dimension, is_abstract;
    9235        17283 :   int codimension;
    9236        17283 :   bool coindexed;
    9237        17283 :   bool unlimited;
    9238        17283 :   symbol_attribute attr;
    9239        17283 :   gfc_ref *ref, *ref2;
    9240        17283 :   gfc_expr *e2;
    9241        17283 :   gfc_array_ref *ar;
    9242        17283 :   gfc_symbol *sym = NULL;
    9243        17283 :   gfc_alloc *a;
    9244        17283 :   gfc_component *c;
    9245        17283 :   bool t;
    9246              : 
    9247              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9248              :      checking of coarrays.  */
    9249        21974 :   for (ref = e->ref; ref; ref = ref->next)
    9250        17814 :     if (ref->next == NULL)
    9251              :       break;
    9252              : 
    9253        17283 :   if (ref && ref->type == REF_ARRAY)
    9254        11928 :     ref->u.ar.in_allocate = true;
    9255              : 
    9256        17283 :   if (!gfc_resolve_expr (e))
    9257            1 :     goto failure;
    9258              : 
    9259              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9260              :      pointer, the next-to-last reference must be a pointer.  */
    9261              : 
    9262        17282 :   ref2 = NULL;
    9263        17282 :   if (e->symtree)
    9264        17282 :     sym = e->symtree->n.sym;
    9265              : 
    9266              :   /* Check whether ultimate component is abstract and CLASS.  */
    9267        34564 :   is_abstract = 0;
    9268              : 
    9269              :   /* Is the allocate-object unlimited polymorphic?  */
    9270        17282 :   unlimited = UNLIMITED_POLY(e);
    9271              : 
    9272        17282 :   if (e->expr_type != EXPR_VARIABLE)
    9273              :     {
    9274            0 :       allocatable = 0;
    9275            0 :       attr = gfc_expr_attr (e);
    9276            0 :       pointer = attr.pointer;
    9277            0 :       dimension = attr.dimension;
    9278            0 :       codimension = attr.codimension;
    9279              :     }
    9280              :   else
    9281              :     {
    9282        17282 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9283              :         {
    9284         3390 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9285         3390 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9286         3390 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9287         3390 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9288         3390 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9289              :         }
    9290              :       else
    9291              :         {
    9292        13892 :           allocatable = sym->attr.allocatable;
    9293        13892 :           pointer = sym->attr.pointer;
    9294        13892 :           dimension = sym->attr.dimension;
    9295        13892 :           codimension = sym->attr.codimension;
    9296              :         }
    9297              : 
    9298        17282 :       coindexed = false;
    9299              : 
    9300        35090 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9301              :         {
    9302        17810 :           switch (ref->type)
    9303              :             {
    9304        13353 :               case REF_ARRAY:
    9305        13353 :                 if (ref->u.ar.codimen > 0)
    9306              :                   {
    9307          754 :                     int n;
    9308         1052 :                     for (n = ref->u.ar.dimen;
    9309         1052 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9310          795 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9311              :                         {
    9312              :                           coindexed = true;
    9313              :                           break;
    9314              :                         }
    9315              :                    }
    9316              : 
    9317        13353 :                 if (ref->next != NULL)
    9318         1427 :                   pointer = 0;
    9319              :                 break;
    9320              : 
    9321         4457 :               case REF_COMPONENT:
    9322              :                 /* F2008, C644.  */
    9323         4457 :                 if (coindexed)
    9324              :                   {
    9325            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9326              :                                &e->where);
    9327            2 :                     goto failure;
    9328              :                   }
    9329              : 
    9330         4455 :                 c = ref->u.c.component;
    9331         4455 :                 if (c->ts.type == BT_CLASS)
    9332              :                   {
    9333          988 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9334          988 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9335          988 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9336          988 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9337          988 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9338              :                   }
    9339              :                 else
    9340              :                   {
    9341         3467 :                     allocatable = c->attr.allocatable;
    9342         3467 :                     pointer = c->attr.pointer;
    9343         3467 :                     dimension = c->attr.dimension;
    9344         3467 :                     codimension = c->attr.codimension;
    9345         3467 :                     is_abstract = c->attr.abstract;
    9346              :                   }
    9347              :                 break;
    9348              : 
    9349            0 :               case REF_SUBSTRING:
    9350            0 :               case REF_INQUIRY:
    9351            0 :                 allocatable = 0;
    9352            0 :                 pointer = 0;
    9353            0 :                 break;
    9354              :             }
    9355              :         }
    9356              :     }
    9357              : 
    9358              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9359              :      pointer or an allocatable variable.  */
    9360        17280 :   if (allocatable == 0 && pointer == 0)
    9361              :     {
    9362            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9363              :                  &e->where);
    9364            4 :       goto failure;
    9365              :     }
    9366              : 
    9367              :   /* Some checks for the SOURCE tag.  */
    9368        17276 :   if (code->expr3)
    9369              :     {
    9370              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9371              :          rank as allocate-object".  This would require the MOLD argument to
    9372              :          NULL() as source-expr for subsequent checking.  However, even the
    9373              :          resulting disassociated pointer or unallocated array has no shape that
    9374              :          could be used for SOURCE= or MOLD=.  */
    9375         3840 :       if (code->expr3->expr_type == EXPR_NULL)
    9376              :         {
    9377            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9378              :                      &code->expr3->where);
    9379            4 :           goto failure;
    9380              :         }
    9381              : 
    9382              :       /* Check F03:C631.  */
    9383         3836 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9384              :         {
    9385           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9386           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9387           10 :           goto failure;
    9388              :         }
    9389              : 
    9390              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9391         3826 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9392            7 :         goto failure;
    9393              : 
    9394              :       /* Check F03:C633.  */
    9395         3819 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9396              :         {
    9397            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9398              :                      "shall have the same kind type parameter",
    9399              :                      &e->where, &code->expr3->where);
    9400            1 :           goto failure;
    9401              :         }
    9402              : 
    9403              :       /* Check F2008, C642.  */
    9404         3818 :       if (code->expr3->ts.type == BT_DERIVED
    9405         3818 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9406         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9407              :                      == INTMOD_ISO_FORTRAN_ENV
    9408            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9409              :                      == ISOFORTRAN_LOCK_TYPE)))
    9410              :         {
    9411            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9412              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9413              :                       "allocate-object at %L is a coarray",
    9414            0 :                       &code->expr3->where, &e->where);
    9415            0 :           goto failure;
    9416              :         }
    9417              : 
    9418              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9419              :          allocate-object and source-expr shall have the same values."  */
    9420         3818 :       if (e->ts.type == BT_CHARACTER
    9421          816 :           && !e->ts.deferred
    9422          162 :           && e->ts.u.cl->length
    9423          162 :           && code->expr3->ts.type == BT_CHARACTER
    9424         3980 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9425              :                                      "SOURCE= or MOLD= specifier"))
    9426           17 :             goto failure;
    9427              : 
    9428              :       /* Check TS18508, C702/C703.  */
    9429         3801 :       if (code->expr3->ts.type == BT_DERIVED
    9430         4993 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9431         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9432              :                      == INTMOD_ISO_FORTRAN_ENV
    9433            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9434              :                      == ISOFORTRAN_EVENT_TYPE)))
    9435              :         {
    9436            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9437              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9438              :                       "allocate-object at %L is a coarray",
    9439            0 :                       &code->expr3->where, &e->where);
    9440            0 :           goto failure;
    9441              :         }
    9442              :     }
    9443              : 
    9444              :   /* Check F08:C629.  */
    9445        17237 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9446          153 :       && !code->expr3)
    9447              :     {
    9448            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9449            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9450              :                  "type-spec or source-expr", sym->name, &e->where);
    9451            2 :       goto failure;
    9452              :     }
    9453              : 
    9454              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9455              :      if and only if each allocate-object is a dummy argument for which the
    9456              :      corresponding type parameter is assumed.  */
    9457        17235 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9458          513 :       && code->ext.alloc.ts.u.cl->length != NULL
    9459          498 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9460           23 :       && e->ts.u.cl->length == NULL
    9461            2 :       && e->symtree->n.sym->attr.dummy)
    9462              :     {
    9463            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9464              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9465              :                  "dummy argument with assumed type parameter",
    9466              :                  sym->name, &e->where);
    9467            2 :       goto failure;
    9468              :     }
    9469              : 
    9470              :   /* Check F08:C632.  */
    9471        17233 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9472           60 :       && !UNLIMITED_POLY (e))
    9473              :     {
    9474           36 :       int cmp;
    9475              : 
    9476           36 :       if (!e->ts.u.cl->length)
    9477           15 :         goto failure;
    9478              : 
    9479           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9480           21 :                                   code->ext.alloc.ts.u.cl->length);
    9481           21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9482              :         {
    9483            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9484              :                      "character-length parameter as in the declaration",
    9485              :                      sym->name, &e->where);
    9486            2 :           goto failure;
    9487              :         }
    9488              :     }
    9489              : 
    9490              :   /* In the variable definition context checks, gfc_expr_attr is used
    9491              :      on the expression.  This is fooled by the array specification
    9492              :      present in e, thus we have to eliminate that one temporarily.  */
    9493        17216 :   e2 = remove_last_array_ref (e);
    9494        17216 :   t = true;
    9495        17216 :   if (t && pointer)
    9496         3857 :     t = gfc_check_vardef_context (e2, true, true, false,
    9497         3857 :                                   _("ALLOCATE object"));
    9498         3857 :   if (t)
    9499        17208 :     t = gfc_check_vardef_context (e2, false, true, false,
    9500        17208 :                                   _("ALLOCATE object"));
    9501        17216 :   gfc_free_expr (e2);
    9502        17216 :   if (!t)
    9503           11 :     goto failure;
    9504              : 
    9505        17205 :   code->ext.alloc.expr3_not_explicit = 0;
    9506        17205 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9507         1611 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9508              :     {
    9509              :       /* For class arrays, the initialization with SOURCE is done
    9510              :          using _copy and trans_call. It is convenient to exploit that
    9511              :          when the allocated type is different from the declared type but
    9512              :          no SOURCE exists by setting expr3.  */
    9513          299 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9514          299 :       code->ext.alloc.expr3_not_explicit = 1;
    9515              :     }
    9516        16906 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9517         2620 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9518            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9519              :     {
    9520              :       /* We have to zero initialize the integer variable.  */
    9521            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9522            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9523              :     }
    9524              : 
    9525        17205 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9526              :     {
    9527              :       /* Make sure the vtab symbol is present when
    9528              :          the module variables are generated.  */
    9529         2972 :       gfc_typespec ts = e->ts;
    9530         2972 :       if (code->expr3)
    9531         1325 :         ts = code->expr3->ts;
    9532         1647 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9533          714 :         ts = code->ext.alloc.ts;
    9534              : 
    9535              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9536              :          statement is necessary.  */
    9537         2972 :       gfc_find_derived_vtab (ts.u.derived);
    9538         2972 :     }
    9539        14233 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9540              :     {
    9541              :       /* Again, make sure the vtab symbol is present when
    9542              :          the module variables are generated.  */
    9543          434 :       gfc_typespec *ts = NULL;
    9544          434 :       if (code->expr3)
    9545          347 :         ts = &code->expr3->ts;
    9546              :       else
    9547           87 :         ts = &code->ext.alloc.ts;
    9548              : 
    9549          434 :       gcc_assert (ts);
    9550              : 
    9551              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9552              :          statement is necessary.  */
    9553          434 :       gfc_find_vtab (ts);
    9554              :     }
    9555              : 
    9556        17205 :   if (dimension == 0 && codimension == 0)
    9557         5308 :     goto success;
    9558              : 
    9559              :   /* Make sure the last reference node is an array specification.  */
    9560              : 
    9561        11897 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9562        10665 :       || (dimension && ref2->u.ar.dimen == 0))
    9563              :     {
    9564              :       /* F08:C633.  */
    9565         1232 :       if (code->expr3)
    9566              :         {
    9567         1231 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9568              :                                "in ALLOCATE statement at %L", &e->where))
    9569            0 :             goto failure;
    9570         1231 :           if (code->expr3->rank != 0)
    9571         1230 :             *array_alloc_wo_spec = true;
    9572              :           else
    9573              :             {
    9574            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9575              :                          "expression required in ALLOCATE statement at %L",
    9576              :                          &e->where);
    9577            1 :               goto failure;
    9578              :             }
    9579              :         }
    9580              :       else
    9581              :         {
    9582            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9583              :                      "at %L", &e->where);
    9584            1 :           goto failure;
    9585              :         }
    9586              :     }
    9587              : 
    9588              :   /* Make sure that the array section reference makes sense in the
    9589              :      context of an ALLOCATE specification.  */
    9590              : 
    9591        11895 :   ar = &ref2->u.ar;
    9592              : 
    9593        11895 :   if (codimension)
    9594         1173 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9595              :       {
    9596          689 :         switch (ar->dimen_type[i])
    9597              :           {
    9598            2 :           case DIMEN_THIS_IMAGE:
    9599            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9600              :                        "at %L", &e->where);
    9601            2 :             goto failure;
    9602              : 
    9603           98 :           case  DIMEN_RANGE:
    9604              :             /* F2018:R937:
    9605              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9606              :              */
    9607           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9608              :               {
    9609            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9610              :                            "at %L", &e->where);
    9611            8 :                 goto failure;
    9612              :               }
    9613           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9614              :               {
    9615            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9616            2 :                            &ar->start[i]->where);
    9617            2 :                 goto failure;
    9618              :               }
    9619              :             break;
    9620              : 
    9621          105 :           case DIMEN_ELEMENT:
    9622          105 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9623              :               {
    9624           97 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9625           97 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9626              :                   {
    9627            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9628              :                                "of 1 at %L", &ar->start[i]->where);
    9629            1 :                     goto failure;
    9630              :                   }
    9631              :               }
    9632              :             break;
    9633              : 
    9634              :           case DIMEN_STAR:
    9635              :             break;
    9636              : 
    9637            0 :           default:
    9638            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9639              :                        &e->where);
    9640            0 :             goto failure;
    9641              : 
    9642              :           }
    9643              :       }
    9644        29143 :   for (i = 0; i < ar->dimen; i++)
    9645              :     {
    9646        17265 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9647        14555 :         goto check_symbols;
    9648              : 
    9649         2710 :       switch (ar->dimen_type[i])
    9650              :         {
    9651              :         case DIMEN_ELEMENT:
    9652              :           break;
    9653              : 
    9654         2444 :         case DIMEN_RANGE:
    9655         2444 :           if (ar->start[i] != NULL
    9656         2444 :               && ar->end[i] != NULL
    9657         2443 :               && ar->stride[i] == NULL)
    9658              :             break;
    9659              : 
    9660              :           /* Fall through.  */
    9661              : 
    9662            1 :         case DIMEN_UNKNOWN:
    9663            1 :         case DIMEN_VECTOR:
    9664            1 :         case DIMEN_STAR:
    9665            1 :         case DIMEN_THIS_IMAGE:
    9666            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9667              :                      &e->where);
    9668            1 :           goto failure;
    9669              :         }
    9670              : 
    9671         2443 : check_symbols:
    9672        44951 :       for (a = code->ext.alloc.list; a; a = a->next)
    9673              :         {
    9674        27690 :           sym = a->expr->symtree->n.sym;
    9675              : 
    9676              :           /* TODO - check derived type components.  */
    9677        27690 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9678         9275 :             continue;
    9679              : 
    9680        18415 :           if ((ar->start[i] != NULL
    9681        17735 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9682        36147 :               || (ar->end[i] != NULL
    9683         2722 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9684              :             {
    9685            3 :               gfc_error ("%qs must not appear in the array specification at "
    9686              :                          "%L in the same ALLOCATE statement where it is "
    9687              :                          "itself allocated", sym->name, &ar->where);
    9688            3 :               goto failure;
    9689              :             }
    9690              :         }
    9691              :     }
    9692              : 
    9693        12069 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9694              :     {
    9695          865 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9696          674 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9697              :         {
    9698          191 :           if (i == (ar->dimen + ar->codimen - 1))
    9699              :             {
    9700            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9701              :                          "statement at %L", &e->where);
    9702            0 :               goto failure;
    9703              :             }
    9704          191 :           continue;
    9705              :         }
    9706              : 
    9707          483 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9708          483 :           && ar->stride[i] == NULL)
    9709              :         break;
    9710              : 
    9711            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9712              :                  &e->where);
    9713            0 :       goto failure;
    9714              :     }
    9715              : 
    9716        11878 : success:
    9717              :   return true;
    9718              : 
    9719              : failure:
    9720              :   return false;
    9721              : }
    9722              : 
    9723              : 
    9724              : static void
    9725        20324 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9726              : {
    9727        20324 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9728        20324 :   gfc_alloc *a, *p, *q;
    9729              : 
    9730        20324 :   stat = code->expr1;
    9731        20324 :   errmsg = code->expr2;
    9732              : 
    9733              :   /* Check the stat variable.  */
    9734        20324 :   if (stat)
    9735              :     {
    9736          661 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9737          661 :                                      _("STAT variable")))
    9738            8 :           goto done_stat;
    9739              : 
    9740          653 :       if (stat->ts.type != BT_INTEGER
    9741          644 :           || stat->rank > 0)
    9742           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9743              :                    "variable", &stat->where);
    9744              : 
    9745          653 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9746            0 :         goto done_stat;
    9747              : 
    9748              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9749              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9750              :        */
    9751         1354 :       for (p = code->ext.alloc.list; p; p = p->next)
    9752          708 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9753              :           {
    9754            9 :             gfc_ref *ref1, *ref2;
    9755            9 :             bool found = true;
    9756              : 
    9757           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9758            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9759              :               {
    9760            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9761            5 :                   continue;
    9762            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9763              :                   {
    9764              :                     found = false;
    9765              :                     break;
    9766              :                   }
    9767              :               }
    9768              : 
    9769            9 :             if (found)
    9770              :               {
    9771            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9772              :                            "the same %s statement", &stat->where, fcn, fcn);
    9773            7 :                 break;
    9774              :               }
    9775              :           }
    9776              :     }
    9777              : 
    9778        19663 : done_stat:
    9779              : 
    9780              :   /* Check the errmsg variable.  */
    9781        20324 :   if (errmsg)
    9782              :     {
    9783          150 :       if (!stat)
    9784            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9785              :                      &errmsg->where);
    9786              : 
    9787          150 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9788          150 :                                      _("ERRMSG variable")))
    9789            6 :           goto done_errmsg;
    9790              : 
    9791              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9792              :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9793              :          F18:R906  default-char-variable is variable
    9794              :          F18:C906  default-char-variable shall be default character.  */
    9795          144 :       if (errmsg->ts.type != BT_CHARACTER
    9796          142 :           || errmsg->rank > 0
    9797          141 :           || errmsg->ts.kind != gfc_default_character_kind)
    9798            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9799              :                    "variable", &errmsg->where);
    9800              : 
    9801          144 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9802            0 :         goto done_errmsg;
    9803              : 
    9804              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9805              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9806              :        */
    9807          286 :       for (p = code->ext.alloc.list; p; p = p->next)
    9808          147 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9809              :           {
    9810            9 :             gfc_ref *ref1, *ref2;
    9811            9 :             bool found = true;
    9812              : 
    9813           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9814            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9815              :               {
    9816           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9817            4 :                   continue;
    9818            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9819              :                   {
    9820              :                     found = false;
    9821              :                     break;
    9822              :                   }
    9823              :               }
    9824              : 
    9825            9 :             if (found)
    9826              :               {
    9827            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9828              :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9829            5 :                 break;
    9830              :               }
    9831              :           }
    9832              :     }
    9833              : 
    9834        20174 : done_errmsg:
    9835              : 
    9836              :   /* Check that an allocate-object appears only once in the statement.  */
    9837              : 
    9838        45943 :   for (p = code->ext.alloc.list; p; p = p->next)
    9839              :     {
    9840        25619 :       pe = p->expr;
    9841        34891 :       for (q = p->next; q; q = q->next)
    9842              :         {
    9843         9272 :           qe = q->expr;
    9844         9272 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9845              :             {
    9846              :               /* This is a potential collision.  */
    9847         2093 :               gfc_ref *pr = pe->ref;
    9848         2093 :               gfc_ref *qr = qe->ref;
    9849              : 
    9850              :               /* Follow the references  until
    9851              :                  a) They start to differ, in which case there is no error;
    9852              :                  you can deallocate a%b and a%c in a single statement
    9853              :                  b) Both of them stop, which is an error
    9854              :                  c) One of them stops, which is also an error.  */
    9855         4517 :               while (1)
    9856              :                 {
    9857         3305 :                   if (pr == NULL && qr == NULL)
    9858              :                     {
    9859            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9860              :                                  &pe->where, &qe->where);
    9861            7 :                       break;
    9862              :                     }
    9863         3298 :                   else if (pr != NULL && qr == NULL)
    9864              :                     {
    9865            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9866              :                                  " object at %L", &pe->where, &qe->where);
    9867            2 :                       break;
    9868              :                     }
    9869         3296 :                   else if (pr == NULL && qr != NULL)
    9870              :                     {
    9871            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9872              :                                  " object at %L", &qe->where, &pe->where);
    9873            2 :                       break;
    9874              :                     }
    9875              :                   /* Here, pr != NULL && qr != NULL  */
    9876         3294 :                   gcc_assert(pr->type == qr->type);
    9877         3294 :                   if (pr->type == REF_ARRAY)
    9878              :                     {
    9879              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9880              :                          which are legal.  */
    9881         1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9882              : 
    9883         1065 :                       if (pr->next && qr->next)
    9884              :                         {
    9885              :                           int i;
    9886              :                           gfc_array_ref *par = &(pr->u.ar);
    9887              :                           gfc_array_ref *qar = &(qr->u.ar);
    9888              : 
    9889         1840 :                           for (i=0; i<par->dimen; i++)
    9890              :                             {
    9891          954 :                               if ((par->start[i] != NULL
    9892            0 :                                    || qar->start[i] != NULL)
    9893         1908 :                                   && gfc_dep_compare_expr (par->start[i],
    9894          954 :                                                            qar->start[i]) != 0)
    9895          168 :                                 goto break_label;
    9896              :                             }
    9897              :                         }
    9898              :                     }
    9899              :                   else
    9900              :                     {
    9901         2229 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9902              :                         break;
    9903              :                     }
    9904              : 
    9905         1212 :                   pr = pr->next;
    9906         1212 :                   qr = qr->next;
    9907         1212 :                 }
    9908         9272 :             break_label:
    9909              :               ;
    9910              :             }
    9911              :         }
    9912              :     }
    9913              : 
    9914        20324 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9915              :     {
    9916        14256 :       bool arr_alloc_wo_spec = false;
    9917              : 
    9918              :       /* Resolving the expr3 in the loop over all objects to allocate would
    9919              :          execute loop invariant code for each loop item.  Therefore do it just
    9920              :          once here.  */
    9921        14256 :       if (code->expr3 && code->expr3->mold
    9922          350 :           && code->expr3->ts.type == BT_DERIVED
    9923           24 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
    9924              :         {
    9925              :           /* Default initialization via MOLD (non-polymorphic).  */
    9926           22 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9927           22 :           if (rhs != NULL)
    9928              :             {
    9929            9 :               gfc_resolve_expr (rhs);
    9930            9 :               gfc_free_expr (code->expr3);
    9931            9 :               code->expr3 = rhs;
    9932              :             }
    9933              :         }
    9934        31539 :       for (a = code->ext.alloc.list; a; a = a->next)
    9935        17283 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9936              : 
    9937        14256 :       if (arr_alloc_wo_spec && code->expr3)
    9938              :         {
    9939              :           /* Mark the allocate to have to take the array specification
    9940              :              from the expr3.  */
    9941         1224 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9942              :         }
    9943              :     }
    9944              :   else
    9945              :     {
    9946        14404 :       for (a = code->ext.alloc.list; a; a = a->next)
    9947         8336 :         resolve_deallocate_expr (a->expr);
    9948              :     }
    9949        20324 : }
    9950              : 
    9951              : 
    9952              : /************ SELECT CASE resolution subroutines ************/
    9953              : 
    9954              : /* Callback function for our mergesort variant.  Determines interval
    9955              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
    9956              :    op1 > op2.  Assumes we're not dealing with the default case.
    9957              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
    9958              :    There are nine situations to check.  */
    9959              : 
    9960              : static int
    9961         1578 : compare_cases (const gfc_case *op1, const gfc_case *op2)
    9962              : {
    9963         1578 :   int retval;
    9964              : 
    9965         1578 :   if (op1->low == NULL) /* op1 = (:L)  */
    9966              :     {
    9967              :       /* op2 = (:N), so overlap.  */
    9968           52 :       retval = 0;
    9969              :       /* op2 = (M:) or (M:N),  L < M  */
    9970           52 :       if (op2->low != NULL
    9971           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9972              :         retval = -1;
    9973              :     }
    9974         1526 :   else if (op1->high == NULL) /* op1 = (K:)  */
    9975              :     {
    9976              :       /* op2 = (M:), so overlap.  */
    9977           10 :       retval = 0;
    9978              :       /* op2 = (:N) or (M:N), K > N  */
    9979           10 :       if (op2->high != NULL
    9980           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9981              :         retval = 1;
    9982              :     }
    9983              :   else /* op1 = (K:L)  */
    9984              :     {
    9985         1516 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
    9986           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9987           18 :                  ? 1 : 0;
    9988         1498 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
    9989           14 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9990           10 :                  ? -1 : 0;
    9991              :       else                      /* op2 = (M:N)  */
    9992              :         {
    9993         1488 :           retval =  0;
    9994              :           /* L < M  */
    9995         1488 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9996              :             retval =  -1;
    9997              :           /* K > N  */
    9998          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9999          438 :             retval =  1;
   10000              :         }
   10001              :     }
   10002              : 
   10003         1578 :   return retval;
   10004              : }
   10005              : 
   10006              : 
   10007              : /* Merge-sort a double linked case list, detecting overlap in the
   10008              :    process.  LIST is the head of the double linked case list before it
   10009              :    is sorted.  Returns the head of the sorted list if we don't see any
   10010              :    overlap, or NULL otherwise.  */
   10011              : 
   10012              : static gfc_case *
   10013          646 : check_case_overlap (gfc_case *list)
   10014              : {
   10015          646 :   gfc_case *p, *q, *e, *tail;
   10016          646 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10017              : 
   10018              :   /* If the passed list was empty, return immediately.  */
   10019          646 :   if (!list)
   10020              :     return NULL;
   10021              : 
   10022              :   overlap_seen = 0;
   10023              :   insize = 1;
   10024              : 
   10025              :   /* Loop unconditionally.  The only exit from this loop is a return
   10026              :      statement, when we've finished sorting the case list.  */
   10027         1350 :   for (;;)
   10028              :     {
   10029          998 :       p = list;
   10030          998 :       list = NULL;
   10031          998 :       tail = NULL;
   10032              : 
   10033              :       /* Count the number of merges we do in this pass.  */
   10034          998 :       nmerges = 0;
   10035              : 
   10036              :       /* Loop while there exists a merge to be done.  */
   10037         2523 :       while (p)
   10038              :         {
   10039         1525 :           int i;
   10040              : 
   10041              :           /* Count this merge.  */
   10042         1525 :           nmerges++;
   10043              : 
   10044              :           /* Cut the list in two pieces by stepping INSIZE places
   10045              :              forward in the list, starting from P.  */
   10046         1525 :           psize = 0;
   10047         1525 :           q = p;
   10048         3208 :           for (i = 0; i < insize; i++)
   10049              :             {
   10050         2243 :               psize++;
   10051         2243 :               q = q->right;
   10052         2243 :               if (!q)
   10053              :                 break;
   10054              :             }
   10055              :           qsize = insize;
   10056              : 
   10057              :           /* Now we have two lists.  Merge them!  */
   10058         5013 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10059              :             {
   10060              :               /* See from which the next case to merge comes from.  */
   10061          807 :               if (psize == 0)
   10062              :                 {
   10063              :                   /* P is empty so the next case must come from Q.  */
   10064          807 :                   e = q;
   10065          807 :                   q = q->right;
   10066          807 :                   qsize--;
   10067              :                 }
   10068         2681 :               else if (qsize == 0 || q == NULL)
   10069              :                 {
   10070              :                   /* Q is empty.  */
   10071         1103 :                   e = p;
   10072         1103 :                   p = p->right;
   10073         1103 :                   psize--;
   10074              :                 }
   10075              :               else
   10076              :                 {
   10077         1578 :                   cmp = compare_cases (p, q);
   10078         1578 :                   if (cmp < 0)
   10079              :                     {
   10080              :                       /* The whole case range for P is less than the
   10081              :                          one for Q.  */
   10082         1136 :                       e = p;
   10083         1136 :                       p = p->right;
   10084         1136 :                       psize--;
   10085              :                     }
   10086          442 :                   else if (cmp > 0)
   10087              :                     {
   10088              :                       /* The whole case range for Q is greater than
   10089              :                          the case range for P.  */
   10090          438 :                       e = q;
   10091          438 :                       q = q->right;
   10092          438 :                       qsize--;
   10093              :                     }
   10094              :                   else
   10095              :                     {
   10096              :                       /* The cases overlap, or they are the same
   10097              :                          element in the list.  Either way, we must
   10098              :                          issue an error and get the next case from P.  */
   10099              :                       /* FIXME: Sort P and Q by line number.  */
   10100            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10101              :                                  "label at %L", &p->where, &q->where);
   10102            4 :                       overlap_seen = 1;
   10103            4 :                       e = p;
   10104            4 :                       p = p->right;
   10105            4 :                       psize--;
   10106              :                     }
   10107              :                 }
   10108              : 
   10109              :                 /* Add the next element to the merged list.  */
   10110         3488 :               if (tail)
   10111         2490 :                 tail->right = e;
   10112              :               else
   10113              :                 list = e;
   10114         3488 :               e->left = tail;
   10115         3488 :               tail = e;
   10116              :             }
   10117              : 
   10118              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10119              :              they're the same.  */
   10120              :           p = q;
   10121              :         }
   10122          998 :       tail->right = NULL;
   10123              : 
   10124              :       /* If we have done only one merge or none at all, we've
   10125              :          finished sorting the cases.  */
   10126          998 :       if (nmerges <= 1)
   10127              :         {
   10128          646 :           if (!overlap_seen)
   10129              :             return list;
   10130              :           else
   10131              :             return NULL;
   10132              :         }
   10133              : 
   10134              :       /* Otherwise repeat, merging lists twice the size.  */
   10135          352 :       insize *= 2;
   10136          352 :     }
   10137              : }
   10138              : 
   10139              : 
   10140              : /* Check to see if an expression is suitable for use in a CASE statement.
   10141              :    Makes sure that all case expressions are scalar constants of the same
   10142              :    type.  Return false if anything is wrong.  */
   10143              : 
   10144              : static bool
   10145         3307 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10146              : {
   10147         3307 :   if (e == NULL) return true;
   10148              : 
   10149         3214 :   if (e->ts.type != case_expr->ts.type)
   10150              :     {
   10151            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10152              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10153            4 :       return false;
   10154              :     }
   10155              : 
   10156              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10157              :      the same type as case-expr.  For character type, length differences
   10158              :      are allowed, but the kind type parameters shall be the same.  */
   10159              : 
   10160         3210 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10161              :     {
   10162            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10163              :                  &e->where, case_expr->ts.kind);
   10164            4 :       return false;
   10165              :     }
   10166              : 
   10167              :   /* Convert the case value kind to that of case expression kind,
   10168              :      if needed */
   10169              : 
   10170         3206 :   if (e->ts.kind != case_expr->ts.kind)
   10171           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10172              : 
   10173         3206 :   if (e->rank != 0)
   10174              :     {
   10175            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10176              :                  &e->where);
   10177            0 :       return false;
   10178              :     }
   10179              : 
   10180              :   return true;
   10181              : }
   10182              : 
   10183              : 
   10184              : /* Given a completely parsed select statement, we:
   10185              : 
   10186              :      - Validate all expressions and code within the SELECT.
   10187              :      - Make sure that the selection expression is not of the wrong type.
   10188              :      - Make sure that no case ranges overlap.
   10189              :      - Eliminate unreachable cases and unreachable code resulting from
   10190              :        removing case labels.
   10191              : 
   10192              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10193              :    they are a hassle for code generation, and to prevent that, we just
   10194              :    cut them out here.  This is not necessary for overlapping cases
   10195              :    because they are illegal and we never even try to generate code.
   10196              : 
   10197              :    We have the additional caveat that a SELECT construct could have
   10198              :    been a computed GOTO in the source code. Fortunately we can fairly
   10199              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10200              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10201              :    we have to do is make sure that the case_expr is a scalar integer
   10202              :    expression.  */
   10203              : 
   10204              : static void
   10205          687 : resolve_select (gfc_code *code, bool select_type)
   10206              : {
   10207          687 :   gfc_code *body;
   10208          687 :   gfc_expr *case_expr;
   10209          687 :   gfc_case *cp, *default_case, *tail, *head;
   10210          687 :   int seen_unreachable;
   10211          687 :   int seen_logical;
   10212          687 :   int ncases;
   10213          687 :   bt type;
   10214          687 :   bool t;
   10215              : 
   10216          687 :   if (code->expr1 == NULL)
   10217              :     {
   10218              :       /* This was actually a computed GOTO statement.  */
   10219            5 :       case_expr = code->expr2;
   10220            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10221            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10222              :                    "at %L must be a scalar integer expression",
   10223              :                    &case_expr->where);
   10224              : 
   10225              :       /* Further checking is not necessary because this SELECT was built
   10226              :          by the compiler, so it should always be OK.  Just move the
   10227              :          case_expr from expr2 to expr so that we can handle computed
   10228              :          GOTOs as normal SELECTs from here on.  */
   10229            5 :       code->expr1 = code->expr2;
   10230            5 :       code->expr2 = NULL;
   10231            5 :       return;
   10232              :     }
   10233              : 
   10234          682 :   case_expr = code->expr1;
   10235          682 :   type = case_expr->ts.type;
   10236              : 
   10237              :   /* F08:C830.  */
   10238          682 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10239            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10240              : 
   10241              :     {
   10242            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10243              :                  &case_expr->where, gfc_typename (case_expr));
   10244              : 
   10245              :       /* Punt. Going on here just produce more garbage error messages.  */
   10246            0 :       return;
   10247              :     }
   10248              : 
   10249              :   /* F08:R842.  */
   10250          682 :   if (!select_type && case_expr->rank != 0)
   10251              :     {
   10252            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10253              :                  "expression", &case_expr->where);
   10254              : 
   10255              :       /* Punt.  */
   10256            1 :       return;
   10257              :     }
   10258              : 
   10259              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10260              :      the case-expr. Later, all expressions will be promoted to the
   10261              :      largest kind of all case-labels.  */
   10262              : 
   10263          681 :   if (type == BT_INTEGER)
   10264         1927 :     for (body = code->block; body; body = body->block)
   10265         2852 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10266              :         {
   10267         1462 :           if (cp->low
   10268         1462 :               && gfc_check_integer_range (cp->low->value.integer,
   10269              :                                           case_expr->ts.kind) != ARITH_OK)
   10270            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10271            6 :                          "not in the range of %s", &cp->low->where,
   10272              :                          gfc_typename (case_expr));
   10273              : 
   10274         1462 :           if (cp->high
   10275         1178 :               && cp->low != cp->high
   10276         1570 :               && gfc_check_integer_range (cp->high->value.integer,
   10277              :                                           case_expr->ts.kind) != ARITH_OK)
   10278            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10279            0 :                          "not in the range of %s", &cp->high->where,
   10280              :                          gfc_typename (case_expr));
   10281              :         }
   10282              : 
   10283              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10284              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10285              :      of case values, and if we find a mismatch, promote case_expr to
   10286              :      the appropriate kind.  */
   10287              : 
   10288          681 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10289              :     {
   10290         2113 :       for (body = code->block; body; body = body->block)
   10291              :         {
   10292              :           /* Walk the case label list.  */
   10293         3113 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10294              :             {
   10295              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10296         1597 :               if (cp->low == NULL && cp->high == NULL)
   10297          292 :                 continue;
   10298              : 
   10299              :               /* Unreachable case ranges are discarded, so ignore.  */
   10300         1260 :               if (cp->low != NULL && cp->high != NULL
   10301         1212 :                   && cp->low != cp->high
   10302         1370 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10303           33 :                 continue;
   10304              : 
   10305         1272 :               if (cp->low != NULL
   10306         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10307           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10308              : 
   10309         1272 :               if (cp->high != NULL
   10310         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10311            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10312              :             }
   10313              :          }
   10314              :     }
   10315              : 
   10316              :   /* Assume there is no DEFAULT case.  */
   10317          681 :   default_case = NULL;
   10318          681 :   head = tail = NULL;
   10319          681 :   ncases = 0;
   10320          681 :   seen_logical = 0;
   10321              : 
   10322         2502 :   for (body = code->block; body; body = body->block)
   10323              :     {
   10324              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10325         1821 :       t = true;
   10326         1821 :       seen_unreachable = 0;
   10327              : 
   10328              :       /* Walk the case label list, making sure that all case labels
   10329              :          are legal.  */
   10330         3829 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10331              :         {
   10332              :           /* Count the number of cases in the whole construct.  */
   10333         2019 :           ncases++;
   10334              : 
   10335              :           /* Intercept the DEFAULT case.  */
   10336         2019 :           if (cp->low == NULL && cp->high == NULL)
   10337              :             {
   10338          362 :               if (default_case != NULL)
   10339              :                 {
   10340            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10341              :                              "by a second DEFAULT CASE at %L",
   10342              :                              &default_case->where, &cp->where);
   10343            0 :                   t = false;
   10344            0 :                   break;
   10345              :                 }
   10346              :               else
   10347              :                 {
   10348          362 :                   default_case = cp;
   10349          362 :                   continue;
   10350              :                 }
   10351              :             }
   10352              : 
   10353              :           /* Deal with single value cases and case ranges.  Errors are
   10354              :              issued from the validation function.  */
   10355         1657 :           if (!validate_case_label_expr (cp->low, case_expr)
   10356         1657 :               || !validate_case_label_expr (cp->high, case_expr))
   10357              :             {
   10358              :               t = false;
   10359              :               break;
   10360              :             }
   10361              : 
   10362         1649 :           if (type == BT_LOGICAL
   10363           78 :               && ((cp->low == NULL || cp->high == NULL)
   10364           76 :                   || cp->low != cp->high))
   10365              :             {
   10366            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10367              :                          "allowed",
   10368            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10369            2 :               t = false;
   10370            2 :               break;
   10371              :             }
   10372              : 
   10373           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10374              :             {
   10375           76 :               int value;
   10376           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10377           76 :               if (value & seen_logical)
   10378              :                 {
   10379            1 :                   gfc_error ("Constant logical value in CASE statement "
   10380              :                              "is repeated at %L",
   10381              :                              &cp->low->where);
   10382            1 :                   t = false;
   10383            1 :                   break;
   10384              :                 }
   10385           75 :               seen_logical |= value;
   10386              :             }
   10387              : 
   10388         1602 :           if (cp->low != NULL && cp->high != NULL
   10389         1555 :               && cp->low != cp->high
   10390         1758 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10391              :             {
   10392           35 :               if (warn_surprising)
   10393            1 :                 gfc_warning (OPT_Wsurprising,
   10394              :                              "Range specification at %L can never be matched",
   10395              :                              &cp->where);
   10396              : 
   10397           35 :               cp->unreachable = 1;
   10398           35 :               seen_unreachable = 1;
   10399              :             }
   10400              :           else
   10401              :             {
   10402              :               /* If the case range can be matched, it can also overlap with
   10403              :                  other cases.  To make sure it does not, we put it in a
   10404              :                  double linked list here.  We sort that with a merge sort
   10405              :                  later on to detect any overlapping cases.  */
   10406         1611 :               if (!head)
   10407              :                 {
   10408          646 :                   head = tail = cp;
   10409          646 :                   head->right = head->left = NULL;
   10410              :                 }
   10411              :               else
   10412              :                 {
   10413          965 :                   tail->right = cp;
   10414          965 :                   tail->right->left = tail;
   10415          965 :                   tail = tail->right;
   10416          965 :                   tail->right = NULL;
   10417              :                 }
   10418              :             }
   10419              :         }
   10420              : 
   10421              :       /* It there was a failure in the previous case label, give up
   10422              :          for this case label list.  Continue with the next block.  */
   10423         1821 :       if (!t)
   10424           11 :         continue;
   10425              : 
   10426              :       /* See if any case labels that are unreachable have been seen.
   10427              :          If so, we eliminate them.  This is a bit of a kludge because
   10428              :          the case lists for a single case statement (label) is a
   10429              :          single forward linked lists.  */
   10430         1810 :       if (seen_unreachable)
   10431              :       {
   10432              :         /* Advance until the first case in the list is reachable.  */
   10433           69 :         while (body->ext.block.case_list != NULL
   10434           69 :                && body->ext.block.case_list->unreachable)
   10435              :           {
   10436           34 :             gfc_case *n = body->ext.block.case_list;
   10437           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10438           34 :             n->next = NULL;
   10439           34 :             gfc_free_case_list (n);
   10440              :           }
   10441              : 
   10442              :         /* Strip all other unreachable cases.  */
   10443           35 :         if (body->ext.block.case_list)
   10444              :           {
   10445            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10446              :               {
   10447            1 :                 if (cp->next->unreachable)
   10448              :                   {
   10449            1 :                     gfc_case *n = cp->next;
   10450            1 :                     cp->next = cp->next->next;
   10451            1 :                     n->next = NULL;
   10452            1 :                     gfc_free_case_list (n);
   10453              :                   }
   10454              :               }
   10455              :           }
   10456              :       }
   10457              :     }
   10458              : 
   10459              :   /* See if there were overlapping cases.  If the check returns NULL,
   10460              :      there was overlap.  In that case we don't do anything.  If head
   10461              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10462              :      then used during code generation for SELECT CASE constructs with
   10463              :      a case expression of a CHARACTER type.  */
   10464          681 :   if (head)
   10465              :     {
   10466          646 :       head = check_case_overlap (head);
   10467              : 
   10468              :       /* Prepend the default_case if it is there.  */
   10469          646 :       if (head != NULL && default_case)
   10470              :         {
   10471          345 :           default_case->left = NULL;
   10472          345 :           default_case->right = head;
   10473          345 :           head->left = default_case;
   10474              :         }
   10475              :     }
   10476              : 
   10477              :   /* Eliminate dead blocks that may be the result if we've seen
   10478              :      unreachable case labels for a block.  */
   10479         2468 :   for (body = code; body && body->block; body = body->block)
   10480              :     {
   10481         1787 :       if (body->block->ext.block.case_list == NULL)
   10482              :         {
   10483              :           /* Cut the unreachable block from the code chain.  */
   10484           34 :           gfc_code *c = body->block;
   10485           34 :           body->block = c->block;
   10486              : 
   10487              :           /* Kill the dead block, but not the blocks below it.  */
   10488           34 :           c->block = NULL;
   10489           34 :           gfc_free_statements (c);
   10490              :         }
   10491              :     }
   10492              : 
   10493              :   /* More than two cases is legal but insane for logical selects.
   10494              :      Issue a warning for it.  */
   10495          681 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10496            0 :     gfc_warning (OPT_Wsurprising,
   10497              :                  "Logical SELECT CASE block at %L has more that two cases",
   10498              :                  &code->loc);
   10499              : }
   10500              : 
   10501              : 
   10502              : /* Check if a derived type is extensible.  */
   10503              : 
   10504              : bool
   10505        23945 : gfc_type_is_extensible (gfc_symbol *sym)
   10506              : {
   10507        23945 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10508        23929 :            || (sym->attr.is_class
   10509         2208 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10510              : }
   10511              : 
   10512              : 
   10513              : static void
   10514              : resolve_types (gfc_namespace *ns);
   10515              : 
   10516              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10517              :    correct as well as possibly the array-spec.  */
   10518              : 
   10519              : static void
   10520        12773 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10521              : {
   10522        12773 :   gfc_expr* target;
   10523        12773 :   bool parentheses = false;
   10524              : 
   10525        12773 :   gcc_assert (sym->assoc);
   10526        12773 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10527              : 
   10528        12773 :   if (sym->assoc->target
   10529         7613 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10530          540 :       && sym->assoc->target->symtree
   10531          540 :       && sym->assoc->target->symtree->n.sym
   10532          540 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10533              :     {
   10534           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10535           33 :         sym->ts = sym->assoc->target->ts;
   10536              :       else
   10537              :         {
   10538            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10539            0 :                      sym->assoc->target->symtree->n.sym->name,
   10540            0 :                      &sym->assoc->target->where);
   10541            0 :           return;
   10542              :         }
   10543              :     }
   10544              : 
   10545              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10546              :      case, return.  Resolution will be called later manually again when
   10547              :      this is done.  */
   10548        12773 :   target = sym->assoc->target;
   10549        12773 :   if (!target)
   10550              :     return;
   10551         7613 :   gcc_assert (!sym->assoc->dangling);
   10552              : 
   10553         7613 :   if (target->expr_type == EXPR_OP
   10554          261 :       && target->value.op.op == INTRINSIC_PARENTHESES
   10555           42 :       && target->value.op.op1->expr_type == EXPR_VARIABLE)
   10556              :     {
   10557           23 :       sym->assoc->target = gfc_copy_expr (target->value.op.op1);
   10558           23 :       gfc_free_expr (target);
   10559           23 :       target = sym->assoc->target;
   10560           23 :       parentheses = true;
   10561              :     }
   10562              : 
   10563         7613 :   if (resolve_target && !gfc_resolve_expr (target))
   10564              :     return;
   10565              : 
   10566         7608 :   if (sym->assoc->ar)
   10567              :     {
   10568              :       int dim;
   10569              :       gfc_array_ref *ar = sym->assoc->ar;
   10570           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10571              :         {
   10572           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10573           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10574           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10575           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10576            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10577              :                        "remapping of associate name %s at %L",
   10578              :                        sym->name, &sym->declared_at);
   10579              :         }
   10580              :     }
   10581              : 
   10582              :   /* For variable targets, we get some attributes from the target.  */
   10583         7608 :   if (target->expr_type == EXPR_VARIABLE)
   10584              :     {
   10585         6623 :       gfc_symbol *tsym, *dsym;
   10586              : 
   10587         6623 :       gcc_assert (target->symtree);
   10588         6623 :       tsym = target->symtree->n.sym;
   10589              : 
   10590         6623 :       if (gfc_expr_attr (target).proc_pointer)
   10591              :         {
   10592            0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10593              :                      tsym->name, &target->where);
   10594            0 :           return;
   10595              :         }
   10596              : 
   10597           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10598            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10599         6624 :           && dsym->attr.flavor == FL_DERIVED)
   10600              :         {
   10601            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10602              :                      tsym->name, &target->where);
   10603            1 :           return;
   10604              :         }
   10605              : 
   10606         6622 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10607              :         {
   10608           73 :           bool is_error = true;
   10609           73 :           if (tsym->attr.function && tsym->result == tsym)
   10610          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10611          137 :               if (tsym == ns->proc_name)
   10612              :                 {
   10613              :                   is_error = false;
   10614              :                   break;
   10615              :                 }
   10616           64 :           if (is_error)
   10617              :             {
   10618           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10619              :                          tsym->name, &target->where);
   10620           13 :               return;
   10621              :             }
   10622              :         }
   10623              : 
   10624         6609 :       sym->attr.asynchronous = tsym->attr.asynchronous;
   10625         6609 :       sym->attr.volatile_ = tsym->attr.volatile_;
   10626              : 
   10627        13218 :       sym->attr.target = tsym->attr.target
   10628         6609 :                          || gfc_expr_attr (target).pointer;
   10629         6609 :       if (is_subref_array (target))
   10630          402 :         sym->attr.subref_array_pointer = 1;
   10631              :     }
   10632          985 :   else if (target->ts.type == BT_PROCEDURE)
   10633              :     {
   10634            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10635              :                  &target->where);
   10636            0 :       return;
   10637              :     }
   10638              : 
   10639         7594 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10640              :     {
   10641              :       /* By now, the type of the target has been fixed up.  */
   10642          293 :       symbol_attribute attr;
   10643              : 
   10644          293 :       if (sym->ts.type == BT_DERIVED
   10645          166 :           && target->ts.type == BT_CLASS
   10646           31 :           && !UNLIMITED_POLY (target))
   10647              :         {
   10648              :           /* Inferred to be derived type but the target has type class.  */
   10649           31 :           sym->ts = CLASS_DATA (target)->ts;
   10650           31 :           if (!sym->as)
   10651           31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10652           31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10653           31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10654           31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10655              :                             target->corank);
   10656           31 :           sym->as = NULL;
   10657              :         }
   10658          262 :       else if (target->ts.type == BT_DERIVED
   10659          135 :                && target->symtree && target->symtree->n.sym
   10660          111 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10661            0 :                && IS_INFERRED_TYPE (target)
   10662            0 :                && target->ref && target->ref->next
   10663            0 :                && target->ref->next->type == REF_ARRAY
   10664            0 :                && !target->ref->next->next)
   10665              :         {
   10666              :           /* A inferred type selector whose symbol has been determined to be
   10667              :              a class array but which only has an array reference. Change the
   10668              :              associate name and the selector to class type.  */
   10669            0 :           sym->ts = target->ts;
   10670            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10671            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10672            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10673              :                             target->corank);
   10674            0 :           sym->as = NULL;
   10675            0 :           target->ts = sym->ts;
   10676              :         }
   10677          262 :       else if ((target->ts.type == BT_DERIVED)
   10678          127 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10679           61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10680              :         /* Confirmed to be either a derived type or misidentified to be a
   10681              :            scalar class object, when the selector is a class array.  */
   10682          141 :         sym->ts = target->ts;
   10683              :     }
   10684              : 
   10685              : 
   10686         7594 :   if (target->expr_type == EXPR_NULL)
   10687              :     {
   10688            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10689            1 :       return;
   10690              :     }
   10691         7593 :   else if (target->ts.type == BT_UNKNOWN)
   10692              :     {
   10693            2 :       gfc_error ("Selector at %L has no type", &target->where);
   10694            2 :       return;
   10695              :     }
   10696              : 
   10697              :   /* Get type if this was not already set.  Note that it can be
   10698              :      some other type than the target in case this is a SELECT TYPE
   10699              :      selector!  So we must not update when the type is already there.  */
   10700         7591 :   if (sym->ts.type == BT_UNKNOWN)
   10701          258 :     sym->ts = target->ts;
   10702              : 
   10703         7591 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10704              : 
   10705              :   /* See if this is a valid association-to-variable.  */
   10706        15182 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10707         6609 :                            && !parentheses
   10708         6588 :                            && !gfc_has_vector_subscript (target))
   10709         7639 :                           || gfc_is_ptr_fcn (target));
   10710              : 
   10711              :   /* Finally resolve if this is an array or not.  */
   10712         7591 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10713          179 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10714              :     {
   10715          103 :       gfc_expression_rank (target);
   10716          103 :       if (target->ts.type == BT_DERIVED
   10717           56 :           && !sym->as
   10718           56 :           && target->symtree->n.sym->as)
   10719              :         {
   10720            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10721            0 :           sym->attr.dimension = 1;
   10722              :         }
   10723          103 :       else if (target->ts.type == BT_CLASS
   10724           47 :                && CLASS_DATA (target)->as)
   10725              :         {
   10726            0 :           target->rank = CLASS_DATA (target)->as->rank;
   10727            0 :           target->corank = CLASS_DATA (target)->as->corank;
   10728            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10729              :             {
   10730            0 :               sym->ts = target->ts;
   10731            0 :               sym->attr.dimension = 0;
   10732              :             }
   10733              :         }
   10734              :     }
   10735              : 
   10736              : 
   10737         7591 :   if (sym->attr.dimension && target->rank == 0)
   10738              :     {
   10739              :       /* primary.cc makes the assumption that a reference to an associate
   10740              :          name followed by a left parenthesis is an array reference.  */
   10741           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10742              :         {
   10743           12 :           gfc_expression_rank (sym->assoc->target);
   10744           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10745           12 :           if (!sym->attr.dimension && sym->as)
   10746            0 :             sym->as = NULL;
   10747              :         }
   10748              : 
   10749           17 :       if (sym->attr.dimension && target->rank == 0)
   10750              :         {
   10751            5 :           if (sym->ts.type != BT_CHARACTER)
   10752            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10753              :                        sym->name, &sym->declared_at);
   10754            5 :           sym->attr.dimension = 0;
   10755            5 :           return;
   10756              :         }
   10757              :     }
   10758              : 
   10759              :   /* We cannot deal with class selectors that need temporaries.  */
   10760         7586 :   if (target->ts.type == BT_CLASS
   10761         7586 :         && gfc_ref_needs_temporary_p (target->ref))
   10762              :     {
   10763            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10764              :                  "yet implemented", &target->where);
   10765            1 :       return;
   10766              :     }
   10767              : 
   10768         7585 :   if (target->ts.type == BT_CLASS)
   10769         2785 :     gfc_fix_class_refs (target);
   10770              : 
   10771         7585 :   if ((target->rank > 0 || target->corank > 0)
   10772         2732 :       && !sym->attr.select_rank_temporary)
   10773              :     {
   10774         2732 :       gfc_array_spec *as;
   10775              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10776              :          it is corrected now.  */
   10777         2732 :       if (sym->ts.type != BT_CLASS
   10778         2156 :           && (!sym->as || sym->as->corank != target->corank))
   10779              :         {
   10780          141 :           if (!sym->as)
   10781          134 :             sym->as = gfc_get_array_spec ();
   10782          141 :           as = sym->as;
   10783          141 :           as->rank = target->rank;
   10784          141 :           as->type = AS_DEFERRED;
   10785          141 :           as->corank = target->corank;
   10786          141 :           sym->attr.dimension = 1;
   10787          141 :           if (as->corank != 0)
   10788            7 :             sym->attr.codimension = 1;
   10789              :         }
   10790         2591 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10791          575 :                && (!CLASS_DATA (sym)->as
   10792          575 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10793              :         {
   10794            0 :           if (!CLASS_DATA (sym)->as)
   10795            0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10796            0 :           as = CLASS_DATA (sym)->as;
   10797            0 :           as->rank = target->rank;
   10798            0 :           as->type = AS_DEFERRED;
   10799            0 :           as->corank = target->corank;
   10800            0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10801            0 :           if (as->corank != 0)
   10802            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10803              :         }
   10804              :     }
   10805         4853 :   else if (!sym->attr.select_rank_temporary)
   10806              :     {
   10807              :       /* target's rank is 0, but the type of the sym is still array valued,
   10808              :          which has to be corrected.  */
   10809         3464 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10810          700 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10811              :         {
   10812           24 :           gfc_array_spec *as;
   10813           24 :           symbol_attribute attr;
   10814              :           /* The associated variable's type is still the array type
   10815              :              correct this now.  */
   10816           24 :           gfc_typespec *ts = &target->ts;
   10817           24 :           gfc_ref *ref;
   10818              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10819              :            */
   10820           24 :           bool internal_ref = true;
   10821              : 
   10822           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10823              :             {
   10824           48 :               switch (ref->type)
   10825              :                 {
   10826           24 :                 case REF_COMPONENT:
   10827           24 :                   ts = &ref->u.c.component->ts;
   10828           24 :                   internal_ref
   10829           24 :                     = target->ref == ref && ref->next
   10830           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10831              :                   break;
   10832           24 :                 case REF_ARRAY:
   10833           24 :                   if (ts->type == BT_CLASS)
   10834            0 :                     ts = &ts->u.derived->components->ts;
   10835           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10836            0 :                     for (int i = ref->u.ar.dimen;
   10837              :                          internal_ref
   10838            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10839              :                          ++i)
   10840            0 :                       internal_ref
   10841            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10842              :                   break;
   10843              :                 default:
   10844              :                   break;
   10845              :                 }
   10846              :             }
   10847              :           /* Only rewrite the type of this symbol, when the refs are not the
   10848              :              internal ones for class and co-array this-image.  */
   10849           24 :           if (!internal_ref)
   10850              :             {
   10851              :               /* Create a scalar instance of the current class type.  Because
   10852              :                  the rank of a class array goes into its name, the type has to
   10853              :                  be rebuilt.  The alternative of (re-)setting just the
   10854              :                  attributes and as in the current type, destroys the type also
   10855              :                  in other places.  */
   10856            0 :               as = NULL;
   10857            0 :               sym->ts = *ts;
   10858            0 :               sym->ts.type = BT_CLASS;
   10859            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10860            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10861            0 :               sym->as = NULL;
   10862              :             }
   10863              :         }
   10864              :     }
   10865              : 
   10866              :   /* Mark this as an associate variable.  */
   10867         7585 :   sym->attr.associate_var = 1;
   10868              : 
   10869              :   /* Fix up the type-spec for CHARACTER types.  */
   10870         7585 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10871              :     {
   10872          503 :       gfc_ref *ref;
   10873          788 :       for (ref = target->ref; ref; ref = ref->next)
   10874          311 :         if (ref->type == REF_SUBSTRING
   10875           74 :             && (ref->u.ss.start == NULL
   10876           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10877           74 :                 || ref->u.ss.end == NULL
   10878           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10879              :           break;
   10880              : 
   10881          503 :       if (!sym->ts.u.cl)
   10882          182 :         sym->ts.u.cl = target->ts.u.cl;
   10883              : 
   10884          503 :       if (sym->ts.deferred
   10885          189 :           && sym->ts.u.cl == target->ts.u.cl)
   10886              :         {
   10887          110 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10888          110 :           sym->ts.deferred = 1;
   10889              :         }
   10890              : 
   10891          503 :       if (!sym->ts.u.cl->length
   10892          327 :           && !sym->ts.deferred
   10893          138 :           && target->expr_type == EXPR_CONSTANT)
   10894              :         {
   10895           30 :           sym->ts.u.cl->length =
   10896           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10897           30 :                                   target->value.character.length);
   10898              :         }
   10899          473 :       else if (((!sym->ts.u.cl->length
   10900          176 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10901          303 :                 && target->expr_type != EXPR_VARIABLE)
   10902          350 :                || ref)
   10903              :         {
   10904          149 :           if (!sym->ts.deferred)
   10905              :             {
   10906           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10907           45 :               sym->ts.deferred = 1;
   10908              :             }
   10909              : 
   10910              :           /* This is reset in trans-stmt.cc after the assignment
   10911              :              of the target expression to the associate name.  */
   10912          149 :           if (ref && sym->as)
   10913           26 :             sym->attr.pointer = 1;
   10914              :           else
   10915          123 :             sym->attr.allocatable = 1;
   10916              :         }
   10917              :     }
   10918              : 
   10919         7585 :   if (sym->ts.type == BT_CLASS
   10920         1421 :       && IS_INFERRED_TYPE (target)
   10921           13 :       && target->ts.type == BT_DERIVED
   10922            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10923            0 :       && target->ref && target->ref->next && !target->ref->next->next
   10924            0 :       && target->ref->next->type == REF_ARRAY)
   10925            0 :     target->ts = target->symtree->n.sym->ts;
   10926              : 
   10927              :   /* If the target is a good class object, so is the associate variable.  */
   10928         7585 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10929          713 :     sym->attr.class_ok = 1;
   10930              : 
   10931              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   10932         7585 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   10933            3 :     sym->attr.contiguous = 1;
   10934              : }
   10935              : 
   10936              : 
   10937              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   10938              :    array reference, where necessary.  The symbols are artificial and so
   10939              :    the dimension attribute and arrayspec can also be set.  In addition,
   10940              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   10941              :    This is corrected here as well.*/
   10942              : 
   10943              : static void
   10944         1687 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   10945              :                  gfc_ref *ref)
   10946              : {
   10947         1687 :   gfc_ref *nref = (*expr1)->ref;
   10948         1687 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   10949         1687 :   gfc_symbol *sym2;
   10950         1687 :   gfc_expr *selector = gfc_copy_expr (expr2);
   10951              : 
   10952         1687 :   (*expr1)->rank = rank;
   10953         1687 :   (*expr1)->corank = corank;
   10954         1687 :   if (selector)
   10955              :     {
   10956          311 :       gfc_resolve_expr (selector);
   10957          311 :       if (selector->expr_type == EXPR_OP
   10958            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   10959            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   10960          309 :       else if (selector->expr_type == EXPR_VARIABLE
   10961            7 :                || selector->expr_type == EXPR_FUNCTION)
   10962          309 :         sym2 = selector->symtree->n.sym;
   10963              :       else
   10964            0 :         gcc_unreachable ();
   10965              :     }
   10966              :   else
   10967              :     sym2 = NULL;
   10968              : 
   10969         1687 :   if (sym1->ts.type == BT_CLASS)
   10970              :     {
   10971         1687 :       if ((*expr1)->ts.type != BT_CLASS)
   10972           13 :         (*expr1)->ts = sym1->ts;
   10973              : 
   10974         1687 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   10975         1687 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   10976         1687 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   10977            1 :         CLASS_DATA (sym1)->as
   10978            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   10979              :     }
   10980              :   else
   10981              :     {
   10982            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   10983            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   10984            0 :       if (sym1->as == NULL && sym2)
   10985            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   10986              :     }
   10987              : 
   10988         3057 :   for (; nref; nref = nref->next)
   10989         2746 :     if (nref->next == NULL)
   10990              :       break;
   10991              : 
   10992         1687 :   if (ref && nref && nref->type != REF_ARRAY)
   10993            6 :     nref->next = gfc_copy_ref (ref);
   10994         1681 :   else if (ref && !nref)
   10995          302 :     (*expr1)->ref = gfc_copy_ref (ref);
   10996         1379 :   else if (ref && nref->u.ar.codimen != corank)
   10997              :     {
   10998          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   10999          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11000           61 :       nref->u.ar.codimen = corank;
   11001              :     }
   11002         1687 : }
   11003              : 
   11004              : 
   11005              : static gfc_expr *
   11006         6752 : build_loc_call (gfc_expr *sym_expr)
   11007              : {
   11008         6752 :   gfc_expr *loc_call;
   11009         6752 :   loc_call = gfc_get_expr ();
   11010         6752 :   loc_call->expr_type = EXPR_FUNCTION;
   11011         6752 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11012         6752 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11013         6752 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11014         6752 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11015         6752 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11016         6752 :   loc_call->ts.type = BT_INTEGER;
   11017         6752 :   loc_call->ts.kind = gfc_index_integer_kind;
   11018         6752 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11019         6752 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11020         6752 :   loc_call->value.function.actual->expr = sym_expr;
   11021         6752 :   loc_call->where = sym_expr->where;
   11022         6752 :   return loc_call;
   11023              : }
   11024              : 
   11025              : /* Resolve a SELECT TYPE statement.  */
   11026              : 
   11027              : static void
   11028         3029 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11029              : {
   11030         3029 :   gfc_symbol *selector_type;
   11031         3029 :   gfc_code *body, *new_st, *if_st, *tail;
   11032         3029 :   gfc_code *class_is = NULL, *default_case = NULL;
   11033         3029 :   gfc_case *c;
   11034         3029 :   gfc_symtree *st;
   11035         3029 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11036         3029 :   gfc_namespace *ns;
   11037         3029 :   int error = 0;
   11038         3029 :   int rank = 0, corank = 0;
   11039         3029 :   gfc_ref* ref = NULL;
   11040         3029 :   gfc_expr *selector_expr = NULL;
   11041         3029 :   gfc_code *old_code = code;
   11042              : 
   11043         3029 :   ns = code->ext.block.ns;
   11044         3029 :   if (code->expr2)
   11045              :     {
   11046              :       /* Set this, or coarray checks in resolve will fail.  */
   11047          639 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11048              :     }
   11049         3029 :   gfc_resolve (ns);
   11050              : 
   11051              :   /* Check for F03:C813.  */
   11052         3029 :   if (code->expr1->ts.type != BT_CLASS
   11053           36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11054              :     {
   11055           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11056              :                  "at %L", &code->loc);
   11057           42 :       return;
   11058              :     }
   11059              : 
   11060              :   /* Prevent segfault, when class type is not initialized due to previous
   11061              :      error.  */
   11062         3016 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11063         3014 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11064              :     return;
   11065              : 
   11066         3009 :   if (code->expr2)
   11067              :     {
   11068          630 :       gfc_ref *ref2 = NULL;
   11069         1466 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11070          836 :          if (ref->type == REF_COMPONENT
   11071          432 :              && ref->u.c.component->ts.type == BT_CLASS)
   11072          836 :            ref2 = ref;
   11073              : 
   11074          630 :       if (ref2)
   11075              :         {
   11076          340 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11077            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11078          340 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11079              :         }
   11080              :       else
   11081              :         {
   11082          290 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11083           28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11084              :           /* Sometimes the selector expression is given the typespec of the
   11085              :              '_data' field, which is logical enough but inappropriate here. */
   11086          290 :           if (code->expr2->ts.type == BT_DERIVED
   11087           80 :               && code->expr2->symtree
   11088           80 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11089           80 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11090          290 :           selector_type = CLASS_DATA (code->expr2)
   11091              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11092              :         }
   11093              : 
   11094          630 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11095              :         {
   11096          297 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11097          297 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11098          297 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11099              :         }
   11100              : 
   11101              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11102          630 :       if (gfc_is_coindexed (code->expr2))
   11103              :         {
   11104            4 :           gfc_error ("Selector at %L must not be coindexed",
   11105            4 :                      &code->expr2->where);
   11106            4 :           return;
   11107              :         }
   11108              : 
   11109              :     }
   11110              :   else
   11111              :     {
   11112         2379 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11113              : 
   11114         2379 :       if (gfc_is_coindexed (code->expr1))
   11115              :         {
   11116            0 :           gfc_error ("Selector at %L must not be coindexed",
   11117            0 :                      &code->expr1->where);
   11118            0 :           return;
   11119              :         }
   11120              :     }
   11121              : 
   11122              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11123         8379 :   for (body = code->block; body; body = body->block)
   11124              :     {
   11125         5375 :       c = body->ext.block.case_list;
   11126              : 
   11127         5375 :       if (!error)
   11128              :         {
   11129              :           /* Check for repeated cases.  */
   11130         8340 :           for (tail = code->block; tail; tail = tail->block)
   11131              :             {
   11132         8340 :               gfc_case *d = tail->ext.block.case_list;
   11133         8340 :               if (tail == body)
   11134              :                 break;
   11135              : 
   11136         2974 :               if (c->ts.type == d->ts.type
   11137          516 :                   && ((c->ts.type == BT_DERIVED
   11138          418 :                        && c->ts.u.derived && d->ts.u.derived
   11139          418 :                        && !strcmp (c->ts.u.derived->name,
   11140              :                                    d->ts.u.derived->name))
   11141          515 :                       || c->ts.type == BT_UNKNOWN
   11142          515 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11143           55 :                           && c->ts.kind == d->ts.kind)))
   11144              :                 {
   11145            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11146              :                              &c->where, &d->where);
   11147            1 :                   return;
   11148              :                 }
   11149              :             }
   11150              :         }
   11151              : 
   11152              :       /* Check F03:C815.  */
   11153         3404 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11154         2318 :           && selector_type
   11155         2318 :           && !selector_type->attr.unlimited_polymorphic
   11156         7371 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11157              :         {
   11158            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11159            1 :                      c->ts.u.derived->name, &c->where);
   11160            1 :           error++;
   11161            1 :           continue;
   11162              :         }
   11163              : 
   11164              :       /* Check F03:C816.  */
   11165         5379 :       if (c->ts.type != BT_UNKNOWN
   11166         3763 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11167         7373 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11168         1996 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11169              :         {
   11170            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11171            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11172            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11173              :           else
   11174            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11175              :                        gfc_basic_typename (c->ts.type), &c->where);
   11176            6 :           error++;
   11177            6 :           continue;
   11178              :         }
   11179              : 
   11180              :       /* Check F03:C814.  */
   11181         5367 :       if (c->ts.type == BT_CHARACTER
   11182          736 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11183              :         {
   11184            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11185              :                      "type parameter is assumed", &c->where);
   11186            0 :           error++;
   11187            0 :           continue;
   11188              :         }
   11189              : 
   11190              :       /* Intercept the DEFAULT case.  */
   11191         5367 :       if (c->ts.type == BT_UNKNOWN)
   11192              :         {
   11193              :           /* Check F03:C818.  */
   11194         1610 :           if (default_case)
   11195              :             {
   11196            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11197              :                          "by a second DEFAULT CASE at %L",
   11198            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11199            1 :               error++;
   11200            1 :               continue;
   11201              :             }
   11202              : 
   11203              :           default_case = body;
   11204              :         }
   11205              :     }
   11206              : 
   11207         3004 :   if (error > 0)
   11208              :     return;
   11209              : 
   11210              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11211              :      target if present.  If there are any EXIT statements referring to the
   11212              :      SELECT TYPE construct, this is no problem because the gfc_code
   11213              :      reference stays the same and EXIT is equally possible from the BLOCK
   11214              :      it is changed to.  */
   11215         3001 :   code->op = EXEC_BLOCK;
   11216         3001 :   if (code->expr2)
   11217              :     {
   11218          626 :       gfc_association_list* assoc;
   11219              : 
   11220          626 :       assoc = gfc_get_association_list ();
   11221          626 :       assoc->st = code->expr1->symtree;
   11222          626 :       assoc->target = gfc_copy_expr (code->expr2);
   11223          626 :       assoc->target->where = code->expr2->where;
   11224              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11225              : 
   11226          626 :       code->ext.block.assoc = assoc;
   11227          626 :       code->expr1->symtree->n.sym->assoc = assoc;
   11228              : 
   11229          626 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11230              :     }
   11231              :   else
   11232         2375 :     code->ext.block.assoc = NULL;
   11233              : 
   11234              :   /* Ensure that the selector rank and arrayspec are available to
   11235              :      correct expressions in which they might be missing.  */
   11236         3001 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11237              :     {
   11238          311 :       rank = code->expr2->rank;
   11239          311 :       corank = code->expr2->corank;
   11240          585 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11241          576 :         if (ref->next == NULL)
   11242              :           break;
   11243          311 :       if (ref && ref->type == REF_ARRAY)
   11244          302 :         ref = gfc_copy_ref (ref);
   11245              : 
   11246              :       /* Fixup expr1 if necessary.  */
   11247          311 :       if (rank || corank)
   11248          311 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11249              :     }
   11250         2690 :   else if (code->expr1->rank || code->expr1->corank)
   11251              :     {
   11252          884 :       rank = code->expr1->rank;
   11253          884 :       corank = code->expr1->corank;
   11254          884 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11255          884 :         if (ref->next == NULL)
   11256              :           break;
   11257          884 :       if (ref && ref->type == REF_ARRAY)
   11258          884 :         ref = gfc_copy_ref (ref);
   11259              :     }
   11260              : 
   11261         3001 :   gfc_expr *orig_expr1 = code->expr1;
   11262              : 
   11263              :   /* Add EXEC_SELECT to switch on type.  */
   11264         3001 :   new_st = gfc_get_code (code->op);
   11265         3001 :   new_st->expr1 = code->expr1;
   11266         3001 :   new_st->expr2 = code->expr2;
   11267         3001 :   new_st->block = code->block;
   11268         3001 :   code->expr1 = code->expr2 =  NULL;
   11269         3001 :   code->block = NULL;
   11270         3001 :   if (!ns->code)
   11271         3001 :     ns->code = new_st;
   11272              :   else
   11273            0 :     ns->code->next = new_st;
   11274         3001 :   code = new_st;
   11275         3001 :   code->op = EXEC_SELECT_TYPE;
   11276              : 
   11277              :   /* Use the intrinsic LOC function to generate an integer expression
   11278              :      for the vtable of the selector.  Note that the rank of the selector
   11279              :      expression has to be set to zero.  */
   11280         3001 :   gfc_add_vptr_component (code->expr1);
   11281         3001 :   code->expr1->rank = 0;
   11282         3001 :   code->expr1->corank = 0;
   11283         3001 :   code->expr1 = build_loc_call (code->expr1);
   11284         3001 :   selector_expr = code->expr1->value.function.actual->expr;
   11285              : 
   11286              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11287         8360 :   for (body = code->block; body; body = body->block)
   11288              :     {
   11289         5359 :       gfc_symbol *vtab;
   11290         5359 :       c = body->ext.block.case_list;
   11291              : 
   11292              :       /* Generate an index integer expression for address of the
   11293              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11294              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11295         5359 :       if (c->ts.type != BT_UNKNOWN)
   11296              :         {
   11297         3751 :           gfc_expr *e;
   11298         3751 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11299              :             {
   11300         2309 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11301         2309 :               gcc_assert (vtab);
   11302         2309 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11303         2309 :                                           c->ts.u.derived->hash_value);
   11304              :             }
   11305              :           else
   11306              :             {
   11307         1442 :               vtab = gfc_find_vtab (&c->ts);
   11308         1442 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11309         1442 :               e = CLASS_DATA (vtab)->initializer;
   11310         1442 :               c->high = gfc_copy_expr (e);
   11311         1442 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11312              :                 {
   11313            1 :                   gfc_typespec ts;
   11314            1 :                   ts.kind = gfc_integer_4_kind;
   11315            1 :                   ts.type = BT_INTEGER;
   11316            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11317              :                 }
   11318              :             }
   11319              : 
   11320         3751 :           e = gfc_lval_expr_from_sym (vtab);
   11321         3751 :           c->low = build_loc_call (e);
   11322              :         }
   11323              :       else
   11324         1608 :         continue;
   11325              : 
   11326              :       /* Associate temporary to selector.  This should only be done
   11327              :          when this case is actually true, so build a new ASSOCIATE
   11328              :          that does precisely this here (instead of using the
   11329              :          'global' one).  */
   11330              : 
   11331              :       /* First check the derived type import status.  */
   11332         3751 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11333            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11334              :         {
   11335           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11336            6 :                                  c->ts.u.derived->name);
   11337            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11338              :                                         gfc_current_ns))
   11339            6 :             error++;
   11340              :         }
   11341              : 
   11342         3751 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11343         3751 :       if (c->ts.type == BT_CLASS)
   11344          346 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11345          346 :                   c->ts.u.derived->name, var_name);
   11346         3405 :       else if (c->ts.type == BT_DERIVED)
   11347         1963 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11348         1963 :                   c->ts.u.derived->name, var_name);
   11349         1442 :       else if (c->ts.type == BT_CHARACTER)
   11350              :         {
   11351          736 :           HOST_WIDE_INT charlen = 0;
   11352          736 :           if (c->ts.u.cl && c->ts.u.cl->length
   11353            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11354            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11355          736 :           snprintf (name, sizeof (name),
   11356              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11357              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11358              :                     var_name);
   11359              :         }
   11360              :       else
   11361          706 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11362              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11363              : 
   11364         3751 :       st = gfc_find_symtree (ns->sym_root, name);
   11365         3751 :       gcc_assert (st->n.sym->assoc);
   11366         3751 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11367         3751 :       st->n.sym->assoc->target->where = selector_expr->where;
   11368         3751 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11369              :         {
   11370         3405 :           gfc_add_data_component (st->n.sym->assoc->target);
   11371              :           /* Fixup the target expression if necessary.  */
   11372         3405 :           if (rank || corank)
   11373         1376 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11374              :                              ref);
   11375              :         }
   11376              : 
   11377         3751 :       new_st = gfc_get_code (EXEC_BLOCK);
   11378         3751 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11379         3751 :       new_st->ext.block.ns->code = body->next;
   11380         3751 :       body->next = new_st;
   11381              : 
   11382              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11383              :          there is a CASE label overlap and this is already used.  Just ignore,
   11384              :          the error is diagnosed elsewhere.  */
   11385         3751 :       if (st->n.sym->assoc->dangling)
   11386              :         {
   11387         3750 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11388         3750 :           st->n.sym->assoc->dangling = 0;
   11389              :         }
   11390              : 
   11391         3751 :       resolve_assoc_var (st->n.sym, false);
   11392              :     }
   11393              : 
   11394              :   /* Take out CLASS IS cases for separate treatment.  */
   11395              :   body = code;
   11396         8360 :   while (body && body->block)
   11397              :     {
   11398         5359 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11399              :         {
   11400              :           /* Add to class_is list.  */
   11401          346 :           if (class_is == NULL)
   11402              :             {
   11403          315 :               class_is = body->block;
   11404          315 :               tail = class_is;
   11405              :             }
   11406              :           else
   11407              :             {
   11408           43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11409           31 :               tail->block = body->block;
   11410           31 :               tail = tail->block;
   11411              :             }
   11412              :           /* Remove from EXEC_SELECT list.  */
   11413          346 :           body->block = body->block->block;
   11414          346 :           tail->block = NULL;
   11415              :         }
   11416              :       else
   11417              :         body = body->block;
   11418              :     }
   11419              : 
   11420         3001 :   if (class_is)
   11421              :     {
   11422          315 :       gfc_symbol *vtab;
   11423              : 
   11424          315 :       if (!default_case)
   11425              :         {
   11426              :           /* Add a default case to hold the CLASS IS cases.  */
   11427          313 :           for (tail = code; tail->block; tail = tail->block) ;
   11428          205 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11429          205 :           tail = tail->block;
   11430          205 :           tail->ext.block.case_list = gfc_get_case ();
   11431          205 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11432          205 :           tail->next = NULL;
   11433          205 :           default_case = tail;
   11434              :         }
   11435              : 
   11436              :       /* More than one CLASS IS block?  */
   11437          315 :       if (class_is->block)
   11438              :         {
   11439           37 :           gfc_code **c1,*c2;
   11440           37 :           bool swapped;
   11441              :           /* Sort CLASS IS blocks by extension level.  */
   11442           36 :           do
   11443              :             {
   11444           37 :               swapped = false;
   11445           97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11446              :                 {
   11447           61 :                   c2 = (*c1)->block;
   11448              :                   /* F03:C817 (check for doubles).  */
   11449           61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   11450           61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   11451              :                     {
   11452            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11453              :                                  "statement at %L",
   11454              :                                  &c2->ext.block.case_list->where);
   11455            1 :                       return;
   11456              :                     }
   11457           60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11458           60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11459              :                     {
   11460              :                       /* Swap.  */
   11461           24 :                       (*c1)->block = c2->block;
   11462           24 :                       c2->block = *c1;
   11463           24 :                       *c1 = c2;
   11464           24 :                       swapped = true;
   11465              :                     }
   11466              :                 }
   11467              :             }
   11468              :           while (swapped);
   11469              :         }
   11470              : 
   11471              :       /* Generate IF chain.  */
   11472          314 :       if_st = gfc_get_code (EXEC_IF);
   11473          314 :       new_st = if_st;
   11474          658 :       for (body = class_is; body; body = body->block)
   11475              :         {
   11476          344 :           new_st->block = gfc_get_code (EXEC_IF);
   11477          344 :           new_st = new_st->block;
   11478              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11479          344 :           new_st->expr1 = gfc_get_expr ();
   11480          344 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11481          344 :           new_st->expr1->ts.type = BT_LOGICAL;
   11482          344 :           new_st->expr1->ts.kind = 4;
   11483          344 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11484          344 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11485          344 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11486              :           /* Set up arguments.  */
   11487          344 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11488          344 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11489          344 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11490          344 :           new_st->expr1->where = code->loc;
   11491          344 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11492          344 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11493          344 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11494          344 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11495          344 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11496          344 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11497              :           /* Set up types in formal arg list.  */
   11498          344 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11499          344 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11500          344 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11501          344 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11502              : 
   11503          344 :           new_st->next = body->next;
   11504              :         }
   11505          314 :         if (default_case->next)
   11506              :           {
   11507          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11508          110 :             new_st = new_st->block;
   11509          110 :             new_st->next = default_case->next;
   11510              :           }
   11511              : 
   11512              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11513          314 :         default_case->next = if_st;
   11514              :     }
   11515              : 
   11516              :   /* Resolve the internal code.  This cannot be done earlier because
   11517              :      it requires that the sym->assoc of selectors is set already.  */
   11518         3000 :   gfc_current_ns = ns;
   11519         3000 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11520         3000 :   gfc_current_ns = old_ns;
   11521              : 
   11522         3000 :   free (ref);
   11523              : }
   11524              : 
   11525              : 
   11526              : /* Resolve a SELECT RANK statement.  */
   11527              : 
   11528              : static void
   11529         1024 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11530              : {
   11531         1024 :   gfc_namespace *ns;
   11532         1024 :   gfc_code *body, *new_st, *tail;
   11533         1024 :   gfc_case *c;
   11534         1024 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11535         1024 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11536         1024 :   gfc_symtree *st;
   11537         1024 :   gfc_expr *selector_expr = NULL;
   11538         1024 :   int case_value;
   11539         1024 :   HOST_WIDE_INT charlen = 0;
   11540              : 
   11541         1024 :   ns = code->ext.block.ns;
   11542         1024 :   gfc_resolve (ns);
   11543              : 
   11544         1024 :   code->op = EXEC_BLOCK;
   11545         1024 :   if (code->expr2)
   11546              :     {
   11547           42 :       gfc_association_list* assoc;
   11548              : 
   11549           42 :       assoc = gfc_get_association_list ();
   11550           42 :       assoc->st = code->expr1->symtree;
   11551           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11552           42 :       assoc->target->where = code->expr2->where;
   11553              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11554              : 
   11555           42 :       code->ext.block.assoc = assoc;
   11556           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11557              : 
   11558           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11559              :     }
   11560              :   else
   11561          982 :     code->ext.block.assoc = NULL;
   11562              : 
   11563              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11564              :      cascade of further errors because the case blocks do not compile
   11565              :      correctly.  */
   11566         3332 :   for (body = code->block; body; body = body->block)
   11567              :     {
   11568         2308 :       c = body->ext.block.case_list;
   11569         2308 :       if (c->low)
   11570         1389 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11571              :       else
   11572              :         case_value = -2;
   11573              : 
   11574              :       /* Check for repeated cases.  */
   11575         5842 :       for (tail = code->block; tail; tail = tail->block)
   11576              :         {
   11577         5842 :           gfc_case *d = tail->ext.block.case_list;
   11578         5842 :           int case_value2;
   11579              : 
   11580         5842 :           if (tail == body)
   11581              :             break;
   11582              : 
   11583              :           /* Check F2018: C1153.  */
   11584         3534 :           if (!c->low && !d->low)
   11585            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11586              :                        &c->where, &d->where);
   11587              : 
   11588         3534 :           if (!c->low || !d->low)
   11589         1253 :             continue;
   11590              : 
   11591              :           /* Check F2018: C1153.  */
   11592         2281 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11593         2281 :           if ((case_value == case_value2) && case_value == -1)
   11594            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11595              :                        &c->where, &d->where);
   11596         2280 :           else if (case_value == case_value2)
   11597            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11598              :                        case_value, &c->where, &d->where);
   11599              :         }
   11600              : 
   11601         2308 :       if (!c->low)
   11602          919 :         continue;
   11603              : 
   11604              :       /* Check F2018: C1155.  */
   11605         1389 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11606         1387 :                                || gfc_expr_attr (code->expr1).pointer))
   11607            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11608            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11609              :     }
   11610              : 
   11611              :   /* Add EXEC_SELECT to switch on rank.  */
   11612         1024 :   new_st = gfc_get_code (code->op);
   11613         1024 :   new_st->expr1 = code->expr1;
   11614         1024 :   new_st->expr2 = code->expr2;
   11615         1024 :   new_st->block = code->block;
   11616         1024 :   code->expr1 = code->expr2 =  NULL;
   11617         1024 :   code->block = NULL;
   11618         1024 :   if (!ns->code)
   11619         1024 :     ns->code = new_st;
   11620              :   else
   11621            0 :     ns->code->next = new_st;
   11622         1024 :   code = new_st;
   11623         1024 :   code->op = EXEC_SELECT_RANK;
   11624              : 
   11625         1024 :   selector_expr = code->expr1;
   11626              : 
   11627              :   /* Loop over SELECT RANK cases.  */
   11628         3332 :   for (body = code->block; body; body = body->block)
   11629              :     {
   11630         2308 :       c = body->ext.block.case_list;
   11631         2308 :       int case_value;
   11632              : 
   11633              :       /* Pass on the default case.  */
   11634         2308 :       if (c->low == NULL)
   11635          919 :         continue;
   11636              : 
   11637              :       /* Associate temporary to selector.  This should only be done
   11638              :          when this case is actually true, so build a new ASSOCIATE
   11639              :          that does precisely this here (instead of using the
   11640              :          'global' one).  */
   11641         1389 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11642          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11643          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11644              : 
   11645         1389 :       if (c->ts.type == BT_CLASS)
   11646          145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11647         1244 :       else if (c->ts.type == BT_DERIVED)
   11648          110 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11649         1134 :       else if (c->ts.type != BT_CHARACTER)
   11650          575 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11651              :       else
   11652          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11653              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11654              : 
   11655         1389 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11656         1389 :       if (case_value >= 0)
   11657         1356 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11658              :       else
   11659           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11660              : 
   11661         1389 :       st = gfc_find_symtree (ns->sym_root, name);
   11662         1389 :       gcc_assert (st->n.sym->assoc);
   11663              : 
   11664         1389 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11665         1389 :       st->n.sym->assoc->target->where = selector_expr->where;
   11666              : 
   11667         1389 :       new_st = gfc_get_code (EXEC_BLOCK);
   11668         1389 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11669         1389 :       new_st->ext.block.ns->code = body->next;
   11670         1389 :       body->next = new_st;
   11671              : 
   11672              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11673              :          there is a CASE label overlap and this is already used.  Just ignore,
   11674              :          the error is diagnosed elsewhere.  */
   11675         1389 :       if (st->n.sym->assoc->dangling)
   11676              :         {
   11677         1387 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11678         1387 :           st->n.sym->assoc->dangling = 0;
   11679              :         }
   11680              : 
   11681         1389 :       resolve_assoc_var (st->n.sym, false);
   11682              :     }
   11683              : 
   11684         1024 :   gfc_current_ns = ns;
   11685         1024 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11686         1024 :   gfc_current_ns = old_ns;
   11687         1024 : }
   11688              : 
   11689              : 
   11690              : /* Resolve a transfer statement. This is making sure that:
   11691              :    -- a derived type being transferred has only non-pointer components
   11692              :    -- a derived type being transferred doesn't have private components, unless
   11693              :       it's being transferred from the module where the type was defined
   11694              :    -- we're not trying to transfer a whole assumed size array.  */
   11695              : 
   11696              : static void
   11697        46414 : resolve_transfer (gfc_code *code)
   11698              : {
   11699        46414 :   gfc_symbol *sym, *derived;
   11700        46414 :   gfc_ref *ref;
   11701        46414 :   gfc_expr *exp;
   11702        46414 :   bool write = false;
   11703        46414 :   bool formatted = false;
   11704        46414 :   gfc_dt *dt = code->ext.dt;
   11705        46414 :   gfc_symbol *dtio_sub = NULL;
   11706              : 
   11707        46414 :   exp = code->expr1;
   11708              : 
   11709        92834 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11710        47329 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11711            6 :     exp = exp->value.op.op1;
   11712              : 
   11713        46414 :   if (exp && exp->expr_type == EXPR_NULL
   11714            2 :       && code->ext.dt)
   11715              :     {
   11716            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11717              :                  &exp->where);
   11718            2 :       return;
   11719              :     }
   11720              : 
   11721              :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11722              :                       && exp->expr_type != EXPR_FUNCTION
   11723              :                       && exp->expr_type != EXPR_ARRAY
   11724              :                       && exp->expr_type != EXPR_STRUCTURE))
   11725              :     return;
   11726              : 
   11727              :   /* If we are reading, the variable will be changed.  Note that
   11728              :      code->ext.dt may be NULL if the TRANSFER is related to
   11729              :      an INQUIRE statement -- but in this case, we are not reading, either.  */
   11730        25331 :   if (dt && dt->dt_io_kind->value.iokind == M_READ
   11731        32801 :       && !gfc_check_vardef_context (exp, false, false, false,
   11732         7322 :                                     _("item in READ")))
   11733              :     return;
   11734              : 
   11735        25475 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11736        25475 :                         || exp->expr_type == EXPR_FUNCTION
   11737        21101 :                         || exp->expr_type == EXPR_ARRAY
   11738        46576 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11739              : 
   11740              :   /* Go to actual component transferred.  */
   11741        33216 :   for (ref = exp->ref; ref; ref = ref->next)
   11742         7741 :     if (ref->type == REF_COMPONENT)
   11743         2195 :       ts = &ref->u.c.component->ts;
   11744              : 
   11745        25475 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11746        25327 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11747              :     {
   11748          719 :       derived = ts->u.derived;
   11749              : 
   11750              :       /* Determine when to use the formatted DTIO procedure.  */
   11751          719 :       if (dt && (dt->format_expr || dt->format_label))
   11752          644 :         formatted = true;
   11753              : 
   11754          719 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11755          719 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11756          719 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11757              : 
   11758          719 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11759              :         {
   11760          449 :           dt->udtio = exp;
   11761          449 :           sym = exp->symtree->n.sym->ns->proc_name;
   11762              :           /* Check to see if this is a nested DTIO call, with the
   11763              :              dummy as the io-list object.  */
   11764          449 :           if (sym && sym == dtio_sub && sym->formal
   11765           30 :               && sym->formal->sym == exp->symtree->n.sym
   11766           30 :               && exp->ref == NULL)
   11767              :             {
   11768            0 :               if (!sym->attr.recursive)
   11769              :                 {
   11770            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11771              :                              sym->name, &sym->declared_at);
   11772            0 :                   return;
   11773              :                 }
   11774              :             }
   11775              :         }
   11776              :     }
   11777              : 
   11778        25475 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11779              :     {
   11780            3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11781              :                 "it is processed by a defined input/output procedure",
   11782              :                 &code->loc);
   11783            3 :       return;
   11784              :     }
   11785              : 
   11786        25472 :   if (ts->type == BT_DERIVED)
   11787              :     {
   11788              :       /* Check that transferred derived type doesn't contain POINTER
   11789              :          components unless it is processed by a defined input/output
   11790              :          procedure".  */
   11791          687 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11792              :         {
   11793            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11794              :                      "components unless it is processed by a defined "
   11795              :                      "input/output procedure", &code->loc);
   11796            2 :           return;
   11797              :         }
   11798              : 
   11799              :       /* F08:C935.  */
   11800          685 :       if (ts->u.derived->attr.proc_pointer_comp)
   11801              :         {
   11802            2 :           gfc_error ("Data transfer element at %L cannot have "
   11803              :                      "procedure pointer components", &code->loc);
   11804            2 :           return;
   11805              :         }
   11806              : 
   11807          683 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11808              :         {
   11809            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11810              :                      "components unless it is processed by a defined "
   11811              :                      "input/output procedure", &code->loc);
   11812            6 :           return;
   11813              :         }
   11814              : 
   11815              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11816              :          be printed.  However, if -std=gnu and not -pedantic, allow
   11817              :          the component to be printed to help debugging.  */
   11818          677 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11819              :         {
   11820            4 :           gfc_error ("Data transfer element at %L "
   11821              :                      "cannot have PRIVATE components", &code->loc);
   11822            4 :             return;
   11823              :         }
   11824          673 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11825              :         {
   11826            4 :           gfc_error ("Data transfer element at %L cannot have "
   11827              :                      "PRIVATE components unless it is processed by "
   11828              :                      "a defined input/output procedure", &code->loc);
   11829            4 :           return;
   11830              :         }
   11831              :     }
   11832              : 
   11833        25454 :   if (exp->expr_type == EXPR_STRUCTURE)
   11834              :     return;
   11835              : 
   11836        25409 :   if (exp->expr_type == EXPR_ARRAY)
   11837              :     return;
   11838              : 
   11839        25033 :   sym = exp->symtree->n.sym;
   11840              : 
   11841        25033 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11842           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11843              :     {
   11844            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11845              :                  "an assumed-size array", &code->loc);
   11846            1 :       return;
   11847              :     }
   11848              : }
   11849              : 
   11850              : 
   11851              : /*********** Toplevel code resolution subroutines ***********/
   11852              : 
   11853              : /* Find the set of labels that are reachable from this block.  We also
   11854              :    record the last statement in each block.  */
   11855              : 
   11856              : static void
   11857       674743 : find_reachable_labels (gfc_code *block)
   11858              : {
   11859       674743 :   gfc_code *c;
   11860              : 
   11861       674743 :   if (!block)
   11862              :     return;
   11863              : 
   11864       423384 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11865              : 
   11866              :   /* Collect labels in this block.  We don't keep those corresponding
   11867              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11868              :      up through the code_stack.  */
   11869      1553867 :   for (c = block; c; c = c->next)
   11870              :     {
   11871      1130483 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11872         3661 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11873              :     }
   11874              : 
   11875              :   /* Merge with labels from parent block.  */
   11876       423384 :   if (cs_base->prev)
   11877              :     {
   11878       347675 :       gcc_assert (cs_base->prev->reachable_labels);
   11879       347675 :       bitmap_ior_into (cs_base->reachable_labels,
   11880              :                        cs_base->prev->reachable_labels);
   11881              :     }
   11882              : }
   11883              : 
   11884              : static void
   11885          197 : resolve_lock_unlock_event (gfc_code *code)
   11886              : {
   11887          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11888          197 :       && (code->expr1->ts.type != BT_DERIVED
   11889          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   11890          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11891          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11892          136 :           || code->expr1->rank != 0
   11893          181 :           || (!gfc_is_coarray (code->expr1) &&
   11894           46 :               !gfc_is_coindexed (code->expr1))))
   11895            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11896            4 :                &code->expr1->where);
   11897          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11898           58 :            && (code->expr1->ts.type != BT_DERIVED
   11899           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   11900           58 :                || code->expr1->ts.u.derived->from_intmod
   11901              :                   != INTMOD_ISO_FORTRAN_ENV
   11902           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   11903              :                   != ISOFORTRAN_EVENT_TYPE
   11904           58 :                || code->expr1->rank != 0))
   11905            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11906              :                &code->expr1->where);
   11907           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11908          209 :            && !gfc_is_coindexed (code->expr1))
   11909            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11910            0 :                &code->expr1->where);
   11911          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11912            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11913            0 :                "coindexed", &code->expr1->where);
   11914              : 
   11915              :   /* Check STAT.  */
   11916          197 :   if (code->expr2
   11917           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11918           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11919            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11920              :                &code->expr2->where);
   11921              : 
   11922          197 :   if (code->expr2
   11923          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11924           54 :                                     _("STAT variable")))
   11925              :     return;
   11926              : 
   11927              :   /* Check ERRMSG.  */
   11928          197 :   if (code->expr3
   11929            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   11930            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   11931            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11932              :                &code->expr3->where);
   11933              : 
   11934          197 :   if (code->expr3
   11935          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   11936            2 :                                     _("ERRMSG variable")))
   11937              :     return;
   11938              : 
   11939              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   11940          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11941           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   11942           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   11943            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   11944              :                "variable", &code->expr4->where);
   11945              : 
   11946          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11947          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   11948           22 :                                     _("ACQUIRED_LOCK variable")))
   11949              :     return;
   11950              : 
   11951              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   11952          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   11953              :     {
   11954           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   11955           36 :           || code->expr4->rank != 0)
   11956            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   11957            0 :                    "expression", &code->expr4->where);
   11958              :     }
   11959              : }
   11960              : 
   11961              : static void
   11962          246 : resolve_team_argument (gfc_expr *team)
   11963              : {
   11964          246 :   gfc_resolve_expr (team);
   11965          246 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   11966          239 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11967          239 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   11968              :     {
   11969            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   11970              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   11971              :                  &team->where);
   11972              :     }
   11973          246 : }
   11974              : 
   11975              : static void
   11976         1358 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   11977              :                                 gfc_expr *e)
   11978              : {
   11979         1358 :   gfc_resolve_expr (e);
   11980         1358 :   if (e
   11981          139 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   11982          124 :           || e->expr_type != EXPR_VARIABLE))
   11983           15 :     gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   11984              :                "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   11985              :                exp_kind);
   11986         1358 : }
   11987              : 
   11988              : void
   11989          679 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   11990              : {
   11991          679 :   resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
   11992          679 :   resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   11993              :                                   gfc_default_character_kind,
   11994              :                                   sync_stat->errmsg);
   11995          679 : }
   11996              : 
   11997              : static void
   11998          260 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   11999              :                          gfc_expr *e)
   12000              : {
   12001          260 :   gfc_resolve_expr (e);
   12002          260 :   if (e
   12003          161 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12004            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12005              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12006          260 : }
   12007              : 
   12008              : static void
   12009          130 : resolve_form_team (gfc_code *code)
   12010              : {
   12011          130 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12012              :                            code->expr1);
   12013          130 :   resolve_team_argument (code->expr2);
   12014          130 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12015              :                            code->expr3);
   12016          130 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12017          130 : }
   12018              : 
   12019              : static void resolve_block_construct (gfc_code *);
   12020              : 
   12021              : static void
   12022           73 : resolve_change_team (gfc_code *code)
   12023              : {
   12024           73 :   resolve_team_argument (code->expr1);
   12025           73 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12026          146 :   resolve_block_construct (code);
   12027              :   /* Map the coarray bounds as selected.  */
   12028           76 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12029            3 :     if (a->ar)
   12030              :       {
   12031            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12032            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12033            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12034              :         else
   12035            3 :           dst = a->st->n.sym->as;
   12036            3 :         dst->corank = src->corank;
   12037            3 :         dst->cotype = src->cotype;
   12038            6 :         for (int i = 0; i < src->corank; ++i)
   12039              :           {
   12040            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12041            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12042            3 :             src->lower[i] = src->upper[i] = nullptr;
   12043              :           }
   12044            3 :         gfc_free_array_spec (src);
   12045            3 :         free (a->ar);
   12046            3 :         a->ar = nullptr;
   12047            3 :         dst->resolved = false;
   12048            3 :         gfc_resolve_array_spec (dst, 0);
   12049              :       }
   12050           73 : }
   12051              : 
   12052              : static void
   12053           43 : resolve_sync_team (gfc_code *code)
   12054              : {
   12055           43 :   resolve_team_argument (code->expr1);
   12056           43 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12057           43 : }
   12058              : 
   12059              : static void
   12060           71 : resolve_end_team (gfc_code *code)
   12061              : {
   12062           71 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12063           71 : }
   12064              : 
   12065              : static void
   12066           54 : resolve_critical (gfc_code *code)
   12067              : {
   12068           54 :   gfc_symtree *symtree;
   12069           54 :   gfc_symbol *lock_type;
   12070           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12071           54 :   static int serial = 0;
   12072              : 
   12073           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12074              : 
   12075           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12076           30 :     return;
   12077              : 
   12078           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12079              :                               GFC_PREFIX ("lock_type"));
   12080           24 :   if (symtree)
   12081           12 :     lock_type = symtree->n.sym;
   12082              :   else
   12083              :     {
   12084           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12085              :                             false) != 0)
   12086            0 :         gcc_unreachable ();
   12087           12 :       lock_type = symtree->n.sym;
   12088           12 :       lock_type->attr.flavor = FL_DERIVED;
   12089           12 :       lock_type->attr.zero_comp = 1;
   12090           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12091           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12092              :     }
   12093              : 
   12094           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12095           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12096            0 :     gcc_unreachable ();
   12097              : 
   12098           24 :   code->resolved_sym = symtree->n.sym;
   12099           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12100           24 :   symtree->n.sym->attr.referenced = 1;
   12101           24 :   symtree->n.sym->attr.artificial = 1;
   12102           24 :   symtree->n.sym->attr.codimension = 1;
   12103           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12104           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12105           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12106           24 :   symtree->n.sym->as->corank = 1;
   12107           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12108           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12109           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12110              :                                                    NULL, 1);
   12111           24 :   gfc_commit_symbols();
   12112              : }
   12113              : 
   12114              : 
   12115              : static void
   12116         1307 : resolve_sync (gfc_code *code)
   12117              : {
   12118              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12119         1307 :   if (code->expr1)
   12120              :     {
   12121           71 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12122            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12123              :                    "INTEGER expression", &code->expr1->where);
   12124           71 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12125           27 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12126            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12127              :                    &code->expr1->where);
   12128           70 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12129           70 :                && gfc_simplify_expr (code->expr1, 0))
   12130              :         {
   12131           20 :            gfc_constructor *cons;
   12132           20 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12133           60 :            for (; cons; cons = gfc_constructor_next (cons))
   12134           20 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12135           20 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12136            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12137              :                           "num_images()", &cons->expr->where);
   12138              :         }
   12139              :     }
   12140              : 
   12141              :   /* Check STAT.  */
   12142         1307 :   gfc_resolve_expr (code->expr2);
   12143         1307 :   if (code->expr2)
   12144              :     {
   12145          108 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12146            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12147              :                    &code->expr2->where);
   12148              :       else
   12149          107 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12150          107 :                                   _("STAT variable"));
   12151              :     }
   12152              : 
   12153              :   /* Check ERRMSG.  */
   12154         1307 :   gfc_resolve_expr (code->expr3);
   12155         1307 :   if (code->expr3)
   12156              :     {
   12157           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12158            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12159              :                    &code->expr3->where);
   12160              :       else
   12161           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12162           86 :                                   _("ERRMSG variable"));
   12163              :     }
   12164         1307 : }
   12165              : 
   12166              : 
   12167              : /* Given a branch to a label, see if the branch is conforming.
   12168              :    The code node describes where the branch is located.  */
   12169              : 
   12170              : static void
   12171       108266 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12172              : {
   12173       108266 :   code_stack *stack;
   12174              : 
   12175       108266 :   if (label == NULL)
   12176              :     return;
   12177              : 
   12178              :   /* Step one: is this a valid branching target?  */
   12179              : 
   12180         2460 :   if (label->defined == ST_LABEL_UNKNOWN)
   12181              :     {
   12182            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12183              :                  &code->loc);
   12184            4 :       return;
   12185              :     }
   12186              : 
   12187         2456 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12188              :     {
   12189            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12190              :                  "for the branch statement at %L", &label->where, &code->loc);
   12191            4 :       return;
   12192              :     }
   12193              : 
   12194              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12195              : 
   12196         2452 :   if (code->here == label)
   12197              :     {
   12198            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12199              :                    &code->loc);
   12200            0 :       return;
   12201              :     }
   12202              : 
   12203              :   /* Step three:  See if the label is in the same block as the
   12204              :      branching statement.  The hard work has been done by setting up
   12205              :      the bitmap reachable_labels.  */
   12206              : 
   12207         2452 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12208              :     {
   12209              :       /* Check now whether there is a CRITICAL construct; if so, check
   12210              :          whether the label is still visible outside of the CRITICAL block,
   12211              :          which is invalid.  */
   12212         6267 :       for (stack = cs_base; stack; stack = stack->prev)
   12213              :         {
   12214         3883 :           if (stack->current->op == EXEC_CRITICAL
   12215         3883 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12216            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12217              :                       "label at %L", &code->loc, &label->where);
   12218         3881 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12219         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12220            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12221              :                       "for label at %L", &code->loc, &label->where);
   12222         3881 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12223         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12224            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12225              :                       "for label at %L", &code->loc, &label->where);
   12226              :         }
   12227              : 
   12228              :       return;
   12229              :     }
   12230              : 
   12231              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12232              :     still be the label of the END of the enclosing block, in which
   12233              :     case we find it by going up the code_stack.  */
   12234              : 
   12235          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12236              :     {
   12237          131 :       if (stack->current->next && stack->current->next->here == label)
   12238              :         break;
   12239          101 :       if (stack->current->op == EXEC_CRITICAL)
   12240              :         {
   12241              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12242              :              construct as END CRITICAL is still part of it.  */
   12243            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12244              :                       " at %L", &code->loc, &label->where);
   12245            2 :           return;
   12246              :         }
   12247           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12248              :         {
   12249            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12250              :                      "label at %L", &code->loc, &label->where);
   12251            0 :           return;
   12252              :         }
   12253              :     }
   12254              : 
   12255           66 :   if (stack)
   12256              :     {
   12257           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12258              :       return;
   12259              :     }
   12260              : 
   12261              :   /* The label is not in an enclosing block, so illegal.  This was
   12262              :      allowed in Fortran 66, so we allow it as extension.  No
   12263              :      further checks are necessary in this case.  */
   12264           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12265              :                   "as the GOTO statement at %L", &label->where,
   12266              :                   &code->loc);
   12267           36 :   return;
   12268              : }
   12269              : 
   12270              : 
   12271              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12272              : 
   12273              : static bool
   12274         1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12275              : {
   12276         1467 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12277         1467 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12278         1467 :   bool result = false;
   12279         1467 :   int i;
   12280              : 
   12281              :   /* Compare the rank.  */
   12282         1467 :   if (expr1->rank != expr2->rank)
   12283              :     return result;
   12284              : 
   12285              :   /* Compare the size of each dimension.  */
   12286         2811 :   for (i=0; i<expr1->rank; i++)
   12287              :     {
   12288         1495 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12289          151 :         goto ignore;
   12290              : 
   12291         1344 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12292            0 :         goto ignore;
   12293              : 
   12294         1344 :       if (mpz_cmp (shape[i], shape2[i]))
   12295            0 :         goto over;
   12296              :     }
   12297              : 
   12298              :   /* When either of the two expression is an assumed size array, we
   12299              :      ignore the comparison of dimension sizes.  */
   12300         1316 : ignore:
   12301              :   result = true;
   12302              : 
   12303         1467 : over:
   12304         1467 :   gfc_clear_shape (shape, i);
   12305         1467 :   gfc_clear_shape (shape2, i);
   12306         1467 :   return result;
   12307              : }
   12308              : 
   12309              : 
   12310              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12311              :    has the same shape as the outermost WHERE mask expression.  */
   12312              : 
   12313              : static void
   12314          509 : resolve_where (gfc_code *code, gfc_expr *mask)
   12315              : {
   12316          509 :   gfc_code *cblock;
   12317          509 :   gfc_code *cnext;
   12318          509 :   gfc_expr *e = NULL;
   12319              : 
   12320          509 :   cblock = code->block;
   12321              : 
   12322              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12323              :      In case of nested WHERE, only the outermost one is stored.  */
   12324          509 :   if (mask == NULL) /* outermost WHERE */
   12325          453 :     e = cblock->expr1;
   12326              :   else /* inner WHERE */
   12327          509 :     e = mask;
   12328              : 
   12329         1387 :   while (cblock)
   12330              :     {
   12331          878 :       if (cblock->expr1)
   12332              :         {
   12333              :           /* Check if the mask-expr has a consistent shape with the
   12334              :              outermost WHERE mask-expr.  */
   12335          714 :           if (!resolve_where_shape (cblock->expr1, e))
   12336            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12337            0 :                        &cblock->expr1->where);
   12338              :          }
   12339              : 
   12340              :       /* the assignment statement of a WHERE statement, or the first
   12341              :          statement in where-body-construct of a WHERE construct */
   12342          878 :       cnext = cblock->next;
   12343         1733 :       while (cnext)
   12344              :         {
   12345          855 :           switch (cnext->op)
   12346              :             {
   12347              :             /* WHERE assignment statement */
   12348          753 :             case EXEC_ASSIGN:
   12349              : 
   12350              :               /* Check shape consistent for WHERE assignment target.  */
   12351          753 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12352            0 :                gfc_error ("WHERE assignment target at %L has "
   12353            0 :                           "inconsistent shape", &cnext->expr1->where);
   12354              : 
   12355          753 :               if (cnext->op == EXEC_ASSIGN
   12356          753 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12357            0 :                 cnext->expr1->must_finalize = 1;
   12358              : 
   12359              :               break;
   12360              : 
   12361              : 
   12362           46 :             case EXEC_ASSIGN_CALL:
   12363           46 :               resolve_call (cnext);
   12364           46 :               if (!cnext->resolved_sym->attr.elemental)
   12365            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12366            2 :                           &cnext->ext.actual->expr->where);
   12367              :               break;
   12368              : 
   12369              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12370           56 :             case EXEC_WHERE:
   12371           56 :               resolve_where (cnext, e);
   12372           56 :               break;
   12373              : 
   12374            0 :             default:
   12375            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12376              :                          &cnext->loc);
   12377              :             }
   12378              :          /* the next statement within the same where-body-construct */
   12379          855 :          cnext = cnext->next;
   12380              :        }
   12381              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12382          878 :     cblock = cblock->block;
   12383              :   }
   12384          509 : }
   12385              : 
   12386              : 
   12387              : /* Resolve assignment in FORALL construct.
   12388              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12389              :    FORALL index variables.  */
   12390              : 
   12391              : static void
   12392         2375 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12393              : {
   12394         2375 :   int n;
   12395         2375 :   gfc_symbol *forall_index;
   12396              : 
   12397         6771 :   for (n = 0; n < nvar; n++)
   12398              :     {
   12399         4396 :       forall_index = var_expr[n]->symtree->n.sym;
   12400              : 
   12401              :       /* Check whether the assignment target is one of the FORALL index
   12402              :          variable.  */
   12403         4396 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12404         4396 :           && (code->expr1->symtree->n.sym == forall_index))
   12405            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12406              :                    &code->expr1->where);
   12407              :       else
   12408              :         {
   12409              :           /* If one of the FORALL index variables doesn't appear in the
   12410              :              assignment variable, then there could be a many-to-one
   12411              :              assignment.  Emit a warning rather than an error because the
   12412              :              mask could be resolving this problem.
   12413              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12414              :              many-to-one assignments are semantically valid (formalized with
   12415              :              the REDUCE locality-spec in Fortran 2023).  */
   12416         4396 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12417         4396 :               && !gfc_do_concurrent_flag)
   12418            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12419              :                          "left side of the assignment at %L and so might "
   12420              :                          "cause multiple assignment to this object",
   12421            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12422              :         }
   12423              :     }
   12424         2375 : }
   12425              : 
   12426              : 
   12427              : /* Resolve WHERE statement in FORALL construct.  */
   12428              : 
   12429              : static void
   12430           47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12431              :                                   gfc_expr **var_expr)
   12432              : {
   12433           47 :   gfc_code *cblock;
   12434           47 :   gfc_code *cnext;
   12435              : 
   12436           47 :   cblock = code->block;
   12437          113 :   while (cblock)
   12438              :     {
   12439              :       /* the assignment statement of a WHERE statement, or the first
   12440              :          statement in where-body-construct of a WHERE construct */
   12441           66 :       cnext = cblock->next;
   12442          132 :       while (cnext)
   12443              :         {
   12444           66 :           switch (cnext->op)
   12445              :             {
   12446              :             /* WHERE assignment statement */
   12447           66 :             case EXEC_ASSIGN:
   12448           66 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12449              : 
   12450           66 :               if (cnext->op == EXEC_ASSIGN
   12451           66 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12452            0 :                 cnext->expr1->must_finalize = 1;
   12453              : 
   12454              :               break;
   12455              : 
   12456              :             /* WHERE operator assignment statement */
   12457            0 :             case EXEC_ASSIGN_CALL:
   12458            0 :               resolve_call (cnext);
   12459            0 :               if (!cnext->resolved_sym->attr.elemental)
   12460            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12461            0 :                           &cnext->ext.actual->expr->where);
   12462              :               break;
   12463              : 
   12464              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12465            0 :             case EXEC_WHERE:
   12466            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12467            0 :               break;
   12468              : 
   12469            0 :             default:
   12470            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12471              :                          &cnext->loc);
   12472              :             }
   12473              :           /* the next statement within the same where-body-construct */
   12474           66 :           cnext = cnext->next;
   12475              :         }
   12476              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12477           66 :       cblock = cblock->block;
   12478              :     }
   12479           47 : }
   12480              : 
   12481              : 
   12482              : /* Traverse the FORALL body to check whether the following errors exist:
   12483              :    1. For assignment, check if a many-to-one assignment happens.
   12484              :    2. For WHERE statement, check the WHERE body to see if there is any
   12485              :       many-to-one assignment.  */
   12486              : 
   12487              : static void
   12488         2202 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12489              : {
   12490         2202 :   gfc_code *c;
   12491              : 
   12492         2202 :   c = code->block->next;
   12493         4827 :   while (c)
   12494              :     {
   12495         2625 :       switch (c->op)
   12496              :         {
   12497         2309 :         case EXEC_ASSIGN:
   12498         2309 :         case EXEC_POINTER_ASSIGN:
   12499         2309 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12500              : 
   12501         2309 :           if (c->op == EXEC_ASSIGN
   12502         2309 :               && gfc_may_be_finalized (c->expr1->ts))
   12503            0 :             c->expr1->must_finalize = 1;
   12504              : 
   12505              :           break;
   12506              : 
   12507            0 :         case EXEC_ASSIGN_CALL:
   12508            0 :           resolve_call (c);
   12509            0 :           break;
   12510              : 
   12511              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12512              :            there is no need to handle it here.  */
   12513              :         case EXEC_FORALL:
   12514              :           break;
   12515           47 :         case EXEC_WHERE:
   12516           47 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12517           47 :           break;
   12518              :         default:
   12519              :           break;
   12520              :         }
   12521              :       /* The next statement in the FORALL body.  */
   12522         2625 :       c = c->next;
   12523              :     }
   12524         2202 : }
   12525              : 
   12526              : 
   12527              : /* Counts the number of iterators needed inside a forall construct, including
   12528              :    nested forall constructs. This is used to allocate the needed memory
   12529              :    in gfc_resolve_forall.  */
   12530              : 
   12531              : static int gfc_count_forall_iterators (gfc_code *code);
   12532              : 
   12533              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12534              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12535              : 
   12536              : static int
   12537         2387 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12538              : {
   12539         2387 :   int max_iters = 0;
   12540              : 
   12541         5226 :   for (gfc_code *c = code; c; c = c->next)
   12542              :     {
   12543         2839 :       int sub_iters = 0;
   12544              : 
   12545         2839 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12546           94 :         sub_iters = gfc_count_forall_iterators (c);
   12547         2745 :       else if (c->op == EXEC_BLOCK)
   12548              :         {
   12549              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12550              :              not in the generic c->block arm list used by IF/SELECT.  */
   12551           21 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12552           21 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12553              :         }
   12554         2724 :       else if (c->block)
   12555          307 :         for (gfc_code *b = c->block; b; b = b->block)
   12556              :           {
   12557          164 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12558          164 :             if (arm_iters > sub_iters)
   12559              :               sub_iters = arm_iters;
   12560              :           }
   12561              : 
   12562         2839 :       if (sub_iters > max_iters)
   12563              :         max_iters = sub_iters;
   12564              :     }
   12565              : 
   12566         2387 :   return max_iters;
   12567              : }
   12568              : 
   12569              : 
   12570              : static int
   12571         2202 : gfc_count_forall_iterators (gfc_code *code)
   12572              : {
   12573         2202 :   int current_iters = 0;
   12574         2202 :   gfc_forall_iterator *fa;
   12575              : 
   12576         2202 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12577              : 
   12578         6320 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12579         4118 :     current_iters++;
   12580              : 
   12581         2202 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   12582              : }
   12583              : 
   12584              : 
   12585              : /* Given a FORALL construct.
   12586              :    1) Resolve the FORALL iterator.
   12587              :    2) Check for shadow index-name(s) and update code block.
   12588              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   12589              : 
   12590              : /* Custom recursive expression walker that replaces symbols.
   12591              :    This ensures we visit ALL expressions including those in array subscripts.  */
   12592              : 
   12593              : static void
   12594          114 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
   12595              : {
   12596          144 :   if (!expr)
   12597              :     return;
   12598              : 
   12599              :   /* Check if this is a variable reference to replace */
   12600          108 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   12601              :     {
   12602           18 :       expr->symtree = new_st;
   12603           18 :       expr->ts = new_st->n.sym->ts;
   12604              :     }
   12605              : 
   12606              :   /* Walk through reference chain (array subscripts, substrings, etc.) */
   12607          108 :   for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
   12608              :     {
   12609            0 :       if (ref->type == REF_ARRAY)
   12610              :         {
   12611              :           gfc_array_ref *ar = &ref->u.ar;
   12612            0 :           for (int i = 0; i < ar->dimen; i++)
   12613              :             {
   12614            0 :               replace_in_expr_recursive (ar->start[i], old_sym, new_st);
   12615            0 :               replace_in_expr_recursive (ar->end[i], old_sym, new_st);
   12616            0 :               replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
   12617              :             }
   12618              :         }
   12619            0 :       else if (ref->type == REF_SUBSTRING)
   12620              :         {
   12621            0 :           replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
   12622            0 :           replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
   12623              :         }
   12624              :     }
   12625              : 
   12626              :   /* Walk through sub-expressions based on expression type */
   12627          108 :   switch (expr->expr_type)
   12628              :     {
   12629           30 :     case EXPR_OP:
   12630           30 :       replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
   12631           30 :       replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
   12632           30 :       break;
   12633              : 
   12634            6 :     case EXPR_FUNCTION:
   12635           18 :       for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
   12636           12 :         replace_in_expr_recursive (a->expr, old_sym, new_st);
   12637              :       break;
   12638              : 
   12639            0 :     case EXPR_ARRAY:
   12640            0 :     case EXPR_STRUCTURE:
   12641            0 :       for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   12642            0 :            c; c = gfc_constructor_next (c))
   12643              :         {
   12644            0 :           replace_in_expr_recursive (c->expr, old_sym, new_st);
   12645            0 :           if (c->iterator)
   12646              :             {
   12647            0 :               replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
   12648            0 :               replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
   12649            0 :               replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
   12650              :             }
   12651              :         }
   12652              :       break;
   12653              : 
   12654              :     default:
   12655              :       break;
   12656              :     }
   12657              : }
   12658              : 
   12659              : 
   12660              : /* Walk code tree and replace all variable references */
   12661              : 
   12662              : static void
   12663           18 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   12664              : {
   12665           18 :   if (!code)
   12666              :     return;
   12667              : 
   12668           36 :   for (gfc_code *c = code; c; c = c->next)
   12669              :     {
   12670              :       /* Replace in expressions associated with this code node */
   12671           18 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   12672           18 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   12673           18 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   12674           18 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   12675              : 
   12676              :       /* Handle special code types with additional expressions */
   12677           18 :       switch (c->op)
   12678              :         {
   12679            0 :         case EXEC_DO:
   12680            0 :           if (c->ext.iterator)
   12681              :             {
   12682            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   12683            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   12684            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   12685              :             }
   12686              :           break;
   12687              : 
   12688            0 :         case EXEC_CALL:
   12689            0 :         case EXEC_ASSIGN_CALL:
   12690            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   12691            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   12692              :           break;
   12693              : 
   12694            0 :         case EXEC_SELECT:
   12695            0 :           for (gfc_code *b = c->block; b; b = b->block)
   12696              :             {
   12697            0 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   12698              :                 {
   12699            0 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   12700            0 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   12701              :                 }
   12702            0 :               replace_in_code_recursive (b->next, old_sym, new_st);
   12703              :             }
   12704              :           break;
   12705              : 
   12706            0 :         case EXEC_FORALL:
   12707            0 :         case EXEC_DO_CONCURRENT:
   12708            0 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   12709              :             {
   12710            0 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   12711            0 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   12712            0 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   12713              :             }
   12714              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   12715              :              they'll be handled separately */
   12716              :           break;
   12717              : 
   12718              :         default:
   12719              :           break;
   12720              :         }
   12721              : 
   12722              :       /* Recurse into blocks */
   12723           18 :       if (c->block)
   12724            0 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   12725              :     }
   12726              : }
   12727              : 
   12728              : 
   12729              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   12730              : 
   12731              : static void
   12732           18 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   12733              :                               gfc_symtree *shadow_st)
   12734              : {
   12735              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   12736            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   12737           18 : }
   12738              : 
   12739              : 
   12740              : static void
   12741         2202 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   12742              : {
   12743         2202 :   static gfc_expr **var_expr;
   12744         2202 :   static int total_var = 0;
   12745         2202 :   static int nvar = 0;
   12746         2202 :   int i, old_nvar, tmp;
   12747         2202 :   gfc_forall_iterator *fa;
   12748         2202 :   bool shadow = false;
   12749              : 
   12750         2202 :   old_nvar = nvar;
   12751              : 
   12752              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   12753         2202 :   if (code->op == EXEC_FORALL
   12754         2202 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   12755              :     return;
   12756              : 
   12757              :   /* Start to resolve a FORALL construct   */
   12758              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   12759              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   12760              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   12761              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   12762         2202 :   if (forall_save == 0 && nvar == 0)
   12763              :     {
   12764              :       /* Count the total number of FORALL indices in the nested FORALL
   12765              :          construct in order to allocate the VAR_EXPR with proper size.  */
   12766         2108 :       total_var = gfc_count_forall_iterators (code);
   12767              : 
   12768              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12769         2108 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12770              :     }
   12771              : 
   12772              :   /* The information about FORALL iterator, including FORALL indices start,
   12773              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   12774              :      stride.  Check for a shadow index-name.  */
   12775         6320 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12776              :     {
   12777              :       /* Fortran 2008: C738 (R753).  */
   12778         4118 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12779              :         {
   12780            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12781              :                      "of type integer", &fa->var->where);
   12782            2 :           continue;
   12783              :         }
   12784              : 
   12785              :       /* Check if any outer FORALL index name is the same as the current
   12786              :          one.  Skip this check if the iterator is a shadow variable (from
   12787              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   12788         7125 :       for (i = 0; i < nvar; i++)
   12789              :         {
   12790         3009 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   12791         3009 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12792            0 :             gfc_error ("An outer FORALL construct already has an index "
   12793              :                         "with this name %L", &fa->var->where);
   12794              :         }
   12795              : 
   12796         4116 :       if (fa->shadow)
   12797           18 :         shadow = true;
   12798              : 
   12799              :       /* Record the current FORALL index.  */
   12800         4116 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12801              : 
   12802         4116 :       nvar++;
   12803              : 
   12804              :       /* No memory leak.  */
   12805         4116 :       gcc_assert (nvar <= total_var);
   12806              :     }
   12807              : 
   12808              :   /* Need to walk the code and replace references to the index-name with
   12809              :      references to the shadow index-name. This must be done BEFORE resolving
   12810              :      the body so that resolution uses the correct shadow variables.  */
   12811         2202 :   if (shadow)
   12812              :     {
   12813              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   12814           42 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12815              :         {
   12816           24 :           if (fa->shadow)
   12817              :             {
   12818           18 :               gfc_symtree *shadow_st;
   12819           18 :               const char *shadow_name_str;
   12820           18 :               char *outer_name;
   12821              : 
   12822              :               /* fa->var now points to the shadow variable "_name".  */
   12823           18 :               shadow_name_str = fa->var->symtree->name;
   12824           18 :               shadow_st = fa->var->symtree;
   12825              : 
   12826           18 :               if (shadow_name_str[0] != '_')
   12827            0 :                 gfc_internal_error ("Expected shadow variable name to start with _");
   12828              : 
   12829           18 :               outer_name = (char *) alloca (strlen (shadow_name_str));
   12830           18 :               strcpy (outer_name, shadow_name_str + 1);
   12831              : 
   12832              :               /* Find the ITERATOR symbol in the current namespace.
   12833              :                  This is the local DO CONCURRENT variable that body expressions reference.  */
   12834           18 :               gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
   12835              : 
   12836           18 :               if (!iter_st)
   12837              :                 /* No iterator variable found - this shouldn't happen */
   12838            0 :                 continue;
   12839              : 
   12840           18 :               gfc_symbol *iter_sym = iter_st->n.sym;
   12841              : 
   12842              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   12843           18 :               if (code->block && code->block->next)
   12844           18 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   12845              :             }
   12846              :         }
   12847              :     }
   12848              : 
   12849              :   /* Resolve the FORALL body.  */
   12850         2202 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12851              : 
   12852              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12853         2202 :   gfc_resolve_blocks (code->block, ns);
   12854              : 
   12855         2202 :   tmp = nvar;
   12856         2202 :   nvar = old_nvar;
   12857              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12858         6318 :   for (i = nvar; i < tmp; i++)
   12859         4116 :      gfc_free_expr (var_expr[i]);
   12860              : 
   12861         2202 :   if (nvar == 0)
   12862              :     {
   12863              :       /* We are in the outermost FORALL construct.  */
   12864         2108 :       gcc_assert (forall_save == 0);
   12865              : 
   12866              :       /* VAR_EXPR is not needed any more.  */
   12867         2108 :       free (var_expr);
   12868         2108 :       total_var = 0;
   12869              :     }
   12870              : }
   12871              : 
   12872              : 
   12873              : /* Resolve a BLOCK construct statement.  */
   12874              : 
   12875              : static void
   12876         8013 : resolve_block_construct (gfc_code* code)
   12877              : {
   12878         8013 :   gfc_namespace *ns = code->ext.block.ns;
   12879              : 
   12880              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12881              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12882              :      namespace.  */
   12883         8013 :   gfc_resolve (ns);
   12884            0 : }
   12885              : 
   12886              : 
   12887              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12888              :    DO code nodes.  */
   12889              : 
   12890              : void
   12891       330314 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12892              : {
   12893       330314 :   bool t;
   12894              : 
   12895       672065 :   for (; b; b = b->block)
   12896              :     {
   12897       341751 :       t = gfc_resolve_expr (b->expr1);
   12898       341751 :       if (!gfc_resolve_expr (b->expr2))
   12899            0 :         t = false;
   12900              : 
   12901       341751 :       switch (b->op)
   12902              :         {
   12903       236319 :         case EXEC_IF:
   12904       236319 :           if (t && b->expr1 != NULL
   12905       232032 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   12906            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   12907              :                        &b->expr1->where);
   12908              :           break;
   12909              : 
   12910          764 :         case EXEC_WHERE:
   12911          764 :           if (t
   12912          764 :               && b->expr1 != NULL
   12913          631 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   12914            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   12915              :                        &b->expr1->where);
   12916              :           break;
   12917              : 
   12918           76 :         case EXEC_GOTO:
   12919           76 :           resolve_branch (b->label1, b);
   12920           76 :           break;
   12921              : 
   12922            0 :         case EXEC_BLOCK:
   12923            0 :           resolve_block_construct (b);
   12924            0 :           break;
   12925              : 
   12926              :         case EXEC_SELECT:
   12927              :         case EXEC_SELECT_TYPE:
   12928              :         case EXEC_SELECT_RANK:
   12929              :         case EXEC_FORALL:
   12930              :         case EXEC_DO:
   12931              :         case EXEC_DO_WHILE:
   12932              :         case EXEC_DO_CONCURRENT:
   12933              :         case EXEC_CRITICAL:
   12934              :         case EXEC_READ:
   12935              :         case EXEC_WRITE:
   12936              :         case EXEC_IOLENGTH:
   12937              :         case EXEC_WAIT:
   12938              :           break;
   12939              : 
   12940         2697 :         case EXEC_OMP_ATOMIC:
   12941         2697 :         case EXEC_OACC_ATOMIC:
   12942         2697 :           {
   12943              :             /* Verify this before calling gfc_resolve_code, which might
   12944              :                change it.  */
   12945         2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   12946              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   12947              :           }
   12948              :           break;
   12949              : 
   12950              :         case EXEC_OACC_PARALLEL_LOOP:
   12951              :         case EXEC_OACC_PARALLEL:
   12952              :         case EXEC_OACC_KERNELS_LOOP:
   12953              :         case EXEC_OACC_KERNELS:
   12954              :         case EXEC_OACC_SERIAL_LOOP:
   12955              :         case EXEC_OACC_SERIAL:
   12956              :         case EXEC_OACC_DATA:
   12957              :         case EXEC_OACC_HOST_DATA:
   12958              :         case EXEC_OACC_LOOP:
   12959              :         case EXEC_OACC_UPDATE:
   12960              :         case EXEC_OACC_WAIT:
   12961              :         case EXEC_OACC_CACHE:
   12962              :         case EXEC_OACC_ENTER_DATA:
   12963              :         case EXEC_OACC_EXIT_DATA:
   12964              :         case EXEC_OACC_ROUTINE:
   12965              :         case EXEC_OMP_ALLOCATE:
   12966              :         case EXEC_OMP_ALLOCATORS:
   12967              :         case EXEC_OMP_ASSUME:
   12968              :         case EXEC_OMP_CRITICAL:
   12969              :         case EXEC_OMP_DISPATCH:
   12970              :         case EXEC_OMP_DISTRIBUTE:
   12971              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   12972              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   12973              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   12974              :         case EXEC_OMP_DO:
   12975              :         case EXEC_OMP_DO_SIMD:
   12976              :         case EXEC_OMP_ERROR:
   12977              :         case EXEC_OMP_LOOP:
   12978              :         case EXEC_OMP_MASKED:
   12979              :         case EXEC_OMP_MASKED_TASKLOOP:
   12980              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   12981              :         case EXEC_OMP_MASTER:
   12982              :         case EXEC_OMP_MASTER_TASKLOOP:
   12983              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   12984              :         case EXEC_OMP_ORDERED:
   12985              :         case EXEC_OMP_PARALLEL:
   12986              :         case EXEC_OMP_PARALLEL_DO:
   12987              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   12988              :         case EXEC_OMP_PARALLEL_LOOP:
   12989              :         case EXEC_OMP_PARALLEL_MASKED:
   12990              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   12991              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   12992              :         case EXEC_OMP_PARALLEL_MASTER:
   12993              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   12994              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   12995              :         case EXEC_OMP_PARALLEL_SECTIONS:
   12996              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   12997              :         case EXEC_OMP_SECTIONS:
   12998              :         case EXEC_OMP_SIMD:
   12999              :         case EXEC_OMP_SCOPE:
   13000              :         case EXEC_OMP_SINGLE:
   13001              :         case EXEC_OMP_TARGET:
   13002              :         case EXEC_OMP_TARGET_DATA:
   13003              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13004              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13005              :         case EXEC_OMP_TARGET_PARALLEL:
   13006              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13007              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13008              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13009              :         case EXEC_OMP_TARGET_SIMD:
   13010              :         case EXEC_OMP_TARGET_TEAMS:
   13011              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13012              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13013              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13014              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13015              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13016              :         case EXEC_OMP_TARGET_UPDATE:
   13017              :         case EXEC_OMP_TASK:
   13018              :         case EXEC_OMP_TASKGROUP:
   13019              :         case EXEC_OMP_TASKLOOP:
   13020              :         case EXEC_OMP_TASKLOOP_SIMD:
   13021              :         case EXEC_OMP_TASKWAIT:
   13022              :         case EXEC_OMP_TASKYIELD:
   13023              :         case EXEC_OMP_TEAMS:
   13024              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13025              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13026              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13027              :         case EXEC_OMP_TEAMS_LOOP:
   13028              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13029              :         case EXEC_OMP_TILE:
   13030              :         case EXEC_OMP_UNROLL:
   13031              :         case EXEC_OMP_WORKSHARE:
   13032              :           break;
   13033              : 
   13034            0 :         default:
   13035            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13036              :         }
   13037              : 
   13038       341751 :       gfc_resolve_code (b->next, ns);
   13039              :     }
   13040       330314 : }
   13041              : 
   13042              : bool
   13043            0 : caf_possible_reallocate (gfc_expr *e)
   13044              : {
   13045            0 :   symbol_attribute caf_attr;
   13046            0 :   gfc_ref *last_arr_ref = nullptr;
   13047              : 
   13048            0 :   caf_attr = gfc_caf_attr (e);
   13049            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13050              :     return false;
   13051              : 
   13052              :   /* Only full array refs can indicate a needed reallocation.  */
   13053            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13054            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13055            0 :       last_arr_ref = ref;
   13056              : 
   13057            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13058              : }
   13059              : 
   13060              : /* Does everything to resolve an ordinary assignment.  Returns true
   13061              :    if this is an interface assignment.  */
   13062              : static bool
   13063       285132 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13064              : {
   13065       285132 :   bool rval = false;
   13066       285132 :   gfc_expr *lhs;
   13067       285132 :   gfc_expr *rhs;
   13068       285132 :   int n;
   13069       285132 :   gfc_ref *ref;
   13070       285132 :   symbol_attribute attr;
   13071              : 
   13072       285132 :   if (gfc_extend_assign (code, ns))
   13073              :     {
   13074          918 :       gfc_expr** rhsptr;
   13075              : 
   13076          918 :       if (code->op == EXEC_ASSIGN_CALL)
   13077              :         {
   13078          469 :           lhs = code->ext.actual->expr;
   13079          469 :           rhsptr = &code->ext.actual->next->expr;
   13080              :         }
   13081              :       else
   13082              :         {
   13083          449 :           gfc_actual_arglist* args;
   13084          449 :           gfc_typebound_proc* tbp;
   13085              : 
   13086          449 :           gcc_assert (code->op == EXEC_COMPCALL);
   13087              : 
   13088          449 :           args = code->expr1->value.compcall.actual;
   13089          449 :           lhs = args->expr;
   13090          449 :           rhsptr = &args->next->expr;
   13091              : 
   13092          449 :           tbp = code->expr1->value.compcall.tbp;
   13093          449 :           gcc_assert (!tbp->is_generic);
   13094              :         }
   13095              : 
   13096              :       /* Make a temporary rhs when there is a default initializer
   13097              :          and rhs is the same symbol as the lhs.  */
   13098          918 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13099          507 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13100          436 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13101         1206 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13102           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13103              : 
   13104          918 :       return true;
   13105              :     }
   13106              : 
   13107       284214 :   lhs = code->expr1;
   13108       284214 :   rhs = code->expr2;
   13109              : 
   13110       284214 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13111       264279 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13112        22505 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13113       306719 :       && gfc_expr_attr (lhs).proc_pointer)
   13114              :     {
   13115            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13116              :                  "pointer component",
   13117              :                  &lhs->where);
   13118            1 :       return false;
   13119              :     }
   13120              : 
   13121       334882 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13122       248939 :       && rhs->ts.type == BT_CHARACTER
   13123       284606 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13124              :     {
   13125              :       /* Use of -fdec-char-conversions allows assignment of character data
   13126              :          to non-character variables.  This not permitted for nonconstant
   13127              :          strings.  */
   13128           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13129              :                  gfc_typename (lhs), &rhs->where);
   13130           29 :       return false;
   13131              :     }
   13132              : 
   13133       284184 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13134              :     {
   13135            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13136              :                    gfc_typename (lhs), &rhs->where);
   13137            0 :       return false;
   13138              :     }
   13139              : 
   13140              :   /* Handle the case of a BOZ literal on the RHS.  */
   13141       284184 :   if (rhs->ts.type == BT_BOZ)
   13142              :     {
   13143            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13144              :                            "statement value nor an actual argument of "
   13145              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13146              :                            &rhs->where))
   13147              :         return false;
   13148              : 
   13149            1 :       switch (lhs->ts.type)
   13150              :         {
   13151            0 :         case BT_INTEGER:
   13152            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13153              :             return false;
   13154              :           break;
   13155            1 :         case BT_REAL:
   13156            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13157              :             return false;
   13158              :           break;
   13159            0 :         default:
   13160            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13161            0 :           return false;
   13162              :         }
   13163              :     }
   13164              : 
   13165       284182 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13166              :     {
   13167           64 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13168           64 :       if (lhs->ts.u.cl != NULL
   13169           64 :             && lhs->ts.u.cl->length != NULL
   13170           53 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13171           53 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13172              : 
   13173           64 :       if (rhs->expr_type == EXPR_CONSTANT)
   13174           26 :         rlen = rhs->value.character.length;
   13175              : 
   13176           38 :       else if (rhs->ts.u.cl != NULL
   13177           38 :                  && rhs->ts.u.cl->length != NULL
   13178           35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13179           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13180              : 
   13181           64 :       if (rlen && llen && rlen > llen)
   13182           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13183              :                          "CHARACTER expression will be truncated "
   13184              :                          "in assignment (%wd/%wd) at %L",
   13185              :                          llen, rlen, &code->loc);
   13186              :     }
   13187              : 
   13188              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13189              :      to a temporary if the lvalue symbol is referenced in it.  */
   13190       284182 :   if (lhs->rank)
   13191              :     {
   13192       111677 :       for (ref = lhs->ref; ref; ref= ref->next)
   13193        59624 :         if (ref->type == REF_ARRAY)
   13194              :           {
   13195       131854 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13196        78029 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13197        78259 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13198          230 :                                            ref->u.ar.start[n]))
   13199           14 :                 ref->u.ar.start[n]
   13200           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13201              :           }
   13202              :     }
   13203              : 
   13204       284182 :   if (gfc_pure (NULL))
   13205              :     {
   13206         3370 :       if (lhs->ts.type == BT_DERIVED
   13207          136 :             && lhs->expr_type == EXPR_VARIABLE
   13208          136 :             && lhs->ts.u.derived->attr.pointer_comp
   13209            4 :             && rhs->expr_type == EXPR_VARIABLE
   13210         3373 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13211            2 :                 || gfc_is_coindexed (rhs)))
   13212              :         {
   13213              :           /* F2008, C1283.  */
   13214            2 :           if (gfc_is_coindexed (rhs))
   13215            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13216              :                         "a derived type variable with a POINTER "
   13217              :                         "component in a PURE procedure",
   13218              :                         &rhs->where);
   13219              :           else
   13220              :           /* F2008, C1283 (4).  */
   13221            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13222              :                         "shall not be used as the expr at %L of an intrinsic "
   13223              :                         "assignment statement in which the variable is of a "
   13224              :                         "derived type if the derived type has a pointer "
   13225              :                         "component at any level of component selection.",
   13226              :                         &rhs->where);
   13227            2 :           return rval;
   13228              :         }
   13229              : 
   13230              :       /* Fortran 2008, C1283.  */
   13231         3368 :       if (gfc_is_coindexed (lhs))
   13232              :         {
   13233            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13234              :                      "procedure", &rhs->where);
   13235            1 :           return rval;
   13236              :         }
   13237              :     }
   13238              : 
   13239       284179 :   if (gfc_implicit_pure (NULL))
   13240              :     {
   13241         7201 :       if (lhs->expr_type == EXPR_VARIABLE
   13242         7201 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13243         5124 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13244          253 :         gfc_unset_implicit_pure (NULL);
   13245              : 
   13246         7201 :       if (lhs->ts.type == BT_DERIVED
   13247          320 :             && lhs->expr_type == EXPR_VARIABLE
   13248          320 :             && lhs->ts.u.derived->attr.pointer_comp
   13249            7 :             && rhs->expr_type == EXPR_VARIABLE
   13250         7208 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13251            7 :                 || gfc_is_coindexed (rhs)))
   13252            0 :         gfc_unset_implicit_pure (NULL);
   13253              : 
   13254              :       /* Fortran 2008, C1283.  */
   13255         7201 :       if (gfc_is_coindexed (lhs))
   13256            0 :         gfc_unset_implicit_pure (NULL);
   13257              :     }
   13258              : 
   13259              :   /* F2008, 7.2.1.2.  */
   13260       284179 :   attr = gfc_expr_attr (lhs);
   13261       284179 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13262              :     {
   13263          987 :       if (attr.codimension)
   13264              :         {
   13265            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13266              :                      "permitted", &lhs->where);
   13267            1 :           return false;
   13268              :         }
   13269          986 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13270              :                            "polymorphic variable at %L", &lhs->where))
   13271              :         return false;
   13272          985 :       if (!flag_realloc_lhs)
   13273              :         {
   13274            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13275              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13276            1 :           return false;
   13277              :         }
   13278              :     }
   13279       283192 :   else if (lhs->ts.type == BT_CLASS)
   13280              :     {
   13281            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13282              :                  "assignment at %L - check that there is a matching specific "
   13283              :                  "subroutine for %<=%> operator", &lhs->where);
   13284            9 :       return false;
   13285              :     }
   13286              : 
   13287       284167 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13288              : 
   13289              :   /* F2008, Section 7.2.1.2.  */
   13290       284167 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13291              :     {
   13292            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13293              :                  "component in assignment at %L", &lhs->where);
   13294            1 :       return false;
   13295              :     }
   13296              : 
   13297              :   /* Assign the 'data' of a class object to a derived type.  */
   13298       284166 :   if (lhs->ts.type == BT_DERIVED
   13299         7159 :       && rhs->ts.type == BT_CLASS
   13300          150 :       && (rhs->expr_type != EXPR_ARRAY
   13301          144 :           && rhs->expr_type != EXPR_OP))
   13302          138 :     gfc_add_data_component (rhs);
   13303              : 
   13304              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13305              :      rhs type.  */
   13306       284166 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13307          615 :     gfc_find_vtab (&rhs->ts);
   13308              : 
   13309       284166 :   gfc_check_assign (lhs, rhs, 1);
   13310              : 
   13311       284166 :   return false;
   13312              : }
   13313              : 
   13314              : 
   13315              : /* Add a component reference onto an expression.  */
   13316              : 
   13317              : static void
   13318          665 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13319              : {
   13320          665 :   gfc_ref **ref;
   13321          665 :   ref = &(e->ref);
   13322          889 :   while (*ref)
   13323          224 :     ref = &((*ref)->next);
   13324          665 :   *ref = gfc_get_ref ();
   13325          665 :   (*ref)->type = REF_COMPONENT;
   13326          665 :   (*ref)->u.c.sym = e->ts.u.derived;
   13327          665 :   (*ref)->u.c.component = c;
   13328          665 :   e->ts = c->ts;
   13329              : 
   13330              :   /* Add a full array ref, as necessary.  */
   13331          665 :   if (c->as)
   13332              :     {
   13333           84 :       gfc_add_full_array_ref (e, c->as);
   13334           84 :       e->rank = c->as->rank;
   13335           84 :       e->corank = c->as->corank;
   13336              :     }
   13337          665 : }
   13338              : 
   13339              : 
   13340              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13341              :    pointer assignments can be made.  */
   13342              : 
   13343              : static gfc_code *
   13344          988 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13345              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13346              : {
   13347          988 :   gfc_code *this_code;
   13348              : 
   13349          988 :   this_code = gfc_get_code (op);
   13350          988 :   this_code->next = NULL;
   13351          988 :   this_code->expr1 = gfc_copy_expr (expr1);
   13352          988 :   this_code->expr2 = gfc_copy_expr (expr2);
   13353          988 :   this_code->loc = loc;
   13354          988 :   if (comp1 && comp2)
   13355              :     {
   13356          288 :       add_comp_ref (this_code->expr1, comp1);
   13357          288 :       add_comp_ref (this_code->expr2, comp2);
   13358              :     }
   13359              : 
   13360          988 :   return this_code;
   13361              : }
   13362              : 
   13363              : 
   13364              : /* Makes a temporary variable expression based on the characteristics of
   13365              :    a given variable expression.  If allocatable is set, the temporary is
   13366              :    unconditionally allocatable*/
   13367              : 
   13368              : static gfc_expr*
   13369          482 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13370              :                     bool allocatable = false)
   13371              : {
   13372          482 :   static int serial = 0;
   13373          482 :   char name[GFC_MAX_SYMBOL_LEN];
   13374          482 :   gfc_symtree *tmp;
   13375          482 :   gfc_array_spec *as;
   13376          482 :   gfc_array_ref *aref;
   13377          482 :   gfc_ref *ref;
   13378              : 
   13379          482 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13380          482 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13381          482 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13382              : 
   13383          482 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13384            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13385              :                                                     NULL,
   13386            0 :                                                     e->value.character.length);
   13387              : 
   13388          482 :   as = NULL;
   13389          482 :   ref = NULL;
   13390          482 :   aref = NULL;
   13391              : 
   13392              :   /* Obtain the arrayspec for the temporary.  */
   13393          482 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13394              :        && e->expr_type != EXPR_FUNCTION
   13395              :        && e->expr_type != EXPR_OP)
   13396              :     {
   13397           52 :       aref = gfc_find_array_ref (e);
   13398           52 :       if (e->expr_type == EXPR_VARIABLE
   13399           52 :           && e->symtree->n.sym->as == aref->as)
   13400              :         as = aref->as;
   13401              :       else
   13402              :         {
   13403            0 :           for (ref = e->ref; ref; ref = ref->next)
   13404            0 :             if (ref->type == REF_COMPONENT
   13405            0 :                 && ref->u.c.component->as == aref->as)
   13406              :               {
   13407              :                 as = aref->as;
   13408              :                 break;
   13409              :               }
   13410              :         }
   13411              :     }
   13412              : 
   13413              :   /* Add the attributes and the arrayspec to the temporary.  */
   13414          482 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13415          482 :   tmp->n.sym->attr.function = 0;
   13416          482 :   tmp->n.sym->attr.proc_pointer = 0;
   13417          482 :   tmp->n.sym->attr.result = 0;
   13418          482 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13419          482 :   tmp->n.sym->attr.dummy = 0;
   13420          482 :   tmp->n.sym->attr.use_assoc = 0;
   13421          482 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13422              : 
   13423              : 
   13424          482 :   if (as && !allocatable)
   13425              :     {
   13426           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13427           52 :       if (!ref)
   13428           52 :         ref = e->ref;
   13429           52 :       if (as->type == AS_DEFERRED)
   13430           46 :         tmp->n.sym->attr.allocatable = 1;
   13431              :     }
   13432          430 :   else if ((e->rank || e->corank)
   13433          130 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13434           24 :                || e->expr_type == EXPR_OP || allocatable))
   13435              :     {
   13436          130 :       tmp->n.sym->as = gfc_get_array_spec ();
   13437          130 :       tmp->n.sym->as->type = AS_DEFERRED;
   13438          130 :       tmp->n.sym->as->rank = e->rank;
   13439          130 :       tmp->n.sym->as->corank = e->corank;
   13440          130 :       tmp->n.sym->attr.allocatable = 1;
   13441          130 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13442          260 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13443              :     }
   13444              :   else
   13445          300 :     tmp->n.sym->attr.dimension = 0;
   13446              : 
   13447          482 :   gfc_set_sym_referenced (tmp->n.sym);
   13448          482 :   gfc_commit_symbol (tmp->n.sym);
   13449          482 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13450              : 
   13451              :   /* Should the lhs be a section, use its array ref for the
   13452              :      temporary expression.  */
   13453          482 :   if (aref && aref->type != AR_FULL && !allocatable)
   13454              :     {
   13455            6 :       gfc_free_ref_list (e->ref);
   13456            6 :       e->ref = gfc_copy_ref (ref);
   13457              :     }
   13458          482 :   return e;
   13459              : }
   13460              : 
   13461              : 
   13462              : /* Helper function to take an argument in a subroutine call with a dependency
   13463              :    on another argument, copy it to an allocatable temporary and use the
   13464              :    temporary in the call expression. The new code is embedded in a block to
   13465              :    ensure local, automatic deallocation.  */
   13466              : 
   13467              : static void
   13468           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13469              :                              gfc_expr **rhsptr)
   13470              : {
   13471           36 :   gfc_namespace *block_ns;
   13472           36 :   gfc_expr *tmp_var;
   13473              : 
   13474              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13475           36 :   block_ns = gfc_build_block_ns (ns);
   13476              : 
   13477              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13478              :      the assignment would be converted to a call and, in any case, the modified
   13479              :      call fails in gfc_check_conformance.  */
   13480           36 :   block_ns->resolved = 1;
   13481              : 
   13482              :   /* Assign the original expression to the temporary.  */
   13483           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13484           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13485           36 :                                      NULL, NULL, (*rhsptr)->where);
   13486              : 
   13487              :   /* Transfer the call to the block and terminate block code.  */
   13488           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13489           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13490           36 :   *(block_ns->code->next) = *code;
   13491           36 :   block_ns->code->next->next = NULL;
   13492              : 
   13493              :   /* Convert the original code to execute the block.  */
   13494           36 :   code->op = EXEC_BLOCK;
   13495           36 :   code->ext.block.ns = block_ns;
   13496           36 :   code->ext.block.assoc = NULL;
   13497           36 :   code->expr1 = code->expr2 = NULL;
   13498           36 : }
   13499              : 
   13500              : 
   13501              : /* Add one line of code to the code chain, making sure that 'head' and
   13502              :    'tail' are appropriately updated.  */
   13503              : 
   13504              : static void
   13505          656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13506              : {
   13507          656 :   gcc_assert (this_code);
   13508          656 :   if (*head == NULL)
   13509          308 :     *head = *tail = *this_code;
   13510              :   else
   13511          348 :     *tail = gfc_append_code (*tail, *this_code);
   13512          656 :   *this_code = NULL;
   13513          656 : }
   13514              : 
   13515              : 
   13516              : /* Generate a final call from a variable expression  */
   13517              : 
   13518              : static void
   13519           81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13520              : {
   13521           81 :   gfc_code *this_code;
   13522           81 :   gfc_expr *final_expr = NULL;
   13523           81 :   gfc_expr *size_expr;
   13524           81 :   gfc_expr *fini_coarray;
   13525              : 
   13526           81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13527           81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13528           75 :     return;
   13529              : 
   13530              :   /* Now generate the finalizer call.  */
   13531            6 :   this_code = gfc_get_code (EXEC_CALL);
   13532            6 :   this_code->symtree = final_expr->symtree;
   13533            6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13534              : 
   13535              :   //* Expression to be finalized  */
   13536            6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13537            6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13538              : 
   13539              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13540            6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13541            6 :   size_expr = gfc_get_expr ();
   13542            6 :   size_expr->where = gfc_current_locus;
   13543            6 :   size_expr->expr_type = EXPR_OP;
   13544            6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13545            6 :   size_expr->value.op.op1
   13546           12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13547              :                                     "storage_size", gfc_current_locus, 2,
   13548            6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13549              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13550              :                                                       NULL, 0));
   13551            6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   13552              :                                               gfc_character_storage_size);
   13553            6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   13554            6 :   size_expr->ts = size_expr->value.op.op1->ts;
   13555            6 :   this_code->ext.actual->next->expr = size_expr;
   13556              : 
   13557              :   /* fini_coarray  */
   13558            6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   13559            6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   13560              :                                         &tmp_expr->where);
   13561            6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   13562            6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   13563              : 
   13564            6 :   add_code_to_chain (&this_code, head, tail);
   13565              : 
   13566              : }
   13567              : 
   13568              : /* Counts the potential number of part array references that would
   13569              :    result from resolution of typebound defined assignments.  */
   13570              : 
   13571              : 
   13572              : static int
   13573          243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   13574              : {
   13575          243 :   gfc_component *c;
   13576          243 :   int c_depth = 0, t_depth;
   13577              : 
   13578          584 :   for (c= derived->components; c; c = c->next)
   13579              :     {
   13580          341 :       if ((!gfc_bt_struct (c->ts.type)
   13581          261 :             || c->attr.pointer
   13582          261 :             || c->attr.allocatable
   13583          260 :             || c->attr.proc_pointer_comp
   13584          260 :             || c->attr.class_pointer
   13585          260 :             || c->attr.proc_pointer)
   13586           81 :           && !c->attr.defined_assign_comp)
   13587           81 :         continue;
   13588              : 
   13589          260 :       if (c->as && c_depth == 0)
   13590          260 :         c_depth = 1;
   13591              : 
   13592          260 :       if (c->ts.u.derived->attr.defined_assign_comp)
   13593          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   13594              :                                               c->as ? 1 : 0);
   13595              :       else
   13596              :         t_depth = 0;
   13597              : 
   13598          260 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   13599              :     }
   13600          243 :   return depth + c_depth;
   13601              : }
   13602              : 
   13603              : 
   13604              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   13605              :    "An intrinsic assignment where the variable is of derived type is performed
   13606              :     as if each component of the variable were assigned from the corresponding
   13607              :     component of expr using pointer assignment (10.2.2) for each pointer
   13608              :     component, defined assignment for each nonpointer nonallocatable component
   13609              :     of a type that has a type-bound defined assignment consistent with the
   13610              :     component, intrinsic assignment for each other nonpointer nonallocatable
   13611              :     component, and intrinsic assignment for each allocated coarray component.
   13612              :     For unallocated coarray components, the corresponding component of the
   13613              :     variable shall be unallocated. For a noncoarray allocatable component the
   13614              :     following sequence of operations is applied.
   13615              :         (1) If the component of the variable is allocated, it is deallocated.
   13616              :         (2) If the component of the value of expr is allocated, the
   13617              :             corresponding component of the variable is allocated with the same
   13618              :             dynamic type and type parameters as the component of the value of
   13619              :             expr. If it is an array, it is allocated with the same bounds. The
   13620              :             value of the component of the value of expr is then assigned to the
   13621              :             corresponding component of the variable using defined assignment if
   13622              :             the declared type of the component has a type-bound defined
   13623              :             assignment consistent with the component, and intrinsic assignment
   13624              :             for the dynamic type of that component otherwise."
   13625              : 
   13626              :    The pointer assignments are taken care of by the intrinsic assignment of the
   13627              :    structure itself.  This function recursively adds defined assignments where
   13628              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   13629              : 
   13630              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   13631              :    and the component of 'var' is finalizable, we need a temporary for the
   13632              :    lhs.  In pseudo-code for an assignment var = expr:
   13633              : 
   13634              :    ! Confine finalization of temporaries, as far as possible.
   13635              :      Enclose the code for the assignment in a block
   13636              :    ! Only call function 'expr' once.
   13637              :       #if ('expr is not a constant or an variable)
   13638              :         temp_expr = expr
   13639              :         expr = temp_x
   13640              :    ! Do the intrinsic assignment
   13641              :       #if typeof ('var') has a typebound final subroutine
   13642              :         finalize (var)
   13643              :       var = expr
   13644              :    ! Now do the component assignments
   13645              :       #do over derived type components [%cmp]
   13646              :         #if (cmp is a pointer of any kind)
   13647              :           continue
   13648              :         build the assignment
   13649              :         resolve the code
   13650              :         #if the code is a typebound assignment
   13651              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   13652              :              t1 = var
   13653              :              arg1 = t1
   13654              :              deal with allocatation or not of var and this component
   13655              :         #elseif the code is an assignment by itself
   13656              :            #if this component does not need finalization
   13657              :              delete code and continue
   13658              :         #else
   13659              :            remove the leading assignment
   13660              :         #endif
   13661              :         commit the code
   13662              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   13663              :            var%cmp = t1%cmp
   13664              :       #enddo
   13665              :       put all code chunks involving t1 to the top of the generated code
   13666              :       insert the generated block in place of the original code
   13667              : */
   13668              : 
   13669              : static bool
   13670          381 : is_finalizable_type (gfc_typespec ts)
   13671              : {
   13672          381 :   gfc_component *c;
   13673              : 
   13674          381 :   if (ts.type != BT_DERIVED)
   13675              :     return false;
   13676              : 
   13677              :   /* (1) Check for FINAL subroutines.  */
   13678          381 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   13679              :     return true;
   13680              : 
   13681              :   /* (2) Check for components of finalizable type.  */
   13682          809 :   for (c = ts.u.derived->components; c; c = c->next)
   13683          470 :     if (c->ts.type == BT_DERIVED
   13684          243 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   13685          242 :         && c->ts.u.derived->f2k_derived
   13686          242 :         && c->ts.u.derived->f2k_derived->finalizers)
   13687              :       return true;
   13688              : 
   13689              :   return false;
   13690              : }
   13691              : 
   13692              : /* The temporary assignments have to be put on top of the additional
   13693              :    code to avoid the result being changed by the intrinsic assignment.
   13694              :    */
   13695              : static int component_assignment_level = 0;
   13696              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   13697              : static bool finalizable_comp;
   13698              : 
   13699              : static void
   13700          188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   13701              : {
   13702          188 :   gfc_component *comp1, *comp2;
   13703          188 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   13704          188 :   gfc_code *tmp_code = NULL;
   13705          188 :   gfc_expr *t1 = NULL;
   13706          188 :   gfc_expr *tmp_expr = NULL;
   13707          188 :   int error_count, depth;
   13708          188 :   bool finalizable_lhs;
   13709              : 
   13710          188 :   gfc_get_errors (NULL, &error_count);
   13711              : 
   13712              :   /* Filter out continuing processing after an error.  */
   13713          188 :   if (error_count
   13714          188 :       || (*code)->expr1->ts.type != BT_DERIVED
   13715          188 :       || (*code)->expr2->ts.type != BT_DERIVED)
   13716          140 :     return;
   13717              : 
   13718              :   /* TODO: Handle more than one part array reference in assignments.  */
   13719          188 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   13720          188 :                                       (*code)->expr1->rank ? 1 : 0);
   13721          188 :   if (depth > 1)
   13722              :     {
   13723            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   13724              :                    "done because multiple part array references would "
   13725              :                    "occur in intermediate expressions.", &(*code)->loc);
   13726            6 :       return;
   13727              :     }
   13728              : 
   13729          182 :   if (!component_assignment_level)
   13730          134 :     finalizable_comp = true;
   13731              : 
   13732              :   /* Build a block so that function result temporaries are finalized
   13733              :      locally on exiting the rather than enclosing scope.  */
   13734          182 :   if (!component_assignment_level)
   13735              :     {
   13736          134 :       ns = gfc_build_block_ns (ns);
   13737          134 :       tmp_code = gfc_get_code (EXEC_NOP);
   13738          134 :       *tmp_code = **code;
   13739          134 :       tmp_code->next = NULL;
   13740          134 :       (*code)->op = EXEC_BLOCK;
   13741          134 :       (*code)->ext.block.ns = ns;
   13742          134 :       (*code)->ext.block.assoc = NULL;
   13743          134 :       (*code)->expr1 = (*code)->expr2 = NULL;
   13744          134 :       ns->code = tmp_code;
   13745          134 :       code = &ns->code;
   13746              :     }
   13747              : 
   13748          182 :   component_assignment_level++;
   13749              : 
   13750          182 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   13751              : 
   13752              :   /* Create a temporary so that functions get called only once.  */
   13753          182 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   13754          182 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   13755              :     {
   13756              :       /* Assign the rhs to the temporary.  */
   13757           81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13758           81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   13759              :         {
   13760              :           /* Use allocate on assignment for the sake of simplicity. The
   13761              :              temporary must not take on the optional attribute. Assume
   13762              :              that the assignment is guarded by a PRESENT condition if the
   13763              :              lhs is optional.  */
   13764           25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   13765           25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   13766           25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   13767              :         }
   13768          162 :       this_code = build_assignment (EXEC_ASSIGN,
   13769              :                                     tmp_expr, (*code)->expr2,
   13770           81 :                                     NULL, NULL, (*code)->loc);
   13771           81 :       this_code->expr2->must_finalize = 1;
   13772              :       /* Add the code and substitute the rhs expression.  */
   13773           81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   13774           81 :       gfc_free_expr ((*code)->expr2);
   13775           81 :       (*code)->expr2 = tmp_expr;
   13776              :     }
   13777              : 
   13778              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   13779              :      of the temporaries generated here, since the intrinsic assignment
   13780              :      to the final result already does this.  */
   13781          182 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   13782              :     {
   13783          182 :       if (finalizable_lhs)
   13784           18 :         (*code)->expr1->must_finalize = 1;
   13785          182 :       this_code = build_assignment (EXEC_ASSIGN,
   13786              :                                     (*code)->expr1, (*code)->expr2,
   13787              :                                     NULL, NULL, (*code)->loc);
   13788          182 :       add_code_to_chain (&this_code, &head, &tail);
   13789              :     }
   13790              : 
   13791          182 :   comp1 = (*code)->expr1->ts.u.derived->components;
   13792          182 :   comp2 = (*code)->expr2->ts.u.derived->components;
   13793              : 
   13794          449 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   13795              :     {
   13796          267 :       bool inout = false;
   13797          267 :       bool finalizable_out = false;
   13798              : 
   13799              :       /* The intrinsic assignment does the right thing for pointers
   13800              :          of all kinds and allocatable components.  */
   13801          267 :       if (!gfc_bt_struct (comp1->ts.type)
   13802          200 :           || comp1->attr.pointer
   13803          200 :           || comp1->attr.allocatable
   13804          199 :           || comp1->attr.proc_pointer_comp
   13805          199 :           || comp1->attr.class_pointer
   13806          199 :           || comp1->attr.proc_pointer)
   13807           68 :         continue;
   13808              : 
   13809          398 :       finalizable_comp = is_finalizable_type (comp1->ts)
   13810          199 :                          && !finalizable_lhs;
   13811              : 
   13812              :       /* Make an assignment for this component.  */
   13813          398 :       this_code = build_assignment (EXEC_ASSIGN,
   13814              :                                     (*code)->expr1, (*code)->expr2,
   13815          199 :                                     comp1, comp2, (*code)->loc);
   13816              : 
   13817              :       /* Convert the assignment if there is a defined assignment for
   13818              :          this type.  Otherwise, using the call from gfc_resolve_code,
   13819              :          recurse into its components.  */
   13820          199 :       gfc_resolve_code (this_code, ns);
   13821              : 
   13822          199 :       if (this_code->op == EXEC_ASSIGN_CALL)
   13823              :         {
   13824          144 :           gfc_formal_arglist *dummy_args;
   13825          144 :           gfc_symbol *rsym;
   13826              :           /* Check that there is a typebound defined assignment.  If not,
   13827              :              then this must be a module defined assignment.  We cannot
   13828              :              use the defined_assign_comp attribute here because it must
   13829              :              be this derived type that has the defined assignment and not
   13830              :              a parent type.  */
   13831          144 :           if (!(comp1->ts.u.derived->f2k_derived
   13832              :                 && comp1->ts.u.derived->f2k_derived
   13833          144 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   13834              :             {
   13835            1 :               gfc_free_statements (this_code);
   13836            1 :               this_code = NULL;
   13837            1 :               continue;
   13838              :             }
   13839              : 
   13840              :           /* If the first argument of the subroutine has intent INOUT
   13841              :              a temporary must be generated and used instead.  */
   13842          143 :           rsym = this_code->resolved_sym;
   13843          143 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   13844          268 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   13845           18 :                             && dummy_args
   13846          161 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   13847          286 :           inout = dummy_args
   13848          268 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   13849           72 :           if ((inout || finalizable_out)
   13850           89 :               && !comp1->attr.allocatable)
   13851              :             {
   13852           89 :               gfc_code *temp_code;
   13853           89 :               inout = true;
   13854              : 
   13855              :               /* Build the temporary required for the assignment and put
   13856              :                  it at the head of the generated code.  */
   13857           89 :               if (!t1)
   13858              :                 {
   13859           89 :                   gfc_namespace *tmp_ns = ns;
   13860           89 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   13861           18 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   13862           89 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   13863           89 :                   t1->symtree->n.sym->attr.artificial = 1;
   13864          178 :                   temp_code = build_assignment (EXEC_ASSIGN,
   13865              :                                                 t1, (*code)->expr1,
   13866           89 :                                 NULL, NULL, (*code)->loc);
   13867              : 
   13868              :                   /* For allocatable LHS, check whether it is allocated.  Note
   13869              :                      that allocatable components with defined assignment are
   13870              :                      not yet support.  See PR 57696.  */
   13871           89 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   13872              :                     {
   13873           24 :                       gfc_code *block;
   13874           24 :                       gfc_expr *e =
   13875           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13876           24 :                       block = gfc_get_code (EXEC_IF);
   13877           24 :                       block->block = gfc_get_code (EXEC_IF);
   13878           24 :                       block->block->expr1
   13879           48 :                           = gfc_build_intrinsic_call (ns,
   13880              :                                     GFC_ISYM_ALLOCATED, "allocated",
   13881           24 :                                     (*code)->loc, 1, e);
   13882           24 :                       block->block->next = temp_code;
   13883           24 :                       temp_code = block;
   13884              :                     }
   13885           89 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   13886              :                 }
   13887              : 
   13888              :               /* Replace the first actual arg with the component of the
   13889              :                  temporary.  */
   13890           89 :               gfc_free_expr (this_code->ext.actual->expr);
   13891           89 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   13892           89 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   13893              : 
   13894              :               /* If the LHS variable is allocatable and wasn't allocated and
   13895              :                  the temporary is allocatable, pointer assign the address of
   13896              :                  the freshly allocated LHS to the temporary.  */
   13897           89 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   13898           89 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   13899              :                 {
   13900           18 :                   gfc_code *block;
   13901           18 :                   gfc_expr *cond;
   13902              : 
   13903           18 :                   cond = gfc_get_expr ();
   13904           18 :                   cond->ts.type = BT_LOGICAL;
   13905           18 :                   cond->ts.kind = gfc_default_logical_kind;
   13906           18 :                   cond->expr_type = EXPR_OP;
   13907           18 :                   cond->where = (*code)->loc;
   13908           18 :                   cond->value.op.op = INTRINSIC_NOT;
   13909           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   13910              :                                           GFC_ISYM_ALLOCATED, "allocated",
   13911           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   13912           18 :                   block = gfc_get_code (EXEC_IF);
   13913           18 :                   block->block = gfc_get_code (EXEC_IF);
   13914           18 :                   block->block->expr1 = cond;
   13915           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13916              :                                         t1, (*code)->expr1,
   13917           18 :                                         NULL, NULL, (*code)->loc);
   13918           18 :                   add_code_to_chain (&block, &head, &tail);
   13919              :                 }
   13920              :             }
   13921              :         }
   13922           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   13923              :         {
   13924              :           /* Don't add intrinsic assignments since they are already
   13925              :              effected by the intrinsic assignment of the structure, unless
   13926              :              finalization is required.  */
   13927            7 :           if (finalizable_comp)
   13928            0 :             this_code->expr1->must_finalize = 1;
   13929              :           else
   13930              :             {
   13931            7 :               gfc_free_statements (this_code);
   13932            7 :               this_code = NULL;
   13933            7 :               continue;
   13934              :             }
   13935              :         }
   13936              :       else
   13937              :         {
   13938              :           /* Resolution has expanded an assignment of a derived type with
   13939              :              defined assigned components.  Remove the redundant, leading
   13940              :              assignment.  */
   13941           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   13942           48 :           gfc_code *tmp = this_code;
   13943           48 :           this_code = this_code->next;
   13944           48 :           tmp->next = NULL;
   13945           48 :           gfc_free_statements (tmp);
   13946              :         }
   13947              : 
   13948          191 :       add_code_to_chain (&this_code, &head, &tail);
   13949              : 
   13950          191 :       if (t1 && (inout || finalizable_out))
   13951              :         {
   13952              :           /* Transfer the value to the final result.  */
   13953          178 :           this_code = build_assignment (EXEC_ASSIGN,
   13954              :                                         (*code)->expr1, t1,
   13955           89 :                                         comp1, comp2, (*code)->loc);
   13956           89 :           this_code->expr1->must_finalize = 0;
   13957           89 :           add_code_to_chain (&this_code, &head, &tail);
   13958              :         }
   13959              :     }
   13960              : 
   13961              :   /* Put the temporary assignments at the top of the generated code.  */
   13962          182 :   if (tmp_head && component_assignment_level == 1)
   13963              :     {
   13964          126 :       gfc_append_code (tmp_head, head);
   13965          126 :       head = tmp_head;
   13966          126 :       tmp_head = tmp_tail = NULL;
   13967              :     }
   13968              : 
   13969              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   13970              :      not accidentally deallocated. Hence, nullify t1.  */
   13971           89 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   13972          271 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   13973              :     {
   13974           18 :       gfc_code *block;
   13975           18 :       gfc_expr *cond;
   13976           18 :       gfc_expr *e;
   13977              : 
   13978           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13979           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   13980           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   13981           18 :       block = gfc_get_code (EXEC_IF);
   13982           18 :       block->block = gfc_get_code (EXEC_IF);
   13983           18 :       block->block->expr1 = cond;
   13984           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13985              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   13986           18 :                                         NULL, NULL, (*code)->loc);
   13987           18 :       gfc_append_code (tail, block);
   13988           18 :       tail = block;
   13989              :     }
   13990              : 
   13991          182 :   component_assignment_level--;
   13992              : 
   13993              :   /* Make an explicit final call for the function result.  */
   13994          182 :   if (tmp_expr)
   13995           81 :     generate_final_call (tmp_expr, &head, &tail);
   13996              : 
   13997          182 :   if (tmp_code)
   13998              :     {
   13999          134 :       ns->code = head;
   14000          134 :       return;
   14001              :     }
   14002              : 
   14003              :   /* Now attach the remaining code chain to the input code.  Step on
   14004              :      to the end of the new code since resolution is complete.  */
   14005           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14006           48 :   tail->next = (*code)->next;
   14007              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14008              :      before the temporary for the lhs is created.  */
   14009           48 :   gfc_free_expr ((*code)->expr1);
   14010           48 :   gfc_free_expr ((*code)->expr2);
   14011           48 :   **code = *head;
   14012           48 :   if (head != tail)
   14013           48 :     free (head);
   14014           48 :   *code = tail;
   14015              : }
   14016              : 
   14017              : 
   14018              : /* F2008: Pointer function assignments are of the form:
   14019              :         ptr_fcn (args) = expr
   14020              :    This function breaks these assignments into two statements:
   14021              :         temporary_pointer => ptr_fcn(args)
   14022              :         temporary_pointer = expr  */
   14023              : 
   14024              : static bool
   14025       285376 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14026              : {
   14027       285376 :   gfc_expr *tmp_ptr_expr;
   14028       285376 :   gfc_code *this_code;
   14029       285376 :   gfc_component *comp;
   14030       285376 :   gfc_symbol *s;
   14031              : 
   14032       285376 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14033              :     return false;
   14034              : 
   14035              :   /* Even if standard does not support this feature, continue to build
   14036              :      the two statements to avoid upsetting frontend_passes.c.  */
   14037          205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14038              :                   "%L", &(*code)->loc);
   14039              : 
   14040          205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14041              : 
   14042          205 :   if (comp)
   14043            6 :     s = comp->ts.interface;
   14044              :   else
   14045          199 :     s = (*code)->expr1->symtree->n.sym;
   14046              : 
   14047          205 :   if (s == NULL || !s->result->attr.pointer)
   14048              :     {
   14049            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14050              :                  "%L must have the pointer attribute.",
   14051            5 :                  &(*code)->expr1->where);
   14052            5 :       (*code)->op = EXEC_NOP;
   14053            5 :       return false;
   14054              :     }
   14055              : 
   14056          200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14057              : 
   14058              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14059              :      end, where array bounds are not known, arrays are made allocatable.
   14060              :      Change the temporary to a pointer here.  */
   14061          200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14062          200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14063          200 :   tmp_ptr_expr->where = (*code)->loc;
   14064              : 
   14065              :   /* A new charlen is required to ensure that the variable string length
   14066              :      is different to that of the original lhs for deferred results.  */
   14067          200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14068              :     {
   14069           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14070           60 :       tmp_ptr_expr->ts.deferred = 1;
   14071           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14072           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14073           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14074              :     }
   14075              : 
   14076          400 :   this_code = build_assignment (EXEC_ASSIGN,
   14077              :                                 tmp_ptr_expr, (*code)->expr2,
   14078          200 :                                 NULL, NULL, (*code)->loc);
   14079          200 :   this_code->next = (*code)->next;
   14080          200 :   (*code)->next = this_code;
   14081          200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14082          200 :   (*code)->expr2 = (*code)->expr1;
   14083          200 :   (*code)->expr1 = tmp_ptr_expr;
   14084              : 
   14085          200 :   return true;
   14086              : }
   14087              : 
   14088              : 
   14089              : /* Deferred character length assignments from an operator expression
   14090              :    require a temporary because the character length of the lhs can
   14091              :    change in the course of the assignment.  */
   14092              : 
   14093              : static bool
   14094       284214 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14095              : {
   14096       284214 :   gfc_expr *tmp_expr;
   14097       284214 :   gfc_code *this_code;
   14098              : 
   14099       284214 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14100        27117 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14101          836 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14102          835 :          && (*code)->expr2->expr_type == EXPR_OP))
   14103              :     return false;
   14104              : 
   14105           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14106              :     return false;
   14107              : 
   14108           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14109              :     return false;
   14110              : 
   14111           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14112           22 :   tmp_expr->where = (*code)->loc;
   14113              : 
   14114              :   /* A new charlen is required to ensure that the variable string
   14115              :      length is different to that of the original lhs.  */
   14116           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14117           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14118           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14119           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14120              : 
   14121           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14122              : 
   14123           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14124           22 :                                 (*code)->expr1,
   14125              :                                 gfc_copy_expr (tmp_expr),
   14126              :                                 NULL, NULL, (*code)->loc);
   14127              : 
   14128           22 :   (*code)->expr1 = tmp_expr;
   14129              : 
   14130           22 :   this_code->next = (*code)->next;
   14131           22 :   (*code)->next = this_code;
   14132              : 
   14133           22 :   return true;
   14134              : }
   14135              : 
   14136              : 
   14137              : /* Given a block of code, recursively resolve everything pointed to by this
   14138              :    code block.  */
   14139              : 
   14140              : void
   14141       674743 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14142              : {
   14143       674743 :   int omp_workshare_save;
   14144       674743 :   int forall_save, do_concurrent_save;
   14145       674743 :   code_stack frame;
   14146       674743 :   bool t;
   14147              : 
   14148       674743 :   frame.prev = cs_base;
   14149       674743 :   frame.head = code;
   14150       674743 :   cs_base = &frame;
   14151              : 
   14152       674743 :   find_reachable_labels (code);
   14153              : 
   14154      1805500 :   for (; code; code = code->next)
   14155              :     {
   14156      1130758 :       frame.current = code;
   14157      1130758 :       forall_save = forall_flag;
   14158      1130758 :       do_concurrent_save = gfc_do_concurrent_flag;
   14159              : 
   14160      1130758 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14161              :         {
   14162         2202 :           if (code->op == EXEC_FORALL)
   14163         1992 :             forall_flag = 1;
   14164          210 :           else if (code->op == EXEC_DO_CONCURRENT)
   14165          210 :             gfc_do_concurrent_flag = 1;
   14166         2202 :           gfc_resolve_forall (code, ns, forall_save);
   14167         2202 :           if (code->op == EXEC_FORALL)
   14168         1992 :             forall_flag = 2;
   14169          210 :           else if (code->op == EXEC_DO_CONCURRENT)
   14170          210 :             gfc_do_concurrent_flag = 2;
   14171              :         }
   14172      1128556 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14173          138 :         for (gfc_omp_variant *variant
   14174              :                = code->ext.omp_variants;
   14175          448 :              variant; variant = variant->next)
   14176          310 :           gfc_resolve_code (variant->code, ns);
   14177      1128418 :       else if (code->block)
   14178              :         {
   14179       328115 :           omp_workshare_save = -1;
   14180       328115 :           switch (code->op)
   14181              :             {
   14182        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14183        10119 :             case EXEC_OACC_PARALLEL:
   14184        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14185        10119 :             case EXEC_OACC_KERNELS:
   14186        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14187        10119 :             case EXEC_OACC_SERIAL:
   14188        10119 :             case EXEC_OACC_DATA:
   14189        10119 :             case EXEC_OACC_HOST_DATA:
   14190        10119 :             case EXEC_OACC_LOOP:
   14191        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14192        10119 :               break;
   14193           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14194           54 :               omp_workshare_save = omp_workshare_flag;
   14195           54 :               omp_workshare_flag = 1;
   14196           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14197           54 :               break;
   14198         5977 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14199         5977 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14200         5977 :             case EXEC_OMP_MASKED_TASKLOOP:
   14201         5977 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14202         5977 :             case EXEC_OMP_MASTER_TASKLOOP:
   14203         5977 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14204         5977 :             case EXEC_OMP_PARALLEL:
   14205         5977 :             case EXEC_OMP_PARALLEL_DO:
   14206         5977 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14207         5977 :             case EXEC_OMP_PARALLEL_LOOP:
   14208         5977 :             case EXEC_OMP_PARALLEL_MASKED:
   14209         5977 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14210         5977 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14211         5977 :             case EXEC_OMP_PARALLEL_MASTER:
   14212         5977 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14213         5977 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14214         5977 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14215         5977 :             case EXEC_OMP_TARGET_PARALLEL:
   14216         5977 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14217         5977 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14218         5977 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14219         5977 :             case EXEC_OMP_TARGET_TEAMS:
   14220         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14221         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14222         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14223         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14224         5977 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14225         5977 :             case EXEC_OMP_TASK:
   14226         5977 :             case EXEC_OMP_TASKLOOP:
   14227         5977 :             case EXEC_OMP_TASKLOOP_SIMD:
   14228         5977 :             case EXEC_OMP_TEAMS:
   14229         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14230         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14231         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14232         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14233         5977 :             case EXEC_OMP_TEAMS_LOOP:
   14234         5977 :               omp_workshare_save = omp_workshare_flag;
   14235         5977 :               omp_workshare_flag = 0;
   14236         5977 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14237         5977 :               break;
   14238         3063 :             case EXEC_OMP_DISTRIBUTE:
   14239         3063 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14240         3063 :             case EXEC_OMP_DO:
   14241         3063 :             case EXEC_OMP_DO_SIMD:
   14242         3063 :             case EXEC_OMP_LOOP:
   14243         3063 :             case EXEC_OMP_SIMD:
   14244         3063 :             case EXEC_OMP_TARGET_SIMD:
   14245         3063 :             case EXEC_OMP_TILE:
   14246         3063 :             case EXEC_OMP_UNROLL:
   14247         3063 :               gfc_resolve_omp_do_blocks (code, ns);
   14248         3063 :               break;
   14249              :             case EXEC_SELECT_TYPE:
   14250              :             case EXEC_SELECT_RANK:
   14251              :               /* Blocks are handled in resolve_select_type/rank because we
   14252              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14253              :               break;
   14254              :             case EXEC_DO_CONCURRENT:
   14255              :               gfc_do_concurrent_flag = 1;
   14256              :               gfc_resolve_blocks (code->block, ns);
   14257              :               gfc_do_concurrent_flag = 2;
   14258              :               break;
   14259           39 :             case EXEC_OMP_WORKSHARE:
   14260           39 :               omp_workshare_save = omp_workshare_flag;
   14261           39 :               omp_workshare_flag = 1;
   14262              :               /* FALL THROUGH */
   14263       304875 :             default:
   14264       304875 :               gfc_resolve_blocks (code->block, ns);
   14265       304875 :               break;
   14266              :             }
   14267              : 
   14268       324088 :           if (omp_workshare_save != -1)
   14269         6070 :             omp_workshare_flag = omp_workshare_save;
   14270              :         }
   14271       800303 : start:
   14272      1130963 :       t = true;
   14273      1130963 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14274      1129564 :           t = gfc_resolve_expr (code->expr1);
   14275              : 
   14276      1130963 :       forall_flag = forall_save;
   14277      1130963 :       gfc_do_concurrent_flag = do_concurrent_save;
   14278              : 
   14279      1130963 :       if (!gfc_resolve_expr (code->expr2))
   14280          637 :         t = false;
   14281              : 
   14282      1130963 :       if (code->op == EXEC_ALLOCATE
   14283      1130963 :           && !gfc_resolve_expr (code->expr3))
   14284              :         t = false;
   14285              : 
   14286      1130963 :       switch (code->op)
   14287              :         {
   14288              :         case EXEC_NOP:
   14289              :         case EXEC_END_BLOCK:
   14290              :         case EXEC_END_NESTED_BLOCK:
   14291              :         case EXEC_CYCLE:
   14292              :         case EXEC_PAUSE:
   14293              :           break;
   14294              : 
   14295       216743 :         case EXEC_STOP:
   14296       216743 :         case EXEC_ERROR_STOP:
   14297       216743 :           if (code->expr2 != NULL
   14298           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14299           37 :                   || code->expr2->rank != 0))
   14300            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14301              :                        &code->expr2->where);
   14302              :           break;
   14303              : 
   14304              :         case EXEC_EXIT:
   14305              :         case EXEC_CONTINUE:
   14306              :         case EXEC_DT_END:
   14307              :         case EXEC_ASSIGN_CALL:
   14308              :           break;
   14309              : 
   14310           54 :         case EXEC_CRITICAL:
   14311           54 :           resolve_critical (code);
   14312           54 :           break;
   14313              : 
   14314         1307 :         case EXEC_SYNC_ALL:
   14315         1307 :         case EXEC_SYNC_IMAGES:
   14316         1307 :         case EXEC_SYNC_MEMORY:
   14317         1307 :           resolve_sync (code);
   14318         1307 :           break;
   14319              : 
   14320          197 :         case EXEC_LOCK:
   14321          197 :         case EXEC_UNLOCK:
   14322          197 :         case EXEC_EVENT_POST:
   14323          197 :         case EXEC_EVENT_WAIT:
   14324          197 :           resolve_lock_unlock_event (code);
   14325          197 :           break;
   14326              : 
   14327              :         case EXEC_FAIL_IMAGE:
   14328              :           break;
   14329              : 
   14330          130 :         case EXEC_FORM_TEAM:
   14331          130 :           resolve_form_team (code);
   14332          130 :           break;
   14333              : 
   14334           73 :         case EXEC_CHANGE_TEAM:
   14335           73 :           resolve_change_team (code);
   14336           73 :           break;
   14337              : 
   14338           71 :         case EXEC_END_TEAM:
   14339           71 :           resolve_end_team (code);
   14340           71 :           break;
   14341              : 
   14342           43 :         case EXEC_SYNC_TEAM:
   14343           43 :           resolve_sync_team (code);
   14344           43 :           break;
   14345              : 
   14346         1491 :         case EXEC_ENTRY:
   14347              :           /* Keep track of which entry we are up to.  */
   14348         1491 :           current_entry_id = code->ext.entry->id;
   14349         1491 :           break;
   14350              : 
   14351          453 :         case EXEC_WHERE:
   14352          453 :           resolve_where (code, NULL);
   14353          453 :           break;
   14354              : 
   14355         1250 :         case EXEC_GOTO:
   14356         1250 :           if (code->expr1 != NULL)
   14357              :             {
   14358           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14359           76 :                   || code->expr1->ts.type != BT_INTEGER
   14360           76 :                   || (code->expr1->ref
   14361            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14362           75 :                   || code->expr1->symtree == NULL
   14363           75 :                   || (code->expr1->symtree->n.sym
   14364           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14365           75 :                           == FL_PARAMETER)))
   14366            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14367              :                            "scalar INTEGER variable", &code->expr1->where);
   14368           74 :               else if (code->expr1->symtree->n.sym
   14369           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14370            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14371              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14372              :                            &code->expr1->where);
   14373              :             }
   14374              :           else
   14375         1172 :             resolve_branch (code->label1, code);
   14376              :           break;
   14377              : 
   14378         3224 :         case EXEC_RETURN:
   14379         3224 :           if (code->expr1 != NULL
   14380           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14381            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14382              :                        "INTEGER return specifier", &code->expr1->where);
   14383              :           break;
   14384              : 
   14385              :         case EXEC_INIT_ASSIGN:
   14386              :         case EXEC_END_PROCEDURE:
   14387              :           break;
   14388              : 
   14389       286551 :         case EXEC_ASSIGN:
   14390       286551 :           if (!t)
   14391              :             break;
   14392              : 
   14393       285876 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14394       285876 :               && gfc_is_coindexed (code->expr1))
   14395              :             {
   14396              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14397              :                  coindexed variable.  */
   14398          500 :               code->op = EXEC_CALL;
   14399          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14400              :                                 true);
   14401          500 :               code->resolved_sym = code->symtree->n.sym;
   14402          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14403          500 :               code->resolved_sym->attr.intrinsic = 1;
   14404          500 :               code->resolved_sym->attr.subroutine = 1;
   14405          500 :               code->resolved_isym
   14406          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14407          500 :               gfc_commit_symbol (code->resolved_sym);
   14408          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14409          500 :               code->ext.actual->expr = code->expr1;
   14410          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14411          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14412          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14413              :                 {
   14414              :                   /* Convert assignments of expr1[...] = expr2 into
   14415              :                         tvar = expr2
   14416              :                         expr1[...] = tvar
   14417              :                      when expr2 is not trivial.  */
   14418           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14419           54 :                   gfc_code next_code = *code;
   14420           54 :                   gfc_code *rhs_code
   14421          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14422           54 :                                         NULL, code->expr2->where);
   14423           54 :                   *code = *rhs_code;
   14424           54 :                   code->next = rhs_code;
   14425           54 :                   *rhs_code = next_code;
   14426              : 
   14427           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14428           54 :                   rhs_code->expr1 = NULL;
   14429           54 :                   rhs_code->expr2 = NULL;
   14430              :                 }
   14431              :               else
   14432              :                 {
   14433          446 :                   code->ext.actual->next->expr = code->expr2;
   14434              : 
   14435          446 :                   code->expr1 = NULL;
   14436          446 :                   code->expr2 = NULL;
   14437              :                 }
   14438              :               break;
   14439              :             }
   14440              : 
   14441       285376 :           if (code->expr1->ts.type == BT_CLASS)
   14442         1114 :             gfc_find_vtab (&code->expr2->ts);
   14443              : 
   14444              :           /* If this is a pointer function in an lvalue variable context,
   14445              :              the new code will have to be resolved afresh. This is also the
   14446              :              case with an error, where the code is transformed into NOP to
   14447              :              prevent ICEs downstream.  */
   14448       285376 :           if (resolve_ptr_fcn_assign (&code, ns)
   14449       285376 :               || code->op == EXEC_NOP)
   14450          205 :             goto start;
   14451              : 
   14452       285171 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14453       285171 :                                          _("assignment")))
   14454              :             break;
   14455              : 
   14456       285132 :           if (resolve_ordinary_assign (code, ns))
   14457              :             {
   14458          918 :               if (omp_workshare_flag)
   14459              :                 {
   14460            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14461            1 :                              "at %L", &code->loc);
   14462            1 :                   break;
   14463              :                 }
   14464          917 :               if (code->op == EXEC_COMPCALL)
   14465          449 :                 goto compcall;
   14466              :               else
   14467          468 :                 goto call;
   14468              :             }
   14469              : 
   14470              :           /* Check for dependencies in deferred character length array
   14471              :              assignments and generate a temporary, if necessary.  */
   14472       284214 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14473              :             break;
   14474              : 
   14475              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14476       284192 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14477         7162 :               && code->expr1->ts.u.derived
   14478         7162 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14479          188 :             generate_component_assignments (&code, ns);
   14480       284004 :           else if (code->op == EXEC_ASSIGN)
   14481              :             {
   14482       284004 :               if (gfc_may_be_finalized (code->expr1->ts))
   14483         1253 :                 code->expr1->must_finalize = 1;
   14484       284004 :               if (code->expr2->expr_type == EXPR_ARRAY
   14485       284004 :                   && gfc_may_be_finalized (code->expr2->ts))
   14486           49 :                 code->expr2->must_finalize = 1;
   14487              :             }
   14488              : 
   14489              :           break;
   14490              : 
   14491          126 :         case EXEC_LABEL_ASSIGN:
   14492          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   14493            0 :             gfc_error ("Label %d referenced at %L is never defined",
   14494              :                        code->label1->value, &code->label1->where);
   14495          126 :           if (t
   14496          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   14497          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   14498          126 :                   || code->expr1->symtree->n.sym->ts.kind
   14499          126 :                      != gfc_default_integer_kind
   14500          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   14501          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   14502            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   14503              :                        "default INTEGER variable", &code->expr1->where);
   14504              :           break;
   14505              : 
   14506        10429 :         case EXEC_POINTER_ASSIGN:
   14507        10429 :           {
   14508        10429 :             gfc_expr* e;
   14509              : 
   14510        10429 :             if (!t)
   14511              :               break;
   14512              : 
   14513              :             /* This is both a variable definition and pointer assignment
   14514              :                context, so check both of them.  For rank remapping, a final
   14515              :                array ref may be present on the LHS and fool gfc_expr_attr
   14516              :                used in gfc_check_vardef_context.  Remove it.  */
   14517        10424 :             e = remove_last_array_ref (code->expr1);
   14518        20848 :             t = gfc_check_vardef_context (e, true, false, false,
   14519        10424 :                                           _("pointer assignment"));
   14520        10424 :             if (t)
   14521        10395 :               t = gfc_check_vardef_context (e, false, false, false,
   14522        10395 :                                             _("pointer assignment"));
   14523        10424 :             gfc_free_expr (e);
   14524              : 
   14525      1141039 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   14526              : 
   14527        10282 :             if (!t)
   14528              :               break;
   14529              : 
   14530              :             /* Assigning a class object always is a regular assign.  */
   14531        10282 :             if (code->expr2->ts.type == BT_CLASS
   14532          581 :                 && code->expr1->ts.type == BT_CLASS
   14533          490 :                 && CLASS_DATA (code->expr2)
   14534          489 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   14535        10918 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   14536           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   14537           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   14538           43 :                         == FL_PROCEDURE))
   14539          339 :               code->op = EXEC_ASSIGN;
   14540              :             break;
   14541              :           }
   14542              : 
   14543           72 :         case EXEC_ARITHMETIC_IF:
   14544           72 :           {
   14545           72 :             gfc_expr *e = code->expr1;
   14546              : 
   14547           72 :             gfc_resolve_expr (e);
   14548           72 :             if (e->expr_type == EXPR_NULL)
   14549            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   14550              : 
   14551           72 :             if (t && (e->rank > 0
   14552           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   14553            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   14554              :                          "REAL or INTEGER expression", &e->where);
   14555              : 
   14556           72 :             resolve_branch (code->label1, code);
   14557           72 :             resolve_branch (code->label2, code);
   14558           72 :             resolve_branch (code->label3, code);
   14559              :           }
   14560           72 :           break;
   14561              : 
   14562       230159 :         case EXEC_IF:
   14563       230159 :           if (t && code->expr1 != NULL
   14564            0 :               && (code->expr1->ts.type != BT_LOGICAL
   14565            0 :                   || code->expr1->rank != 0))
   14566            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   14567              :                        &code->expr1->where);
   14568              :           break;
   14569              : 
   14570        79535 :         case EXEC_CALL:
   14571        79535 :         call:
   14572        79535 :           resolve_call (code);
   14573        79535 :           break;
   14574              : 
   14575         1724 :         case EXEC_COMPCALL:
   14576         1724 :         compcall:
   14577         1724 :           resolve_typebound_subroutine (code);
   14578         1724 :           break;
   14579              : 
   14580          124 :         case EXEC_CALL_PPC:
   14581          124 :           resolve_ppc_call (code);
   14582          124 :           break;
   14583              : 
   14584          687 :         case EXEC_SELECT:
   14585              :           /* Select is complicated. Also, a SELECT construct could be
   14586              :              a transformed computed GOTO.  */
   14587          687 :           resolve_select (code, false);
   14588          687 :           break;
   14589              : 
   14590         3029 :         case EXEC_SELECT_TYPE:
   14591         3029 :           resolve_select_type (code, ns);
   14592         3029 :           break;
   14593              : 
   14594         1024 :         case EXEC_SELECT_RANK:
   14595         1024 :           resolve_select_rank (code, ns);
   14596         1024 :           break;
   14597              : 
   14598         7940 :         case EXEC_BLOCK:
   14599         7940 :           resolve_block_construct (code);
   14600         7940 :           break;
   14601              : 
   14602        32764 :         case EXEC_DO:
   14603        32764 :           if (code->ext.iterator != NULL)
   14604              :             {
   14605        32764 :               gfc_iterator *iter = code->ext.iterator;
   14606        32764 :               if (gfc_resolve_iterator (iter, true, false))
   14607        32750 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   14608              :                                          true);
   14609              :             }
   14610              :           break;
   14611              : 
   14612          531 :         case EXEC_DO_WHILE:
   14613          531 :           if (code->expr1 == NULL)
   14614            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   14615              :                                 "DO WHILE");
   14616          531 :           if (t
   14617          531 :               && (code->expr1->rank != 0
   14618          531 :                   || code->expr1->ts.type != BT_LOGICAL))
   14619            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   14620              :                        "a scalar LOGICAL expression", &code->expr1->where);
   14621              :           break;
   14622              : 
   14623        14258 :         case EXEC_ALLOCATE:
   14624        14258 :           if (t)
   14625        14256 :             resolve_allocate_deallocate (code, "ALLOCATE");
   14626              : 
   14627              :           break;
   14628              : 
   14629         6068 :         case EXEC_DEALLOCATE:
   14630         6068 :           if (t)
   14631         6068 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   14632              : 
   14633              :           break;
   14634              : 
   14635         3901 :         case EXEC_OPEN:
   14636         3901 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   14637              :             break;
   14638              : 
   14639         3674 :           resolve_branch (code->ext.open->err, code);
   14640         3674 :           break;
   14641              : 
   14642         3089 :         case EXEC_CLOSE:
   14643         3089 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   14644              :             break;
   14645              : 
   14646         3055 :           resolve_branch (code->ext.close->err, code);
   14647         3055 :           break;
   14648              : 
   14649         2799 :         case EXEC_BACKSPACE:
   14650         2799 :         case EXEC_ENDFILE:
   14651         2799 :         case EXEC_REWIND:
   14652         2799 :         case EXEC_FLUSH:
   14653         2799 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   14654              :             break;
   14655              : 
   14656         2733 :           resolve_branch (code->ext.filepos->err, code);
   14657         2733 :           break;
   14658              : 
   14659          836 :         case EXEC_INQUIRE:
   14660          836 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14661              :               break;
   14662              : 
   14663          788 :           resolve_branch (code->ext.inquire->err, code);
   14664          788 :           break;
   14665              : 
   14666           92 :         case EXEC_IOLENGTH:
   14667           92 :           gcc_assert (code->ext.inquire != NULL);
   14668           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14669              :             break;
   14670              : 
   14671           90 :           resolve_branch (code->ext.inquire->err, code);
   14672           90 :           break;
   14673              : 
   14674           89 :         case EXEC_WAIT:
   14675           89 :           if (!gfc_resolve_wait (code->ext.wait))
   14676              :             break;
   14677              : 
   14678           74 :           resolve_branch (code->ext.wait->err, code);
   14679           74 :           resolve_branch (code->ext.wait->end, code);
   14680           74 :           resolve_branch (code->ext.wait->eor, code);
   14681           74 :           break;
   14682              : 
   14683        32388 :         case EXEC_READ:
   14684        32388 :         case EXEC_WRITE:
   14685        32388 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   14686              :             break;
   14687              : 
   14688        32080 :           resolve_branch (code->ext.dt->err, code);
   14689        32080 :           resolve_branch (code->ext.dt->end, code);
   14690        32080 :           resolve_branch (code->ext.dt->eor, code);
   14691        32080 :           break;
   14692              : 
   14693        46414 :         case EXEC_TRANSFER:
   14694        46414 :           resolve_transfer (code);
   14695        46414 :           break;
   14696              : 
   14697         2202 :         case EXEC_DO_CONCURRENT:
   14698         2202 :         case EXEC_FORALL:
   14699         2202 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   14700              : 
   14701         2202 :           if (code->expr1 != NULL
   14702          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   14703            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   14704              :                        "expression", &code->expr1->where);
   14705              : 
   14706         2202 :     if (code->op == EXEC_DO_CONCURRENT)
   14707          210 :       resolve_locality_spec (code, ns);
   14708              :           break;
   14709              : 
   14710        13168 :         case EXEC_OACC_PARALLEL_LOOP:
   14711        13168 :         case EXEC_OACC_PARALLEL:
   14712        13168 :         case EXEC_OACC_KERNELS_LOOP:
   14713        13168 :         case EXEC_OACC_KERNELS:
   14714        13168 :         case EXEC_OACC_SERIAL_LOOP:
   14715        13168 :         case EXEC_OACC_SERIAL:
   14716        13168 :         case EXEC_OACC_DATA:
   14717        13168 :         case EXEC_OACC_HOST_DATA:
   14718        13168 :         case EXEC_OACC_LOOP:
   14719        13168 :         case EXEC_OACC_UPDATE:
   14720        13168 :         case EXEC_OACC_WAIT:
   14721        13168 :         case EXEC_OACC_CACHE:
   14722        13168 :         case EXEC_OACC_ENTER_DATA:
   14723        13168 :         case EXEC_OACC_EXIT_DATA:
   14724        13168 :         case EXEC_OACC_ATOMIC:
   14725        13168 :         case EXEC_OACC_DECLARE:
   14726        13168 :           gfc_resolve_oacc_directive (code, ns);
   14727        13168 :           break;
   14728              : 
   14729        16895 :         case EXEC_OMP_ALLOCATE:
   14730        16895 :         case EXEC_OMP_ALLOCATORS:
   14731        16895 :         case EXEC_OMP_ASSUME:
   14732        16895 :         case EXEC_OMP_ATOMIC:
   14733        16895 :         case EXEC_OMP_BARRIER:
   14734        16895 :         case EXEC_OMP_CANCEL:
   14735        16895 :         case EXEC_OMP_CANCELLATION_POINT:
   14736        16895 :         case EXEC_OMP_CRITICAL:
   14737        16895 :         case EXEC_OMP_FLUSH:
   14738        16895 :         case EXEC_OMP_DEPOBJ:
   14739        16895 :         case EXEC_OMP_DISPATCH:
   14740        16895 :         case EXEC_OMP_DISTRIBUTE:
   14741        16895 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14742        16895 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14743        16895 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   14744        16895 :         case EXEC_OMP_DO:
   14745        16895 :         case EXEC_OMP_DO_SIMD:
   14746        16895 :         case EXEC_OMP_ERROR:
   14747        16895 :         case EXEC_OMP_INTEROP:
   14748        16895 :         case EXEC_OMP_LOOP:
   14749        16895 :         case EXEC_OMP_MASTER:
   14750        16895 :         case EXEC_OMP_MASTER_TASKLOOP:
   14751        16895 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14752        16895 :         case EXEC_OMP_MASKED:
   14753        16895 :         case EXEC_OMP_MASKED_TASKLOOP:
   14754        16895 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14755        16895 :         case EXEC_OMP_METADIRECTIVE:
   14756        16895 :         case EXEC_OMP_ORDERED:
   14757        16895 :         case EXEC_OMP_SCAN:
   14758        16895 :         case EXEC_OMP_SCOPE:
   14759        16895 :         case EXEC_OMP_SECTIONS:
   14760        16895 :         case EXEC_OMP_SIMD:
   14761        16895 :         case EXEC_OMP_SINGLE:
   14762        16895 :         case EXEC_OMP_TARGET:
   14763        16895 :         case EXEC_OMP_TARGET_DATA:
   14764        16895 :         case EXEC_OMP_TARGET_ENTER_DATA:
   14765        16895 :         case EXEC_OMP_TARGET_EXIT_DATA:
   14766        16895 :         case EXEC_OMP_TARGET_PARALLEL:
   14767        16895 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   14768        16895 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14769        16895 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14770        16895 :         case EXEC_OMP_TARGET_SIMD:
   14771        16895 :         case EXEC_OMP_TARGET_TEAMS:
   14772        16895 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14773        16895 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14774        16895 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14775        16895 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14776        16895 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   14777        16895 :         case EXEC_OMP_TARGET_UPDATE:
   14778        16895 :         case EXEC_OMP_TASK:
   14779        16895 :         case EXEC_OMP_TASKGROUP:
   14780        16895 :         case EXEC_OMP_TASKLOOP:
   14781        16895 :         case EXEC_OMP_TASKLOOP_SIMD:
   14782        16895 :         case EXEC_OMP_TASKWAIT:
   14783        16895 :         case EXEC_OMP_TASKYIELD:
   14784        16895 :         case EXEC_OMP_TEAMS:
   14785        16895 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   14786        16895 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14787        16895 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14788        16895 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14789        16895 :         case EXEC_OMP_TEAMS_LOOP:
   14790        16895 :         case EXEC_OMP_TILE:
   14791        16895 :         case EXEC_OMP_UNROLL:
   14792        16895 :         case EXEC_OMP_WORKSHARE:
   14793        16895 :           gfc_resolve_omp_directive (code, ns);
   14794        16895 :           break;
   14795              : 
   14796         3888 :         case EXEC_OMP_PARALLEL:
   14797         3888 :         case EXEC_OMP_PARALLEL_DO:
   14798         3888 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   14799         3888 :         case EXEC_OMP_PARALLEL_LOOP:
   14800         3888 :         case EXEC_OMP_PARALLEL_MASKED:
   14801         3888 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14802         3888 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14803         3888 :         case EXEC_OMP_PARALLEL_MASTER:
   14804         3888 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14805         3888 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14806         3888 :         case EXEC_OMP_PARALLEL_SECTIONS:
   14807         3888 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   14808         3888 :           omp_workshare_save = omp_workshare_flag;
   14809         3888 :           omp_workshare_flag = 0;
   14810         3888 :           gfc_resolve_omp_directive (code, ns);
   14811         3888 :           omp_workshare_flag = omp_workshare_save;
   14812         3888 :           break;
   14813              : 
   14814            0 :         default:
   14815            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   14816              :         }
   14817              :     }
   14818              : 
   14819       674742 :   cs_base = frame.prev;
   14820       674742 : }
   14821              : 
   14822              : 
   14823              : /* Resolve initial values and make sure they are compatible with
   14824              :    the variable.  */
   14825              : 
   14826              : static void
   14827      1845495 : resolve_values (gfc_symbol *sym)
   14828              : {
   14829      1845495 :   bool t;
   14830              : 
   14831      1845495 :   if (sym->value == NULL)
   14832              :     return;
   14833              : 
   14834       415517 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   14835           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   14836              :                  "Using parameter %qs declared at %L is deprecated",
   14837              :                  sym->name, &sym->declared_at);
   14838              : 
   14839       415517 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   14840        39735 :     t= resolve_structure_cons (sym->value, 1);
   14841              :   else
   14842       375782 :     t = gfc_resolve_expr (sym->value);
   14843              : 
   14844       415517 :   if (!t)
   14845              :     return;
   14846              : 
   14847       415515 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   14848              : }
   14849              : 
   14850              : 
   14851              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   14852              :    for them once, rather than for each variable declared of that type.  */
   14853              : 
   14854              : static void
   14855      1816248 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   14856              : {
   14857      1816248 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   14858        83139 :       && derived_sym->attr.is_bind_c == 1)
   14859        27022 :     verify_bind_c_derived_type (derived_sym);
   14860              : 
   14861      1816248 :   return;
   14862              : }
   14863              : 
   14864              : 
   14865              : /* Check the interfaces of DTIO procedures associated with derived
   14866              :    type 'sym'.  These procedures can either have typebound bindings or
   14867              :    can appear in DTIO generic interfaces.  */
   14868              : 
   14869              : static void
   14870      1846465 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   14871              : {
   14872      1846465 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   14873              :     return;
   14874              : 
   14875        92429 :   gfc_check_dtio_interfaces (sym);
   14876              : 
   14877        92429 :   return;
   14878              : }
   14879              : 
   14880              : /* Verify that any binding labels used in a given namespace do not collide
   14881              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   14882              :    for the same procedure are permitted.  Abstract interfaces and dummy
   14883              :    arguments are not checked.  */
   14884              : 
   14885              : static void
   14886      1846465 : gfc_verify_binding_labels (gfc_symbol *sym)
   14887              : {
   14888      1846465 :   gfc_gsymbol *gsym;
   14889      1846465 :   const char *module;
   14890              : 
   14891      1846465 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   14892        61881 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   14893        33931 :       || sym->attr.abstract || sym->attr.dummy)
   14894              :     return;
   14895              : 
   14896        33795 :   gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   14897              : 
   14898        33795 :   if (sym->module)
   14899              :     module = sym->module;
   14900        12085 :   else if (sym->ns && sym->ns->proc_name
   14901        12085 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   14902         4511 :     module = sym->ns->proc_name->name;
   14903         7574 :   else if (sym->ns && sym->ns->parent
   14904          358 :            && sym->ns && sym->ns->parent->proc_name
   14905          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   14906          272 :     module = sym->ns->parent->proc_name->name;
   14907              :   else
   14908              :     module = NULL;
   14909              : 
   14910        33795 :   if (!gsym
   14911        11459 :       || (!gsym->defined
   14912         8511 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   14913              :     {
   14914        22336 :       if (!gsym)
   14915        22336 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   14916        30847 :       gsym->where = sym->declared_at;
   14917        30847 :       gsym->sym_name = sym->name;
   14918        30847 :       gsym->binding_label = sym->binding_label;
   14919        30847 :       gsym->ns = sym->ns;
   14920        30847 :       gsym->mod_name = module;
   14921        30847 :       if (sym->attr.function)
   14922        19957 :         gsym->type = GSYM_FUNCTION;
   14923        10890 :       else if (sym->attr.subroutine)
   14924        10751 :         gsym->type = GSYM_SUBROUTINE;
   14925              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   14926        30847 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   14927        30847 :       return;
   14928              :     }
   14929              : 
   14930         2948 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   14931              :     {
   14932            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   14933              :                  "identifier as entity at %L", sym->name,
   14934              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14935              :       /* Clear the binding label to prevent checking multiple times.  */
   14936            1 :       sym->binding_label = NULL;
   14937            1 :       return;
   14938              :     }
   14939              : 
   14940         2947 :   if (sym->attr.flavor == FL_VARIABLE && module
   14941           37 :       && (strcmp (module, gsym->mod_name) != 0
   14942           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   14943              :     {
   14944              :       /* This can only happen if the variable is defined in a module - if it
   14945              :          isn't the same module, reject it.  */
   14946            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   14947              :                  "uses the same global identifier as entity at %L from module %qs",
   14948              :                  sym->name, module, sym->binding_label,
   14949              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   14950            3 :       sym->binding_label = NULL;
   14951            3 :       return;
   14952              :     }
   14953              : 
   14954         2944 :   if ((sym->attr.function || sym->attr.subroutine)
   14955         2908 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   14956         2906 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   14957         2521 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   14958         2091 :       && (module != gsym->mod_name
   14959         2087 :           || strcmp (gsym->sym_name, sym->name) != 0
   14960         2087 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   14961              :     {
   14962              :       /* Print an error if the procedure is defined multiple times; we have to
   14963              :          exclude references to the same procedure via module association or
   14964              :          multiple checks for the same procedure.  */
   14965            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   14966              :                  "global identifier as entity at %L", sym->name,
   14967              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14968            4 :       sym->binding_label = NULL;
   14969              :     }
   14970              : }
   14971              : 
   14972              : 
   14973              : /* Resolve an index expression.  */
   14974              : 
   14975              : static bool
   14976       265031 : resolve_index_expr (gfc_expr *e)
   14977              : {
   14978       265031 :   if (!gfc_resolve_expr (e))
   14979              :     return false;
   14980              : 
   14981       265021 :   if (!gfc_simplify_expr (e, 0))
   14982              :     return false;
   14983              : 
   14984       265019 :   if (!gfc_specification_expr (e))
   14985              :     return false;
   14986              : 
   14987              :   return true;
   14988              : }
   14989              : 
   14990              : 
   14991              : /* Resolve a charlen structure.  */
   14992              : 
   14993              : static bool
   14994       103400 : resolve_charlen (gfc_charlen *cl)
   14995              : {
   14996       103400 :   int k;
   14997       103400 :   bool saved_specification_expr;
   14998              : 
   14999       103400 :   if (cl->resolved)
   15000              :     return true;
   15001              : 
   15002        94919 :   cl->resolved = 1;
   15003        94919 :   saved_specification_expr = specification_expr;
   15004        94919 :   specification_expr = true;
   15005              : 
   15006        94919 :   if (cl->length_from_typespec)
   15007              :     {
   15008         2113 :       if (!gfc_resolve_expr (cl->length))
   15009              :         {
   15010            1 :           specification_expr = saved_specification_expr;
   15011            1 :           return false;
   15012              :         }
   15013              : 
   15014         2112 :       if (!gfc_simplify_expr (cl->length, 0))
   15015              :         {
   15016            0 :           specification_expr = saved_specification_expr;
   15017            0 :           return false;
   15018              :         }
   15019              : 
   15020              :       /* cl->length has been resolved.  It should have an integer type.  */
   15021         2112 :       if (cl->length
   15022         2111 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15023              :         {
   15024            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15025              :                      &cl->length->where);
   15026            4 :           return false;
   15027              :         }
   15028              :     }
   15029              :   else
   15030              :     {
   15031        92806 :       if (!resolve_index_expr (cl->length))
   15032              :         {
   15033           19 :           specification_expr = saved_specification_expr;
   15034           19 :           return false;
   15035              :         }
   15036              :     }
   15037              : 
   15038              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15039              :      a negative value, the length of character entities declared is zero.  */
   15040        94895 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15041        56506 :       && mpz_sgn (cl->length->value.integer) < 0)
   15042            0 :     gfc_replace_expr (cl->length,
   15043              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15044              : 
   15045              :   /* Check that the character length is not too large.  */
   15046        94895 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15047        94895 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15048        56506 :       && cl->length->ts.type == BT_INTEGER
   15049        56506 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15050              :     {
   15051            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15052            4 :       specification_expr = saved_specification_expr;
   15053            4 :       return false;
   15054              :     }
   15055              : 
   15056        94891 :   specification_expr = saved_specification_expr;
   15057        94891 :   return true;
   15058              : }
   15059              : 
   15060              : 
   15061              : /* Test for non-constant shape arrays.  */
   15062              : 
   15063              : static bool
   15064       117500 : is_non_constant_shape_array (gfc_symbol *sym)
   15065              : {
   15066       117500 :   gfc_expr *e;
   15067       117500 :   int i;
   15068       117500 :   bool not_constant;
   15069              : 
   15070       117500 :   not_constant = false;
   15071       117500 :   if (sym->as != NULL)
   15072              :     {
   15073              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15074              :          has not been simplified; parameter array references.  Do the
   15075              :          simplification now.  */
   15076       154997 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15077              :         {
   15078        89514 :           if (i == GFC_MAX_DIMENSIONS)
   15079              :             break;
   15080              : 
   15081        89512 :           e = sym->as->lower[i];
   15082        89512 :           if (e && (!resolve_index_expr(e)
   15083        86695 :                     || !gfc_is_constant_expr (e)))
   15084              :             not_constant = true;
   15085        89512 :           e = sym->as->upper[i];
   15086        89512 :           if (e && (!resolve_index_expr(e)
   15087        85502 :                     || !gfc_is_constant_expr (e)))
   15088              :             not_constant = true;
   15089              :         }
   15090              :     }
   15091       117500 :   return not_constant;
   15092              : }
   15093              : 
   15094              : /* Given a symbol and an initialization expression, add code to initialize
   15095              :    the symbol to the function entry.  */
   15096              : static void
   15097         2093 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15098              : {
   15099         2093 :   gfc_expr *lval;
   15100         2093 :   gfc_code *init_st;
   15101         2093 :   gfc_namespace *ns = sym->ns;
   15102              : 
   15103         2093 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15104              :     {
   15105           46 :       gfc_free_expr (init);
   15106           46 :       return;
   15107              :     }
   15108              : 
   15109              :   /* Search for the function namespace if this is a contained
   15110              :      function without an explicit result.  */
   15111         2047 :   if (sym->attr.function && sym == sym->result
   15112          299 :       && sym->name != sym->ns->proc_name->name)
   15113              :     {
   15114          298 :       ns = ns->contained;
   15115         1376 :       for (;ns; ns = ns->sibling)
   15116         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15117              :           break;
   15118              :     }
   15119              : 
   15120         2047 :   if (ns == NULL)
   15121              :     {
   15122           61 :       gfc_free_expr (init);
   15123           61 :       return;
   15124              :     }
   15125              : 
   15126              :   /* Build an l-value expression for the result.  */
   15127         1986 :   lval = gfc_lval_expr_from_sym (sym);
   15128              : 
   15129              :   /* Add the code at scope entry.  */
   15130         1986 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15131         1986 :   init_st->next = ns->code;
   15132         1986 :   ns->code = init_st;
   15133              : 
   15134              :   /* Assign the default initializer to the l-value.  */
   15135         1986 :   init_st->loc = sym->declared_at;
   15136         1986 :   init_st->expr1 = lval;
   15137         1986 :   init_st->expr2 = init;
   15138              : }
   15139              : 
   15140              : 
   15141              : /* Whether or not we can generate a default initializer for a symbol.  */
   15142              : 
   15143              : static bool
   15144        30043 : can_generate_init (gfc_symbol *sym)
   15145              : {
   15146        30043 :   symbol_attribute *a;
   15147        30043 :   if (!sym)
   15148              :     return false;
   15149        30043 :   a = &sym->attr;
   15150              : 
   15151              :   /* These symbols should never have a default initialization.  */
   15152        49397 :   return !(
   15153        30043 :        a->allocatable
   15154        30043 :     || a->external
   15155        28884 :     || a->pointer
   15156        28884 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15157         5704 :         && (CLASS_DATA (sym)->attr.class_pointer
   15158         3749 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15159        26929 :     || a->in_equivalence
   15160        26808 :     || a->in_common
   15161        26761 :     || a->data
   15162        26583 :     || sym->module
   15163        22758 :     || a->cray_pointee
   15164        22696 :     || a->cray_pointer
   15165        22696 :     || sym->assoc
   15166        20024 :     || (!a->referenced && !a->result)
   15167        19354 :     || (a->dummy && (a->intent != INTENT_OUT
   15168         1081 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15169        19354 :     || (a->function && sym != sym->result)
   15170              :   );
   15171              : }
   15172              : 
   15173              : 
   15174              : /* Assign the default initializer to a derived type variable or result.  */
   15175              : 
   15176              : static void
   15177        11486 : apply_default_init (gfc_symbol *sym)
   15178              : {
   15179        11486 :   gfc_expr *init = NULL;
   15180              : 
   15181        11486 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15182              :     return;
   15183              : 
   15184        11241 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15185        10388 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15186              : 
   15187        11241 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15188              :     return;
   15189              : 
   15190         1711 :   build_init_assign (sym, init);
   15191         1711 :   sym->attr.referenced = 1;
   15192              : }
   15193              : 
   15194              : 
   15195              : /* Build an initializer for a local. Returns null if the symbol should not have
   15196              :    a default initialization.  */
   15197              : 
   15198              : static gfc_expr *
   15199       204160 : build_default_init_expr (gfc_symbol *sym)
   15200              : {
   15201              :   /* These symbols should never have a default initialization.  */
   15202       204160 :   if (sym->attr.allocatable
   15203       190452 :       || sym->attr.external
   15204       190452 :       || sym->attr.dummy
   15205       125139 :       || sym->attr.pointer
   15206       117028 :       || sym->attr.in_equivalence
   15207       114652 :       || sym->attr.in_common
   15208       111551 :       || sym->attr.data
   15209       109253 :       || sym->module
   15210       106724 :       || sym->attr.cray_pointee
   15211       106423 :       || sym->attr.cray_pointer
   15212       106121 :       || sym->assoc)
   15213              :     return NULL;
   15214              : 
   15215              :   /* Get the appropriate init expression.  */
   15216       101395 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15217              : }
   15218              : 
   15219              : /* Add an initialization expression to a local variable.  */
   15220              : static void
   15221       204160 : apply_default_init_local (gfc_symbol *sym)
   15222              : {
   15223       204160 :   gfc_expr *init = NULL;
   15224              : 
   15225              :   /* The symbol should be a variable or a function return value.  */
   15226       204160 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15227       204160 :       || (sym->attr.function && sym->result != sym))
   15228              :     return;
   15229              : 
   15230              :   /* Try to build the initializer expression.  If we can't initialize
   15231              :      this symbol, then init will be NULL.  */
   15232       204160 :   init = build_default_init_expr (sym);
   15233       204160 :   if (init == NULL)
   15234              :     return;
   15235              : 
   15236              :   /* For saved variables, we don't want to add an initializer at function
   15237              :      entry, so we just add a static initializer. Note that automatic variables
   15238              :      are stack allocated even with -fno-automatic; we have also to exclude
   15239              :      result variable, which are also nonstatic.  */
   15240          419 :   if (!sym->attr.automatic
   15241          419 :       && (sym->attr.save || sym->ns->save_all
   15242          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15243           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15244           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15245              :     {
   15246              :       /* Don't clobber an existing initializer!  */
   15247           37 :       gcc_assert (sym->value == NULL);
   15248           37 :       sym->value = init;
   15249           37 :       return;
   15250              :     }
   15251              : 
   15252          382 :   build_init_assign (sym, init);
   15253              : }
   15254              : 
   15255              : 
   15256              : /* Resolution of common features of flavors variable and procedure.  */
   15257              : 
   15258              : static bool
   15259       964820 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15260              : {
   15261       964820 :   gfc_array_spec *as;
   15262              : 
   15263       964820 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15264        19337 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15265        19331 :     as = CLASS_DATA (sym)->as;
   15266              :   else
   15267       945489 :     as = sym->as;
   15268              : 
   15269              :   /* Constraints on deferred shape variable.  */
   15270       964820 :   if (as == NULL || as->type != AS_DEFERRED)
   15271              :     {
   15272       940519 :       bool pointer, allocatable, dimension;
   15273              : 
   15274       940519 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15275        16124 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15276              :         {
   15277        16118 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15278        16118 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15279        16118 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15280              :         }
   15281              :       else
   15282              :         {
   15283       924401 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   15284       924401 :           allocatable = sym->attr.allocatable;
   15285       924401 :           dimension = sym->attr.dimension;
   15286              :         }
   15287              : 
   15288       940519 :       if (allocatable)
   15289              :         {
   15290         8021 :           if (dimension
   15291         8021 :               && as
   15292          524 :               && as->type != AS_ASSUMED_RANK
   15293            5 :               && !sym->attr.select_rank_temporary)
   15294              :             {
   15295            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   15296              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   15297            3 :               return false;
   15298              :             }
   15299         8018 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   15300              :                                     "%qs at %L may not be ALLOCATABLE",
   15301              :                                     sym->name, &sym->declared_at))
   15302              :             return false;
   15303              :         }
   15304              : 
   15305       940515 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   15306              :         {
   15307            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   15308              :                      "assumed rank", sym->name, &sym->declared_at);
   15309            4 :           sym->error = 1;
   15310            4 :           return false;
   15311              :         }
   15312              :     }
   15313              :   else
   15314              :     {
   15315        24301 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   15316         4678 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   15317              :         {
   15318            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   15319              :                      sym->name, &sym->declared_at);
   15320            3 :           return false;
   15321              :          }
   15322              :     }
   15323              : 
   15324              :   /* Constraints on polymorphic variables.  */
   15325       964809 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   15326              :     {
   15327              :       /* F03:C502.  */
   15328        18670 :       if (sym->attr.class_ok
   15329        18614 :           && sym->ts.u.derived
   15330        18609 :           && !sym->attr.select_type_temporary
   15331        17508 :           && !UNLIMITED_POLY (sym)
   15332        14998 :           && CLASS_DATA (sym)
   15333        14997 :           && CLASS_DATA (sym)->ts.u.derived
   15334        33666 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   15335              :         {
   15336            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   15337            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   15338              :                      &sym->declared_at);
   15339            5 :           return false;
   15340              :         }
   15341              : 
   15342              :       /* F03:C509.  */
   15343              :       /* Assume that use associated symbols were checked in the module ns.
   15344              :          Class-variables that are associate-names are also something special
   15345              :          and excepted from the test.  */
   15346        18665 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   15347           54 :           && !sym->attr.select_type_temporary
   15348           54 :           && !sym->attr.select_rank_temporary)
   15349              :         {
   15350           54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   15351              :                      "or pointer", sym->name, &sym->declared_at);
   15352           54 :           return false;
   15353              :         }
   15354              :     }
   15355              : 
   15356              :   return true;
   15357              : }
   15358              : 
   15359              : 
   15360              : /* Additional checks for symbols with flavor variable and derived
   15361              :    type.  To be called from resolve_fl_variable.  */
   15362              : 
   15363              : static bool
   15364        82038 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   15365              : {
   15366        82038 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   15367              : 
   15368              :   /* Check to see if a derived type is blocked from being host
   15369              :      associated by the presence of another class I symbol in the same
   15370              :      namespace.  14.6.1.3 of the standard and the discussion on
   15371              :      comp.lang.fortran.  */
   15372        82038 :   if (sym->ts.u.derived
   15373        82033 :       && sym->ns != sym->ts.u.derived->ns
   15374        47054 :       && !sym->ts.u.derived->attr.use_assoc
   15375        17462 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   15376              :     {
   15377        16492 :       gfc_symbol *s;
   15378        16492 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   15379        16492 :       if (s && s->attr.generic)
   15380            2 :         s = gfc_find_dt_in_generic (s);
   15381        16492 :       if (s && !gfc_fl_struct (s->attr.flavor))
   15382              :         {
   15383            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   15384              :                      "because it is blocked by an incompatible object "
   15385              :                      "of the same name declared at %L",
   15386            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   15387              :                      &s->declared_at);
   15388            2 :           return false;
   15389              :         }
   15390              :     }
   15391              : 
   15392              :   /* 4th constraint in section 11.3: "If an object of a type for which
   15393              :      component-initialization is specified (R429) appears in the
   15394              :      specification-part of a module and does not have the ALLOCATABLE
   15395              :      or POINTER attribute, the object shall have the SAVE attribute."
   15396              : 
   15397              :      The check for initializers is performed with
   15398              :      gfc_has_default_initializer because gfc_default_initializer generates
   15399              :      a hidden default for allocatable components.  */
   15400        81359 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   15401        18405 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   15402          413 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   15403           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   15404           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   15405        82045 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   15406              :                           "%qs at %L, needed due to the default "
   15407              :                           "initialization", sym->name, &sym->declared_at))
   15408              :     return false;
   15409              : 
   15410              :   /* Assign default initializer.  */
   15411        82034 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   15412        75799 :       && (!no_init_flag
   15413        59195 :           || (sym->attr.intent == INTENT_OUT
   15414         3225 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   15415        19655 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15416              : 
   15417              :   return true;
   15418              : }
   15419              : 
   15420              : 
   15421              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   15422              :    except in the declaration of an entity or component that has the POINTER
   15423              :    or ALLOCATABLE attribute.  */
   15424              : 
   15425              : static bool
   15426      1502384 : deferred_requirements (gfc_symbol *sym)
   15427              : {
   15428      1502384 :   if (sym->ts.deferred
   15429         7903 :       && !(sym->attr.pointer
   15430         2375 :            || sym->attr.allocatable
   15431           92 :            || sym->attr.associate_var
   15432            7 :            || sym->attr.omp_udr_artificial_var))
   15433              :     {
   15434              :       /* If a function has a result variable, only check the variable.  */
   15435            7 :       if (sym->result && sym->name != sym->result->name)
   15436              :         return true;
   15437              : 
   15438            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   15439              :                  "requires either the POINTER or ALLOCATABLE attribute",
   15440              :                  sym->name, &sym->declared_at);
   15441            6 :       return false;
   15442              :     }
   15443              :   return true;
   15444              : }
   15445              : 
   15446              : 
   15447              : /* Resolve symbols with flavor variable.  */
   15448              : 
   15449              : static bool
   15450       647025 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   15451              : {
   15452       647025 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   15453              :                                  "SAVE attribute");
   15454              : 
   15455       647025 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   15456              :     return false;
   15457              : 
   15458              :   /* Set this flag to check that variables are parameters of all entries.
   15459              :      This check is effected by the call to gfc_resolve_expr through
   15460              :      is_non_constant_shape_array.  */
   15461       646965 :   bool saved_specification_expr = specification_expr;
   15462       646965 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   15463       646965 :   specification_expr = true;
   15464       646965 :   specification_expr_symbol = sym;
   15465              : 
   15466       646965 :   if (sym->ns->proc_name
   15467       646870 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15468       641861 :           || sym->ns->proc_name->attr.is_main_program)
   15469        82456 :       && !sym->attr.use_assoc
   15470        79306 :       && !sym->attr.allocatable
   15471        73572 :       && !sym->attr.pointer
   15472       716903 :       && is_non_constant_shape_array (sym))
   15473              :     {
   15474              :       /* F08:C541. The shape of an array defined in a main program or module
   15475              :        * needs to be constant.  */
   15476            3 :       gfc_error ("The module or main program array %qs at %L must "
   15477              :                  "have constant shape", sym->name, &sym->declared_at);
   15478            3 :       specification_expr = saved_specification_expr;
   15479            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   15480            3 :       return false;
   15481              :     }
   15482              : 
   15483              :   /* Constraints on deferred type parameter.  */
   15484       646962 :   if (!deferred_requirements (sym))
   15485              :     return false;
   15486              : 
   15487       646958 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   15488              :     {
   15489              :       /* Make sure that character string variables with assumed length are
   15490              :          dummy arguments.  */
   15491        35871 :       gfc_expr *e = NULL;
   15492              : 
   15493        35871 :       if (sym->ts.u.cl)
   15494        35871 :         e = sym->ts.u.cl->length;
   15495              :       else
   15496              :         return false;
   15497              : 
   15498        35871 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   15499         2582 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   15500            2 :           && !sym->attr.omp_udr_artificial_var)
   15501              :         {
   15502            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   15503              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   15504            2 :           specification_expr = saved_specification_expr;
   15505            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15506            2 :           return false;
   15507              :         }
   15508              : 
   15509        20759 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   15510              :         {
   15511            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15512            1 :           specification_expr = saved_specification_expr;
   15513            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   15514            1 :           return false;
   15515              :         }
   15516              : 
   15517        35868 :       if (!gfc_is_constant_expr (e)
   15518        35868 :           && !(e->expr_type == EXPR_VARIABLE
   15519         1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   15520              :         {
   15521         2184 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   15522         1680 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15523         1679 :                   || sym->ns->proc_name->attr.is_main_program))
   15524              :             {
   15525            3 :               gfc_error ("%qs at %L must have constant character length "
   15526              :                         "in this context", sym->name, &sym->declared_at);
   15527            3 :               specification_expr = saved_specification_expr;
   15528            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   15529            3 :               return false;
   15530              :             }
   15531         2181 :           if (sym->attr.in_common)
   15532              :             {
   15533            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   15534              :                          "character length", sym->name, &sym->declared_at);
   15535            1 :               specification_expr = saved_specification_expr;
   15536            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   15537            1 :               return false;
   15538              :             }
   15539              :         }
   15540              :     }
   15541              : 
   15542       646951 :   if (sym->value == NULL && sym->attr.referenced
   15543       206075 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   15544       204160 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   15545              : 
   15546              :   /* Determine if the symbol may not have an initializer.  */
   15547       646951 :   int no_init_flag = 0, automatic_flag = 0;
   15548       646951 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   15549       170455 :       || sym->attr.intrinsic || sym->attr.result)
   15550              :     no_init_flag = 1;
   15551       138185 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   15552       172762 :            && is_non_constant_shape_array (sym))
   15553              :     {
   15554         1345 :       no_init_flag = automatic_flag = 1;
   15555              : 
   15556              :       /* Also, they must not have the SAVE attribute.
   15557              :          SAVE_IMPLICIT is checked below.  */
   15558         1345 :       if (sym->as && sym->attr.codimension)
   15559              :         {
   15560            7 :           int corank = sym->as->corank;
   15561            7 :           sym->as->corank = 0;
   15562            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   15563            7 :           sym->as->corank = corank;
   15564              :         }
   15565         1345 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   15566              :         {
   15567            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15568            2 :           specification_expr = saved_specification_expr;
   15569            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15570            2 :           return false;
   15571              :         }
   15572              :     }
   15573              : 
   15574              :   /* Ensure that any initializer is simplified.  */
   15575       646949 :   if (sym->value)
   15576         8095 :     gfc_simplify_expr (sym->value, 1);
   15577              : 
   15578              :   /* Reject illegal initializers.  */
   15579       646949 :   if (!sym->mark && sym->value)
   15580              :     {
   15581         8095 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   15582           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   15583            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   15584              :                    sym->name, &sym->declared_at);
   15585         8094 :       else if (sym->attr.external)
   15586            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   15587              :                    sym->name, &sym->declared_at);
   15588         8094 :       else if (sym->attr.dummy)
   15589            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   15590              :                    sym->name, &sym->declared_at);
   15591         8091 :       else if (sym->attr.intrinsic)
   15592            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   15593              :                    sym->name, &sym->declared_at);
   15594         8091 :       else if (sym->attr.result)
   15595            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   15596              :                    sym->name, &sym->declared_at);
   15597         8090 :       else if (automatic_flag)
   15598            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   15599              :                    sym->name, &sym->declared_at);
   15600              :       else
   15601         8085 :         goto no_init_error;
   15602           10 :       specification_expr = saved_specification_expr;
   15603           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   15604           10 :       return false;
   15605              :     }
   15606              : 
   15607       638854 : no_init_error:
   15608       646939 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   15609              :     {
   15610        82038 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   15611        82038 :       specification_expr = saved_specification_expr;
   15612        82038 :       specification_expr_symbol = saved_specification_expr_symbol;
   15613        82038 :       return res;
   15614              :     }
   15615              : 
   15616       564901 :   specification_expr = saved_specification_expr;
   15617       564901 :   specification_expr_symbol = saved_specification_expr_symbol;
   15618       564901 :   return true;
   15619              : }
   15620              : 
   15621              : 
   15622              : /* Compare the dummy characteristics of a module procedure interface
   15623              :    declaration with the corresponding declaration in a submodule.  */
   15624              : static gfc_formal_arglist *new_formal;
   15625              : static char errmsg[200];
   15626              : 
   15627              : static void
   15628         1324 : compare_fsyms (gfc_symbol *sym)
   15629              : {
   15630         1324 :   gfc_symbol *fsym;
   15631              : 
   15632         1324 :   if (sym == NULL || new_formal == NULL)
   15633              :     return;
   15634              : 
   15635         1324 :   fsym = new_formal->sym;
   15636              : 
   15637         1324 :   if (sym == fsym)
   15638              :     return;
   15639              : 
   15640         1300 :   if (strcmp (sym->name, fsym->name) == 0)
   15641              :     {
   15642          499 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   15643            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   15644              :     }
   15645              : }
   15646              : 
   15647              : 
   15648              : /* Resolve a procedure.  */
   15649              : 
   15650              : static bool
   15651       474085 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   15652              : {
   15653       474085 :   gfc_formal_arglist *arg;
   15654       474085 :   bool allocatable_or_pointer = false;
   15655              : 
   15656       474085 :   if (sym->attr.function
   15657       474085 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   15658              :     return false;
   15659              : 
   15660              :   /* Constraints on deferred type parameter.  */
   15661       474075 :   if (!deferred_requirements (sym))
   15662              :     return false;
   15663              : 
   15664       474074 :   if (sym->ts.type == BT_CHARACTER)
   15665              :     {
   15666        11697 :       gfc_charlen *cl = sym->ts.u.cl;
   15667              : 
   15668         7590 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   15669        12987 :              && !resolve_charlen (cl))
   15670              :         return false;
   15671              : 
   15672        11696 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   15673        10407 :           && sym->attr.proc == PROC_ST_FUNCTION)
   15674              :         {
   15675            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   15676              :                      "have constant length", sym->name, &sym->declared_at);
   15677            0 :           return false;
   15678              :         }
   15679              :     }
   15680              : 
   15681              :   /* Ensure that derived type for are not of a private type.  Internal
   15682              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   15683              :      externally accessible and can access all the objects accessible in
   15684              :      the host.  */
   15685       109191 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   15686       109191 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15687       558943 :       && gfc_check_symbol_access (sym))
   15688              :     {
   15689       442740 :       gfc_interface *iface;
   15690              : 
   15691       933529 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   15692              :         {
   15693       490790 :           if (arg->sym
   15694       490649 :               && arg->sym->ts.type == BT_DERIVED
   15695        42876 :               && arg->sym->ts.u.derived
   15696        42876 :               && !arg->sym->ts.u.derived->attr.use_assoc
   15697         4420 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15698       490799 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   15699              :                                   "and cannot be a dummy argument"
   15700              :                                   " of %qs, which is PUBLIC at %L",
   15701            9 :                                   arg->sym->name, sym->name,
   15702              :                                   &sym->declared_at))
   15703              :             {
   15704              :               /* Stop this message from recurring.  */
   15705            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   15706            1 :               return false;
   15707              :             }
   15708              :         }
   15709              : 
   15710              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   15711              :          PRIVATE to the containing module.  */
   15712       630021 :       for (iface = sym->generic; iface; iface = iface->next)
   15713              :         {
   15714       437070 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   15715              :             {
   15716       249788 :               if (arg->sym
   15717       249756 :                   && arg->sym->ts.type == BT_DERIVED
   15718         8018 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   15719          244 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15720       249792 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   15721              :                                       "PUBLIC interface %qs at %L "
   15722              :                                       "takes dummy arguments of %qs which "
   15723              :                                       "is PRIVATE", iface->sym->name,
   15724            4 :                                       sym->name, &iface->sym->declared_at,
   15725            4 :                                       gfc_typename(&arg->sym->ts)))
   15726              :                 {
   15727              :                   /* Stop this message from recurring.  */
   15728            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   15729            1 :                   return false;
   15730              :                 }
   15731              :              }
   15732              :         }
   15733              :     }
   15734              : 
   15735       474071 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   15736           79 :       && !sym->attr.proc_pointer)
   15737              :     {
   15738            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   15739              :                  sym->name, &sym->declared_at);
   15740              : 
   15741              :       /* Make sure no second error is issued for this.  */
   15742            2 :       sym->value->error = 1;
   15743            2 :       return false;
   15744              :     }
   15745              : 
   15746              :   /* An external symbol may not have an initializer because it is taken to be
   15747              :      a procedure. Exception: Procedure Pointers.  */
   15748       474069 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   15749              :     {
   15750            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   15751              :                  sym->name, &sym->declared_at);
   15752            0 :       return false;
   15753              :     }
   15754              : 
   15755              :   /* An elemental function is required to return a scalar 12.7.1  */
   15756       474069 :   if (sym->attr.elemental && sym->attr.function
   15757        86326 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15758            2 :                       && CLASS_DATA (sym)->as)))
   15759              :     {
   15760            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   15761              :                  "result", sym->name, &sym->declared_at);
   15762              :       /* Reset so that the error only occurs once.  */
   15763            3 :       sym->attr.elemental = 0;
   15764            3 :       return false;
   15765              :     }
   15766              : 
   15767       474066 :   if (sym->attr.proc == PROC_ST_FUNCTION
   15768          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   15769              :     {
   15770            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   15771              :                  "allocatable attribute", sym->name, &sym->declared_at);
   15772            2 :       return false;
   15773              :     }
   15774              : 
   15775              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   15776              :      char-len-param shall not be array-valued, pointer-valued, recursive
   15777              :      or pure.  ....snip... A character value of * may only be used in the
   15778              :      following ways: (i) Dummy arg of procedure - dummy associates with
   15779              :      actual length; (ii) To declare a named constant; or (iii) External
   15780              :      function - but length must be declared in calling scoping unit.  */
   15781       474064 :   if (sym->attr.function
   15782       317776 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   15783         6689 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   15784              :     {
   15785          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   15786          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   15787              :         {
   15788            4 :           if (sym->as && sym->as->rank)
   15789            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15790              :                        "array-valued", sym->name, &sym->declared_at);
   15791              : 
   15792            4 :           if (sym->attr.pointer)
   15793            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15794              :                        "pointer-valued", sym->name, &sym->declared_at);
   15795              : 
   15796            4 :           if (sym->attr.pure)
   15797            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15798              :                        "pure", sym->name, &sym->declared_at);
   15799              : 
   15800            4 :           if (sym->attr.recursive)
   15801            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15802              :                        "recursive", sym->name, &sym->declared_at);
   15803              : 
   15804            4 :           return false;
   15805              :         }
   15806              : 
   15807              :       /* Appendix B.2 of the standard.  Contained functions give an
   15808              :          error anyway.  Deferred character length is an F2003 feature.
   15809              :          Don't warn on intrinsic conversion functions, which start
   15810              :          with two underscores.  */
   15811          176 :       if (!sym->attr.contained && !sym->ts.deferred
   15812          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   15813          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   15814              :                         "CHARACTER(*) function %qs at %L",
   15815              :                         sym->name, &sym->declared_at);
   15816              :     }
   15817              : 
   15818              :   /* F2008, C1218.  */
   15819       474060 :   if (sym->attr.elemental)
   15820              :     {
   15821        89592 :       if (sym->attr.proc_pointer)
   15822              :         {
   15823            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   15824              :                                                : sym->name);
   15825            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   15826              :                      name, &sym->declared_at);
   15827            7 :           return false;
   15828              :         }
   15829        89585 :       if (sym->attr.dummy)
   15830              :         {
   15831            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   15832              :                      sym->name, &sym->declared_at);
   15833            3 :           return false;
   15834              :         }
   15835              :     }
   15836              : 
   15837              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   15838              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   15839              :      pointer is tested and caught elsewhere.  */
   15840       474050 :   if (sym->result)
   15841       266770 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   15842       266770 :                              && CLASS_DATA (sym->result) ?
   15843         1663 :                              (CLASS_DATA (sym->result)->attr.allocatable
   15844         1663 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   15845       265107 :                              (sym->result->attr.allocatable
   15846       265107 :                               || sym->result->attr.pointer);
   15847              : 
   15848       474050 :   if (sym->attr.elemental && sym->result
   15849        85951 :       && allocatable_or_pointer)
   15850              :     {
   15851            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   15852              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   15853              :                  "attribute", sym->result->name,
   15854              :                  &sym->result->declared_at, sym->name);
   15855            4 :       return false;
   15856              :     }
   15857              : 
   15858              :   /* F2018:C1585: "The function result of a pure function shall not be both
   15859              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   15860              :      component."  */
   15861       474046 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   15862              :     {
   15863         2459 :       if (sym->ts.type == BT_CLASS
   15864            5 :           && sym->attr.class_ok
   15865            4 :           && CLASS_DATA (sym->result)
   15866            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   15867              :         {
   15868            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   15869              :                      "polymorphic allocatable",
   15870              :                      sym->result->name, &sym->result->declared_at);
   15871            4 :           return false;
   15872              :         }
   15873              : 
   15874         2455 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   15875              :         {
   15876              :           gfc_component *c = sym->ts.u.derived->components;
   15877         4491 :           for (; c; c = c->next)
   15878         2345 :             if (c->ts.type == BT_CLASS
   15879            2 :                 && CLASS_DATA (c)
   15880            2 :                 && CLASS_DATA (c)->attr.allocatable)
   15881              :               {
   15882            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   15883              :                            "polymorphic allocatable component %qs",
   15884              :                            sym->result->name, &sym->result->declared_at,
   15885              :                            c->name);
   15886            2 :                 return false;
   15887              :               }
   15888              :         }
   15889              :     }
   15890              : 
   15891       474040 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   15892              :     {
   15893         6707 :       gfc_formal_arglist *curr_arg;
   15894         6707 :       int has_non_interop_arg = 0;
   15895              : 
   15896         6707 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   15897         6707 :                               sym->common_block))
   15898              :         {
   15899              :           /* Clear these to prevent looking at them again if there was an
   15900              :              error.  */
   15901            2 :           sym->attr.is_bind_c = 0;
   15902            2 :           sym->attr.is_c_interop = 0;
   15903            2 :           sym->ts.is_c_interop = 0;
   15904              :         }
   15905              :       else
   15906              :         {
   15907              :           /* So far, no errors have been found.  */
   15908         6705 :           sym->attr.is_c_interop = 1;
   15909         6705 :           sym->ts.is_c_interop = 1;
   15910              :         }
   15911              : 
   15912         6707 :       curr_arg = gfc_sym_get_dummy_args (sym);
   15913        29880 :       while (curr_arg != NULL)
   15914              :         {
   15915              :           /* Skip implicitly typed dummy args here.  */
   15916        16466 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   15917        16409 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   15918              :               /* If something is found to fail, record the fact so we
   15919              :                  can mark the symbol for the procedure as not being
   15920              :                  BIND(C) to try and prevent multiple errors being
   15921              :                  reported.  */
   15922        16466 :               has_non_interop_arg = 1;
   15923              : 
   15924        16466 :           curr_arg = curr_arg->next;
   15925              :         }
   15926              : 
   15927              :       /* See if any of the arguments were not interoperable and if so, clear
   15928              :          the procedure symbol to prevent duplicate error messages.  */
   15929         6707 :       if (has_non_interop_arg != 0)
   15930              :         {
   15931          128 :           sym->attr.is_c_interop = 0;
   15932          128 :           sym->ts.is_c_interop = 0;
   15933          128 :           sym->attr.is_bind_c = 0;
   15934              :         }
   15935              :     }
   15936              : 
   15937       474040 :   if (!sym->attr.proc_pointer)
   15938              :     {
   15939       472959 :       if (sym->attr.save == SAVE_EXPLICIT)
   15940              :         {
   15941            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   15942              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15943            5 :           return false;
   15944              :         }
   15945       472954 :       if (sym->attr.intent)
   15946              :         {
   15947            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   15948              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15949            1 :           return false;
   15950              :         }
   15951       472953 :       if (sym->attr.subroutine && sym->attr.result)
   15952              :         {
   15953            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   15954            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   15955            2 :           return false;
   15956              :         }
   15957       472951 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   15958       134672 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   15959       134669 :               || sym->attr.contained))
   15960              :         {
   15961            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   15962              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15963            3 :           return false;
   15964              :         }
   15965       472948 :       if (strcmp ("ppr@", sym->name) == 0)
   15966              :         {
   15967            0 :           gfc_error ("Procedure pointer result %qs at %L "
   15968              :                      "is missing the pointer attribute",
   15969            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   15970            0 :           return false;
   15971              :         }
   15972              :     }
   15973              : 
   15974              :   /* Assume that a procedure whose body is not known has references
   15975              :      to external arrays.  */
   15976       474029 :   if (sym->attr.if_source != IFSRC_DECL)
   15977       325843 :     sym->attr.array_outer_dependency = 1;
   15978              : 
   15979              :   /* Compare the characteristics of a module procedure with the
   15980              :      interface declaration. Ideally this would be done with
   15981              :      gfc_compare_interfaces but, at present, the formal interface
   15982              :      cannot be copied to the ts.interface.  */
   15983       474029 :   if (sym->attr.module_procedure
   15984         1517 :       && sym->attr.if_source == IFSRC_DECL)
   15985              :     {
   15986          629 :       gfc_symbol *iface;
   15987          629 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   15988          629 :       char *module_name;
   15989          629 :       char *submodule_name;
   15990          629 :       strcpy (name, sym->ns->proc_name->name);
   15991          629 :       module_name = strtok (name, ".");
   15992          629 :       submodule_name = strtok (NULL, ".");
   15993              : 
   15994          629 :       iface = sym->tlink;
   15995          629 :       sym->tlink = NULL;
   15996              : 
   15997              :       /* Make sure that the result uses the correct charlen for deferred
   15998              :          length results.  */
   15999          629 :       if (iface && sym->result
   16000          189 :           && iface->ts.type == BT_CHARACTER
   16001           19 :           && iface->ts.deferred)
   16002            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16003              : 
   16004            6 :       if (iface == NULL)
   16005          195 :         goto check_formal;
   16006              : 
   16007              :       /* Check the procedure characteristics.  */
   16008          434 :       if (sym->attr.elemental != iface->attr.elemental)
   16009              :         {
   16010            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16011              :                      "PROCEDURE at %L and its interface in %s",
   16012              :                      &sym->declared_at, module_name);
   16013           10 :           return false;
   16014              :         }
   16015              : 
   16016          433 :       if (sym->attr.pure != iface->attr.pure)
   16017              :         {
   16018            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16019              :                      "PROCEDURE at %L and its interface in %s",
   16020              :                      &sym->declared_at, module_name);
   16021            2 :           return false;
   16022              :         }
   16023              : 
   16024          431 :       if (sym->attr.recursive != iface->attr.recursive)
   16025              :         {
   16026            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16027              :                      "PROCEDURE at %L and its interface in %s",
   16028              :                      &sym->declared_at, module_name);
   16029            2 :           return false;
   16030              :         }
   16031              : 
   16032              :       /* Check the result characteristics.  */
   16033          429 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16034              :         {
   16035            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16036              :                      "in MODULE %qs and the declaration at %L in "
   16037              :                      "(SUB)MODULE %qs",
   16038              :                      errmsg, module_name, &sym->declared_at,
   16039              :                      submodule_name ? submodule_name : module_name);
   16040            5 :           return false;
   16041              :         }
   16042              : 
   16043          424 : check_formal:
   16044              :       /* Check the characteristics of the formal arguments.  */
   16045          619 :       if (sym->formal && sym->formal_ns)
   16046              :         {
   16047         1212 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16048              :             {
   16049          697 :               new_formal = arg;
   16050          697 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16051              :             }
   16052              :         }
   16053              :     }
   16054              : 
   16055              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16056              :      BIND(C) attribute.  */
   16057       474019 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16058              :     {
   16059            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16060              :                  sym->name, &sym->declared_at);
   16061            1 :       return false;
   16062              :     }
   16063              : 
   16064              :   return true;
   16065              : }
   16066              : 
   16067              : 
   16068              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16069              :    been defined and we now know their defined arguments, check that they fulfill
   16070              :    the requirements of the standard for procedures used as finalizers.  */
   16071              : 
   16072              : static bool
   16073       111821 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16074              : {
   16075       111821 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16076       111821 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16077       111821 :   bool result = true;
   16078       111821 :   bool seen_scalar = false;
   16079       111821 :   gfc_symbol *vtab;
   16080       111821 :   gfc_component *c;
   16081       111821 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16082              : 
   16083       111821 :   if (parent)
   16084        15575 :     gfc_resolve_finalizers (parent, finalizable);
   16085              : 
   16086              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16087       111821 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16088       352015 :   for (c = derived->components; c; c = c->next)
   16089       240194 :     if (c->ts.type == BT_DERIVED
   16090        67517 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16091              :       {
   16092         8294 :         bool has_final2 = false;
   16093         8294 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16094            0 :           return false;  /* Error.  */
   16095         8294 :         has_final = has_final || has_final2;
   16096              :       }
   16097              :   /* Return early if not finalizable.  */
   16098       111821 :   if (!has_final)
   16099              :     {
   16100       109286 :       if (finalizable)
   16101         8208 :         *finalizable = false;
   16102       109286 :       return true;
   16103              :     }
   16104              : 
   16105              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16106              :      the template. If the finalizers field has the same value, it needs to be
   16107              :      supplied with finalizers of the same pdt_type.  */
   16108         2535 :   if (derived->attr.pdt_type
   16109           30 :       && derived->template_sym
   16110           12 :       && derived->template_sym->f2k_derived
   16111           12 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16112         2547 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16113              :     {
   16114           12 :       gfc_finalizer *tmp = NULL;
   16115           12 :       derived->f2k_derived->finalizers = NULL;
   16116           12 :       prev_link = &derived->f2k_derived->finalizers;
   16117           48 :       for (list = pdt_finalizers; list; list = list->next)
   16118              :         {
   16119           36 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16120           36 :           if (args->sym
   16121           36 :               && args->sym->ts.type == BT_DERIVED
   16122           36 :               && args->sym->ts.u.derived
   16123           36 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16124              :             {
   16125           18 :               tmp = gfc_get_finalizer ();
   16126           18 :               *tmp = *list;
   16127           18 :               tmp->next = NULL;
   16128           18 :               if (*prev_link)
   16129              :                 {
   16130            6 :                   (*prev_link)->next = tmp;
   16131            6 :                   prev_link = &tmp;
   16132              :                 }
   16133              :               else
   16134           12 :                 *prev_link = tmp;
   16135           18 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16136              :             }
   16137              :         }
   16138              :     }
   16139              : 
   16140              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16141              :      does not fit in with the standard's definition, print an error and remove
   16142              :      it from the list.  */
   16143         2535 :   prev_link = &derived->f2k_derived->finalizers;
   16144         5230 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16145              :     {
   16146         2695 :       gfc_formal_arglist *dummy_args;
   16147         2695 :       gfc_symbol* arg;
   16148         2695 :       gfc_finalizer* i;
   16149         2695 :       int my_rank;
   16150              : 
   16151              :       /* Skip this finalizer if we already resolved it.  */
   16152         2695 :       if (list->proc_tree)
   16153              :         {
   16154         2162 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16155          584 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16156         1578 :             seen_scalar = true;
   16157         2162 :           prev_link = &(list->next);
   16158         2162 :           continue;
   16159              :         }
   16160              : 
   16161              :       /* Check this exists and is a SUBROUTINE.  */
   16162          533 :       if (!list->proc_sym->attr.subroutine)
   16163              :         {
   16164            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16165              :                      list->proc_sym->name, &list->where);
   16166            3 :           goto error;
   16167              :         }
   16168              : 
   16169              :       /* We should have exactly one argument.  */
   16170          530 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16171          530 :       if (!dummy_args || dummy_args->next)
   16172              :         {
   16173            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16174              :                      &list->where);
   16175            2 :           goto error;
   16176              :         }
   16177          528 :       arg = dummy_args->sym;
   16178              : 
   16179          528 :       if (!arg)
   16180              :         {
   16181            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16182            1 :                      &list->proc_sym->declared_at, derived->name);
   16183            1 :           goto error;
   16184              :         }
   16185              : 
   16186          527 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16187            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16188              :         {
   16189            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16190              :                      "be the only finalizer with the same kind/type "
   16191              :                      "(F2018: C790)", &list->where);
   16192            0 :           goto error;
   16193              :         }
   16194              : 
   16195              :       /* This argument must be of our type.  */
   16196          527 :       if (!derived->attr.pdt_template
   16197          527 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16198              :         {
   16199            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16200              :                      &arg->declared_at, derived->name);
   16201            2 :           goto error;
   16202              :         }
   16203              : 
   16204              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16205          525 :       if (arg->attr.pointer)
   16206              :         {
   16207            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16208              :                      &arg->declared_at);
   16209            1 :           goto error;
   16210              :         }
   16211          524 :       if (arg->attr.allocatable)
   16212              :         {
   16213            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16214              :                      " ALLOCATABLE", &arg->declared_at);
   16215            1 :           goto error;
   16216              :         }
   16217          523 :       if (arg->attr.optional)
   16218              :         {
   16219            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16220              :                      &arg->declared_at);
   16221            1 :           goto error;
   16222              :         }
   16223              : 
   16224              :       /* It must not be INTENT(OUT).  */
   16225          522 :       if (arg->attr.intent == INTENT_OUT)
   16226              :         {
   16227            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16228              :                      " INTENT(OUT)", &arg->declared_at);
   16229            1 :           goto error;
   16230              :         }
   16231              : 
   16232              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16233          521 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16234            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16235            2 :         gfc_warning (OPT_Wsurprising,
   16236              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16237              :                      " shape argument", &arg->declared_at);
   16238              : 
   16239              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16240              :          defined earlier.  To really loop over the *earlier* declarations,
   16241              :          we need to walk the tail of the list as new ones were pushed at the
   16242              :          front.  */
   16243              :       /* TODO: Handle kind parameters once they are implemented.  */
   16244          521 :       my_rank = (arg->as ? arg->as->rank : 0);
   16245          616 :       for (i = list->next; i; i = i->next)
   16246              :         {
   16247           97 :           gfc_formal_arglist *dummy_args;
   16248              : 
   16249              :           /* Argument list might be empty; that is an error signalled earlier,
   16250              :              but we nevertheless continued resolving.  */
   16251           97 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16252           97 :           if (dummy_args && !derived->attr.pdt_template)
   16253              :             {
   16254           95 :               gfc_symbol* i_arg = dummy_args->sym;
   16255           95 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16256           95 :               if (i_rank == my_rank)
   16257              :                 {
   16258            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16259              :                              " rank (%d) as %qs",
   16260            2 :                              list->proc_sym->name, &list->where, my_rank,
   16261            2 :                              i->proc_sym->name);
   16262            2 :                   goto error;
   16263              :                 }
   16264              :             }
   16265              :         }
   16266              : 
   16267              :         /* Is this the/a scalar finalizer procedure?  */
   16268          519 :         if (my_rank == 0)
   16269          393 :           seen_scalar = true;
   16270              : 
   16271              :         /* Find the symtree for this procedure.  */
   16272          519 :         gcc_assert (!list->proc_tree);
   16273          519 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16274              : 
   16275          519 :         prev_link = &list->next;
   16276          519 :         continue;
   16277              : 
   16278              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16279              :            troubles in the future when they might fail later expectations.  */
   16280           14 : error:
   16281           14 :         i = list;
   16282           14 :         *prev_link = list->next;
   16283           14 :         gfc_free_finalizer (i);
   16284           14 :         result = false;
   16285          519 :     }
   16286              : 
   16287         2535 :   if (result == false)
   16288              :     return false;
   16289              : 
   16290              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   16291              :      were nodes in the list, must have been for arrays.  It is surely a good
   16292              :      idea to have a scalar version there if there's something to finalize.  */
   16293         2531 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   16294            1 :     gfc_warning (OPT_Wsurprising,
   16295              :                  "Only array FINAL procedures declared for derived type %qs"
   16296              :                  " defined at %L, suggest also scalar one unless an assumed"
   16297              :                  " rank finalizer has been declared",
   16298              :                  derived->name, &derived->declared_at);
   16299              : 
   16300         2531 :   if (!derived->attr.pdt_template)
   16301              :     {
   16302         2507 :       vtab = gfc_find_derived_vtab (derived);
   16303         2507 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   16304         2507 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   16305         2507 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   16306              :     }
   16307              : 
   16308         2531 :   if (finalizable)
   16309          640 :     *finalizable = true;
   16310              : 
   16311              :   return true;
   16312              : }
   16313              : 
   16314              : 
   16315              : static gfc_symbol * containing_dt;
   16316              : 
   16317              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   16318              :    arguments whose declared types are PDT instances only transmit the PASS arg
   16319              :    if they match the enclosing derived type.  */
   16320              : 
   16321              : static bool
   16322         1460 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   16323              : {
   16324         1460 :   gfc_formal_arglist *dummy_args;
   16325         1460 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   16326              :     {
   16327          532 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   16328         1190 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   16329          126 :         dummy_args = dummy_args->next;
   16330          532 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   16331          532 :       if (dummy_args->sym->ts.type == BT_CLASS
   16332          532 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   16333              :                      containing_dt->name))
   16334              :         return true;
   16335              :     }
   16336              :   return false;
   16337              : }
   16338              : 
   16339              : 
   16340              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   16341              : 
   16342              : static bool
   16343          732 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   16344              :                              const char* generic_name, locus where)
   16345              : {
   16346          732 :   gfc_symbol *sym1, *sym2;
   16347          732 :   const char *pass1, *pass2;
   16348          732 :   gfc_formal_arglist *dummy_args;
   16349              : 
   16350          732 :   gcc_assert (t1->specific && t2->specific);
   16351          732 :   gcc_assert (!t1->specific->is_generic);
   16352          732 :   gcc_assert (!t2->specific->is_generic);
   16353          732 :   gcc_assert (t1->is_operator == t2->is_operator);
   16354              : 
   16355          732 :   sym1 = t1->specific->u.specific->n.sym;
   16356          732 :   sym2 = t2->specific->u.specific->n.sym;
   16357              : 
   16358          732 :   if (sym1 == sym2)
   16359              :     return true;
   16360              : 
   16361              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   16362          732 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   16363          730 :       || sym1->attr.function != sym2->attr.function)
   16364              :     {
   16365            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   16366              :                  " GENERIC %qs at %L",
   16367              :                  sym1->name, sym2->name, generic_name, &where);
   16368            2 :       return false;
   16369              :     }
   16370              : 
   16371              :   /* Determine PASS arguments.  */
   16372          730 :   if (t1->specific->nopass)
   16373              :     pass1 = NULL;
   16374          679 :   else if (t1->specific->pass_arg)
   16375              :     pass1 = t1->specific->pass_arg;
   16376              :   else
   16377              :     {
   16378          420 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   16379          420 :       if (dummy_args)
   16380          419 :         pass1 = dummy_args->sym->name;
   16381              :       else
   16382              :         pass1 = NULL;
   16383              :     }
   16384          730 :   if (t2->specific->nopass)
   16385              :     pass2 = NULL;
   16386          678 :   else if (t2->specific->pass_arg)
   16387              :     pass2 = t2->specific->pass_arg;
   16388              :   else
   16389              :     {
   16390          541 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   16391          541 :       if (dummy_args)
   16392          540 :         pass2 = dummy_args->sym->name;
   16393              :       else
   16394              :         pass2 = NULL;
   16395              :     }
   16396              : 
   16397              :   /* Care must be taken with pdt types and templates because the declared type
   16398              :      of the argument that is not 'no_pass' need not be the same as the
   16399              :      containing derived type.  If this is the case, subject the argument to
   16400              :      the full interface check, even though it cannot be used in the type
   16401              :      bound context.  */
   16402          730 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   16403          730 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   16404              : 
   16405          730 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   16406          730 :     pass1 = pass2 = NULL;
   16407              : 
   16408              :   /* Compare the interfaces.  */
   16409          730 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   16410              :                               NULL, 0, pass1, pass2))
   16411              :     {
   16412            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   16413              :                  sym1->name, sym2->name, generic_name, &where);
   16414            8 :       return false;
   16415              :     }
   16416              : 
   16417              :   return true;
   16418              : }
   16419              : 
   16420              : 
   16421              : /* Worker function for resolving a generic procedure binding; this is used to
   16422              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   16423              : 
   16424              :    The difference between those cases is finding possible inherited bindings
   16425              :    that are overridden, as one has to look for them in tb_sym_root,
   16426              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   16427              :    the super-type and set p->overridden correctly.  */
   16428              : 
   16429              : static bool
   16430         2308 : resolve_tb_generic_targets (gfc_symbol* super_type,
   16431              :                             gfc_typebound_proc* p, const char* name)
   16432              : {
   16433         2308 :   gfc_tbp_generic* target;
   16434         2308 :   gfc_symtree* first_target;
   16435         2308 :   gfc_symtree* inherited;
   16436              : 
   16437         2308 :   gcc_assert (p && p->is_generic);
   16438              : 
   16439              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   16440         2308 :   gcc_assert (p->u.generic);
   16441         5196 :   for (target = p->u.generic; target; target = target->next)
   16442         2905 :     if (!target->specific)
   16443              :       {
   16444         2526 :         gfc_typebound_proc* overridden_tbp;
   16445         2526 :         gfc_tbp_generic* g;
   16446         2526 :         const char* target_name;
   16447              : 
   16448         2526 :         target_name = target->specific_st->name;
   16449              : 
   16450              :         /* Defined for this type directly.  */
   16451         2526 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   16452              :           {
   16453         2517 :             target->specific = target->specific_st->n.tb;
   16454         2517 :             goto specific_found;
   16455              :           }
   16456              : 
   16457              :         /* Look for an inherited specific binding.  */
   16458            9 :         if (super_type)
   16459              :           {
   16460            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   16461              :                                                  true, NULL);
   16462              : 
   16463            5 :             if (inherited)
   16464              :               {
   16465            5 :                 gcc_assert (inherited->n.tb);
   16466            5 :                 target->specific = inherited->n.tb;
   16467            5 :                 goto specific_found;
   16468              :               }
   16469              :           }
   16470              : 
   16471            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   16472              :                    " at %L", target_name, name, &p->where);
   16473            4 :         return false;
   16474              : 
   16475              :         /* Once we've found the specific binding, check it is not ambiguous with
   16476              :            other specifics already found or inherited for the same GENERIC.  */
   16477         2522 : specific_found:
   16478         2522 :         gcc_assert (target->specific);
   16479              : 
   16480              :         /* This must really be a specific binding!  */
   16481         2522 :         if (target->specific->is_generic)
   16482              :           {
   16483            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   16484              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   16485            3 :             return false;
   16486              :           }
   16487              : 
   16488              :         /* Check those already resolved on this type directly.  */
   16489         6452 :         for (g = p->u.generic; g; g = g->next)
   16490         1428 :           if (g != target && g->specific
   16491         4654 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   16492              :             return false;
   16493              : 
   16494              :         /* Check for ambiguity with inherited specific targets.  */
   16495         2528 :         for (overridden_tbp = p->overridden; overridden_tbp;
   16496           16 :              overridden_tbp = overridden_tbp->overridden)
   16497           19 :           if (overridden_tbp->is_generic)
   16498              :             {
   16499           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   16500              :                 {
   16501           18 :                   gcc_assert (g->specific);
   16502           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   16503              :                     return false;
   16504              :                 }
   16505              :             }
   16506              :       }
   16507              : 
   16508              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   16509         2291 :   if (p->overridden && !p->overridden->is_generic)
   16510              :     {
   16511            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   16512              :                  " the same name", name, &p->where);
   16513            1 :       return false;
   16514              :     }
   16515              : 
   16516              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   16517              :      all must have the same attributes here.  */
   16518         2290 :   first_target = p->u.generic->specific->u.specific;
   16519         2290 :   gcc_assert (first_target);
   16520         2290 :   p->subroutine = first_target->n.sym->attr.subroutine;
   16521         2290 :   p->function = first_target->n.sym->attr.function;
   16522              : 
   16523         2290 :   return true;
   16524              : }
   16525              : 
   16526              : 
   16527              : /* Resolve a GENERIC procedure binding for a derived type.  */
   16528              : 
   16529              : static bool
   16530         1202 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   16531              : {
   16532         1202 :   gfc_symbol* super_type;
   16533              : 
   16534              :   /* Find the overridden binding if any.  */
   16535         1202 :   st->n.tb->overridden = NULL;
   16536         1202 :   super_type = gfc_get_derived_super_type (derived);
   16537         1202 :   if (super_type)
   16538              :     {
   16539           40 :       gfc_symtree* overridden;
   16540           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   16541              :                                             true, NULL);
   16542              : 
   16543           40 :       if (overridden && overridden->n.tb)
   16544           21 :         st->n.tb->overridden = overridden->n.tb;
   16545              :     }
   16546              : 
   16547              :   /* Resolve using worker function.  */
   16548         1202 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   16549              : }
   16550              : 
   16551              : 
   16552              : /* Retrieve the target-procedure of an operator binding and do some checks in
   16553              :    common for intrinsic and user-defined type-bound operators.  */
   16554              : 
   16555              : static gfc_symbol*
   16556         1178 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   16557              : {
   16558         1178 :   gfc_symbol* target_proc;
   16559              : 
   16560         1178 :   gcc_assert (target->specific && !target->specific->is_generic);
   16561         1178 :   target_proc = target->specific->u.specific->n.sym;
   16562         1178 :   gcc_assert (target_proc);
   16563              : 
   16564              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   16565         1178 :   if (target->specific->nopass)
   16566              :     {
   16567            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   16568            2 :       return NULL;
   16569              :     }
   16570              : 
   16571              :   return target_proc;
   16572              : }
   16573              : 
   16574              : 
   16575              : /* Resolve a type-bound intrinsic operator.  */
   16576              : 
   16577              : static bool
   16578         1047 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   16579              :                                 gfc_typebound_proc* p)
   16580              : {
   16581         1047 :   gfc_symbol* super_type;
   16582         1047 :   gfc_tbp_generic* target;
   16583              : 
   16584              :   /* If there's already an error here, do nothing (but don't fail again).  */
   16585         1047 :   if (p->error)
   16586              :     return true;
   16587              : 
   16588              :   /* Operators should always be GENERIC bindings.  */
   16589         1047 :   gcc_assert (p->is_generic);
   16590              : 
   16591              :   /* Look for an overridden binding.  */
   16592         1047 :   super_type = gfc_get_derived_super_type (derived);
   16593         1047 :   if (super_type && super_type->f2k_derived)
   16594            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   16595              :                                                      op, true, NULL);
   16596              :   else
   16597         1046 :     p->overridden = NULL;
   16598              : 
   16599              :   /* Resolve general GENERIC properties using worker function.  */
   16600         1047 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   16601            1 :     goto error;
   16602              : 
   16603              :   /* Check the targets to be procedures of correct interface.  */
   16604         2139 :   for (target = p->u.generic; target; target = target->next)
   16605              :     {
   16606         1118 :       gfc_symbol* target_proc;
   16607              : 
   16608         1118 :       target_proc = get_checked_tb_operator_target (target, p->where);
   16609         1118 :       if (!target_proc)
   16610            1 :         goto error;
   16611              : 
   16612         1117 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   16613            3 :         goto error;
   16614              : 
   16615              :       /* Add target to non-typebound operator list.  */
   16616         1114 :       if (!target->specific->deferred && !derived->attr.use_assoc
   16617          391 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   16618              :         {
   16619          389 :           gfc_interface *head, *intr;
   16620              : 
   16621              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   16622              :              mechanism for handling module procedures winds up resolving
   16623              :              operator interfaces twice and would otherwise cause an error.
   16624              :              Likewise, new instances of PDTs can cause the operator inter-
   16625              :              faces to be resolved multiple times.  */
   16626          461 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   16627           91 :             if (intr->sym == target_proc
   16628           21 :                 && (target_proc->attr.used_in_submodule
   16629            4 :                     || derived->attr.pdt_type
   16630            2 :                     || derived->attr.pdt_template))
   16631              :               return true;
   16632              : 
   16633          370 :           if (!gfc_check_new_interface (derived->ns->op[op],
   16634              :                                         target_proc, p->where))
   16635              :             return false;
   16636          368 :           head = derived->ns->op[op];
   16637          368 :           intr = gfc_get_interface ();
   16638          368 :           intr->sym = target_proc;
   16639          368 :           intr->where = p->where;
   16640          368 :           intr->next = head;
   16641          368 :           derived->ns->op[op] = intr;
   16642              :         }
   16643              :     }
   16644              : 
   16645              :   return true;
   16646              : 
   16647            5 : error:
   16648            5 :   p->error = 1;
   16649            5 :   return false;
   16650              : }
   16651              : 
   16652              : 
   16653              : /* Resolve a type-bound user operator (tree-walker callback).  */
   16654              : 
   16655              : static gfc_symbol* resolve_bindings_derived;
   16656              : static bool resolve_bindings_result;
   16657              : 
   16658              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   16659              : 
   16660              : static void
   16661           59 : resolve_typebound_user_op (gfc_symtree* stree)
   16662              : {
   16663           59 :   gfc_symbol* super_type;
   16664           59 :   gfc_tbp_generic* target;
   16665              : 
   16666           59 :   gcc_assert (stree && stree->n.tb);
   16667              : 
   16668           59 :   if (stree->n.tb->error)
   16669              :     return;
   16670              : 
   16671              :   /* Operators should always be GENERIC bindings.  */
   16672           59 :   gcc_assert (stree->n.tb->is_generic);
   16673              : 
   16674              :   /* Find overridden procedure, if any.  */
   16675           59 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16676           59 :   if (super_type && super_type->f2k_derived)
   16677              :     {
   16678            0 :       gfc_symtree* overridden;
   16679            0 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   16680              :                                                stree->name, true, NULL);
   16681              : 
   16682            0 :       if (overridden && overridden->n.tb)
   16683            0 :         stree->n.tb->overridden = overridden->n.tb;
   16684              :     }
   16685              :   else
   16686           59 :     stree->n.tb->overridden = NULL;
   16687              : 
   16688              :   /* Resolve basically using worker function.  */
   16689           59 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   16690            0 :     goto error;
   16691              : 
   16692              :   /* Check the targets to be functions of correct interface.  */
   16693          116 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   16694              :     {
   16695           60 :       gfc_symbol* target_proc;
   16696              : 
   16697           60 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   16698           60 :       if (!target_proc)
   16699            1 :         goto error;
   16700              : 
   16701           59 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   16702            2 :         goto error;
   16703              :     }
   16704              : 
   16705              :   return;
   16706              : 
   16707            3 : error:
   16708            3 :   resolve_bindings_result = false;
   16709            3 :   stree->n.tb->error = 1;
   16710              : }
   16711              : 
   16712              : 
   16713              : /* Resolve the type-bound procedures for a derived type.  */
   16714              : 
   16715              : static void
   16716         9935 : resolve_typebound_procedure (gfc_symtree* stree)
   16717              : {
   16718         9935 :   gfc_symbol* proc;
   16719         9935 :   locus where;
   16720         9935 :   gfc_symbol* me_arg;
   16721         9935 :   gfc_symbol* super_type;
   16722         9935 :   gfc_component* comp;
   16723              : 
   16724         9935 :   gcc_assert (stree);
   16725              : 
   16726              :   /* Undefined specific symbol from GENERIC target definition.  */
   16727         9935 :   if (!stree->n.tb)
   16728         9853 :     return;
   16729              : 
   16730         9929 :   if (stree->n.tb->error)
   16731              :     return;
   16732              : 
   16733              :   /* If this is a GENERIC binding, use that routine.  */
   16734         9913 :   if (stree->n.tb->is_generic)
   16735              :     {
   16736         1202 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   16737           17 :         goto error;
   16738              :       return;
   16739              :     }
   16740              : 
   16741              :   /* Get the target-procedure to check it.  */
   16742         8711 :   gcc_assert (!stree->n.tb->is_generic);
   16743         8711 :   gcc_assert (stree->n.tb->u.specific);
   16744         8711 :   proc = stree->n.tb->u.specific->n.sym;
   16745         8711 :   where = stree->n.tb->where;
   16746              : 
   16747              :   /* Default access should already be resolved from the parser.  */
   16748         8711 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   16749              : 
   16750         8711 :   if (stree->n.tb->deferred)
   16751              :     {
   16752          676 :       if (!check_proc_interface (proc, &where))
   16753            5 :         goto error;
   16754              :     }
   16755              :   else
   16756              :     {
   16757              :       /* If proc has not been resolved at this point, proc->name may
   16758              :          actually be a USE associated entity. See PR fortran/89647. */
   16759         8035 :       if (!proc->resolve_symbol_called
   16760         5351 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   16761              :         {
   16762           11 :           gfc_symbol *tmp;
   16763           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   16764           11 :           if (tmp && tmp->attr.use_assoc)
   16765              :             {
   16766            1 :               proc->module = tmp->module;
   16767            1 :               proc->attr.proc = tmp->attr.proc;
   16768            1 :               proc->attr.function = tmp->attr.function;
   16769            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   16770            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   16771            1 :               proc->ts = tmp->ts;
   16772            1 :               proc->result = tmp->result;
   16773              :             }
   16774              :         }
   16775              : 
   16776              :       /* Check for F08:C465.  */
   16777         8035 :       if ((!proc->attr.subroutine && !proc->attr.function)
   16778         8025 :           || (proc->attr.proc != PROC_MODULE
   16779           70 :               && proc->attr.if_source != IFSRC_IFBODY
   16780            7 :               && !proc->attr.module_procedure)
   16781         8024 :           || proc->attr.abstract)
   16782              :         {
   16783           12 :           gfc_error ("%qs must be a module procedure or an external "
   16784              :                      "procedure with an explicit interface at %L",
   16785              :                      proc->name, &where);
   16786           12 :           goto error;
   16787              :         }
   16788              :     }
   16789              : 
   16790         8694 :   stree->n.tb->subroutine = proc->attr.subroutine;
   16791         8694 :   stree->n.tb->function = proc->attr.function;
   16792              : 
   16793              :   /* Find the super-type of the current derived type.  We could do this once and
   16794              :      store in a global if speed is needed, but as long as not I believe this is
   16795              :      more readable and clearer.  */
   16796         8694 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16797              : 
   16798              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   16799              :      from a .mod file.  */
   16800         8694 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   16801              :     {
   16802         2767 :       gfc_formal_arglist *dummy_args;
   16803              : 
   16804         2767 :       dummy_args = gfc_sym_get_dummy_args (proc);
   16805         2767 :       if (stree->n.tb->pass_arg)
   16806              :         {
   16807          468 :           gfc_formal_arglist *i;
   16808              : 
   16809              :           /* If an explicit passing argument name is given, walk the arg-list
   16810              :              and look for it.  */
   16811              : 
   16812          468 :           me_arg = NULL;
   16813          468 :           stree->n.tb->pass_arg_num = 1;
   16814          601 :           for (i = dummy_args; i; i = i->next)
   16815              :             {
   16816          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   16817              :                 {
   16818              :                   me_arg = i->sym;
   16819              :                   break;
   16820              :                 }
   16821          133 :               ++stree->n.tb->pass_arg_num;
   16822              :             }
   16823              : 
   16824          468 :           if (!me_arg)
   16825              :             {
   16826            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   16827              :                          " argument %qs",
   16828              :                          proc->name, stree->n.tb->pass_arg, &where,
   16829              :                          stree->n.tb->pass_arg);
   16830            2 :               goto error;
   16831              :             }
   16832              :         }
   16833              :       else
   16834              :         {
   16835              :           /* Otherwise, take the first one; there should in fact be at least
   16836              :              one.  */
   16837         2299 :           stree->n.tb->pass_arg_num = 1;
   16838         2299 :           if (!dummy_args)
   16839              :             {
   16840            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   16841              :                          " least one argument", proc->name, &where);
   16842            2 :               goto error;
   16843              :             }
   16844         2297 :           me_arg = dummy_args->sym;
   16845              :         }
   16846              : 
   16847              :       /* Now check that the argument-type matches and the passed-object
   16848              :          dummy argument is generally fine.  */
   16849              : 
   16850         2297 :       gcc_assert (me_arg);
   16851              : 
   16852         2763 :       if (me_arg->ts.type != BT_CLASS)
   16853              :         {
   16854            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   16855              :                      " at %L", proc->name, &where);
   16856            5 :           goto error;
   16857              :         }
   16858              : 
   16859              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   16860         2758 :       if (!resolve_bindings_derived->attr.pdt_template
   16861         2749 :           && !(containing_dt && containing_dt->attr.pdt_type
   16862           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   16863         2729 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   16864              :         {
   16865            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   16866              :                      "the derived-type %qs", me_arg->name, proc->name,
   16867              :                      me_arg->name, &where, resolve_bindings_derived->name);
   16868            0 :           goto error;
   16869              :         }
   16870              : 
   16871         2758 :       if (resolve_bindings_derived->attr.pdt_template
   16872         2767 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   16873            9 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   16874              :         {
   16875            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   16876              :                      "the parametric derived-type %qs", me_arg->name,
   16877              :                      proc->name, me_arg->name, &where,
   16878              :                      resolve_bindings_derived->name);
   16879            0 :           goto error;
   16880              :         }
   16881              : 
   16882         2758 :       if (((resolve_bindings_derived->attr.pdt_template
   16883            9 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   16884            9 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   16885         2749 :            || resolve_bindings_derived->attr.pdt_type)
   16886           69 :           && (me_arg->param_list != NULL)
   16887         2827 :           && (gfc_spec_list_type (me_arg->param_list,
   16888           69 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   16889              :                                   != SPEC_ASSUMED))
   16890              :         {
   16891              : 
   16892              :           /* Add a check to verify if there are any LEN parameters in the
   16893              :              first place.  If there are LEN parameters, throw this error.
   16894              :              If there are only KIND parameters, then don't trigger
   16895              :              this error.  */
   16896            6 :           gfc_component *c;
   16897            6 :           bool seen_len_param = false;
   16898            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   16899              : 
   16900            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   16901              :             {
   16902            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   16903              :                                      me_arg_param->name, true, true, NULL);
   16904              : 
   16905            6 :               gcc_assert (c != NULL);
   16906              : 
   16907            6 :               if (c->attr.pdt_kind)
   16908            0 :                 continue;
   16909              : 
   16910              :               /* Getting here implies that there is a pdt_len parameter
   16911              :                  in the list.  */
   16912              :               seen_len_param = true;
   16913              :               break;
   16914              :             }
   16915              : 
   16916            6 :             if (seen_len_param)
   16917              :               {
   16918            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   16919              :                            "argument %qs of %qs at %L must be ASSUMED.",
   16920              :                            me_arg->name, proc->name, &where);
   16921            6 :                 goto error;
   16922              :               }
   16923              :         }
   16924              : 
   16925         2752 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   16926         2752 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   16927              :         {
   16928            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   16929              :                      " scalar", proc->name, &where);
   16930            1 :           goto error;
   16931              :         }
   16932         2751 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   16933              :         {
   16934            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   16935              :                      " be ALLOCATABLE", proc->name, &where);
   16936            2 :           goto error;
   16937              :         }
   16938         2749 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   16939              :         {
   16940            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   16941              :                      " be POINTER", proc->name, &where);
   16942            2 :           goto error;
   16943              :         }
   16944              :     }
   16945              : 
   16946              :   /* If we are extending some type, check that we don't override a procedure
   16947              :      flagged NON_OVERRIDABLE.  */
   16948         8674 :   stree->n.tb->overridden = NULL;
   16949         8674 :   if (super_type)
   16950              :     {
   16951         1491 :       gfc_symtree* overridden;
   16952         1491 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   16953              :                                             stree->name, true, NULL);
   16954              : 
   16955         1491 :       if (overridden)
   16956              :         {
   16957         1214 :           if (overridden->n.tb)
   16958         1214 :             stree->n.tb->overridden = overridden->n.tb;
   16959              : 
   16960         1214 :           if (!gfc_check_typebound_override (stree, overridden))
   16961           26 :             goto error;
   16962              :         }
   16963              :     }
   16964              : 
   16965              :   /* See if there's a name collision with a component directly in this type.  */
   16966        20838 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   16967        12191 :     if (!strcmp (comp->name, stree->name))
   16968              :       {
   16969            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   16970              :                    " %qs",
   16971              :                    stree->name, &where, resolve_bindings_derived->name);
   16972            1 :         goto error;
   16973              :       }
   16974              : 
   16975              :   /* Try to find a name collision with an inherited component.  */
   16976         8647 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   16977              :                                         NULL))
   16978              :     {
   16979            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   16980              :                  " component of %qs",
   16981              :                  stree->name, &where, resolve_bindings_derived->name);
   16982            1 :       goto error;
   16983              :     }
   16984              : 
   16985         8646 :   stree->n.tb->error = 0;
   16986         8646 :   return;
   16987              : 
   16988           82 : error:
   16989           82 :   resolve_bindings_result = false;
   16990           82 :   stree->n.tb->error = 1;
   16991              : }
   16992              : 
   16993              : 
   16994              : static bool
   16995        85860 : resolve_typebound_procedures (gfc_symbol* derived)
   16996              : {
   16997        85860 :   int op;
   16998        85860 :   gfc_symbol* super_type;
   16999              : 
   17000        85860 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17001              :     return true;
   17002              : 
   17003         4756 :   super_type = gfc_get_derived_super_type (derived);
   17004         4756 :   if (super_type)
   17005          857 :     resolve_symbol (super_type);
   17006              : 
   17007         4756 :   resolve_bindings_derived = derived;
   17008         4756 :   resolve_bindings_result = true;
   17009              : 
   17010         4756 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17011         4756 :   if (derived->f2k_derived->tb_sym_root)
   17012         4756 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17013              :                           &resolve_typebound_procedure);
   17014              : 
   17015         4756 :   if (derived->f2k_derived->tb_uop_root)
   17016           55 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17017              :                           &resolve_typebound_user_op);
   17018         4756 :   containing_dt = NULL;
   17019              : 
   17020       137924 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17021              :     {
   17022       133168 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17023       133168 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17024              :                                                 (gfc_intrinsic_op)op, p))
   17025            7 :         resolve_bindings_result = false;
   17026              :     }
   17027              : 
   17028         4756 :   return resolve_bindings_result;
   17029              : }
   17030              : 
   17031              : 
   17032              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17033              :    to give all identical derived types the same backend_decl.  */
   17034              : static void
   17035       176304 : add_dt_to_dt_list (gfc_symbol *derived)
   17036              : {
   17037       176304 :   if (!derived->dt_next)
   17038              :     {
   17039        82043 :       if (gfc_derived_types)
   17040              :         {
   17041        67278 :           derived->dt_next = gfc_derived_types->dt_next;
   17042        67278 :           gfc_derived_types->dt_next = derived;
   17043              :         }
   17044              :       else
   17045              :         {
   17046        14765 :           derived->dt_next = derived;
   17047              :         }
   17048        82043 :       gfc_derived_types = derived;
   17049              :     }
   17050       176304 : }
   17051              : 
   17052              : 
   17053              : /* Ensure that a derived-type is really not abstract, meaning that every
   17054              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17055              : 
   17056              : static bool
   17057         7086 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17058              : {
   17059         7086 :   if (!st)
   17060              :     return true;
   17061              : 
   17062         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17063              :     return false;
   17064         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17065              :     return false;
   17066              : 
   17067         2771 :   if (st->n.tb && st->n.tb->deferred)
   17068              :     {
   17069         2019 :       gfc_symtree* overriding;
   17070         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17071         2019 :       if (!overriding)
   17072              :         return false;
   17073         2018 :       gcc_assert (overriding->n.tb);
   17074         2018 :       if (overriding->n.tb->deferred)
   17075              :         {
   17076            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17077              :                      " %qs is DEFERRED and not overridden",
   17078              :                      sub->name, &sub->declared_at, st->name);
   17079            5 :           return false;
   17080              :         }
   17081              :     }
   17082              : 
   17083              :   return true;
   17084              : }
   17085              : 
   17086              : static bool
   17087         1394 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17088              : {
   17089              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17090              :      and for each ancestor-type, check all bindings.  If any of them is
   17091              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17092              :      binding is not DEFERRED.
   17093              :      This is not the most efficient way to do this, but it should be ok and is
   17094              :      clearer than something sophisticated.  */
   17095              : 
   17096         1543 :   gcc_assert (ancestor && !sub->attr.abstract);
   17097              : 
   17098         1543 :   if (!ancestor->attr.abstract)
   17099              :     return true;
   17100              : 
   17101              :   /* Walk bindings of this ancestor.  */
   17102         1542 :   if (ancestor->f2k_derived)
   17103              :     {
   17104         1542 :       bool t;
   17105         1542 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17106         1542 :       if (!t)
   17107              :         return false;
   17108              :     }
   17109              : 
   17110              :   /* Find next ancestor type and recurse on it.  */
   17111         1536 :   ancestor = gfc_get_derived_super_type (ancestor);
   17112         1536 :   if (ancestor)
   17113              :     return ensure_not_abstract (sub, ancestor);
   17114              : 
   17115              :   return true;
   17116              : }
   17117              : 
   17118              : 
   17119              : /* This check for typebound defined assignments is done recursively
   17120              :    since the order in which derived types are resolved is not always in
   17121              :    order of the declarations.  */
   17122              : 
   17123              : static void
   17124       180801 : check_defined_assignments (gfc_symbol *derived)
   17125              : {
   17126       180801 :   gfc_component *c;
   17127              : 
   17128       605961 :   for (c = derived->components; c; c = c->next)
   17129              :     {
   17130       426937 :       if (!gfc_bt_struct (c->ts.type)
   17131       103146 :           || c->attr.pointer
   17132        20446 :           || c->attr.proc_pointer_comp
   17133        20446 :           || c->attr.class_pointer
   17134        20440 :           || c->attr.proc_pointer)
   17135       406941 :         continue;
   17136              : 
   17137        19996 :       if (c->ts.u.derived->attr.defined_assign_comp
   17138        19761 :           || (c->ts.u.derived->f2k_derived
   17139        19191 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17140              :         {
   17141         1753 :           derived->attr.defined_assign_comp = 1;
   17142         1753 :           return;
   17143              :         }
   17144              : 
   17145        18243 :       if (c->attr.allocatable)
   17146         6637 :         continue;
   17147              : 
   17148        11606 :       check_defined_assignments (c->ts.u.derived);
   17149        11606 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17150              :         {
   17151           24 :           derived->attr.defined_assign_comp = 1;
   17152           24 :           return;
   17153              :         }
   17154              :     }
   17155              : }
   17156              : 
   17157              : 
   17158              : /* Resolve a single component of a derived type or structure.  */
   17159              : 
   17160              : static bool
   17161       407182 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17162              : {
   17163       407182 :   gfc_symbol *super_type;
   17164       407182 :   symbol_attribute *attr;
   17165              : 
   17166       407182 :   if (c->attr.artificial)
   17167              :     return true;
   17168              : 
   17169              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17170              :      such as block data because the procedure pointers will cause ICEs
   17171              :      and vtables are not needed in these contexts.  */
   17172       277996 :   if (sym->attr.vtype && sym->attr.use_assoc
   17173        48469 :       && sym->ns->proc_name == NULL)
   17174              :     return true;
   17175              : 
   17176              :   /* F2008, C442.  */
   17177       277987 :   if ((!sym->attr.is_class || c != sym->components)
   17178       277987 :       && c->attr.codimension
   17179          208 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17180              :     {
   17181            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17182              :                  "deferred shape", c->name, &c->loc);
   17183            4 :       return false;
   17184              :     }
   17185              : 
   17186              :   /* F2008, C443.  */
   17187       277983 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17188           85 :       && c->ts.u.derived->ts.is_iso_c)
   17189              :     {
   17190            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17191              :                  "shall not be a coarray", c->name, &c->loc);
   17192            1 :       return false;
   17193              :     }
   17194              : 
   17195              :   /* F2008, C444.  */
   17196       277982 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17197           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17198           26 :           || c->attr.allocatable))
   17199              :     {
   17200            3 :       gfc_error ("Component %qs at %L with coarray component "
   17201              :                  "shall be a nonpointer, nonallocatable scalar",
   17202              :                  c->name, &c->loc);
   17203            3 :       return false;
   17204              :     }
   17205              : 
   17206              :   /* F2008, C448.  */
   17207       277979 :   if (c->ts.type == BT_CLASS)
   17208              :     {
   17209         6916 :       if (c->attr.class_ok && CLASS_DATA (c))
   17210              :         {
   17211         6908 :           attr = &(CLASS_DATA (c)->attr);
   17212              : 
   17213              :           /* Fix up contiguous attribute.  */
   17214         6908 :           if (c->attr.contiguous)
   17215           11 :             attr->contiguous = 1;
   17216              :         }
   17217              :       else
   17218              :         attr = NULL;
   17219              :     }
   17220              :   else
   17221       271063 :     attr = &c->attr;
   17222              : 
   17223       277982 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17224              :     {
   17225            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17226              :                  "is not an array pointer", c->name, &c->loc);
   17227            5 :       return false;
   17228              :     }
   17229              : 
   17230              :   /* F2003, 15.2.1 - length has to be one.  */
   17231        40590 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17232       277993 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17233           19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17234           19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17235              :     {
   17236            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17237              :                  c->name, &c->loc);
   17238            1 :       return false;
   17239              :     }
   17240              : 
   17241        51646 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17242          307 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17243       277981 :       && !(gfc_get_derived_super_type (sym)
   17244            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17245            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17246              :     {
   17247            8 :       gfc_actual_arglist *type_spec_list;
   17248            8 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17249              :                                 &type_spec_list)
   17250              :           != MATCH_YES)
   17251            0 :         return false;
   17252            8 :       gfc_free_actual_arglist (c->param_list);
   17253            8 :       c->param_list = type_spec_list;
   17254            8 :       if (!sym->attr.pdt_type)
   17255            8 :         sym->attr.pdt_comp = 1;
   17256              :     }
   17257       277965 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17258           54 :     sym->attr.pdt_comp = 1;
   17259              : 
   17260       277973 :   if (c->attr.proc_pointer && c->ts.interface)
   17261              :     {
   17262        14594 :       gfc_symbol *ifc = c->ts.interface;
   17263              : 
   17264        14594 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17265              :         {
   17266            6 :           c->tb->error = 1;
   17267            6 :           return false;
   17268              :         }
   17269              : 
   17270        14588 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17271              :         {
   17272              :           /* Resolve interface and copy attributes.  */
   17273        14539 :           if (ifc->formal && !ifc->formal_ns)
   17274         2553 :             resolve_symbol (ifc);
   17275        14539 :           if (ifc->attr.intrinsic)
   17276            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17277              : 
   17278        14539 :           if (ifc->result)
   17279              :             {
   17280         7601 :               c->ts = ifc->result->ts;
   17281         7601 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17282         7601 :               c->attr.pointer = ifc->result->attr.pointer;
   17283         7601 :               c->attr.dimension = ifc->result->attr.dimension;
   17284         7601 :               c->as = gfc_copy_array_spec (ifc->result->as);
   17285         7601 :               c->attr.class_ok = ifc->result->attr.class_ok;
   17286              :             }
   17287              :           else
   17288              :             {
   17289         6938 :               c->ts = ifc->ts;
   17290         6938 :               c->attr.allocatable = ifc->attr.allocatable;
   17291         6938 :               c->attr.pointer = ifc->attr.pointer;
   17292         6938 :               c->attr.dimension = ifc->attr.dimension;
   17293         6938 :               c->as = gfc_copy_array_spec (ifc->as);
   17294         6938 :               c->attr.class_ok = ifc->attr.class_ok;
   17295              :             }
   17296        14539 :           c->ts.interface = ifc;
   17297        14539 :           c->attr.function = ifc->attr.function;
   17298        14539 :           c->attr.subroutine = ifc->attr.subroutine;
   17299              : 
   17300        14539 :           c->attr.pure = ifc->attr.pure;
   17301        14539 :           c->attr.elemental = ifc->attr.elemental;
   17302        14539 :           c->attr.recursive = ifc->attr.recursive;
   17303        14539 :           c->attr.always_explicit = ifc->attr.always_explicit;
   17304        14539 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   17305              :           /* Copy char length.  */
   17306        14539 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   17307              :             {
   17308          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   17309          454 :               if (cl->length && !cl->resolved
   17310          601 :                   && !gfc_resolve_expr (cl->length))
   17311              :                 {
   17312            0 :                   c->tb->error = 1;
   17313            0 :                   return false;
   17314              :                 }
   17315          491 :               c->ts.u.cl = cl;
   17316              :             }
   17317              :         }
   17318              :     }
   17319       263379 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   17320              :     {
   17321              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   17322              :          interface must be a subroutine.  */
   17323          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   17324              :     }
   17325              : 
   17326              :   /* Procedure pointer components: Check PASS arg.  */
   17327       277967 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   17328          805 :       && !sym->attr.vtype)
   17329              :     {
   17330           95 :       gfc_symbol* me_arg;
   17331              : 
   17332           95 :       if (c->tb->pass_arg)
   17333              :         {
   17334           20 :           gfc_formal_arglist* i;
   17335              : 
   17336              :           /* If an explicit passing argument name is given, walk the arg-list
   17337              :             and look for it.  */
   17338              : 
   17339           20 :           me_arg = NULL;
   17340           20 :           c->tb->pass_arg_num = 1;
   17341           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   17342              :             {
   17343           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   17344              :                 {
   17345              :                   me_arg = i->sym;
   17346              :                   break;
   17347              :                 }
   17348           14 :               c->tb->pass_arg_num++;
   17349              :             }
   17350              : 
   17351           20 :           if (!me_arg)
   17352              :             {
   17353            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   17354              :                          "at %L has no argument %qs", c->name,
   17355              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   17356            1 :               c->tb->error = 1;
   17357            1 :               return false;
   17358              :             }
   17359              :         }
   17360              :       else
   17361              :         {
   17362              :           /* Otherwise, take the first one; there should in fact be at least
   17363              :             one.  */
   17364           75 :           c->tb->pass_arg_num = 1;
   17365           75 :           if (!c->ts.interface->formal)
   17366              :             {
   17367            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   17368              :                          "must have at least one argument",
   17369              :                          c->name, &c->loc);
   17370            3 :               c->tb->error = 1;
   17371            3 :               return false;
   17372              :             }
   17373           72 :           me_arg = c->ts.interface->formal->sym;
   17374              :         }
   17375              : 
   17376              :       /* Now check that the argument-type matches.  */
   17377           72 :       gcc_assert (me_arg);
   17378           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   17379           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   17380           90 :           || (me_arg->ts.type == BT_CLASS
   17381           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   17382              :         {
   17383            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   17384              :                      " the derived type %qs", me_arg->name, c->name,
   17385              :                      me_arg->name, &c->loc, sym->name);
   17386            1 :           c->tb->error = 1;
   17387            1 :           return false;
   17388              :         }
   17389              : 
   17390              :       /* Check for F03:C453.  */
   17391           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   17392              :         {
   17393            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17394              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   17395              :                      &c->loc);
   17396            1 :           c->tb->error = 1;
   17397            1 :           return false;
   17398              :         }
   17399              : 
   17400           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17401              :         {
   17402            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17403              :                      "may not have the POINTER attribute", me_arg->name,
   17404              :                      c->name, me_arg->name, &c->loc);
   17405            1 :           c->tb->error = 1;
   17406            1 :           return false;
   17407              :         }
   17408              : 
   17409           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17410              :         {
   17411            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17412              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   17413              :                      me_arg->name, &c->loc);
   17414            1 :           c->tb->error = 1;
   17415            1 :           return false;
   17416              :         }
   17417              : 
   17418           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   17419              :         {
   17420            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17421              :                      " at %L", c->name, &c->loc);
   17422            2 :           return false;
   17423              :         }
   17424              : 
   17425              :     }
   17426              : 
   17427              :   /* Check type-spec if this is not the parent-type component.  */
   17428       277957 :   if (((sym->attr.is_class
   17429        12366 :         && (!sym->components->ts.u.derived->attr.extension
   17430         2385 :             || c != CLASS_DATA (sym->components)))
   17431       266927 :        || (!sym->attr.is_class
   17432       265591 :            && (!sym->attr.extension || c != sym->components)))
   17433       269864 :       && !sym->attr.vtype
   17434       439994 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   17435              :     return false;
   17436              : 
   17437       277956 :   super_type = gfc_get_derived_super_type (sym);
   17438              : 
   17439              :   /* If this type is an extension, set the accessibility of the parent
   17440              :      component.  */
   17441       277956 :   if (super_type
   17442        25518 :       && ((sym->attr.is_class
   17443        12366 :            && c == CLASS_DATA (sym->components))
   17444        16920 :           || (!sym->attr.is_class && c == sym->components))
   17445        15355 :       && strcmp (super_type->name, c->name) == 0)
   17446         6595 :     c->attr.access = super_type->attr.access;
   17447              : 
   17448              :   /* If this type is an extension, see if this component has the same name
   17449              :      as an inherited type-bound procedure.  */
   17450        25518 :   if (super_type && !sym->attr.is_class
   17451        13152 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   17452              :     {
   17453            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   17454              :                  " inherited type-bound procedure",
   17455              :                  c->name, sym->name, &c->loc);
   17456            1 :       return false;
   17457              :     }
   17458              : 
   17459       277955 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   17460         9410 :       && !c->ts.deferred)
   17461              :     {
   17462         7184 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   17463          258 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   17464              : 
   17465         7184 :       if (c->ts.u.cl->length == NULL
   17466         7178 :           || !resolve_charlen(c->ts.u.cl)
   17467        14361 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   17468              :         {
   17469            9 :           gfc_error ("Character length of component %qs needs to "
   17470              :                      "be a constant specification expression at %L",
   17471              :                      c->name,
   17472            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   17473            9 :           return false;
   17474              :         }
   17475              : 
   17476         7175 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   17477              :         {
   17478            2 :          if (!c->ts.u.cl->length->error)
   17479              :            {
   17480            1 :              gfc_error ("Character length expression of component %qs at %L "
   17481              :                         "must be of INTEGER type, found %s",
   17482            1 :                         c->name, &c->ts.u.cl->length->where,
   17483              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   17484            1 :              c->ts.u.cl->length->error = 1;
   17485              :            }
   17486            2 :          return false;
   17487              :        }
   17488              :     }
   17489              : 
   17490       277944 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   17491         2262 :       && !c->attr.pointer && !c->attr.allocatable)
   17492              :     {
   17493            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   17494              :                  "length must be a POINTER or ALLOCATABLE",
   17495              :                  c->name, sym->name, &c->loc);
   17496            1 :       return false;
   17497              :     }
   17498              : 
   17499              :   /* Add the hidden deferred length field.  */
   17500       277943 :   if (c->ts.type == BT_CHARACTER
   17501         9910 :       && (c->ts.deferred || c->attr.pdt_string)
   17502         2438 :       && !c->attr.function
   17503         2402 :       && !sym->attr.is_class)
   17504              :     {
   17505         2255 :       char name[GFC_MAX_SYMBOL_LEN+9];
   17506         2255 :       gfc_component *strlen;
   17507         2255 :       sprintf (name, "_%s_length", c->name);
   17508         2255 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   17509         2255 :       if (strlen == NULL)
   17510              :         {
   17511          479 :           if (!gfc_add_component (sym, name, &strlen))
   17512            0 :             return false;
   17513          479 :           strlen->ts.type = BT_INTEGER;
   17514          479 :           strlen->ts.kind = gfc_charlen_int_kind;
   17515          479 :           strlen->attr.access = ACCESS_PRIVATE;
   17516          479 :           strlen->attr.artificial = 1;
   17517              :         }
   17518              :     }
   17519              : 
   17520       277943 :   if (c->ts.type == BT_DERIVED
   17521        51826 :       && sym->component_access != ACCESS_PRIVATE
   17522        50806 :       && gfc_check_symbol_access (sym)
   17523        99576 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   17524        49736 :       && !c->ts.u.derived->attr.use_assoc
   17525        26676 :       && !gfc_check_symbol_access (c->ts.u.derived)
   17526       278139 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   17527              :                           "PRIVATE type and cannot be a component of "
   17528              :                           "%qs, which is PUBLIC at %L", c->name,
   17529              :                           sym->name, &sym->declared_at))
   17530              :     return false;
   17531              : 
   17532       277942 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   17533              :     {
   17534            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   17535              :                  "type %s", c->name, &c->loc, sym->name);
   17536            2 :       return false;
   17537              :     }
   17538              : 
   17539       277940 :   if (sym->attr.sequence)
   17540              :     {
   17541         2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   17542              :         {
   17543            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   17544              :                      "not have the SEQUENCE attribute",
   17545              :                      c->ts.u.derived->name, &sym->declared_at);
   17546            0 :           return false;
   17547              :         }
   17548              :     }
   17549              : 
   17550       277940 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   17551            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   17552       277940 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   17553         7248 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   17554            0 :     CLASS_DATA (c)->ts.u.derived
   17555            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   17556              : 
   17557              :   /* If an allocatable component derived type is of the same type as
   17558              :      the enclosing derived type, we need a vtable generating so that
   17559              :      the __deallocate procedure is created.  */
   17560       277940 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   17561        59084 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   17562          399 :     gfc_find_vtab (&c->ts);
   17563              : 
   17564              :   /* Ensure that all the derived type components are put on the
   17565              :      derived type list; even in formal namespaces, where derived type
   17566              :      pointer components might not have been declared.  */
   17567       277940 :   if (c->ts.type == BT_DERIVED
   17568        51825 :       && c->ts.u.derived
   17569        51825 :       && c->ts.u.derived->components
   17570        48561 :       && c->attr.pointer
   17571        33315 :       && sym != c->ts.u.derived)
   17572         4248 :     add_dt_to_dt_list (c->ts.u.derived);
   17573              : 
   17574       277940 :   if (c->as && c->as->type != AS_DEFERRED
   17575         6285 :       && (c->attr.pointer || c->attr.allocatable))
   17576              :     return false;
   17577              : 
   17578       277926 :   if (!gfc_resolve_array_spec (c->as,
   17579       277926 :                                !(c->attr.pointer || c->attr.proc_pointer
   17580       226244 :                                  || c->attr.allocatable)))
   17581              :     return false;
   17582              : 
   17583       104832 :   if (c->initializer && !sym->attr.vtype
   17584        32017 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   17585       306857 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   17586              :     return false;
   17587              : 
   17588              :   return true;
   17589              : }
   17590              : 
   17591              : 
   17592              : /* Be nice about the locus for a structure expression - show the locus of the
   17593              :    first non-null sub-expression if we can.  */
   17594              : 
   17595              : static locus *
   17596            4 : cons_where (gfc_expr *struct_expr)
   17597              : {
   17598            4 :   gfc_constructor *cons;
   17599              : 
   17600            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   17601              : 
   17602            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   17603           12 :   for (; cons; cons = gfc_constructor_next (cons))
   17604              :     {
   17605            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   17606            4 :         return &cons->expr->where;
   17607              :     }
   17608              : 
   17609            0 :   return &struct_expr->where;
   17610              : }
   17611              : 
   17612              : /* Resolve the components of a structure type. Much less work than derived
   17613              :    types.  */
   17614              : 
   17615              : static bool
   17616          913 : resolve_fl_struct (gfc_symbol *sym)
   17617              : {
   17618          913 :   gfc_component *c;
   17619          913 :   gfc_expr *init = NULL;
   17620          913 :   bool success;
   17621              : 
   17622              :   /* Make sure UNIONs do not have overlapping initializers.  */
   17623          913 :   if (sym->attr.flavor == FL_UNION)
   17624              :     {
   17625          498 :       for (c = sym->components; c; c = c->next)
   17626              :         {
   17627          331 :           if (init && c->initializer)
   17628              :             {
   17629            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   17630              :                          cons_where (init), cons_where (c->initializer));
   17631            2 :               gfc_free_expr (c->initializer);
   17632            2 :               c->initializer = NULL;
   17633              :             }
   17634          291 :           if (init == NULL)
   17635          291 :             init = c->initializer;
   17636              :         }
   17637              :     }
   17638              : 
   17639          913 :   success = true;
   17640         2830 :   for (c = sym->components; c; c = c->next)
   17641         1917 :     if (!resolve_component (c, sym))
   17642            0 :       success = false;
   17643              : 
   17644          913 :   if (!success)
   17645              :     return false;
   17646              : 
   17647          913 :   if (sym->components)
   17648          862 :     add_dt_to_dt_list (sym);
   17649              : 
   17650              :   return true;
   17651              : }
   17652              : 
   17653              : /* Figure if the derived type is using itself directly in one of its components
   17654              :    or through referencing other derived types.  The information is required to
   17655              :    generate the __deallocate and __final type bound procedures to ensure
   17656              :    freeing larger hierarchies of derived types with allocatable objects.  */
   17657              : 
   17658              : static void
   17659       137362 : resolve_cyclic_derived_type (gfc_symbol *derived)
   17660              : {
   17661       137362 :   hash_set<gfc_symbol *> seen, to_examin;
   17662       137362 :   gfc_component *c;
   17663       137362 :   seen.add (derived);
   17664       137362 :   to_examin.add (derived);
   17665       460608 :   while (!to_examin.is_empty ())
   17666              :     {
   17667       188076 :       gfc_symbol *cand = *to_examin.begin ();
   17668       188076 :       to_examin.remove (cand);
   17669       506847 :       for (c = cand->components; c; c = c->next)
   17670       320963 :         if (c->ts.type == BT_DERIVED)
   17671              :           {
   17672        70801 :             if (c->ts.u.derived == derived)
   17673              :               {
   17674         1168 :                 derived->attr.recursive = 1;
   17675         2192 :                 return;
   17676              :               }
   17677        69633 :             else if (!seen.contains (c->ts.u.derived))
   17678              :               {
   17679        46178 :                 seen.add (c->ts.u.derived);
   17680        46178 :                 to_examin.add (c->ts.u.derived);
   17681              :               }
   17682              :           }
   17683       250162 :         else if (c->ts.type == BT_CLASS)
   17684              :           {
   17685         9560 :             if (!c->attr.class_ok)
   17686            7 :               continue;
   17687         9553 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   17688              :               {
   17689         1024 :                 derived->attr.recursive = 1;
   17690         1024 :                 return;
   17691              :               }
   17692         8529 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   17693              :               {
   17694         4764 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   17695         4764 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   17696              :               }
   17697              :           }
   17698              :     }
   17699       137362 : }
   17700              : 
   17701              : /* Resolve the components of a derived type. This does not have to wait until
   17702              :    resolution stage, but can be done as soon as the dt declaration has been
   17703              :    parsed.  */
   17704              : 
   17705              : static bool
   17706       169291 : resolve_fl_derived0 (gfc_symbol *sym)
   17707              : {
   17708       169291 :   gfc_symbol* super_type;
   17709       169291 :   gfc_component *c;
   17710       169291 :   gfc_formal_arglist *f;
   17711       169291 :   bool success;
   17712              : 
   17713       169291 :   if (sym->attr.unlimited_polymorphic)
   17714              :     return true;
   17715              : 
   17716       169291 :   super_type = gfc_get_derived_super_type (sym);
   17717              : 
   17718              :   /* F2008, C432.  */
   17719       169291 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   17720              :     {
   17721            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   17722              :                  "parent type %qs shall also have one", sym->name,
   17723              :                  &sym->declared_at, super_type->name);
   17724            2 :       return false;
   17725              :     }
   17726              : 
   17727              :   /* Ensure the extended type gets resolved before we do.  */
   17728        17389 :   if (super_type && !resolve_fl_derived0 (super_type))
   17729              :     return false;
   17730              : 
   17731              :   /* An ABSTRACT type must be extensible.  */
   17732       169283 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   17733              :     {
   17734            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   17735              :                  sym->name, &sym->declared_at);
   17736            2 :       return false;
   17737              :     }
   17738              : 
   17739              :   /* Resolving components below, may create vtabs for which the cyclic type
   17740              :      information needs to be present.  */
   17741       169281 :   if (!sym->attr.vtype)
   17742       137362 :     resolve_cyclic_derived_type (sym);
   17743              : 
   17744       169281 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   17745              :                            : sym->components;
   17746              : 
   17747              :   success = true;
   17748       574546 :   for ( ; c != NULL; c = c->next)
   17749       405265 :     if (!resolve_component (c, sym))
   17750           96 :       success = false;
   17751              : 
   17752       169281 :   if (!success)
   17753              :     return false;
   17754              : 
   17755              :   /* Now add the caf token field, where needed.  */
   17756       169195 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   17757          990 :       && !sym->attr.vtype)
   17758              :     {
   17759         2222 :       for (c = sym->components; c; c = c->next)
   17760         1431 :         if (!c->attr.dimension && !c->attr.codimension
   17761          795 :             && (c->attr.allocatable || c->attr.pointer))
   17762              :           {
   17763          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   17764          146 :             gfc_component *token;
   17765          146 :             sprintf (name, "_caf_%s", c->name);
   17766          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   17767          146 :             if (token == NULL)
   17768              :               {
   17769           82 :                 if (!gfc_add_component (sym, name, &token))
   17770            0 :                   return false;
   17771           82 :                 token->ts.type = BT_VOID;
   17772           82 :                 token->ts.kind = gfc_default_integer_kind;
   17773           82 :                 token->attr.access = ACCESS_PRIVATE;
   17774           82 :                 token->attr.artificial = 1;
   17775           82 :                 token->attr.caf_token = 1;
   17776              :               }
   17777          146 :             c->caf_token = token;
   17778              :           }
   17779              :     }
   17780              : 
   17781       169195 :   check_defined_assignments (sym);
   17782              : 
   17783       169195 :   if (!sym->attr.defined_assign_comp && super_type)
   17784        16382 :     sym->attr.defined_assign_comp
   17785        16382 :                         = super_type->attr.defined_assign_comp;
   17786              : 
   17787              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   17788              :      all DEFERRED bindings are overridden.  */
   17789        17382 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   17790         1397 :       && !sym->attr.is_class
   17791         3147 :       && !ensure_not_abstract (sym, super_type))
   17792              :     return false;
   17793              : 
   17794              :   /* Check that there is a component for every PDT parameter.  */
   17795       169189 :   if (sym->attr.pdt_template)
   17796              :     {
   17797         2340 :       for (f = sym->formal; f; f = f->next)
   17798              :         {
   17799         1362 :           if (!f->sym)
   17800            1 :             continue;
   17801         1361 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   17802         1361 :           if (c == NULL)
   17803              :             {
   17804            9 :               gfc_error ("Parameterized type %qs does not have a component "
   17805              :                          "corresponding to parameter %qs at %L", sym->name,
   17806            9 :                          f->sym->name, &sym->declared_at);
   17807            9 :               break;
   17808              :             }
   17809              :         }
   17810              :     }
   17811              : 
   17812              :   /* Add derived type to the derived type list.  */
   17813       169189 :   add_dt_to_dt_list (sym);
   17814              : 
   17815       169189 :   return true;
   17816              : }
   17817              : 
   17818              : /* The following procedure does the full resolution of a derived type,
   17819              :    including resolution of all type-bound procedures (if present). In contrast
   17820              :    to 'resolve_fl_derived0' this can only be done after the module has been
   17821              :    parsed completely.  */
   17822              : 
   17823              : static bool
   17824        87969 : resolve_fl_derived (gfc_symbol *sym)
   17825              : {
   17826        87969 :   gfc_symbol *gen_dt = NULL;
   17827              : 
   17828        87969 :   if (sym->attr.unlimited_polymorphic)
   17829              :     return true;
   17830              : 
   17831        87969 :   if (!sym->attr.is_class)
   17832        75333 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   17833        56335 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   17834         2289 :       && (!gen_dt->generic->sym->attr.use_assoc
   17835         2146 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   17836        88145 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   17837              :                           "%qs at %L being the same name as derived "
   17838              :                           "type at %L", sym->name,
   17839              :                           gen_dt->generic->sym == sym
   17840           11 :                           ? gen_dt->generic->next->sym->name
   17841              :                           : gen_dt->generic->sym->name,
   17842              :                           gen_dt->generic->sym == sym
   17843           11 :                           ? &gen_dt->generic->next->sym->declared_at
   17844              :                           : &gen_dt->generic->sym->declared_at,
   17845              :                           &sym->declared_at))
   17846              :     return false;
   17847              : 
   17848        87965 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   17849              :     {
   17850           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   17851              :                   sym->name, &sym->declared_at);
   17852           13 :       return false;
   17853              :     }
   17854              : 
   17855              :   /* Resolve the finalizer procedures.  */
   17856        87952 :   if (!gfc_resolve_finalizers (sym, NULL))
   17857              :     return false;
   17858              : 
   17859        87949 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   17860              :     {
   17861              :       /* Fix up incomplete CLASS symbols.  */
   17862        12636 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   17863        12636 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   17864              : 
   17865        12636 :       if (data->ts.u.derived->attr.pdt_template)
   17866              :         {
   17867            6 :           match m;
   17868            6 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   17869              :                                     &data->param_list);
   17870            6 :           if (m != MATCH_YES
   17871            6 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   17872              :             {
   17873            0 :               gfc_error ("Failed to build PDT class component at %L",
   17874              :                          &sym->declared_at);
   17875            0 :               return false;
   17876              :             }
   17877            6 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   17878            6 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   17879              :         }
   17880              : 
   17881              :       /* Nothing more to do for unlimited polymorphic entities.  */
   17882        12636 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   17883              :         {
   17884         2005 :           add_dt_to_dt_list (sym);
   17885         2005 :           return true;
   17886              :         }
   17887        10631 :       else if (vptr->ts.u.derived == NULL)
   17888              :         {
   17889         6274 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   17890         6274 :           gcc_assert (vtab);
   17891         6274 :           vptr->ts.u.derived = vtab->ts.u.derived;
   17892         6274 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   17893              :             return false;
   17894              :         }
   17895              :     }
   17896              : 
   17897        85944 :   if (!resolve_fl_derived0 (sym))
   17898              :     return false;
   17899              : 
   17900              :   /* Resolve the type-bound procedures.  */
   17901        85860 :   if (!resolve_typebound_procedures (sym))
   17902              :     return false;
   17903              : 
   17904              :   /* Generate module vtables subject to their accessibility and their not
   17905              :      being vtables or pdt templates. If this is not done class declarations
   17906              :      in external procedures wind up with their own version and so SELECT TYPE
   17907              :      fails because the vptrs do not have the same address.  */
   17908        85819 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   17909        85758 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   17910        64317 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   17911        21595 :       && sym->attr.access != ACCESS_PRIVATE
   17912        21562 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   17913              :     {
   17914        19398 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   17915        19398 :       gfc_set_sym_referenced (vtab);
   17916              :     }
   17917              : 
   17918              :   return true;
   17919              : }
   17920              : 
   17921              : 
   17922              : static bool
   17923          835 : resolve_fl_namelist (gfc_symbol *sym)
   17924              : {
   17925          835 :   gfc_namelist *nl;
   17926          835 :   gfc_symbol *nlsym;
   17927              : 
   17928         2984 :   for (nl = sym->namelist; nl; nl = nl->next)
   17929              :     {
   17930              :       /* Check again, the check in match only works if NAMELIST comes
   17931              :          after the decl.  */
   17932         2154 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   17933              :         {
   17934            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   17935              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   17936            1 :           return false;
   17937              :         }
   17938              : 
   17939          652 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   17940         2161 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   17941              :                               "with assumed shape in namelist %qs at %L",
   17942              :                               nl->sym->name, sym->name, &sym->declared_at))
   17943              :         return false;
   17944              : 
   17945         2152 :       if (is_non_constant_shape_array (nl->sym)
   17946         2202 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   17947              :                               "with nonconstant shape in namelist %qs at %L",
   17948           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   17949              :         return false;
   17950              : 
   17951         2151 :       if (nl->sym->ts.type == BT_CHARACTER
   17952          589 :           && (nl->sym->ts.u.cl->length == NULL
   17953          550 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   17954         2233 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   17955              :                               "nonconstant character length in "
   17956           82 :                               "namelist %qs at %L", nl->sym->name,
   17957              :                               sym->name, &sym->declared_at))
   17958              :         return false;
   17959              : 
   17960              :     }
   17961              : 
   17962              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   17963          830 :   if (gfc_check_symbol_access (sym))
   17964              :     {
   17965         2965 :       for (nl = sym->namelist; nl; nl = nl->next)
   17966              :         {
   17967         2148 :           if (!nl->sym->attr.use_assoc
   17968         4000 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   17969         4126 :               && !gfc_check_symbol_access (nl->sym))
   17970              :             {
   17971            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   17972              :                          "cannot be member of PUBLIC namelist %qs at %L",
   17973            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   17974            2 :               return false;
   17975              :             }
   17976              : 
   17977         2146 :           if (nl->sym->ts.type == BT_DERIVED
   17978          466 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   17979          464 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   17980              :            {
   17981            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   17982              :                                   "namelist %qs at %L with ALLOCATABLE "
   17983              :                                   "or POINTER components", nl->sym->name,
   17984              :                                   sym->name, &sym->declared_at))
   17985              :                return false;
   17986              :              return true;
   17987              :            }
   17988              : 
   17989              :           /* Types with private components that came here by USE-association.  */
   17990         2141 :           if (nl->sym->ts.type == BT_DERIVED
   17991         2141 :               && derived_inaccessible (nl->sym->ts.u.derived))
   17992              :             {
   17993            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   17994              :                          "components and cannot be member of namelist %qs at %L",
   17995              :                          nl->sym->name, sym->name, &sym->declared_at);
   17996            6 :               return false;
   17997              :             }
   17998              : 
   17999              :           /* Types with private components that are defined in the same module.  */
   18000         2135 :           if (nl->sym->ts.type == BT_DERIVED
   18001          910 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18002         2413 :               && nl->sym->ts.u.derived->attr.private_comp)
   18003              :             {
   18004            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18005              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18006              :                          nl->sym->name, sym->name, &sym->declared_at);
   18007            0 :               return false;
   18008              :             }
   18009              :         }
   18010              :     }
   18011              : 
   18012              : 
   18013              :   /* 14.1.2 A module or internal procedure represent local entities
   18014              :      of the same type as a namelist member and so are not allowed.  */
   18015         2949 :   for (nl = sym->namelist; nl; nl = nl->next)
   18016              :     {
   18017         2135 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18018         1576 :         continue;
   18019              : 
   18020          559 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18021            7 :         if ((nl->sym == sym->ns->proc_name)
   18022            1 :                ||
   18023            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18024            6 :           continue;
   18025              : 
   18026          553 :       nlsym = NULL;
   18027          553 :       if (nl->sym->name)
   18028          553 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18029          553 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18030              :         {
   18031            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18032              :                      "attribute in %qs at %L", nlsym->name,
   18033              :                      &sym->declared_at);
   18034            3 :           return false;
   18035              :         }
   18036              :     }
   18037              : 
   18038              :   return true;
   18039              : }
   18040              : 
   18041              : 
   18042              : static bool
   18043       381364 : resolve_fl_parameter (gfc_symbol *sym)
   18044              : {
   18045              :   /* A parameter array's shape needs to be constant.  */
   18046       381364 :   if (sym->as != NULL
   18047       381364 :       && (sym->as->type == AS_DEFERRED
   18048         6252 :           || is_non_constant_shape_array (sym)))
   18049              :     {
   18050           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18051              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18052           17 :       return false;
   18053              :     }
   18054              : 
   18055              :   /* Constraints on deferred type parameter.  */
   18056       381347 :   if (!deferred_requirements (sym))
   18057              :     return false;
   18058              : 
   18059              :   /* Make sure a parameter that has been implicitly typed still
   18060              :      matches the implicit type, since PARAMETER statements can precede
   18061              :      IMPLICIT statements.  */
   18062       381346 :   if (sym->attr.implicit_type
   18063       382059 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18064          713 :                                                              sym->ns)))
   18065              :     {
   18066            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18067              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18068            0 :       return false;
   18069              :     }
   18070              : 
   18071              :   /* Make sure the types of derived parameters are consistent.  This
   18072              :      type checking is deferred until resolution because the type may
   18073              :      refer to a derived type from the host.  */
   18074       381346 :   if (sym->ts.type == BT_DERIVED
   18075       381346 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18076              :     {
   18077            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18078            0 :                  &sym->value->where);
   18079            0 :       return false;
   18080              :     }
   18081              : 
   18082              :   /* F03:C509,C514.  */
   18083       381346 :   if (sym->ts.type == BT_CLASS)
   18084              :     {
   18085            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18086              :                  sym->name, &sym->declared_at);
   18087            0 :       return false;
   18088              :     }
   18089              : 
   18090              :   /* Some programmers can have a typo when using an implied-do loop to
   18091              :      initialize an array constant.  For example,
   18092              :        INTEGER I,J
   18093              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18094              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18095              :      This check catches the typo.  */
   18096       381346 :   if (sym->attr.dimension
   18097         6245 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18098       387587 :       && !gfc_is_constant_expr (sym->value))
   18099              :     {
   18100              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18101              :          the array constructor of a paramater.  This seems inconsistant with
   18102              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18103           20 :       if (sym->value->ts.type == BT_DERIVED
   18104           18 :           && sym->value->ts.u.derived
   18105           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18106              :         return true;
   18107            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18108            2 :       return false;
   18109              :     }
   18110              : 
   18111              :   return true;
   18112              : }
   18113              : 
   18114              : 
   18115              : /* Called by resolve_symbol to check PDTs.  */
   18116              : 
   18117              : static void
   18118         1377 : resolve_pdt (gfc_symbol* sym)
   18119              : {
   18120         1377 :   gfc_symbol *derived = NULL;
   18121         1377 :   gfc_actual_arglist *param;
   18122         1377 :   gfc_component *c;
   18123         1377 :   bool const_len_exprs = true;
   18124         1377 :   bool assumed_len_exprs = false;
   18125         1377 :   symbol_attribute *attr;
   18126              : 
   18127         1377 :   if (sym->ts.type == BT_DERIVED)
   18128              :     {
   18129         1150 :       derived = sym->ts.u.derived;
   18130         1150 :       attr = &(sym->attr);
   18131              :     }
   18132          227 :   else if (sym->ts.type == BT_CLASS)
   18133              :     {
   18134          227 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18135          227 :       attr = &(CLASS_DATA (sym)->attr);
   18136              :     }
   18137              :   else
   18138            0 :     gcc_unreachable ();
   18139              : 
   18140         1377 :   gcc_assert (derived->attr.pdt_type);
   18141              : 
   18142         3276 :   for (param = sym->param_list; param; param = param->next)
   18143              :     {
   18144         1899 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18145         1899 :       gcc_assert (c);
   18146         1899 :       if (c->attr.pdt_kind)
   18147         1016 :         continue;
   18148              : 
   18149          614 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18150          967 :           && c->attr.pdt_len)
   18151              :         const_len_exprs = false;
   18152          799 :       else if (param->spec_type == SPEC_ASSUMED)
   18153          291 :         assumed_len_exprs = true;
   18154              : 
   18155          883 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18156           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18157           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18158            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18159              :                    "parameter %qs and requires either the POINTER "
   18160              :                    "or ALLOCATABLE attribute",
   18161              :                    sym->name, &sym->declared_at,
   18162              :                    param->name);
   18163              : 
   18164              :     }
   18165              : 
   18166         1377 :   if (!const_len_exprs
   18167           84 :       && (sym->ns->proc_name->attr.is_main_program
   18168           83 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18169           82 :           || sym->attr.save != SAVE_NONE))
   18170            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18171              :                "SAVE attribute or be a variable declared in the "
   18172              :                "main program, a module or a submodule(F08/C513)",
   18173              :                sym->name, &sym->declared_at);
   18174              : 
   18175         1377 :   if (assumed_len_exprs && !(sym->attr.dummy
   18176            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18177            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18178              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18179              :                sym->name, &sym->declared_at);
   18180         1377 : }
   18181              : 
   18182              : 
   18183              : /* Resolve the symbol's array spec.  */
   18184              : 
   18185              : static bool
   18186      1689634 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18187              : {
   18188      1689634 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18189      1689634 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18190              : 
   18191      1689634 :   bool saved_specification_expr = specification_expr;
   18192      1689634 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18193      1689634 :   specification_expr = true;
   18194      1689634 :   specification_expr_symbol = sym;
   18195              : 
   18196      1689634 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18197              : 
   18198      1689634 :   specification_expr = saved_specification_expr;
   18199      1689634 :   specification_expr_symbol = saved_specification_expr_symbol;
   18200      1689634 :   gfc_current_ns = orig_current_ns;
   18201              : 
   18202      1689634 :   return result;
   18203              : }
   18204              : 
   18205              : 
   18206              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18207              :    assume that an otherwise unknown symbol is a variable.  This sort
   18208              :    of thing commonly happens for symbols in module.  */
   18209              : 
   18210              : static void
   18211      1830961 : resolve_symbol (gfc_symbol *sym)
   18212              : {
   18213      1830961 :   int check_constant, mp_flag;
   18214      1830961 :   gfc_symtree *symtree;
   18215      1830961 :   gfc_symtree *this_symtree;
   18216      1830961 :   gfc_namespace *ns;
   18217      1830961 :   gfc_component *c;
   18218      1830961 :   symbol_attribute class_attr;
   18219      1830961 :   gfc_array_spec *as;
   18220              : 
   18221      1830961 :   if (sym->resolve_symbol_called >= 1)
   18222       172636 :     return;
   18223      1756951 :   sym->resolve_symbol_called = 1;
   18224              : 
   18225              :   /* No symbol will ever have union type; only components can be unions.
   18226              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18227              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18228      1756951 :   gcc_assert (sym->ts.type != BT_UNION);
   18229              : 
   18230              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18231              :      yet unsupported for -fcoarray=lib.  */
   18232      1756951 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18233          112 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18234          112 :       && CLASS_DATA (sym)->attr.codimension
   18235           94 :       && CLASS_DATA (sym)->ts.u.derived
   18236           93 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18237           90 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18238              :     {
   18239            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18240              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18241            6 :       return;
   18242              :     }
   18243              : 
   18244      1756945 :   if (sym->attr.artificial)
   18245              :     return;
   18246              : 
   18247      1660992 :   if (sym->attr.unlimited_polymorphic)
   18248              :     return;
   18249              : 
   18250      1659535 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18251              :     {
   18252            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18253              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18254            4 :       return;
   18255              :     }
   18256              : 
   18257      1659531 :   if (sym->attr.flavor == FL_UNKNOWN
   18258      1638308 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18259       442110 :           && !sym->attr.generic && !sym->attr.external
   18260       179743 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18261        80861 :           && sym->ts.type == BT_UNKNOWN))
   18262              :     {
   18263              :       /* A symbol in a common block might not have been resolved yet properly.
   18264              :          Do not try to find an interface with the same name.  */
   18265        93619 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18266        21219 :           && !sym->attr.generic && !sym->attr.external
   18267        21168 :           && sym->attr.in_common)
   18268         2594 :         goto skip_interfaces;
   18269              : 
   18270              :     /* If we find that a flavorless symbol is an interface in one of the
   18271              :        parent namespaces, find its symtree in this namespace, free the
   18272              :        symbol and set the symtree to point to the interface symbol.  */
   18273       129924 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18274              :         {
   18275        39577 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   18276        39577 :           if (symtree && (symtree->n.sym->generic ||
   18277          724 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   18278          634 :                            && sym->ns->construct_entities)))
   18279              :             {
   18280          686 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   18281              :                                                sym->name);
   18282          686 :               if (this_symtree->n.sym == sym)
   18283              :                 {
   18284          678 :                   symtree->n.sym->refs++;
   18285          678 :                   gfc_release_symbol (sym);
   18286          678 :                   this_symtree->n.sym = symtree->n.sym;
   18287          678 :                   return;
   18288              :                 }
   18289              :             }
   18290              :         }
   18291              : 
   18292        90347 : skip_interfaces:
   18293              :       /* Otherwise give it a flavor according to such attributes as
   18294              :          it has.  */
   18295        92941 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   18296        21038 :           && sym->attr.intrinsic == 0)
   18297        21034 :         sym->attr.flavor = FL_VARIABLE;
   18298        71907 :       else if (sym->attr.flavor == FL_UNKNOWN)
   18299              :         {
   18300           55 :           sym->attr.flavor = FL_PROCEDURE;
   18301           55 :           if (sym->attr.dimension)
   18302            0 :             sym->attr.function = 1;
   18303              :         }
   18304              :     }
   18305              : 
   18306      1658853 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   18307         2304 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   18308              : 
   18309         1492 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   18310      1660345 :       && !resolve_procedure_interface (sym))
   18311              :     return;
   18312              : 
   18313      1658842 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   18314          130 :       && (sym->attr.procedure || sym->attr.external))
   18315              :     {
   18316            0 :       if (sym->attr.external)
   18317            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   18318              :                    "at %L", &sym->declared_at);
   18319              :       else
   18320            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   18321              :                    "at %L", &sym->declared_at);
   18322              : 
   18323            0 :       return;
   18324              :     }
   18325              : 
   18326              :   /* Ensure that variables of derived or class type having a finalizer are
   18327              :      marked used even when the variable is not used anything else in the scope.
   18328              :      This fixes PR118730.  */
   18329       647142 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   18330       442301 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   18331      1708200 :       && gfc_may_be_finalized (sym->ts))
   18332         8464 :     gfc_set_sym_referenced (sym);
   18333              : 
   18334      1658842 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   18335              :     return;
   18336              : 
   18337      1658066 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   18338      1658829 :            && !resolve_fl_struct (sym))
   18339              :     return;
   18340              : 
   18341              :   /* Symbols that are module procedures with results (functions) have
   18342              :      the types and array specification copied for type checking in
   18343              :      procedures that call them, as well as for saving to a module
   18344              :      file.  These symbols can't stand the scrutiny that their results
   18345              :      can.  */
   18346      1658697 :   mp_flag = (sym->result != NULL && sym->result != sym);
   18347              : 
   18348              :   /* Make sure that the intrinsic is consistent with its internal
   18349              :      representation. This needs to be done before assigning a default
   18350              :      type to avoid spurious warnings.  */
   18351      1624708 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   18352      1691227 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   18353              :     return;
   18354              : 
   18355              :   /* Resolve associate names.  */
   18356      1658661 :   if (sym->assoc)
   18357         6748 :     resolve_assoc_var (sym, true);
   18358              : 
   18359              :   /* Assign default type to symbols that need one and don't have one.  */
   18360      1658661 :   if (sym->ts.type == BT_UNKNOWN)
   18361              :     {
   18362       400079 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   18363              :         {
   18364        11765 :           gfc_set_default_type (sym, 1, NULL);
   18365              :         }
   18366              : 
   18367       258280 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   18368        61005 :           && !sym->attr.function && !sym->attr.subroutine
   18369       401698 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   18370          568 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   18371              : 
   18372       400079 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18373              :         {
   18374              :           /* The specific case of an external procedure should emit an error
   18375              :              in the case that there is no implicit type.  */
   18376       101992 :           if (!mp_flag)
   18377              :             {
   18378        96000 :               if (!sym->attr.mixed_entry_master)
   18379        95892 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   18380              :             }
   18381              :           else
   18382              :             {
   18383              :               /* Result may be in another namespace.  */
   18384         5992 :               resolve_symbol (sym->result);
   18385              : 
   18386         5992 :               if (!sym->result->attr.proc_pointer)
   18387              :                 {
   18388         5813 :                   sym->ts = sym->result->ts;
   18389         5813 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   18390         5813 :                   sym->attr.dimension = sym->result->attr.dimension;
   18391         5813 :                   sym->attr.codimension = sym->result->attr.codimension;
   18392         5813 :                   sym->attr.pointer = sym->result->attr.pointer;
   18393         5813 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   18394         5813 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   18395              :                 }
   18396              :             }
   18397              :         }
   18398              :     }
   18399      1258582 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18400        31306 :     resolve_symbol_array_spec (sym->result, false);
   18401              : 
   18402              :   /* For a CLASS-valued function with a result variable, affirm that it has
   18403              :      been resolved also when looking at the symbol 'sym'.  */
   18404       431385 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   18405          720 :     sym->attr.class_ok = sym->result->attr.class_ok;
   18406              : 
   18407      1658661 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   18408        19340 :       && CLASS_DATA (sym))
   18409              :     {
   18410        19339 :       as = CLASS_DATA (sym)->as;
   18411        19339 :       class_attr = CLASS_DATA (sym)->attr;
   18412        19339 :       class_attr.pointer = class_attr.class_pointer;
   18413              :     }
   18414              :   else
   18415              :     {
   18416      1639322 :       class_attr = sym->attr;
   18417      1639322 :       as = sym->as;
   18418              :     }
   18419              : 
   18420              :   /* F2008, C530.  */
   18421      1658661 :   if (sym->attr.contiguous
   18422         7717 :       && !sym->attr.associate_var
   18423         7716 :       && (!class_attr.dimension
   18424         7713 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   18425          128 :               && !class_attr.pointer)))
   18426              :     {
   18427            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   18428              :                  "array pointer or an assumed-shape or assumed-rank array",
   18429              :                  sym->name, &sym->declared_at);
   18430            7 :       return;
   18431              :     }
   18432              : 
   18433              :   /* Assumed size arrays and assumed shape arrays must be dummy
   18434              :      arguments.  Array-spec's of implied-shape should have been resolved to
   18435              :      AS_EXPLICIT already.  */
   18436              : 
   18437      1651069 :   if (as)
   18438              :     {
   18439              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   18440              :          specification expression.  */
   18441       145512 :       if (as->type == AS_IMPLIED_SHAPE)
   18442              :         {
   18443              :           int i;
   18444            1 :           for (i=0; i<as->rank; i++)
   18445              :             {
   18446            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   18447              :                 {
   18448            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   18449              :                              &as->lower[i]->where);
   18450            1 :                   return;
   18451              :                 }
   18452              :             }
   18453            0 :           gcc_unreachable();
   18454              :         }
   18455              : 
   18456       145511 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   18457       112610 :            || as->type == AS_ASSUMED_SHAPE)
   18458        44533 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   18459            8 :           && !sym->attr.associate_var)
   18460              :         {
   18461            7 :           if (as->type == AS_ASSUMED_SIZE)
   18462            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   18463              :                        &sym->declared_at);
   18464              :           else
   18465            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   18466              :                        &sym->declared_at);
   18467            7 :           return;
   18468              :         }
   18469              :       /* TS 29113, C535a.  */
   18470       145504 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   18471           60 :           && !sym->attr.select_type_temporary
   18472           60 :           && !(cs_base && cs_base->current
   18473           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   18474            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   18475            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   18476              :         {
   18477           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   18478              :                      &sym->declared_at);
   18479           18 :           return;
   18480              :         }
   18481       145486 :       if (as->type == AS_ASSUMED_RANK
   18482        26256 :           && (sym->attr.codimension || sym->attr.value))
   18483              :         {
   18484            2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   18485              :                      "CODIMENSION attribute", &sym->declared_at);
   18486            2 :           return;
   18487              :         }
   18488              :     }
   18489              : 
   18490              :   /* Make sure symbols with known intent or optional are really dummy
   18491              :      variable.  Because of ENTRY statement, this has to be deferred
   18492              :      until resolution time.  */
   18493              : 
   18494      1658626 :   if (!sym->attr.dummy
   18495      1193078 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   18496              :     {
   18497            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   18498            2 :       return;
   18499              :     }
   18500              : 
   18501      1658624 :   if (sym->attr.value && !sym->attr.dummy)
   18502              :     {
   18503            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   18504              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   18505            2 :       return;
   18506              :     }
   18507              : 
   18508      1658622 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   18509              :     {
   18510          616 :       gfc_charlen *cl = sym->ts.u.cl;
   18511          616 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   18512              :         {
   18513            2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   18514              :                      "attribute must have constant length",
   18515              :                      sym->name, &sym->declared_at);
   18516            2 :           return;
   18517              :         }
   18518              : 
   18519          614 :       if (sym->ts.is_c_interop
   18520          381 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   18521              :         {
   18522            1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   18523              :                      "with VALUE attribute must have length one",
   18524              :                      sym->name, &sym->declared_at);
   18525            1 :           return;
   18526              :         }
   18527              :     }
   18528              : 
   18529      1658619 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18530       122941 :       && sym->ts.u.derived->attr.generic)
   18531              :     {
   18532           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   18533           20 :       if (!sym->ts.u.derived)
   18534              :         {
   18535            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   18536              :                      "which has not been defined", sym->name,
   18537              :                      &sym->declared_at, sym->ts.u.derived->name);
   18538            0 :           sym->ts.type = BT_UNKNOWN;
   18539            0 :           return;
   18540              :         }
   18541              :     }
   18542              : 
   18543              :     /* Use the same constraints as TYPE(*), except for the type check
   18544              :        and that only scalars and assumed-size arrays are permitted.  */
   18545      1658619 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   18546              :       {
   18547        12960 :         if (!sym->attr.dummy)
   18548              :           {
   18549            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18550              :                        "a dummy argument", sym->name, &sym->declared_at);
   18551            1 :             return;
   18552              :           }
   18553              : 
   18554        12959 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   18555            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   18556            0 :             && sym->ts.type != BT_COMPLEX)
   18557              :           {
   18558            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18559              :                        "of type TYPE(*) or of an numeric intrinsic type",
   18560              :                        sym->name, &sym->declared_at);
   18561            0 :             return;
   18562              :           }
   18563              : 
   18564        12959 :       if (sym->attr.allocatable || sym->attr.codimension
   18565        12957 :           || sym->attr.pointer || sym->attr.value)
   18566              :         {
   18567            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18568              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   18569              :                      "attribute", sym->name, &sym->declared_at);
   18570            4 :           return;
   18571              :         }
   18572              : 
   18573        12955 :       if (sym->attr.intent == INTENT_OUT)
   18574              :         {
   18575            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18576              :                      "have the INTENT(OUT) attribute",
   18577              :                      sym->name, &sym->declared_at);
   18578            0 :           return;
   18579              :         }
   18580        12955 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   18581              :         {
   18582            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   18583              :                      "either be a scalar or an assumed-size array",
   18584              :                      sym->name, &sym->declared_at);
   18585            1 :           return;
   18586              :         }
   18587              : 
   18588              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   18589              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   18590              :          packing.  */
   18591        12954 :       sym->ts.type = BT_ASSUMED;
   18592        12954 :       sym->as = gfc_get_array_spec ();
   18593        12954 :       sym->as->type = AS_ASSUMED_SIZE;
   18594        12954 :       sym->as->rank = 1;
   18595        12954 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   18596              :     }
   18597      1645659 :   else if (sym->ts.type == BT_ASSUMED)
   18598              :     {
   18599              :       /* TS 29113, C407a.  */
   18600        11006 :       if (!sym->attr.dummy)
   18601              :         {
   18602            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   18603              :                      "for dummy variables", sym->name, &sym->declared_at);
   18604            7 :           return;
   18605              :         }
   18606        10999 :       if (sym->attr.allocatable || sym->attr.codimension
   18607        10995 :           || sym->attr.pointer || sym->attr.value)
   18608              :         {
   18609            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18610              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   18611              :                      sym->name, &sym->declared_at);
   18612            8 :           return;
   18613              :         }
   18614        10991 :       if (sym->attr.intent == INTENT_OUT)
   18615              :         {
   18616            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18617              :                      "INTENT(OUT) attribute",
   18618              :                      sym->name, &sym->declared_at);
   18619            2 :           return;
   18620              :         }
   18621        10989 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   18622              :         {
   18623            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   18624              :                      "explicit-shape array", sym->name, &sym->declared_at);
   18625            3 :           return;
   18626              :         }
   18627              :     }
   18628              : 
   18629              :   /* If the symbol is marked as bind(c), that it is declared at module level
   18630              :      scope and verify its type and kind.  Do not do the latter for symbols
   18631              :      that are implicitly typed because that is handled in
   18632              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   18633              :      separately.  Also, anything that is use associated is not handled here
   18634              :      but instead is handled in the module it is declared in.  Finally, derived
   18635              :      type definitions are allowed to be BIND(C) since that only implies that
   18636              :      they're interoperable, and they are checked fully for interoperability
   18637              :      when a variable is declared of that type.  */
   18638      1658593 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   18639         7282 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   18640          567 :       && sym->attr.flavor != FL_DERIVED)
   18641              :     {
   18642          167 :       bool t = true;
   18643              : 
   18644              :       /* First, make sure the variable is declared at the
   18645              :          module-level scope (J3/04-007, Section 15.3).  */
   18646          167 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   18647            7 :           && !sym->attr.in_common)
   18648              :         {
   18649            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   18650              :                      "is neither a COMMON block nor declared at the "
   18651              :                      "module level scope", sym->name, &(sym->declared_at));
   18652            6 :           t = false;
   18653              :         }
   18654          161 :       else if (sym->ts.type == BT_CHARACTER
   18655          161 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   18656            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   18657            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   18658              :         {
   18659            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   18660            1 :                      sym->name, &sym->declared_at);
   18661            1 :           t = false;
   18662              :         }
   18663          160 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   18664              :         {
   18665            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   18666              :         }
   18667          159 :       else if (sym->attr.implicit_type == 0)
   18668              :         {
   18669              :           /* If type() declaration, we need to verify that the components
   18670              :              of the given type are all C interoperable, etc.  */
   18671          157 :           if (sym->ts.type == BT_DERIVED &&
   18672           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   18673              :             {
   18674              :               /* Make sure the user marked the derived type as BIND(C).  If
   18675              :                  not, call the verify routine.  This could print an error
   18676              :                  for the derived type more than once if multiple variables
   18677              :                  of that type are declared.  */
   18678           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   18679            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   18680          157 :               t = false;
   18681              :             }
   18682              : 
   18683              :           /* Verify the variable itself as C interoperable if it
   18684              :              is BIND(C).  It is not possible for this to succeed if
   18685              :              the verify_bind_c_derived_type failed, so don't have to handle
   18686              :              any error returned by verify_bind_c_derived_type.  */
   18687          157 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   18688          157 :                                  sym->common_block);
   18689              :         }
   18690              : 
   18691          165 :       if (!t)
   18692              :         {
   18693              :           /* clear the is_bind_c flag to prevent reporting errors more than
   18694              :              once if something failed.  */
   18695           10 :           sym->attr.is_bind_c = 0;
   18696           10 :           return;
   18697              :         }
   18698              :     }
   18699              : 
   18700              :   /* If a derived type symbol has reached this point, without its
   18701              :      type being declared, we have an error.  Notice that most
   18702              :      conditions that produce undefined derived types have already
   18703              :      been dealt with.  However, the likes of:
   18704              :      implicit type(t) (t) ..... call foo (t) will get us here if
   18705              :      the type is not declared in the scope of the implicit
   18706              :      statement. Change the type to BT_UNKNOWN, both because it is so
   18707              :      and to prevent an ICE.  */
   18708      1658583 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18709       122939 :       && sym->ts.u.derived->components == NULL
   18710         1138 :       && !sym->ts.u.derived->attr.zero_comp)
   18711              :     {
   18712            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   18713              :                  "which has not been defined", sym->name,
   18714              :                   &sym->declared_at, sym->ts.u.derived->name);
   18715            3 :       sym->ts.type = BT_UNKNOWN;
   18716            3 :       return;
   18717              :     }
   18718              : 
   18719              :   /* Make sure that the derived type has been resolved and that the
   18720              :      derived type is visible in the symbol's namespace, if it is a
   18721              :      module function and is not PRIVATE.  */
   18722      1658580 :   if (sym->ts.type == BT_DERIVED
   18723       129878 :         && sym->ts.u.derived->attr.use_assoc
   18724       112510 :         && sym->ns->proc_name
   18725       112502 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   18726      1664459 :         && !resolve_fl_derived (sym->ts.u.derived))
   18727              :     return;
   18728              : 
   18729              :   /* Unless the derived-type declaration is use associated, Fortran 95
   18730              :      does not allow public entries of private derived types.
   18731              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   18732              :      161 in 95-006r3.  */
   18733      1658580 :   if (sym->ts.type == BT_DERIVED
   18734       129878 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   18735         7953 :       && !sym->ts.u.derived->attr.use_assoc
   18736         2074 :       && gfc_check_symbol_access (sym)
   18737         1861 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   18738      1658594 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   18739              :                           "derived type %qs",
   18740           14 :                           (sym->attr.flavor == FL_PARAMETER)
   18741              :                           ? "parameter" : "variable",
   18742              :                           sym->name, &sym->declared_at,
   18743           14 :                           sym->ts.u.derived->name))
   18744              :     return;
   18745              : 
   18746              :   /* F2008, C1302.  */
   18747      1658573 :   if (sym->ts.type == BT_DERIVED
   18748       129871 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   18749          154 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   18750       129840 :           || sym->ts.u.derived->attr.lock_comp)
   18751           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   18752              :     {
   18753            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   18754              :                  "type LOCK_TYPE must be a coarray", sym->name,
   18755              :                  &sym->declared_at);
   18756            4 :       return;
   18757              :     }
   18758              : 
   18759              :   /* TS18508, C702/C703.  */
   18760      1658569 :   if (sym->ts.type == BT_DERIVED
   18761       129867 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   18762          153 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   18763       129850 :           || sym->ts.u.derived->attr.event_comp)
   18764           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   18765              :     {
   18766            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   18767              :                  "type EVENT_TYPE must be a coarray", sym->name,
   18768              :                  &sym->declared_at);
   18769            1 :       return;
   18770              :     }
   18771              : 
   18772              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   18773              :      default initialization is defined (5.1.2.4.4).  */
   18774      1658568 :   if (sym->ts.type == BT_DERIVED
   18775       129866 :       && sym->attr.dummy
   18776        44740 :       && sym->attr.intent == INTENT_OUT
   18777         2356 :       && sym->as
   18778          381 :       && sym->as->type == AS_ASSUMED_SIZE)
   18779              :     {
   18780            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   18781              :         {
   18782            1 :           if (c->initializer)
   18783              :             {
   18784            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   18785              :                          "ASSUMED SIZE and so cannot have a default initializer",
   18786              :                          sym->name, &sym->declared_at);
   18787            1 :               return;
   18788              :             }
   18789              :         }
   18790              :     }
   18791              : 
   18792              :   /* F2008, C542.  */
   18793      1658567 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   18794        44739 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   18795              :     {
   18796            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   18797              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   18798            0 :       return;
   18799              :     }
   18800              : 
   18801              :   /* TS18508.  */
   18802      1658567 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   18803        44739 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   18804              :     {
   18805            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   18806              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   18807            0 :       return;
   18808              :     }
   18809              : 
   18810              :   /* F2008, C525.  */
   18811      1658567 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18812      1658467 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18813        19343 :              && sym->ts.u.derived && CLASS_DATA (sym)
   18814        19337 :              && CLASS_DATA (sym)->attr.coarray_comp))
   18815      1658467 :        || class_attr.codimension)
   18816         1774 :       && (sym->attr.result || sym->result == sym))
   18817              :     {
   18818            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   18819              :                  "a coarray component", sym->name, &sym->declared_at);
   18820            8 :       return;
   18821              :     }
   18822              : 
   18823              :   /* F2008, C524.  */
   18824      1658559 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   18825          411 :       && sym->ts.u.derived->ts.is_iso_c)
   18826              :     {
   18827            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   18828              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   18829            3 :       return;
   18830              :     }
   18831              : 
   18832              :   /* F2008, C525.  */
   18833      1658556 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18834      1658459 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18835        19342 :             && sym->ts.u.derived && CLASS_DATA (sym)
   18836        19336 :             && CLASS_DATA (sym)->attr.coarray_comp))
   18837           97 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   18838           93 :           || class_attr.allocatable))
   18839              :     {
   18840            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   18841              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   18842              :                  sym->name, &sym->declared_at);
   18843            4 :       return;
   18844              :     }
   18845              : 
   18846              :   /* F2008, C526.  The function-result case was handled above.  */
   18847      1658552 :   if (class_attr.codimension
   18848         1666 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   18849          349 :            || sym->attr.select_type_temporary
   18850          273 :            || sym->attr.associate_var
   18851          255 :            || (sym->ns->save_all && !sym->attr.automatic)
   18852          255 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   18853          255 :            || sym->ns->proc_name->attr.is_main_program
   18854            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   18855              :     {
   18856            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   18857              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   18858            4 :       return;
   18859              :     }
   18860              :   /* F2008, C528.  */
   18861      1658548 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   18862         1586 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   18863              :     {
   18864            6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   18865              :                  "deferred shape without allocatable", sym->name,
   18866              :                  &sym->declared_at);
   18867            6 :       return;
   18868              :     }
   18869      1658542 :   else if (class_attr.codimension && class_attr.allocatable && as
   18870          611 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   18871              :     {
   18872            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   18873              :                  "deferred shape", sym->name, &sym->declared_at);
   18874            9 :       return;
   18875              :     }
   18876              : 
   18877              :   /* F2008, C541.  */
   18878      1658533 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18879      1658440 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18880        19337 :             && sym->ts.u.derived && CLASS_DATA (sym)
   18881        19331 :             && CLASS_DATA (sym)->attr.coarray_comp))
   18882      1658440 :        || (class_attr.codimension && class_attr.allocatable))
   18883          695 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   18884              :     {
   18885            3 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   18886              :                  "allocatable coarray or have coarray components",
   18887              :                  sym->name, &sym->declared_at);
   18888            3 :       return;
   18889              :     }
   18890              : 
   18891      1658530 :   if (class_attr.codimension && sym->attr.dummy
   18892          469 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   18893              :     {
   18894            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   18895              :                  "procedure %qs", sym->name, &sym->declared_at,
   18896              :                  sym->ns->proc_name->name);
   18897            2 :       return;
   18898              :     }
   18899              : 
   18900      1658528 :   if (sym->ts.type == BT_LOGICAL
   18901       112066 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   18902       112063 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   18903        30954 :               && sym->ns->proc_name->attr.is_bind_c)))
   18904              :     {
   18905              :       int i;
   18906          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   18907          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   18908              :           break;
   18909           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   18910          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   18911              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   18912              :                               "%qs", sym->name, &sym->declared_at,
   18913           13 :                               sym->ns->proc_name->name))
   18914              :         return;
   18915          167 :       else if (!gfc_logical_kinds[i].c_bool
   18916          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   18917              :                                    "%qs at %L with non-C_Bool kind in "
   18918              :                                    "BIND(C) procedure %qs", sym->name,
   18919              :                                    &sym->declared_at,
   18920           15 :                                    sym->attr.function ? sym->name
   18921           13 :                                    : sym->ns->proc_name->name))
   18922              :         return;
   18923              :     }
   18924              : 
   18925      1658525 :   switch (sym->attr.flavor)
   18926              :     {
   18927       647025 :     case FL_VARIABLE:
   18928       647025 :       if (!resolve_fl_variable (sym, mp_flag))
   18929              :         return;
   18930              :       break;
   18931              : 
   18932       474086 :     case FL_PROCEDURE:
   18933       474086 :       if (sym->formal && !sym->formal_ns)
   18934              :         {
   18935              :           /* Check that none of the arguments are a namelist.  */
   18936              :           gfc_formal_arglist *formal = sym->formal;
   18937              : 
   18938       105203 :           for (; formal; formal = formal->next)
   18939        71422 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   18940              :               {
   18941            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   18942              :                            "subroutine or function at %L",
   18943              :                            formal->sym->name, &sym->declared_at);
   18944            1 :                 return;
   18945              :               }
   18946              :         }
   18947              : 
   18948       474085 :       if (!resolve_fl_procedure (sym, mp_flag))
   18949              :         return;
   18950              :       break;
   18951              : 
   18952          835 :     case FL_NAMELIST:
   18953          835 :       if (!resolve_fl_namelist (sym))
   18954              :         return;
   18955              :       break;
   18956              : 
   18957       381364 :     case FL_PARAMETER:
   18958       381364 :       if (!resolve_fl_parameter (sym))
   18959              :         return;
   18960              :       break;
   18961              : 
   18962              :     default:
   18963              :       break;
   18964              :     }
   18965              : 
   18966              :   /* Resolve array specifier. Check as well some constraints
   18967              :      on COMMON blocks.  */
   18968              : 
   18969      1658328 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   18970              : 
   18971      1658328 :   resolve_symbol_array_spec (sym, check_constant);
   18972              : 
   18973              :   /* Resolve formal namespaces.  */
   18974      1658328 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   18975       259238 :       && !sym->attr.contained && !sym->attr.intrinsic)
   18976       234019 :     gfc_resolve (sym->formal_ns);
   18977              : 
   18978              :   /* Make sure the formal namespace is present.  */
   18979      1658328 :   if (sym->formal && !sym->formal_ns)
   18980              :     {
   18981              :       gfc_formal_arglist *formal = sym->formal;
   18982        34225 :       while (formal && !formal->sym)
   18983           11 :         formal = formal->next;
   18984              : 
   18985        34214 :       if (formal)
   18986              :         {
   18987        34203 :           sym->formal_ns = formal->sym->ns;
   18988        34203 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   18989        25896 :             sym->formal_ns->refs++;
   18990              :         }
   18991              :     }
   18992              : 
   18993              :   /* Check threadprivate restrictions.  */
   18994      1658328 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   18995          384 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   18996           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   18997           32 :       && sym->module == NULL
   18998           17 :       && (sym->ns->proc_name == NULL
   18999           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19000            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19001              :     {
   19002            2 :       if (sym->attr.threadprivate)
   19003            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19004              :       else
   19005            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19006              :                    "attribute", sym->name, &sym->declared_at);
   19007              :     }
   19008              : 
   19009      1658328 :   if (sym->attr.omp_groupprivate && sym->value)
   19010            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19011              :                "initializer", sym->name, &sym->declared_at);
   19012              : 
   19013              :   /* Check omp declare target restrictions.  */
   19014      1658328 :   if ((sym->attr.omp_declare_target
   19015      1656912 :        || sym->attr.omp_declare_target_link
   19016      1656864 :        || sym->attr.omp_declare_target_local)
   19017         1504 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19018         1457 :       && sym->attr.flavor == FL_VARIABLE
   19019          616 :       && !sym->attr.save
   19020          199 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19021          199 :       && (!sym->attr.in_common
   19022          186 :           && sym->module == NULL
   19023           96 :           && (sym->ns->proc_name == NULL
   19024           96 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19025            6 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19026            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19027              :                sym->name, &sym->declared_at);
   19028              : 
   19029              :   /* If we have come this far we can apply default-initializers, as
   19030              :      described in 14.7.5, to those variables that have not already
   19031              :      been assigned one.  */
   19032      1658328 :   if (sym->ts.type == BT_DERIVED
   19033       129836 :       && !sym->value
   19034       104947 :       && !sym->attr.allocatable
   19035       101977 :       && !sym->attr.alloc_comp)
   19036              :     {
   19037       101919 :       symbol_attribute *a = &sym->attr;
   19038              : 
   19039       101919 :       if ((!a->save && !a->dummy && !a->pointer
   19040        55878 :            && !a->in_common && !a->use_assoc
   19041        10315 :            && a->referenced
   19042         8089 :            && !((a->function || a->result)
   19043         1602 :                 && (!a->dimension
   19044          160 :                     || sym->ts.u.derived->attr.alloc_comp
   19045           95 :                     || sym->ts.u.derived->attr.pointer_comp))
   19046         6568 :            && !(a->function && sym != sym->result))
   19047        95371 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19048         1528 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19049         7977 :         apply_default_init (sym);
   19050        93942 :       else if (a->function && !a->pointer && !a->allocatable
   19051        20381 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19052              :         /* Default initialization for function results.  */
   19053         2656 :         apply_default_init (sym->result);
   19054        91286 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19055        11657 :                && (sym->ts.u.derived->attr.alloc_comp
   19056        11140 :                    || sym->ts.u.derived->attr.pointer_comp))
   19057              :         /* Mark the result symbol to be referenced, when it has allocatable
   19058              :            components.  */
   19059          576 :         sym->result->attr.referenced = 1;
   19060              :     }
   19061              : 
   19062      1658328 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19063        18838 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19064         1226 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19065         1151 :       && !CLASS_DATA (sym)->attr.class_pointer
   19066         1125 :       && !CLASS_DATA (sym)->attr.allocatable)
   19067          853 :     apply_default_init (sym);
   19068              : 
   19069              :   /* If this symbol has a type-spec, check it.  */
   19070      1658328 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19071       630049 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19072      1346016 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19073              :       return;
   19074              : 
   19075      1658325 :   if (sym->param_list)
   19076         1377 :     resolve_pdt (sym);
   19077              : }
   19078              : 
   19079              : 
   19080         3963 : void gfc_resolve_symbol (gfc_symbol *sym)
   19081              : {
   19082         3963 :   resolve_symbol (sym);
   19083         3963 :   return;
   19084              : }
   19085              : 
   19086              : 
   19087              : /************* Resolve DATA statements *************/
   19088              : 
   19089              : static struct
   19090              : {
   19091              :   gfc_data_value *vnode;
   19092              :   mpz_t left;
   19093              : }
   19094              : values;
   19095              : 
   19096              : 
   19097              : /* Advance the values structure to point to the next value in the data list.  */
   19098              : 
   19099              : static bool
   19100        10892 : next_data_value (void)
   19101              : {
   19102        16660 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19103              :     {
   19104              : 
   19105         8198 :       if (values.vnode->next == NULL)
   19106              :         return false;
   19107              : 
   19108         5768 :       values.vnode = values.vnode->next;
   19109         5768 :       mpz_set (values.left, values.vnode->repeat);
   19110              :     }
   19111              : 
   19112              :   return true;
   19113              : }
   19114              : 
   19115              : 
   19116              : static bool
   19117         3557 : check_data_variable (gfc_data_variable *var, locus *where)
   19118              : {
   19119         3557 :   gfc_expr *e;
   19120         3557 :   mpz_t size;
   19121         3557 :   mpz_t offset;
   19122         3557 :   bool t;
   19123         3557 :   ar_type mark = AR_UNKNOWN;
   19124         3557 :   int i;
   19125         3557 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19126         3557 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19127         3557 :   gfc_ref *ref;
   19128         3557 :   gfc_array_ref *ar;
   19129         3557 :   gfc_symbol *sym;
   19130         3557 :   int has_pointer;
   19131              : 
   19132         3557 :   if (!gfc_resolve_expr (var->expr))
   19133              :     return false;
   19134              : 
   19135         3557 :   ar = NULL;
   19136         3557 :   e = var->expr;
   19137              : 
   19138         3557 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19139            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19140            0 :     e = e->value.function.actual->expr;
   19141              : 
   19142         3557 :   if (e->expr_type != EXPR_VARIABLE)
   19143              :     {
   19144            0 :       gfc_error ("Expecting definable entity near %L", where);
   19145            0 :       return false;
   19146              :     }
   19147              : 
   19148         3557 :   sym = e->symtree->n.sym;
   19149              : 
   19150         3557 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19151              :     {
   19152            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19153              :                  sym->name, &sym->declared_at);
   19154            2 :       return false;
   19155              :     }
   19156              : 
   19157         3555 :   if (e->ref == NULL && sym->as)
   19158              :     {
   19159            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19160              :                  " declaration", sym->name, where);
   19161            1 :       return false;
   19162              :     }
   19163              : 
   19164         3554 :   if (gfc_is_coindexed (e))
   19165              :     {
   19166            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19167              :                  where);
   19168            7 :       return false;
   19169              :     }
   19170              : 
   19171         3547 :   has_pointer = sym->attr.pointer;
   19172              : 
   19173         5988 :   for (ref = e->ref; ref; ref = ref->next)
   19174              :     {
   19175         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19176              :         has_pointer = 1;
   19177              : 
   19178         2419 :       if (has_pointer)
   19179              :         {
   19180           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19181              :             {
   19182            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19183              :                          "be a full array", sym->name, where);
   19184            1 :               return false;
   19185              :             }
   19186              : 
   19187           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19188              :             {
   19189            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19190              :                          "and the corresponding DATA value is not a valid "
   19191              :                          "initial-data-target", where);
   19192            1 :               return false;
   19193              :             }
   19194              :         }
   19195              : 
   19196         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19197              :         {
   19198            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19199              :                      "attribute", ref->u.c.component->name, &e->where);
   19200            1 :           return false;
   19201              :         }
   19202              : 
   19203              :       /* Reject substrings of strings of non-constant length.  */
   19204         2442 :       if (ref->type == REF_SUBSTRING
   19205           73 :           && ref->u.ss.length
   19206           73 :           && ref->u.ss.length->length
   19207         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19208            1 :         goto bad_charlen;
   19209              :     }
   19210              : 
   19211              :   /* Reject strings with deferred length or non-constant length.  */
   19212         3543 :   if (e->ts.type == BT_CHARACTER
   19213         3543 :       && (e->ts.deferred
   19214          374 :           || (e->ts.u.cl->length
   19215          323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19216            5 :     goto bad_charlen;
   19217              : 
   19218         3538 :   mpz_init_set_si (offset, 0);
   19219              : 
   19220         3538 :   if (e->rank == 0 || has_pointer)
   19221              :     {
   19222         2691 :       mpz_init_set_ui (size, 1);
   19223         2691 :       ref = NULL;
   19224              :     }
   19225              :   else
   19226              :     {
   19227          847 :       ref = e->ref;
   19228              : 
   19229              :       /* Find the array section reference.  */
   19230         1030 :       for (ref = e->ref; ref; ref = ref->next)
   19231              :         {
   19232         1030 :           if (ref->type != REF_ARRAY)
   19233           92 :             continue;
   19234          938 :           if (ref->u.ar.type == AR_ELEMENT)
   19235           91 :             continue;
   19236              :           break;
   19237              :         }
   19238          847 :       gcc_assert (ref);
   19239              : 
   19240              :       /* Set marks according to the reference pattern.  */
   19241          847 :       switch (ref->u.ar.type)
   19242              :         {
   19243              :         case AR_FULL:
   19244              :           mark = AR_FULL;
   19245              :           break;
   19246              : 
   19247          151 :         case AR_SECTION:
   19248          151 :           ar = &ref->u.ar;
   19249              :           /* Get the start position of array section.  */
   19250          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   19251          151 :           mark = AR_SECTION;
   19252          151 :           break;
   19253              : 
   19254            0 :         default:
   19255            0 :           gcc_unreachable ();
   19256              :         }
   19257              : 
   19258          847 :       if (!gfc_array_size (e, &size))
   19259              :         {
   19260            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   19261              :                      where);
   19262            1 :           mpz_clear (offset);
   19263            1 :           return false;
   19264              :         }
   19265              :     }
   19266              : 
   19267         3537 :   t = true;
   19268              : 
   19269        11937 :   while (mpz_cmp_ui (size, 0) > 0)
   19270              :     {
   19271         8463 :       if (!next_data_value ())
   19272              :         {
   19273            1 :           gfc_error ("DATA statement at %L has more variables than values",
   19274              :                      where);
   19275            1 :           t = false;
   19276            1 :           break;
   19277              :         }
   19278              : 
   19279         8462 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   19280         8462 :       if (!t)
   19281              :         break;
   19282              : 
   19283              :       /* If we have more than one element left in the repeat count,
   19284              :          and we have more than one element left in the target variable,
   19285              :          then create a range assignment.  */
   19286              :       /* FIXME: Only done for full arrays for now, since array sections
   19287              :          seem tricky.  */
   19288         8443 :       if (mark == AR_FULL && ref && ref->next == NULL
   19289         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   19290              :         {
   19291          137 :           mpz_t range;
   19292              : 
   19293          137 :           if (mpz_cmp (size, values.left) >= 0)
   19294              :             {
   19295          126 :               mpz_init_set (range, values.left);
   19296          126 :               mpz_sub (size, size, values.left);
   19297          126 :               mpz_set_ui (values.left, 0);
   19298              :             }
   19299              :           else
   19300              :             {
   19301           11 :               mpz_init_set (range, size);
   19302           11 :               mpz_sub (values.left, values.left, size);
   19303           11 :               mpz_set_ui (size, 0);
   19304              :             }
   19305              : 
   19306          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19307              :                                      offset, &range);
   19308              : 
   19309          137 :           mpz_add (offset, offset, range);
   19310          137 :           mpz_clear (range);
   19311              : 
   19312          137 :           if (!t)
   19313              :             break;
   19314          129 :         }
   19315              : 
   19316              :       /* Assign initial value to symbol.  */
   19317              :       else
   19318              :         {
   19319         8306 :           mpz_sub_ui (values.left, values.left, 1);
   19320         8306 :           mpz_sub_ui (size, size, 1);
   19321              : 
   19322         8306 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19323              :                                      offset, NULL);
   19324         8306 :           if (!t)
   19325              :             break;
   19326              : 
   19327         8271 :           if (mark == AR_FULL)
   19328         5259 :             mpz_add_ui (offset, offset, 1);
   19329              : 
   19330              :           /* Modify the array section indexes and recalculate the offset
   19331              :              for next element.  */
   19332         3012 :           else if (mark == AR_SECTION)
   19333          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   19334              :         }
   19335              :     }
   19336              : 
   19337         3537 :   if (mark == AR_SECTION)
   19338              :     {
   19339          344 :       for (i = 0; i < ar->dimen; i++)
   19340          194 :         mpz_clear (section_index[i]);
   19341              :     }
   19342              : 
   19343         3537 :   mpz_clear (size);
   19344         3537 :   mpz_clear (offset);
   19345              : 
   19346         3537 :   return t;
   19347              : 
   19348            6 : bad_charlen:
   19349            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   19350              :              &e->where);
   19351            6 :   return false;
   19352              : }
   19353              : 
   19354              : 
   19355              : static bool traverse_data_var (gfc_data_variable *, locus *);
   19356              : 
   19357              : /* Iterate over a list of elements in a DATA statement.  */
   19358              : 
   19359              : static bool
   19360          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   19361              : {
   19362          237 :   mpz_t trip;
   19363          237 :   iterator_stack frame;
   19364          237 :   gfc_expr *e, *start, *end, *step;
   19365          237 :   bool retval = true;
   19366              : 
   19367          237 :   mpz_init (frame.value);
   19368          237 :   mpz_init (trip);
   19369              : 
   19370          237 :   start = gfc_copy_expr (var->iter.start);
   19371          237 :   end = gfc_copy_expr (var->iter.end);
   19372          237 :   step = gfc_copy_expr (var->iter.step);
   19373              : 
   19374          237 :   if (!gfc_simplify_expr (start, 1)
   19375          237 :       || start->expr_type != EXPR_CONSTANT)
   19376              :     {
   19377            0 :       gfc_error ("start of implied-do loop at %L could not be "
   19378              :                  "simplified to a constant value", &start->where);
   19379            0 :       retval = false;
   19380            0 :       goto cleanup;
   19381              :     }
   19382          237 :   if (!gfc_simplify_expr (end, 1)
   19383          237 :       || end->expr_type != EXPR_CONSTANT)
   19384              :     {
   19385            0 :       gfc_error ("end of implied-do loop at %L could not be "
   19386              :                  "simplified to a constant value", &end->where);
   19387            0 :       retval = false;
   19388            0 :       goto cleanup;
   19389              :     }
   19390          237 :   if (!gfc_simplify_expr (step, 1)
   19391          237 :       || step->expr_type != EXPR_CONSTANT)
   19392              :     {
   19393            0 :       gfc_error ("step of implied-do loop at %L could not be "
   19394              :                  "simplified to a constant value", &step->where);
   19395            0 :       retval = false;
   19396            0 :       goto cleanup;
   19397              :     }
   19398          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   19399              :     {
   19400            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   19401              :                  &step->where);
   19402            1 :       retval = false;
   19403            1 :       goto cleanup;
   19404              :     }
   19405              : 
   19406          236 :   mpz_set (trip, end->value.integer);
   19407          236 :   mpz_sub (trip, trip, start->value.integer);
   19408          236 :   mpz_add (trip, trip, step->value.integer);
   19409              : 
   19410          236 :   mpz_div (trip, trip, step->value.integer);
   19411              : 
   19412          236 :   mpz_set (frame.value, start->value.integer);
   19413              : 
   19414          236 :   frame.prev = iter_stack;
   19415          236 :   frame.variable = var->iter.var->symtree;
   19416          236 :   iter_stack = &frame;
   19417              : 
   19418         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   19419              :     {
   19420          905 :       if (!traverse_data_var (var->list, where))
   19421              :         {
   19422           14 :           retval = false;
   19423           14 :           goto cleanup;
   19424              :         }
   19425              : 
   19426          891 :       e = gfc_copy_expr (var->expr);
   19427          891 :       if (!gfc_simplify_expr (e, 1))
   19428              :         {
   19429            0 :           gfc_free_expr (e);
   19430            0 :           retval = false;
   19431            0 :           goto cleanup;
   19432              :         }
   19433              : 
   19434          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   19435              : 
   19436          891 :       mpz_sub_ui (trip, trip, 1);
   19437              :     }
   19438              : 
   19439          222 : cleanup:
   19440          237 :   mpz_clear (frame.value);
   19441          237 :   mpz_clear (trip);
   19442              : 
   19443          237 :   gfc_free_expr (start);
   19444          237 :   gfc_free_expr (end);
   19445          237 :   gfc_free_expr (step);
   19446              : 
   19447          237 :   iter_stack = frame.prev;
   19448          237 :   return retval;
   19449              : }
   19450              : 
   19451              : 
   19452              : /* Type resolve variables in the variable list of a DATA statement.  */
   19453              : 
   19454              : static bool
   19455         3418 : traverse_data_var (gfc_data_variable *var, locus *where)
   19456              : {
   19457         3418 :   bool t;
   19458              : 
   19459         7114 :   for (; var; var = var->next)
   19460              :     {
   19461         3794 :       if (var->expr == NULL)
   19462          237 :         t = traverse_data_list (var, where);
   19463              :       else
   19464         3557 :         t = check_data_variable (var, where);
   19465              : 
   19466         3794 :       if (!t)
   19467              :         return false;
   19468              :     }
   19469              : 
   19470              :   return true;
   19471              : }
   19472              : 
   19473              : 
   19474              : /* Resolve the expressions and iterators associated with a data statement.
   19475              :    This is separate from the assignment checking because data lists should
   19476              :    only be resolved once.  */
   19477              : 
   19478              : static bool
   19479         2668 : resolve_data_variables (gfc_data_variable *d)
   19480              : {
   19481         5707 :   for (; d; d = d->next)
   19482              :     {
   19483         3044 :       if (d->list == NULL)
   19484              :         {
   19485         2891 :           if (!gfc_resolve_expr (d->expr))
   19486              :             return false;
   19487              :         }
   19488              :       else
   19489              :         {
   19490          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   19491              :             return false;
   19492              : 
   19493          150 :           if (!resolve_data_variables (d->list))
   19494              :             return false;
   19495              :         }
   19496              :     }
   19497              : 
   19498              :   return true;
   19499              : }
   19500              : 
   19501              : 
   19502              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   19503              :    the value list into static variables, and then recursively traversing the
   19504              :    variables list, expanding iterators and such.  */
   19505              : 
   19506              : static void
   19507         2518 : resolve_data (gfc_data *d)
   19508              : {
   19509              : 
   19510         2518 :   if (!resolve_data_variables (d->var))
   19511              :     return;
   19512              : 
   19513         2513 :   values.vnode = d->value;
   19514         2513 :   if (d->value == NULL)
   19515            0 :     mpz_set_ui (values.left, 0);
   19516              :   else
   19517         2513 :     mpz_set (values.left, d->value->repeat);
   19518              : 
   19519         2513 :   if (!traverse_data_var (d->var, &d->where))
   19520              :     return;
   19521              : 
   19522              :   /* At this point, we better not have any values left.  */
   19523              : 
   19524         2429 :   if (next_data_value ())
   19525            0 :     gfc_error ("DATA statement at %L has more values than variables",
   19526              :                &d->where);
   19527              : }
   19528              : 
   19529              : 
   19530              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   19531              :    accessed by host or use association, is a dummy argument to a pure function,
   19532              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   19533              :    is storage associated with any such variable, shall not be used in the
   19534              :    following contexts: (clients of this function).  */
   19535              : 
   19536              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   19537              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   19538              :    problem.  */
   19539              : bool
   19540        55505 : gfc_impure_variable (gfc_symbol *sym)
   19541              : {
   19542        55505 :   gfc_symbol *proc;
   19543        55505 :   gfc_namespace *ns;
   19544              : 
   19545        55505 :   if (sym->attr.use_assoc || sym->attr.in_common)
   19546              :     return 1;
   19547              : 
   19548              :   /* The namespace of a module procedure interface holds the arguments and
   19549              :      symbols, and so the symbol namespace can be different to that of the
   19550              :      procedure.  */
   19551        54888 :   if (sym->ns != gfc_current_ns
   19552         5854 :       && gfc_current_ns->proc_name->abr_modproc_decl
   19553           48 :       && sym->ns->proc_name->attr.function
   19554           12 :       && sym->attr.result
   19555           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   19556              :     return 0;
   19557              : 
   19558              :   /* Check if the symbol's ns is inside the pure procedure.  */
   19559        59535 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   19560              :     {
   19561        59244 :       if (ns == sym->ns)
   19562              :         break;
   19563         6160 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   19564         5098 :           && !(sym->attr.function || sym->attr.result))
   19565              :         return 1;
   19566              :     }
   19567              : 
   19568        53375 :   proc = sym->ns->proc_name;
   19569        53375 :   if (sym->attr.dummy
   19570         5912 :       && !sym->attr.value
   19571         5790 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   19572         5587 :           || proc->attr.function))
   19573          691 :     return 1;
   19574              : 
   19575              :   /* TODO: Sort out what can be storage associated, if anything, and include
   19576              :      it here.  In principle equivalences should be scanned but it does not
   19577              :      seem to be possible to storage associate an impure variable this way.  */
   19578              :   return 0;
   19579              : }
   19580              : 
   19581              : 
   19582              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   19583              :    current namespace is inside a pure procedure.  */
   19584              : 
   19585              : bool
   19586      2307112 : gfc_pure (gfc_symbol *sym)
   19587              : {
   19588      2307112 :   symbol_attribute attr;
   19589      2307112 :   gfc_namespace *ns;
   19590              : 
   19591      2307112 :   if (sym == NULL)
   19592              :     {
   19593              :       /* Check if the current namespace or one of its parents
   19594              :         belongs to a pure procedure.  */
   19595      3169340 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19596              :         {
   19597      1871977 :           sym = ns->proc_name;
   19598      1871977 :           if (sym == NULL)
   19599              :             return 0;
   19600      1870838 :           attr = sym->attr;
   19601      1870838 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   19602              :             return 1;
   19603              :         }
   19604              :       return 0;
   19605              :     }
   19606              : 
   19607      1001393 :   attr = sym->attr;
   19608              : 
   19609      1001393 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   19610              : }
   19611              : 
   19612              : 
   19613              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   19614              :    checks if the current namespace is implicitly pure.  Note that this
   19615              :    function returns false for a PURE procedure.  */
   19616              : 
   19617              : bool
   19618       721965 : gfc_implicit_pure (gfc_symbol *sym)
   19619              : {
   19620       721965 :   gfc_namespace *ns;
   19621              : 
   19622       721965 :   if (sym == NULL)
   19623              :     {
   19624              :       /* Check if the current procedure is implicit_pure.  Walk up
   19625              :          the procedure list until we find a procedure.  */
   19626       994408 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19627              :         {
   19628       709954 :           sym = ns->proc_name;
   19629       709954 :           if (sym == NULL)
   19630              :             return 0;
   19631              : 
   19632       709881 :           if (sym->attr.flavor == FL_PROCEDURE)
   19633              :             break;
   19634              :         }
   19635              :     }
   19636              : 
   19637       437435 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   19638       748935 :     && !sym->attr.pure;
   19639              : }
   19640              : 
   19641              : 
   19642              : void
   19643       422682 : gfc_unset_implicit_pure (gfc_symbol *sym)
   19644              : {
   19645       422682 :   gfc_namespace *ns;
   19646              : 
   19647       422682 :   if (sym == NULL)
   19648              :     {
   19649              :       /* Check if the current procedure is implicit_pure.  Walk up
   19650              :          the procedure list until we find a procedure.  */
   19651       690748 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19652              :         {
   19653       427089 :           sym = ns->proc_name;
   19654       427089 :           if (sym == NULL)
   19655              :             return;
   19656              : 
   19657       426258 :           if (sym->attr.flavor == FL_PROCEDURE)
   19658              :             break;
   19659              :         }
   19660              :     }
   19661              : 
   19662       421851 :   if (sym->attr.flavor == FL_PROCEDURE)
   19663       149944 :     sym->attr.implicit_pure = 0;
   19664              :   else
   19665       271907 :     sym->attr.pure = 0;
   19666              : }
   19667              : 
   19668              : 
   19669              : /* Test whether the current procedure is elemental or not.  */
   19670              : 
   19671              : bool
   19672      1342265 : gfc_elemental (gfc_symbol *sym)
   19673              : {
   19674      1342265 :   symbol_attribute attr;
   19675              : 
   19676      1342265 :   if (sym == NULL)
   19677            0 :     sym = gfc_current_ns->proc_name;
   19678            0 :   if (sym == NULL)
   19679              :     return 0;
   19680      1342265 :   attr = sym->attr;
   19681              : 
   19682      1342265 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   19683              : }
   19684              : 
   19685              : 
   19686              : /* Warn about unused labels.  */
   19687              : 
   19688              : static void
   19689         4656 : warn_unused_fortran_label (gfc_st_label *label)
   19690              : {
   19691         4682 :   if (label == NULL)
   19692              :     return;
   19693              : 
   19694           27 :   warn_unused_fortran_label (label->left);
   19695              : 
   19696           27 :   if (label->defined == ST_LABEL_UNKNOWN)
   19697              :     return;
   19698              : 
   19699           26 :   switch (label->referenced)
   19700              :     {
   19701            2 :     case ST_LABEL_UNKNOWN:
   19702            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   19703              :                    label->value, &label->where);
   19704            2 :       break;
   19705              : 
   19706            1 :     case ST_LABEL_BAD_TARGET:
   19707            1 :       gfc_warning (OPT_Wunused_label,
   19708              :                    "Label %d at %L defined but cannot be used",
   19709              :                    label->value, &label->where);
   19710            1 :       break;
   19711              : 
   19712              :     default:
   19713              :       break;
   19714              :     }
   19715              : 
   19716           26 :   warn_unused_fortran_label (label->right);
   19717              : }
   19718              : 
   19719              : 
   19720              : /* Returns the sequence type of a symbol or sequence.  */
   19721              : 
   19722              : static seq_type
   19723         1076 : sequence_type (gfc_typespec ts)
   19724              : {
   19725         1076 :   seq_type result;
   19726         1076 :   gfc_component *c;
   19727              : 
   19728         1076 :   switch (ts.type)
   19729              :   {
   19730           49 :     case BT_DERIVED:
   19731              : 
   19732           49 :       if (ts.u.derived->components == NULL)
   19733              :         return SEQ_NONDEFAULT;
   19734              : 
   19735           49 :       result = sequence_type (ts.u.derived->components->ts);
   19736          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   19737           67 :         if (sequence_type (c->ts) != result)
   19738              :           return SEQ_MIXED;
   19739              : 
   19740              :       return result;
   19741              : 
   19742          129 :     case BT_CHARACTER:
   19743          129 :       if (ts.kind != gfc_default_character_kind)
   19744            0 :           return SEQ_NONDEFAULT;
   19745              : 
   19746              :       return SEQ_CHARACTER;
   19747              : 
   19748          240 :     case BT_INTEGER:
   19749          240 :       if (ts.kind != gfc_default_integer_kind)
   19750           25 :           return SEQ_NONDEFAULT;
   19751              : 
   19752              :       return SEQ_NUMERIC;
   19753              : 
   19754          559 :     case BT_REAL:
   19755          559 :       if (!(ts.kind == gfc_default_real_kind
   19756          269 :             || ts.kind == gfc_default_double_kind))
   19757            0 :           return SEQ_NONDEFAULT;
   19758              : 
   19759              :       return SEQ_NUMERIC;
   19760              : 
   19761           81 :     case BT_COMPLEX:
   19762           81 :       if (ts.kind != gfc_default_complex_kind)
   19763           48 :           return SEQ_NONDEFAULT;
   19764              : 
   19765              :       return SEQ_NUMERIC;
   19766              : 
   19767           17 :     case BT_LOGICAL:
   19768           17 :       if (ts.kind != gfc_default_logical_kind)
   19769            0 :           return SEQ_NONDEFAULT;
   19770              : 
   19771              :       return SEQ_NUMERIC;
   19772              : 
   19773              :     default:
   19774              :       return SEQ_NONDEFAULT;
   19775              :   }
   19776              : }
   19777              : 
   19778              : 
   19779              : /* Resolve derived type EQUIVALENCE object.  */
   19780              : 
   19781              : static bool
   19782           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   19783              : {
   19784           80 :   gfc_component *c = derived->components;
   19785              : 
   19786           80 :   if (!derived)
   19787              :     return true;
   19788              : 
   19789              :   /* Shall not be an object of nonsequence derived type.  */
   19790           80 :   if (!derived->attr.sequence)
   19791              :     {
   19792            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   19793              :                  "attribute to be an EQUIVALENCE object", sym->name,
   19794              :                  &e->where);
   19795            0 :       return false;
   19796              :     }
   19797              : 
   19798              :   /* Shall not have allocatable components.  */
   19799           80 :   if (derived->attr.alloc_comp)
   19800              :     {
   19801            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   19802              :                  "components to be an EQUIVALENCE object",sym->name,
   19803              :                  &e->where);
   19804            1 :       return false;
   19805              :     }
   19806              : 
   19807           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   19808              :     {
   19809            1 :       gfc_error ("Derived type variable %qs at %L with default "
   19810              :                  "initialization cannot be in EQUIVALENCE with a variable "
   19811              :                  "in COMMON", sym->name, &e->where);
   19812            1 :       return false;
   19813              :     }
   19814              : 
   19815          245 :   for (; c ; c = c->next)
   19816              :     {
   19817          167 :       if (gfc_bt_struct (c->ts.type)
   19818          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   19819              :         return false;
   19820              : 
   19821              :       /* Shall not be an object of sequence derived type containing a pointer
   19822              :          in the structure.  */
   19823          167 :       if (c->attr.pointer)
   19824              :         {
   19825            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   19826              :                      "component(s) cannot be an EQUIVALENCE object",
   19827              :                      sym->name, &e->where);
   19828            0 :           return false;
   19829              :         }
   19830              :     }
   19831              :   return true;
   19832              : }
   19833              : 
   19834              : 
   19835              : /* Resolve equivalence object.
   19836              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   19837              :    an allocatable array, an object of nonsequence derived type, an object of
   19838              :    sequence derived type containing a pointer at any level of component
   19839              :    selection, an automatic object, a function name, an entry name, a result
   19840              :    name, a named constant, a structure component, or a subobject of any of
   19841              :    the preceding objects.  A substring shall not have length zero.  A
   19842              :    derived type shall not have components with default initialization nor
   19843              :    shall two objects of an equivalence group be initialized.
   19844              :    Either all or none of the objects shall have an protected attribute.
   19845              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   19846              :    are implemented here.  */
   19847              : 
   19848              : static void
   19849         1565 : resolve_equivalence (gfc_equiv *eq)
   19850              : {
   19851         1565 :   gfc_symbol *sym;
   19852         1565 :   gfc_symbol *first_sym;
   19853         1565 :   gfc_expr *e;
   19854         1565 :   gfc_ref *r;
   19855         1565 :   locus *last_where = NULL;
   19856         1565 :   seq_type eq_type, last_eq_type;
   19857         1565 :   gfc_typespec *last_ts;
   19858         1565 :   int object, cnt_protected;
   19859         1565 :   const char *msg;
   19860              : 
   19861         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   19862              : 
   19863         1565 :   first_sym = eq->expr->symtree->n.sym;
   19864              : 
   19865         1565 :   cnt_protected = 0;
   19866              : 
   19867         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   19868              :     {
   19869         3171 :       e = eq->expr;
   19870              : 
   19871         3171 :       e->ts = e->symtree->n.sym->ts;
   19872              :       /* match_varspec might not know yet if it is seeing
   19873              :          array reference or substring reference, as it doesn't
   19874              :          know the types.  */
   19875         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   19876              :         {
   19877         2152 :           gfc_ref *ref = e->ref;
   19878         2152 :           sym = e->symtree->n.sym;
   19879              : 
   19880         2152 :           if (sym->attr.dimension)
   19881              :             {
   19882         1855 :               ref->u.ar.as = sym->as;
   19883         1855 :               ref = ref->next;
   19884              :             }
   19885              : 
   19886              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   19887         2152 :           if (e->ts.type == BT_CHARACTER
   19888          592 :               && ref
   19889          371 :               && ref->type == REF_ARRAY
   19890          371 :               && ref->u.ar.dimen == 1
   19891          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   19892          371 :               && ref->u.ar.stride[0] == NULL)
   19893              :             {
   19894          370 :               gfc_expr *start = ref->u.ar.start[0];
   19895          370 :               gfc_expr *end = ref->u.ar.end[0];
   19896          370 :               void *mem = NULL;
   19897              : 
   19898              :               /* Optimize away the (:) reference.  */
   19899          370 :               if (start == NULL && end == NULL)
   19900              :                 {
   19901            9 :                   if (e->ref == ref)
   19902            0 :                     e->ref = ref->next;
   19903              :                   else
   19904            9 :                     e->ref->next = ref->next;
   19905              :                   mem = ref;
   19906              :                 }
   19907              :               else
   19908              :                 {
   19909          361 :                   ref->type = REF_SUBSTRING;
   19910          361 :                   if (start == NULL)
   19911            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   19912              :                                               NULL, 1);
   19913          361 :                   ref->u.ss.start = start;
   19914          361 :                   if (end == NULL && e->ts.u.cl)
   19915           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   19916          361 :                   ref->u.ss.end = end;
   19917          361 :                   ref->u.ss.length = e->ts.u.cl;
   19918          361 :                   e->ts.u.cl = NULL;
   19919              :                 }
   19920          370 :               ref = ref->next;
   19921          370 :               free (mem);
   19922              :             }
   19923              : 
   19924              :           /* Any further ref is an error.  */
   19925         1930 :           if (ref)
   19926              :             {
   19927            1 :               gcc_assert (ref->type == REF_ARRAY);
   19928            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   19929              :                          &ref->u.ar.where);
   19930            1 :               continue;
   19931              :             }
   19932              :         }
   19933              : 
   19934         3170 :       if (!gfc_resolve_expr (e))
   19935            2 :         continue;
   19936              : 
   19937         3168 :       sym = e->symtree->n.sym;
   19938              : 
   19939         3168 :       if (sym->attr.is_protected)
   19940            2 :         cnt_protected++;
   19941         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   19942              :         {
   19943            2 :               gfc_error ("Either all or none of the objects in the "
   19944              :                          "EQUIVALENCE set at %L shall have the "
   19945              :                          "PROTECTED attribute",
   19946              :                          &e->where);
   19947            2 :               break;
   19948              :         }
   19949              : 
   19950              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   19951         3166 :       if (sym->ns->proc_name
   19952         3150 :           && sym->ns->proc_name->attr.pure
   19953            7 :           && sym->attr.in_common)
   19954              :         {
   19955              :           /* Need to check for symbols that may have entered the pure
   19956              :              procedure via a USE statement.  */
   19957            7 :           bool saw_sym = false;
   19958            7 :           if (sym->ns->use_stmts)
   19959              :             {
   19960            6 :               gfc_use_rename *r;
   19961           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   19962            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   19963              :             }
   19964              :           else
   19965              :             saw_sym = true;
   19966              : 
   19967            6 :           if (saw_sym)
   19968            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   19969              :                        "EQUIVALENCE object in the pure procedure %qs",
   19970              :                        sym->name, &e->where, sym->ns->proc_name->name);
   19971              :           break;
   19972              :         }
   19973              : 
   19974              :       /* Shall not be a named constant.  */
   19975         3159 :       if (e->expr_type == EXPR_CONSTANT)
   19976              :         {
   19977            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   19978              :                      "object", sym->name, &e->where);
   19979            0 :           continue;
   19980              :         }
   19981              : 
   19982         3161 :       if (e->ts.type == BT_DERIVED
   19983         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   19984            2 :         continue;
   19985              : 
   19986              :       /* Check that the types correspond correctly:
   19987              :          Note 5.28:
   19988              :          A numeric sequence structure may be equivalenced to another sequence
   19989              :          structure, an object of default integer type, default real type, double
   19990              :          precision real type, default logical type such that components of the
   19991              :          structure ultimately only become associated to objects of the same
   19992              :          kind. A character sequence structure may be equivalenced to an object
   19993              :          of default character kind or another character sequence structure.
   19994              :          Other objects may be equivalenced only to objects of the same type and
   19995              :          kind parameters.  */
   19996              : 
   19997              :       /* Identical types are unconditionally OK.  */
   19998         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   19999         2677 :         goto identical_types;
   20000              : 
   20001          480 :       last_eq_type = sequence_type (*last_ts);
   20002          480 :       eq_type = sequence_type (sym->ts);
   20003              : 
   20004              :       /* Since the pair of objects is not of the same type, mixed or
   20005              :          non-default sequences can be rejected.  */
   20006              : 
   20007          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20008              :                "statement at %L with different type objects");
   20009          481 :       if ((object ==2
   20010          480 :            && last_eq_type == SEQ_MIXED
   20011            7 :            && last_where
   20012            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20013          486 :           || (eq_type == SEQ_MIXED
   20014            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20015            1 :         continue;
   20016              : 
   20017          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20018              :                "statement at %L with objects of different type");
   20019          483 :       if ((object ==2
   20020          479 :            && last_eq_type == SEQ_NONDEFAULT
   20021           50 :            && last_where
   20022           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20023          525 :           || (eq_type == SEQ_NONDEFAULT
   20024           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20025            4 :         continue;
   20026              : 
   20027          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20028              :                "EQUIVALENCE statement at %L");
   20029          479 :       if (last_eq_type == SEQ_CHARACTER
   20030          475 :           && eq_type != SEQ_CHARACTER
   20031          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20032            4 :                 continue;
   20033              : 
   20034          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20035              :                "EQUIVALENCE statement at %L");
   20036          473 :       if (last_eq_type == SEQ_NUMERIC
   20037          471 :           && eq_type != SEQ_NUMERIC
   20038          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20039            2 :                 continue;
   20040              : 
   20041         3146 : identical_types:
   20042              : 
   20043         3146 :       last_ts =&sym->ts;
   20044         3146 :       last_where = &e->where;
   20045              : 
   20046         3146 :       if (!e->ref)
   20047         1003 :         continue;
   20048              : 
   20049              :       /* Shall not be an automatic array.  */
   20050         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20051              :         {
   20052            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20053              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20054            3 :           continue;
   20055              :         }
   20056              : 
   20057         2140 :       r = e->ref;
   20058         4326 :       while (r)
   20059              :         {
   20060              :           /* Shall not be a structure component.  */
   20061         2187 :           if (r->type == REF_COMPONENT)
   20062              :             {
   20063            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20064              :                          "EQUIVALENCE object",
   20065            0 :                          r->u.c.component->name, &e->where);
   20066            0 :               break;
   20067              :             }
   20068              : 
   20069              :           /* A substring shall not have length zero.  */
   20070         2187 :           if (r->type == REF_SUBSTRING)
   20071              :             {
   20072          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20073              :                 {
   20074            1 :                   gfc_error ("Substring at %L has length zero",
   20075              :                              &r->u.ss.start->where);
   20076            1 :                   break;
   20077              :                 }
   20078              :             }
   20079         2186 :           r = r->next;
   20080              :         }
   20081              :     }
   20082         1565 : }
   20083              : 
   20084              : 
   20085              : /* Function called by resolve_fntype to flag other symbols used in the
   20086              :    length type parameter specification of function results.  */
   20087              : 
   20088              : static bool
   20089         4208 : flag_fn_result_spec (gfc_expr *expr,
   20090              :                      gfc_symbol *sym,
   20091              :                      int *f ATTRIBUTE_UNUSED)
   20092              : {
   20093         4208 :   gfc_namespace *ns;
   20094         4208 :   gfc_symbol *s;
   20095              : 
   20096         4208 :   if (expr->expr_type == EXPR_VARIABLE)
   20097              :     {
   20098         1378 :       s = expr->symtree->n.sym;
   20099         2153 :       for (ns = s->ns; ns; ns = ns->parent)
   20100         2153 :         if (!ns->parent)
   20101              :           break;
   20102              : 
   20103         1378 :       if (sym == s)
   20104              :         {
   20105            1 :           gfc_error ("Self reference in character length expression "
   20106              :                      "for %qs at %L", sym->name, &expr->where);
   20107            1 :           return true;
   20108              :         }
   20109              : 
   20110         1377 :       if (!s->fn_result_spec
   20111         1377 :           && s->attr.flavor == FL_PARAMETER)
   20112              :         {
   20113              :           /* Function contained in a module.... */
   20114           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20115              :             {
   20116           32 :               gfc_symtree *st;
   20117           32 :               s->fn_result_spec = 1;
   20118              :               /* Make sure that this symbol is translated as a module
   20119              :                  variable.  */
   20120           32 :               st = gfc_get_unique_symtree (ns);
   20121           32 :               st->n.sym = s;
   20122           32 :               s->refs++;
   20123           32 :             }
   20124              :           /* ... which is use associated and called.  */
   20125           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20126            0 :                         ||
   20127              :                   /* External function matched with an interface.  */
   20128            0 :                   (s->ns->proc_name
   20129            0 :                    && ((s->ns == ns
   20130            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20131            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20132            0 :                    && s->ns->proc_name->attr.function))
   20133           31 :             s->fn_result_spec = 1;
   20134              :         }
   20135              :     }
   20136              :   return false;
   20137              : }
   20138              : 
   20139              : 
   20140              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20141              : 
   20142              : static void
   20143       342788 : resolve_fntype (gfc_namespace *ns)
   20144              : {
   20145       342788 :   gfc_entry_list *el;
   20146       342788 :   gfc_symbol *sym;
   20147              : 
   20148       342788 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20149              :     return;
   20150              : 
   20151              :   /* If there are any entries, ns->proc_name is the entry master
   20152              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20153       178488 :   if (ns->entries)
   20154          596 :     sym = ns->entries->sym;
   20155              :   else
   20156              :     sym = ns->proc_name;
   20157       178488 :   if (sym->result == sym
   20158       143399 :       && sym->ts.type == BT_UNKNOWN
   20159            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20160       178492 :       && !sym->attr.untyped)
   20161              :     {
   20162            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20163              :                  sym->name, &sym->declared_at);
   20164            3 :       sym->attr.untyped = 1;
   20165              :     }
   20166              : 
   20167        13588 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20168         1807 :       && !sym->attr.contained
   20169          299 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20170       178488 :       && gfc_check_symbol_access (sym))
   20171              :     {
   20172            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20173              :                       "%L of PRIVATE type %qs", sym->name,
   20174            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20175              :     }
   20176              : 
   20177       178488 :     if (ns->entries)
   20178         1253 :     for (el = ns->entries->next; el; el = el->next)
   20179              :       {
   20180          657 :         if (el->sym->result == el->sym
   20181          445 :             && el->sym->ts.type == BT_UNKNOWN
   20182            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20183          659 :             && !el->sym->attr.untyped)
   20184              :           {
   20185            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20186              :                        el->sym->name, &el->sym->declared_at);
   20187            2 :             el->sym->attr.untyped = 1;
   20188              :           }
   20189              :       }
   20190              : 
   20191       178488 :   if (sym->ts.type == BT_CHARACTER
   20192         6948 :       && sym->ts.u.cl->length
   20193         1860 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20194         1855 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20195              : }
   20196              : 
   20197              : 
   20198              : /* 12.3.2.1.1 Defined operators.  */
   20199              : 
   20200              : static bool
   20201          452 : check_uop_procedure (gfc_symbol *sym, locus where)
   20202              : {
   20203          452 :   gfc_formal_arglist *formal;
   20204              : 
   20205          452 :   if (!sym->attr.function)
   20206              :     {
   20207            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20208              :                  sym->name, &where);
   20209            4 :       return false;
   20210              :     }
   20211              : 
   20212          448 :   if (sym->ts.type == BT_CHARACTER
   20213           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20214            2 :       && !(sym->result && ((sym->result->ts.u.cl
   20215            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   20216              :     {
   20217            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   20218              :                  "character length", sym->name, &where);
   20219            2 :       return false;
   20220              :     }
   20221              : 
   20222          446 :   formal = gfc_sym_get_dummy_args (sym);
   20223          446 :   if (!formal || !formal->sym)
   20224              :     {
   20225            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   20226              :                  "one argument", sym->name, &where);
   20227            1 :       return false;
   20228              :     }
   20229              : 
   20230          445 :   if (formal->sym->attr.intent != INTENT_IN)
   20231              :     {
   20232            0 :       gfc_error ("First argument of operator interface at %L must be "
   20233              :                  "INTENT(IN)", &where);
   20234            0 :       return false;
   20235              :     }
   20236              : 
   20237          445 :   if (formal->sym->attr.optional)
   20238              :     {
   20239            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   20240              :                  "optional", &where);
   20241            0 :       return false;
   20242              :     }
   20243              : 
   20244          445 :   formal = formal->next;
   20245          445 :   if (!formal || !formal->sym)
   20246              :     return true;
   20247              : 
   20248          295 :   if (formal->sym->attr.intent != INTENT_IN)
   20249              :     {
   20250            0 :       gfc_error ("Second argument of operator interface at %L must be "
   20251              :                  "INTENT(IN)", &where);
   20252            0 :       return false;
   20253              :     }
   20254              : 
   20255          295 :   if (formal->sym->attr.optional)
   20256              :     {
   20257            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   20258              :                  "optional", &where);
   20259            1 :       return false;
   20260              :     }
   20261              : 
   20262          294 :   if (formal->next)
   20263              :     {
   20264            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   20265              :                  "arguments", &where);
   20266            2 :       return false;
   20267              :     }
   20268              : 
   20269              :   return true;
   20270              : }
   20271              : 
   20272              : static void
   20273       343548 : gfc_resolve_uops (gfc_symtree *symtree)
   20274              : {
   20275       343548 :   gfc_interface *itr;
   20276              : 
   20277       343548 :   if (symtree == NULL)
   20278              :     return;
   20279              : 
   20280          380 :   gfc_resolve_uops (symtree->left);
   20281          380 :   gfc_resolve_uops (symtree->right);
   20282              : 
   20283          773 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   20284          393 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   20285              : }
   20286              : 
   20287              : 
   20288              : /* Examine all of the expressions associated with a program unit,
   20289              :    assign types to all intermediate expressions, make sure that all
   20290              :    assignments are to compatible types and figure out which names
   20291              :    refer to which functions or subroutines.  It doesn't check code
   20292              :    block, which is handled by gfc_resolve_code.  */
   20293              : 
   20294              : static void
   20295       345294 : resolve_types (gfc_namespace *ns)
   20296              : {
   20297       345294 :   gfc_namespace *n;
   20298       345294 :   gfc_charlen *cl;
   20299       345294 :   gfc_data *d;
   20300       345294 :   gfc_equiv *eq;
   20301       345294 :   gfc_namespace* old_ns = gfc_current_ns;
   20302       345294 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   20303              : 
   20304       345294 :   if (ns->types_resolved)
   20305              :     return;
   20306              : 
   20307              :   /* Check that all IMPLICIT types are ok.  */
   20308       342789 :   if (!ns->seen_implicit_none)
   20309              :     {
   20310              :       unsigned letter;
   20311      8623477 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   20312      8304089 :         if (ns->set_flag[letter]
   20313      8304089 :             && !resolve_typespec_used (&ns->default_type[letter],
   20314              :                                        &ns->implicit_loc[letter], NULL))
   20315              :           return;
   20316              :     }
   20317              : 
   20318       342788 :   gfc_current_ns = ns;
   20319              : 
   20320       342788 :   resolve_entries (ns);
   20321              : 
   20322       342788 :   resolve_common_vars (&ns->blank_common, false);
   20323       342788 :   resolve_common_blocks (ns->common_root);
   20324              : 
   20325       342788 :   resolve_contained_functions (ns);
   20326              : 
   20327       342788 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   20328       292981 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20329       191359 :     gfc_resolve_formal_arglist (ns->proc_name);
   20330              : 
   20331       342788 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   20332              : 
   20333       437720 :   for (cl = ns->cl_list; cl; cl = cl->next)
   20334        94932 :     resolve_charlen (cl);
   20335              : 
   20336       342788 :   gfc_traverse_ns (ns, resolve_symbol);
   20337              : 
   20338       342788 :   resolve_fntype (ns);
   20339              : 
   20340       390602 :   for (n = ns->contained; n; n = n->sibling)
   20341              :     {
   20342              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   20343        47814 :       if (gfc_pure (ns->proc_name)
   20344            5 :           && !gfc_pure (n->proc_name)
   20345        47814 :           && !n->proc_name->attr.artificial)
   20346            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   20347              :                    "also be PURE", n->proc_name->name,
   20348              :                    &n->proc_name->declared_at);
   20349              : 
   20350        47814 :       resolve_types (n);
   20351              :     }
   20352              : 
   20353       342788 :   forall_flag = 0;
   20354       342788 :   gfc_do_concurrent_flag = 0;
   20355       342788 :   gfc_check_interfaces (ns);
   20356              : 
   20357       342788 :   gfc_traverse_ns (ns, resolve_values);
   20358              : 
   20359       342788 :   if (ns->save_all || (!flag_automatic && !recursive))
   20360          313 :     gfc_save_all (ns);
   20361              : 
   20362       342788 :   iter_stack = NULL;
   20363       345306 :   for (d = ns->data; d; d = d->next)
   20364         2518 :     resolve_data (d);
   20365              : 
   20366       342788 :   iter_stack = NULL;
   20367       342788 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   20368              : 
   20369       342788 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   20370              : 
   20371       344353 :   for (eq = ns->equiv; eq; eq = eq->next)
   20372         1565 :     resolve_equivalence (eq);
   20373              : 
   20374              :   /* Warn about unused labels.  */
   20375       342788 :   if (warn_unused_label)
   20376         4629 :     warn_unused_fortran_label (ns->st_labels);
   20377              : 
   20378       342788 :   gfc_resolve_uops (ns->uop_root);
   20379              : 
   20380       342788 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   20381              : 
   20382       342788 :   gfc_resolve_omp_declare (ns);
   20383              : 
   20384       342788 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   20385              : 
   20386       342788 :   ns->types_resolved = 1;
   20387              : 
   20388       342788 :   gfc_current_ns = old_ns;
   20389              : }
   20390              : 
   20391              : 
   20392              : /* Call gfc_resolve_code recursively.  */
   20393              : 
   20394              : static void
   20395       345350 : resolve_codes (gfc_namespace *ns)
   20396              : {
   20397       345350 :   gfc_namespace *n;
   20398       345350 :   bitmap_obstack old_obstack;
   20399              : 
   20400       345350 :   if (ns->resolved == 1)
   20401        13974 :     return;
   20402              : 
   20403       379246 :   for (n = ns->contained; n; n = n->sibling)
   20404        47870 :     resolve_codes (n);
   20405              : 
   20406       331376 :   gfc_current_ns = ns;
   20407              : 
   20408              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   20409       331376 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   20410       319318 :     cs_base = NULL;
   20411              : 
   20412              :   /* Set to an out of range value.  */
   20413       331376 :   current_entry_id = -1;
   20414              : 
   20415       331376 :   old_obstack = labels_obstack;
   20416       331376 :   bitmap_obstack_initialize (&labels_obstack);
   20417              : 
   20418       331376 :   gfc_resolve_oacc_declare (ns);
   20419       331376 :   gfc_resolve_oacc_routines (ns);
   20420       331376 :   gfc_resolve_omp_local_vars (ns);
   20421       331376 :   if (ns->omp_allocate)
   20422           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   20423       331376 :   gfc_resolve_code (ns->code, ns);
   20424              : 
   20425       331375 :   bitmap_obstack_release (&labels_obstack);
   20426       331375 :   labels_obstack = old_obstack;
   20427              : }
   20428              : 
   20429              : 
   20430              : /* This function is called after a complete program unit has been compiled.
   20431              :    Its purpose is to examine all of the expressions associated with a program
   20432              :    unit, assign types to all intermediate expressions, make sure that all
   20433              :    assignments are to compatible types and figure out which names refer to
   20434              :    which functions or subroutines.  */
   20435              : 
   20436              : void
   20437       302234 : gfc_resolve (gfc_namespace *ns)
   20438              : {
   20439       302234 :   gfc_namespace *old_ns;
   20440       302234 :   code_stack *old_cs_base;
   20441       302234 :   struct gfc_omp_saved_state old_omp_state;
   20442              : 
   20443       302234 :   if (ns->resolved)
   20444         4754 :     return;
   20445              : 
   20446       297480 :   ns->resolved = -1;
   20447       297480 :   old_ns = gfc_current_ns;
   20448       297480 :   old_cs_base = cs_base;
   20449              : 
   20450              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   20451              :      body, we should clear any state associated to it, so that say NS's
   20452              :      DO loops are not interpreted as OpenMP loops.  */
   20453       297480 :   if (!ns->construct_entities)
   20454       285422 :     gfc_omp_save_and_clear_state (&old_omp_state);
   20455              : 
   20456       297480 :   resolve_types (ns);
   20457       297480 :   component_assignment_level = 0;
   20458       297480 :   resolve_codes (ns);
   20459              : 
   20460       297479 :   if (ns->omp_assumes)
   20461           13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   20462              : 
   20463       297479 :   gfc_current_ns = old_ns;
   20464       297479 :   cs_base = old_cs_base;
   20465       297479 :   ns->resolved = 1;
   20466              : 
   20467       297479 :   gfc_run_passes (ns);
   20468              : 
   20469       297479 :   if (!ns->construct_entities)
   20470       285421 :     gfc_omp_restore_state (&old_omp_state);
   20471              : }
        

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.