LCOV - code coverage report
Current view: top level - gcc/fortran - resolve.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.5 % 9676 9043
Test Date: 2026-05-11 19:44:49 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        52304 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
     120              : {
     121        56811 :   for (ns = ns->parent; ns; ns = ns->parent)
     122              :     {
     123         4758 :       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      1516110 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
     136              : {
     137      1516110 :   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       520499 : gfc_resolve_formal_arglist (gfc_symbol *proc)
     288              : {
     289       520499 :   gfc_formal_arglist *f;
     290       520499 :   gfc_symbol *sym;
     291       520499 :   bool saved_specification_expr;
     292       520499 :   int i;
     293              : 
     294       520499 :   if (proc->result != NULL)
     295       323996 :     sym = proc->result;
     296              :   else
     297              :     sym = proc;
     298              : 
     299       520499 :   if (gfc_elemental (proc)
     300       358319 :       || sym->attr.pointer || sym->attr.allocatable
     301       866706 :       || (sym->as && sym->as->rank != 0))
     302              :     {
     303       176622 :       proc->attr.always_explicit = 1;
     304       176622 :       sym->attr.always_explicit = 1;
     305              :     }
     306              : 
     307       520499 :   gfc_namespace *orig_current_ns = gfc_current_ns;
     308       520499 :   gfc_current_ns = gfc_get_procedure_ns (proc);
     309              : 
     310      1345921 :   for (f = proc->formal; f; f = f->next)
     311              :     {
     312       825424 :       gfc_array_spec *as;
     313       825424 :       gfc_symbol *saved_specification_expr_symbol;
     314              : 
     315       825424 :       sym = f->sym;
     316              : 
     317       825424 :       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       825840 :                && !resolve_procedure_interface (sym))
     333              :         break;
     334              : 
     335       825253 :       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       825251 :       if (sym->attr.if_source != IFSRC_UNKNOWN)
     344          855 :         gfc_resolve_formal_arglist (sym);
     345              : 
     346       825251 :       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       824392 :           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       825251 :       as = sym->ts.type == BT_CLASS && sym->attr.class_ok
     359       839047 :            ? CLASS_DATA (sym)->as : sym->as;
     360              : 
     361       825251 :       saved_specification_expr = specification_expr;
     362       825251 :       saved_specification_expr_symbol = specification_expr_symbol;
     363       825251 :       specification_expr = true;
     364       825251 :       specification_expr_symbol = sym;
     365       825251 :       gfc_resolve_array_spec (as, 0);
     366       825251 :       specification_expr = saved_specification_expr;
     367       825251 :       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       825251 :       if (as && as->rank > 0 && as->type == AS_DEFERRED
     373        12218 :           && ((sym->ts.type != BT_CLASS
     374        11098 :                && !(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         7379 :           && sym->attr.flavor != FL_PROCEDURE)
     379              :         {
     380         7378 :           as->type = AS_ASSUMED_SHAPE;
     381        17117 :           for (i = 0; i < as->rank; i++)
     382         9739 :             as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
     383              :         }
     384              : 
     385       128276 :       if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
     386       114685 :           || (as && as->type == AS_ASSUMED_RANK)
     387       774202 :           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
     388       764094 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
     389        11617 :               && (CLASS_DATA (sym)->attr.class_pointer
     390        11134 :                   || CLASS_DATA (sym)->attr.allocatable
     391        10236 :                   || CLASS_DATA (sym)->attr.target))
     392       762713 :           || sym->attr.optional)
     393              :         {
     394        77772 :           proc->attr.always_explicit = 1;
     395        77772 :           if (proc->result)
     396        36197 :             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       825251 :       if (sym->attr.flavor == FL_UNKNOWN)
     403        50370 :         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
     404              : 
     405       825251 :       if (gfc_pure (proc))
     406              :         {
     407       327171 :           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       327142 :           else if (!sym->attr.pointer)
     418              :             {
     419       327128 :               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       327128 :               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       327170 :           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       825249 :       if (proc->attr.implicit_pure)
     458              :         {
     459        24776 :           if (sym->attr.flavor == FL_PROCEDURE)
     460              :             {
     461          313 :               if (!gfc_pure (sym))
     462          293 :                 proc->attr.implicit_pure = 0;
     463              :             }
     464        24463 :           else if (!sym->attr.pointer)
     465              :             {
     466        23683 :               if (proc->attr.function && sym->attr.intent != INTENT_IN
     467         2739 :                   && !sym->value)
     468         2739 :                 proc->attr.implicit_pure = 0;
     469              : 
     470        23683 :               if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
     471         4196 :                   && !sym->value)
     472         4196 :                 proc->attr.implicit_pure = 0;
     473              :             }
     474              :         }
     475              : 
     476       825249 :       if (gfc_elemental (proc))
     477              :         {
     478              :           /* F08:C1289.  */
     479       301688 :           if (sym->attr.codimension
     480       301687 :               || (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       301685 :           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       301683 :           if (sym->attr.allocatable
     497       301682 :               || (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       301681 :           if (sym->attr.pointer
     507       301680 :               || (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       301679 :           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       301677 :           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       825236 :       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       520499 :   if (sym)
     562       520407 :     sym->formal_resolved = 1;
     563       520499 :   gfc_current_ns = orig_current_ns;
     564       520499 : }
     565              : 
     566              : 
     567              : /* Work function called when searching for symbols that have argument lists
     568              :    associated with them.  */
     569              : 
     570              : static void
     571      1819715 : find_arglists (gfc_symbol *sym)
     572              : {
     573      1819715 :   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
     574       329637 :       || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
     575              :     return;
     576              : 
     577       327600 :   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       343587 : resolve_formal_arglists (gfc_namespace *ns)
     586              : {
     587            0 :   if (ns == NULL)
     588              :     return;
     589              : 
     590       343587 :   gfc_traverse_ns (ns, find_arglists);
     591              : }
     592              : 
     593              : 
     594              : static void
     595        36967 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
     596              : {
     597        36967 :   bool t;
     598              : 
     599        36967 :   if (sym && sym->attr.flavor == FL_PROCEDURE
     600        36967 :       && 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        36967 :   if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
     610        10862 :       || sym->attr.entry_master)
     611        26294 :     return;
     612              : 
     613        10673 :   if (!sym->result)
     614              :     return;
     615              : 
     616              :   /* Try to find out of what the return type is.  */
     617        10673 :   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        10673 :   if (sym->result->ts.type == BT_CHARACTER)
     642              :     {
     643         1188 :       gfc_charlen *cl = sym->result->ts.u.cl;
     644         1188 :       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       380047 : resolve_entries (gfc_namespace *ns)
     727              : {
     728       380047 :   gfc_namespace *old_ns;
     729       380047 :   gfc_code *c;
     730       380047 :   gfc_symbol *proc;
     731       380047 :   gfc_entry_list *el;
     732              :   /* Provide sufficient space to hold "master.%d.%s".  */
     733       380047 :   char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
     734       380047 :   static int master_count = 0;
     735              : 
     736       380047 :   if (ns->proc_name == NULL)
     737       379344 :     return;
     738              : 
     739              :   /* No need to do anything if this procedure doesn't have alternate entry
     740              :      points.  */
     741       379998 :   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       345564 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
     996              : {
     997       345564 :   gfc_symbol *csym = common_block->head;
     998       345564 :   gfc_gsymbol *gsym;
     999              : 
    1000       351615 :   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       345564 : }
    1071              : 
    1072              : /* Resolve common blocks.  */
    1073              : static void
    1074       344117 : resolve_common_blocks (gfc_symtree *common_root)
    1075              : {
    1076       344117 :   gfc_symbol *sym = NULL;
    1077       344117 :   gfc_gsymbol * gsym;
    1078              : 
    1079       344117 :   if (common_root == NULL)
    1080       343995 :     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       343587 : resolve_contained_functions (gfc_namespace *ns)
    1206              : {
    1207       343587 :   gfc_namespace *child;
    1208       343587 :   gfc_entry_list *el;
    1209              : 
    1210       343587 :   resolve_formal_arglists (ns);
    1211              : 
    1212       380047 :   for (child = ns->contained; child; child = child->sibling)
    1213              :     {
    1214              :       /* Resolve alternate entry points first.  */
    1215        36460 :       resolve_entries (child);
    1216              : 
    1217              :       /* Then check function return types.  */
    1218        36460 :       resolve_contained_fntype (child->proc_name, child);
    1219        36967 :       for (el = child->entries; el; el = el->next)
    1220          507 :         resolve_contained_fntype (el->sym, child);
    1221              :     }
    1222       343587 : }
    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        62710 : resolve_structure_cons (gfc_expr *expr, int init)
    1319              : {
    1320        62710 :   gfc_constructor *cons;
    1321        62710 :   gfc_component *comp;
    1322        62710 :   bool t;
    1323        62710 :   symbol_attribute a;
    1324              : 
    1325        62710 :   t = true;
    1326              : 
    1327        62710 :   if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
    1328              :     {
    1329        59849 :       if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
    1330        59699 :         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        59849 :       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        62710 :   if (expr->ref)
    1358          160 :     comp = expr->ref->u.c.sym->components;
    1359        62550 :   else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
    1360              :             || expr->ts.type == BT_UNION)
    1361        62548 :            && expr->ts.u.derived)
    1362        62548 :     comp = expr->ts.u.derived->components;
    1363              :   else
    1364              :     return false;
    1365              : 
    1366        62708 :   cons = gfc_constructor_first (expr->value.constructor);
    1367              : 
    1368       208685 :   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
    1369              :     {
    1370       145979 :       int rank;
    1371              : 
    1372       145979 :       if (!cons->expr)
    1373         9727 :         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       136252 :       if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
    1379           15 :         continue;
    1380              : 
    1381       136237 :       if (!gfc_resolve_expr (cons->expr))
    1382              :         {
    1383            0 :           t = false;
    1384            0 :           continue;
    1385              :         }
    1386              : 
    1387       136237 :       rank = comp->as ? comp->as->rank : 0;
    1388       136237 :       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       136237 :       if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
    1394          228 :           gfc_find_vtab (&cons->expr->ts);
    1395              : 
    1396       136237 :       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       238221 :       if (!comp->attr.proc_pointer &&
    1409       101984 :           !gfc_compare_types (&cons->expr->ts, &comp->ts))
    1410              :         {
    1411        12426 :           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         9080 :               cons->expr->ts = comp->ts;
    1417              :             }
    1418         3346 :           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         3344 :           else if (!UNLIMITED_POLY (comp))
    1428              :             {
    1429         3281 :               bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
    1430         3281 :               if (t)
    1431       136237 :                 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       136237 :       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       136237 :       if (cons->expr->expr_type == EXPR_NULL
    1494        40855 :           && !(comp->attr.pointer || comp->attr.allocatable
    1495        20335 :                || 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       136237 :       if (comp->attr.proc_pointer && comp->ts.interface)
    1508              :         {
    1509              :           /* Check procedure pointer interface.  */
    1510        15350 :           gfc_symbol *s2 = NULL;
    1511        15350 :           gfc_component *c2;
    1512        15350 :           const char *name;
    1513        15350 :           char err[200];
    1514              : 
    1515        15350 :           c2 = gfc_get_proc_ptr_comp (cons->expr);
    1516        15350 :           if (c2)
    1517              :             {
    1518           12 :               s2 = c2->ts.interface;
    1519           12 :               name = c2->name;
    1520              :             }
    1521        15338 :           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        15338 :           else if (cons->expr->expr_type != EXPR_NULL)
    1527              :             {
    1528        14925 :               s2 = cons->expr->symtree->n.sym;
    1529        14925 :               name = cons->expr->symtree->n.sym->name;
    1530              :             }
    1531              : 
    1532        14937 :           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       136235 :       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       136235 :       if (!comp->attr.pointer || comp->attr.proc_pointer
    1580        21895 :           || cons->expr->expr_type == EXPR_NULL)
    1581       126179 :         continue;
    1582              : 
    1583        10056 :       a = gfc_expr_attr (cons->expr);
    1584              : 
    1585        10056 :       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        10056 :       if (init)
    1594              :         {
    1595              :           /* F08:C461. Additional checks for pointer initialization.  */
    1596         9988 :           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         9988 :           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        10056 :       if (comp->attr.pointer && (a.pointer || a.target)
    1613        20111 :           && 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        10056 :       bool impure = cons->expr->expr_type == EXPR_VARIABLE
    1622        10056 :                     && (gfc_impure_variable (cons->expr->symtree->n.sym)
    1623        10020 :                         || 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        10056 :       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       743163 : was_declared (gfc_symbol *sym)
    1647              : {
    1648       743163 :   symbol_attribute a;
    1649              : 
    1650       743163 :   a = sym->attr;
    1651              : 
    1652       743163 :   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
    1653              :     return 1;
    1654              : 
    1655       629742 :   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
    1656       621162 :       || a.optional || a.pointer || a.save || a.target || a.volatile_
    1657       621160 :       || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
    1658       621106 :       || a.asynchronous || a.codimension || a.subroutine)
    1659        94720 :     return 1;
    1660              : 
    1661              :   return 0;
    1662              : }
    1663              : 
    1664              : 
    1665              : /* Determine if a symbol is generic or not.  */
    1666              : 
    1667              : static int
    1668       412692 : generic_sym (gfc_symbol *sym)
    1669              : {
    1670       412692 :   gfc_symbol *s;
    1671              : 
    1672       412692 :   if (sym->attr.generic ||
    1673       383489 :       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
    1674        30266 :     return 1;
    1675              : 
    1676       382426 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1677              :     return 0;
    1678              : 
    1679        76984 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1680              : 
    1681        76984 :   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       382338 : specific_sym (gfc_symbol *sym)
    1697              : {
    1698       382338 :   gfc_symbol *s;
    1699              : 
    1700       382338 :   if (sym->attr.if_source == IFSRC_IFBODY
    1701       371025 :       || sym->attr.proc == PROC_MODULE
    1702              :       || sym->attr.proc == PROC_INTERNAL
    1703              :       || sym->attr.proc == PROC_ST_FUNCTION
    1704       294814 :       || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
    1705       676421 :       || sym->attr.external)
    1706        90640 :     return 1;
    1707              : 
    1708       291698 :   if (was_declared (sym) || sym->ns->parent == NULL)
    1709              :     return 0;
    1710              : 
    1711        76882 :   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
    1712              : 
    1713        76882 :   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       412414 : procedure_kind (gfc_symbol *sym)
    1724              : {
    1725       412414 :   if (generic_sym (sym))
    1726              :     return PTYPE_GENERIC;
    1727              : 
    1728       382291 :   if (specific_sym (sym))
    1729        90640 :     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      1420317 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
    1741              : {
    1742      1420317 :   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       229043 : resolve_assumed_size_actual (gfc_expr *e)
    1769              : {
    1770       229043 :   if (e == NULL)
    1771              :    return false;
    1772              : 
    1773       228476 :   switch (e->expr_type)
    1774              :     {
    1775       110224 :     case EXPR_VARIABLE:
    1776       110224 :       if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
    1777              :         return true;
    1778              :       break;
    1779              : 
    1780        48410 :     case EXPR_OP:
    1781        48410 :       if (resolve_assumed_size_actual (e->value.op.op1)
    1782        48410 :           || 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       151380 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
    1834              : {
    1835       151380 :   gfc_symbol* proc_sym;
    1836       151380 :   gfc_symbol* context_proc;
    1837       151380 :   gfc_namespace* real_context;
    1838              : 
    1839       151380 :   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       151379 :   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       151379 :   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         1850 :   for (real_context = context; ; real_context = real_context->parent)
    1857              :     {
    1858              :       /* We should find something, eventually!  */
    1859       128387 :       gcc_assert (real_context);
    1860              : 
    1861       128387 :       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       128387 :       if (!context_proc)
    1871              :         return false;
    1872              : 
    1873       128123 :       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       126273 :   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       126258 :   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        42586 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
    1904              : {
    1905        42586 :   gfc_intrinsic_sym* isym = NULL;
    1906        42586 :   const char* symstd;
    1907              : 
    1908        42586 :   if (sym->resolve_symbol_called >= 2)
    1909              :     return true;
    1910              : 
    1911        32707 :   sym->resolve_symbol_called = 2;
    1912              : 
    1913              :   /* Already resolved.  */
    1914        32707 :   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        24858 :   if (sym->intmod_sym_id && sym->attr.subroutine)
    1923              :     {
    1924         8938 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1925         8938 :       isym = gfc_intrinsic_subroutine_by_id (id);
    1926         8938 :     }
    1927        15920 :   else if (sym->intmod_sym_id)
    1928              :     {
    1929        12234 :       gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
    1930        12234 :       isym = gfc_intrinsic_function_by_id (id);
    1931              :     }
    1932         3686 :   else if (!sym->attr.subroutine)
    1933         3599 :     isym = gfc_find_function (sym->name);
    1934              : 
    1935        24771 :   if (isym && !sym->attr.subroutine)
    1936              :     {
    1937        15788 :       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        19983 :       if (!sym->attr.function &&
    1944         4195 :           !gfc_add_function(&sym->attr, sym->name, loc))
    1945              :         return false;
    1946              : 
    1947        15788 :       sym->ts = isym->ts;
    1948              :     }
    1949         9070 :   else if (isym || (isym = gfc_find_subroutine (sym->name)))
    1950              :     {
    1951         9067 :       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         9107 :       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        24853 :   gfc_copy_formal_args_intr (sym, isym, NULL);
    1970              : 
    1971        24853 :   sym->attr.pure = isym->pure;
    1972        24853 :   sym->attr.elemental = isym->elemental;
    1973              : 
    1974              :   /* Check it is actually available in the standard settings.  */
    1975        24853 :   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      1323120 : resolve_procedure_expression (gfc_expr* expr)
    1994              : {
    1995      1323120 :   gfc_symbol* sym;
    1996              : 
    1997      1323120 :   if (expr->expr_type != EXPR_VARIABLE)
    1998              :     return true;
    1999      1323103 :   gcc_assert (expr->symtree);
    2000              : 
    2001      1323103 :   sym = expr->symtree->n.sym;
    2002              : 
    2003      1323103 :   if (sym->attr.intrinsic)
    2004         1346 :     gfc_resolve_intrinsic (sym, &expr->where);
    2005              : 
    2006      1323103 :   if (sym->attr.flavor != FL_PROCEDURE
    2007        31609 :       || (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        17061 :   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       426540 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
    2056              :                         bool no_formal_args)
    2057              : {
    2058       426540 :   gfc_symbol *sym = NULL;
    2059       426540 :   gfc_symtree *parent_st;
    2060       426540 :   gfc_expr *e;
    2061       426540 :   gfc_component *comp;
    2062       426540 :   int save_need_full_assumed_size;
    2063       426540 :   bool return_value = false;
    2064       426540 :   bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
    2065              : 
    2066       426540 :   actual_arg = true;
    2067       426540 :   first_actual_arg = true;
    2068              : 
    2069      1095481 :   for (; arg; arg = arg->next)
    2070              :     {
    2071       669042 :       e = arg->expr;
    2072       669042 :       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       666606 :       if (e->expr_type == EXPR_VARIABLE
    2089       294145 :             && e->symtree->n.sym->attr.generic
    2090            8 :             && no_formal_args
    2091       666611 :             && count_specific_procs (e) != 1)
    2092            2 :         goto cleanup;
    2093              : 
    2094       666604 :       if (e->ts.type != BT_PROCEDURE)
    2095              :         {
    2096       594162 :           save_need_full_assumed_size = need_full_assumed_size;
    2097       594162 :           if (e->expr_type != EXPR_VARIABLE)
    2098       372461 :             need_full_assumed_size = 0;
    2099       594162 :           if (!gfc_resolve_expr (e))
    2100           60 :             goto cleanup;
    2101       594102 :           need_full_assumed_size = save_need_full_assumed_size;
    2102       594102 :           goto argument_list;
    2103              :         }
    2104              : 
    2105              :       /* See if the expression node should really be a variable reference.  */
    2106              : 
    2107        72442 :       sym = e->symtree->n.sym;
    2108              : 
    2109        72442 :       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        72439 :       if (sym->attr.flavor == FL_PROCEDURE
    2117        69039 :           || sym->attr.intrinsic
    2118        69039 :           || 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        69039 :       if (was_declared (sym) || sym->ns->parent == NULL)
    2197        68946 :         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        69039 :       e->expr_type = EXPR_VARIABLE;
    2222        69039 :       e->ts = sym->ts;
    2223        69039 :       if ((sym->as != NULL && sym->ts.type != BT_CLASS)
    2224        35784 :           || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2225         3834 :               && CLASS_DATA (sym)->as))
    2226              :         {
    2227        38795 :           gfc_array_spec *as
    2228        36025 :             = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
    2229        36025 :           e->rank = as->rank;
    2230        36025 :           e->corank = as->corank;
    2231        36025 :           e->ref = gfc_get_ref ();
    2232        36025 :           e->ref->type = REF_ARRAY;
    2233        36025 :           e->ref->u.ar.type = AR_FULL;
    2234        36025 :           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        69039 :       if (e->expr_type == EXPR_VARIABLE
    2241        69039 :           && 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        69039 :       save_need_full_assumed_size = need_full_assumed_size;
    2250        69039 :       if (e->expr_type != EXPR_VARIABLE)
    2251            0 :         need_full_assumed_size = 0;
    2252        69039 :       if (!gfc_resolve_expr (e))
    2253           22 :         goto cleanup;
    2254        69017 :       need_full_assumed_size = save_need_full_assumed_size;
    2255              : 
    2256       666516 :     argument_list:
    2257              :       /* Check argument list functions %VAL, %LOC and %REF.  There is
    2258              :          nothing to do for %REF.  */
    2259       666516 :       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       666510 :       comp = gfc_get_proc_ptr_comp(e);
    2306       666510 :       if (e->expr_type == EXPR_VARIABLE
    2307       292767 :           && 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       292767 :       if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
    2316       666955 :           && 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       666507 :       if (e->expr_type == EXPR_VARIABLE
    2324       292764 :           && 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       666505 :       first_actual_arg = false;
    2348              :     }
    2349              : 
    2350              :   return_value = true;
    2351              : 
    2352       426540 : cleanup:
    2353       426540 :   actual_arg = actual_arg_sav;
    2354       426540 :   first_actual_arg = first_actual_arg_sav;
    2355              : 
    2356       426540 :   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       324713 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
    2366              : {
    2367       324713 :   gfc_actual_arglist *arg0;
    2368       324713 :   gfc_actual_arglist *arg;
    2369       324713 :   gfc_symbol *esym = NULL;
    2370       324713 :   gfc_intrinsic_sym *isym = NULL;
    2371       324713 :   gfc_expr *e = NULL;
    2372       324713 :   gfc_intrinsic_arg *iformal = NULL;
    2373       324713 :   gfc_formal_arglist *eformal = NULL;
    2374       324713 :   bool formal_optional = false;
    2375       324713 :   bool set_by_optional = false;
    2376       324713 :   int i;
    2377       324713 :   int rank = 0;
    2378              : 
    2379              :   /* Is this an elemental procedure?  */
    2380       324713 :   if (expr && expr->value.function.actual != NULL)
    2381              :     {
    2382       235404 :       if (expr->value.function.esym != NULL
    2383        43817 :           && expr->value.function.esym->attr.elemental)
    2384              :         {
    2385              :           arg0 = expr->value.function.actual;
    2386              :           esym = expr->value.function.esym;
    2387              :         }
    2388       219096 :       else if (expr->value.function.isym != NULL
    2389       190533 :                && 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        89309 :   else if (c && c->ext.actual != NULL)
    2398              :     {
    2399        70765 :       arg0 = c->ext.actual;
    2400              : 
    2401        70765 :       if (c->resolved_sym)
    2402              :         esym = c->resolved_sym;
    2403              :       else
    2404          313 :         esym = c->symtree->n.sym;
    2405        70765 :       gcc_assert (esym);
    2406              : 
    2407        70765 :       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       173925 :   for (arg = arg0; arg; arg = arg->next)
    2415              :     {
    2416       112740 :       if (arg->expr != NULL && arg->expr->rank != 0)
    2417              :         {
    2418        10704 :           rank = arg->expr->rank;
    2419        10704 :           if (arg->expr->expr_type == EXPR_VARIABLE
    2420         5484 :               && arg->expr->symtree->n.sym->attr.optional)
    2421        10704 :             set_by_optional = true;
    2422              : 
    2423              :           /* Function specific; set the result rank and shape.  */
    2424        10704 :           if (expr)
    2425              :             {
    2426         8302 :               expr->rank = rank;
    2427         8302 :               expr->corank = arg->expr->corank;
    2428         8302 :               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        71889 :   formal_optional = false;
    2444        71889 :   if (isym)
    2445        49374 :     iformal = isym->formal;
    2446              :   else
    2447        22515 :     eformal = esym->formal;
    2448              : 
    2449       190145 :   for (arg = arg0; arg; arg = arg->next)
    2450              :     {
    2451       118256 :       if (eformal)
    2452              :         {
    2453        40405 :           if (eformal->sym && eformal->sym->attr.optional)
    2454        40405 :             formal_optional = true;
    2455        40405 :           eformal = eformal->next;
    2456              :         }
    2457        77851 :       else if (isym && iformal)
    2458              :         {
    2459        67607 :           if (iformal->optional)
    2460        13418 :             formal_optional = true;
    2461        67607 :           iformal = iformal->next;
    2462              :         }
    2463        10244 :       else if (isym)
    2464        10236 :         formal_optional = true;
    2465              : 
    2466       118256 :       if (pedantic && arg->expr != NULL
    2467        69049 :           && arg->expr->expr_type == EXPR_VARIABLE
    2468        32640 :           && 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       190134 :   for (arg = arg0; arg; arg = arg->next)
    2502              :     {
    2503       118254 :       if (arg->expr == NULL || arg->expr->rank == 0)
    2504       104662 :         continue;
    2505              : 
    2506              :       /* Being elemental, the last upper bound of an assumed size array
    2507              :          argument must be present.  */
    2508        13592 :       if (resolve_assumed_size_actual (arg->expr))
    2509              :         return false;
    2510              : 
    2511              :       /* Elemental procedure's array actual arguments must conform.  */
    2512        13589 :       if (e != NULL)
    2513              :         {
    2514         2888 :           if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
    2515              :             return false;
    2516              :         }
    2517              :       else
    2518        10701 :         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        71880 :   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        14942 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2559              : {
    2560        14942 :   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        14942 : not_entry_self_reference  (gfc_symbol *sym, gfc_namespace *gsym_ns)
    2574              : {
    2575        14942 :   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        15730 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
    2602              : {
    2603        15730 :   gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
    2604              : 
    2605        58842 :   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        15346 :   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        15203 :   if (sym->attr.elemental && !sym->attr.intrinsic)  /* (4)  */
    2708              :     {
    2709            7 :       strncpy (errmsg, _("elemental procedure"), err_len);
    2710            7 :       return true;
    2711              :     }
    2712        15196 :   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        29430 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
    2724              : {
    2725        29430 :   gfc_gsymbol * gsym;
    2726        29430 :   gfc_namespace *ns;
    2727        29430 :   enum gfc_symbol_type type;
    2728        29430 :   char reason[200];
    2729              : 
    2730        29430 :   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
    2731              : 
    2732        29430 :   gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
    2733        29430 :                           sym->binding_label != NULL);
    2734              : 
    2735        29430 :   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
    2736           10 :     gfc_global_used (gsym, where);
    2737              : 
    2738        29430 :   if ((sym->attr.if_source == IFSRC_UNKNOWN
    2739         9280 :        || sym->attr.if_source == IFSRC_IFBODY)
    2740        25035 :       && gsym->type != GSYM_UNKNOWN
    2741        22869 :       && !gsym->binding_label
    2742        20570 :       && gsym->ns
    2743        14942 :       && gsym->ns->proc_name
    2744        14942 :       && not_in_recursive (sym, gsym->ns)
    2745        44372 :       && not_entry_self_reference (sym, gsym->ns))
    2746              :     {
    2747        14942 :       gfc_symbol *def_sym;
    2748        14942 :       def_sym = gsym->ns->proc_name;
    2749              : 
    2750        14942 :       if (gsym->ns->resolved != -1)
    2751              :         {
    2752              : 
    2753              :           /* Resolve the gsymbol namespace if needed.  */
    2754        14920 :           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        14920 :           ns = gfc_global_ns_list;
    2776        25309 :           for (; ns && ns != gsym->ns; ns = ns->sibling)
    2777              :             {
    2778        16913 :               if (ns->sibling == gsym->ns)
    2779              :                 {
    2780         6524 :                   ns->sibling = gsym->ns->sibling;
    2781         6524 :                   gsym->ns->sibling = gfc_global_ns_list;
    2782         6524 :                   gfc_global_ns_list = gsym->ns;
    2783         6524 :                   break;
    2784              :                 }
    2785              :             }
    2786              : 
    2787              :           /* This can happen if a binding name has been specified.  */
    2788        14920 :           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        14942 :       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        14942 :       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        14936 :       if (sym->attr.if_source == IFSRC_UNKNOWN
    2817        14936 :           && 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        14928 :       bool bad_result_characteristics;
    2825        14928 :       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        29430 : done:
    2846              : 
    2847        29430 :   if (gsym->type == GSYM_UNKNOWN)
    2848              :     {
    2849         3962 :       gsym->type = type;
    2850         3962 :       gsym->where = *where;
    2851              :     }
    2852              : 
    2853        29430 :   gsym->used = 1;
    2854        29430 : }
    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        27453 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
    2864              : {
    2865        27453 :   gfc_symbol *s;
    2866              : 
    2867        27453 :   if (sym->attr.generic)
    2868              :     {
    2869        26348 :       s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
    2870        26348 :       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         7679 :   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        27312 : resolve_generic_f (gfc_expr *expr)
    2909              : {
    2910        27312 :   gfc_symbol *sym;
    2911        27312 :   match m;
    2912        27312 :   gfc_interface *intr = NULL;
    2913              : 
    2914        27312 :   sym = expr->symtree->n.sym;
    2915              : 
    2916        27453 :   for (;;)
    2917              :     {
    2918        27453 :       m = resolve_generic_f0 (expr, sym);
    2919        27453 :       if (m == MATCH_YES)
    2920              :         return true;
    2921         6619 :       else if (m == MATCH_ERROR)
    2922              :         return false;
    2923              : 
    2924         6619 : generic:
    2925         6622 :       if (!intr)
    2926         6593 :         for (intr = sym->generic; intr; intr = intr->next)
    2927         6509 :           if (gfc_fl_struct (intr->sym->attr.flavor))
    2928              :             break;
    2929              : 
    2930         6622 :       if (sym->ns->parent == NULL)
    2931              :         break;
    2932          289 :       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
    2933              : 
    2934          289 :       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         6478 :   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         6473 :   if (intr)
    2955              :     {
    2956         6438 :       if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
    2957              :                                                  NULL, false))
    2958              :         return false;
    2959         6411 :       if (!gfc_use_derived (expr->ts.u.derived))
    2960              :         return false;
    2961         6411 :       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        28065 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
    2981              : {
    2982        28065 :   match m;
    2983              : 
    2984        28065 :   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
    2985              :     {
    2986         8123 :       if (sym->attr.dummy)
    2987              :         {
    2988          276 :           sym->attr.proc = PROC_DUMMY;
    2989          276 :           goto found;
    2990              :         }
    2991              : 
    2992         7847 :       sym->attr.proc = PROC_EXTERNAL;
    2993         7847 :       goto found;
    2994              :     }
    2995              : 
    2996        19942 :   if (sym->attr.proc == PROC_MODULE
    2997              :       || sym->attr.proc == PROC_ST_FUNCTION
    2998              :       || sym->attr.proc == PROC_INTERNAL)
    2999        19204 :     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        27327 : found:
    3016        27327 :   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
    3017              : 
    3018        27327 :   if (sym->result)
    3019        27327 :     expr->ts = sym->result->ts;
    3020              :   else
    3021            0 :     expr->ts = sym->ts;
    3022        27327 :   expr->value.function.name = sym->name;
    3023        27327 :   expr->value.function.esym = sym;
    3024              :   /* Prevent crash when sym->ts.u.derived->components is not set due to previous
    3025              :      error(s).  */
    3026        27327 :   if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
    3027              :     return MATCH_ERROR;
    3028        27326 :   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        27004 :   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        28058 : resolve_specific_f (gfc_expr *expr)
    3045              : {
    3046        28058 :   gfc_symbol *sym;
    3047        28058 :   match m;
    3048              : 
    3049        28058 :   sym = expr->symtree->n.sym;
    3050              : 
    3051        28065 :   for (;;)
    3052              :     {
    3053        28065 :       m = resolve_specific_f0 (sym, expr);
    3054        28065 :       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       275864 : resolve_unknown_f (gfc_expr *expr)
    3116              : {
    3117       275864 :   gfc_symbol *sym;
    3118       275864 :   gfc_typespec *ts;
    3119              : 
    3120       275864 :   sym = expr->symtree->n.sym;
    3121              : 
    3122       275864 :   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       275575 :   if (gfc_is_intrinsic (sym, 0, expr->where))
    3132              :     {
    3133       273318 :       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       850488 : is_external_proc (gfc_symbol *sym)
    3199              : {
    3200       848797 :   if (!sym->attr.dummy && !sym->attr.contained
    3201       740787 :         && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
    3202       161349 :         && sym->attr.proc != PROC_ST_FUNCTION
    3203       160754 :         && !sym->attr.proc_pointer
    3204       159560 :         && !sym->attr.use_assoc
    3205       909379 :         && 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       255760 : gfc_pure_function (gfc_expr *e, const char **name)
    3220              : {
    3221       255760 :   bool pure;
    3222       255760 :   gfc_component *comp;
    3223              : 
    3224       255760 :   *name = NULL;
    3225              : 
    3226       255760 :   if (e->symtree != NULL
    3227       255406 :         && e->symtree->n.sym != NULL
    3228       255406 :         && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
    3229          305 :     return pure_stmt_function (e, e->symtree->n.sym);
    3230              : 
    3231       255455 :   comp = gfc_get_proc_ptr_comp (e);
    3232       255455 :   if (comp)
    3233              :     {
    3234          465 :       pure = gfc_pure (comp->ts.interface);
    3235          465 :       *name = comp->name;
    3236              :     }
    3237       254990 :   else if (e->value.function.esym)
    3238              :     {
    3239        52646 :       pure = gfc_pure (e->value.function.esym);
    3240        52646 :       *name = e->value.function.esym->name;
    3241              :     }
    3242       202344 :   else if (e->value.function.isym)
    3243              :     {
    3244       402550 :       pure = e->value.function.isym->pure
    3245       201275 :              || e->value.function.isym->elemental;
    3246       201275 :       *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        38057 : gfc_implicit_pure_function (gfc_expr *e)
    3270              : {
    3271        38057 :   gfc_component *comp = gfc_get_proc_ptr_comp (e);
    3272        38057 :   if (comp)
    3273          449 :     return gfc_implicit_pure (comp->ts.interface);
    3274        37608 :   else if (e->value.function.esym)
    3275        32205 :     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       243871 : static bool check_pure_function (gfc_expr *e)
    3309              : {
    3310       243871 :   const char *name = NULL;
    3311       243871 :   code_stack *stack;
    3312       243871 :   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       564440 :   for (stack = cs_base; stack; stack = stack->prev)
    3320              :     {
    3321       320571 :       if (!saw_block && stack->current->op == EXEC_BLOCK)
    3322              :         {
    3323         7282 :           saw_block = true;
    3324         7282 :           continue;
    3325              :         }
    3326              : 
    3327         5221 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3328              :         {
    3329           10 :           bool is_pure;
    3330       320569 :           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       243869 :   if (!gfc_pure_function (e, &name) && name)
    3346              :     {
    3347        36778 :       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        36774 :       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        36772 :       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        36767 :       if (!gfc_implicit_pure_function (e))
    3368        30334 :         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       132331 : 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       132331 :   gfc_namespace *sibling = gfc_current_ns->sibling;
    3383       249929 :   for (; sibling; sibling = sibling->sibling)
    3384              :     {
    3385       124629 :       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       132331 :   if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
    3395        68012 :       && gfc_current_ns->proc_name)
    3396        67968 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3397       132331 : }
    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       344416 : resolve_function (gfc_expr *expr)
    3405              : {
    3406       344416 :   gfc_actual_arglist *arg;
    3407       344416 :   gfc_symbol *sym;
    3408       344416 :   bool t;
    3409       344416 :   int temp;
    3410       344416 :   procedure_type p = PROC_INTRINSIC;
    3411       344416 :   bool no_formal_args;
    3412              : 
    3413       344416 :   sym = NULL;
    3414       344416 :   if (expr->symtree)
    3415       344062 :     sym = expr->symtree->n.sym;
    3416              : 
    3417              :   /* If this is a procedure pointer component, it has already been resolved.  */
    3418       344416 :   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       344018 :   if (sym && sym->attr.intrinsic
    3424         8606 :       && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
    3425         8606 :           || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
    3426              :     return true;
    3427              : 
    3428       344018 :   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       343663 :   if (sym && sym->attr.intrinsic
    3436       352623 :       && !gfc_resolve_intrinsic (sym, &expr->where))
    3437              :     return false;
    3438              : 
    3439       344017 :   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       343659 :   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       343658 :   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       344011 :   need_full_assumed_size++;
    3470              : 
    3471       344011 :   if (expr->symtree && expr->symtree->n.sym)
    3472       343657 :     p = expr->symtree->n.sym->attr.proc;
    3473              : 
    3474       344011 :   if (expr->value.function.isym && expr->value.function.isym->inquiry)
    3475         1105 :     inquiry_argument = true;
    3476       343657 :   no_formal_args = sym && is_external_proc (sym)
    3477       357817 :                        && gfc_sym_get_dummy_args (sym) == NULL;
    3478              : 
    3479       344011 :   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       343944 :   inquiry_argument = false;
    3487              : 
    3488              :   /* Resume assumed_size checking.  */
    3489       343944 :   need_full_assumed_size--;
    3490              : 
    3491              :   /* If the procedure is external, check for usage.  */
    3492       343944 :   if (sym && is_external_proc (sym))
    3493        13786 :     resolve_global_procedure (sym, &expr->where, 0);
    3494              : 
    3495       343944 :   if (sym && sym->ts.type == BT_CHARACTER
    3496         3291 :       && sym->ts.u.cl
    3497         3231 :       && sym->ts.u.cl->length == NULL
    3498          671 :       && !sym->attr.dummy
    3499          664 :       && !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       343943 :   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       343943 :   if (expr->value.function.name != NULL
    3539       332032 :       || expr->value.function.isym != NULL)
    3540              :     {
    3541        12709 :       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       331234 :       switch (procedure_kind (sym))
    3550              :         {
    3551        27312 :         case PTYPE_GENERIC:
    3552        27312 :           t = resolve_generic_f (expr);
    3553        27312 :           break;
    3554              : 
    3555        28058 :         case PTYPE_SPECIFIC:
    3556        28058 :           t = resolve_specific_f (expr);
    3557        28058 :           break;
    3558              : 
    3559       275864 :         case PTYPE_UNKNOWN:
    3560       275864 :           t = resolve_unknown_f (expr);
    3561       275864 :           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       343943 :   if (expr->expr_type != EXPR_FUNCTION)
    3572              :     return t;
    3573              : 
    3574              :   /* Walk the argument list looking for invalid BOZ.  */
    3575       738566 :   for (arg = expr->value.function.actual; arg; arg = arg->next)
    3576       495137 :     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       243429 :   temp = need_full_assumed_size;
    3585       243429 :   need_full_assumed_size = 0;
    3586              : 
    3587       243429 :   if (!resolve_elemental_actual (expr, NULL))
    3588              :     return false;
    3589              : 
    3590       243426 :   if (omp_workshare_flag
    3591           32 :       && expr->value.function.esym
    3592       243431 :       && ! 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       243422 :   else if (expr->value.function.actual != NULL
    3602       235401 :            && expr->value.function.isym != NULL
    3603       190532 :            && 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       536474 :       for (arg = expr->value.function.actual; arg; arg = arg->next)
    3617              :         {
    3618       371760 :           if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
    3619        45431 :               && arg == expr->value.function.actual
    3620        16747 :               && arg->next != NULL && arg->next->expr)
    3621              :             {
    3622         8260 :               if (arg->next->expr->expr_type != EXPR_CONSTANT)
    3623              :                 break;
    3624              : 
    3625         8036 :               if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
    3626              :                 break;
    3627              : 
    3628         8036 :               if ((int)mpz_get_si (arg->next->expr->value.integer)
    3629         8036 :                         < arg->expr->rank)
    3630              :                 break;
    3631              :             }
    3632              : 
    3633       369357 :           if (arg->expr != NULL
    3634       246427 :               && arg->expr->rank > 0
    3635       487988 :               && resolve_assumed_size_actual (arg->expr))
    3636              :             return false;
    3637              :         }
    3638              :     }
    3639              : #undef GENERIC_ID
    3640              : 
    3641       243423 :   need_full_assumed_size = temp;
    3642              : 
    3643       243423 :   if (!check_pure_function(expr))
    3644           12 :     t = false;
    3645              : 
    3646              :   /* Functions without the RECURSIVE attribution are not allowed to
    3647              :    * call themselves.  */
    3648       243423 :   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
    3649              :     {
    3650        51389 :       gfc_symbol *esym;
    3651        51389 :       esym = expr->value.function.esym;
    3652              : 
    3653        51389 :       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       243423 :   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
    3672         3429 :       && 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       243423 :   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       243423 :   if (expr->ts.type == BT_DERIVED
    3690         9416 :       && !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       243423 :   if (!expr->ref && !expr->value.function.isym)
    3705              :     {
    3706        52770 :       if (expr->value.function.esym)
    3707        51701 :         update_current_proc_array_outer_dependency (expr->value.function.esym);
    3708              :       else
    3709         1069 :         update_current_proc_array_outer_dependency (sym);
    3710              :     }
    3711       190653 :   else if (expr->ref)
    3712              :     /* typebound procedure: Assume the worst.  */
    3713            0 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    3714              : 
    3715       243423 :   if (expr->value.function.esym
    3716        51701 :       && 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       243423 :   if (expr->expr_type == EXPR_FUNCTION
    3724       243423 :       && expr->symtree
    3725       243069 :       && expr->symtree->n.sym->attr.dummy
    3726          564 :       && expr->symtree->n.sym->ns->has_implicit_none_export
    3727       243424 :       && !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        77078 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
    3742              : {
    3743        77078 :   code_stack *stack;
    3744        77078 :   bool saw_block = false;
    3745              : 
    3746        77078 :   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       158948 :   for (stack = cs_base; stack; stack = stack->prev)
    3755              :     {
    3756        87515 :       if (stack->current->op == EXEC_BLOCK)
    3757              :         {
    3758         1896 :           saw_block = true;
    3759         1896 :           continue;
    3760              :         }
    3761              : 
    3762        85619 :       if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
    3763              :         {
    3764              : 
    3765            2 :           bool is_pure = true;
    3766        87515 :           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        71433 :   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        71433 :   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        71427 :   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        71423 :   gfc_unset_implicit_pure (NULL);
    3796        71423 :   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        62582 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
    3879              : {
    3880        62582 :   match m;
    3881              : 
    3882        62582 :   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        56921 :   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
    3895        56921 :     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        62582 : found:
    3912        62582 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3913              : 
    3914        62582 :   c->resolved_sym = sym;
    3915        62582 :   if (!pure_subroutine (sym, sym->name, &c->loc))
    3916              :     return MATCH_ERROR;
    3917              : 
    3918              :   return MATCH_YES;
    3919              : }
    3920              : 
    3921              : 
    3922              : static bool
    3923        62582 : resolve_specific_s (gfc_code *c)
    3924              : {
    3925        62582 :   gfc_symbol *sym;
    3926        62582 :   match m;
    3927              : 
    3928        62582 :   sym = c->symtree->n.sym;
    3929              : 
    3930        62582 :   for (;;)
    3931              :     {
    3932        62582 :       m = resolve_specific_s0 (c, sym);
    3933        62582 :       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        15787 : resolve_unknown_s (gfc_code *c)
    3959              : {
    3960        15787 :   gfc_symbol *sym;
    3961              : 
    3962        15787 :   sym = c->symtree->n.sym;
    3963              : 
    3964        15787 :   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        15767 :   if (gfc_is_intrinsic (sym, 1, c->loc))
    3973              :     {
    3974         4217 :       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        11550 : found:
    3982        11570 :   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
    3983              : 
    3984        11570 :   c->resolved_sym = sym;
    3985              : 
    3986        11570 :   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        81325 : resolve_call (gfc_code *c)
    4188              : {
    4189        81325 :   bool t;
    4190        81325 :   procedure_type ptype = PROC_INTRINSIC;
    4191        81325 :   gfc_symbol *csym, *sym;
    4192        81325 :   bool no_formal_args;
    4193              : 
    4194        81325 :   csym = c->symtree ? c->symtree->n.sym : NULL;
    4195              : 
    4196        81325 :   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        81321 :   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        81321 :   if (!c->expr1 && csym)
    4224              :     {
    4225        79606 :       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        79605 :       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        81320 :           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        81320 :   need_full_assumed_size++;
    4251              : 
    4252        81320 :   if (csym)
    4253        81320 :     ptype = csym->attr.proc;
    4254              : 
    4255        81320 :   no_formal_args = csym && is_external_proc (csym)
    4256        15650 :                         && gfc_sym_get_dummy_args (csym) == NULL;
    4257        81320 :   if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
    4258              :     return false;
    4259              : 
    4260              :   /* Resume assumed_size checking.  */
    4261        81286 :   need_full_assumed_size--;
    4262              : 
    4263              :   /* If 'implicit none (external)' and the symbol is a dummy argument,
    4264              :      check for an 'external' attribute.  */
    4265        81286 :   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        81285 :   if (csym && is_external_proc (csym))
    4275        15644 :     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        81285 :   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        81285 :   t = true;
    4315        81285 :   if (c->resolved_sym == NULL)
    4316              :     {
    4317        81180 :       c->resolved_isym = NULL;
    4318        81180 :       switch (procedure_kind (csym))
    4319              :         {
    4320         2811 :         case PTYPE_GENERIC:
    4321         2811 :           t = resolve_generic_s (c);
    4322         2811 :           break;
    4323              : 
    4324        62582 :         case PTYPE_SPECIFIC:
    4325        62582 :           t = resolve_specific_s (c);
    4326        62582 :           break;
    4327              : 
    4328        15787 :         case PTYPE_UNKNOWN:
    4329        15787 :           t = resolve_unknown_s (c);
    4330        15787 :           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        81284 :   if (!resolve_elemental_actual (NULL, c))
    4339              :     return false;
    4340              : 
    4341              :   /* Deal with complicated dependencies that the scalarizer cannot handle.  */
    4342        81276 :   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        81276 :   if (!c->expr1)
    4347        79561 :     update_current_proc_array_outer_dependency (csym);
    4348              :   else
    4349              :     /* Typebound procedure: Assume the worst.  */
    4350         1715 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    4351              : 
    4352        81276 :   if (c->resolved_sym
    4353        80963 :       && 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        81276 :   csym = c->resolved_sym ? c->resolved_sym : csym;
    4359        81276 :   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        32321 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
    4375              : {
    4376        32321 :   bool t;
    4377        32321 :   int i;
    4378              : 
    4379        32321 :   t = true;
    4380              : 
    4381        32321 :   if (op1->shape != NULL && op2->shape != NULL)
    4382              :     {
    4383        42926 :       for (i = 0; i < op1->rank; i++)
    4384              :         {
    4385        22900 :           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        32321 :   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       193146 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
    4527              :                           void *data)
    4528              : {
    4529       193146 :   gfc_expr *f = *e;
    4530       193146 :   const char *name;
    4531       193146 :   static gfc_expr *last = NULL;
    4532       193146 :   bool *found = (bool *) data;
    4533              : 
    4534       193146 :   if (f->expr_type == EXPR_FUNCTION)
    4535              :     {
    4536        11860 :       *found = 1;
    4537        11860 :       if (f != last && !gfc_pure_function (f, &name)
    4538        13145 :           && !gfc_implicit_pure_function (f))
    4539              :         {
    4540         1146 :           if (name)
    4541         1146 :             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        11860 :       last = f;
    4550              :     }
    4551              : 
    4552       193146 :   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       531818 : resolve_operator (gfc_expr *e)
    4607              : {
    4608       531818 :   gfc_expr *op1, *op2;
    4609              :   /* One error uses 3 names; additional space for wording (also via gettext). */
    4610       531818 :   bool t = true;
    4611              : 
    4612              :   /* Reduce stacked parentheses to single pair  */
    4613       531818 :   while (e->expr_type == EXPR_OP
    4614       531976 :          && e->value.op.op == INTRINSIC_PARENTHESES
    4615        23499 :          && e->value.op.op1->expr_type == EXPR_OP
    4616       548808 :          && 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       531818 :   switch (e->value.op.op)
    4625              :     {
    4626       479829 :     default:
    4627       479829 :       if (!gfc_resolve_expr (e->value.op.op2))
    4628       531818 :         t = false;
    4629              : 
    4630              :     /* Fall through.  */
    4631              : 
    4632       531818 :     case INTRINSIC_NOT:
    4633       531818 :     case INTRINSIC_UPLUS:
    4634       531818 :     case INTRINSIC_UMINUS:
    4635       531818 :     case INTRINSIC_PARENTHESES:
    4636       531818 :       if (!gfc_resolve_expr (e->value.op.op1))
    4637              :         return false;
    4638       531657 :       if (e->value.op.op1
    4639       531648 :           && 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       531657 :       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       531655 :       break;
    4654              :     }
    4655              : 
    4656              :   /* Typecheck the new node.  */
    4657              : 
    4658       531655 :   op1 = e->value.op.op1;
    4659       531655 :   op2 = e->value.op.op2;
    4660       531655 :   if (op1 == NULL && op2 == NULL)
    4661              :     return false;
    4662              :   /* Error out if op2 did not resolve. We already diagnosed op1.  */
    4663       531646 :   if (t == false)
    4664              :     return false;
    4665              : 
    4666              :   /* op1 and op2 cannot both be BOZ.  */
    4667       531580 :   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       531580 :   if ((op1 && op1->expr_type == EXPR_NULL)
    4677       531578 :       || (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       531577 :   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       155135 :     case INTRINSIC_POWER:
    4703       155135 :     case INTRINSIC_PLUS:
    4704       155135 :     case INTRINSIC_MINUS:
    4705       155135 :     case INTRINSIC_TIMES:
    4706       155135 :     case INTRINSIC_DIVIDE:
    4707              : 
    4708              :       /* UNSIGNED cannot appear in a mixed expression without explicit
    4709              :              conversion.  */
    4710       155135 :       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       155132 :       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       154678 :           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       154642 :           gfc_type_convert_binary (e, 1);
    4733       154642 :           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        69639 :     case INTRINSIC_AND:
    4768        69639 :     case INTRINSIC_OR:
    4769        69639 :     case INTRINSIC_EQV:
    4770        69639 :     case INTRINSIC_NEQV:
    4771        69639 :       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
    4772              :         {
    4773        69088 :           e->ts.type = BT_LOGICAL;
    4774        69088 :           e->ts.kind = gfc_kind_max (op1, op2);
    4775        69088 :           if (op1->ts.kind < e->ts.kind)
    4776          140 :             gfc_convert_type (op1, &e->ts, 2);
    4777        68948 :           else if (op2->ts.kind < e->ts.kind)
    4778          117 :             gfc_convert_type (op2, &e->ts, 2);
    4779              : 
    4780        69088 :           if (flag_frontend_optimize &&
    4781        58035 :             (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        52030 :               bool op2_f = false;
    4786        52030 :               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        20526 :     case INTRINSIC_NOT:
    4812              :       /* Logical ops on integers become bitwise ops with -fdec.  */
    4813        20526 :       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        20507 :       if (op1->ts.type == BT_LOGICAL)
    4822              :         {
    4823        20501 :           e->ts.type = BT_LOGICAL;
    4824        20501 :           e->ts.kind = op1->ts.kind;
    4825        20501 :           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        21317 :     case INTRINSIC_GT:
    4834        21317 :     case INTRINSIC_GT_OS:
    4835        21317 :     case INTRINSIC_GE:
    4836        21317 :     case INTRINSIC_GE_OS:
    4837        21317 :     case INTRINSIC_LT:
    4838        21317 :     case INTRINSIC_LT_OS:
    4839        21317 :     case INTRINSIC_LE:
    4840        21317 :     case INTRINSIC_LE_OS:
    4841        21317 :       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       252301 :     case INTRINSIC_EQ:
    4851       252301 :     case INTRINSIC_EQ_OS:
    4852       252301 :     case INTRINSIC_NE:
    4853       252301 :     case INTRINSIC_NE_OS:
    4854              : 
    4855       252301 :       if (flag_dec
    4856         1038 :           && is_character_based (op1->ts.type)
    4857       252636 :           && is_character_based (op2->ts.type))
    4858              :         {
    4859          204 :           convert_hollerith_to_character (op1);
    4860          204 :           convert_hollerith_to_character (op2);
    4861              :         }
    4862              : 
    4863       252301 :       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
    4864        37947 :           && op1->ts.kind == op2->ts.kind)
    4865              :         {
    4866        37910 :           e->ts.type = BT_LOGICAL;
    4867        37910 :           e->ts.kind = gfc_default_logical_kind;
    4868        37910 :           break;
    4869              :         }
    4870              : 
    4871              :       /* If op1 is BOZ, then op2 is not!.  Try to convert to type of op2.  */
    4872       214391 :       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       214391 :       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       214391 :       if (flag_dec
    4901       214391 :           && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
    4902          120 :         convert_to_numeric (op1, op2);
    4903              : 
    4904       214391 :       if (flag_dec
    4905       214391 :           && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
    4906          120 :         convert_to_numeric (op2, op1);
    4907              : 
    4908       214391 :       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       213262 :           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       213192 :           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       213191 :           gfc_type_convert_binary (e, 1);
    4931              : 
    4932       213191 :           e->ts.type = BT_LOGICAL;
    4933       213191 :           e->ts.kind = gfc_default_logical_kind;
    4934              : 
    4935       213191 :           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       528905 :   switch (e->value.op.op)
    5021              :     {
    5022       477065 :     case INTRINSIC_PLUS:
    5023       477065 :     case INTRINSIC_MINUS:
    5024       477065 :     case INTRINSIC_TIMES:
    5025       477065 :     case INTRINSIC_DIVIDE:
    5026       477065 :     case INTRINSIC_POWER:
    5027       477065 :     case INTRINSIC_CONCAT:
    5028       477065 :     case INTRINSIC_AND:
    5029       477065 :     case INTRINSIC_OR:
    5030       477065 :     case INTRINSIC_EQV:
    5031       477065 :     case INTRINSIC_NEQV:
    5032       477065 :     case INTRINSIC_EQ:
    5033       477065 :     case INTRINSIC_EQ_OS:
    5034       477065 :     case INTRINSIC_NE:
    5035       477065 :     case INTRINSIC_NE_OS:
    5036       477065 :     case INTRINSIC_GT:
    5037       477065 :     case INTRINSIC_GT_OS:
    5038       477065 :     case INTRINSIC_GE:
    5039       477065 :     case INTRINSIC_GE_OS:
    5040       477065 :     case INTRINSIC_LT:
    5041       477065 :     case INTRINSIC_LT_OS:
    5042       477065 :     case INTRINSIC_LE:
    5043       477065 :     case INTRINSIC_LE_OS:
    5044              : 
    5045       477065 :       if (op1->rank == 0 && op2->rank == 0)
    5046       425115 :         e->rank = 0;
    5047              : 
    5048       477065 :       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       477065 :       if (op1->rank != 0 && op2->rank == 0)
    5057              :         {
    5058        17039 :           e->rank = op1->rank;
    5059              : 
    5060        17039 :           if (e->shape == NULL)
    5061        17015 :             e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5062              :         }
    5063              : 
    5064       477065 :       if (op1->rank != 0 && op2->rank != 0)
    5065              :         {
    5066        32382 :           if (op1->rank == op2->rank)
    5067              :             {
    5068        32382 :               e->rank = op1->rank;
    5069        32382 :               if (e->shape == NULL)
    5070              :                 {
    5071        32321 :                   t = compare_shapes (op1, op2);
    5072        32321 :                   if (!t)
    5073            3 :                     e->shape = NULL;
    5074              :                   else
    5075        32318 :                     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        51840 :     case INTRINSIC_PARENTHESES:
    5093        51840 :     case INTRINSIC_NOT:
    5094        51840 :     case INTRINSIC_UPLUS:
    5095        51840 :     case INTRINSIC_UMINUS:
    5096              :       /* Simply copy arrayness attribute */
    5097        51840 :       e->rank = op1->rank;
    5098        51840 :       e->corank = op1->corank;
    5099              : 
    5100        51840 :       if (e->shape == NULL)
    5101        51833 :         e->shape = gfc_copy_shape (op1->shape, op1->rank);
    5102              : 
    5103              :       break;
    5104              : 
    5105              :     default:
    5106              :       break;
    5107              :     }
    5108              : 
    5109       529447 : simplify_op:
    5110              : 
    5111              :   /* Attempt to simplify the expression.  */
    5112            3 :   if (t)
    5113              :     {
    5114       529444 :       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       529444 :       if (!gfc_is_constant_expr (e))
    5119       483737 :         t = true;
    5120              :     }
    5121              :   return t;
    5122              : }
    5123              : 
    5124              : static bool
    5125          162 : resolve_conditional (gfc_expr *expr)
    5126              : {
    5127          162 :   gfc_expr *condition, *true_expr, *false_expr;
    5128              : 
    5129          162 :   condition = expr->value.conditional.condition;
    5130          162 :   true_expr = expr->value.conditional.true_expr;
    5131          162 :   false_expr = expr->value.conditional.false_expr;
    5132              : 
    5133          324 :   if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
    5134          324 :       || !gfc_resolve_expr (false_expr))
    5135            0 :     return false;
    5136              : 
    5137          162 :   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          160 :   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          159 :   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          158 :   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          157 :   if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
    5171          157 :       && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
    5172           67 :       && true_expr->ts.type != BT_CHARACTER)
    5173              :     {
    5174            1 :       gfc_error (
    5175              :         "Sorry, only integer, logical, real, complex and character types are "
    5176              :         "currently supported for conditional expressions at %L",
    5177              :         &expr->where);
    5178            1 :       return false;
    5179              :     }
    5180              : 
    5181              :   /* TODO: support arrays in conditional expressions  */
    5182          156 :   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          155 :   expr->ts = true_expr->ts;
    5191          155 :   expr->rank = true_expr->rank;
    5192          155 :   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       465059 : compare_bound (gfc_expr *a, gfc_expr *b)
    5204              : {
    5205       465059 :   int i;
    5206              : 
    5207       465059 :   if (a == NULL || a->expr_type != EXPR_CONSTANT
    5208       305598 :       || 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       210946 :   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
    5215              :     return CMP_UNKNOWN;
    5216              : 
    5217       210942 :   i = mpz_cmp (a->value.integer, b->value.integer);
    5218              : 
    5219       210942 :   if (i < 0)
    5220              :     return CMP_LT;
    5221        99447 :   if (i > 0)
    5222        39579 :     return CMP_GT;
    5223              :   return CMP_EQ;
    5224              : }
    5225              : 
    5226              : 
    5227              : /* Compare an integer expression with an integer.  */
    5228              : 
    5229              : static compare_result
    5230        74395 : compare_bound_int (gfc_expr *a, int b)
    5231              : {
    5232        74395 :   int i;
    5233              : 
    5234        74395 :   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        69095 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
    5253              : {
    5254        69095 :   int i;
    5255              : 
    5256        69095 :   if (a == NULL
    5257        56417 :       || a->expr_type != EXPR_CONSTANT
    5258        54294 :       || a->ts.type != BT_INTEGER)
    5259              :     return CMP_UNKNOWN;
    5260              : 
    5261        54291 :   i = mpz_cmp (a->value.integer, b);
    5262              : 
    5263        54291 :   if (i < 0)
    5264              :     return CMP_LT;
    5265        24910 :   if (i > 0)
    5266        10702 :     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        51894 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
    5277              :                                 gfc_expr *stride, mpz_t last)
    5278              : {
    5279        51894 :   mpz_t rem;
    5280              : 
    5281        51894 :   if (start == NULL || start->expr_type != EXPR_CONSTANT
    5282        36809 :       || end == NULL || end->expr_type != EXPR_CONSTANT
    5283        32190 :       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
    5284              :     return 0;
    5285              : 
    5286        31871 :   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
    5287        31870 :       || (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        25350 :       if (compare_bound (start, end) == CMP_GT)
    5293              :         return 0;
    5294        23961 :       mpz_set (last, end->value.integer);
    5295        23961 :       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       215941 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
    5326              : {
    5327       215941 :   mpz_t last_value;
    5328              : 
    5329       215941 :   if (ar->dimen_type[i] == DIMEN_STAR)
    5330              :     {
    5331          498 :       gcc_assert (ar->stride[i] == NULL);
    5332              :       /* This implies [*] as [*:] and [*:3] are not possible.  */
    5333          498 :       if (ar->start[i] == NULL)
    5334              :         {
    5335          406 :           gcc_assert (ar->end[i] == NULL);
    5336              :           return true;
    5337              :         }
    5338              :     }
    5339              : 
    5340              : /* Given start, end and stride values, calculate the minimum and
    5341              :    maximum referenced indexes.  */
    5342              : 
    5343       215535 :   switch (ar->dimen_type[i])
    5344              :     {
    5345              :     case DIMEN_VECTOR:
    5346              :     case DIMEN_THIS_IMAGE:
    5347              :       break;
    5348              : 
    5349       155354 :     case DIMEN_STAR:
    5350       155354 :     case DIMEN_ELEMENT:
    5351       155354 :       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       155352 :       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        51939 :     case DIMEN_RANGE:
    5385        51939 :       {
    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        51939 :         compare_result comp_start_end = compare_bound (AR_START, AR_END);
    5390        51939 :         compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
    5391              : 
    5392              :         /* Check for zero stride, which is not allowed.  */
    5393        51939 :         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        51938 :         if (comp_start_end == CMP_EQ
    5405        51176 :             || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
    5406        48387 :                 && comp_start_end == CMP_LT)
    5407        22776 :             || (comp_stride_zero == CMP_LT
    5408        22776 :                 && comp_start_end == CMP_GT))
    5409              :           {
    5410        30507 :             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        30480 :             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        51894 :         mpz_init (last_value);
    5431        51894 :         if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
    5432              :                                             last_value))
    5433              :           {
    5434        30461 :             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        30458 :             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        51884 :         mpz_clear (last_value);
    5454              : 
    5455              : #undef AR_START
    5456              : #undef AR_END
    5457              :       }
    5458        51884 :       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       425103 : compare_spec_to_ref (gfc_array_ref *ar)
    5472              : {
    5473       425103 :   gfc_array_spec *as;
    5474       425103 :   int i;
    5475              : 
    5476       425103 :   as = ar->as;
    5477       425103 :   i = as->rank - 1;
    5478              :   /* TODO: Full array sections are only allowed as actual parameters.  */
    5479       425103 :   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       425098 :   if (ar->type == AR_FULL)
    5490              :     return true;
    5491              : 
    5492       163897 :   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       163869 :   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       369996 :   for (i = 0; i < as->rank; i++)
    5508       206128 :     if (!check_dimension (i, ar, as))
    5509              :       return false;
    5510              : 
    5511              :   /* Local access has no coarray spec.  */
    5512       163868 :   if (ar->codimen != 0)
    5513        18868 :     for (i = as->rank; i < as->rank + as->corank; i++)
    5514              :       {
    5515         9815 :         if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
    5516         6830 :             && 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         9813 :         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       732817 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
    5534              :                      int force_index_integer_kind)
    5535              : {
    5536       732817 :   gfc_typespec ts;
    5537              : 
    5538       732817 :   if (index == NULL)
    5539              :     return true;
    5540              : 
    5541       217440 :   if (!gfc_resolve_expr (index))
    5542              :     return false;
    5543              : 
    5544       217417 :   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       217415 :   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       217411 :   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       217411 :   if ((index->ts.kind != gfc_index_integer_kind
    5563       212472 :        && force_index_integer_kind)
    5564       186187 :       || (index->ts.type != BT_INTEGER
    5565              :           && index->ts.type != BT_UNKNOWN))
    5566              :     {
    5567        31560 :       gfc_clear_ts (&ts);
    5568        31560 :       ts.type = BT_INTEGER;
    5569        31560 :       ts.kind = gfc_index_integer_kind;
    5570              : 
    5571        31560 :       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       488795 : gfc_resolve_index (gfc_expr *index, int check_scalar)
    5581              : {
    5582       488795 :   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       425829 : resolve_array_ref (gfc_array_ref *ar)
    5703              : {
    5704       425829 :   int i, check_scalar;
    5705       425829 :   gfc_expr *e;
    5706              : 
    5707       669822 :   for (i = 0; i < ar->dimen + ar->codimen; i++)
    5708              :     {
    5709       244022 :       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       244022 :       if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
    5715              :         return false;
    5716       243995 :       if (!gfc_resolve_index (ar->end[i], check_scalar))
    5717              :         return false;
    5718       243993 :       if (!gfc_resolve_index (ar->stride[i], check_scalar))
    5719              :         return false;
    5720              : 
    5721       243993 :       e = ar->start[i];
    5722              : 
    5723       243993 :       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
    5724       145487 :         switch (e->rank)
    5725              :           {
    5726       144395 :           case 0:
    5727       144395 :             ar->dimen_type[i] = DIMEN_ELEMENT;
    5728       144395 :             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       243993 :       if (ar->dimen_type[i] == DIMEN_RANGE
    5748        71632 :           && 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       425800 :   if (ar->type == AR_FULL)
    5778              :     {
    5779       264646 :       if (ar->as->rank == 0)
    5780         3411 :         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       264646 :       ar->dimen = ar->as->rank;
    5785       631267 :       for (i = 0; i < ar->dimen; i++)
    5786              :         {
    5787       366621 :           ar->dimen_type[i] = DIMEN_RANGE;
    5788              : 
    5789       366621 :           gcc_assert (ar->start[i] == NULL);
    5790       366621 :           gcc_assert (ar->end[i] == NULL);
    5791       366621 :           gcc_assert (ar->stride[i] == NULL);
    5792              :         }
    5793              :     }
    5794              : 
    5795              :   /* If the reference type is unknown, figure out what kind it is.  */
    5796              : 
    5797       425800 :   if (ar->type == AR_UNKNOWN)
    5798              :     {
    5799       148091 :       ar->type = AR_ELEMENT;
    5800       286845 :       for (i = 0; i < ar->dimen; i++)
    5801       176607 :         if (ar->dimen_type[i] == DIMEN_RANGE
    5802       176607 :             || ar->dimen_type[i] == DIMEN_VECTOR)
    5803              :           {
    5804        37853 :             ar->type = AR_SECTION;
    5805        37853 :             break;
    5806              :           }
    5807              :     }
    5808              : 
    5809       425800 :   if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
    5810              :     return false;
    5811              : 
    5812       425764 :   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       425764 :   if (ar->codimen)
    5821              :     {
    5822        13630 :       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        13570 :       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        13618 :       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       541572 : gfc_resolve_ref (gfc_expr *expr)
    6115              : {
    6116       541572 :   int current_part_dimension, n_components, seen_part_dimension;
    6117       541572 :   gfc_ref *ref, **prev, *array_ref;
    6118       541572 :   bool equal_length;
    6119       541572 :   gfc_symbol *last_pdt = NULL;
    6120              : 
    6121      1064074 :   for (ref = expr->ref; ref; ref = ref->next)
    6122       523411 :     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      1588292 :   for (prev = &expr->ref; *prev != NULL;
    6130       523465 :        prev = *prev == NULL ? prev : &(*prev)->next)
    6131       523556 :     switch ((*prev)->type)
    6132              :       {
    6133       425829 :       case REF_ARRAY:
    6134       425829 :         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       541474 :   current_part_dimension = 0;
    6163       541474 :   seen_part_dimension = 0;
    6164       541474 :   n_components = 0;
    6165       541474 :   array_ref = NULL;
    6166              : 
    6167       541474 :   if (expr->expr_type == EXPR_VARIABLE && IS_PDT (expr))
    6168          534 :     last_pdt = expr->symtree->n.sym->ts.u.derived;
    6169              : 
    6170      1064711 :   for (ref = expr->ref; ref; ref = ref->next)
    6171              :     {
    6172       523248 :       switch (ref->type)
    6173              :         {
    6174       425739 :         case REF_ARRAY:
    6175       425739 :           array_ref = ref;
    6176       425739 :           switch (ref->u.ar.type)
    6177              :             {
    6178       261233 :             case AR_FULL:
    6179              :               /* Coarray scalar.  */
    6180       261233 :               if (ref->u.ar.as->rank == 0)
    6181              :                 {
    6182              :                   current_part_dimension = 0;
    6183              :                   break;
    6184              :                 }
    6185              :               /* Fall through.  */
    6186       302150 :             case AR_SECTION:
    6187       302150 :               current_part_dimension = 1;
    6188       302150 :               break;
    6189              : 
    6190       123589 :             case AR_ELEMENT:
    6191       123589 :               array_ref = NULL;
    6192       123589 :               current_part_dimension = 0;
    6193       123589 :               break;
    6194              : 
    6195            0 :             case AR_UNKNOWN:
    6196            0 :               gfc_internal_error ("resolve_ref(): Bad array reference");
    6197              :             }
    6198              : 
    6199              :           break;
    6200              : 
    6201        88803 :         case REF_COMPONENT:
    6202        88803 :           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        88792 :           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        88792 :           if (ref->u.c.component->ts.type == BT_DERIVED)
    6244              :             {
    6245        20689 :               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        20689 :               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        88792 :           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        88792 :           n_components++;
    6268        88792 :           break;
    6269              : 
    6270              :         case REF_SUBSTRING:
    6271              :           break;
    6272              : 
    6273          821 :         case REF_INQUIRY:
    6274              :           /* Implement requirement in note 9.7 of F2018 that the result of the
    6275              :              LEN inquiry be a scalar.  */
    6276          821 :           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       523237 :       if (((ref->type == REF_COMPONENT && n_components > 1)
    6284       509984 :            || ref->next == NULL)
    6285              :           && current_part_dimension
    6286       458943 :           && 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       523237 :       if (ref->type == REF_COMPONENT)
    6294              :         {
    6295        88792 :           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      2592274 : expression_shape (gfc_expr *e)
    6312              : {
    6313      2592274 :   mpz_t array[GFC_MAX_DIMENSIONS];
    6314      2592274 :   int i;
    6315              : 
    6316      2592274 :   if (e->rank <= 0 || e->shape != NULL)
    6317      2416763 :     return;
    6318              : 
    6319       703144 :   for (i = 0; i < e->rank; i++)
    6320       474907 :     if (!gfc_array_dimen_size (e, i, &array[i]))
    6321       175511 :       goto fail;
    6322              : 
    6323       228237 :   e->shape = gfc_get_shape (e->rank);
    6324              : 
    6325       228237 :   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
    6326              : 
    6327       228237 :   return;
    6328              : 
    6329       175511 : fail:
    6330       177182 :   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      2592274 : gfc_expression_rank (gfc_expr *e)
    6340              : {
    6341      2592274 :   gfc_ref *ref, *last_arr_ref = nullptr;
    6342      2592274 :   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      2592274 :   gcc_assert (e->expr_type != EXPR_COMPCALL);
    6347              : 
    6348      2592274 :   if (e->ref == NULL)
    6349              :     {
    6350      1911199 :       if (e->expr_type == EXPR_ARRAY)
    6351        71411 :         goto done;
    6352              :       /* Constructors can have a rank different from one via RESHAPE().  */
    6353              : 
    6354      1839788 :       if (e->symtree != NULL)
    6355              :         {
    6356              :           /* After errors the ts.u.derived of a CLASS might not be set.  */
    6357      1839776 :           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      1839776 :                                  ? CLASS_DATA (e->symtree->n.sym)->as
    6361              :                                  : e->symtree->n.sym->as;
    6362      1839776 :           if (as)
    6363              :             {
    6364          589 :               e->rank = as->rank;
    6365          589 :               e->corank = as->corank;
    6366          589 :               goto done;
    6367              :             }
    6368              :         }
    6369      1839199 :       e->rank = 0;
    6370      1839199 :       e->corank = 0;
    6371      1839199 :       goto done;
    6372              :     }
    6373              : 
    6374              :   rank = 0;
    6375              :   corank = 0;
    6376              : 
    6377      1076551 :   for (ref = e->ref; ref; ref = ref->next)
    6378              :     {
    6379       787602 :       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       787602 :       if (ref->type != REF_ARRAY)
    6387       156969 :         continue;
    6388              : 
    6389       630633 :       last_arr_ref = ref;
    6390       630633 :       if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
    6391              :         {
    6392       346541 :           rank = ref->u.ar.as->rank;
    6393       346541 :           break;
    6394              :         }
    6395              : 
    6396       284092 :       if (ref->u.ar.type == AR_SECTION)
    6397              :         {
    6398              :           /* Figure out the rank of the section.  */
    6399        45585 :           if (rank != 0)
    6400            0 :             gfc_internal_error ("gfc_expression_rank(): Two array specs");
    6401              : 
    6402       113680 :           for (i = 0; i < ref->u.ar.dimen; i++)
    6403        68095 :             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6404        68095 :                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6405        59377 :               rank++;
    6406              : 
    6407              :           break;
    6408              :         }
    6409              :     }
    6410       681075 :   if (last_arr_ref && last_arr_ref->u.ar.as
    6411       611195 :       && last_arr_ref->u.ar.as->rank != -1)
    6412              :     {
    6413        19271 :       for (i = last_arr_ref->u.ar.as->rank;
    6414       622328 :            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        20161 :           if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
    6418        19594 :               || (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        19271 :           else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
    6425        19271 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
    6426        19173 :                    || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
    6427        16682 :             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       681075 :   e->rank = rank;
    6434       681075 :   e->corank = corank;
    6435              : 
    6436      2592274 : done:
    6437      2592274 :   expression_shape (e);
    6438      2592274 : }
    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     12198841 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
    6446              : {
    6447     12198841 :   if (op1->expr_type == EXPR_VARIABLE)
    6448       731878 :     gfc_expression_rank (op1);
    6449     12198841 :   if (op2->expr_type == EXPR_VARIABLE)
    6450       446323 :     gfc_expression_rank (op2);
    6451              : 
    6452        76524 :   return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
    6453     12275039 :          && (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      1324003 : resolve_variable (gfc_expr *e)
    6461              : {
    6462      1324003 :   gfc_symbol *sym;
    6463      1324003 :   bool t;
    6464              : 
    6465      1324003 :   t = true;
    6466              : 
    6467      1324003 :   if (e->symtree == NULL)
    6468              :     return false;
    6469      1323558 :   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      1323558 :   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      1323375 :   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      1322804 :   else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6505        37159 :              && sym->ts.u.derived && CLASS_DATA (sym)
    6506        37154 :              && CLASS_DATA (sym)->as
    6507        14516 :              && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6508      1321894 :             || (sym->ts.type != BT_CLASS && sym->as
    6509       362637 :                 && 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      1323392 :   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      1323391 :   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      1323384 :   if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
    6555        37159 :         && sym->ts.u.derived && CLASS_DATA (sym)
    6556        37154 :         && CLASS_DATA (sym)->as
    6557        14516 :         && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    6558      1322474 :        || (sym->ts.type != BT_CLASS && sym->as
    6559       363173 :            && 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      1323380 :   if (IS_INFERRED_TYPE (e) && e->ref)
    6575              :     {
    6576          408 :       gfc_fixup_inferred_type_refs (e);
    6577              :       /* KIND inquiry ref returns the kind of the target.  */
    6578          408 :       if (e->expr_type == EXPR_CONSTANT)
    6579              :         return true;
    6580              :     }
    6581      1322972 :   else if (IS_INFERRED_TYPE (e)
    6582          391 :            && sym->ts.type != BT_UNKNOWN
    6583          391 :            && (sym->ts.type != e->ts.type || sym->ts.kind != e->ts.kind))
    6584              :     /* No subobject ref, but the expression's typespec was set at parse
    6585              :        time before the target's actual type/kind was known.  Refresh from
    6586              :        the now-resolved associate-name symbol.  */
    6587          133 :     e->ts = sym->ts;
    6588      1322839 :   else if (sym->attr.select_type_temporary
    6589         8936 :            && sym->ns->assoc_name_inferred)
    6590           92 :     gfc_fixup_inferred_type_refs (e);
    6591              : 
    6592              :   /* For variables that are used in an associate (target => object) where
    6593              :      the object's basetype is array valued while the target is scalar,
    6594              :      the ts' type of the component refs is still array valued, which
    6595              :      can't be translated that way.  */
    6596      1323368 :   if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
    6597          603 :       && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
    6598          603 :       && sym->assoc->target->ts.u.derived
    6599          603 :       && CLASS_DATA (sym->assoc->target)
    6600          603 :       && CLASS_DATA (sym->assoc->target)->as)
    6601              :     {
    6602              :       gfc_ref *ref = e->ref;
    6603          697 :       while (ref)
    6604              :         {
    6605          539 :           switch (ref->type)
    6606              :             {
    6607          236 :             case REF_COMPONENT:
    6608          236 :               ref->u.c.sym = sym->ts.u.derived;
    6609              :               /* Stop the loop.  */
    6610          236 :               ref = NULL;
    6611          236 :               break;
    6612          303 :             default:
    6613          303 :               ref = ref->next;
    6614          303 :               break;
    6615              :             }
    6616              :         }
    6617              :     }
    6618              : 
    6619              :   /* If this is an associate-name, it may be parsed with an array reference
    6620              :      in error even though the target is scalar.  Fail directly in this case.
    6621              :      TODO Understand why class scalar expressions must be excluded.  */
    6622      1323368 :   if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
    6623              :     {
    6624        11592 :       if (sym->ts.type == BT_CLASS)
    6625          242 :         gfc_fix_class_refs (e);
    6626        11592 :       if (!sym->attr.dimension && !sym->attr.codimension && e->ref
    6627         2139 :           && e->ref->type == REF_ARRAY)
    6628              :         {
    6629              :           /* Unambiguously scalar!  */
    6630            3 :           if (sym->assoc->target
    6631            3 :               && (sym->assoc->target->expr_type == EXPR_CONSTANT
    6632            1 :                   || sym->assoc->target->expr_type == EXPR_STRUCTURE))
    6633            2 :             gfc_error ("Scalar variable %qs has an array reference at %L",
    6634              :                        sym->name, &e->where);
    6635            3 :           return false;
    6636              :         }
    6637        11589 :       else if ((sym->attr.dimension || sym->attr.codimension)
    6638         6977 :                && (!e->ref || e->ref->type != REF_ARRAY))
    6639              :         {
    6640              :           /* This can happen because the parser did not detect that the
    6641              :              associate name is an array and the expression had no array
    6642              :              part_ref.  */
    6643          147 :           gfc_ref *ref = gfc_get_ref ();
    6644          147 :           ref->type = REF_ARRAY;
    6645          147 :           ref->u.ar.type = AR_FULL;
    6646          147 :           if (sym->as)
    6647              :             {
    6648          146 :               ref->u.ar.as = sym->as;
    6649          146 :               ref->u.ar.dimen = sym->as->rank;
    6650              :             }
    6651          147 :           ref->next = e->ref;
    6652          147 :           e->ref = ref;
    6653              :         }
    6654              :     }
    6655              : 
    6656      1323365 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
    6657            0 :     sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
    6658              : 
    6659              :   /* On the other hand, the parser may not have known this is an array;
    6660              :      in this case, we have to add a FULL reference.  */
    6661      1323365 :   if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
    6662              :     {
    6663            0 :       e->ref = gfc_get_ref ();
    6664            0 :       e->ref->type = REF_ARRAY;
    6665            0 :       e->ref->u.ar.type = AR_FULL;
    6666            0 :       e->ref->u.ar.dimen = 0;
    6667              :     }
    6668              : 
    6669              :   /* Like above, but for class types, where the checking whether an array
    6670              :      ref is present is more complicated.  Furthermore make sure not to add
    6671              :      the full array ref to _vptr or _len refs.  */
    6672      1323365 :   if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
    6673         1012 :       && CLASS_DATA (sym)
    6674         1012 :       && (CLASS_DATA (sym)->attr.dimension
    6675          443 :           || CLASS_DATA (sym)->attr.codimension)
    6676          575 :       && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
    6677              :     {
    6678          551 :       gfc_ref *ref, *newref;
    6679              : 
    6680          551 :       newref = gfc_get_ref ();
    6681          551 :       newref->type = REF_ARRAY;
    6682          551 :       newref->u.ar.type = AR_FULL;
    6683          551 :       newref->u.ar.dimen = 0;
    6684              : 
    6685              :       /* Because this is an associate var and the first ref either is a ref to
    6686              :          the _data component or not, no traversal of the ref chain is
    6687              :          needed.  The array ref needs to be inserted after the _data ref,
    6688              :          or when that is not present, which may happened for polymorphic
    6689              :          types, then at the first position.  */
    6690          551 :       ref = e->ref;
    6691          551 :       if (!ref)
    6692           18 :         e->ref = newref;
    6693          533 :       else if (ref->type == REF_COMPONENT
    6694          230 :                && strcmp ("_data", ref->u.c.component->name) == 0)
    6695              :         {
    6696          230 :           if (!ref->next || ref->next->type != REF_ARRAY)
    6697              :             {
    6698           12 :               newref->next = ref->next;
    6699           12 :               ref->next = newref;
    6700              :             }
    6701              :           else
    6702              :             /* Array ref present already.  */
    6703          218 :             gfc_free_ref_list (newref);
    6704              :         }
    6705          303 :       else if (ref->type == REF_ARRAY)
    6706              :         /* Array ref present already.  */
    6707          303 :         gfc_free_ref_list (newref);
    6708              :       else
    6709              :         {
    6710            0 :           newref->next = ref;
    6711            0 :           e->ref = newref;
    6712              :         }
    6713              :     }
    6714      1322814 :   else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    6715              :     {
    6716          486 :       gfc_ref *ref;
    6717          910 :       for (ref = e->ref; ref; ref = ref->next)
    6718          454 :         if (ref->type == REF_SUBSTRING)
    6719              :           break;
    6720          486 :       if (ref == NULL)
    6721          456 :         e->ts = sym->ts;
    6722              :     }
    6723              : 
    6724      1323365 :   if (e->ref && !gfc_resolve_ref (e))
    6725              :     return false;
    6726              : 
    6727      1323272 :   if (sym->attr.flavor == FL_PROCEDURE
    6728        31627 :       && (!sym->attr.function
    6729        18574 :           || (sym->attr.function && sym->result
    6730        18126 :               && sym->result->attr.proc_pointer
    6731          713 :               && !sym->result->attr.function)))
    6732              :     {
    6733        13053 :       e->ts.type = BT_PROCEDURE;
    6734        13053 :       goto resolve_procedure;
    6735              :     }
    6736              : 
    6737      1310219 :   if (sym->ts.type != BT_UNKNOWN)
    6738      1309574 :     gfc_variable_attr (e, &e->ts);
    6739          645 :   else if (sym->attr.flavor == FL_PROCEDURE
    6740           12 :            && sym->attr.function && sym->result
    6741           12 :            && sym->result->ts.type != BT_UNKNOWN
    6742           10 :            && sym->result->attr.proc_pointer)
    6743           10 :     e->ts = sym->result->ts;
    6744              :   else
    6745              :     {
    6746              :       /* Must be a simple variable reference.  */
    6747          635 :       if (!gfc_set_default_type (sym, 1, sym->ns))
    6748              :         return false;
    6749          509 :       e->ts = sym->ts;
    6750              :     }
    6751              : 
    6752      1310093 :   if (check_assumed_size_reference (sym, e))
    6753              :     return false;
    6754              : 
    6755              :   /* Deal with forward references to entries during gfc_resolve_code, to
    6756              :      satisfy, at least partially, 12.5.2.5.  */
    6757      1310074 :   if (gfc_current_ns->entries
    6758         3229 :       && current_entry_id == sym->entry_id
    6759         1050 :       && cs_base
    6760          964 :       && cs_base->current
    6761          964 :       && cs_base->current->op != EXEC_ENTRY)
    6762              :     {
    6763          964 :       int n;
    6764          964 :       bool saved_specification_expr;
    6765          964 :       gfc_symbol *saved_specification_expr_symbol;
    6766              : 
    6767              :       /* If the symbol is a dummy...  */
    6768          964 :       if (sym->attr.dummy && sym->ns == gfc_current_ns)
    6769              :         {
    6770              :           /*  If it has not been seen as a dummy, this is an error.  */
    6771          462 :           if (!entry_dummy_seen_p (sym))
    6772              :             {
    6773            5 :               if (specification_expr
    6774            4 :                   && specification_expr_symbol
    6775            4 :                   && specification_expr_symbol->attr.dummy
    6776            2 :                   && specification_expr_symbol->ns == gfc_current_ns
    6777            7 :                   && !entry_dummy_seen_p (specification_expr_symbol))
    6778              :                 ;
    6779            3 :               else if (specification_expr)
    6780            2 :                 gfc_error ("Variable %qs, used in a specification expression"
    6781              :                            ", is referenced at %L before the ENTRY statement "
    6782              :                            "in which it is a parameter",
    6783              :                            sym->name, &cs_base->current->loc);
    6784              :               else
    6785            1 :                 gfc_error ("Variable %qs is used at %L before the ENTRY "
    6786              :                            "statement in which it is a parameter",
    6787              :                            sym->name, &cs_base->current->loc);
    6788              :               t = false;
    6789              :             }
    6790              :         }
    6791              : 
    6792              :       /* Now do the same check on the specification expressions.  */
    6793          964 :       saved_specification_expr = specification_expr;
    6794          964 :       saved_specification_expr_symbol = specification_expr_symbol;
    6795          964 :       specification_expr = true;
    6796          964 :       specification_expr_symbol = sym;
    6797          964 :       if (sym->ts.type == BT_CHARACTER
    6798          964 :           && !gfc_resolve_expr (sym->ts.u.cl->length))
    6799              :         t = false;
    6800              : 
    6801          964 :       if (sym->as)
    6802              :         {
    6803          279 :           for (n = 0; n < sym->as->rank; n++)
    6804              :             {
    6805          164 :               if (!gfc_resolve_expr (sym->as->lower[n]))
    6806            0 :                 t = false;
    6807          164 :               if (!gfc_resolve_expr (sym->as->upper[n]))
    6808            1 :                 t = false;
    6809              :             }
    6810              :         }
    6811          964 :       specification_expr = saved_specification_expr;
    6812          964 :       specification_expr_symbol = saved_specification_expr_symbol;
    6813              : 
    6814          964 :       if (t)
    6815              :         /* Update the symbol's entry level.  */
    6816          957 :         sym->entry_id = current_entry_id + 1;
    6817              :     }
    6818              : 
    6819              :   /* If a symbol has been host_associated mark it.  This is used latter,
    6820              :      to identify if aliasing is possible via host association.  */
    6821      1310074 :   if (sym->attr.flavor == FL_VARIABLE
    6822      1271974 :       && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
    6823         6172 :           || !sym->ns->code->ext.block.assoc)
    6824      1269866 :       && gfc_current_ns->parent
    6825       605939 :       && (gfc_current_ns->parent == sym->ns
    6826       567889 :           || (gfc_current_ns->parent->parent
    6827        11288 :               && gfc_current_ns->parent->parent == sym->ns)))
    6828        44674 :     sym->attr.host_assoc = 1;
    6829              : 
    6830      1310074 :   if (gfc_current_ns->proc_name
    6831      1306026 :       && sym->attr.dimension
    6832       356630 :       && (sym->ns != gfc_current_ns
    6833       332849 :           || sym->attr.use_assoc
    6834       328868 :           || sym->attr.in_common))
    6835        32550 :     gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
    6836              : 
    6837      1323127 : resolve_procedure:
    6838      1323127 :   if (t && !resolve_procedure_expression (e))
    6839              :     t = false;
    6840              : 
    6841              :   /* F2008, C617 and C1229.  */
    6842      1322087 :   if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
    6843      1421087 :       && gfc_is_coindexed (e))
    6844              :     {
    6845          359 :       gfc_ref *ref, *ref2 = NULL;
    6846              : 
    6847          442 :       for (ref = e->ref; ref; ref = ref->next)
    6848              :         {
    6849          442 :           if (ref->type == REF_COMPONENT)
    6850           83 :             ref2 = ref;
    6851          442 :           if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6852              :             break;
    6853              :         }
    6854              : 
    6855          718 :       for ( ; ref; ref = ref->next)
    6856          371 :         if (ref->type == REF_COMPONENT)
    6857              :           break;
    6858              : 
    6859              :       /* Expression itself is not coindexed object.  */
    6860          359 :       if (ref && e->ts.type == BT_CLASS)
    6861              :         {
    6862            3 :           gfc_error ("Polymorphic subobject of coindexed object at %L",
    6863              :                      &e->where);
    6864            3 :           t = false;
    6865              :         }
    6866              : 
    6867              :       /* Expression itself is coindexed object.  */
    6868          347 :       if (ref == NULL)
    6869              :         {
    6870          347 :           gfc_component *c;
    6871          347 :           c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
    6872          467 :           for ( ; c; c = c->next)
    6873          120 :             if (c->attr.allocatable && c->ts.type == BT_CLASS)
    6874              :               {
    6875            0 :                 gfc_error ("Coindexed object with polymorphic allocatable "
    6876              :                          "subcomponent at %L", &e->where);
    6877            0 :                 t = false;
    6878            0 :                 break;
    6879              :               }
    6880              :         }
    6881              :     }
    6882              : 
    6883      1323127 :   if (t)
    6884      1323117 :     gfc_expression_rank (e);
    6885              : 
    6886      1323127 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
    6887            3 :     gfc_warning (OPT_Wdeprecated_declarations,
    6888              :                  "Using variable %qs at %L is deprecated",
    6889              :                  sym->name, &e->where);
    6890              :   /* Simplify cases where access to a parameter array results in a
    6891              :      single constant.  Suppress errors since those will have been
    6892              :      issued before, as warnings.  */
    6893      1323127 :   if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
    6894              :     {
    6895         2727 :       gfc_push_suppress_errors ();
    6896         2727 :       gfc_simplify_expr (e, 1);
    6897         2727 :       gfc_pop_suppress_errors ();
    6898              :     }
    6899              : 
    6900              :   return t;
    6901              : }
    6902              : 
    6903              : 
    6904              : /* 'sym' was initially guessed to be derived type but has been corrected
    6905              :    in resolve_assoc_var to be a class entity or the derived type correcting.
    6906              :    If a class entity it will certainly need the _data reference or the
    6907              :    reference derived type symbol correcting in the first component ref if
    6908              :    a derived type.  */
    6909              : 
    6910              : void
    6911          916 : gfc_fixup_inferred_type_refs (gfc_expr *e)
    6912              : {
    6913          916 :   gfc_ref *ref, *new_ref;
    6914          916 :   gfc_symbol *sym, *derived;
    6915          916 :   gfc_expr *target;
    6916          916 :   sym = e->symtree->n.sym;
    6917              : 
    6918              :   /* An associate_name whose selector is (i) a component ref of a selector
    6919              :      that is a inferred type associate_name; or (ii) an intrinsic type that
    6920              :      has been inferred from an inquiry ref.  */
    6921          916 :   if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    6922              :     {
    6923          318 :       sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
    6924          318 :       sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
    6925          318 :       if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
    6926              :         {
    6927           60 :           ref = e->ref;
    6928              :           /* A substring misidentified as an array section.  */
    6929           60 :           if (sym->ts.type == BT_CHARACTER
    6930           30 :               && ref->u.ar.start[0] && ref->u.ar.end[0]
    6931            6 :               && !ref->u.ar.stride[0])
    6932              :             {
    6933            6 :               new_ref = gfc_get_ref ();
    6934            6 :               new_ref->type = REF_SUBSTRING;
    6935            6 :               new_ref->u.ss.start = ref->u.ar.start[0];
    6936            6 :               new_ref->u.ss.end = ref->u.ar.end[0];
    6937            6 :               new_ref->u.ss.length = sym->ts.u.cl;
    6938            6 :               *ref = *new_ref;
    6939            6 :               free (new_ref);
    6940              :             }
    6941              :           else
    6942              :             {
    6943           54 :               if (e->ref->u.ar.type == AR_UNKNOWN)
    6944           24 :                 gfc_error ("Invalid array reference at %L", &e->where);
    6945           54 :               e->ref = ref->next;
    6946           54 :               free (ref);
    6947              :             }
    6948              :         }
    6949              : 
    6950              :       /* It is possible for an inquiry reference to be mistaken for a
    6951              :          component reference. Correct this now.  */
    6952          318 :       ref = e->ref;
    6953          318 :       if (ref && ref->type == REF_ARRAY)
    6954          138 :         ref = ref->next;
    6955          186 :       if (ref && ref->type == REF_COMPONENT
    6956          150 :           && is_inquiry_ref (ref->u.c.component->name, &new_ref))
    6957              :         {
    6958           12 :           e->symtree->n.sym = sym;
    6959           12 :           *ref = *new_ref;
    6960           12 :           gfc_free_ref_list (new_ref);
    6961              :         }
    6962              : 
    6963              :       /* The kind of the associate name is best evaluated directly from the
    6964              :          selector because of the guesses made in primary.cc, when the type
    6965              :          is still unknown.  */
    6966          318 :       if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
    6967              :         {
    6968           24 :           gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
    6969           12 :                                            sym->assoc->target->ts.kind);
    6970           12 :           gfc_replace_expr (e, ne);
    6971           12 :         }
    6972          174 :       else if (ref && ref->type == REF_INQUIRY
    6973          150 :                && (ref->u.i == INQUIRY_RE || ref->u.i == INQUIRY_IM)
    6974          114 :                && sym->ts.type == BT_COMPLEX
    6975          114 :                && e->ts.type == BT_REAL
    6976          114 :                && e->ts.kind != sym->ts.kind)
    6977              :         /* primary.cc set the inquiry-result kind to the default real kind
    6978              :            when the associate-name's type was inferred from %re/%im before
    6979              :            the target was resolved.  Now use the (resolved) selector kind.  */
    6980           24 :         e->ts.kind = sym->ts.kind;
    6981              : 
    6982              :       /* Now that the references are all sorted out, set the expression rank
    6983              :          and return.  */
    6984          318 :       gfc_expression_rank (e);
    6985          318 :       return;
    6986              :     }
    6987              : 
    6988          598 :   derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
    6989              :                                      : sym->ts.u.derived;
    6990              : 
    6991              :   /* Ensure that class symbols have an array spec and ensure that there
    6992              :      is a _data field reference following class type references.  */
    6993          598 :   if (sym->ts.type == BT_CLASS
    6994          196 :       && sym->assoc->target->ts.type == BT_CLASS)
    6995              :     {
    6996          196 :       e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
    6997          196 :       e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
    6998          196 :       sym->attr.dimension = 0;
    6999          196 :       sym->attr.codimension = 0;
    7000          196 :       CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
    7001          196 :       CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
    7002          196 :       if (e->ref && (e->ref->type != REF_COMPONENT
    7003          160 :                      || e->ref->u.c.component->name[0] != '_'))
    7004              :         {
    7005           82 :           ref = gfc_get_ref ();
    7006           82 :           ref->type = REF_COMPONENT;
    7007           82 :           ref->next = e->ref;
    7008           82 :           e->ref = ref;
    7009           82 :           ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
    7010              :                                                    true, true, NULL);
    7011           82 :           ref->u.c.sym = sym->ts.u.derived;
    7012              :         }
    7013              :     }
    7014              : 
    7015              :   /* Proceed as far as the first component reference and ensure that the
    7016              :      correct derived type is being used.  */
    7017          861 :   for (ref = e->ref; ref; ref = ref->next)
    7018          825 :     if (ref->type == REF_COMPONENT)
    7019              :       {
    7020          562 :         if (ref->u.c.component->name[0] != '_')
    7021          366 :           ref->u.c.sym = derived;
    7022              :         else
    7023          196 :           ref->u.c.sym = sym->ts.u.derived;
    7024              :         break;
    7025              :       }
    7026              : 
    7027              :   /* Verify that the type inferrence mechanism has not introduced a spurious
    7028              :      array reference.  This can happen with an associate name, whose selector
    7029              :      is an element of another inferred type.  */
    7030          598 :   target = e->symtree->n.sym->assoc->target;
    7031          598 :   if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
    7032          186 :       && e != target && !target->rank)
    7033              :     {
    7034              :       /* First case: array ref after the scalar class or derived
    7035              :          associate_name.  */
    7036          186 :       if (e->ref && e->ref->type == REF_ARRAY
    7037            7 :           && e->ref->u.ar.type != AR_ELEMENT)
    7038              :         {
    7039            7 :           ref = e->ref;
    7040            7 :           if (ref->u.ar.type == AR_UNKNOWN)
    7041            1 :             gfc_error ("Invalid array reference at %L", &e->where);
    7042            7 :           e->ref = ref->next;
    7043            7 :           free (ref);
    7044              : 
    7045              :           /* If it hasn't a ref to the '_data' field supply one.  */
    7046            7 :           if (sym->ts.type == BT_CLASS
    7047            0 :               && !(e->ref->type == REF_COMPONENT
    7048            0 :                    && strcmp (e->ref->u.c.component->name, "_data")))
    7049              :             {
    7050            0 :               gfc_ref *new_ref;
    7051            0 :               gfc_find_component (e->symtree->n.sym->ts.u.derived,
    7052              :                                   "_data", true, true, &new_ref);
    7053            0 :               new_ref->next = e->ref;
    7054            0 :               e->ref = new_ref;
    7055              :             }
    7056              :         }
    7057              :       /* 2nd case: a ref to the '_data' field followed by an array ref.  */
    7058          179 :       else if (e->ref && e->ref->type == REF_COMPONENT
    7059          179 :                && strcmp (e->ref->u.c.component->name, "_data") == 0
    7060           64 :                && e->ref->next && e->ref->next->type == REF_ARRAY
    7061            0 :                && e->ref->next->u.ar.type != AR_ELEMENT)
    7062              :         {
    7063            0 :           ref = e->ref->next;
    7064            0 :           if (ref->u.ar.type == AR_UNKNOWN)
    7065            0 :             gfc_error ("Invalid array reference at %L", &e->where);
    7066            0 :           e->ref->next = e->ref->next->next;
    7067            0 :           free (ref);
    7068              :         }
    7069              :     }
    7070              : 
    7071              :   /* Now that all the references are OK, get the expression rank.  */
    7072          598 :   gfc_expression_rank (e);
    7073              : }
    7074              : 
    7075              : 
    7076              : /* Checks to see that the correct symbol has been host associated.
    7077              :    The only situations where this arises are:
    7078              :         (i)  That in which a twice contained function is parsed after
    7079              :              the host association is made. On detecting this, change
    7080              :              the symbol in the expression and convert the array reference
    7081              :              into an actual arglist if the old symbol is a variable; or
    7082              :         (ii) That in which an external function is typed but not declared
    7083              :              explicitly to be external. Here, the old symbol is changed
    7084              :              from a variable to an external function.  */
    7085              : static bool
    7086      1668419 : check_host_association (gfc_expr *e)
    7087              : {
    7088      1668419 :   gfc_symbol *sym, *old_sym;
    7089      1668419 :   gfc_symtree *st;
    7090      1668419 :   int n;
    7091      1668419 :   gfc_ref *ref;
    7092      1668419 :   gfc_actual_arglist *arg, *tail = NULL;
    7093      1668419 :   bool retval = e->expr_type == EXPR_FUNCTION;
    7094              : 
    7095              :   /*  If the expression is the result of substitution in
    7096              :       interface.cc(gfc_extend_expr) because there is no way in
    7097              :       which the host association can be wrong.  */
    7098      1668419 :   if (e->symtree == NULL
    7099      1667620 :         || e->symtree->n.sym == NULL
    7100      1667620 :         || e->user_operator)
    7101              :     return retval;
    7102              : 
    7103      1665855 :   old_sym = e->symtree->n.sym;
    7104              : 
    7105      1665855 :   if (gfc_current_ns->parent
    7106       731275 :         && old_sym->ns != gfc_current_ns)
    7107              :     {
    7108              :       /* Use the 'USE' name so that renamed module symbols are
    7109              :          correctly handled.  */
    7110        91222 :       gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
    7111              : 
    7112        91222 :       if (sym && old_sym != sym
    7113          683 :               && sym->attr.flavor == FL_PROCEDURE
    7114          105 :               && sym->attr.contained)
    7115              :         {
    7116              :           /* Clear the shape, since it might not be valid.  */
    7117           83 :           gfc_free_shape (&e->shape, e->rank);
    7118              : 
    7119              :           /* Give the expression the right symtree!  */
    7120           83 :           gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
    7121           83 :           gcc_assert (st != NULL);
    7122              : 
    7123           83 :           if (old_sym->attr.flavor == FL_PROCEDURE
    7124           59 :                 || e->expr_type == EXPR_FUNCTION)
    7125              :             {
    7126              :               /* Original was function so point to the new symbol, since
    7127              :                  the actual argument list is already attached to the
    7128              :                  expression.  */
    7129           30 :               e->value.function.esym = NULL;
    7130           30 :               e->symtree = st;
    7131              :             }
    7132              :           else
    7133              :             {
    7134              :               /* Original was variable so convert array references into
    7135              :                  an actual arglist. This does not need any checking now
    7136              :                  since resolve_function will take care of it.  */
    7137           53 :               e->value.function.actual = NULL;
    7138           53 :               e->expr_type = EXPR_FUNCTION;
    7139           53 :               e->symtree = st;
    7140              : 
    7141              :               /* Ambiguity will not arise if the array reference is not
    7142              :                  the last reference.  */
    7143           55 :               for (ref = e->ref; ref; ref = ref->next)
    7144           38 :                 if (ref->type == REF_ARRAY && ref->next == NULL)
    7145              :                   break;
    7146              : 
    7147           53 :               if ((ref == NULL || ref->type != REF_ARRAY)
    7148           17 :                   && sym->attr.proc == PROC_INTERNAL)
    7149              :                 {
    7150            4 :                   gfc_error ("%qs at %L is host associated at %L into "
    7151              :                              "a contained procedure with an internal "
    7152              :                              "procedure of the same name", sym->name,
    7153              :                               &old_sym->declared_at, &e->where);
    7154            4 :                   return false;
    7155              :                 }
    7156              : 
    7157           13 :               if (ref == NULL)
    7158              :                 return false;
    7159              : 
    7160           36 :               gcc_assert (ref->type == REF_ARRAY);
    7161              : 
    7162              :               /* Grab the start expressions from the array ref and
    7163              :                  copy them into actual arguments.  */
    7164           84 :               for (n = 0; n < ref->u.ar.dimen; n++)
    7165              :                 {
    7166           48 :                   arg = gfc_get_actual_arglist ();
    7167           48 :                   arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
    7168           48 :                   if (e->value.function.actual == NULL)
    7169           36 :                     tail = e->value.function.actual = arg;
    7170              :                   else
    7171              :                     {
    7172           12 :                       tail->next = arg;
    7173           12 :                       tail = arg;
    7174              :                     }
    7175              :                 }
    7176              : 
    7177              :               /* Dump the reference list and set the rank.  */
    7178           36 :               gfc_free_ref_list (e->ref);
    7179           36 :               e->ref = NULL;
    7180           36 :               e->rank = sym->as ? sym->as->rank : 0;
    7181           36 :               e->corank = sym->as ? sym->as->corank : 0;
    7182              :             }
    7183              : 
    7184           66 :           gfc_resolve_expr (e);
    7185           66 :           sym->refs++;
    7186              :         }
    7187              :       /* This case corresponds to a call, from a block or a contained
    7188              :          procedure, to an external function, which has not been declared
    7189              :          as being external in the main program but has been typed.  */
    7190        91139 :       else if (sym && old_sym != sym
    7191          600 :                && !e->ref
    7192          328 :                && sym->ts.type == BT_UNKNOWN
    7193           21 :                && old_sym->ts.type != BT_UNKNOWN
    7194           19 :                && sym->attr.flavor == FL_PROCEDURE
    7195           19 :                && old_sym->attr.flavor == FL_VARIABLE
    7196            7 :                && sym->ns->parent == old_sym->ns
    7197            7 :                && sym->ns->proc_name
    7198            7 :                && sym->ns->proc_name->attr.proc != PROC_MODULE
    7199            6 :                && (sym->ns->proc_name->attr.flavor == FL_LABEL
    7200            6 :                    || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
    7201              :         {
    7202            6 :           old_sym->attr.flavor = FL_PROCEDURE;
    7203            6 :           old_sym->attr.external = 1;
    7204            6 :           old_sym->attr.function = 1;
    7205            6 :           old_sym->result = old_sym;
    7206            6 :           gfc_resolve_expr (e);
    7207              :         }
    7208              :     }
    7209              :   /* This might have changed!  */
    7210      1665838 :   return e->expr_type == EXPR_FUNCTION;
    7211              : }
    7212              : 
    7213              : 
    7214              : static void
    7215         1442 : gfc_resolve_character_operator (gfc_expr *e)
    7216              : {
    7217         1442 :   gfc_expr *op1 = e->value.op.op1;
    7218         1442 :   gfc_expr *op2 = e->value.op.op2;
    7219         1442 :   gfc_expr *e1 = NULL;
    7220         1442 :   gfc_expr *e2 = NULL;
    7221              : 
    7222         1442 :   gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
    7223              : 
    7224         1442 :   if (op1->ts.u.cl && op1->ts.u.cl->length)
    7225          761 :     e1 = gfc_copy_expr (op1->ts.u.cl->length);
    7226          681 :   else if (op1->expr_type == EXPR_CONSTANT)
    7227          268 :     e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7228          268 :                            op1->value.character.length);
    7229              : 
    7230         1442 :   if (op2->ts.u.cl && op2->ts.u.cl->length)
    7231          749 :     e2 = gfc_copy_expr (op2->ts.u.cl->length);
    7232          693 :   else if (op2->expr_type == EXPR_CONSTANT)
    7233          462 :     e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
    7234          462 :                            op2->value.character.length);
    7235              : 
    7236         1442 :   e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7237              : 
    7238         1442 :   if (!e1 || !e2)
    7239              :     {
    7240          541 :       gfc_free_expr (e1);
    7241          541 :       gfc_free_expr (e2);
    7242              : 
    7243          541 :       return;
    7244              :     }
    7245              : 
    7246          901 :   e->ts.u.cl->length = gfc_add (e1, e2);
    7247          901 :   e->ts.u.cl->length->ts.type = BT_INTEGER;
    7248          901 :   e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
    7249          901 :   gfc_simplify_expr (e->ts.u.cl->length, 0);
    7250          901 :   gfc_resolve_expr (e->ts.u.cl->length);
    7251              : 
    7252          901 :   return;
    7253              : }
    7254              : 
    7255              : 
    7256              : /*  Ensure that an character expression has a charlen and, if possible, a
    7257              :     length expression.  */
    7258              : 
    7259              : static void
    7260       180758 : fixup_charlen (gfc_expr *e)
    7261              : {
    7262              :   /* The cases fall through so that changes in expression type and the need
    7263              :      for multiple fixes are picked up.  In all circumstances, a charlen should
    7264              :      be available for the middle end to hang a backend_decl on.  */
    7265       180758 :   switch (e->expr_type)
    7266              :     {
    7267         1442 :     case EXPR_OP:
    7268         1442 :       gfc_resolve_character_operator (e);
    7269              :       /* FALLTHRU */
    7270              : 
    7271         1509 :     case EXPR_ARRAY:
    7272         1509 :       if (e->expr_type == EXPR_ARRAY)
    7273           67 :         gfc_resolve_character_array_constructor (e);
    7274              :       /* FALLTHRU */
    7275              : 
    7276         1966 :     case EXPR_SUBSTRING:
    7277         1966 :       if (!e->ts.u.cl && e->ref)
    7278          453 :         gfc_resolve_substring_charlen (e);
    7279              :       /* FALLTHRU */
    7280              : 
    7281       180758 :     default:
    7282       180758 :       if (!e->ts.u.cl)
    7283       178796 :         e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7284              : 
    7285       180758 :       break;
    7286              :     }
    7287       180758 : }
    7288              : 
    7289              : 
    7290              : /* Update an actual argument to include the passed-object for type-bound
    7291              :    procedures at the right position.  */
    7292              : 
    7293              : static gfc_actual_arglist*
    7294         2968 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
    7295              :                      const char *name)
    7296              : {
    7297         2992 :   gcc_assert (argpos > 0);
    7298              : 
    7299         2992 :   if (argpos == 1)
    7300              :     {
    7301         2843 :       gfc_actual_arglist* result;
    7302              : 
    7303         2843 :       result = gfc_get_actual_arglist ();
    7304         2843 :       result->expr = po;
    7305         2843 :       result->next = lst;
    7306         2843 :       if (name)
    7307          514 :         result->name = name;
    7308              : 
    7309         2843 :       return result;
    7310              :     }
    7311              : 
    7312          149 :   if (lst)
    7313          125 :     lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
    7314              :   else
    7315           24 :     lst = update_arglist_pass (NULL, po, argpos - 1, name);
    7316              :   return lst;
    7317              : }
    7318              : 
    7319              : 
    7320              : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it).  */
    7321              : 
    7322              : static gfc_expr*
    7323         7214 : extract_compcall_passed_object (gfc_expr* e)
    7324              : {
    7325         7214 :   gfc_expr* po;
    7326              : 
    7327         7214 :   if (e->expr_type == EXPR_UNKNOWN)
    7328              :     {
    7329            0 :       gfc_error ("Error in typebound call at %L",
    7330              :                  &e->where);
    7331            0 :       return NULL;
    7332              :     }
    7333              : 
    7334         7214 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7335              : 
    7336         7214 :   if (e->value.compcall.base_object)
    7337         1584 :     po = gfc_copy_expr (e->value.compcall.base_object);
    7338              :   else
    7339              :     {
    7340         5630 :       po = gfc_get_expr ();
    7341         5630 :       po->expr_type = EXPR_VARIABLE;
    7342         5630 :       po->symtree = e->symtree;
    7343         5630 :       po->ref = gfc_copy_ref (e->ref);
    7344         5630 :       po->where = e->where;
    7345              :     }
    7346              : 
    7347         7214 :   if (!gfc_resolve_expr (po))
    7348              :     return NULL;
    7349              : 
    7350              :   return po;
    7351              : }
    7352              : 
    7353              : 
    7354              : /* Update the arglist of an EXPR_COMPCALL expression to include the
    7355              :    passed-object.  */
    7356              : 
    7357              : static bool
    7358         3327 : update_compcall_arglist (gfc_expr* e)
    7359              : {
    7360         3327 :   gfc_expr* po;
    7361         3327 :   gfc_typebound_proc* tbp;
    7362              : 
    7363         3327 :   tbp = e->value.compcall.tbp;
    7364              : 
    7365         3327 :   if (tbp->error)
    7366              :     return false;
    7367              : 
    7368         3326 :   po = extract_compcall_passed_object (e);
    7369         3326 :   if (!po)
    7370              :     return false;
    7371              : 
    7372         3326 :   if (tbp->nopass || e->value.compcall.ignore_pass)
    7373              :     {
    7374         1116 :       gfc_free_expr (po);
    7375         1116 :       return true;
    7376              :     }
    7377              : 
    7378         2210 :   if (tbp->pass_arg_num <= 0)
    7379              :     return false;
    7380              : 
    7381         2209 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7382              :                                                   tbp->pass_arg_num,
    7383              :                                                   tbp->pass_arg);
    7384              : 
    7385         2209 :   return true;
    7386              : }
    7387              : 
    7388              : 
    7389              : /* Extract the passed object from a PPC call (a copy of it).  */
    7390              : 
    7391              : static gfc_expr*
    7392           85 : extract_ppc_passed_object (gfc_expr *e)
    7393              : {
    7394           85 :   gfc_expr *po;
    7395           85 :   gfc_ref **ref;
    7396              : 
    7397           85 :   po = gfc_get_expr ();
    7398           85 :   po->expr_type = EXPR_VARIABLE;
    7399           85 :   po->symtree = e->symtree;
    7400           85 :   po->ref = gfc_copy_ref (e->ref);
    7401           85 :   po->where = e->where;
    7402              : 
    7403              :   /* Remove PPC reference.  */
    7404           85 :   ref = &po->ref;
    7405           91 :   while ((*ref)->next)
    7406            6 :     ref = &(*ref)->next;
    7407           85 :   gfc_free_ref_list (*ref);
    7408           85 :   *ref = NULL;
    7409              : 
    7410           85 :   if (!gfc_resolve_expr (po))
    7411            0 :     return NULL;
    7412              : 
    7413              :   return po;
    7414              : }
    7415              : 
    7416              : 
    7417              : /* Update the actual arglist of a procedure pointer component to include the
    7418              :    passed-object.  */
    7419              : 
    7420              : static bool
    7421          574 : update_ppc_arglist (gfc_expr* e)
    7422              : {
    7423          574 :   gfc_expr* po;
    7424          574 :   gfc_component *ppc;
    7425          574 :   gfc_typebound_proc* tb;
    7426              : 
    7427          574 :   ppc = gfc_get_proc_ptr_comp (e);
    7428          574 :   if (!ppc)
    7429              :     return false;
    7430              : 
    7431          574 :   tb = ppc->tb;
    7432              : 
    7433          574 :   if (tb->error)
    7434              :     return false;
    7435          572 :   else if (tb->nopass)
    7436              :     return true;
    7437              : 
    7438           85 :   po = extract_ppc_passed_object (e);
    7439           85 :   if (!po)
    7440              :     return false;
    7441              : 
    7442              :   /* F08:R739.  */
    7443           85 :   if (po->rank != 0)
    7444              :     {
    7445            0 :       gfc_error ("Passed-object at %L must be scalar", &e->where);
    7446            0 :       return false;
    7447              :     }
    7448              : 
    7449              :   /* F08:C611.  */
    7450           85 :   if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
    7451              :     {
    7452            1 :       gfc_error ("Base object for procedure-pointer component call at %L is of"
    7453              :                  " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
    7454            1 :       return false;
    7455              :     }
    7456              : 
    7457           84 :   gcc_assert (tb->pass_arg_num > 0);
    7458           84 :   e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
    7459              :                                                   tb->pass_arg_num,
    7460              :                                                   tb->pass_arg);
    7461              : 
    7462           84 :   return true;
    7463              : }
    7464              : 
    7465              : 
    7466              : /* Check that the object a TBP is called on is valid, i.e. it must not be
    7467              :    of ABSTRACT type (as in subobject%abstract_parent%tbp()).  */
    7468              : 
    7469              : static bool
    7470         3338 : check_typebound_baseobject (gfc_expr* e)
    7471              : {
    7472         3338 :   gfc_expr* base;
    7473         3338 :   bool return_value = false;
    7474              : 
    7475         3338 :   base = extract_compcall_passed_object (e);
    7476         3338 :   if (!base)
    7477              :     return false;
    7478              : 
    7479         3335 :   if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
    7480              :     {
    7481            1 :       gfc_error ("Error in typebound call at %L", &e->where);
    7482            1 :       goto cleanup;
    7483              :     }
    7484              : 
    7485         3334 :   if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
    7486            1 :     return false;
    7487              : 
    7488              :   /* F08:C611.  */
    7489         3333 :   if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
    7490              :     {
    7491            3 :       gfc_error ("Base object for type-bound procedure call at %L is of"
    7492              :                  " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
    7493            3 :       goto cleanup;
    7494              :     }
    7495              : 
    7496              :   /* F08:C1230. If the procedure called is NOPASS,
    7497              :      the base object must be scalar.  */
    7498         3330 :   if (e->value.compcall.tbp->nopass && base->rank != 0)
    7499              :     {
    7500            1 :       gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
    7501              :                  " be scalar", &e->where);
    7502            1 :       goto cleanup;
    7503              :     }
    7504              : 
    7505              :   return_value = true;
    7506              : 
    7507         3334 : cleanup:
    7508         3334 :   gfc_free_expr (base);
    7509         3334 :   return return_value;
    7510              : }
    7511              : 
    7512              : 
    7513              : /* Resolve a call to a type-bound procedure, either function or subroutine,
    7514              :    statically from the data in an EXPR_COMPCALL expression.  The adapted
    7515              :    arglist and the target-procedure symtree are returned.  */
    7516              : 
    7517              : static bool
    7518         3327 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
    7519              :                           gfc_actual_arglist** actual)
    7520              : {
    7521         3327 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7522         3327 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7523              : 
    7524              :   /* Update the actual arglist for PASS.  */
    7525         3327 :   if (!update_compcall_arglist (e))
    7526              :     return false;
    7527              : 
    7528         3325 :   *actual = e->value.compcall.actual;
    7529         3325 :   *target = e->value.compcall.tbp->u.specific;
    7530              : 
    7531         3325 :   gfc_free_ref_list (e->ref);
    7532         3325 :   e->ref = NULL;
    7533         3325 :   e->value.compcall.actual = NULL;
    7534              : 
    7535              :   /* If we find a deferred typebound procedure, check for derived types
    7536              :      that an overriding typebound procedure has not been missed.  */
    7537         3325 :   if (e->value.compcall.name
    7538         3325 :       && !e->value.compcall.tbp->non_overridable
    7539         3307 :       && e->value.compcall.base_object
    7540          792 :       && e->value.compcall.base_object->ts.type == BT_DERIVED)
    7541              :     {
    7542          499 :       gfc_symtree *st;
    7543          499 :       gfc_symbol *derived;
    7544              : 
    7545              :       /* Use the derived type of the base_object.  */
    7546          499 :       derived = e->value.compcall.base_object->ts.u.derived;
    7547          499 :       st = NULL;
    7548              : 
    7549              :       /* If necessary, go through the inheritance chain.  */
    7550         1505 :       while (!st && derived)
    7551              :         {
    7552              :           /* Look for the typebound procedure 'name'.  */
    7553          507 :           if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
    7554          499 :             st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
    7555              :                                    e->value.compcall.name);
    7556          507 :           if (!st)
    7557            8 :             derived = gfc_get_derived_super_type (derived);
    7558              :         }
    7559              : 
    7560              :       /* Now find the specific name in the derived type namespace.  */
    7561          499 :       if (st && st->n.tb && st->n.tb->u.specific)
    7562          499 :         gfc_find_sym_tree (st->n.tb->u.specific->name,
    7563          499 :                            derived->ns, 1, &st);
    7564          499 :       if (st)
    7565          499 :         *target = st;
    7566              :     }
    7567              : 
    7568         3325 :   if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
    7569         3325 :       && !e->value.compcall.tbp->deferred)
    7570            1 :     gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
    7571              :                  " itself recursively.  Declare it RECURSIVE or use"
    7572              :                  " %<-frecursive%>", (*target)->n.sym->name, &e->where);
    7573              : 
    7574              :   return true;
    7575              : }
    7576              : 
    7577              : 
    7578              : /* Get the ultimate declared type from an expression.  In addition,
    7579              :    return the last class/derived type reference and the copy of the
    7580              :    reference list.  If check_types is set true, derived types are
    7581              :    identified as well as class references.  */
    7582              : static gfc_symbol*
    7583         3269 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
    7584              :                         gfc_expr *e, bool check_types)
    7585              : {
    7586         3269 :   gfc_symbol *declared;
    7587         3269 :   gfc_ref *ref;
    7588              : 
    7589         3269 :   declared = NULL;
    7590         3269 :   if (class_ref)
    7591         2861 :     *class_ref = NULL;
    7592         3269 :   if (new_ref)
    7593         2568 :     *new_ref = gfc_copy_ref (e->ref);
    7594              : 
    7595         4064 :   for (ref = e->ref; ref; ref = ref->next)
    7596              :     {
    7597          795 :       if (ref->type != REF_COMPONENT)
    7598          292 :         continue;
    7599              : 
    7600          503 :       if ((ref->u.c.component->ts.type == BT_CLASS
    7601          256 :              || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
    7602          428 :           && ref->u.c.component->attr.flavor != FL_PROCEDURE)
    7603              :         {
    7604          354 :           declared = ref->u.c.component->ts.u.derived;
    7605          354 :           if (class_ref)
    7606          332 :             *class_ref = ref;
    7607              :         }
    7608              :     }
    7609              : 
    7610         3269 :   if (declared == NULL)
    7611         2941 :     declared = e->symtree->n.sym->ts.u.derived;
    7612              : 
    7613         3269 :   return declared;
    7614              : }
    7615              : 
    7616              : 
    7617              : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
    7618              :    which of the specific bindings (if any) matches the arglist and transform
    7619              :    the expression into a call of that binding.  */
    7620              : 
    7621              : static bool
    7622         3329 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
    7623              : {
    7624         3329 :   gfc_typebound_proc* genproc;
    7625         3329 :   const char* genname;
    7626         3329 :   gfc_symtree *st;
    7627         3329 :   gfc_symbol *derived;
    7628              : 
    7629         3329 :   gcc_assert (e->expr_type == EXPR_COMPCALL);
    7630         3329 :   genname = e->value.compcall.name;
    7631         3329 :   genproc = e->value.compcall.tbp;
    7632              : 
    7633         3329 :   if (!genproc->is_generic)
    7634              :     return true;
    7635              : 
    7636              :   /* Try the bindings on this type and in the inheritance hierarchy.  */
    7637          420 :   for (; genproc; genproc = genproc->overridden)
    7638              :     {
    7639          418 :       gfc_tbp_generic* g;
    7640              : 
    7641          418 :       gcc_assert (genproc->is_generic);
    7642          646 :       for (g = genproc->u.generic; g; g = g->next)
    7643              :         {
    7644          636 :           gfc_symbol* target;
    7645          636 :           gfc_actual_arglist* args;
    7646          636 :           bool matches;
    7647              : 
    7648          636 :           gcc_assert (g->specific);
    7649              : 
    7650          636 :           if (g->specific->error)
    7651            0 :             continue;
    7652              : 
    7653          636 :           target = g->specific->u.specific->n.sym;
    7654              : 
    7655              :           /* Get the right arglist by handling PASS/NOPASS.  */
    7656          636 :           args = gfc_copy_actual_arglist (e->value.compcall.actual);
    7657          636 :           if (!g->specific->nopass)
    7658              :             {
    7659          550 :               gfc_expr* po;
    7660          550 :               po = extract_compcall_passed_object (e);
    7661          550 :               if (!po)
    7662              :                 {
    7663            0 :                   gfc_free_actual_arglist (args);
    7664            0 :                   return false;
    7665              :                 }
    7666              : 
    7667          550 :               gcc_assert (g->specific->pass_arg_num > 0);
    7668          550 :               gcc_assert (!g->specific->error);
    7669          550 :               args = update_arglist_pass (args, po, g->specific->pass_arg_num,
    7670              :                                           g->specific->pass_arg);
    7671              :             }
    7672          636 :           resolve_actual_arglist (args, target->attr.proc,
    7673          636 :                                   is_external_proc (target)
    7674          636 :                                   && gfc_sym_get_dummy_args (target) == NULL);
    7675              : 
    7676              :           /* Check if this arglist matches the formal.  */
    7677          636 :           matches = gfc_arglist_matches_symbol (&args, target);
    7678              : 
    7679              :           /* Clean up and break out of the loop if we've found it.  */
    7680          636 :           gfc_free_actual_arglist (args);
    7681          636 :           if (matches)
    7682              :             {
    7683          408 :               e->value.compcall.tbp = g->specific;
    7684          408 :               genname = g->specific_st->name;
    7685              :               /* Pass along the name for CLASS methods, where the vtab
    7686              :                  procedure pointer component has to be referenced.  */
    7687          408 :               if (name)
    7688          161 :                 *name = genname;
    7689          408 :               goto success;
    7690              :             }
    7691              :         }
    7692              :     }
    7693              : 
    7694              :   /* Nothing matching found!  */
    7695            2 :   gfc_error ("Found no matching specific binding for the call to the GENERIC"
    7696              :              " %qs at %L", genname, &e->where);
    7697            2 :   return false;
    7698              : 
    7699          408 : success:
    7700              :   /* Make sure that we have the right specific instance for the name.  */
    7701          408 :   derived = get_declared_from_expr (NULL, NULL, e, true);
    7702              : 
    7703          408 :   st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
    7704          408 :   if (st)
    7705          408 :     e->value.compcall.tbp = st->n.tb;
    7706              : 
    7707              :   return true;
    7708              : }
    7709              : 
    7710              : 
    7711              : /* Resolve a call to a type-bound subroutine.  */
    7712              : 
    7713              : static bool
    7714         1730 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
    7715              : {
    7716         1730 :   gfc_actual_arglist* newactual;
    7717         1730 :   gfc_symtree* target;
    7718              : 
    7719              :   /* Check that's really a SUBROUTINE.  */
    7720         1730 :   if (!c->expr1->value.compcall.tbp->subroutine)
    7721              :     {
    7722           17 :       if (!c->expr1->value.compcall.tbp->is_generic
    7723           15 :           && c->expr1->value.compcall.tbp->u.specific
    7724           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym
    7725           15 :           && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
    7726           12 :         c->expr1->value.compcall.tbp->subroutine = 1;
    7727              :       else
    7728              :         {
    7729            5 :           gfc_error ("%qs at %L should be a SUBROUTINE",
    7730              :                      c->expr1->value.compcall.name, &c->loc);
    7731            5 :           return false;
    7732              :         }
    7733              :     }
    7734              : 
    7735         1725 :   if (!check_typebound_baseobject (c->expr1))
    7736              :     return false;
    7737              : 
    7738              :   /* Pass along the name for CLASS methods, where the vtab
    7739              :      procedure pointer component has to be referenced.  */
    7740         1718 :   if (name)
    7741          480 :     *name = c->expr1->value.compcall.name;
    7742              : 
    7743         1718 :   if (!resolve_typebound_generic_call (c->expr1, name))
    7744              :     return false;
    7745              : 
    7746              :   /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
    7747         1717 :   if (overridable)
    7748          371 :     *overridable = !c->expr1->value.compcall.tbp->non_overridable;
    7749              : 
    7750              :   /* Transform into an ordinary EXEC_CALL for now.  */
    7751              : 
    7752         1717 :   if (!resolve_typebound_static (c->expr1, &target, &newactual))
    7753              :     return false;
    7754              : 
    7755         1715 :   c->ext.actual = newactual;
    7756         1715 :   c->symtree = target;
    7757         1715 :   c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
    7758              : 
    7759         1715 :   gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
    7760              : 
    7761         1715 :   gfc_free_expr (c->expr1);
    7762         1715 :   c->expr1 = gfc_get_expr ();
    7763         1715 :   c->expr1->expr_type = EXPR_FUNCTION;
    7764         1715 :   c->expr1->symtree = target;
    7765         1715 :   c->expr1->where = c->loc;
    7766              : 
    7767         1715 :   return resolve_call (c);
    7768              : }
    7769              : 
    7770              : 
    7771              : /* Resolve a component-call expression.  */
    7772              : static bool
    7773         1632 : resolve_compcall (gfc_expr* e, const char **name)
    7774              : {
    7775         1632 :   gfc_actual_arglist* newactual;
    7776         1632 :   gfc_symtree* target;
    7777              : 
    7778              :   /* Check that's really a FUNCTION.  */
    7779         1632 :   if (!e->value.compcall.tbp->function)
    7780              :     {
    7781           19 :       if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
    7782            5 :         gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
    7783              :                    &e->where);
    7784           19 :       return false;
    7785              :     }
    7786              : 
    7787              : 
    7788              :   /* These must not be assign-calls!  */
    7789         1613 :   gcc_assert (!e->value.compcall.assign);
    7790              : 
    7791         1613 :   if (!check_typebound_baseobject (e))
    7792              :     return false;
    7793              : 
    7794              :   /* Pass along the name for CLASS methods, where the vtab
    7795              :      procedure pointer component has to be referenced.  */
    7796         1611 :   if (name)
    7797          864 :     *name = e->value.compcall.name;
    7798              : 
    7799         1611 :   if (!resolve_typebound_generic_call (e, name))
    7800              :     return false;
    7801         1610 :   gcc_assert (!e->value.compcall.tbp->is_generic);
    7802              : 
    7803              :   /* Take the rank from the function's symbol.  */
    7804         1610 :   if (e->value.compcall.tbp->u.specific->n.sym->as)
    7805              :     {
    7806          155 :       e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
    7807          155 :       e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
    7808              :     }
    7809              : 
    7810              :   /* For now, we simply transform it into an EXPR_FUNCTION call with the same
    7811              :      arglist to the TBP's binding target.  */
    7812              : 
    7813         1610 :   if (!resolve_typebound_static (e, &target, &newactual))
    7814              :     return false;
    7815              : 
    7816         1610 :   e->value.function.actual = newactual;
    7817         1610 :   e->value.function.name = NULL;
    7818         1610 :   e->value.function.esym = target->n.sym;
    7819         1610 :   e->value.function.isym = NULL;
    7820         1610 :   e->symtree = target;
    7821         1610 :   e->ts = target->n.sym->ts;
    7822         1610 :   e->expr_type = EXPR_FUNCTION;
    7823              : 
    7824              :   /* Resolution is not necessary if this is a class subroutine; this
    7825              :      function only has to identify the specific proc. Resolution of
    7826              :      the call will be done next in resolve_typebound_call.  */
    7827         1610 :   return gfc_resolve_expr (e);
    7828              : }
    7829              : 
    7830              : 
    7831              : static bool resolve_fl_derived (gfc_symbol *sym);
    7832              : 
    7833              : 
    7834              : /* Resolve a typebound function, or 'method'. First separate all
    7835              :    the non-CLASS references by calling resolve_compcall directly.  */
    7836              : 
    7837              : static bool
    7838         1632 : resolve_typebound_function (gfc_expr* e)
    7839              : {
    7840         1632 :   gfc_symbol *declared;
    7841         1632 :   gfc_component *c;
    7842         1632 :   gfc_ref *new_ref;
    7843         1632 :   gfc_ref *class_ref;
    7844         1632 :   gfc_symtree *st;
    7845         1632 :   const char *name;
    7846         1632 :   gfc_typespec ts;
    7847         1632 :   gfc_expr *expr;
    7848         1632 :   bool overridable;
    7849              : 
    7850         1632 :   st = e->symtree;
    7851              : 
    7852              :   /* Deal with typebound operators for CLASS objects.  */
    7853         1632 :   expr = e->value.compcall.base_object;
    7854         1632 :   overridable = !e->value.compcall.tbp->non_overridable;
    7855         1632 :   if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
    7856              :     {
    7857              :       /* Since the typebound operators are generic, we have to ensure
    7858              :          that any delays in resolution are corrected and that the vtab
    7859              :          is present.  */
    7860          184 :       ts = expr->ts;
    7861          184 :       declared = ts.u.derived;
    7862          184 :       if (!resolve_fl_derived (declared))
    7863              :         return false;
    7864              : 
    7865          184 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    7866          184 :       if (c->ts.u.derived == NULL)
    7867            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    7868              : 
    7869          184 :       if (!resolve_compcall (e, &name))
    7870              :         return false;
    7871              : 
    7872              :       /* Use the generic name if it is there.  */
    7873          184 :       name = name ? name : e->value.function.esym->name;
    7874          184 :       e->symtree = expr->symtree;
    7875          184 :       e->ref = gfc_copy_ref (expr->ref);
    7876          184 :       get_declared_from_expr (&class_ref, NULL, e, false);
    7877              : 
    7878              :       /* Trim away the extraneous references that emerge from nested
    7879              :          use of interface.cc (extend_expr).  */
    7880          184 :       if (class_ref && class_ref->next)
    7881              :         {
    7882            0 :           gfc_free_ref_list (class_ref->next);
    7883            0 :           class_ref->next = NULL;
    7884              :         }
    7885          184 :       else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
    7886              :         {
    7887            0 :           gfc_free_ref_list (e->ref);
    7888            0 :           e->ref = NULL;
    7889              :         }
    7890              : 
    7891          184 :       gfc_add_vptr_component (e);
    7892          184 :       gfc_add_component_ref (e, name);
    7893          184 :       e->value.function.esym = NULL;
    7894          184 :       if (expr->expr_type != EXPR_VARIABLE)
    7895           80 :         e->base_expr = expr;
    7896          184 :       return true;
    7897              :     }
    7898              : 
    7899         1448 :   if (st == NULL)
    7900          159 :     return resolve_compcall (e, NULL);
    7901              : 
    7902         1289 :   if (!gfc_resolve_ref (e))
    7903              :     return false;
    7904              : 
    7905              :   /* It can happen that a generic, typebound procedure is marked as overridable
    7906              :      with all of the specific procedures being non-overridable. If this is the
    7907              :      case, it is safe to resolve the compcall.  */
    7908         1289 :   if (!expr && overridable
    7909         1281 :       && e->value.compcall.tbp->is_generic
    7910          186 :       && e->value.compcall.tbp->u.generic->specific
    7911          185 :       && e->value.compcall.tbp->u.generic->specific->non_overridable)
    7912              :     {
    7913              :       gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
    7914            6 :       for (; g; g = g->next)
    7915            4 :         if (!g->specific->non_overridable)
    7916              :           break;
    7917            2 :       if (g == NULL && resolve_compcall (e, &name))
    7918              :         return true;
    7919              :     }
    7920              : 
    7921              :   /* Get the CLASS declared type.  */
    7922         1287 :   declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
    7923              : 
    7924         1287 :   if (!resolve_fl_derived (declared))
    7925              :     return false;
    7926              : 
    7927              :   /* Weed out cases of the ultimate component being a derived type.  */
    7928         1287 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    7929         1193 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    7930              :     {
    7931          595 :       gfc_free_ref_list (new_ref);
    7932          595 :       return resolve_compcall (e, NULL);
    7933              :     }
    7934              : 
    7935          692 :   c = gfc_find_component (declared, "_data", true, true, NULL);
    7936              : 
    7937              :   /* Treat the call as if it is a typebound procedure, in order to roll
    7938              :      out the correct name for the specific function.  */
    7939          692 :   if (!resolve_compcall (e, &name))
    7940              :     {
    7941           15 :       gfc_free_ref_list (new_ref);
    7942           15 :       return false;
    7943              :     }
    7944          677 :   ts = e->ts;
    7945              : 
    7946          677 :   if (overridable)
    7947              :     {
    7948              :       /* Convert the expression to a procedure pointer component call.  */
    7949          675 :       e->value.function.esym = NULL;
    7950          675 :       e->symtree = st;
    7951              : 
    7952          675 :       if (new_ref)
    7953          125 :         e->ref = new_ref;
    7954              : 
    7955              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    7956          675 :       gfc_add_vptr_component (e);
    7957          675 :       gfc_add_component_ref (e, name);
    7958              : 
    7959              :       /* Recover the typespec for the expression.  This is really only
    7960              :         necessary for generic procedures, where the additional call
    7961              :         to gfc_add_component_ref seems to throw the collection of the
    7962              :         correct typespec.  */
    7963          675 :       e->ts = ts;
    7964              :     }
    7965            2 :   else if (new_ref)
    7966            0 :     gfc_free_ref_list (new_ref);
    7967              : 
    7968              :   return true;
    7969              : }
    7970              : 
    7971              : /* Resolve a typebound subroutine, or 'method'. First separate all
    7972              :    the non-CLASS references by calling resolve_typebound_call
    7973              :    directly.  */
    7974              : 
    7975              : static bool
    7976         1730 : resolve_typebound_subroutine (gfc_code *code)
    7977              : {
    7978         1730 :   gfc_symbol *declared;
    7979         1730 :   gfc_component *c;
    7980         1730 :   gfc_ref *new_ref;
    7981         1730 :   gfc_ref *class_ref;
    7982         1730 :   gfc_symtree *st;
    7983         1730 :   const char *name;
    7984         1730 :   gfc_typespec ts;
    7985         1730 :   gfc_expr *expr;
    7986         1730 :   bool overridable;
    7987              : 
    7988         1730 :   st = code->expr1->symtree;
    7989              : 
    7990              :   /* Deal with typebound operators for CLASS objects.  */
    7991         1730 :   expr = code->expr1->value.compcall.base_object;
    7992         1730 :   overridable = !code->expr1->value.compcall.tbp->non_overridable;
    7993         1730 :   if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
    7994              :     {
    7995              :       /* If the base_object is not a variable, the corresponding actual
    7996              :          argument expression must be stored in e->base_expression so
    7997              :          that the corresponding tree temporary can be used as the base
    7998              :          object in gfc_conv_procedure_call.  */
    7999          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8000              :         {
    8001              :           gfc_actual_arglist *args;
    8002              : 
    8003              :           args= code->expr1->value.function.actual;
    8004              :           for (; args; args = args->next)
    8005              :             if (expr == args->expr)
    8006              :               expr = args->expr;
    8007              :         }
    8008              : 
    8009              :       /* Since the typebound operators are generic, we have to ensure
    8010              :          that any delays in resolution are corrected and that the vtab
    8011              :          is present.  */
    8012          109 :       declared = expr->ts.u.derived;
    8013          109 :       c = gfc_find_component (declared, "_vptr", true, true, NULL);
    8014          109 :       if (c->ts.u.derived == NULL)
    8015            0 :         c->ts.u.derived = gfc_find_derived_vtab (declared);
    8016              : 
    8017          109 :       if (!resolve_typebound_call (code, &name, NULL))
    8018              :         return false;
    8019              : 
    8020              :       /* Use the generic name if it is there.  */
    8021          109 :       name = name ? name : code->expr1->value.function.esym->name;
    8022          109 :       code->expr1->symtree = expr->symtree;
    8023          109 :       code->expr1->ref = gfc_copy_ref (expr->ref);
    8024              : 
    8025              :       /* Trim away the extraneous references that emerge from nested
    8026              :          use of interface.cc (extend_expr).  */
    8027          109 :       get_declared_from_expr (&class_ref, NULL, code->expr1, false);
    8028          109 :       if (class_ref && class_ref->next)
    8029              :         {
    8030            0 :           gfc_free_ref_list (class_ref->next);
    8031            0 :           class_ref->next = NULL;
    8032              :         }
    8033          109 :       else if (code->expr1->ref && !class_ref)
    8034              :         {
    8035           18 :           gfc_free_ref_list (code->expr1->ref);
    8036           18 :           code->expr1->ref = NULL;
    8037              :         }
    8038              : 
    8039              :       /* Now use the procedure in the vtable.  */
    8040          109 :       gfc_add_vptr_component (code->expr1);
    8041          109 :       gfc_add_component_ref (code->expr1, name);
    8042          109 :       code->expr1->value.function.esym = NULL;
    8043          109 :       if (expr->expr_type != EXPR_VARIABLE)
    8044            0 :         code->expr1->base_expr = expr;
    8045          109 :       return true;
    8046              :     }
    8047              : 
    8048         1621 :   if (st == NULL)
    8049          340 :     return resolve_typebound_call (code, NULL, NULL);
    8050              : 
    8051         1281 :   if (!gfc_resolve_ref (code->expr1))
    8052              :     return false;
    8053              : 
    8054              :   /* Get the CLASS declared type.  */
    8055         1281 :   get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
    8056              : 
    8057              :   /* Weed out cases of the ultimate component being a derived type.  */
    8058         1281 :   if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
    8059         1216 :          || (!class_ref && st->n.sym->ts.type != BT_CLASS))
    8060              :     {
    8061          905 :       gfc_free_ref_list (new_ref);
    8062          905 :       return resolve_typebound_call (code, NULL, NULL);
    8063              :     }
    8064              : 
    8065          376 :   if (!resolve_typebound_call (code, &name, &overridable))
    8066              :     {
    8067            5 :       gfc_free_ref_list (new_ref);
    8068            5 :       return false;
    8069              :     }
    8070          371 :   ts = code->expr1->ts;
    8071              : 
    8072          371 :   if (overridable)
    8073              :     {
    8074              :       /* Convert the expression to a procedure pointer component call.  */
    8075          369 :       code->expr1->value.function.esym = NULL;
    8076          369 :       code->expr1->symtree = st;
    8077              : 
    8078          369 :       if (new_ref)
    8079           93 :         code->expr1->ref = new_ref;
    8080              : 
    8081              :       /* '_vptr' points to the vtab, which contains the procedure pointers.  */
    8082          369 :       gfc_add_vptr_component (code->expr1);
    8083          369 :       gfc_add_component_ref (code->expr1, name);
    8084              : 
    8085              :       /* Recover the typespec for the expression.  This is really only
    8086              :         necessary for generic procedures, where the additional call
    8087              :         to gfc_add_component_ref seems to throw the collection of the
    8088              :         correct typespec.  */
    8089          369 :       code->expr1->ts = ts;
    8090              :     }
    8091            2 :   else if (new_ref)
    8092            0 :     gfc_free_ref_list (new_ref);
    8093              : 
    8094              :   return true;
    8095              : }
    8096              : 
    8097              : 
    8098              : /* Resolve a CALL to a Procedure Pointer Component (Subroutine).  */
    8099              : 
    8100              : static bool
    8101          124 : resolve_ppc_call (gfc_code* c)
    8102              : {
    8103          124 :   gfc_component *comp;
    8104              : 
    8105          124 :   comp = gfc_get_proc_ptr_comp (c->expr1);
    8106          124 :   gcc_assert (comp != NULL);
    8107              : 
    8108          124 :   c->resolved_sym = c->expr1->symtree->n.sym;
    8109          124 :   c->expr1->expr_type = EXPR_VARIABLE;
    8110              : 
    8111          124 :   if (!comp->attr.subroutine)
    8112            1 :     gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
    8113              : 
    8114          124 :   if (!gfc_resolve_ref (c->expr1))
    8115              :     return false;
    8116              : 
    8117          124 :   if (!update_ppc_arglist (c->expr1))
    8118              :     return false;
    8119              : 
    8120          123 :   c->ext.actual = c->expr1->value.compcall.actual;
    8121              : 
    8122          123 :   if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
    8123          123 :                                !(comp->ts.interface
    8124           93 :                                  && comp->ts.interface->formal)))
    8125              :     return false;
    8126              : 
    8127          123 :   if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
    8128              :     return false;
    8129              : 
    8130          122 :   gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
    8131              : 
    8132          122 :   return true;
    8133              : }
    8134              : 
    8135              : 
    8136              : /* Resolve a Function Call to a Procedure Pointer Component (Function).  */
    8137              : 
    8138              : static bool
    8139          450 : resolve_expr_ppc (gfc_expr* e)
    8140              : {
    8141          450 :   gfc_component *comp;
    8142              : 
    8143          450 :   comp = gfc_get_proc_ptr_comp (e);
    8144          450 :   gcc_assert (comp != NULL);
    8145              : 
    8146              :   /* Convert to EXPR_FUNCTION.  */
    8147          450 :   e->expr_type = EXPR_FUNCTION;
    8148          450 :   e->value.function.isym = NULL;
    8149          450 :   e->value.function.actual = e->value.compcall.actual;
    8150          450 :   e->ts = comp->ts;
    8151          450 :   if (comp->as != NULL)
    8152              :     {
    8153           28 :       e->rank = comp->as->rank;
    8154           28 :       e->corank = comp->as->corank;
    8155              :     }
    8156              : 
    8157          450 :   if (!comp->attr.function)
    8158            3 :     gfc_add_function (&comp->attr, comp->name, &e->where);
    8159              : 
    8160          450 :   if (!gfc_resolve_ref (e))
    8161              :     return false;
    8162              : 
    8163          450 :   if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
    8164          450 :                                !(comp->ts.interface
    8165          449 :                                  && comp->ts.interface->formal)))
    8166              :     return false;
    8167              : 
    8168          450 :   if (!update_ppc_arglist (e))
    8169              :     return false;
    8170              : 
    8171          448 :   if (!check_pure_function(e))
    8172              :     return false;
    8173              : 
    8174          447 :   gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
    8175              : 
    8176          447 :   return true;
    8177              : }
    8178              : 
    8179              : 
    8180              : static bool
    8181        11409 : gfc_is_expandable_expr (gfc_expr *e)
    8182              : {
    8183        11409 :   gfc_constructor *con;
    8184              : 
    8185        11409 :   if (e->expr_type == EXPR_ARRAY)
    8186              :     {
    8187              :       /* Traverse the constructor looking for variables that are flavor
    8188              :          parameter.  Parameters must be expanded since they are fully used at
    8189              :          compile time.  */
    8190        11409 :       con = gfc_constructor_first (e->value.constructor);
    8191        30227 :       for (; con; con = gfc_constructor_next (con))
    8192              :         {
    8193        13314 :           if (con->expr->expr_type == EXPR_VARIABLE
    8194         5181 :               && con->expr->symtree
    8195         5181 :               && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
    8196         5099 :               || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
    8197              :             return true;
    8198         8133 :           if (con->expr->expr_type == EXPR_ARRAY
    8199         8133 :               && gfc_is_expandable_expr (con->expr))
    8200              :             return true;
    8201              :         }
    8202              :     }
    8203              : 
    8204              :   return false;
    8205              : }
    8206              : 
    8207              : 
    8208              : /* Sometimes variables in specification expressions of the result
    8209              :    of module procedures in submodules wind up not being the 'real'
    8210              :    dummy.  Find this, if possible, in the namespace of the first
    8211              :    formal argument.  */
    8212              : 
    8213              : static void
    8214         3453 : fixup_unique_dummy (gfc_expr *e)
    8215              : {
    8216         3453 :   gfc_symtree *st = NULL;
    8217         3453 :   gfc_symbol *s = NULL;
    8218              : 
    8219         3453 :   if (e->symtree->n.sym->ns->proc_name
    8220         3423 :       && e->symtree->n.sym->ns->proc_name->formal)
    8221         3423 :     s = e->symtree->n.sym->ns->proc_name->formal->sym;
    8222              : 
    8223         3423 :   if (s != NULL)
    8224         3423 :     st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
    8225              : 
    8226         3453 :   if (st != NULL
    8227           14 :       && st->n.sym != NULL
    8228           14 :       && st->n.sym->attr.dummy)
    8229           14 :     e->symtree = st;
    8230         3453 : }
    8231              : 
    8232              : 
    8233              : /* Resolve an expression.  That is, make sure that types of operands agree
    8234              :    with their operators, intrinsic operators are converted to function calls
    8235              :    for overloaded types and unresolved function references are resolved.  */
    8236              : 
    8237              : bool
    8238      7124311 : gfc_resolve_expr (gfc_expr *e)
    8239              : {
    8240      7124311 :   bool t;
    8241      7124311 :   bool inquiry_save, actual_arg_save, first_actual_arg_save;
    8242              : 
    8243      7124311 :   if (e == NULL || e->do_not_resolve_again)
    8244              :     return true;
    8245              : 
    8246              :   /* inquiry_argument only applies to variables.  */
    8247      5209540 :   inquiry_save = inquiry_argument;
    8248      5209540 :   actual_arg_save = actual_arg;
    8249      5209540 :   first_actual_arg_save = first_actual_arg;
    8250              : 
    8251      5209540 :   if (e->expr_type != EXPR_VARIABLE)
    8252              :     {
    8253      3885501 :       inquiry_argument = false;
    8254      3885501 :       actual_arg = false;
    8255      3885501 :       first_actual_arg = false;
    8256              :     }
    8257      1324039 :   else if (e->symtree != NULL
    8258      1323594 :            && *e->symtree->name == '@'
    8259         4160 :            && e->symtree->n.sym->attr.dummy)
    8260              :     {
    8261              :       /* Deal with submodule specification expressions that are not
    8262              :          found to be referenced in module.cc(read_cleanup).  */
    8263         3453 :       fixup_unique_dummy (e);
    8264              :     }
    8265              : 
    8266      5209540 :   switch (e->expr_type)
    8267              :     {
    8268       531818 :     case EXPR_OP:
    8269       531818 :       t = resolve_operator (e);
    8270       531818 :       break;
    8271              : 
    8272          162 :     case EXPR_CONDITIONAL:
    8273          162 :       t = resolve_conditional (e);
    8274          162 :       break;
    8275              : 
    8276      1668419 :     case EXPR_FUNCTION:
    8277      1668419 :     case EXPR_VARIABLE:
    8278              : 
    8279      1668419 :       if (check_host_association (e))
    8280       344416 :         t = resolve_function (e);
    8281              :       else
    8282      1324003 :         t = resolve_variable (e);
    8283              : 
    8284      1668419 :       if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
    8285         6911 :           && e->ref->type != REF_SUBSTRING)
    8286         2162 :         gfc_resolve_substring_charlen (e);
    8287              : 
    8288              :       break;
    8289              : 
    8290         1632 :     case EXPR_COMPCALL:
    8291         1632 :       t = resolve_typebound_function (e);
    8292         1632 :       break;
    8293              : 
    8294          508 :     case EXPR_SUBSTRING:
    8295          508 :       t = gfc_resolve_ref (e);
    8296          508 :       break;
    8297              : 
    8298              :     case EXPR_CONSTANT:
    8299              :     case EXPR_NULL:
    8300              :       t = true;
    8301              :       break;
    8302              : 
    8303          450 :     case EXPR_PPC:
    8304          450 :       t = resolve_expr_ppc (e);
    8305          450 :       break;
    8306              : 
    8307        71640 :     case EXPR_ARRAY:
    8308        71640 :       t = false;
    8309        71640 :       if (!gfc_resolve_ref (e))
    8310              :         break;
    8311              : 
    8312        71640 :       t = gfc_resolve_array_constructor (e);
    8313              :       /* Also try to expand a constructor.  */
    8314        71640 :       if (t)
    8315              :         {
    8316        71538 :           gfc_expression_rank (e);
    8317        71538 :           if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
    8318        67160 :             gfc_expand_constructor (e, false);
    8319              :         }
    8320              : 
    8321              :       /* This provides the opportunity for the length of constructors with
    8322              :          character valued function elements to propagate the string length
    8323              :          to the expression.  */
    8324        71538 :       if (t && e->ts.type == BT_CHARACTER)
    8325              :         {
    8326              :           /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
    8327              :              here rather then add a duplicate test for it above.  */
    8328        10733 :           gfc_expand_constructor (e, false);
    8329        10733 :           t = gfc_resolve_character_array_constructor (e);
    8330              :         }
    8331              : 
    8332              :       break;
    8333              : 
    8334        16561 :     case EXPR_STRUCTURE:
    8335        16561 :       t = gfc_resolve_ref (e);
    8336        16561 :       if (!t)
    8337              :         break;
    8338              : 
    8339        16561 :       t = resolve_structure_cons (e, 0);
    8340        16561 :       if (!t)
    8341              :         break;
    8342              : 
    8343        16549 :       t = gfc_simplify_expr (e, 0);
    8344        16549 :       break;
    8345              : 
    8346            0 :     default:
    8347            0 :       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
    8348              :     }
    8349              : 
    8350      5209540 :   if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
    8351       180758 :     fixup_charlen (e);
    8352              : 
    8353      5209540 :   inquiry_argument = inquiry_save;
    8354      5209540 :   actual_arg = actual_arg_save;
    8355      5209540 :   first_actual_arg = first_actual_arg_save;
    8356              : 
    8357              :   /* For some reason, resolving these expressions a second time mangles
    8358              :      the typespec of the expression itself.  */
    8359      5209540 :   if (t && e->expr_type == EXPR_VARIABLE
    8360      1321160 :       && e->symtree->n.sym->attr.select_rank_temporary
    8361         3428 :       && UNLIMITED_POLY (e->symtree->n.sym))
    8362           83 :     e->do_not_resolve_again = 1;
    8363              : 
    8364      5207000 :   if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
    8365         6919 :     t = check_import_status (e);
    8366              : 
    8367              :   return t;
    8368              : }
    8369              : 
    8370              : 
    8371              : /* Resolve an expression from an iterator.  They must be scalar and have
    8372              :    INTEGER or (optionally) REAL type.  */
    8373              : 
    8374              : static bool
    8375       151265 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
    8376              :                            const char *name_msgid)
    8377              : {
    8378       151265 :   if (!gfc_resolve_expr (expr))
    8379              :     return false;
    8380              : 
    8381       151260 :   if (expr->rank != 0)
    8382              :     {
    8383            0 :       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
    8384            0 :       return false;
    8385              :     }
    8386              : 
    8387       151260 :   if (expr->ts.type != BT_INTEGER)
    8388              :     {
    8389          274 :       if (expr->ts.type == BT_REAL)
    8390              :         {
    8391          274 :           if (real_ok)
    8392          271 :             return gfc_notify_std (GFC_STD_F95_DEL,
    8393              :                                    "%s at %L must be integer",
    8394          271 :                                    _(name_msgid), &expr->where);
    8395              :           else
    8396              :             {
    8397            3 :               gfc_error ("%s at %L must be INTEGER", _(name_msgid),
    8398              :                          &expr->where);
    8399            3 :               return false;
    8400              :             }
    8401              :         }
    8402              :       else
    8403              :         {
    8404            0 :           gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
    8405            0 :           return false;
    8406              :         }
    8407              :     }
    8408              :   return true;
    8409              : }
    8410              : 
    8411              : 
    8412              : /* Resolve the expressions in an iterator structure.  If REAL_OK is
    8413              :    false allow only INTEGER type iterators, otherwise allow REAL types.
    8414              :    Set own_scope to true for ac-implied-do and data-implied-do as those
    8415              :    have a separate scope such that, e.g., a INTENT(IN) doesn't apply.  */
    8416              : 
    8417              : bool
    8418        37825 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
    8419              : {
    8420        37825 :   if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
    8421              :     return false;
    8422              : 
    8423        37821 :   if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
    8424        37821 :                                  _("iterator variable")))
    8425              :     return false;
    8426              : 
    8427        37815 :   if (!gfc_resolve_iterator_expr (iter->start, real_ok,
    8428              :                                   "Start expression in DO loop"))
    8429              :     return false;
    8430              : 
    8431        37814 :   if (!gfc_resolve_iterator_expr (iter->end, real_ok,
    8432              :                                   "End expression in DO loop"))
    8433              :     return false;
    8434              : 
    8435        37811 :   if (!gfc_resolve_iterator_expr (iter->step, real_ok,
    8436              :                                   "Step expression in DO loop"))
    8437              :     return false;
    8438              : 
    8439              :   /* Convert start, end, and step to the same type as var.  */
    8440        37810 :   if (iter->start->ts.kind != iter->var->ts.kind
    8441        37530 :       || iter->start->ts.type != iter->var->ts.type)
    8442          315 :     gfc_convert_type (iter->start, &iter->var->ts, 1);
    8443              : 
    8444        37810 :   if (iter->end->ts.kind != iter->var->ts.kind
    8445        37557 :       || iter->end->ts.type != iter->var->ts.type)
    8446          278 :     gfc_convert_type (iter->end, &iter->var->ts, 1);
    8447              : 
    8448        37810 :   if (iter->step->ts.kind != iter->var->ts.kind
    8449        37566 :       || iter->step->ts.type != iter->var->ts.type)
    8450          280 :     gfc_convert_type (iter->step, &iter->var->ts, 1);
    8451              : 
    8452        37810 :   if (iter->step->expr_type == EXPR_CONSTANT)
    8453              :     {
    8454        36688 :       if ((iter->step->ts.type == BT_INTEGER
    8455        36605 :            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
    8456        73291 :           || (iter->step->ts.type == BT_REAL
    8457           83 :               && mpfr_sgn (iter->step->value.real) == 0))
    8458              :         {
    8459            3 :           gfc_error ("Step expression in DO loop at %L cannot be zero",
    8460            3 :                      &iter->step->where);
    8461            3 :           return false;
    8462              :         }
    8463              :     }
    8464              : 
    8465        37807 :   if (iter->start->expr_type == EXPR_CONSTANT
    8466        34675 :       && iter->end->expr_type == EXPR_CONSTANT
    8467        27127 :       && iter->step->expr_type == EXPR_CONSTANT)
    8468              :     {
    8469        26860 :       int sgn, cmp;
    8470        26860 :       if (iter->start->ts.type == BT_INTEGER)
    8471              :         {
    8472        26806 :           sgn = mpz_cmp_ui (iter->step->value.integer, 0);
    8473        26806 :           cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
    8474              :         }
    8475              :       else
    8476              :         {
    8477           54 :           sgn = mpfr_sgn (iter->step->value.real);
    8478           54 :           cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
    8479              :         }
    8480        26860 :       if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
    8481          146 :         gfc_warning (OPT_Wzerotrip,
    8482              :                      "DO loop at %L will be executed zero times",
    8483          146 :                      &iter->step->where);
    8484              :     }
    8485              : 
    8486        37807 :   if (iter->end->expr_type == EXPR_CONSTANT
    8487        27495 :       && iter->end->ts.type == BT_INTEGER
    8488        27441 :       && iter->step->expr_type == EXPR_CONSTANT
    8489        27131 :       && iter->step->ts.type == BT_INTEGER
    8490        27131 :       && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
    8491        26760 :           || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
    8492              :     {
    8493        25974 :       bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
    8494        25974 :       int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
    8495              : 
    8496        25974 :       if (is_step_positive
    8497        25603 :           && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
    8498            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8499              :                      "DO loop at %L is undefined as it overflows",
    8500            7 :                      &iter->step->where);
    8501              :       else if (!is_step_positive
    8502          371 :                && mpz_cmp (iter->end->value.integer,
    8503          371 :                            gfc_integer_kinds[k].min_int) == 0)
    8504            7 :         gfc_warning (OPT_Wundefined_do_loop,
    8505              :                      "DO loop at %L is undefined as it underflows",
    8506            7 :                      &iter->step->where);
    8507              :     }
    8508              : 
    8509              :   return true;
    8510              : }
    8511              : 
    8512              : 
    8513              : /* Traversal function for find_forall_index.  f == 2 signals that
    8514              :    that variable itself is not to be checked - only the references.  */
    8515              : 
    8516              : static bool
    8517        42620 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
    8518              : {
    8519        42620 :   if (expr->expr_type != EXPR_VARIABLE)
    8520              :     return false;
    8521              : 
    8522              :   /* A scalar assignment  */
    8523        18188 :   if (!expr->ref || *f == 1)
    8524              :     {
    8525        12128 :       if (expr->symtree->n.sym == sym)
    8526              :         return true;
    8527              :       else
    8528              :         return false;
    8529              :     }
    8530              : 
    8531         6060 :   if (*f == 2)
    8532         1731 :     *f = 1;
    8533              :   return false;
    8534              : }
    8535              : 
    8536              : 
    8537              : /* Check whether the FORALL index appears in the expression or not.
    8538              :    Returns true if SYM is found in EXPR.  */
    8539              : 
    8540              : bool
    8541        27001 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
    8542              : {
    8543        27001 :   if (gfc_traverse_expr (expr, sym, forall_index, f))
    8544              :     return true;
    8545              :   else
    8546              :     return false;
    8547              : }
    8548              : 
    8549              : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
    8550              :    This constraint specifies rules for variables in locality-specs.  */
    8551              : 
    8552              : static int
    8553          717 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
    8554              : {
    8555          717 :   struct check_default_none_data *dt = (struct check_default_none_data *) data;
    8556              : 
    8557          717 :   if ((*expr)->expr_type == EXPR_VARIABLE)
    8558              :     {
    8559           22 :       gfc_symbol *sym = (*expr)->symtree->n.sym;
    8560           22 :       for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
    8561           24 :            list; list = list->next)
    8562              :         {
    8563            5 :           if (list->expr->symtree->n.sym == sym)
    8564              :             {
    8565            3 :               gfc_error ("Variable %qs referenced in concurrent-header at %L "
    8566              :                          "must not appear in LOCAL locality-spec at %L",
    8567              :                          sym->name, &(*expr)->where, &list->expr->where);
    8568            3 :               *walk_subtrees = 0;
    8569            3 :               return 1;
    8570              :             }
    8571              :         }
    8572              :     }
    8573              : 
    8574          714 :     *walk_subtrees = 1;
    8575          714 :     return 0;
    8576              : }
    8577              : 
    8578              : static int
    8579         3969 : check_default_none_expr (gfc_expr **e, int *, void *data)
    8580              : {
    8581         3969 :   struct check_default_none_data *d = (struct check_default_none_data*) data;
    8582              : 
    8583         3969 :   if ((*e)->expr_type == EXPR_VARIABLE)
    8584              :     {
    8585         1798 :       gfc_symbol *sym = (*e)->symtree->n.sym;
    8586              : 
    8587         1798 :       if (d->sym_hash->contains (sym))
    8588         1263 :         sym->mark = 1;
    8589              : 
    8590          535 :       else if (d->default_none)
    8591              :         {
    8592            6 :           gfc_namespace *ns2 = d->ns;
    8593           10 :           while (ns2)
    8594              :             {
    8595            6 :               if (ns2 == sym->ns)
    8596              :                 break;
    8597            4 :               ns2 = ns2->parent;
    8598              :             }
    8599              : 
    8600              :           /* A DO CONCURRENT iterator cannot appear in a locality spec.  */
    8601            6 :           if (sym->ns->code->ext.concur.forall_iterator)
    8602              :             {
    8603              :               gfc_forall_iterator *iter
    8604              :                 = sym->ns->code->ext.concur.forall_iterator;
    8605            5 :               for (; iter; iter = iter->next)
    8606            3 :                 if (iter->var->symtree
    8607            1 :                     && strcmp(sym->name, iter->var->symtree->name) == 0)
    8608            1 :                   return 0;
    8609              :             }
    8610              : 
    8611              :           /* A named constant is not a variable, so skip test.  */
    8612            5 :           if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
    8613              :             {
    8614            1 :               gfc_error ("Variable %qs at %L not specified in a locality spec "
    8615              :                         "of DO CONCURRENT at %L but required due to "
    8616              :                         "DEFAULT (NONE)",
    8617            1 :                         sym->name, &(*e)->where, &d->code->loc);
    8618            1 :               d->sym_hash->add (sym);
    8619              :             }
    8620              :         }
    8621              :     }
    8622              :   return 0;
    8623              : }
    8624              : 
    8625              : static void
    8626          210 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
    8627              : {
    8628          210 :   struct check_default_none_data data;
    8629          210 :   data.code = code;
    8630          210 :   data.sym_hash = new hash_set<gfc_symbol *>;
    8631          210 :   data.ns = ns;
    8632          210 :   data.default_none = code->ext.concur.default_none;
    8633              : 
    8634         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8635              :     {
    8636          840 :       const char *name;
    8637          840 :       switch (locality)
    8638              :         {
    8639              :           case LOCALITY_LOCAL: name = "LOCAL"; break;
    8640          210 :           case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
    8641          210 :           case LOCALITY_SHARED: name = "SHARED"; break;
    8642          210 :           case LOCALITY_REDUCE: name = "REDUCE"; break;
    8643              :           default: gcc_unreachable ();
    8644              :         }
    8645              : 
    8646         1227 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8647          387 :            list = list->next)
    8648              :         {
    8649          387 :           gfc_expr *expr = list->expr;
    8650              : 
    8651          387 :           if (locality == LOCALITY_REDUCE
    8652           72 :               && (expr->expr_type == EXPR_FUNCTION
    8653           48 :                   || expr->expr_type == EXPR_OP))
    8654           35 :             continue;
    8655              : 
    8656          363 :           if (!gfc_resolve_expr (expr))
    8657            3 :             continue;
    8658              : 
    8659          360 :           if (expr->expr_type != EXPR_VARIABLE
    8660          360 :               || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
    8661          360 :               || (expr->ref
    8662          147 :                   && (expr->ref->type != REF_ARRAY
    8663          147 :                       || expr->ref->u.ar.type != AR_FULL
    8664          143 :                       || expr->ref->next)))
    8665              :             {
    8666            4 :               gfc_error ("Expected variable name in %s locality spec at %L",
    8667              :                          name, &expr->where);
    8668            4 :                 continue;
    8669              :             }
    8670              : 
    8671          356 :           gfc_symbol *sym = expr->symtree->n.sym;
    8672              : 
    8673          356 :           if (data.sym_hash->contains (sym))
    8674              :             {
    8675            4 :               gfc_error ("Variable %qs at %L has already been specified in a "
    8676              :                          "locality-spec", sym->name, &expr->where);
    8677            4 :               continue;
    8678              :             }
    8679              : 
    8680          352 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8681          704 :                iter; iter = iter->next)
    8682              :             {
    8683          352 :               if (iter->var->symtree->n.sym == sym)
    8684              :                 {
    8685            1 :                   gfc_error ("Index variable %qs at %L cannot be specified in a "
    8686              :                              "locality-spec", sym->name, &expr->where);
    8687            1 :                   continue;
    8688              :                 }
    8689              : 
    8690          351 :               data.sym_hash->add (iter->var->symtree->n.sym);
    8691              :             }
    8692              : 
    8693          352 :           if (locality == LOCALITY_LOCAL
    8694          352 :               || locality == LOCALITY_LOCAL_INIT
    8695          352 :               || locality == LOCALITY_REDUCE)
    8696              :             {
    8697          198 :               if (sym->attr.optional)
    8698            3 :                 gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
    8699              :                            "locality-spec at %L",
    8700              :                            sym->name, name, &expr->where);
    8701              : 
    8702          198 :               if (sym->attr.dimension
    8703           66 :                   && sym->as
    8704           66 :                   && sym->as->type == AS_ASSUMED_SIZE)
    8705            0 :                 gfc_error ("Assumed-size array not permitted for %qs in %s "
    8706              :                            "locality-spec at %L",
    8707              :                            sym->name, name, &expr->where);
    8708              : 
    8709          198 :               gfc_check_vardef_context (expr, false, false, false, name);
    8710              :             }
    8711              : 
    8712          198 :           if (locality == LOCALITY_LOCAL
    8713              :               || locality == LOCALITY_LOCAL_INIT)
    8714              :             {
    8715          181 :               symbol_attribute attr = gfc_expr_attr (expr);
    8716              : 
    8717          181 :               if (attr.allocatable)
    8718            2 :                 gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
    8719              :                            "locality-spec at %L",
    8720              :                            sym->name, name, &expr->where);
    8721              : 
    8722          179 :               else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
    8723            2 :                 gfc_error ("Nonpointer polymorphic dummy argument not permitted"
    8724              :                            " for %qs in %s locality-spec at %L",
    8725              :                            sym->name, name, &expr->where);
    8726              : 
    8727          177 :               else if (attr.codimension)
    8728            0 :                 gfc_error ("Coarray not permitted for %qs in %s locality-spec "
    8729              :                            "at %L",
    8730              :                            sym->name, name, &expr->where);
    8731              : 
    8732          177 :               else if (expr->ts.type == BT_DERIVED
    8733          177 :                        && gfc_is_finalizable (expr->ts.u.derived, NULL))
    8734            0 :                 gfc_error ("Finalizable type not permitted for %qs in %s "
    8735              :                            "locality-spec at %L",
    8736              :                            sym->name, name, &expr->where);
    8737              : 
    8738          177 :               else if (gfc_has_ultimate_allocatable (expr))
    8739            4 :                 gfc_error ("Type with ultimate allocatable component not "
    8740              :                            "permitted for %qs in %s locality-spec at %L",
    8741              :                            sym->name, name, &expr->where);
    8742              :             }
    8743              : 
    8744          171 :           else if (locality == LOCALITY_REDUCE)
    8745              :             {
    8746           17 :               if (sym->attr.asynchronous)
    8747            1 :                 gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
    8748              :                            "REDUCE locality-spec at %L",
    8749              :                            sym->name, &expr->where);
    8750           17 :               if (sym->attr.volatile_)
    8751            1 :                 gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
    8752              :                            "locality-spec at %L", sym->name, &expr->where);
    8753              :             }
    8754              : 
    8755          352 :           data.sym_hash->add (sym);
    8756              :         }
    8757              : 
    8758          840 :       if (locality == LOCALITY_LOCAL)
    8759              :         {
    8760          210 :           gcc_assert (locality == 0);
    8761              : 
    8762          210 :           for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
    8763          437 :                iter; iter = iter->next)
    8764              :             {
    8765          227 :               gfc_expr_walker (&iter->start,
    8766              :                                do_concur_locality_specs_f2023,
    8767              :                                &data);
    8768              : 
    8769          227 :               gfc_expr_walker (&iter->end,
    8770              :                                do_concur_locality_specs_f2023,
    8771              :                                &data);
    8772              : 
    8773          227 :               gfc_expr_walker (&iter->stride,
    8774              :                                do_concur_locality_specs_f2023,
    8775              :                                &data);
    8776              :             }
    8777              : 
    8778          210 :           if (code->expr1)
    8779            7 :             gfc_expr_walker (&code->expr1,
    8780              :                              do_concur_locality_specs_f2023,
    8781              :                              &data);
    8782              :         }
    8783              :     }
    8784              : 
    8785          210 :   gfc_expr *reduce_op = NULL;
    8786              : 
    8787          210 :   for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
    8788          258 :        list; list = list->next)
    8789              :     {
    8790           48 :       gfc_expr *expr = list->expr;
    8791              : 
    8792           48 :       if (expr->expr_type != EXPR_VARIABLE)
    8793              :         {
    8794           24 :           reduce_op = expr;
    8795           24 :           continue;
    8796              :         }
    8797              : 
    8798           24 :       if (reduce_op->expr_type == EXPR_OP)
    8799              :         {
    8800           17 :           switch (reduce_op->value.op.op)
    8801              :             {
    8802           17 :               case INTRINSIC_PLUS:
    8803           17 :               case INTRINSIC_TIMES:
    8804           17 :                 if (!gfc_numeric_ts (&expr->ts))
    8805            3 :                   gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
    8806            3 :                              "got %s", expr->symtree->n.sym->name,
    8807              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8808              :                 break;
    8809            0 :               case INTRINSIC_AND:
    8810            0 :               case INTRINSIC_OR:
    8811            0 :               case INTRINSIC_EQV:
    8812            0 :               case INTRINSIC_NEQV:
    8813            0 :                 if (expr->ts.type != BT_LOGICAL)
    8814            0 :                   gfc_error ("Expected logical type for %qs in REDUCE at %L, "
    8815            0 :                              "got %qs", expr->symtree->n.sym->name,
    8816              :                              &expr->where, gfc_basic_typename (expr->ts.type));
    8817              :                 break;
    8818            0 :               default:
    8819            0 :                 gcc_unreachable ();
    8820              :             }
    8821              :         }
    8822              : 
    8823            7 :       else if (reduce_op->expr_type == EXPR_FUNCTION)
    8824              :         {
    8825            7 :           switch (reduce_op->value.function.isym->id)
    8826              :             {
    8827            6 :               case GFC_ISYM_MIN:
    8828            6 :               case GFC_ISYM_MAX:
    8829            6 :                 if (expr->ts.type != BT_INTEGER
    8830              :                     && expr->ts.type != BT_REAL
    8831              :                     && expr->ts.type != BT_CHARACTER)
    8832            2 :                   gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
    8833              :                              "in REDUCE with MIN/MAX at %L, got %s",
    8834            2 :                              expr->symtree->n.sym->name, &expr->where,
    8835              :                              gfc_basic_typename (expr->ts.type));
    8836              :                 break;
    8837            1 :               case GFC_ISYM_IAND:
    8838            1 :               case GFC_ISYM_IOR:
    8839            1 :               case GFC_ISYM_IEOR:
    8840            1 :                 if (expr->ts.type != BT_INTEGER)
    8841            1 :                   gfc_error ("Expected integer type for %qs in REDUCE with "
    8842              :                              "IAND/IOR/IEOR at %L, got %s",
    8843            1 :                              expr->symtree->n.sym->name, &expr->where,
    8844              :                              gfc_basic_typename (expr->ts.type));
    8845              :                 break;
    8846            0 :               default:
    8847            0 :                 gcc_unreachable ();
    8848              :             }
    8849              :         }
    8850              : 
    8851              :       else
    8852            0 :         gcc_unreachable ();
    8853              :     }
    8854              : 
    8855         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8856              :     {
    8857         1227 :       for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
    8858          387 :            list = list->next)
    8859              :         {
    8860          387 :           if (list->expr->expr_type == EXPR_VARIABLE)
    8861          363 :             list->expr->symtree->n.sym->mark = 0;
    8862              :         }
    8863              :     }
    8864              : 
    8865          210 :   gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
    8866              :                    check_default_none_expr, &data);
    8867              : 
    8868         1050 :   for (int locality = 0; locality < LOCALITY_NUM; locality++)
    8869              :     {
    8870          840 :       gfc_expr_list **plist = &code->ext.concur.locality[locality];
    8871         1227 :       while (*plist)
    8872              :         {
    8873          387 :           gfc_expr *expr = (*plist)->expr;
    8874          387 :           if (expr->expr_type == EXPR_VARIABLE)
    8875              :             {
    8876          363 :               gfc_symbol *sym = expr->symtree->n.sym;
    8877          363 :               if (sym->mark == 0)
    8878              :                 {
    8879           70 :                   gfc_warning (OPT_Wunused_variable, "Variable %qs in "
    8880              :                                "locality-spec at %L is not used",
    8881              :                                sym->name, &expr->where);
    8882           70 :                   gfc_expr_list *tmp = *plist;
    8883           70 :                   *plist = (*plist)->next;
    8884           70 :                   gfc_free_expr (tmp->expr);
    8885           70 :                   free (tmp);
    8886           70 :                   continue;
    8887           70 :                 }
    8888              :             }
    8889          317 :           plist = &((*plist)->next);
    8890              :         }
    8891              :     }
    8892              : 
    8893          420 :   delete data.sym_hash;
    8894          210 : }
    8895              : 
    8896              : /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
    8897              :    to be a scalar INTEGER variable.  The subscripts and stride are scalar
    8898              :    INTEGERs, and if stride is a constant it must be nonzero.
    8899              :    Furthermore "A subscript or stride in a forall-triplet-spec shall
    8900              :    not contain a reference to any index-name in the
    8901              :    forall-triplet-spec-list in which it appears." (7.5.4.1)  */
    8902              : 
    8903              : static void
    8904         2202 : resolve_forall_iterators (gfc_forall_iterator *it)
    8905              : {
    8906         2202 :   gfc_forall_iterator *iter, *iter2;
    8907              : 
    8908         6320 :   for (iter = it; iter; iter = iter->next)
    8909              :     {
    8910         4118 :       if (gfc_resolve_expr (iter->var)
    8911         4118 :           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
    8912            0 :         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
    8913              :                    &iter->var->where);
    8914              : 
    8915         4118 :       if (gfc_resolve_expr (iter->start)
    8916         4118 :           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
    8917            0 :         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
    8918              :                    &iter->start->where);
    8919         4118 :       if (iter->var->ts.kind != iter->start->ts.kind)
    8920            1 :         gfc_convert_type (iter->start, &iter->var->ts, 1);
    8921              : 
    8922         4118 :       if (gfc_resolve_expr (iter->end)
    8923         4118 :           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
    8924            0 :         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
    8925              :                    &iter->end->where);
    8926         4118 :       if (iter->var->ts.kind != iter->end->ts.kind)
    8927            2 :         gfc_convert_type (iter->end, &iter->var->ts, 1);
    8928              : 
    8929         4118 :       if (gfc_resolve_expr (iter->stride))
    8930              :         {
    8931         4118 :           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
    8932            0 :             gfc_error ("FORALL stride expression at %L must be a scalar %s",
    8933              :                        &iter->stride->where, "INTEGER");
    8934              : 
    8935         4118 :           if (iter->stride->expr_type == EXPR_CONSTANT
    8936         4115 :               && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
    8937            1 :             gfc_error ("FORALL stride expression at %L cannot be zero",
    8938              :                        &iter->stride->where);
    8939              :         }
    8940         4118 :       if (iter->var->ts.kind != iter->stride->ts.kind)
    8941            1 :         gfc_convert_type (iter->stride, &iter->var->ts, 1);
    8942              :     }
    8943              : 
    8944         6320 :   for (iter = it; iter; iter = iter->next)
    8945        11078 :     for (iter2 = iter; iter2; iter2 = iter2->next)
    8946              :       {
    8947         6960 :         if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
    8948         6958 :             || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
    8949        13916 :             || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
    8950            6 :           gfc_error ("FORALL index %qs may not appear in triplet "
    8951            6 :                      "specification at %L", iter->var->symtree->name,
    8952            6 :                      &iter2->start->where);
    8953              :       }
    8954         2202 : }
    8955              : 
    8956              : 
    8957              : /* Given a pointer to a symbol that is a derived type, see if it's
    8958              :    inaccessible, i.e. if it's defined in another module and the components are
    8959              :    PRIVATE.  The search is recursive if necessary.  Returns zero if no
    8960              :    inaccessible components are found, nonzero otherwise.  */
    8961              : 
    8962              : static bool
    8963         1352 : derived_inaccessible (gfc_symbol *sym)
    8964              : {
    8965         1352 :   gfc_component *c;
    8966              : 
    8967         1352 :   if (sym->attr.use_assoc && sym->attr.private_comp)
    8968              :     return 1;
    8969              : 
    8970         4001 :   for (c = sym->components; c; c = c->next)
    8971              :     {
    8972              :         /* Prevent an infinite loop through this function.  */
    8973         2662 :         if (c->ts.type == BT_DERIVED
    8974          289 :             && (c->attr.pointer || c->attr.allocatable)
    8975           72 :             && sym == c->ts.u.derived)
    8976           72 :           continue;
    8977              : 
    8978         2590 :         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
    8979              :           return 1;
    8980              :     }
    8981              : 
    8982              :   return 0;
    8983              : }
    8984              : 
    8985              : 
    8986              : /* Resolve the argument of a deallocate expression.  The expression must be
    8987              :    a pointer or a full array.  */
    8988              : 
    8989              : static bool
    8990         8342 : resolve_deallocate_expr (gfc_expr *e)
    8991              : {
    8992         8342 :   symbol_attribute attr;
    8993         8342 :   int allocatable, pointer;
    8994         8342 :   gfc_ref *ref;
    8995         8342 :   gfc_symbol *sym;
    8996         8342 :   gfc_component *c;
    8997         8342 :   bool unlimited;
    8998              : 
    8999         8342 :   if (!gfc_resolve_expr (e))
    9000              :     return false;
    9001              : 
    9002         8342 :   if (e->expr_type != EXPR_VARIABLE)
    9003            0 :     goto bad;
    9004              : 
    9005         8342 :   sym = e->symtree->n.sym;
    9006         8342 :   unlimited = UNLIMITED_POLY(sym);
    9007              : 
    9008         8342 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
    9009              :     {
    9010         1574 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    9011         1574 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    9012              :     }
    9013              :   else
    9014              :     {
    9015         6768 :       allocatable = sym->attr.allocatable;
    9016         6768 :       pointer = sym->attr.pointer;
    9017              :     }
    9018        16755 :   for (ref = e->ref; ref; ref = ref->next)
    9019              :     {
    9020         8413 :       switch (ref->type)
    9021              :         {
    9022         6275 :         case REF_ARRAY:
    9023         6275 :           if (ref->u.ar.type != AR_FULL
    9024         6483 :               && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
    9025          208 :                    && ref->u.ar.codimen && gfc_ref_this_image (ref)))
    9026              :             allocatable = 0;
    9027              :           break;
    9028              : 
    9029         2138 :         case REF_COMPONENT:
    9030         2138 :           c = ref->u.c.component;
    9031         2138 :           if (c->ts.type == BT_CLASS)
    9032              :             {
    9033          297 :               allocatable = CLASS_DATA (c)->attr.allocatable;
    9034          297 :               pointer = CLASS_DATA (c)->attr.class_pointer;
    9035              :             }
    9036              :           else
    9037              :             {
    9038         1841 :               allocatable = c->attr.allocatable;
    9039         1841 :               pointer = c->attr.pointer;
    9040              :             }
    9041              :           break;
    9042              : 
    9043              :         case REF_SUBSTRING:
    9044              :         case REF_INQUIRY:
    9045          513 :           allocatable = 0;
    9046              :           break;
    9047              :         }
    9048              :     }
    9049              : 
    9050         8342 :   attr = gfc_expr_attr (e);
    9051              : 
    9052         8342 :   if (allocatable == 0 && attr.pointer == 0 && !unlimited)
    9053              :     {
    9054            3 :     bad:
    9055            3 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9056              :                  &e->where);
    9057            3 :       return false;
    9058              :     }
    9059              : 
    9060              :   /* F2008, C644.  */
    9061         8339 :   if (gfc_is_coindexed (e))
    9062              :     {
    9063            1 :       gfc_error ("Coindexed allocatable object at %L", &e->where);
    9064            1 :       return false;
    9065              :     }
    9066              : 
    9067         8338 :   if (pointer
    9068        10706 :       && !gfc_check_vardef_context (e, true, true, false,
    9069         2368 :                                     _("DEALLOCATE object")))
    9070              :     return false;
    9071         8336 :   if (!gfc_check_vardef_context (e, false, true, false,
    9072         8336 :                                  _("DEALLOCATE object")))
    9073              :     return false;
    9074              : 
    9075              :   return true;
    9076              : }
    9077              : 
    9078              : 
    9079              : /* Returns true if the expression e contains a reference to the symbol sym.  */
    9080              : static bool
    9081        47360 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
    9082              : {
    9083        47360 :   if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
    9084         2081 :     return true;
    9085              : 
    9086              :   return false;
    9087              : }
    9088              : 
    9089              : bool
    9090        20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
    9091              : {
    9092        20080 :   return gfc_traverse_expr (e, sym, sym_in_expr, 0);
    9093              : }
    9094              : 
    9095              : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
    9096              :    of character expressions.  */
    9097              : static bool
    9098        20457 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
    9099              : {
    9100            0 :   return gfc_traverse_expr (e, sym, sym_in_expr, -1);
    9101              : }
    9102              : 
    9103              : 
    9104              : /* Given the expression node e for an allocatable/pointer of derived type to be
    9105              :    allocated, get the expression node to be initialized afterwards (needed for
    9106              :    derived types with default initializers, and derived types with allocatable
    9107              :    components that need nullification.)  */
    9108              : 
    9109              : gfc_expr *
    9110         5780 : gfc_expr_to_initialize (gfc_expr *e)
    9111              : {
    9112         5780 :   gfc_expr *result;
    9113         5780 :   gfc_ref *ref;
    9114         5780 :   int i;
    9115              : 
    9116         5780 :   result = gfc_copy_expr (e);
    9117              : 
    9118              :   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
    9119        11431 :   for (ref = result->ref; ref; ref = ref->next)
    9120         9014 :     if (ref->type == REF_ARRAY && ref->next == NULL)
    9121              :       {
    9122         3363 :         if (ref->u.ar.dimen == 0
    9123           77 :             && ref->u.ar.as && ref->u.ar.as->corank)
    9124              :           return result;
    9125              : 
    9126         3286 :         ref->u.ar.type = AR_FULL;
    9127              : 
    9128         7424 :         for (i = 0; i < ref->u.ar.dimen; i++)
    9129         4138 :           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
    9130              : 
    9131              :         break;
    9132              :       }
    9133              : 
    9134         5703 :   gfc_free_shape (&result->shape, result->rank);
    9135              : 
    9136              :   /* Recalculate rank, shape, etc.  */
    9137         5703 :   gfc_resolve_expr (result);
    9138         5703 :   return result;
    9139              : }
    9140              : 
    9141              : 
    9142              : /* If the last ref of an expression is an array ref, return a copy of the
    9143              :    expression with that one removed.  Otherwise, a copy of the original
    9144              :    expression.  This is used for allocate-expressions and pointer assignment
    9145              :    LHS, where there may be an array specification that needs to be stripped
    9146              :    off when using gfc_check_vardef_context.  */
    9147              : 
    9148              : static gfc_expr*
    9149        27665 : remove_last_array_ref (gfc_expr* e)
    9150              : {
    9151        27665 :   gfc_expr* e2;
    9152        27665 :   gfc_ref** r;
    9153              : 
    9154        27665 :   e2 = gfc_copy_expr (e);
    9155        35723 :   for (r = &e2->ref; *r; r = &(*r)->next)
    9156        24392 :     if ((*r)->type == REF_ARRAY && !(*r)->next)
    9157              :       {
    9158        16334 :         gfc_free_ref_list (*r);
    9159        16334 :         *r = NULL;
    9160        16334 :         break;
    9161              :       }
    9162              : 
    9163        27665 :   return e2;
    9164              : }
    9165              : 
    9166              : 
    9167              : /* Used in resolve_allocate_expr to check that a allocation-object and
    9168              :    a source-expr are conformable.  This does not catch all possible
    9169              :    cases; in particular a runtime checking is needed.  */
    9170              : 
    9171              : static bool
    9172         1909 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
    9173              : {
    9174         1909 :   gfc_ref *tail;
    9175         1909 :   bool scalar;
    9176              : 
    9177         2641 :   for (tail = e2->ref; tail && tail->next; tail = tail->next);
    9178              : 
    9179              :   /* If MOLD= is present and is not scalar, and the allocate-object has an
    9180              :      explicit-shape-spec, the ranks need not agree.  This may be unintended,
    9181              :      so let's emit a warning if -Wsurprising is given.  */
    9182         1909 :   scalar = !tail || tail->type == REF_COMPONENT;
    9183         1909 :   if (e1->mold && e1->rank > 0
    9184          165 :       && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
    9185              :     {
    9186           27 :       if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
    9187           15 :         gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
    9188              :                      "but MOLD= expression at %L has rank %d",
    9189            6 :                      &e2->where, scalar ? 0 : tail->u.ar.as->rank,
    9190              :                      &e1->where, e1->rank);
    9191           30 :       return true;
    9192              :     }
    9193              : 
    9194              :   /* First compare rank.  */
    9195         1879 :   if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
    9196            2 :       || (!tail && e1->rank != e2->rank))
    9197              :     {
    9198            7 :       gfc_error ("Source-expr at %L must be scalar or have the "
    9199              :                  "same rank as the allocate-object at %L",
    9200              :                  &e1->where, &e2->where);
    9201            7 :       return false;
    9202              :     }
    9203              : 
    9204         1872 :   if (e1->shape)
    9205              :     {
    9206         1373 :       int i;
    9207         1373 :       mpz_t s;
    9208              : 
    9209         1373 :       mpz_init (s);
    9210              : 
    9211         3165 :       for (i = 0; i < e1->rank; i++)
    9212              :         {
    9213         1379 :           if (tail->u.ar.start[i] == NULL)
    9214              :             break;
    9215              : 
    9216          419 :           if (tail->u.ar.end[i])
    9217              :             {
    9218           54 :               mpz_set (s, tail->u.ar.end[i]->value.integer);
    9219           54 :               mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
    9220           54 :               mpz_add_ui (s, s, 1);
    9221              :             }
    9222              :           else
    9223              :             {
    9224          365 :               mpz_set (s, tail->u.ar.start[i]->value.integer);
    9225              :             }
    9226              : 
    9227          419 :           if (mpz_cmp (e1->shape[i], s) != 0)
    9228              :             {
    9229            0 :               gfc_error ("Source-expr at %L and allocate-object at %L must "
    9230              :                          "have the same shape", &e1->where, &e2->where);
    9231            0 :               mpz_clear (s);
    9232            0 :               return false;
    9233              :             }
    9234              :         }
    9235              : 
    9236         1373 :       mpz_clear (s);
    9237              :     }
    9238              : 
    9239              :   return true;
    9240              : }
    9241              : 
    9242              : 
    9243              : /* Resolve the expression in an ALLOCATE statement, doing the additional
    9244              :    checks to see whether the expression is OK or not.  The expression must
    9245              :    have a trailing array reference that gives the size of the array.  */
    9246              : 
    9247              : static bool
    9248        17308 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
    9249              : {
    9250        17308 :   int i, pointer, allocatable, dimension, is_abstract;
    9251        17308 :   int codimension;
    9252        17308 :   bool coindexed;
    9253        17308 :   bool unlimited;
    9254        17308 :   symbol_attribute attr;
    9255        17308 :   gfc_ref *ref, *ref2;
    9256        17308 :   gfc_expr *e2;
    9257        17308 :   gfc_array_ref *ar;
    9258        17308 :   gfc_symbol *sym = NULL;
    9259        17308 :   gfc_alloc *a;
    9260        17308 :   gfc_component *c;
    9261        17308 :   bool t;
    9262              : 
    9263              :   /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
    9264              :      checking of coarrays.  */
    9265        22035 :   for (ref = e->ref; ref; ref = ref->next)
    9266        17875 :     if (ref->next == NULL)
    9267              :       break;
    9268              : 
    9269        17308 :   if (ref && ref->type == REF_ARRAY)
    9270        11953 :     ref->u.ar.in_allocate = true;
    9271              : 
    9272        17308 :   if (!gfc_resolve_expr (e))
    9273            1 :     goto failure;
    9274              : 
    9275              :   /* Make sure the expression is allocatable or a pointer.  If it is
    9276              :      pointer, the next-to-last reference must be a pointer.  */
    9277              : 
    9278        17307 :   ref2 = NULL;
    9279        17307 :   if (e->symtree)
    9280        17307 :     sym = e->symtree->n.sym;
    9281              : 
    9282              :   /* Check whether ultimate component is abstract and CLASS.  */
    9283        34614 :   is_abstract = 0;
    9284              : 
    9285              :   /* Is the allocate-object unlimited polymorphic?  */
    9286        17307 :   unlimited = UNLIMITED_POLY(e);
    9287              : 
    9288        17307 :   if (e->expr_type != EXPR_VARIABLE)
    9289              :     {
    9290            0 :       allocatable = 0;
    9291            0 :       attr = gfc_expr_attr (e);
    9292            0 :       pointer = attr.pointer;
    9293            0 :       dimension = attr.dimension;
    9294            0 :       codimension = attr.codimension;
    9295              :     }
    9296              :   else
    9297              :     {
    9298        17307 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    9299              :         {
    9300         3390 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
    9301         3390 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
    9302         3390 :           dimension = CLASS_DATA (sym)->attr.dimension;
    9303         3390 :           codimension = CLASS_DATA (sym)->attr.codimension;
    9304         3390 :           is_abstract = CLASS_DATA (sym)->attr.abstract;
    9305              :         }
    9306              :       else
    9307              :         {
    9308        13917 :           allocatable = sym->attr.allocatable;
    9309        13917 :           pointer = sym->attr.pointer;
    9310        13917 :           dimension = sym->attr.dimension;
    9311        13917 :           codimension = sym->attr.codimension;
    9312              :         }
    9313              : 
    9314        17307 :       coindexed = false;
    9315              : 
    9316        35176 :       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
    9317              :         {
    9318        17871 :           switch (ref->type)
    9319              :             {
    9320        13387 :               case REF_ARRAY:
    9321        13387 :                 if (ref->u.ar.codimen > 0)
    9322              :                   {
    9323          760 :                     int n;
    9324         1061 :                     for (n = ref->u.ar.dimen;
    9325         1061 :                          n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    9326          801 :                       if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    9327              :                         {
    9328              :                           coindexed = true;
    9329              :                           break;
    9330              :                         }
    9331              :                    }
    9332              : 
    9333        13387 :                 if (ref->next != NULL)
    9334         1436 :                   pointer = 0;
    9335              :                 break;
    9336              : 
    9337         4484 :               case REF_COMPONENT:
    9338              :                 /* F2008, C644.  */
    9339         4484 :                 if (coindexed)
    9340              :                   {
    9341            2 :                     gfc_error ("Coindexed allocatable object at %L",
    9342              :                                &e->where);
    9343            2 :                     goto failure;
    9344              :                   }
    9345              : 
    9346         4482 :                 c = ref->u.c.component;
    9347         4482 :                 if (c->ts.type == BT_CLASS)
    9348              :                   {
    9349          988 :                     allocatable = CLASS_DATA (c)->attr.allocatable;
    9350          988 :                     pointer = CLASS_DATA (c)->attr.class_pointer;
    9351          988 :                     dimension = CLASS_DATA (c)->attr.dimension;
    9352          988 :                     codimension = CLASS_DATA (c)->attr.codimension;
    9353          988 :                     is_abstract = CLASS_DATA (c)->attr.abstract;
    9354              :                   }
    9355              :                 else
    9356              :                   {
    9357         3494 :                     allocatable = c->attr.allocatable;
    9358         3494 :                     pointer = c->attr.pointer;
    9359         3494 :                     dimension = c->attr.dimension;
    9360         3494 :                     codimension = c->attr.codimension;
    9361         3494 :                     is_abstract = c->attr.abstract;
    9362              :                   }
    9363              :                 break;
    9364              : 
    9365            0 :               case REF_SUBSTRING:
    9366            0 :               case REF_INQUIRY:
    9367            0 :                 allocatable = 0;
    9368            0 :                 pointer = 0;
    9369            0 :                 break;
    9370              :             }
    9371              :         }
    9372              :     }
    9373              : 
    9374              :   /* Check for F08:C628 (F2018:C932).  Each allocate-object shall be a data
    9375              :      pointer or an allocatable variable.  */
    9376        17305 :   if (allocatable == 0 && pointer == 0)
    9377              :     {
    9378            4 :       gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
    9379              :                  &e->where);
    9380            4 :       goto failure;
    9381              :     }
    9382              : 
    9383              :   /* Some checks for the SOURCE tag.  */
    9384        17301 :   if (code->expr3)
    9385              :     {
    9386              :       /* Check F03:C632: "The source-expr shall be a scalar or have the same
    9387              :          rank as allocate-object".  This would require the MOLD argument to
    9388              :          NULL() as source-expr for subsequent checking.  However, even the
    9389              :          resulting disassociated pointer or unallocated array has no shape that
    9390              :          could be used for SOURCE= or MOLD=.  */
    9391         3849 :       if (code->expr3->expr_type == EXPR_NULL)
    9392              :         {
    9393            4 :           gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
    9394              :                      &code->expr3->where);
    9395            4 :           goto failure;
    9396              :         }
    9397              : 
    9398              :       /* Check F03:C631.  */
    9399         3845 :       if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
    9400              :         {
    9401           10 :           gfc_error ("Type of entity at %L is type incompatible with "
    9402           10 :                      "source-expr at %L", &e->where, &code->expr3->where);
    9403           10 :           goto failure;
    9404              :         }
    9405              : 
    9406              :       /* Check F03:C632 and restriction following Note 6.18.  */
    9407         3835 :       if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
    9408            7 :         goto failure;
    9409              : 
    9410              :       /* Check F03:C633.  */
    9411         3828 :       if (code->expr3->ts.kind != e->ts.kind && !unlimited)
    9412              :         {
    9413            1 :           gfc_error ("The allocate-object at %L and the source-expr at %L "
    9414              :                      "shall have the same kind type parameter",
    9415              :                      &e->where, &code->expr3->where);
    9416            1 :           goto failure;
    9417              :         }
    9418              : 
    9419              :       /* Check F2008, C642.  */
    9420         3827 :       if (code->expr3->ts.type == BT_DERIVED
    9421         3827 :           && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
    9422         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9423              :                      == INTMOD_ISO_FORTRAN_ENV
    9424            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9425              :                      == ISOFORTRAN_LOCK_TYPE)))
    9426              :         {
    9427            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9428              :                      "LOCK_TYPE nor have a LOCK_TYPE component if "
    9429              :                       "allocate-object at %L is a coarray",
    9430            0 :                       &code->expr3->where, &e->where);
    9431            0 :           goto failure;
    9432              :         }
    9433              : 
    9434              :       /* Check F2008:C639: "Corresponding kind type parameters of
    9435              :          allocate-object and source-expr shall have the same values."  */
    9436         3827 :       if (e->ts.type == BT_CHARACTER
    9437          816 :           && !e->ts.deferred
    9438          162 :           && e->ts.u.cl->length
    9439          162 :           && code->expr3->ts.type == BT_CHARACTER
    9440         3989 :           && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
    9441              :                                      "SOURCE= or MOLD= specifier"))
    9442           17 :             goto failure;
    9443              : 
    9444              :       /* Check TS18508, C702/C703.  */
    9445         3810 :       if (code->expr3->ts.type == BT_DERIVED
    9446         5002 :           && ((codimension && gfc_expr_attr (code->expr3).event_comp)
    9447         1192 :               || (code->expr3->ts.u.derived->from_intmod
    9448              :                      == INTMOD_ISO_FORTRAN_ENV
    9449            0 :                   && code->expr3->ts.u.derived->intmod_sym_id
    9450              :                      == ISOFORTRAN_EVENT_TYPE)))
    9451              :         {
    9452            0 :           gfc_error ("The source-expr at %L shall neither be of type "
    9453              :                      "EVENT_TYPE nor have a EVENT_TYPE component if "
    9454              :                       "allocate-object at %L is a coarray",
    9455            0 :                       &code->expr3->where, &e->where);
    9456            0 :           goto failure;
    9457              :         }
    9458              :     }
    9459              : 
    9460              :   /* Check F08:C629.  */
    9461        17262 :   if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
    9462          153 :       && !code->expr3)
    9463              :     {
    9464            2 :       gcc_assert (e->ts.type == BT_CLASS);
    9465            2 :       gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
    9466              :                  "type-spec or source-expr", sym->name, &e->where);
    9467            2 :       goto failure;
    9468              :     }
    9469              : 
    9470              :   /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
    9471              :      if and only if each allocate-object is a dummy argument for which the
    9472              :      corresponding type parameter is assumed.  */
    9473        17260 :   if (code->ext.alloc.ts.type == BT_CHARACTER
    9474          513 :       && code->ext.alloc.ts.u.cl->length != NULL
    9475          498 :       && e->ts.type == BT_CHARACTER && !e->ts.deferred
    9476           23 :       && e->ts.u.cl->length == NULL
    9477            2 :       && e->symtree->n.sym->attr.dummy)
    9478              :     {
    9479            2 :       gfc_error ("The type parameter in ALLOCATE statement with type-spec "
    9480              :                  "shall be an asterisk as allocate object %qs at %L is a "
    9481              :                  "dummy argument with assumed type parameter",
    9482              :                  sym->name, &e->where);
    9483            2 :       goto failure;
    9484              :     }
    9485              : 
    9486              :   /* Check F08:C632.  */
    9487        17258 :   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
    9488           60 :       && !UNLIMITED_POLY (e))
    9489              :     {
    9490           36 :       int cmp;
    9491              : 
    9492           36 :       if (!e->ts.u.cl->length)
    9493           15 :         goto failure;
    9494              : 
    9495           42 :       cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
    9496           21 :                                   code->ext.alloc.ts.u.cl->length);
    9497           21 :       if (cmp == 1 || cmp == -1 || cmp == -3)
    9498              :         {
    9499            2 :           gfc_error ("Allocating %s at %L with type-spec requires the same "
    9500              :                      "character-length parameter as in the declaration",
    9501              :                      sym->name, &e->where);
    9502            2 :           goto failure;
    9503              :         }
    9504              :     }
    9505              : 
    9506              :   /* In the variable definition context checks, gfc_expr_attr is used
    9507              :      on the expression.  This is fooled by the array specification
    9508              :      present in e, thus we have to eliminate that one temporarily.  */
    9509        17241 :   e2 = remove_last_array_ref (e);
    9510        17241 :   t = true;
    9511        17241 :   if (t && pointer)
    9512         3869 :     t = gfc_check_vardef_context (e2, true, true, false,
    9513         3869 :                                   _("ALLOCATE object"));
    9514         3869 :   if (t)
    9515        17233 :     t = gfc_check_vardef_context (e2, false, true, false,
    9516        17233 :                                   _("ALLOCATE object"));
    9517        17241 :   gfc_free_expr (e2);
    9518        17241 :   if (!t)
    9519           11 :     goto failure;
    9520              : 
    9521        17230 :   code->ext.alloc.expr3_not_explicit = 0;
    9522        17230 :   if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
    9523         1611 :         && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
    9524              :     {
    9525              :       /* For class arrays, the initialization with SOURCE is done
    9526              :          using _copy and trans_call. It is convenient to exploit that
    9527              :          when the allocated type is different from the declared type but
    9528              :          no SOURCE exists by setting expr3.  */
    9529          299 :       code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
    9530          299 :       code->ext.alloc.expr3_not_explicit = 1;
    9531              :     }
    9532        16931 :   else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
    9533         2628 :            && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9534            6 :            && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9535              :     {
    9536              :       /* We have to zero initialize the integer variable.  */
    9537            2 :       code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
    9538            2 :       code->ext.alloc.expr3_not_explicit = 1;
    9539              :     }
    9540              : 
    9541        17230 :   if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
    9542              :     {
    9543              :       /* Make sure the vtab symbol is present when
    9544              :          the module variables are generated.  */
    9545         2972 :       gfc_typespec ts = e->ts;
    9546         2972 :       if (code->expr3)
    9547         1325 :         ts = code->expr3->ts;
    9548         1647 :       else if (code->ext.alloc.ts.type == BT_DERIVED)
    9549          714 :         ts = code->ext.alloc.ts;
    9550              : 
    9551              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9552              :          statement is necessary.  */
    9553         2972 :       gfc_find_derived_vtab (ts.u.derived);
    9554         2972 :     }
    9555        14258 :   else if (unlimited && !UNLIMITED_POLY (code->expr3))
    9556              :     {
    9557              :       /* Again, make sure the vtab symbol is present when
    9558              :          the module variables are generated.  */
    9559          434 :       gfc_typespec *ts = NULL;
    9560          434 :       if (code->expr3)
    9561          347 :         ts = &code->expr3->ts;
    9562              :       else
    9563           87 :         ts = &code->ext.alloc.ts;
    9564              : 
    9565          434 :       gcc_assert (ts);
    9566              : 
    9567              :       /* Finding the vtab also publishes the type's symbol.  Therefore this
    9568              :          statement is necessary.  */
    9569          434 :       gfc_find_vtab (ts);
    9570              :     }
    9571              : 
    9572        17230 :   if (dimension == 0 && codimension == 0)
    9573         5308 :     goto success;
    9574              : 
    9575              :   /* Make sure the last reference node is an array specification.  */
    9576              : 
    9577        11922 :   if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
    9578        10690 :       || (dimension && ref2->u.ar.dimen == 0))
    9579              :     {
    9580              :       /* F08:C633.  */
    9581         1232 :       if (code->expr3)
    9582              :         {
    9583         1231 :           if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
    9584              :                                "in ALLOCATE statement at %L", &e->where))
    9585            0 :             goto failure;
    9586         1231 :           if (code->expr3->rank != 0)
    9587         1230 :             *array_alloc_wo_spec = true;
    9588              :           else
    9589              :             {
    9590            1 :               gfc_error ("Array specification or array-valued SOURCE= "
    9591              :                          "expression required in ALLOCATE statement at %L",
    9592              :                          &e->where);
    9593            1 :               goto failure;
    9594              :             }
    9595              :         }
    9596              :       else
    9597              :         {
    9598            1 :           gfc_error ("Array specification required in ALLOCATE statement "
    9599              :                      "at %L", &e->where);
    9600            1 :           goto failure;
    9601              :         }
    9602              :     }
    9603              : 
    9604              :   /* Make sure that the array section reference makes sense in the
    9605              :      context of an ALLOCATE specification.  */
    9606              : 
    9607        11920 :   ar = &ref2->u.ar;
    9608              : 
    9609        11920 :   if (codimension)
    9610         1179 :     for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
    9611              :       {
    9612          692 :         switch (ar->dimen_type[i])
    9613              :           {
    9614            2 :           case DIMEN_THIS_IMAGE:
    9615            2 :             gfc_error ("Coarray specification required in ALLOCATE statement "
    9616              :                        "at %L", &e->where);
    9617            2 :             goto failure;
    9618              : 
    9619           98 :           case  DIMEN_RANGE:
    9620              :             /* F2018:R937:
    9621              :              * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
    9622              :              */
    9623           98 :             if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
    9624              :               {
    9625            8 :                 gfc_error ("Bad coarray specification in ALLOCATE statement "
    9626              :                            "at %L", &e->where);
    9627            8 :                 goto failure;
    9628              :               }
    9629           90 :             else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
    9630              :               {
    9631            2 :                 gfc_error ("Upper cobound is less than lower cobound at %L",
    9632            2 :                            &ar->start[i]->where);
    9633            2 :                 goto failure;
    9634              :               }
    9635              :             break;
    9636              : 
    9637          105 :           case DIMEN_ELEMENT:
    9638          105 :             if (ar->start[i]->expr_type == EXPR_CONSTANT)
    9639              :               {
    9640           97 :                 gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
    9641           97 :                 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
    9642              :                   {
    9643            1 :                     gfc_error ("Upper cobound is less than lower cobound "
    9644              :                                "of 1 at %L", &ar->start[i]->where);
    9645            1 :                     goto failure;
    9646              :                   }
    9647              :               }
    9648              :             break;
    9649              : 
    9650              :           case DIMEN_STAR:
    9651              :             break;
    9652              : 
    9653            0 :           default:
    9654            0 :             gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9655              :                        &e->where);
    9656            0 :             goto failure;
    9657              : 
    9658              :           }
    9659              :       }
    9660        29202 :   for (i = 0; i < ar->dimen; i++)
    9661              :     {
    9662        17299 :       if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
    9663        14589 :         goto check_symbols;
    9664              : 
    9665         2710 :       switch (ar->dimen_type[i])
    9666              :         {
    9667              :         case DIMEN_ELEMENT:
    9668              :           break;
    9669              : 
    9670         2444 :         case DIMEN_RANGE:
    9671         2444 :           if (ar->start[i] != NULL
    9672         2444 :               && ar->end[i] != NULL
    9673         2443 :               && ar->stride[i] == NULL)
    9674              :             break;
    9675              : 
    9676              :           /* Fall through.  */
    9677              : 
    9678            1 :         case DIMEN_UNKNOWN:
    9679            1 :         case DIMEN_VECTOR:
    9680            1 :         case DIMEN_STAR:
    9681            1 :         case DIMEN_THIS_IMAGE:
    9682            1 :           gfc_error ("Bad array specification in ALLOCATE statement at %L",
    9683              :                      &e->where);
    9684            1 :           goto failure;
    9685              :         }
    9686              : 
    9687         2443 : check_symbols:
    9688        45019 :       for (a = code->ext.alloc.list; a; a = a->next)
    9689              :         {
    9690        27724 :           sym = a->expr->symtree->n.sym;
    9691              : 
    9692              :           /* TODO - check derived type components.  */
    9693        27724 :           if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
    9694         9309 :             continue;
    9695              : 
    9696        18415 :           if ((ar->start[i] != NULL
    9697        17735 :                && gfc_find_var_in_expr (sym, ar->start[i]))
    9698        36147 :               || (ar->end[i] != NULL
    9699         2722 :                   && gfc_find_var_in_expr (sym, ar->end[i])))
    9700              :             {
    9701            3 :               gfc_error ("%qs must not appear in the array specification at "
    9702              :                          "%L in the same ALLOCATE statement where it is "
    9703              :                          "itself allocated", sym->name, &ar->where);
    9704            3 :               goto failure;
    9705              :             }
    9706              :         }
    9707              :     }
    9708              : 
    9709        12094 :   for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
    9710              :     {
    9711          868 :       if (ar->dimen_type[i] == DIMEN_ELEMENT
    9712          677 :           || ar->dimen_type[i] == DIMEN_RANGE)
    9713              :         {
    9714          191 :           if (i == (ar->dimen + ar->codimen - 1))
    9715              :             {
    9716            0 :               gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
    9717              :                          "statement at %L", &e->where);
    9718            0 :               goto failure;
    9719              :             }
    9720          191 :           continue;
    9721              :         }
    9722              : 
    9723          486 :       if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
    9724          486 :           && ar->stride[i] == NULL)
    9725              :         break;
    9726              : 
    9727            0 :       gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
    9728              :                  &e->where);
    9729            0 :       goto failure;
    9730              :     }
    9731              : 
    9732        11903 : success:
    9733              :   return true;
    9734              : 
    9735              : failure:
    9736              :   return false;
    9737              : }
    9738              : 
    9739              : 
    9740              : static void
    9741        20355 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
    9742              : {
    9743        20355 :   gfc_expr *stat, *errmsg, *pe, *qe;
    9744        20355 :   gfc_alloc *a, *p, *q;
    9745              : 
    9746        20355 :   stat = code->expr1;
    9747        20355 :   errmsg = code->expr2;
    9748              : 
    9749              :   /* Check the stat variable.  */
    9750        20355 :   if (stat)
    9751              :     {
    9752          661 :       if (!gfc_check_vardef_context (stat, false, false, false,
    9753          661 :                                      _("STAT variable")))
    9754            8 :           goto done_stat;
    9755              : 
    9756          653 :       if (stat->ts.type != BT_INTEGER
    9757          644 :           || stat->rank > 0)
    9758           11 :         gfc_error ("Stat-variable at %L must be a scalar INTEGER "
    9759              :                    "variable", &stat->where);
    9760              : 
    9761          653 :       if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
    9762            0 :         goto done_stat;
    9763              : 
    9764              :       /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
    9765              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9766              :        */
    9767         1354 :       for (p = code->ext.alloc.list; p; p = p->next)
    9768          708 :         if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
    9769              :           {
    9770            9 :             gfc_ref *ref1, *ref2;
    9771            9 :             bool found = true;
    9772              : 
    9773           16 :             for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
    9774            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9775              :               {
    9776            9 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9777            5 :                   continue;
    9778            4 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9779              :                   {
    9780              :                     found = false;
    9781              :                     break;
    9782              :                   }
    9783              :               }
    9784              : 
    9785            9 :             if (found)
    9786              :               {
    9787            7 :                 gfc_error ("Stat-variable at %L shall not be %sd within "
    9788              :                            "the same %s statement", &stat->where, fcn, fcn);
    9789            7 :                 break;
    9790              :               }
    9791              :           }
    9792              :     }
    9793              : 
    9794        19694 : done_stat:
    9795              : 
    9796              :   /* Check the errmsg variable.  */
    9797        20355 :   if (errmsg)
    9798              :     {
    9799          150 :       if (!stat)
    9800            2 :         gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
    9801              :                      &errmsg->where);
    9802              : 
    9803          150 :       if (!gfc_check_vardef_context (errmsg, false, false, false,
    9804          150 :                                      _("ERRMSG variable")))
    9805            6 :           goto done_errmsg;
    9806              : 
    9807              :       /* F18:R928  alloc-opt             is ERRMSG = errmsg-variable
    9808              :          F18:R930  errmsg-variable       is scalar-default-char-variable
    9809              :          F18:R906  default-char-variable is variable
    9810              :          F18:C906  default-char-variable shall be default character.  */
    9811          144 :       if (errmsg->ts.type != BT_CHARACTER
    9812          142 :           || errmsg->rank > 0
    9813          141 :           || errmsg->ts.kind != gfc_default_character_kind)
    9814            4 :         gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
    9815              :                    "variable", &errmsg->where);
    9816              : 
    9817          144 :       if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
    9818            0 :         goto done_errmsg;
    9819              : 
    9820              :       /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
    9821              :        * within the ALLOCATE or DEALLOCATE statement in which it appears ...
    9822              :        */
    9823          286 :       for (p = code->ext.alloc.list; p; p = p->next)
    9824          147 :         if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
    9825              :           {
    9826            9 :             gfc_ref *ref1, *ref2;
    9827            9 :             bool found = true;
    9828              : 
    9829           16 :             for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
    9830            7 :                  ref1 = ref1->next, ref2 = ref2->next)
    9831              :               {
    9832           11 :                 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
    9833            4 :                   continue;
    9834            7 :                 if (ref1->u.c.component->name != ref2->u.c.component->name)
    9835              :                   {
    9836              :                     found = false;
    9837              :                     break;
    9838              :                   }
    9839              :               }
    9840              : 
    9841            9 :             if (found)
    9842              :               {
    9843            5 :                 gfc_error ("Errmsg-variable at %L shall not be %sd within "
    9844              :                            "the same %s statement", &errmsg->where, fcn, fcn);
    9845            5 :                 break;
    9846              :               }
    9847              :           }
    9848              :     }
    9849              : 
    9850        20205 : done_errmsg:
    9851              : 
    9852              :   /* Check that an allocate-object appears only once in the statement.  */
    9853              : 
    9854        46005 :   for (p = code->ext.alloc.list; p; p = p->next)
    9855              :     {
    9856        25650 :       pe = p->expr;
    9857        34922 :       for (q = p->next; q; q = q->next)
    9858              :         {
    9859         9272 :           qe = q->expr;
    9860         9272 :           if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
    9861              :             {
    9862              :               /* This is a potential collision.  */
    9863         2093 :               gfc_ref *pr = pe->ref;
    9864         2093 :               gfc_ref *qr = qe->ref;
    9865              : 
    9866              :               /* Follow the references  until
    9867              :                  a) They start to differ, in which case there is no error;
    9868              :                  you can deallocate a%b and a%c in a single statement
    9869              :                  b) Both of them stop, which is an error
    9870              :                  c) One of them stops, which is also an error.  */
    9871         4517 :               while (1)
    9872              :                 {
    9873         3305 :                   if (pr == NULL && qr == NULL)
    9874              :                     {
    9875            7 :                       gfc_error ("Allocate-object at %L also appears at %L",
    9876              :                                  &pe->where, &qe->where);
    9877            7 :                       break;
    9878              :                     }
    9879         3298 :                   else if (pr != NULL && qr == NULL)
    9880              :                     {
    9881            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9882              :                                  " object at %L", &pe->where, &qe->where);
    9883            2 :                       break;
    9884              :                     }
    9885         3296 :                   else if (pr == NULL && qr != NULL)
    9886              :                     {
    9887            2 :                       gfc_error ("Allocate-object at %L is subobject of"
    9888              :                                  " object at %L", &qe->where, &pe->where);
    9889            2 :                       break;
    9890              :                     }
    9891              :                   /* Here, pr != NULL && qr != NULL  */
    9892         3294 :                   gcc_assert(pr->type == qr->type);
    9893         3294 :                   if (pr->type == REF_ARRAY)
    9894              :                     {
    9895              :                       /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
    9896              :                          which are legal.  */
    9897         1065 :                       gcc_assert (qr->type == REF_ARRAY);
    9898              : 
    9899         1065 :                       if (pr->next && qr->next)
    9900              :                         {
    9901              :                           int i;
    9902              :                           gfc_array_ref *par = &(pr->u.ar);
    9903              :                           gfc_array_ref *qar = &(qr->u.ar);
    9904              : 
    9905         1840 :                           for (i=0; i<par->dimen; i++)
    9906              :                             {
    9907          954 :                               if ((par->start[i] != NULL
    9908            0 :                                    || qar->start[i] != NULL)
    9909         1908 :                                   && gfc_dep_compare_expr (par->start[i],
    9910          954 :                                                            qar->start[i]) != 0)
    9911          168 :                                 goto break_label;
    9912              :                             }
    9913              :                         }
    9914              :                     }
    9915              :                   else
    9916              :                     {
    9917         2229 :                       if (pr->u.c.component->name != qr->u.c.component->name)
    9918              :                         break;
    9919              :                     }
    9920              : 
    9921         1212 :                   pr = pr->next;
    9922         1212 :                   qr = qr->next;
    9923         1212 :                 }
    9924         9272 :             break_label:
    9925              :               ;
    9926              :             }
    9927              :         }
    9928              :     }
    9929              : 
    9930        20355 :   if (strcmp (fcn, "ALLOCATE") == 0)
    9931              :     {
    9932        14281 :       bool arr_alloc_wo_spec = false;
    9933              : 
    9934              :       /* Resolving the expr3 in the loop over all objects to allocate would
    9935              :          execute loop invariant code for each loop item.  Therefore do it just
    9936              :          once here.  */
    9937        14281 :       if (code->expr3 && code->expr3->mold
    9938          350 :           && code->expr3->ts.type == BT_DERIVED
    9939           24 :           && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
    9940              :         {
    9941              :           /* Default initialization via MOLD (non-polymorphic).  */
    9942           22 :           gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
    9943           22 :           if (rhs != NULL)
    9944              :             {
    9945            9 :               gfc_resolve_expr (rhs);
    9946            9 :               gfc_free_expr (code->expr3);
    9947            9 :               code->expr3 = rhs;
    9948              :             }
    9949              :         }
    9950        31589 :       for (a = code->ext.alloc.list; a; a = a->next)
    9951        17308 :         resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
    9952              : 
    9953        14281 :       if (arr_alloc_wo_spec && code->expr3)
    9954              :         {
    9955              :           /* Mark the allocate to have to take the array specification
    9956              :              from the expr3.  */
    9957         1224 :           code->ext.alloc.arr_spec_from_expr3 = 1;
    9958              :         }
    9959              :     }
    9960              :   else
    9961              :     {
    9962        14416 :       for (a = code->ext.alloc.list; a; a = a->next)
    9963         8342 :         resolve_deallocate_expr (a->expr);
    9964              :     }
    9965        20355 : }
    9966              : 
    9967              : 
    9968              : /************ SELECT CASE resolution subroutines ************/
    9969              : 
    9970              : /* Callback function for our mergesort variant.  Determines interval
    9971              :    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
    9972              :    op1 > op2.  Assumes we're not dealing with the default case.
    9973              :    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
    9974              :    There are nine situations to check.  */
    9975              : 
    9976              : static int
    9977         1578 : compare_cases (const gfc_case *op1, const gfc_case *op2)
    9978              : {
    9979         1578 :   int retval;
    9980              : 
    9981         1578 :   if (op1->low == NULL) /* op1 = (:L)  */
    9982              :     {
    9983              :       /* op2 = (:N), so overlap.  */
    9984           52 :       retval = 0;
    9985              :       /* op2 = (M:) or (M:N),  L < M  */
    9986           52 :       if (op2->low != NULL
    9987           52 :           && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
    9988              :         retval = -1;
    9989              :     }
    9990         1526 :   else if (op1->high == NULL) /* op1 = (K:)  */
    9991              :     {
    9992              :       /* op2 = (M:), so overlap.  */
    9993           10 :       retval = 0;
    9994              :       /* op2 = (:N) or (M:N), K > N  */
    9995           10 :       if (op2->high != NULL
    9996           10 :           && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
    9997              :         retval = 1;
    9998              :     }
    9999              :   else /* op1 = (K:L)  */
   10000              :     {
   10001         1516 :       if (op2->low == NULL)       /* op2 = (:N), K > N  */
   10002           18 :         retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10003           18 :                  ? 1 : 0;
   10004         1498 :       else if (op2->high == NULL) /* op2 = (M:), L < M  */
   10005           14 :         retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10006           10 :                  ? -1 : 0;
   10007              :       else                      /* op2 = (M:N)  */
   10008              :         {
   10009         1488 :           retval =  0;
   10010              :           /* L < M  */
   10011         1488 :           if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
   10012              :             retval =  -1;
   10013              :           /* K > N  */
   10014          412 :           else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
   10015          438 :             retval =  1;
   10016              :         }
   10017              :     }
   10018              : 
   10019         1578 :   return retval;
   10020              : }
   10021              : 
   10022              : 
   10023              : /* Merge-sort a double linked case list, detecting overlap in the
   10024              :    process.  LIST is the head of the double linked case list before it
   10025              :    is sorted.  Returns the head of the sorted list if we don't see any
   10026              :    overlap, or NULL otherwise.  */
   10027              : 
   10028              : static gfc_case *
   10029          646 : check_case_overlap (gfc_case *list)
   10030              : {
   10031          646 :   gfc_case *p, *q, *e, *tail;
   10032          646 :   int insize, nmerges, psize, qsize, cmp, overlap_seen;
   10033              : 
   10034              :   /* If the passed list was empty, return immediately.  */
   10035          646 :   if (!list)
   10036              :     return NULL;
   10037              : 
   10038              :   overlap_seen = 0;
   10039              :   insize = 1;
   10040              : 
   10041              :   /* Loop unconditionally.  The only exit from this loop is a return
   10042              :      statement, when we've finished sorting the case list.  */
   10043         1350 :   for (;;)
   10044              :     {
   10045          998 :       p = list;
   10046          998 :       list = NULL;
   10047          998 :       tail = NULL;
   10048              : 
   10049              :       /* Count the number of merges we do in this pass.  */
   10050          998 :       nmerges = 0;
   10051              : 
   10052              :       /* Loop while there exists a merge to be done.  */
   10053         2523 :       while (p)
   10054              :         {
   10055         1525 :           int i;
   10056              : 
   10057              :           /* Count this merge.  */
   10058         1525 :           nmerges++;
   10059              : 
   10060              :           /* Cut the list in two pieces by stepping INSIZE places
   10061              :              forward in the list, starting from P.  */
   10062         1525 :           psize = 0;
   10063         1525 :           q = p;
   10064         3208 :           for (i = 0; i < insize; i++)
   10065              :             {
   10066         2243 :               psize++;
   10067         2243 :               q = q->right;
   10068         2243 :               if (!q)
   10069              :                 break;
   10070              :             }
   10071              :           qsize = insize;
   10072              : 
   10073              :           /* Now we have two lists.  Merge them!  */
   10074         5013 :           while (psize > 0 || (qsize > 0 && q != NULL))
   10075              :             {
   10076              :               /* See from which the next case to merge comes from.  */
   10077          807 :               if (psize == 0)
   10078              :                 {
   10079              :                   /* P is empty so the next case must come from Q.  */
   10080          807 :                   e = q;
   10081          807 :                   q = q->right;
   10082          807 :                   qsize--;
   10083              :                 }
   10084         2681 :               else if (qsize == 0 || q == NULL)
   10085              :                 {
   10086              :                   /* Q is empty.  */
   10087         1103 :                   e = p;
   10088         1103 :                   p = p->right;
   10089         1103 :                   psize--;
   10090              :                 }
   10091              :               else
   10092              :                 {
   10093         1578 :                   cmp = compare_cases (p, q);
   10094         1578 :                   if (cmp < 0)
   10095              :                     {
   10096              :                       /* The whole case range for P is less than the
   10097              :                          one for Q.  */
   10098         1136 :                       e = p;
   10099         1136 :                       p = p->right;
   10100         1136 :                       psize--;
   10101              :                     }
   10102          442 :                   else if (cmp > 0)
   10103              :                     {
   10104              :                       /* The whole case range for Q is greater than
   10105              :                          the case range for P.  */
   10106          438 :                       e = q;
   10107          438 :                       q = q->right;
   10108          438 :                       qsize--;
   10109              :                     }
   10110              :                   else
   10111              :                     {
   10112              :                       /* The cases overlap, or they are the same
   10113              :                          element in the list.  Either way, we must
   10114              :                          issue an error and get the next case from P.  */
   10115              :                       /* FIXME: Sort P and Q by line number.  */
   10116            4 :                       gfc_error ("CASE label at %L overlaps with CASE "
   10117              :                                  "label at %L", &p->where, &q->where);
   10118            4 :                       overlap_seen = 1;
   10119            4 :                       e = p;
   10120            4 :                       p = p->right;
   10121            4 :                       psize--;
   10122              :                     }
   10123              :                 }
   10124              : 
   10125              :                 /* Add the next element to the merged list.  */
   10126         3488 :               if (tail)
   10127         2490 :                 tail->right = e;
   10128              :               else
   10129              :                 list = e;
   10130         3488 :               e->left = tail;
   10131         3488 :               tail = e;
   10132              :             }
   10133              : 
   10134              :           /* P has now stepped INSIZE places along, and so has Q.  So
   10135              :              they're the same.  */
   10136              :           p = q;
   10137              :         }
   10138          998 :       tail->right = NULL;
   10139              : 
   10140              :       /* If we have done only one merge or none at all, we've
   10141              :          finished sorting the cases.  */
   10142          998 :       if (nmerges <= 1)
   10143              :         {
   10144          646 :           if (!overlap_seen)
   10145              :             return list;
   10146              :           else
   10147              :             return NULL;
   10148              :         }
   10149              : 
   10150              :       /* Otherwise repeat, merging lists twice the size.  */
   10151          352 :       insize *= 2;
   10152          352 :     }
   10153              : }
   10154              : 
   10155              : 
   10156              : /* Check to see if an expression is suitable for use in a CASE statement.
   10157              :    Makes sure that all case expressions are scalar constants of the same
   10158              :    type.  Return false if anything is wrong.  */
   10159              : 
   10160              : static bool
   10161         3307 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
   10162              : {
   10163         3307 :   if (e == NULL) return true;
   10164              : 
   10165         3214 :   if (e->ts.type != case_expr->ts.type)
   10166              :     {
   10167            4 :       gfc_error ("Expression in CASE statement at %L must be of type %s",
   10168              :                  &e->where, gfc_basic_typename (case_expr->ts.type));
   10169            4 :       return false;
   10170              :     }
   10171              : 
   10172              :   /* C805 (R808) For a given case-construct, each case-value shall be of
   10173              :      the same type as case-expr.  For character type, length differences
   10174              :      are allowed, but the kind type parameters shall be the same.  */
   10175              : 
   10176         3210 :   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
   10177              :     {
   10178            4 :       gfc_error ("Expression in CASE statement at %L must be of kind %d",
   10179              :                  &e->where, case_expr->ts.kind);
   10180            4 :       return false;
   10181              :     }
   10182              : 
   10183              :   /* Convert the case value kind to that of case expression kind,
   10184              :      if needed */
   10185              : 
   10186         3206 :   if (e->ts.kind != case_expr->ts.kind)
   10187           14 :     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
   10188              : 
   10189         3206 :   if (e->rank != 0)
   10190              :     {
   10191            0 :       gfc_error ("Expression in CASE statement at %L must be scalar",
   10192              :                  &e->where);
   10193            0 :       return false;
   10194              :     }
   10195              : 
   10196              :   return true;
   10197              : }
   10198              : 
   10199              : 
   10200              : /* Given a completely parsed select statement, we:
   10201              : 
   10202              :      - Validate all expressions and code within the SELECT.
   10203              :      - Make sure that the selection expression is not of the wrong type.
   10204              :      - Make sure that no case ranges overlap.
   10205              :      - Eliminate unreachable cases and unreachable code resulting from
   10206              :        removing case labels.
   10207              : 
   10208              :    The standard does allow unreachable cases, e.g. CASE (5:3).  But
   10209              :    they are a hassle for code generation, and to prevent that, we just
   10210              :    cut them out here.  This is not necessary for overlapping cases
   10211              :    because they are illegal and we never even try to generate code.
   10212              : 
   10213              :    We have the additional caveat that a SELECT construct could have
   10214              :    been a computed GOTO in the source code. Fortunately we can fairly
   10215              :    easily work around that here: The case_expr for a "real" SELECT CASE
   10216              :    is in code->expr1, but for a computed GOTO it is in code->expr2. All
   10217              :    we have to do is make sure that the case_expr is a scalar integer
   10218              :    expression.  */
   10219              : 
   10220              : static void
   10221          687 : resolve_select (gfc_code *code, bool select_type)
   10222              : {
   10223          687 :   gfc_code *body;
   10224          687 :   gfc_expr *case_expr;
   10225          687 :   gfc_case *cp, *default_case, *tail, *head;
   10226          687 :   int seen_unreachable;
   10227          687 :   int seen_logical;
   10228          687 :   int ncases;
   10229          687 :   bt type;
   10230          687 :   bool t;
   10231              : 
   10232          687 :   if (code->expr1 == NULL)
   10233              :     {
   10234              :       /* This was actually a computed GOTO statement.  */
   10235            5 :       case_expr = code->expr2;
   10236            5 :       if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
   10237            3 :         gfc_error ("Selection expression in computed GOTO statement "
   10238              :                    "at %L must be a scalar integer expression",
   10239              :                    &case_expr->where);
   10240              : 
   10241              :       /* Further checking is not necessary because this SELECT was built
   10242              :          by the compiler, so it should always be OK.  Just move the
   10243              :          case_expr from expr2 to expr so that we can handle computed
   10244              :          GOTOs as normal SELECTs from here on.  */
   10245            5 :       code->expr1 = code->expr2;
   10246            5 :       code->expr2 = NULL;
   10247            5 :       return;
   10248              :     }
   10249              : 
   10250          682 :   case_expr = code->expr1;
   10251          682 :   type = case_expr->ts.type;
   10252              : 
   10253              :   /* F08:C830.  */
   10254          682 :   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
   10255            6 :       && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
   10256              : 
   10257              :     {
   10258            0 :       gfc_error ("Argument of SELECT statement at %L cannot be %s",
   10259              :                  &case_expr->where, gfc_typename (case_expr));
   10260              : 
   10261              :       /* Punt. Going on here just produce more garbage error messages.  */
   10262            0 :       return;
   10263              :     }
   10264              : 
   10265              :   /* F08:R842.  */
   10266          682 :   if (!select_type && case_expr->rank != 0)
   10267              :     {
   10268            1 :       gfc_error ("Argument of SELECT statement at %L must be a scalar "
   10269              :                  "expression", &case_expr->where);
   10270              : 
   10271              :       /* Punt.  */
   10272            1 :       return;
   10273              :     }
   10274              : 
   10275              :   /* Raise a warning if an INTEGER case value exceeds the range of
   10276              :      the case-expr. Later, all expressions will be promoted to the
   10277              :      largest kind of all case-labels.  */
   10278              : 
   10279          681 :   if (type == BT_INTEGER)
   10280         1927 :     for (body = code->block; body; body = body->block)
   10281         2852 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10282              :         {
   10283         1462 :           if (cp->low
   10284         1462 :               && gfc_check_integer_range (cp->low->value.integer,
   10285              :                                           case_expr->ts.kind) != ARITH_OK)
   10286            6 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10287            6 :                          "not in the range of %s", &cp->low->where,
   10288              :                          gfc_typename (case_expr));
   10289              : 
   10290         1462 :           if (cp->high
   10291         1178 :               && cp->low != cp->high
   10292         1570 :               && gfc_check_integer_range (cp->high->value.integer,
   10293              :                                           case_expr->ts.kind) != ARITH_OK)
   10294            0 :             gfc_warning (0, "Expression in CASE statement at %L is "
   10295            0 :                          "not in the range of %s", &cp->high->where,
   10296              :                          gfc_typename (case_expr));
   10297              :         }
   10298              : 
   10299              :   /* PR 19168 has a long discussion concerning a mismatch of the kinds
   10300              :      of the SELECT CASE expression and its CASE values.  Walk the lists
   10301              :      of case values, and if we find a mismatch, promote case_expr to
   10302              :      the appropriate kind.  */
   10303              : 
   10304          681 :   if (type == BT_LOGICAL || type == BT_INTEGER)
   10305              :     {
   10306         2113 :       for (body = code->block; body; body = body->block)
   10307              :         {
   10308              :           /* Walk the case label list.  */
   10309         3113 :           for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10310              :             {
   10311              :               /* Intercept the DEFAULT case.  It does not have a kind.  */
   10312         1597 :               if (cp->low == NULL && cp->high == NULL)
   10313          292 :                 continue;
   10314              : 
   10315              :               /* Unreachable case ranges are discarded, so ignore.  */
   10316         1260 :               if (cp->low != NULL && cp->high != NULL
   10317         1212 :                   && cp->low != cp->high
   10318         1370 :                   && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10319           33 :                 continue;
   10320              : 
   10321         1272 :               if (cp->low != NULL
   10322         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
   10323           17 :                 gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
   10324              : 
   10325         1272 :               if (cp->high != NULL
   10326         1272 :                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
   10327            4 :                 gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
   10328              :             }
   10329              :          }
   10330              :     }
   10331              : 
   10332              :   /* Assume there is no DEFAULT case.  */
   10333          681 :   default_case = NULL;
   10334          681 :   head = tail = NULL;
   10335          681 :   ncases = 0;
   10336          681 :   seen_logical = 0;
   10337              : 
   10338         2502 :   for (body = code->block; body; body = body->block)
   10339              :     {
   10340              :       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
   10341         1821 :       t = true;
   10342         1821 :       seen_unreachable = 0;
   10343              : 
   10344              :       /* Walk the case label list, making sure that all case labels
   10345              :          are legal.  */
   10346         3829 :       for (cp = body->ext.block.case_list; cp; cp = cp->next)
   10347              :         {
   10348              :           /* Count the number of cases in the whole construct.  */
   10349         2019 :           ncases++;
   10350              : 
   10351              :           /* Intercept the DEFAULT case.  */
   10352         2019 :           if (cp->low == NULL && cp->high == NULL)
   10353              :             {
   10354          362 :               if (default_case != NULL)
   10355              :                 {
   10356            0 :                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
   10357              :                              "by a second DEFAULT CASE at %L",
   10358              :                              &default_case->where, &cp->where);
   10359            0 :                   t = false;
   10360            0 :                   break;
   10361              :                 }
   10362              :               else
   10363              :                 {
   10364          362 :                   default_case = cp;
   10365          362 :                   continue;
   10366              :                 }
   10367              :             }
   10368              : 
   10369              :           /* Deal with single value cases and case ranges.  Errors are
   10370              :              issued from the validation function.  */
   10371         1657 :           if (!validate_case_label_expr (cp->low, case_expr)
   10372         1657 :               || !validate_case_label_expr (cp->high, case_expr))
   10373              :             {
   10374              :               t = false;
   10375              :               break;
   10376              :             }
   10377              : 
   10378         1649 :           if (type == BT_LOGICAL
   10379           78 :               && ((cp->low == NULL || cp->high == NULL)
   10380           76 :                   || cp->low != cp->high))
   10381              :             {
   10382            2 :               gfc_error ("Logical range in CASE statement at %L is not "
   10383              :                          "allowed",
   10384            1 :                          cp->low ? &cp->low->where : &cp->high->where);
   10385            2 :               t = false;
   10386            2 :               break;
   10387              :             }
   10388              : 
   10389           76 :           if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
   10390              :             {
   10391           76 :               int value;
   10392           76 :               value = cp->low->value.logical == 0 ? 2 : 1;
   10393           76 :               if (value & seen_logical)
   10394              :                 {
   10395            1 :                   gfc_error ("Constant logical value in CASE statement "
   10396              :                              "is repeated at %L",
   10397              :                              &cp->low->where);
   10398            1 :                   t = false;
   10399            1 :                   break;
   10400              :                 }
   10401           75 :               seen_logical |= value;
   10402              :             }
   10403              : 
   10404         1602 :           if (cp->low != NULL && cp->high != NULL
   10405         1555 :               && cp->low != cp->high
   10406         1758 :               && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
   10407              :             {
   10408           35 :               if (warn_surprising)
   10409            1 :                 gfc_warning (OPT_Wsurprising,
   10410              :                              "Range specification at %L can never be matched",
   10411              :                              &cp->where);
   10412              : 
   10413           35 :               cp->unreachable = 1;
   10414           35 :               seen_unreachable = 1;
   10415              :             }
   10416              :           else
   10417              :             {
   10418              :               /* If the case range can be matched, it can also overlap with
   10419              :                  other cases.  To make sure it does not, we put it in a
   10420              :                  double linked list here.  We sort that with a merge sort
   10421              :                  later on to detect any overlapping cases.  */
   10422         1611 :               if (!head)
   10423              :                 {
   10424          646 :                   head = tail = cp;
   10425          646 :                   head->right = head->left = NULL;
   10426              :                 }
   10427              :               else
   10428              :                 {
   10429          965 :                   tail->right = cp;
   10430          965 :                   tail->right->left = tail;
   10431          965 :                   tail = tail->right;
   10432          965 :                   tail->right = NULL;
   10433              :                 }
   10434              :             }
   10435              :         }
   10436              : 
   10437              :       /* It there was a failure in the previous case label, give up
   10438              :          for this case label list.  Continue with the next block.  */
   10439         1821 :       if (!t)
   10440           11 :         continue;
   10441              : 
   10442              :       /* See if any case labels that are unreachable have been seen.
   10443              :          If so, we eliminate them.  This is a bit of a kludge because
   10444              :          the case lists for a single case statement (label) is a
   10445              :          single forward linked lists.  */
   10446         1810 :       if (seen_unreachable)
   10447              :       {
   10448              :         /* Advance until the first case in the list is reachable.  */
   10449           69 :         while (body->ext.block.case_list != NULL
   10450           69 :                && body->ext.block.case_list->unreachable)
   10451              :           {
   10452           34 :             gfc_case *n = body->ext.block.case_list;
   10453           34 :             body->ext.block.case_list = body->ext.block.case_list->next;
   10454           34 :             n->next = NULL;
   10455           34 :             gfc_free_case_list (n);
   10456              :           }
   10457              : 
   10458              :         /* Strip all other unreachable cases.  */
   10459           35 :         if (body->ext.block.case_list)
   10460              :           {
   10461            2 :             for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
   10462              :               {
   10463            1 :                 if (cp->next->unreachable)
   10464              :                   {
   10465            1 :                     gfc_case *n = cp->next;
   10466            1 :                     cp->next = cp->next->next;
   10467            1 :                     n->next = NULL;
   10468            1 :                     gfc_free_case_list (n);
   10469              :                   }
   10470              :               }
   10471              :           }
   10472              :       }
   10473              :     }
   10474              : 
   10475              :   /* See if there were overlapping cases.  If the check returns NULL,
   10476              :      there was overlap.  In that case we don't do anything.  If head
   10477              :      is non-NULL, we prepend the DEFAULT case.  The sorted list can
   10478              :      then used during code generation for SELECT CASE constructs with
   10479              :      a case expression of a CHARACTER type.  */
   10480          681 :   if (head)
   10481              :     {
   10482          646 :       head = check_case_overlap (head);
   10483              : 
   10484              :       /* Prepend the default_case if it is there.  */
   10485          646 :       if (head != NULL && default_case)
   10486              :         {
   10487          345 :           default_case->left = NULL;
   10488          345 :           default_case->right = head;
   10489          345 :           head->left = default_case;
   10490              :         }
   10491              :     }
   10492              : 
   10493              :   /* Eliminate dead blocks that may be the result if we've seen
   10494              :      unreachable case labels for a block.  */
   10495         2468 :   for (body = code; body && body->block; body = body->block)
   10496              :     {
   10497         1787 :       if (body->block->ext.block.case_list == NULL)
   10498              :         {
   10499              :           /* Cut the unreachable block from the code chain.  */
   10500           34 :           gfc_code *c = body->block;
   10501           34 :           body->block = c->block;
   10502              : 
   10503              :           /* Kill the dead block, but not the blocks below it.  */
   10504           34 :           c->block = NULL;
   10505           34 :           gfc_free_statements (c);
   10506              :         }
   10507              :     }
   10508              : 
   10509              :   /* More than two cases is legal but insane for logical selects.
   10510              :      Issue a warning for it.  */
   10511          681 :   if (warn_surprising && type == BT_LOGICAL && ncases > 2)
   10512            0 :     gfc_warning (OPT_Wsurprising,
   10513              :                  "Logical SELECT CASE block at %L has more that two cases",
   10514              :                  &code->loc);
   10515              : }
   10516              : 
   10517              : 
   10518              : /* Check if a derived type is extensible.  */
   10519              : 
   10520              : bool
   10521        23959 : gfc_type_is_extensible (gfc_symbol *sym)
   10522              : {
   10523        23959 :   return !(sym->attr.is_bind_c || sym->attr.sequence
   10524        23943 :            || (sym->attr.is_class
   10525         2208 :                && sym->components->ts.u.derived->attr.unlimited_polymorphic));
   10526              : }
   10527              : 
   10528              : 
   10529              : static void
   10530              : resolve_types (gfc_namespace *ns);
   10531              : 
   10532              : /* Resolve an associate-name:  Resolve target and ensure the type-spec is
   10533              :    correct as well as possibly the array-spec.  */
   10534              : 
   10535              : static void
   10536        12785 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
   10537              : {
   10538        12785 :   gfc_expr* target;
   10539        12785 :   bool parentheses = false;
   10540              : 
   10541        12785 :   gcc_assert (sym->assoc);
   10542        12785 :   gcc_assert (sym->attr.flavor == FL_VARIABLE);
   10543              : 
   10544        12785 :   if (sym->assoc->target
   10545         7625 :       && sym->assoc->target->expr_type == EXPR_FUNCTION
   10546          552 :       && sym->assoc->target->symtree
   10547          552 :       && sym->assoc->target->symtree->n.sym
   10548          552 :       && sym->assoc->target->symtree->n.sym->attr.generic)
   10549              :     {
   10550           33 :       if (gfc_resolve_expr (sym->assoc->target))
   10551           33 :         sym->ts = sym->assoc->target->ts;
   10552              :       else
   10553              :         {
   10554            0 :           gfc_error ("%s could not be resolved to a specific function at %L",
   10555            0 :                      sym->assoc->target->symtree->n.sym->name,
   10556            0 :                      &sym->assoc->target->where);
   10557            0 :           return;
   10558              :         }
   10559              :     }
   10560              : 
   10561              :   /* If this is for SELECT TYPE, the target may not yet be set.  In that
   10562              :      case, return.  Resolution will be called later manually again when
   10563              :      this is done.  */
   10564        12785 :   target = sym->assoc->target;
   10565        12785 :   if (!target)
   10566              :     return;
   10567         7625 :   gcc_assert (!sym->assoc->dangling);
   10568              : 
   10569         7625 :   if (target->expr_type == EXPR_OP
   10570          261 :       && target->value.op.op == INTRINSIC_PARENTHESES
   10571           42 :       && target->value.op.op1->expr_type == EXPR_VARIABLE)
   10572              :     {
   10573           23 :       sym->assoc->target = gfc_copy_expr (target->value.op.op1);
   10574           23 :       gfc_free_expr (target);
   10575           23 :       target = sym->assoc->target;
   10576           23 :       parentheses = true;
   10577              :     }
   10578              : 
   10579         7625 :   if (resolve_target && !gfc_resolve_expr (target))
   10580              :     return;
   10581              : 
   10582         7620 :   if (sym->assoc->ar)
   10583              :     {
   10584              :       int dim;
   10585              :       gfc_array_ref *ar = sym->assoc->ar;
   10586           68 :       for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
   10587              :         {
   10588           39 :           if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
   10589           39 :                 && ar->start[dim]->ts.type == BT_INTEGER)
   10590           78 :               || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
   10591           39 :                    && ar->end[dim]->ts.type == BT_INTEGER))
   10592            0 :             gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
   10593              :                        "remapping of associate name %s at %L",
   10594              :                        sym->name, &sym->declared_at);
   10595              :         }
   10596              :     }
   10597              : 
   10598              :   /* For variable targets, we get some attributes from the target.  */
   10599         7620 :   if (target->expr_type == EXPR_VARIABLE)
   10600              :     {
   10601         6623 :       gfc_symbol *tsym, *dsym;
   10602              : 
   10603         6623 :       gcc_assert (target->symtree);
   10604         6623 :       tsym = target->symtree->n.sym;
   10605              : 
   10606         6623 :       if (gfc_expr_attr (target).proc_pointer)
   10607              :         {
   10608            0 :           gfc_error ("Associating entity %qs at %L is a procedure pointer",
   10609              :                      tsym->name, &target->where);
   10610            0 :           return;
   10611              :         }
   10612              : 
   10613           74 :       if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
   10614            2 :           && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
   10615         6624 :           && dsym->attr.flavor == FL_DERIVED)
   10616              :         {
   10617            1 :           gfc_error ("Derived type %qs cannot be used as a variable at %L",
   10618              :                      tsym->name, &target->where);
   10619            1 :           return;
   10620              :         }
   10621              : 
   10622         6622 :       if (tsym->attr.flavor == FL_PROCEDURE)
   10623              :         {
   10624           73 :           bool is_error = true;
   10625           73 :           if (tsym->attr.function && tsym->result == tsym)
   10626          141 :             for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
   10627          137 :               if (tsym == ns->proc_name)
   10628              :                 {
   10629              :                   is_error = false;
   10630              :                   break;
   10631              :                 }
   10632           64 :           if (is_error)
   10633              :             {
   10634           13 :               gfc_error ("Associating entity %qs at %L is a procedure name",
   10635              :                          tsym->name, &target->where);
   10636           13 :               return;
   10637              :             }
   10638              :         }
   10639              : 
   10640         6609 :       sym->attr.asynchronous = tsym->attr.asynchronous;
   10641         6609 :       sym->attr.volatile_ = tsym->attr.volatile_;
   10642              : 
   10643        13218 :       sym->attr.target = tsym->attr.target
   10644         6609 :                          || gfc_expr_attr (target).pointer;
   10645         6609 :       if (is_subref_array (target))
   10646          402 :         sym->attr.subref_array_pointer = 1;
   10647              :     }
   10648          997 :   else if (target->ts.type == BT_PROCEDURE)
   10649              :     {
   10650            0 :       gfc_error ("Associating selector-expression at %L yields a procedure",
   10651              :                  &target->where);
   10652            0 :       return;
   10653              :     }
   10654              : 
   10655         7606 :   if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
   10656              :     {
   10657              :       /* By now, the type of the target has been fixed up.  */
   10658          299 :       symbol_attribute attr;
   10659              : 
   10660          299 :       if (sym->ts.type == BT_DERIVED
   10661          166 :           && target->ts.type == BT_CLASS
   10662           31 :           && !UNLIMITED_POLY (target))
   10663              :         {
   10664              :           /* Inferred to be derived type but the target has type class.  */
   10665           31 :           sym->ts = CLASS_DATA (target)->ts;
   10666           31 :           if (!sym->as)
   10667           31 :             sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
   10668           31 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10669           31 :           sym->attr.dimension = target->rank ? 1 : 0;
   10670           31 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10671              :                             target->corank);
   10672           31 :           sym->as = NULL;
   10673              :         }
   10674          268 :       else if (target->ts.type == BT_DERIVED
   10675          135 :                && target->symtree && target->symtree->n.sym
   10676          111 :                && target->symtree->n.sym->ts.type == BT_CLASS
   10677            0 :                && IS_INFERRED_TYPE (target)
   10678            0 :                && target->ref && target->ref->next
   10679            0 :                && target->ref->next->type == REF_ARRAY
   10680            0 :                && !target->ref->next->next)
   10681              :         {
   10682              :           /* A inferred type selector whose symbol has been determined to be
   10683              :              a class array but which only has an array reference. Change the
   10684              :              associate name and the selector to class type.  */
   10685            0 :           sym->ts = target->ts;
   10686            0 :           attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10687            0 :           sym->attr.dimension = target->rank ? 1 : 0;
   10688            0 :           gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
   10689              :                             target->corank);
   10690            0 :           sym->as = NULL;
   10691            0 :           target->ts = sym->ts;
   10692              :         }
   10693          268 :       else if ((target->ts.type == BT_DERIVED)
   10694          133 :                || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
   10695           61 :                    && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
   10696              :         /* Confirmed to be either a derived type or misidentified to be a
   10697              :            scalar class object, when the selector is a class array.  */
   10698          141 :         sym->ts = target->ts;
   10699          127 :       else if (sym->assoc->inferred_type
   10700          120 :                && (sym->ts.type == BT_COMPLEX
   10701           78 :                    || sym->ts.type == BT_CHARACTER)
   10702           66 :                && target->ts.type == sym->ts.type
   10703           66 :                && sym->ts.kind != target->ts.kind)
   10704              :         /* The inferred type was set from a %re, %im or %len inquiry on
   10705              :            the associate name with the default kind, before the target's
   10706              :            actual type was known.  Now that the target has been resolved,
   10707              :            update the kind to match.  */
   10708            6 :         sym->ts = target->ts;
   10709              :     }
   10710              : 
   10711              : 
   10712         7606 :   if (target->expr_type == EXPR_NULL)
   10713              :     {
   10714            1 :       gfc_error ("Selector at %L cannot be NULL()", &target->where);
   10715            1 :       return;
   10716              :     }
   10717         7605 :   else if (target->ts.type == BT_UNKNOWN)
   10718              :     {
   10719            2 :       gfc_error ("Selector at %L has no type", &target->where);
   10720            2 :       return;
   10721              :     }
   10722              : 
   10723              :   /* Get type if this was not already set.  Note that it can be
   10724              :      some other type than the target in case this is a SELECT TYPE
   10725              :      selector!  So we must not update when the type is already there.  */
   10726         7603 :   if (sym->ts.type == BT_UNKNOWN)
   10727          258 :     sym->ts = target->ts;
   10728              : 
   10729         7603 :   gcc_assert (sym->ts.type != BT_UNKNOWN);
   10730              : 
   10731              :   /* See if this is a valid association-to-variable.  */
   10732        15206 :   sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
   10733         6609 :                            && !parentheses
   10734         6588 :                            && !gfc_has_vector_subscript (target))
   10735         7651 :                           || gfc_is_ptr_fcn (target));
   10736              : 
   10737              :   /* Finally resolve if this is an array or not.  */
   10738         7603 :   if (target->expr_type == EXPR_FUNCTION && target->rank == 0
   10739          191 :       && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
   10740              :     {
   10741          109 :       gfc_expression_rank (target);
   10742          109 :       if (target->ts.type == BT_DERIVED
   10743           62 :           && !sym->as
   10744           62 :           && target->symtree->n.sym->as)
   10745              :         {
   10746            0 :           sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
   10747            0 :           sym->attr.dimension = 1;
   10748              :         }
   10749          109 :       else if (target->ts.type == BT_CLASS
   10750           47 :                && CLASS_DATA (target)->as)
   10751              :         {
   10752            0 :           target->rank = CLASS_DATA (target)->as->rank;
   10753            0 :           target->corank = CLASS_DATA (target)->as->corank;
   10754            0 :           if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
   10755              :             {
   10756            0 :               sym->ts = target->ts;
   10757            0 :               sym->attr.dimension = 0;
   10758              :             }
   10759              :         }
   10760              :     }
   10761              : 
   10762              : 
   10763         7603 :   if (sym->attr.dimension && target->rank == 0)
   10764              :     {
   10765              :       /* primary.cc makes the assumption that a reference to an associate
   10766              :          name followed by a left parenthesis is an array reference.  */
   10767           17 :       if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
   10768              :         {
   10769           12 :           gfc_expression_rank (sym->assoc->target);
   10770           12 :           sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
   10771           12 :           if (!sym->attr.dimension && sym->as)
   10772            0 :             sym->as = NULL;
   10773              :         }
   10774              : 
   10775           17 :       if (sym->attr.dimension && target->rank == 0)
   10776              :         {
   10777            5 :           if (sym->ts.type != BT_CHARACTER)
   10778            5 :             gfc_error ("Associate-name %qs at %L is used as array",
   10779              :                        sym->name, &sym->declared_at);
   10780            5 :           sym->attr.dimension = 0;
   10781            5 :           return;
   10782              :         }
   10783              :     }
   10784              : 
   10785              :   /* We cannot deal with class selectors that need temporaries.  */
   10786         7598 :   if (target->ts.type == BT_CLASS
   10787         7598 :         && gfc_ref_needs_temporary_p (target->ref))
   10788              :     {
   10789            1 :       gfc_error ("CLASS selector at %L needs a temporary which is not "
   10790              :                  "yet implemented", &target->where);
   10791            1 :       return;
   10792              :     }
   10793              : 
   10794         7597 :   if (target->ts.type == BT_CLASS)
   10795         2785 :     gfc_fix_class_refs (target);
   10796              : 
   10797         7597 :   if ((target->rank > 0 || target->corank > 0)
   10798         2732 :       && !sym->attr.select_rank_temporary)
   10799              :     {
   10800         2732 :       gfc_array_spec *as;
   10801              :       /* The rank may be incorrectly guessed at parsing, therefore make sure
   10802              :          it is corrected now.  */
   10803         2732 :       if (sym->ts.type != BT_CLASS
   10804         2156 :           && (!sym->as || sym->as->corank != target->corank))
   10805              :         {
   10806          141 :           if (!sym->as)
   10807          134 :             sym->as = gfc_get_array_spec ();
   10808          141 :           as = sym->as;
   10809          141 :           as->rank = target->rank;
   10810          141 :           as->type = AS_DEFERRED;
   10811          141 :           as->corank = target->corank;
   10812          141 :           sym->attr.dimension = 1;
   10813          141 :           if (as->corank != 0)
   10814            7 :             sym->attr.codimension = 1;
   10815              :         }
   10816         2591 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   10817          575 :                && (!CLASS_DATA (sym)->as
   10818          575 :                    || CLASS_DATA (sym)->as->corank != target->corank))
   10819              :         {
   10820            0 :           if (!CLASS_DATA (sym)->as)
   10821            0 :             CLASS_DATA (sym)->as = gfc_get_array_spec ();
   10822            0 :           as = CLASS_DATA (sym)->as;
   10823            0 :           as->rank = target->rank;
   10824            0 :           as->type = AS_DEFERRED;
   10825            0 :           as->corank = target->corank;
   10826            0 :           CLASS_DATA (sym)->attr.dimension = 1;
   10827            0 :           if (as->corank != 0)
   10828            0 :             CLASS_DATA (sym)->attr.codimension = 1;
   10829              :         }
   10830              :     }
   10831         4865 :   else if (!sym->attr.select_rank_temporary)
   10832              :     {
   10833              :       /* target's rank is 0, but the type of the sym is still array valued,
   10834              :          which has to be corrected.  */
   10835         3476 :       if (sym->ts.type == BT_CLASS && sym->ts.u.derived
   10836          700 :           && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
   10837              :         {
   10838           24 :           gfc_array_spec *as;
   10839           24 :           symbol_attribute attr;
   10840              :           /* The associated variable's type is still the array type
   10841              :              correct this now.  */
   10842           24 :           gfc_typespec *ts = &target->ts;
   10843           24 :           gfc_ref *ref;
   10844              :           /* Internal_ref is true, when this is ref'ing only _data and co-ref.
   10845              :            */
   10846           24 :           bool internal_ref = true;
   10847              : 
   10848           72 :           for (ref = target->ref; ref != NULL; ref = ref->next)
   10849              :             {
   10850           48 :               switch (ref->type)
   10851              :                 {
   10852           24 :                 case REF_COMPONENT:
   10853           24 :                   ts = &ref->u.c.component->ts;
   10854           24 :                   internal_ref
   10855           24 :                     = target->ref == ref && ref->next
   10856           48 :                       && strncmp ("_data", ref->u.c.component->name, 5) == 0;
   10857              :                   break;
   10858           24 :                 case REF_ARRAY:
   10859           24 :                   if (ts->type == BT_CLASS)
   10860            0 :                     ts = &ts->u.derived->components->ts;
   10861           24 :                   if (internal_ref && ref->u.ar.codimen > 0)
   10862            0 :                     for (int i = ref->u.ar.dimen;
   10863              :                          internal_ref
   10864            0 :                          && i < ref->u.ar.dimen + ref->u.ar.codimen;
   10865              :                          ++i)
   10866            0 :                       internal_ref
   10867            0 :                         = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
   10868              :                   break;
   10869              :                 default:
   10870              :                   break;
   10871              :                 }
   10872              :             }
   10873              :           /* Only rewrite the type of this symbol, when the refs are not the
   10874              :              internal ones for class and co-array this-image.  */
   10875           24 :           if (!internal_ref)
   10876              :             {
   10877              :               /* Create a scalar instance of the current class type.  Because
   10878              :                  the rank of a class array goes into its name, the type has to
   10879              :                  be rebuilt.  The alternative of (re-)setting just the
   10880              :                  attributes and as in the current type, destroys the type also
   10881              :                  in other places.  */
   10882            0 :               as = NULL;
   10883            0 :               sym->ts = *ts;
   10884            0 :               sym->ts.type = BT_CLASS;
   10885            0 :               attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
   10886            0 :               gfc_change_class (&sym->ts, &attr, as, 0, 0);
   10887            0 :               sym->as = NULL;
   10888              :             }
   10889              :         }
   10890              :     }
   10891              : 
   10892              :   /* Mark this as an associate variable.  */
   10893         7597 :   sym->attr.associate_var = 1;
   10894              : 
   10895              :   /* Fix up the type-spec for CHARACTER types.  */
   10896         7597 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
   10897              :     {
   10898          503 :       gfc_ref *ref;
   10899          788 :       for (ref = target->ref; ref; ref = ref->next)
   10900          311 :         if (ref->type == REF_SUBSTRING
   10901           74 :             && (ref->u.ss.start == NULL
   10902           74 :                 || ref->u.ss.start->expr_type != EXPR_CONSTANT
   10903           74 :                 || ref->u.ss.end == NULL
   10904           54 :                 || ref->u.ss.end->expr_type != EXPR_CONSTANT))
   10905              :           break;
   10906              : 
   10907          503 :       if (!sym->ts.u.cl)
   10908          182 :         sym->ts.u.cl = target->ts.u.cl;
   10909              : 
   10910          503 :       if (sym->ts.deferred
   10911          189 :           && sym->ts.u.cl == target->ts.u.cl)
   10912              :         {
   10913          110 :           sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10914          110 :           sym->ts.deferred = 1;
   10915              :         }
   10916              : 
   10917          503 :       if (!sym->ts.u.cl->length
   10918          327 :           && !sym->ts.deferred
   10919          138 :           && target->expr_type == EXPR_CONSTANT)
   10920              :         {
   10921           30 :           sym->ts.u.cl->length =
   10922           30 :                 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
   10923           30 :                                   target->value.character.length);
   10924              :         }
   10925          473 :       else if (((!sym->ts.u.cl->length
   10926          176 :                  || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   10927          303 :                 && target->expr_type != EXPR_VARIABLE)
   10928          350 :                || ref)
   10929              :         {
   10930          149 :           if (!sym->ts.deferred)
   10931              :             {
   10932           45 :               sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
   10933           45 :               sym->ts.deferred = 1;
   10934              :             }
   10935              : 
   10936              :           /* This is reset in trans-stmt.cc after the assignment
   10937              :              of the target expression to the associate name.  */
   10938          149 :           if (ref && sym->as)
   10939           26 :             sym->attr.pointer = 1;
   10940              :           else
   10941          123 :             sym->attr.allocatable = 1;
   10942              :         }
   10943              :     }
   10944              : 
   10945         7597 :   if (sym->ts.type == BT_CLASS
   10946         1421 :       && IS_INFERRED_TYPE (target)
   10947           13 :       && target->ts.type == BT_DERIVED
   10948            0 :       && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
   10949            0 :       && target->ref && target->ref->next && !target->ref->next->next
   10950            0 :       && target->ref->next->type == REF_ARRAY)
   10951            0 :     target->ts = target->symtree->n.sym->ts;
   10952              : 
   10953              :   /* If the target is a good class object, so is the associate variable.  */
   10954         7597 :   if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
   10955          713 :     sym->attr.class_ok = 1;
   10956              : 
   10957              :   /* If the target is a contiguous pointer, so is the associate variable.  */
   10958         7597 :   if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
   10959            3 :     sym->attr.contiguous = 1;
   10960              : }
   10961              : 
   10962              : 
   10963              : /* Ensure that SELECT TYPE expressions have the correct rank and a full
   10964              :    array reference, where necessary.  The symbols are artificial and so
   10965              :    the dimension attribute and arrayspec can also be set.  In addition,
   10966              :    sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
   10967              :    This is corrected here as well.*/
   10968              : 
   10969              : static void
   10970         1687 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
   10971              :                  gfc_ref *ref)
   10972              : {
   10973         1687 :   gfc_ref *nref = (*expr1)->ref;
   10974         1687 :   gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
   10975         1687 :   gfc_symbol *sym2;
   10976         1687 :   gfc_expr *selector = gfc_copy_expr (expr2);
   10977              : 
   10978         1687 :   (*expr1)->rank = rank;
   10979         1687 :   (*expr1)->corank = corank;
   10980         1687 :   if (selector)
   10981              :     {
   10982          311 :       gfc_resolve_expr (selector);
   10983          311 :       if (selector->expr_type == EXPR_OP
   10984            2 :           && selector->value.op.op == INTRINSIC_PARENTHESES)
   10985            2 :         sym2 = selector->value.op.op1->symtree->n.sym;
   10986          309 :       else if (selector->expr_type == EXPR_VARIABLE
   10987            7 :                || selector->expr_type == EXPR_FUNCTION)
   10988          309 :         sym2 = selector->symtree->n.sym;
   10989              :       else
   10990            0 :         gcc_unreachable ();
   10991              :     }
   10992              :   else
   10993              :     sym2 = NULL;
   10994              : 
   10995         1687 :   if (sym1->ts.type == BT_CLASS)
   10996              :     {
   10997         1687 :       if ((*expr1)->ts.type != BT_CLASS)
   10998           13 :         (*expr1)->ts = sym1->ts;
   10999              : 
   11000         1687 :       CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
   11001         1687 :       CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
   11002         1687 :       if (CLASS_DATA (sym1)->as == NULL && sym2)
   11003            1 :         CLASS_DATA (sym1)->as
   11004            1 :                 = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
   11005              :     }
   11006              :   else
   11007              :     {
   11008            0 :       sym1->attr.dimension = rank > 0 ? 1 : 0;
   11009            0 :       sym1->attr.codimension = corank > 0 ? 1 : 0;
   11010            0 :       if (sym1->as == NULL && sym2)
   11011            0 :         sym1->as = gfc_copy_array_spec (sym2->as);
   11012              :     }
   11013              : 
   11014         3057 :   for (; nref; nref = nref->next)
   11015         2746 :     if (nref->next == NULL)
   11016              :       break;
   11017              : 
   11018         1687 :   if (ref && nref && nref->type != REF_ARRAY)
   11019            6 :     nref->next = gfc_copy_ref (ref);
   11020         1681 :   else if (ref && !nref)
   11021          302 :     (*expr1)->ref = gfc_copy_ref (ref);
   11022         1379 :   else if (ref && nref->u.ar.codimen != corank)
   11023              :     {
   11024          976 :       for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
   11025          915 :         nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
   11026           61 :       nref->u.ar.codimen = corank;
   11027              :     }
   11028         1687 : }
   11029              : 
   11030              : 
   11031              : static gfc_expr *
   11032         6752 : build_loc_call (gfc_expr *sym_expr)
   11033              : {
   11034         6752 :   gfc_expr *loc_call;
   11035         6752 :   loc_call = gfc_get_expr ();
   11036         6752 :   loc_call->expr_type = EXPR_FUNCTION;
   11037         6752 :   gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
   11038         6752 :   loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
   11039         6752 :   loc_call->symtree->n.sym->attr.intrinsic = 1;
   11040         6752 :   loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
   11041         6752 :   gfc_commit_symbol (loc_call->symtree->n.sym);
   11042         6752 :   loc_call->ts.type = BT_INTEGER;
   11043         6752 :   loc_call->ts.kind = gfc_index_integer_kind;
   11044         6752 :   loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
   11045         6752 :   loc_call->value.function.actual = gfc_get_actual_arglist ();
   11046         6752 :   loc_call->value.function.actual->expr = sym_expr;
   11047         6752 :   loc_call->where = sym_expr->where;
   11048         6752 :   return loc_call;
   11049              : }
   11050              : 
   11051              : /* Resolve a SELECT TYPE statement.  */
   11052              : 
   11053              : static void
   11054         3029 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
   11055              : {
   11056         3029 :   gfc_symbol *selector_type;
   11057         3029 :   gfc_code *body, *new_st, *if_st, *tail;
   11058         3029 :   gfc_code *class_is = NULL, *default_case = NULL;
   11059         3029 :   gfc_case *c;
   11060         3029 :   gfc_symtree *st;
   11061         3029 :   char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
   11062         3029 :   gfc_namespace *ns;
   11063         3029 :   int error = 0;
   11064         3029 :   int rank = 0, corank = 0;
   11065         3029 :   gfc_ref* ref = NULL;
   11066         3029 :   gfc_expr *selector_expr = NULL;
   11067         3029 :   gfc_code *old_code = code;
   11068              : 
   11069         3029 :   ns = code->ext.block.ns;
   11070         3029 :   if (code->expr2)
   11071              :     {
   11072              :       /* Set this, or coarray checks in resolve will fail.  */
   11073          639 :       code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
   11074              :     }
   11075         3029 :   gfc_resolve (ns);
   11076              : 
   11077              :   /* Check for F03:C813.  */
   11078         3029 :   if (code->expr1->ts.type != BT_CLASS
   11079           36 :       && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
   11080              :     {
   11081           13 :       gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
   11082              :                  "at %L", &code->loc);
   11083           42 :       return;
   11084              :     }
   11085              : 
   11086              :   /* Prevent segfault, when class type is not initialized due to previous
   11087              :      error.  */
   11088         3016 :   if (!code->expr1->symtree->n.sym->attr.class_ok
   11089         3014 :       || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
   11090              :     return;
   11091              : 
   11092         3009 :   if (code->expr2)
   11093              :     {
   11094          630 :       gfc_ref *ref2 = NULL;
   11095         1466 :       for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
   11096          836 :          if (ref->type == REF_COMPONENT
   11097          432 :              && ref->u.c.component->ts.type == BT_CLASS)
   11098          836 :            ref2 = ref;
   11099              : 
   11100          630 :       if (ref2)
   11101              :         {
   11102          340 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11103            1 :             code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
   11104          340 :           selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
   11105              :         }
   11106              :       else
   11107              :         {
   11108          290 :           if (code->expr1->symtree->n.sym->attr.untyped)
   11109           28 :             code->expr1->symtree->n.sym->ts = code->expr2->ts;
   11110              :           /* Sometimes the selector expression is given the typespec of the
   11111              :              '_data' field, which is logical enough but inappropriate here. */
   11112          290 :           if (code->expr2->ts.type == BT_DERIVED
   11113           73 :               && code->expr2->symtree
   11114           73 :               && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
   11115           73 :             code->expr2->ts = code->expr2->symtree->n.sym->ts;
   11116          290 :           selector_type = CLASS_DATA (code->expr2)
   11117              :             ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
   11118              :         }
   11119              : 
   11120          630 :       if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
   11121              :         {
   11122          297 :           CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
   11123          297 :           CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
   11124          297 :           CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
   11125              :         }
   11126              : 
   11127              :       /* F2008: C803 The selector expression must not be coindexed.  */
   11128          630 :       if (gfc_is_coindexed (code->expr2))
   11129              :         {
   11130            4 :           gfc_error ("Selector at %L must not be coindexed",
   11131            4 :                      &code->expr2->where);
   11132            4 :           return;
   11133              :         }
   11134              : 
   11135              :     }
   11136              :   else
   11137              :     {
   11138         2379 :       selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
   11139              : 
   11140         2379 :       if (gfc_is_coindexed (code->expr1))
   11141              :         {
   11142            0 :           gfc_error ("Selector at %L must not be coindexed",
   11143            0 :                      &code->expr1->where);
   11144            0 :           return;
   11145              :         }
   11146              :     }
   11147              : 
   11148              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11149         8379 :   for (body = code->block; body; body = body->block)
   11150              :     {
   11151         5375 :       c = body->ext.block.case_list;
   11152              : 
   11153         5375 :       if (!error)
   11154              :         {
   11155              :           /* Check for repeated cases.  */
   11156         8340 :           for (tail = code->block; tail; tail = tail->block)
   11157              :             {
   11158         8340 :               gfc_case *d = tail->ext.block.case_list;
   11159         8340 :               if (tail == body)
   11160              :                 break;
   11161              : 
   11162         2974 :               if (c->ts.type == d->ts.type
   11163          516 :                   && ((c->ts.type == BT_DERIVED
   11164          418 :                        && c->ts.u.derived && d->ts.u.derived
   11165          418 :                        && !strcmp (c->ts.u.derived->name,
   11166              :                                    d->ts.u.derived->name))
   11167          515 :                       || c->ts.type == BT_UNKNOWN
   11168          515 :                       || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11169           55 :                           && c->ts.kind == d->ts.kind)))
   11170              :                 {
   11171            1 :                   gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
   11172              :                              &c->where, &d->where);
   11173            1 :                   return;
   11174              :                 }
   11175              :             }
   11176              :         }
   11177              : 
   11178              :       /* Check F03:C815.  */
   11179         3404 :       if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11180         2318 :           && selector_type
   11181         2318 :           && !selector_type->attr.unlimited_polymorphic
   11182         7371 :           && !gfc_type_is_extensible (c->ts.u.derived))
   11183              :         {
   11184            1 :           gfc_error ("Derived type %qs at %L must be extensible",
   11185            1 :                      c->ts.u.derived->name, &c->where);
   11186            1 :           error++;
   11187            1 :           continue;
   11188              :         }
   11189              : 
   11190              :       /* Check F03:C816.  */
   11191         5379 :       if (c->ts.type != BT_UNKNOWN
   11192         3763 :           && selector_type && !selector_type->attr.unlimited_polymorphic
   11193         7373 :           && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
   11194         1996 :               || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
   11195              :         {
   11196            6 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11197            2 :             gfc_error ("Derived type %qs at %L must be an extension of %qs",
   11198            2 :                        c->ts.u.derived->name, &c->where, selector_type->name);
   11199              :           else
   11200            4 :             gfc_error ("Unexpected intrinsic type %qs at %L",
   11201              :                        gfc_basic_typename (c->ts.type), &c->where);
   11202            6 :           error++;
   11203            6 :           continue;
   11204              :         }
   11205              : 
   11206              :       /* Check F03:C814.  */
   11207         5367 :       if (c->ts.type == BT_CHARACTER
   11208          736 :           && (c->ts.u.cl->length != NULL || c->ts.deferred))
   11209              :         {
   11210            0 :           gfc_error ("The type-spec at %L shall specify that each length "
   11211              :                      "type parameter is assumed", &c->where);
   11212            0 :           error++;
   11213            0 :           continue;
   11214              :         }
   11215              : 
   11216              :       /* Intercept the DEFAULT case.  */
   11217         5367 :       if (c->ts.type == BT_UNKNOWN)
   11218              :         {
   11219              :           /* Check F03:C818.  */
   11220         1610 :           if (default_case)
   11221              :             {
   11222            1 :               gfc_error ("The DEFAULT CASE at %L cannot be followed "
   11223              :                          "by a second DEFAULT CASE at %L",
   11224            1 :                          &default_case->ext.block.case_list->where, &c->where);
   11225            1 :               error++;
   11226            1 :               continue;
   11227              :             }
   11228              : 
   11229              :           default_case = body;
   11230              :         }
   11231              :     }
   11232              : 
   11233         3004 :   if (error > 0)
   11234              :     return;
   11235              : 
   11236              :   /* Transform SELECT TYPE statement to BLOCK and associate selector to
   11237              :      target if present.  If there are any EXIT statements referring to the
   11238              :      SELECT TYPE construct, this is no problem because the gfc_code
   11239              :      reference stays the same and EXIT is equally possible from the BLOCK
   11240              :      it is changed to.  */
   11241         3001 :   code->op = EXEC_BLOCK;
   11242         3001 :   if (code->expr2)
   11243              :     {
   11244          626 :       gfc_association_list* assoc;
   11245              : 
   11246          626 :       assoc = gfc_get_association_list ();
   11247          626 :       assoc->st = code->expr1->symtree;
   11248          626 :       assoc->target = gfc_copy_expr (code->expr2);
   11249          626 :       assoc->target->where = code->expr2->where;
   11250              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11251              : 
   11252          626 :       code->ext.block.assoc = assoc;
   11253          626 :       code->expr1->symtree->n.sym->assoc = assoc;
   11254              : 
   11255          626 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11256              :     }
   11257              :   else
   11258         2375 :     code->ext.block.assoc = NULL;
   11259              : 
   11260              :   /* Ensure that the selector rank and arrayspec are available to
   11261              :      correct expressions in which they might be missing.  */
   11262         3001 :   if (code->expr2 && (code->expr2->rank || code->expr2->corank))
   11263              :     {
   11264          311 :       rank = code->expr2->rank;
   11265          311 :       corank = code->expr2->corank;
   11266          585 :       for (ref = code->expr2->ref; ref; ref = ref->next)
   11267          576 :         if (ref->next == NULL)
   11268              :           break;
   11269          311 :       if (ref && ref->type == REF_ARRAY)
   11270          302 :         ref = gfc_copy_ref (ref);
   11271              : 
   11272              :       /* Fixup expr1 if necessary.  */
   11273          311 :       if (rank || corank)
   11274          311 :         fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
   11275              :     }
   11276         2690 :   else if (code->expr1->rank || code->expr1->corank)
   11277              :     {
   11278          884 :       rank = code->expr1->rank;
   11279          884 :       corank = code->expr1->corank;
   11280          884 :       for (ref = code->expr1->ref; ref; ref = ref->next)
   11281          884 :         if (ref->next == NULL)
   11282              :           break;
   11283          884 :       if (ref && ref->type == REF_ARRAY)
   11284          884 :         ref = gfc_copy_ref (ref);
   11285              :     }
   11286              : 
   11287         3001 :   gfc_expr *orig_expr1 = code->expr1;
   11288              : 
   11289              :   /* Add EXEC_SELECT to switch on type.  */
   11290         3001 :   new_st = gfc_get_code (code->op);
   11291         3001 :   new_st->expr1 = code->expr1;
   11292         3001 :   new_st->expr2 = code->expr2;
   11293         3001 :   new_st->block = code->block;
   11294         3001 :   code->expr1 = code->expr2 =  NULL;
   11295         3001 :   code->block = NULL;
   11296         3001 :   if (!ns->code)
   11297         3001 :     ns->code = new_st;
   11298              :   else
   11299            0 :     ns->code->next = new_st;
   11300         3001 :   code = new_st;
   11301         3001 :   code->op = EXEC_SELECT_TYPE;
   11302              : 
   11303              :   /* Use the intrinsic LOC function to generate an integer expression
   11304              :      for the vtable of the selector.  Note that the rank of the selector
   11305              :      expression has to be set to zero.  */
   11306         3001 :   gfc_add_vptr_component (code->expr1);
   11307         3001 :   code->expr1->rank = 0;
   11308         3001 :   code->expr1->corank = 0;
   11309         3001 :   code->expr1 = build_loc_call (code->expr1);
   11310         3001 :   selector_expr = code->expr1->value.function.actual->expr;
   11311              : 
   11312              :   /* Loop over TYPE IS / CLASS IS cases.  */
   11313         8360 :   for (body = code->block; body; body = body->block)
   11314              :     {
   11315         5359 :       gfc_symbol *vtab;
   11316         5359 :       c = body->ext.block.case_list;
   11317              : 
   11318              :       /* Generate an index integer expression for address of the
   11319              :          TYPE/CLASS vtable and store it in c->low.  The hash expression
   11320              :          is stored in c->high and is used to resolve intrinsic cases.  */
   11321         5359 :       if (c->ts.type != BT_UNKNOWN)
   11322              :         {
   11323         3751 :           gfc_expr *e;
   11324         3751 :           if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   11325              :             {
   11326         2309 :               vtab = gfc_find_derived_vtab (c->ts.u.derived);
   11327         2309 :               gcc_assert (vtab);
   11328         2309 :               c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
   11329         2309 :                                           c->ts.u.derived->hash_value);
   11330              :             }
   11331              :           else
   11332              :             {
   11333         1442 :               vtab = gfc_find_vtab (&c->ts);
   11334         1442 :               gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
   11335         1442 :               e = CLASS_DATA (vtab)->initializer;
   11336         1442 :               c->high = gfc_copy_expr (e);
   11337         1442 :               if (c->high->ts.kind != gfc_integer_4_kind)
   11338              :                 {
   11339            1 :                   gfc_typespec ts;
   11340            1 :                   ts.kind = gfc_integer_4_kind;
   11341            1 :                   ts.type = BT_INTEGER;
   11342            1 :                   gfc_convert_type_warn (c->high, &ts, 2, 0);
   11343              :                 }
   11344              :             }
   11345              : 
   11346         3751 :           e = gfc_lval_expr_from_sym (vtab);
   11347         3751 :           c->low = build_loc_call (e);
   11348              :         }
   11349              :       else
   11350         1608 :         continue;
   11351              : 
   11352              :       /* Associate temporary to selector.  This should only be done
   11353              :          when this case is actually true, so build a new ASSOCIATE
   11354              :          that does precisely this here (instead of using the
   11355              :          'global' one).  */
   11356              : 
   11357              :       /* First check the derived type import status.  */
   11358         3751 :       if (gfc_current_ns->import_state != IMPORT_NOT_SET
   11359            6 :           && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
   11360              :         {
   11361           12 :           st = gfc_find_symtree (gfc_current_ns->sym_root,
   11362            6 :                                  c->ts.u.derived->name);
   11363            6 :           if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
   11364              :                                         gfc_current_ns))
   11365            6 :             error++;
   11366              :         }
   11367              : 
   11368         3751 :       const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
   11369         3751 :       if (c->ts.type == BT_CLASS)
   11370          346 :         snprintf (name, sizeof (name), "__tmp_class_%s_%s",
   11371          346 :                   c->ts.u.derived->name, var_name);
   11372         3405 :       else if (c->ts.type == BT_DERIVED)
   11373         1963 :         snprintf (name, sizeof (name), "__tmp_type_%s_%s",
   11374         1963 :                   c->ts.u.derived->name, var_name);
   11375         1442 :       else if (c->ts.type == BT_CHARACTER)
   11376              :         {
   11377          736 :           HOST_WIDE_INT charlen = 0;
   11378          736 :           if (c->ts.u.cl && c->ts.u.cl->length
   11379            0 :               && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11380            0 :             charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11381          736 :           snprintf (name, sizeof (name),
   11382              :                     "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
   11383              :                     gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
   11384              :                     var_name);
   11385              :         }
   11386              :       else
   11387          706 :         snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
   11388              :                   gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
   11389              : 
   11390         3751 :       st = gfc_find_symtree (ns->sym_root, name);
   11391         3751 :       gcc_assert (st->n.sym->assoc);
   11392         3751 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11393         3751 :       st->n.sym->assoc->target->where = selector_expr->where;
   11394         3751 :       if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
   11395              :         {
   11396         3405 :           gfc_add_data_component (st->n.sym->assoc->target);
   11397              :           /* Fixup the target expression if necessary.  */
   11398         3405 :           if (rank || corank)
   11399         1376 :             fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
   11400              :                              ref);
   11401              :         }
   11402              : 
   11403         3751 :       new_st = gfc_get_code (EXEC_BLOCK);
   11404         3751 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11405         3751 :       new_st->ext.block.ns->code = body->next;
   11406         3751 :       body->next = new_st;
   11407              : 
   11408              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11409              :          there is a CASE label overlap and this is already used.  Just ignore,
   11410              :          the error is diagnosed elsewhere.  */
   11411         3751 :       if (st->n.sym->assoc->dangling)
   11412              :         {
   11413         3750 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11414         3750 :           st->n.sym->assoc->dangling = 0;
   11415              :         }
   11416              : 
   11417         3751 :       resolve_assoc_var (st->n.sym, false);
   11418              :     }
   11419              : 
   11420              :   /* Take out CLASS IS cases for separate treatment.  */
   11421              :   body = code;
   11422         8360 :   while (body && body->block)
   11423              :     {
   11424         5359 :       if (body->block->ext.block.case_list->ts.type == BT_CLASS)
   11425              :         {
   11426              :           /* Add to class_is list.  */
   11427          346 :           if (class_is == NULL)
   11428              :             {
   11429          315 :               class_is = body->block;
   11430          315 :               tail = class_is;
   11431              :             }
   11432              :           else
   11433              :             {
   11434           43 :               for (tail = class_is; tail->block; tail = tail->block) ;
   11435           31 :               tail->block = body->block;
   11436           31 :               tail = tail->block;
   11437              :             }
   11438              :           /* Remove from EXEC_SELECT list.  */
   11439          346 :           body->block = body->block->block;
   11440          346 :           tail->block = NULL;
   11441              :         }
   11442              :       else
   11443              :         body = body->block;
   11444              :     }
   11445              : 
   11446         3001 :   if (class_is)
   11447              :     {
   11448          315 :       gfc_symbol *vtab;
   11449              : 
   11450          315 :       if (!default_case)
   11451              :         {
   11452              :           /* Add a default case to hold the CLASS IS cases.  */
   11453          313 :           for (tail = code; tail->block; tail = tail->block) ;
   11454          205 :           tail->block = gfc_get_code (EXEC_SELECT_TYPE);
   11455          205 :           tail = tail->block;
   11456          205 :           tail->ext.block.case_list = gfc_get_case ();
   11457          205 :           tail->ext.block.case_list->ts.type = BT_UNKNOWN;
   11458          205 :           tail->next = NULL;
   11459          205 :           default_case = tail;
   11460              :         }
   11461              : 
   11462              :       /* More than one CLASS IS block?  */
   11463          315 :       if (class_is->block)
   11464              :         {
   11465           37 :           gfc_code **c1,*c2;
   11466           37 :           bool swapped;
   11467              :           /* Sort CLASS IS blocks by extension level.  */
   11468           36 :           do
   11469              :             {
   11470           37 :               swapped = false;
   11471           97 :               for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
   11472              :                 {
   11473           61 :                   c2 = (*c1)->block;
   11474              :                   /* F03:C817 (check for doubles).  */
   11475           61 :                   if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
   11476           61 :                       == c2->ext.block.case_list->ts.u.derived->hash_value)
   11477              :                     {
   11478            1 :                       gfc_error ("Double CLASS IS block in SELECT TYPE "
   11479              :                                  "statement at %L",
   11480              :                                  &c2->ext.block.case_list->where);
   11481            1 :                       return;
   11482              :                     }
   11483           60 :                   if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
   11484           60 :                       < c2->ext.block.case_list->ts.u.derived->attr.extension)
   11485              :                     {
   11486              :                       /* Swap.  */
   11487           24 :                       (*c1)->block = c2->block;
   11488           24 :                       c2->block = *c1;
   11489           24 :                       *c1 = c2;
   11490           24 :                       swapped = true;
   11491              :                     }
   11492              :                 }
   11493              :             }
   11494              :           while (swapped);
   11495              :         }
   11496              : 
   11497              :       /* Generate IF chain.  */
   11498          314 :       if_st = gfc_get_code (EXEC_IF);
   11499          314 :       new_st = if_st;
   11500          658 :       for (body = class_is; body; body = body->block)
   11501              :         {
   11502          344 :           new_st->block = gfc_get_code (EXEC_IF);
   11503          344 :           new_st = new_st->block;
   11504              :           /* Set up IF condition: Call _gfortran_is_extension_of.  */
   11505          344 :           new_st->expr1 = gfc_get_expr ();
   11506          344 :           new_st->expr1->expr_type = EXPR_FUNCTION;
   11507          344 :           new_st->expr1->ts.type = BT_LOGICAL;
   11508          344 :           new_st->expr1->ts.kind = 4;
   11509          344 :           new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
   11510          344 :           new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
   11511          344 :           new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
   11512              :           /* Set up arguments.  */
   11513          344 :           new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
   11514          344 :           new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
   11515          344 :           new_st->expr1->value.function.actual->expr->where = code->loc;
   11516          344 :           new_st->expr1->where = code->loc;
   11517          344 :           gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
   11518          344 :           vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
   11519          344 :           st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
   11520          344 :           new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
   11521          344 :           new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
   11522          344 :           new_st->expr1->value.function.actual->next->expr->where = code->loc;
   11523              :           /* Set up types in formal arg list.  */
   11524          344 :           new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
   11525          344 :           new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
   11526          344 :           new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
   11527          344 :           new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
   11528              : 
   11529          344 :           new_st->next = body->next;
   11530              :         }
   11531          314 :         if (default_case->next)
   11532              :           {
   11533          110 :             new_st->block = gfc_get_code (EXEC_IF);
   11534          110 :             new_st = new_st->block;
   11535          110 :             new_st->next = default_case->next;
   11536              :           }
   11537              : 
   11538              :         /* Replace CLASS DEFAULT code by the IF chain.  */
   11539          314 :         default_case->next = if_st;
   11540              :     }
   11541              : 
   11542              :   /* Resolve the internal code.  This cannot be done earlier because
   11543              :      it requires that the sym->assoc of selectors is set already.  */
   11544         3000 :   gfc_current_ns = ns;
   11545         3000 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11546         3000 :   gfc_current_ns = old_ns;
   11547              : 
   11548         3000 :   free (ref);
   11549              : }
   11550              : 
   11551              : 
   11552              : /* Resolve a SELECT RANK statement.  */
   11553              : 
   11554              : static void
   11555         1024 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
   11556              : {
   11557         1024 :   gfc_namespace *ns;
   11558         1024 :   gfc_code *body, *new_st, *tail;
   11559         1024 :   gfc_case *c;
   11560         1024 :   char tname[GFC_MAX_SYMBOL_LEN + 7];
   11561         1024 :   char name[2 * GFC_MAX_SYMBOL_LEN];
   11562         1024 :   gfc_symtree *st;
   11563         1024 :   gfc_expr *selector_expr = NULL;
   11564         1024 :   int case_value;
   11565         1024 :   HOST_WIDE_INT charlen = 0;
   11566              : 
   11567         1024 :   ns = code->ext.block.ns;
   11568         1024 :   gfc_resolve (ns);
   11569              : 
   11570         1024 :   code->op = EXEC_BLOCK;
   11571         1024 :   if (code->expr2)
   11572              :     {
   11573           42 :       gfc_association_list* assoc;
   11574              : 
   11575           42 :       assoc = gfc_get_association_list ();
   11576           42 :       assoc->st = code->expr1->symtree;
   11577           42 :       assoc->target = gfc_copy_expr (code->expr2);
   11578           42 :       assoc->target->where = code->expr2->where;
   11579              :       /* assoc->variable will be set by resolve_assoc_var.  */
   11580              : 
   11581           42 :       code->ext.block.assoc = assoc;
   11582           42 :       code->expr1->symtree->n.sym->assoc = assoc;
   11583              : 
   11584           42 :       resolve_assoc_var (code->expr1->symtree->n.sym, false);
   11585              :     }
   11586              :   else
   11587          982 :     code->ext.block.assoc = NULL;
   11588              : 
   11589              :   /* Loop over RANK cases. Note that returning on the errors causes a
   11590              :      cascade of further errors because the case blocks do not compile
   11591              :      correctly.  */
   11592         3332 :   for (body = code->block; body; body = body->block)
   11593              :     {
   11594         2308 :       c = body->ext.block.case_list;
   11595         2308 :       if (c->low)
   11596         1389 :         case_value = (int) mpz_get_si (c->low->value.integer);
   11597              :       else
   11598              :         case_value = -2;
   11599              : 
   11600              :       /* Check for repeated cases.  */
   11601         5842 :       for (tail = code->block; tail; tail = tail->block)
   11602              :         {
   11603         5842 :           gfc_case *d = tail->ext.block.case_list;
   11604         5842 :           int case_value2;
   11605              : 
   11606         5842 :           if (tail == body)
   11607              :             break;
   11608              : 
   11609              :           /* Check F2018: C1153.  */
   11610         3534 :           if (!c->low && !d->low)
   11611            1 :             gfc_error ("RANK DEFAULT at %L is repeated at %L",
   11612              :                        &c->where, &d->where);
   11613              : 
   11614         3534 :           if (!c->low || !d->low)
   11615         1253 :             continue;
   11616              : 
   11617              :           /* Check F2018: C1153.  */
   11618         2281 :           case_value2 = (int) mpz_get_si (d->low->value.integer);
   11619         2281 :           if ((case_value == case_value2) && case_value == -1)
   11620            1 :             gfc_error ("RANK (*) at %L is repeated at %L",
   11621              :                        &c->where, &d->where);
   11622         2280 :           else if (case_value == case_value2)
   11623            1 :             gfc_error ("RANK (%i) at %L is repeated at %L",
   11624              :                        case_value, &c->where, &d->where);
   11625              :         }
   11626              : 
   11627         2308 :       if (!c->low)
   11628          919 :         continue;
   11629              : 
   11630              :       /* Check F2018: C1155.  */
   11631         1389 :       if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
   11632         1387 :                                || gfc_expr_attr (code->expr1).pointer))
   11633            3 :         gfc_error ("RANK (*) at %L cannot be used with the pointer or "
   11634            3 :                    "allocatable selector at %L", &c->where, &code->expr1->where);
   11635              :     }
   11636              : 
   11637              :   /* Add EXEC_SELECT to switch on rank.  */
   11638         1024 :   new_st = gfc_get_code (code->op);
   11639         1024 :   new_st->expr1 = code->expr1;
   11640         1024 :   new_st->expr2 = code->expr2;
   11641         1024 :   new_st->block = code->block;
   11642         1024 :   code->expr1 = code->expr2 =  NULL;
   11643         1024 :   code->block = NULL;
   11644         1024 :   if (!ns->code)
   11645         1024 :     ns->code = new_st;
   11646              :   else
   11647            0 :     ns->code->next = new_st;
   11648         1024 :   code = new_st;
   11649         1024 :   code->op = EXEC_SELECT_RANK;
   11650              : 
   11651         1024 :   selector_expr = code->expr1;
   11652              : 
   11653              :   /* Loop over SELECT RANK cases.  */
   11654         3332 :   for (body = code->block; body; body = body->block)
   11655              :     {
   11656         2308 :       c = body->ext.block.case_list;
   11657         2308 :       int case_value;
   11658              : 
   11659              :       /* Pass on the default case.  */
   11660         2308 :       if (c->low == NULL)
   11661          919 :         continue;
   11662              : 
   11663              :       /* Associate temporary to selector.  This should only be done
   11664              :          when this case is actually true, so build a new ASSOCIATE
   11665              :          that does precisely this here (instead of using the
   11666              :          'global' one).  */
   11667         1389 :       if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
   11668          265 :           && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   11669          186 :         charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
   11670              : 
   11671         1389 :       if (c->ts.type == BT_CLASS)
   11672          145 :         sprintf (tname, "class_%s", c->ts.u.derived->name);
   11673         1244 :       else if (c->ts.type == BT_DERIVED)
   11674          110 :         sprintf (tname, "type_%s", c->ts.u.derived->name);
   11675         1134 :       else if (c->ts.type != BT_CHARACTER)
   11676          575 :         sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
   11677              :       else
   11678          559 :         sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
   11679              :                  gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
   11680              : 
   11681         1389 :       case_value = (int) mpz_get_si (c->low->value.integer);
   11682         1389 :       if (case_value >= 0)
   11683         1356 :         sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
   11684              :       else
   11685           33 :         sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
   11686              : 
   11687         1389 :       st = gfc_find_symtree (ns->sym_root, name);
   11688         1389 :       gcc_assert (st->n.sym->assoc);
   11689              : 
   11690         1389 :       st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
   11691         1389 :       st->n.sym->assoc->target->where = selector_expr->where;
   11692              : 
   11693         1389 :       new_st = gfc_get_code (EXEC_BLOCK);
   11694         1389 :       new_st->ext.block.ns = gfc_build_block_ns (ns);
   11695         1389 :       new_st->ext.block.ns->code = body->next;
   11696         1389 :       body->next = new_st;
   11697              : 
   11698              :       /* Chain in the new list only if it is marked as dangling.  Otherwise
   11699              :          there is a CASE label overlap and this is already used.  Just ignore,
   11700              :          the error is diagnosed elsewhere.  */
   11701         1389 :       if (st->n.sym->assoc->dangling)
   11702              :         {
   11703         1387 :           new_st->ext.block.assoc = st->n.sym->assoc;
   11704         1387 :           st->n.sym->assoc->dangling = 0;
   11705              :         }
   11706              : 
   11707         1389 :       resolve_assoc_var (st->n.sym, false);
   11708              :     }
   11709              : 
   11710         1024 :   gfc_current_ns = ns;
   11711         1024 :   gfc_resolve_blocks (code->block, gfc_current_ns);
   11712         1024 :   gfc_current_ns = old_ns;
   11713         1024 : }
   11714              : 
   11715              : 
   11716              : /* Resolve a transfer statement. This is making sure that:
   11717              :    -- a derived type being transferred has only non-pointer components
   11718              :    -- a derived type being transferred doesn't have private components, unless
   11719              :       it's being transferred from the module where the type was defined
   11720              :    -- we're not trying to transfer a whole assumed size array.  */
   11721              : 
   11722              : static void
   11723        46546 : resolve_transfer (gfc_code *code)
   11724              : {
   11725        46546 :   gfc_symbol *sym, *derived;
   11726        46546 :   gfc_ref *ref;
   11727        46546 :   gfc_expr *exp;
   11728        46546 :   bool write = false;
   11729        46546 :   bool formatted = false;
   11730        46546 :   gfc_dt *dt = code->ext.dt;
   11731        46546 :   gfc_symbol *dtio_sub = NULL;
   11732              : 
   11733        46546 :   exp = code->expr1;
   11734              : 
   11735        93098 :   while (exp != NULL && exp->expr_type == EXPR_OP
   11736        47461 :          && exp->value.op.op == INTRINSIC_PARENTHESES)
   11737            6 :     exp = exp->value.op.op1;
   11738              : 
   11739        46546 :   if (exp && exp->expr_type == EXPR_NULL
   11740            2 :       && code->ext.dt)
   11741              :     {
   11742            2 :       gfc_error ("Invalid context for NULL () intrinsic at %L",
   11743              :                  &exp->where);
   11744            2 :       return;
   11745              :     }
   11746              : 
   11747              :   if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
   11748              :                       && exp->expr_type != EXPR_FUNCTION
   11749              :                       && exp->expr_type != EXPR_ARRAY
   11750              :                       && exp->expr_type != EXPR_STRUCTURE))
   11751              :     return;
   11752              : 
   11753              :   /* If we are reading, the variable will be changed.  Note that
   11754              :      code->ext.dt may be NULL if the TRANSFER is related to
   11755              :      an INQUIRE statement -- but in this case, we are not reading, either.  */
   11756        25346 :   if (dt && dt->dt_io_kind->value.iokind == M_READ
   11757        32816 :       && !gfc_check_vardef_context (exp, false, false, false,
   11758         7322 :                                     _("item in READ")))
   11759              :     return;
   11760              : 
   11761        25490 :   const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
   11762        25490 :                         || exp->expr_type == EXPR_FUNCTION
   11763        21115 :                         || exp->expr_type == EXPR_ARRAY
   11764        46605 :                          ? &exp->ts : &exp->symtree->n.sym->ts;
   11765              : 
   11766              :   /* Go to actual component transferred.  */
   11767        33257 :   for (ref = exp->ref; ref; ref = ref->next)
   11768         7767 :     if (ref->type == REF_COMPONENT)
   11769         2208 :       ts = &ref->u.c.component->ts;
   11770              : 
   11771        25490 :   if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
   11772        25342 :       && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
   11773              :     {
   11774          720 :       derived = ts->u.derived;
   11775              : 
   11776              :       /* Determine when to use the formatted DTIO procedure.  */
   11777          720 :       if (dt && (dt->format_expr || dt->format_label))
   11778          645 :         formatted = true;
   11779              : 
   11780          720 :       write = dt->dt_io_kind->value.iokind == M_WRITE
   11781          720 :               || dt->dt_io_kind->value.iokind == M_PRINT;
   11782          720 :       dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
   11783              : 
   11784          720 :       if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
   11785              :         {
   11786          450 :           dt->udtio = exp;
   11787          450 :           sym = exp->symtree->n.sym->ns->proc_name;
   11788              :           /* Check to see if this is a nested DTIO call, with the
   11789              :              dummy as the io-list object.  */
   11790          450 :           if (sym && sym == dtio_sub && sym->formal
   11791           30 :               && sym->formal->sym == exp->symtree->n.sym
   11792           30 :               && exp->ref == NULL)
   11793              :             {
   11794            0 :               if (!sym->attr.recursive)
   11795              :                 {
   11796            0 :                   gfc_error ("DTIO %s procedure at %L must be recursive",
   11797              :                              sym->name, &sym->declared_at);
   11798            0 :                   return;
   11799              :                 }
   11800              :             }
   11801              :         }
   11802              :     }
   11803              : 
   11804        25490 :   if (ts->type == BT_CLASS && dtio_sub == NULL)
   11805              :     {
   11806            3 :       gfc_error ("Data transfer element at %L cannot be polymorphic unless "
   11807              :                 "it is processed by a defined input/output procedure",
   11808              :                 &code->loc);
   11809            3 :       return;
   11810              :     }
   11811              : 
   11812        25487 :   if (ts->type == BT_DERIVED)
   11813              :     {
   11814              :       /* Check that transferred derived type doesn't contain POINTER
   11815              :          components unless it is processed by a defined input/output
   11816              :          procedure".  */
   11817          688 :       if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
   11818              :         {
   11819            2 :           gfc_error ("Data transfer element at %L cannot have POINTER "
   11820              :                      "components unless it is processed by a defined "
   11821              :                      "input/output procedure", &code->loc);
   11822            2 :           return;
   11823              :         }
   11824              : 
   11825              :       /* F08:C935.  */
   11826          686 :       if (ts->u.derived->attr.proc_pointer_comp)
   11827              :         {
   11828            2 :           gfc_error ("Data transfer element at %L cannot have "
   11829              :                      "procedure pointer components", &code->loc);
   11830            2 :           return;
   11831              :         }
   11832              : 
   11833          684 :       if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
   11834              :         {
   11835            6 :           gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
   11836              :                      "components unless it is processed by a defined "
   11837              :                      "input/output procedure", &code->loc);
   11838            6 :           return;
   11839              :         }
   11840              : 
   11841              :       /* C_PTR and C_FUNPTR have private components which means they cannot
   11842              :          be printed.  However, if -std=gnu and not -pedantic, allow
   11843              :          the component to be printed to help debugging.  */
   11844          678 :       if (ts->u.derived->ts.f90_type == BT_VOID)
   11845              :         {
   11846            4 :           gfc_error ("Data transfer element at %L "
   11847              :                      "cannot have PRIVATE components", &code->loc);
   11848            4 :             return;
   11849              :         }
   11850          674 :       else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
   11851              :         {
   11852            4 :           gfc_error ("Data transfer element at %L cannot have "
   11853              :                      "PRIVATE components unless it is processed by "
   11854              :                      "a defined input/output procedure", &code->loc);
   11855            4 :           return;
   11856              :         }
   11857              :     }
   11858              : 
   11859        25469 :   if (exp->expr_type == EXPR_STRUCTURE)
   11860              :     return;
   11861              : 
   11862        25424 :   if (exp->expr_type == EXPR_ARRAY)
   11863              :     return;
   11864              : 
   11865        25048 :   sym = exp->symtree->n.sym;
   11866              : 
   11867        25048 :   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
   11868           81 :       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
   11869              :     {
   11870            1 :       gfc_error ("Data transfer element at %L cannot be a full reference to "
   11871              :                  "an assumed-size array", &code->loc);
   11872            1 :       return;
   11873              :     }
   11874              : }
   11875              : 
   11876              : 
   11877              : /*********** Toplevel code resolution subroutines ***********/
   11878              : 
   11879              : /* Find the set of labels that are reachable from this block.  We also
   11880              :    record the last statement in each block.  */
   11881              : 
   11882              : static void
   11883       676100 : find_reachable_labels (gfc_code *block)
   11884              : {
   11885       676100 :   gfc_code *c;
   11886              : 
   11887       676100 :   if (!block)
   11888              :     return;
   11889              : 
   11890       424028 :   cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
   11891              : 
   11892              :   /* Collect labels in this block.  We don't keep those corresponding
   11893              :      to END {IF|SELECT}, these are checked in resolve_branch by going
   11894              :      up through the code_stack.  */
   11895      1555945 :   for (c = block; c; c = c->next)
   11896              :     {
   11897      1131917 :       if (c->here && c->op != EXEC_END_NESTED_BLOCK)
   11898         3661 :         bitmap_set_bit (cs_base->reachable_labels, c->here->value);
   11899              :     }
   11900              : 
   11901              :   /* Merge with labels from parent block.  */
   11902       424028 :   if (cs_base->prev)
   11903              :     {
   11904       348261 :       gcc_assert (cs_base->prev->reachable_labels);
   11905       348261 :       bitmap_ior_into (cs_base->reachable_labels,
   11906              :                        cs_base->prev->reachable_labels);
   11907              :     }
   11908              : }
   11909              : 
   11910              : static void
   11911          197 : resolve_lock_unlock_event (gfc_code *code)
   11912              : {
   11913          197 :   if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
   11914          197 :       && (code->expr1->ts.type != BT_DERIVED
   11915          137 :           || code->expr1->expr_type != EXPR_VARIABLE
   11916          137 :           || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11917          136 :           || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
   11918          136 :           || code->expr1->rank != 0
   11919          181 :           || (!gfc_is_coarray (code->expr1) &&
   11920           46 :               !gfc_is_coindexed (code->expr1))))
   11921            4 :     gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
   11922            4 :                &code->expr1->where);
   11923          193 :   else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
   11924           58 :            && (code->expr1->ts.type != BT_DERIVED
   11925           58 :                || code->expr1->expr_type != EXPR_VARIABLE
   11926           58 :                || code->expr1->ts.u.derived->from_intmod
   11927              :                   != INTMOD_ISO_FORTRAN_ENV
   11928           58 :                || code->expr1->ts.u.derived->intmod_sym_id
   11929              :                   != ISOFORTRAN_EVENT_TYPE
   11930           58 :                || code->expr1->rank != 0))
   11931            0 :     gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
   11932              :                &code->expr1->where);
   11933           34 :   else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
   11934          209 :            && !gfc_is_coindexed (code->expr1))
   11935            0 :     gfc_error ("Event variable argument at %L must be a coarray or coindexed",
   11936            0 :                &code->expr1->where);
   11937          193 :   else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
   11938            0 :     gfc_error ("Event variable argument at %L must be a coarray but not "
   11939            0 :                "coindexed", &code->expr1->where);
   11940              : 
   11941              :   /* Check STAT.  */
   11942          197 :   if (code->expr2
   11943           54 :       && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
   11944           54 :           || code->expr2->expr_type != EXPR_VARIABLE))
   11945            0 :     gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   11946              :                &code->expr2->where);
   11947              : 
   11948          197 :   if (code->expr2
   11949          251 :       && !gfc_check_vardef_context (code->expr2, false, false, false,
   11950           54 :                                     _("STAT variable")))
   11951              :     return;
   11952              : 
   11953              :   /* Check ERRMSG.  */
   11954          197 :   if (code->expr3
   11955            2 :       && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
   11956            2 :           || code->expr3->expr_type != EXPR_VARIABLE))
   11957            0 :     gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   11958              :                &code->expr3->where);
   11959              : 
   11960          197 :   if (code->expr3
   11961          199 :       && !gfc_check_vardef_context (code->expr3, false, false, false,
   11962            2 :                                     _("ERRMSG variable")))
   11963              :     return;
   11964              : 
   11965              :   /* Check for LOCK the ACQUIRED_LOCK.  */
   11966          197 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11967           22 :       && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
   11968           22 :           || code->expr4->expr_type != EXPR_VARIABLE))
   11969            0 :     gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
   11970              :                "variable", &code->expr4->where);
   11971              : 
   11972          173 :   if (code->op != EXEC_EVENT_WAIT && code->expr4
   11973          219 :       && !gfc_check_vardef_context (code->expr4, false, false, false,
   11974           22 :                                     _("ACQUIRED_LOCK variable")))
   11975              :     return;
   11976              : 
   11977              :   /* Check for EVENT WAIT the UNTIL_COUNT.  */
   11978          197 :   if (code->op == EXEC_EVENT_WAIT && code->expr4)
   11979              :     {
   11980           36 :       if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
   11981           36 :           || code->expr4->rank != 0)
   11982            0 :         gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
   11983            0 :                    "expression", &code->expr4->where);
   11984              :     }
   11985              : }
   11986              : 
   11987              : static void
   11988          246 : resolve_team_argument (gfc_expr *team)
   11989              : {
   11990          246 :   gfc_resolve_expr (team);
   11991          246 :   if (team->rank != 0 || team->ts.type != BT_DERIVED
   11992          239 :       || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
   11993          239 :       || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
   11994              :     {
   11995            7 :       gfc_error ("TEAM argument at %L must be a scalar expression "
   11996              :                  "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
   11997              :                  &team->where);
   11998              :     }
   11999          246 : }
   12000              : 
   12001              : static void
   12002         1358 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
   12003              :                                 gfc_expr *e)
   12004              : {
   12005         1358 :   gfc_resolve_expr (e);
   12006         1358 :   if (e
   12007          139 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
   12008          124 :           || e->expr_type != EXPR_VARIABLE))
   12009           15 :     gfc_error ("%s argument at %L must be a scalar %s variable of at least "
   12010              :                "kind %d", name, &e->where, gfc_basic_typename (exp_type),
   12011              :                exp_kind);
   12012         1358 : }
   12013              : 
   12014              : void
   12015          679 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
   12016              : {
   12017          679 :   resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
   12018          679 :   resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
   12019              :                                   gfc_default_character_kind,
   12020              :                                   sync_stat->errmsg);
   12021          679 : }
   12022              : 
   12023              : static void
   12024          260 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
   12025              :                          gfc_expr *e)
   12026              : {
   12027          260 :   gfc_resolve_expr (e);
   12028          260 :   if (e
   12029          161 :       && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
   12030            3 :     gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
   12031              :                name, &e->where, gfc_basic_typename (exp_type), exp_kind);
   12032          260 : }
   12033              : 
   12034              : static void
   12035          130 : resolve_form_team (gfc_code *code)
   12036              : {
   12037          130 :   resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
   12038              :                            code->expr1);
   12039          130 :   resolve_team_argument (code->expr2);
   12040          130 :   resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
   12041              :                            code->expr3);
   12042          130 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12043          130 : }
   12044              : 
   12045              : static void resolve_block_construct (gfc_code *);
   12046              : 
   12047              : static void
   12048           73 : resolve_change_team (gfc_code *code)
   12049              : {
   12050           73 :   resolve_team_argument (code->expr1);
   12051           73 :   gfc_resolve_sync_stat (&code->ext.block.sync_stat);
   12052          146 :   resolve_block_construct (code);
   12053              :   /* Map the coarray bounds as selected.  */
   12054           76 :   for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
   12055            3 :     if (a->ar)
   12056              :       {
   12057            3 :         gfc_array_spec *src = a->ar->as, *dst;
   12058            3 :         if (a->st->n.sym->ts.type == BT_CLASS)
   12059            0 :           dst = CLASS_DATA (a->st->n.sym)->as;
   12060              :         else
   12061            3 :           dst = a->st->n.sym->as;
   12062            3 :         dst->corank = src->corank;
   12063            3 :         dst->cotype = src->cotype;
   12064            6 :         for (int i = 0; i < src->corank; ++i)
   12065              :           {
   12066            3 :             dst->lower[dst->rank + i] = src->lower[i];
   12067            3 :             dst->upper[dst->rank + i] = src->upper[i];
   12068            3 :             src->lower[i] = src->upper[i] = nullptr;
   12069              :           }
   12070            3 :         gfc_free_array_spec (src);
   12071            3 :         free (a->ar);
   12072            3 :         a->ar = nullptr;
   12073            3 :         dst->resolved = false;
   12074            3 :         gfc_resolve_array_spec (dst, 0);
   12075              :       }
   12076           73 : }
   12077              : 
   12078              : static void
   12079           43 : resolve_sync_team (gfc_code *code)
   12080              : {
   12081           43 :   resolve_team_argument (code->expr1);
   12082           43 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12083           43 : }
   12084              : 
   12085              : static void
   12086           71 : resolve_end_team (gfc_code *code)
   12087              : {
   12088           71 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12089           71 : }
   12090              : 
   12091              : static void
   12092           54 : resolve_critical (gfc_code *code)
   12093              : {
   12094           54 :   gfc_symtree *symtree;
   12095           54 :   gfc_symbol *lock_type;
   12096           54 :   char name[GFC_MAX_SYMBOL_LEN];
   12097           54 :   static int serial = 0;
   12098              : 
   12099           54 :   gfc_resolve_sync_stat (&code->ext.sync_stat);
   12100              : 
   12101           54 :   if (flag_coarray != GFC_FCOARRAY_LIB)
   12102           30 :     return;
   12103              : 
   12104           24 :   symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   12105              :                               GFC_PREFIX ("lock_type"));
   12106           24 :   if (symtree)
   12107           12 :     lock_type = symtree->n.sym;
   12108              :   else
   12109              :     {
   12110           12 :       if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
   12111              :                             false) != 0)
   12112            0 :         gcc_unreachable ();
   12113           12 :       lock_type = symtree->n.sym;
   12114           12 :       lock_type->attr.flavor = FL_DERIVED;
   12115           12 :       lock_type->attr.zero_comp = 1;
   12116           12 :       lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
   12117           12 :       lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
   12118              :     }
   12119              : 
   12120           24 :   sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
   12121           24 :   if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
   12122            0 :     gcc_unreachable ();
   12123              : 
   12124           24 :   code->resolved_sym = symtree->n.sym;
   12125           24 :   symtree->n.sym->attr.flavor = FL_VARIABLE;
   12126           24 :   symtree->n.sym->attr.referenced = 1;
   12127           24 :   symtree->n.sym->attr.artificial = 1;
   12128           24 :   symtree->n.sym->attr.codimension = 1;
   12129           24 :   symtree->n.sym->ts.type = BT_DERIVED;
   12130           24 :   symtree->n.sym->ts.u.derived = lock_type;
   12131           24 :   symtree->n.sym->as = gfc_get_array_spec ();
   12132           24 :   symtree->n.sym->as->corank = 1;
   12133           24 :   symtree->n.sym->as->type = AS_EXPLICIT;
   12134           24 :   symtree->n.sym->as->cotype = AS_EXPLICIT;
   12135           24 :   symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
   12136              :                                                    NULL, 1);
   12137           24 :   gfc_commit_symbols();
   12138              : }
   12139              : 
   12140              : 
   12141              : static void
   12142         1316 : resolve_sync (gfc_code *code)
   12143              : {
   12144              :   /* Check imageset. The * case matches expr1 == NULL.  */
   12145         1316 :   if (code->expr1)
   12146              :     {
   12147           71 :       if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
   12148            1 :         gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
   12149              :                    "INTEGER expression", &code->expr1->where);
   12150           71 :       if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
   12151           27 :           && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
   12152            1 :         gfc_error ("Imageset argument at %L must between 1 and num_images()",
   12153              :                    &code->expr1->where);
   12154           70 :       else if (code->expr1->expr_type == EXPR_ARRAY
   12155           70 :                && gfc_simplify_expr (code->expr1, 0))
   12156              :         {
   12157           20 :            gfc_constructor *cons;
   12158           20 :            cons = gfc_constructor_first (code->expr1->value.constructor);
   12159           60 :            for (; cons; cons = gfc_constructor_next (cons))
   12160           20 :              if (cons->expr->expr_type == EXPR_CONSTANT
   12161           20 :                  &&  mpz_cmp_si (cons->expr->value.integer, 1) < 0)
   12162            0 :                gfc_error ("Imageset argument at %L must between 1 and "
   12163              :                           "num_images()", &cons->expr->where);
   12164              :         }
   12165              :     }
   12166              : 
   12167              :   /* Check STAT.  */
   12168         1316 :   gfc_resolve_expr (code->expr2);
   12169         1316 :   if (code->expr2)
   12170              :     {
   12171          108 :       if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
   12172            1 :         gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
   12173              :                    &code->expr2->where);
   12174              :       else
   12175          107 :         gfc_check_vardef_context (code->expr2, false, false, false,
   12176          107 :                                   _("STAT variable"));
   12177              :     }
   12178              : 
   12179              :   /* Check ERRMSG.  */
   12180         1316 :   gfc_resolve_expr (code->expr3);
   12181         1316 :   if (code->expr3)
   12182              :     {
   12183           90 :       if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
   12184            4 :         gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
   12185              :                    &code->expr3->where);
   12186              :       else
   12187           86 :         gfc_check_vardef_context (code->expr3, false, false, false,
   12188           86 :                                   _("ERRMSG variable"));
   12189              :     }
   12190         1316 : }
   12191              : 
   12192              : 
   12193              : /* Given a branch to a label, see if the branch is conforming.
   12194              :    The code node describes where the branch is located.  */
   12195              : 
   12196              : static void
   12197       108727 : resolve_branch (gfc_st_label *label, gfc_code *code)
   12198              : {
   12199       108727 :   code_stack *stack;
   12200              : 
   12201       108727 :   if (label == NULL)
   12202              :     return;
   12203              : 
   12204              :   /* Step one: is this a valid branching target?  */
   12205              : 
   12206         2460 :   if (label->defined == ST_LABEL_UNKNOWN)
   12207              :     {
   12208            4 :       gfc_error ("Label %d referenced at %L is never defined", label->value,
   12209              :                  &code->loc);
   12210            4 :       return;
   12211              :     }
   12212              : 
   12213         2456 :   if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
   12214              :     {
   12215            4 :       gfc_error ("Statement at %L is not a valid branch target statement "
   12216              :                  "for the branch statement at %L", &label->where, &code->loc);
   12217            4 :       return;
   12218              :     }
   12219              : 
   12220              :   /* Step two: make sure this branch is not a branch to itself ;-)  */
   12221              : 
   12222         2452 :   if (code->here == label)
   12223              :     {
   12224            0 :       gfc_warning (0, "Branch at %L may result in an infinite loop",
   12225              :                    &code->loc);
   12226            0 :       return;
   12227              :     }
   12228              : 
   12229              :   /* Step three:  See if the label is in the same block as the
   12230              :      branching statement.  The hard work has been done by setting up
   12231              :      the bitmap reachable_labels.  */
   12232              : 
   12233         2452 :   if (bitmap_bit_p (cs_base->reachable_labels, label->value))
   12234              :     {
   12235              :       /* Check now whether there is a CRITICAL construct; if so, check
   12236              :          whether the label is still visible outside of the CRITICAL block,
   12237              :          which is invalid.  */
   12238         6267 :       for (stack = cs_base; stack; stack = stack->prev)
   12239              :         {
   12240         3883 :           if (stack->current->op == EXEC_CRITICAL
   12241         3883 :               && bitmap_bit_p (stack->reachable_labels, label->value))
   12242            2 :             gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
   12243              :                       "label at %L", &code->loc, &label->where);
   12244         3881 :           else if (stack->current->op == EXEC_DO_CONCURRENT
   12245         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12246            0 :             gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
   12247              :                       "for label at %L", &code->loc, &label->where);
   12248         3881 :           else if (stack->current->op == EXEC_CHANGE_TEAM
   12249         3881 :                    && bitmap_bit_p (stack->reachable_labels, label->value))
   12250            1 :             gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
   12251              :                       "for label at %L", &code->loc, &label->where);
   12252              :         }
   12253              : 
   12254              :       return;
   12255              :     }
   12256              : 
   12257              :   /* Step four:  If we haven't found the label in the bitmap, it may
   12258              :     still be the label of the END of the enclosing block, in which
   12259              :     case we find it by going up the code_stack.  */
   12260              : 
   12261          167 :   for (stack = cs_base; stack; stack = stack->prev)
   12262              :     {
   12263          131 :       if (stack->current->next && stack->current->next->here == label)
   12264              :         break;
   12265          101 :       if (stack->current->op == EXEC_CRITICAL)
   12266              :         {
   12267              :           /* Note: A label at END CRITICAL does not leave the CRITICAL
   12268              :              construct as END CRITICAL is still part of it.  */
   12269            2 :           gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
   12270              :                       " at %L", &code->loc, &label->where);
   12271            2 :           return;
   12272              :         }
   12273           99 :       else if (stack->current->op == EXEC_DO_CONCURRENT)
   12274              :         {
   12275            0 :           gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
   12276              :                      "label at %L", &code->loc, &label->where);
   12277            0 :           return;
   12278              :         }
   12279              :     }
   12280              : 
   12281           66 :   if (stack)
   12282              :     {
   12283           30 :       gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
   12284              :       return;
   12285              :     }
   12286              : 
   12287              :   /* The label is not in an enclosing block, so illegal.  This was
   12288              :      allowed in Fortran 66, so we allow it as extension.  No
   12289              :      further checks are necessary in this case.  */
   12290           36 :   gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
   12291              :                   "as the GOTO statement at %L", &label->where,
   12292              :                   &code->loc);
   12293           36 :   return;
   12294              : }
   12295              : 
   12296              : 
   12297              : /* Check whether EXPR1 has the same shape as EXPR2.  */
   12298              : 
   12299              : static bool
   12300         1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
   12301              : {
   12302         1467 :   mpz_t shape[GFC_MAX_DIMENSIONS];
   12303         1467 :   mpz_t shape2[GFC_MAX_DIMENSIONS];
   12304         1467 :   bool result = false;
   12305         1467 :   int i;
   12306              : 
   12307              :   /* Compare the rank.  */
   12308         1467 :   if (expr1->rank != expr2->rank)
   12309              :     return result;
   12310              : 
   12311              :   /* Compare the size of each dimension.  */
   12312         2811 :   for (i=0; i<expr1->rank; i++)
   12313              :     {
   12314         1495 :       if (!gfc_array_dimen_size (expr1, i, &shape[i]))
   12315          151 :         goto ignore;
   12316              : 
   12317         1344 :       if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
   12318            0 :         goto ignore;
   12319              : 
   12320         1344 :       if (mpz_cmp (shape[i], shape2[i]))
   12321            0 :         goto over;
   12322              :     }
   12323              : 
   12324              :   /* When either of the two expression is an assumed size array, we
   12325              :      ignore the comparison of dimension sizes.  */
   12326         1316 : ignore:
   12327              :   result = true;
   12328              : 
   12329         1467 : over:
   12330         1467 :   gfc_clear_shape (shape, i);
   12331         1467 :   gfc_clear_shape (shape2, i);
   12332         1467 :   return result;
   12333              : }
   12334              : 
   12335              : 
   12336              : /* Check whether a WHERE assignment target or a WHERE mask expression
   12337              :    has the same shape as the outermost WHERE mask expression.  */
   12338              : 
   12339              : static void
   12340          509 : resolve_where (gfc_code *code, gfc_expr *mask)
   12341              : {
   12342          509 :   gfc_code *cblock;
   12343          509 :   gfc_code *cnext;
   12344          509 :   gfc_expr *e = NULL;
   12345              : 
   12346          509 :   cblock = code->block;
   12347              : 
   12348              :   /* Store the first WHERE mask-expr of the WHERE statement or construct.
   12349              :      In case of nested WHERE, only the outermost one is stored.  */
   12350          509 :   if (mask == NULL) /* outermost WHERE */
   12351          453 :     e = cblock->expr1;
   12352              :   else /* inner WHERE */
   12353          509 :     e = mask;
   12354              : 
   12355         1387 :   while (cblock)
   12356              :     {
   12357          878 :       if (cblock->expr1)
   12358              :         {
   12359              :           /* Check if the mask-expr has a consistent shape with the
   12360              :              outermost WHERE mask-expr.  */
   12361          714 :           if (!resolve_where_shape (cblock->expr1, e))
   12362            0 :             gfc_error ("WHERE mask at %L has inconsistent shape",
   12363            0 :                        &cblock->expr1->where);
   12364              :          }
   12365              : 
   12366              :       /* the assignment statement of a WHERE statement, or the first
   12367              :          statement in where-body-construct of a WHERE construct */
   12368          878 :       cnext = cblock->next;
   12369         1733 :       while (cnext)
   12370              :         {
   12371          855 :           switch (cnext->op)
   12372              :             {
   12373              :             /* WHERE assignment statement */
   12374          753 :             case EXEC_ASSIGN:
   12375              : 
   12376              :               /* Check shape consistent for WHERE assignment target.  */
   12377          753 :               if (e && !resolve_where_shape (cnext->expr1, e))
   12378            0 :                gfc_error ("WHERE assignment target at %L has "
   12379            0 :                           "inconsistent shape", &cnext->expr1->where);
   12380              : 
   12381          753 :               if (cnext->op == EXEC_ASSIGN
   12382          753 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12383            0 :                 cnext->expr1->must_finalize = 1;
   12384              : 
   12385              :               break;
   12386              : 
   12387              : 
   12388           46 :             case EXEC_ASSIGN_CALL:
   12389           46 :               resolve_call (cnext);
   12390           46 :               if (!cnext->resolved_sym->attr.elemental)
   12391            2 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12392            2 :                           &cnext->ext.actual->expr->where);
   12393              :               break;
   12394              : 
   12395              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12396           56 :             case EXEC_WHERE:
   12397           56 :               resolve_where (cnext, e);
   12398           56 :               break;
   12399              : 
   12400            0 :             default:
   12401            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12402              :                          &cnext->loc);
   12403              :             }
   12404              :          /* the next statement within the same where-body-construct */
   12405          855 :          cnext = cnext->next;
   12406              :        }
   12407              :     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12408          878 :     cblock = cblock->block;
   12409              :   }
   12410          509 : }
   12411              : 
   12412              : 
   12413              : /* Resolve assignment in FORALL construct.
   12414              :    NVAR is the number of FORALL index variables, and VAR_EXPR records the
   12415              :    FORALL index variables.  */
   12416              : 
   12417              : static void
   12418         2375 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
   12419              : {
   12420         2375 :   int n;
   12421         2375 :   gfc_symbol *forall_index;
   12422              : 
   12423         6771 :   for (n = 0; n < nvar; n++)
   12424              :     {
   12425         4396 :       forall_index = var_expr[n]->symtree->n.sym;
   12426              : 
   12427              :       /* Check whether the assignment target is one of the FORALL index
   12428              :          variable.  */
   12429         4396 :       if ((code->expr1->expr_type == EXPR_VARIABLE)
   12430         4396 :           && (code->expr1->symtree->n.sym == forall_index))
   12431            0 :         gfc_error ("Assignment to a FORALL index variable at %L",
   12432              :                    &code->expr1->where);
   12433              :       else
   12434              :         {
   12435              :           /* If one of the FORALL index variables doesn't appear in the
   12436              :              assignment variable, then there could be a many-to-one
   12437              :              assignment.  Emit a warning rather than an error because the
   12438              :              mask could be resolving this problem.
   12439              :              DO NOT emit this warning for DO CONCURRENT - reduction-like
   12440              :              many-to-one assignments are semantically valid (formalized with
   12441              :              the REDUCE locality-spec in Fortran 2023).  */
   12442         4396 :           if (!find_forall_index (code->expr1, forall_index, 0)
   12443         4396 :               && !gfc_do_concurrent_flag)
   12444            0 :             gfc_warning (0, "The FORALL with index %qs is not used on the "
   12445              :                          "left side of the assignment at %L and so might "
   12446              :                          "cause multiple assignment to this object",
   12447            0 :                          var_expr[n]->symtree->name, &code->expr1->where);
   12448              :         }
   12449              :     }
   12450         2375 : }
   12451              : 
   12452              : 
   12453              : /* Resolve WHERE statement in FORALL construct.  */
   12454              : 
   12455              : static void
   12456           47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
   12457              :                                   gfc_expr **var_expr)
   12458              : {
   12459           47 :   gfc_code *cblock;
   12460           47 :   gfc_code *cnext;
   12461              : 
   12462           47 :   cblock = code->block;
   12463          113 :   while (cblock)
   12464              :     {
   12465              :       /* the assignment statement of a WHERE statement, or the first
   12466              :          statement in where-body-construct of a WHERE construct */
   12467           66 :       cnext = cblock->next;
   12468          132 :       while (cnext)
   12469              :         {
   12470           66 :           switch (cnext->op)
   12471              :             {
   12472              :             /* WHERE assignment statement */
   12473           66 :             case EXEC_ASSIGN:
   12474           66 :               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
   12475              : 
   12476           66 :               if (cnext->op == EXEC_ASSIGN
   12477           66 :                   && gfc_may_be_finalized (cnext->expr1->ts))
   12478            0 :                 cnext->expr1->must_finalize = 1;
   12479              : 
   12480              :               break;
   12481              : 
   12482              :             /* WHERE operator assignment statement */
   12483            0 :             case EXEC_ASSIGN_CALL:
   12484            0 :               resolve_call (cnext);
   12485            0 :               if (!cnext->resolved_sym->attr.elemental)
   12486            0 :                 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
   12487            0 :                           &cnext->ext.actual->expr->where);
   12488              :               break;
   12489              : 
   12490              :             /* WHERE or WHERE construct is part of a where-body-construct */
   12491            0 :             case EXEC_WHERE:
   12492            0 :               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
   12493            0 :               break;
   12494              : 
   12495            0 :             default:
   12496            0 :               gfc_error ("Unsupported statement inside WHERE at %L",
   12497              :                          &cnext->loc);
   12498              :             }
   12499              :           /* the next statement within the same where-body-construct */
   12500           66 :           cnext = cnext->next;
   12501              :         }
   12502              :       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
   12503           66 :       cblock = cblock->block;
   12504              :     }
   12505           47 : }
   12506              : 
   12507              : 
   12508              : /* Traverse the FORALL body to check whether the following errors exist:
   12509              :    1. For assignment, check if a many-to-one assignment happens.
   12510              :    2. For WHERE statement, check the WHERE body to see if there is any
   12511              :       many-to-one assignment.  */
   12512              : 
   12513              : static void
   12514         2202 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
   12515              : {
   12516         2202 :   gfc_code *c;
   12517              : 
   12518         2202 :   c = code->block->next;
   12519         4827 :   while (c)
   12520              :     {
   12521         2625 :       switch (c->op)
   12522              :         {
   12523         2309 :         case EXEC_ASSIGN:
   12524         2309 :         case EXEC_POINTER_ASSIGN:
   12525         2309 :           gfc_resolve_assign_in_forall (c, nvar, var_expr);
   12526              : 
   12527         2309 :           if (c->op == EXEC_ASSIGN
   12528         2309 :               && gfc_may_be_finalized (c->expr1->ts))
   12529            0 :             c->expr1->must_finalize = 1;
   12530              : 
   12531              :           break;
   12532              : 
   12533            0 :         case EXEC_ASSIGN_CALL:
   12534            0 :           resolve_call (c);
   12535            0 :           break;
   12536              : 
   12537              :         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
   12538              :            there is no need to handle it here.  */
   12539              :         case EXEC_FORALL:
   12540              :           break;
   12541           47 :         case EXEC_WHERE:
   12542           47 :           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
   12543           47 :           break;
   12544              :         default:
   12545              :           break;
   12546              :         }
   12547              :       /* The next statement in the FORALL body.  */
   12548         2625 :       c = c->next;
   12549              :     }
   12550         2202 : }
   12551              : 
   12552              : 
   12553              : /* Counts the number of iterators needed inside a forall construct, including
   12554              :    nested forall constructs. This is used to allocate the needed memory
   12555              :    in gfc_resolve_forall.  */
   12556              : 
   12557              : static int gfc_count_forall_iterators (gfc_code *code);
   12558              : 
   12559              : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
   12560              :    next-chain, descending into block arms such as IF/ELSE branches.  */
   12561              : 
   12562              : static int
   12563         2387 : gfc_max_forall_iterators_in_chain (gfc_code *code)
   12564              : {
   12565         2387 :   int max_iters = 0;
   12566              : 
   12567         5226 :   for (gfc_code *c = code; c; c = c->next)
   12568              :     {
   12569         2839 :       int sub_iters = 0;
   12570              : 
   12571         2839 :       if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
   12572           94 :         sub_iters = gfc_count_forall_iterators (c);
   12573         2745 :       else if (c->op == EXEC_BLOCK)
   12574              :         {
   12575              :           /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
   12576              :              not in the generic c->block arm list used by IF/SELECT.  */
   12577           21 :           if (c->ext.block.ns && c->ext.block.ns->code)
   12578           21 :             sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
   12579              :         }
   12580         2724 :       else if (c->block)
   12581          307 :         for (gfc_code *b = c->block; b; b = b->block)
   12582              :           {
   12583          164 :             int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
   12584          164 :             if (arm_iters > sub_iters)
   12585              :               sub_iters = arm_iters;
   12586              :           }
   12587              : 
   12588         2839 :       if (sub_iters > max_iters)
   12589              :         max_iters = sub_iters;
   12590              :     }
   12591              : 
   12592         2387 :   return max_iters;
   12593              : }
   12594              : 
   12595              : 
   12596              : static int
   12597         2202 : gfc_count_forall_iterators (gfc_code *code)
   12598              : {
   12599         2202 :   int current_iters = 0;
   12600         2202 :   gfc_forall_iterator *fa;
   12601              : 
   12602         2202 :   gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
   12603              : 
   12604         6320 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12605         4118 :     current_iters++;
   12606              : 
   12607         2202 :   return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
   12608              : }
   12609              : 
   12610              : 
   12611              : /* Given a FORALL construct.
   12612              :    1) Resolve the FORALL iterator.
   12613              :    2) Check for shadow index-name(s) and update code block.
   12614              :    3) call gfc_resolve_forall_body to resolve the FORALL body.  */
   12615              : 
   12616              : /* Custom recursive expression walker that replaces symbols.
   12617              :    This ensures we visit ALL expressions including those in array subscripts.  */
   12618              : 
   12619              : static void
   12620          114 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
   12621              : {
   12622          144 :   if (!expr)
   12623              :     return;
   12624              : 
   12625              :   /* Check if this is a variable reference to replace */
   12626          108 :   if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
   12627              :     {
   12628           18 :       expr->symtree = new_st;
   12629           18 :       expr->ts = new_st->n.sym->ts;
   12630              :     }
   12631              : 
   12632              :   /* Walk through reference chain (array subscripts, substrings, etc.) */
   12633          108 :   for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
   12634              :     {
   12635            0 :       if (ref->type == REF_ARRAY)
   12636              :         {
   12637              :           gfc_array_ref *ar = &ref->u.ar;
   12638            0 :           for (int i = 0; i < ar->dimen; i++)
   12639              :             {
   12640            0 :               replace_in_expr_recursive (ar->start[i], old_sym, new_st);
   12641            0 :               replace_in_expr_recursive (ar->end[i], old_sym, new_st);
   12642            0 :               replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
   12643              :             }
   12644              :         }
   12645            0 :       else if (ref->type == REF_SUBSTRING)
   12646              :         {
   12647            0 :           replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
   12648            0 :           replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
   12649              :         }
   12650              :     }
   12651              : 
   12652              :   /* Walk through sub-expressions based on expression type */
   12653          108 :   switch (expr->expr_type)
   12654              :     {
   12655           30 :     case EXPR_OP:
   12656           30 :       replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
   12657           30 :       replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
   12658           30 :       break;
   12659              : 
   12660            6 :     case EXPR_FUNCTION:
   12661           18 :       for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
   12662           12 :         replace_in_expr_recursive (a->expr, old_sym, new_st);
   12663              :       break;
   12664              : 
   12665            0 :     case EXPR_ARRAY:
   12666            0 :     case EXPR_STRUCTURE:
   12667            0 :       for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   12668            0 :            c; c = gfc_constructor_next (c))
   12669              :         {
   12670            0 :           replace_in_expr_recursive (c->expr, old_sym, new_st);
   12671            0 :           if (c->iterator)
   12672              :             {
   12673            0 :               replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
   12674            0 :               replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
   12675            0 :               replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
   12676              :             }
   12677              :         }
   12678              :       break;
   12679              : 
   12680              :     default:
   12681              :       break;
   12682              :     }
   12683              : }
   12684              : 
   12685              : 
   12686              : /* Walk code tree and replace all variable references */
   12687              : 
   12688              : static void
   12689           18 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
   12690              : {
   12691           18 :   if (!code)
   12692              :     return;
   12693              : 
   12694           36 :   for (gfc_code *c = code; c; c = c->next)
   12695              :     {
   12696              :       /* Replace in expressions associated with this code node */
   12697           18 :       replace_in_expr_recursive (c->expr1, old_sym, new_st);
   12698           18 :       replace_in_expr_recursive (c->expr2, old_sym, new_st);
   12699           18 :       replace_in_expr_recursive (c->expr3, old_sym, new_st);
   12700           18 :       replace_in_expr_recursive (c->expr4, old_sym, new_st);
   12701              : 
   12702              :       /* Handle special code types with additional expressions */
   12703           18 :       switch (c->op)
   12704              :         {
   12705            0 :         case EXEC_DO:
   12706            0 :           if (c->ext.iterator)
   12707              :             {
   12708            0 :               replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
   12709            0 :               replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
   12710            0 :               replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
   12711              :             }
   12712              :           break;
   12713              : 
   12714            0 :         case EXEC_CALL:
   12715            0 :         case EXEC_ASSIGN_CALL:
   12716            0 :           for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
   12717            0 :             replace_in_expr_recursive (a->expr, old_sym, new_st);
   12718              :           break;
   12719              : 
   12720            0 :         case EXEC_SELECT:
   12721            0 :           for (gfc_code *b = c->block; b; b = b->block)
   12722              :             {
   12723            0 :               for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
   12724              :                 {
   12725            0 :                   replace_in_expr_recursive (cp->low, old_sym, new_st);
   12726            0 :                   replace_in_expr_recursive (cp->high, old_sym, new_st);
   12727              :                 }
   12728            0 :               replace_in_code_recursive (b->next, old_sym, new_st);
   12729              :             }
   12730              :           break;
   12731              : 
   12732            0 :         case EXEC_FORALL:
   12733            0 :         case EXEC_DO_CONCURRENT:
   12734            0 :           for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
   12735              :             {
   12736            0 :               replace_in_expr_recursive (fa->start, old_sym, new_st);
   12737            0 :               replace_in_expr_recursive (fa->end, old_sym, new_st);
   12738            0 :               replace_in_expr_recursive (fa->stride, old_sym, new_st);
   12739              :             }
   12740              :           /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
   12741              :              they'll be handled separately */
   12742              :           break;
   12743              : 
   12744              :         default:
   12745              :           break;
   12746              :         }
   12747              : 
   12748              :       /* Recurse into blocks */
   12749           18 :       if (c->block)
   12750            0 :         replace_in_code_recursive (c->block->next, old_sym, new_st);
   12751              :     }
   12752              : }
   12753              : 
   12754              : 
   12755              : /* Replace all references to outer_sym with shadow_st in the given code.  */
   12756              : 
   12757              : static void
   12758           18 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
   12759              :                               gfc_symtree *shadow_st)
   12760              : {
   12761              :   /* Use custom recursive walker to ensure we visit ALL expressions */
   12762            0 :   replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
   12763           18 : }
   12764              : 
   12765              : 
   12766              : static void
   12767         2202 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
   12768              : {
   12769         2202 :   static gfc_expr **var_expr;
   12770         2202 :   static int total_var = 0;
   12771         2202 :   static int nvar = 0;
   12772         2202 :   int i, old_nvar, tmp;
   12773         2202 :   gfc_forall_iterator *fa;
   12774         2202 :   bool shadow = false;
   12775              : 
   12776         2202 :   old_nvar = nvar;
   12777              : 
   12778              :   /* Only warn about obsolescent FORALL, not DO CONCURRENT */
   12779         2202 :   if (code->op == EXEC_FORALL
   12780         2202 :       && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
   12781              :     return;
   12782              : 
   12783              :   /* Start to resolve a FORALL construct   */
   12784              :   /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
   12785              :      forall_save==0 means we're not nested in a FORALL in the current scope,
   12786              :      but nvar==0 ensures we're not nested in a parent scope either (prevents
   12787              :      double allocation when FORALL is nested inside DO CONCURRENT).  */
   12788         2202 :   if (forall_save == 0 && nvar == 0)
   12789              :     {
   12790              :       /* Count the total number of FORALL indices in the nested FORALL
   12791              :          construct in order to allocate the VAR_EXPR with proper size.  */
   12792         2108 :       total_var = gfc_count_forall_iterators (code);
   12793              : 
   12794              :       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
   12795         2108 :       var_expr = XCNEWVEC (gfc_expr *, total_var);
   12796              :     }
   12797              : 
   12798              :   /* The information about FORALL iterator, including FORALL indices start,
   12799              :      end and stride.  An outer FORALL indice cannot appear in start, end or
   12800              :      stride.  Check for a shadow index-name.  */
   12801         6320 :   for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12802              :     {
   12803              :       /* Fortran 2008: C738 (R753).  */
   12804         4118 :       if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
   12805              :         {
   12806            2 :           gfc_error ("FORALL index-name at %L must be a scalar variable "
   12807              :                      "of type integer", &fa->var->where);
   12808            2 :           continue;
   12809              :         }
   12810              : 
   12811              :       /* Check if any outer FORALL index name is the same as the current
   12812              :          one.  Skip this check if the iterator is a shadow variable (from
   12813              :          DO CONCURRENT type spec) which may not have a symtree yet.  */
   12814         7125 :       for (i = 0; i < nvar; i++)
   12815              :         {
   12816         3009 :           if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
   12817         3009 :               && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
   12818            0 :             gfc_error ("An outer FORALL construct already has an index "
   12819              :                         "with this name %L", &fa->var->where);
   12820              :         }
   12821              : 
   12822         4116 :       if (fa->shadow)
   12823           18 :         shadow = true;
   12824              : 
   12825              :       /* Record the current FORALL index.  */
   12826         4116 :       var_expr[nvar] = gfc_copy_expr (fa->var);
   12827              : 
   12828         4116 :       nvar++;
   12829              : 
   12830              :       /* No memory leak.  */
   12831         4116 :       gcc_assert (nvar <= total_var);
   12832              :     }
   12833              : 
   12834              :   /* Need to walk the code and replace references to the index-name with
   12835              :      references to the shadow index-name. This must be done BEFORE resolving
   12836              :      the body so that resolution uses the correct shadow variables.  */
   12837         2202 :   if (shadow)
   12838              :     {
   12839              :       /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables.  */
   12840           42 :       for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
   12841              :         {
   12842           24 :           if (fa->shadow)
   12843              :             {
   12844           18 :               gfc_symtree *shadow_st;
   12845           18 :               const char *shadow_name_str;
   12846           18 :               char *outer_name;
   12847              : 
   12848              :               /* fa->var now points to the shadow variable "_name".  */
   12849           18 :               shadow_name_str = fa->var->symtree->name;
   12850           18 :               shadow_st = fa->var->symtree;
   12851              : 
   12852           18 :               if (shadow_name_str[0] != '_')
   12853            0 :                 gfc_internal_error ("Expected shadow variable name to start with _");
   12854              : 
   12855           18 :               outer_name = (char *) alloca (strlen (shadow_name_str));
   12856           18 :               strcpy (outer_name, shadow_name_str + 1);
   12857              : 
   12858              :               /* Find the ITERATOR symbol in the current namespace.
   12859              :                  This is the local DO CONCURRENT variable that body expressions reference.  */
   12860           18 :               gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
   12861              : 
   12862           18 :               if (!iter_st)
   12863              :                 /* No iterator variable found - this shouldn't happen */
   12864            0 :                 continue;
   12865              : 
   12866           18 :               gfc_symbol *iter_sym = iter_st->n.sym;
   12867              : 
   12868              :               /* Walk the FORALL/DO CONCURRENT body and replace all references.  */
   12869           18 :               if (code->block && code->block->next)
   12870           18 :                 gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
   12871              :             }
   12872              :         }
   12873              :     }
   12874              : 
   12875              :   /* Resolve the FORALL body.  */
   12876         2202 :   gfc_resolve_forall_body (code, nvar, var_expr);
   12877              : 
   12878              :   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
   12879         2202 :   gfc_resolve_blocks (code->block, ns);
   12880              : 
   12881         2202 :   tmp = nvar;
   12882         2202 :   nvar = old_nvar;
   12883              :   /* Free only the VAR_EXPRs allocated in this frame.  */
   12884         6318 :   for (i = nvar; i < tmp; i++)
   12885         4116 :      gfc_free_expr (var_expr[i]);
   12886              : 
   12887         2202 :   if (nvar == 0)
   12888              :     {
   12889              :       /* We are in the outermost FORALL construct.  */
   12890         2108 :       gcc_assert (forall_save == 0);
   12891              : 
   12892              :       /* VAR_EXPR is not needed any more.  */
   12893         2108 :       free (var_expr);
   12894         2108 :       total_var = 0;
   12895              :     }
   12896              : }
   12897              : 
   12898              : 
   12899              : /* Resolve a BLOCK construct statement.  */
   12900              : 
   12901              : static void
   12902         8025 : resolve_block_construct (gfc_code* code)
   12903              : {
   12904         8025 :   gfc_namespace *ns = code->ext.block.ns;
   12905              : 
   12906              :   /* For an ASSOCIATE block, the associations (and their targets) will be
   12907              :      resolved by gfc_resolve_symbol, during resolution of the BLOCK's
   12908              :      namespace.  */
   12909         8025 :   gfc_resolve (ns);
   12910            0 : }
   12911              : 
   12912              : 
   12913              : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
   12914              :    DO code nodes.  */
   12915              : 
   12916              : void
   12917       330857 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
   12918              : {
   12919       330857 :   bool t;
   12920              : 
   12921       673182 :   for (; b; b = b->block)
   12922              :     {
   12923       342325 :       t = gfc_resolve_expr (b->expr1);
   12924       342325 :       if (!gfc_resolve_expr (b->expr2))
   12925            0 :         t = false;
   12926              : 
   12927       342325 :       switch (b->op)
   12928              :         {
   12929       236734 :         case EXEC_IF:
   12930       236734 :           if (t && b->expr1 != NULL
   12931       232416 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
   12932            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   12933              :                        &b->expr1->where);
   12934              :           break;
   12935              : 
   12936          764 :         case EXEC_WHERE:
   12937          764 :           if (t
   12938          764 :               && b->expr1 != NULL
   12939          631 :               && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
   12940            0 :             gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
   12941              :                        &b->expr1->where);
   12942              :           break;
   12943              : 
   12944           76 :         case EXEC_GOTO:
   12945           76 :           resolve_branch (b->label1, b);
   12946           76 :           break;
   12947              : 
   12948            0 :         case EXEC_BLOCK:
   12949            0 :           resolve_block_construct (b);
   12950            0 :           break;
   12951              : 
   12952              :         case EXEC_SELECT:
   12953              :         case EXEC_SELECT_TYPE:
   12954              :         case EXEC_SELECT_RANK:
   12955              :         case EXEC_FORALL:
   12956              :         case EXEC_DO:
   12957              :         case EXEC_DO_WHILE:
   12958              :         case EXEC_DO_CONCURRENT:
   12959              :         case EXEC_CRITICAL:
   12960              :         case EXEC_READ:
   12961              :         case EXEC_WRITE:
   12962              :         case EXEC_IOLENGTH:
   12963              :         case EXEC_WAIT:
   12964              :           break;
   12965              : 
   12966         2697 :         case EXEC_OMP_ATOMIC:
   12967         2697 :         case EXEC_OACC_ATOMIC:
   12968         2697 :           {
   12969              :             /* Verify this before calling gfc_resolve_code, which might
   12970              :                change it.  */
   12971         2697 :             gcc_assert (b->op == EXEC_OMP_ATOMIC
   12972              :                         || (b->next && b->next->op == EXEC_ASSIGN));
   12973              :           }
   12974              :           break;
   12975              : 
   12976              :         case EXEC_OACC_PARALLEL_LOOP:
   12977              :         case EXEC_OACC_PARALLEL:
   12978              :         case EXEC_OACC_KERNELS_LOOP:
   12979              :         case EXEC_OACC_KERNELS:
   12980              :         case EXEC_OACC_SERIAL_LOOP:
   12981              :         case EXEC_OACC_SERIAL:
   12982              :         case EXEC_OACC_DATA:
   12983              :         case EXEC_OACC_HOST_DATA:
   12984              :         case EXEC_OACC_LOOP:
   12985              :         case EXEC_OACC_UPDATE:
   12986              :         case EXEC_OACC_WAIT:
   12987              :         case EXEC_OACC_CACHE:
   12988              :         case EXEC_OACC_ENTER_DATA:
   12989              :         case EXEC_OACC_EXIT_DATA:
   12990              :         case EXEC_OACC_ROUTINE:
   12991              :         case EXEC_OMP_ALLOCATE:
   12992              :         case EXEC_OMP_ALLOCATORS:
   12993              :         case EXEC_OMP_ASSUME:
   12994              :         case EXEC_OMP_CRITICAL:
   12995              :         case EXEC_OMP_DISPATCH:
   12996              :         case EXEC_OMP_DISTRIBUTE:
   12997              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   12998              :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   12999              :         case EXEC_OMP_DISTRIBUTE_SIMD:
   13000              :         case EXEC_OMP_DO:
   13001              :         case EXEC_OMP_DO_SIMD:
   13002              :         case EXEC_OMP_ERROR:
   13003              :         case EXEC_OMP_LOOP:
   13004              :         case EXEC_OMP_MASKED:
   13005              :         case EXEC_OMP_MASKED_TASKLOOP:
   13006              :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   13007              :         case EXEC_OMP_MASTER:
   13008              :         case EXEC_OMP_MASTER_TASKLOOP:
   13009              :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   13010              :         case EXEC_OMP_ORDERED:
   13011              :         case EXEC_OMP_PARALLEL:
   13012              :         case EXEC_OMP_PARALLEL_DO:
   13013              :         case EXEC_OMP_PARALLEL_DO_SIMD:
   13014              :         case EXEC_OMP_PARALLEL_LOOP:
   13015              :         case EXEC_OMP_PARALLEL_MASKED:
   13016              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   13017              :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   13018              :         case EXEC_OMP_PARALLEL_MASTER:
   13019              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   13020              :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   13021              :         case EXEC_OMP_PARALLEL_SECTIONS:
   13022              :         case EXEC_OMP_PARALLEL_WORKSHARE:
   13023              :         case EXEC_OMP_SECTIONS:
   13024              :         case EXEC_OMP_SIMD:
   13025              :         case EXEC_OMP_SCOPE:
   13026              :         case EXEC_OMP_SINGLE:
   13027              :         case EXEC_OMP_TARGET:
   13028              :         case EXEC_OMP_TARGET_DATA:
   13029              :         case EXEC_OMP_TARGET_ENTER_DATA:
   13030              :         case EXEC_OMP_TARGET_EXIT_DATA:
   13031              :         case EXEC_OMP_TARGET_PARALLEL:
   13032              :         case EXEC_OMP_TARGET_PARALLEL_DO:
   13033              :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   13034              :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   13035              :         case EXEC_OMP_TARGET_SIMD:
   13036              :         case EXEC_OMP_TARGET_TEAMS:
   13037              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   13038              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13039              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13040              :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   13041              :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   13042              :         case EXEC_OMP_TARGET_UPDATE:
   13043              :         case EXEC_OMP_TASK:
   13044              :         case EXEC_OMP_TASKGROUP:
   13045              :         case EXEC_OMP_TASKLOOP:
   13046              :         case EXEC_OMP_TASKLOOP_SIMD:
   13047              :         case EXEC_OMP_TASKWAIT:
   13048              :         case EXEC_OMP_TASKYIELD:
   13049              :         case EXEC_OMP_TEAMS:
   13050              :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   13051              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   13052              :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   13053              :         case EXEC_OMP_TEAMS_LOOP:
   13054              :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   13055              :         case EXEC_OMP_TILE:
   13056              :         case EXEC_OMP_UNROLL:
   13057              :         case EXEC_OMP_WORKSHARE:
   13058              :           break;
   13059              : 
   13060            0 :         default:
   13061            0 :           gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
   13062              :         }
   13063              : 
   13064       342325 :       gfc_resolve_code (b->next, ns);
   13065              :     }
   13066       330857 : }
   13067              : 
   13068              : bool
   13069            0 : caf_possible_reallocate (gfc_expr *e)
   13070              : {
   13071            0 :   symbol_attribute caf_attr;
   13072            0 :   gfc_ref *last_arr_ref = nullptr;
   13073              : 
   13074            0 :   caf_attr = gfc_caf_attr (e);
   13075            0 :   if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
   13076              :     return false;
   13077              : 
   13078              :   /* Only full array refs can indicate a needed reallocation.  */
   13079            0 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
   13080            0 :     if (ref->type == REF_ARRAY && ref->u.ar.dimen)
   13081            0 :       last_arr_ref = ref;
   13082              : 
   13083            0 :   return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
   13084              : }
   13085              : 
   13086              : /* Does everything to resolve an ordinary assignment.  Returns true
   13087              :    if this is an interface assignment.  */
   13088              : static bool
   13089       285250 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
   13090              : {
   13091       285250 :   bool rval = false;
   13092       285250 :   gfc_expr *lhs;
   13093       285250 :   gfc_expr *rhs;
   13094       285250 :   int n;
   13095       285250 :   gfc_ref *ref;
   13096       285250 :   symbol_attribute attr;
   13097              : 
   13098       285250 :   if (gfc_extend_assign (code, ns))
   13099              :     {
   13100          918 :       gfc_expr** rhsptr;
   13101              : 
   13102          918 :       if (code->op == EXEC_ASSIGN_CALL)
   13103              :         {
   13104          469 :           lhs = code->ext.actual->expr;
   13105          469 :           rhsptr = &code->ext.actual->next->expr;
   13106              :         }
   13107              :       else
   13108              :         {
   13109          449 :           gfc_actual_arglist* args;
   13110          449 :           gfc_typebound_proc* tbp;
   13111              : 
   13112          449 :           gcc_assert (code->op == EXEC_COMPCALL);
   13113              : 
   13114          449 :           args = code->expr1->value.compcall.actual;
   13115          449 :           lhs = args->expr;
   13116          449 :           rhsptr = &args->next->expr;
   13117              : 
   13118          449 :           tbp = code->expr1->value.compcall.tbp;
   13119          449 :           gcc_assert (!tbp->is_generic);
   13120              :         }
   13121              : 
   13122              :       /* Make a temporary rhs when there is a default initializer
   13123              :          and rhs is the same symbol as the lhs.  */
   13124          918 :       if ((*rhsptr)->expr_type == EXPR_VARIABLE
   13125          507 :             && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
   13126          436 :             && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
   13127         1206 :             && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
   13128           60 :         *rhsptr = gfc_get_parentheses (*rhsptr);
   13129              : 
   13130          918 :       return true;
   13131              :     }
   13132              : 
   13133       284332 :   lhs = code->expr1;
   13134       284332 :   rhs = code->expr2;
   13135              : 
   13136       284332 :   if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
   13137       264337 :        || lhs->symtree->n.sym->ts.type == BT_CLASS)
   13138        22565 :       && !lhs->symtree->n.sym->attr.proc_pointer
   13139       306897 :       && gfc_expr_attr (lhs).proc_pointer)
   13140              :     {
   13141            1 :       gfc_error ("Variable in the ordinary assignment at %L is a procedure "
   13142              :                  "pointer component",
   13143              :                  &lhs->where);
   13144            1 :       return false;
   13145              :     }
   13146              : 
   13147       335033 :   if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
   13148       249042 :       && rhs->ts.type == BT_CHARACTER
   13149       284724 :       && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
   13150              :     {
   13151              :       /* Use of -fdec-char-conversions allows assignment of character data
   13152              :          to non-character variables.  This not permitted for nonconstant
   13153              :          strings.  */
   13154           29 :       gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
   13155              :                  gfc_typename (lhs), &rhs->where);
   13156           29 :       return false;
   13157              :     }
   13158              : 
   13159       284302 :   if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
   13160              :     {
   13161            0 :       gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
   13162              :                    gfc_typename (lhs), &rhs->where);
   13163            0 :       return false;
   13164              :     }
   13165              : 
   13166              :   /* Handle the case of a BOZ literal on the RHS.  */
   13167       284302 :   if (rhs->ts.type == BT_BOZ)
   13168              :     {
   13169            3 :       if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
   13170              :                            "statement value nor an actual argument of "
   13171              :                            "INT/REAL/DBLE/CMPLX intrinsic subprogram",
   13172              :                            &rhs->where))
   13173              :         return false;
   13174              : 
   13175            1 :       switch (lhs->ts.type)
   13176              :         {
   13177            0 :         case BT_INTEGER:
   13178            0 :           if (!gfc_boz2int (rhs, lhs->ts.kind))
   13179              :             return false;
   13180              :           break;
   13181            1 :         case BT_REAL:
   13182            1 :           if (!gfc_boz2real (rhs, lhs->ts.kind))
   13183              :             return false;
   13184              :           break;
   13185            0 :         default:
   13186            0 :           gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
   13187            0 :           return false;
   13188              :         }
   13189              :     }
   13190              : 
   13191       284300 :   if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
   13192              :     {
   13193           64 :       HOST_WIDE_INT llen = 0, rlen = 0;
   13194           64 :       if (lhs->ts.u.cl != NULL
   13195           64 :             && lhs->ts.u.cl->length != NULL
   13196           53 :             && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13197           53 :         llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
   13198              : 
   13199           64 :       if (rhs->expr_type == EXPR_CONSTANT)
   13200           26 :         rlen = rhs->value.character.length;
   13201              : 
   13202           38 :       else if (rhs->ts.u.cl != NULL
   13203           38 :                  && rhs->ts.u.cl->length != NULL
   13204           35 :                  && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
   13205           35 :         rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
   13206              : 
   13207           64 :       if (rlen && llen && rlen > llen)
   13208           28 :         gfc_warning_now (OPT_Wcharacter_truncation,
   13209              :                          "CHARACTER expression will be truncated "
   13210              :                          "in assignment (%wd/%wd) at %L",
   13211              :                          llen, rlen, &code->loc);
   13212              :     }
   13213              : 
   13214              :   /* Ensure that a vector index expression for the lvalue is evaluated
   13215              :      to a temporary if the lvalue symbol is referenced in it.  */
   13216       284300 :   if (lhs->rank)
   13217              :     {
   13218       111749 :       for (ref = lhs->ref; ref; ref= ref->next)
   13219        59672 :         if (ref->type == REF_ARRAY)
   13220              :           {
   13221       131902 :             for (n = 0; n < ref->u.ar.dimen; n++)
   13222        78053 :               if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
   13223        78283 :                   && gfc_find_sym_in_expr (lhs->symtree->n.sym,
   13224          230 :                                            ref->u.ar.start[n]))
   13225           14 :                 ref->u.ar.start[n]
   13226           14 :                         = gfc_get_parentheses (ref->u.ar.start[n]);
   13227              :           }
   13228              :     }
   13229              : 
   13230       284300 :   if (gfc_pure (NULL))
   13231              :     {
   13232         3370 :       if (lhs->ts.type == BT_DERIVED
   13233          136 :             && lhs->expr_type == EXPR_VARIABLE
   13234          136 :             && lhs->ts.u.derived->attr.pointer_comp
   13235            4 :             && rhs->expr_type == EXPR_VARIABLE
   13236         3373 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13237            2 :                 || gfc_is_coindexed (rhs)))
   13238              :         {
   13239              :           /* F2008, C1283.  */
   13240            2 :           if (gfc_is_coindexed (rhs))
   13241            1 :             gfc_error ("Coindexed expression at %L is assigned to "
   13242              :                         "a derived type variable with a POINTER "
   13243              :                         "component in a PURE procedure",
   13244              :                         &rhs->where);
   13245              :           else
   13246              :           /* F2008, C1283 (4).  */
   13247            1 :             gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
   13248              :                         "shall not be used as the expr at %L of an intrinsic "
   13249              :                         "assignment statement in which the variable is of a "
   13250              :                         "derived type if the derived type has a pointer "
   13251              :                         "component at any level of component selection.",
   13252              :                         &rhs->where);
   13253            2 :           return rval;
   13254              :         }
   13255              : 
   13256              :       /* Fortran 2008, C1283.  */
   13257         3368 :       if (gfc_is_coindexed (lhs))
   13258              :         {
   13259            1 :           gfc_error ("Assignment to coindexed variable at %L in a PURE "
   13260              :                      "procedure", &rhs->where);
   13261            1 :           return rval;
   13262              :         }
   13263              :     }
   13264              : 
   13265       284297 :   if (gfc_implicit_pure (NULL))
   13266              :     {
   13267         7214 :       if (lhs->expr_type == EXPR_VARIABLE
   13268         7214 :             && lhs->symtree->n.sym != gfc_current_ns->proc_name
   13269         5131 :             && lhs->symtree->n.sym->ns != gfc_current_ns)
   13270          253 :         gfc_unset_implicit_pure (NULL);
   13271              : 
   13272         7214 :       if (lhs->ts.type == BT_DERIVED
   13273          326 :             && lhs->expr_type == EXPR_VARIABLE
   13274          326 :             && lhs->ts.u.derived->attr.pointer_comp
   13275            7 :             && rhs->expr_type == EXPR_VARIABLE
   13276         7221 :             && (gfc_impure_variable (rhs->symtree->n.sym)
   13277            7 :                 || gfc_is_coindexed (rhs)))
   13278            0 :         gfc_unset_implicit_pure (NULL);
   13279              : 
   13280              :       /* Fortran 2008, C1283.  */
   13281         7214 :       if (gfc_is_coindexed (lhs))
   13282            0 :         gfc_unset_implicit_pure (NULL);
   13283              :     }
   13284              : 
   13285              :   /* F2008, 7.2.1.2.  */
   13286       284297 :   attr = gfc_expr_attr (lhs);
   13287       284297 :   if (lhs->ts.type == BT_CLASS && attr.allocatable)
   13288              :     {
   13289          987 :       if (attr.codimension)
   13290              :         {
   13291            1 :           gfc_error ("Assignment to polymorphic coarray at %L is not "
   13292              :                      "permitted", &lhs->where);
   13293            1 :           return false;
   13294              :         }
   13295          986 :       if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
   13296              :                            "polymorphic variable at %L", &lhs->where))
   13297              :         return false;
   13298          985 :       if (!flag_realloc_lhs)
   13299              :         {
   13300            1 :           gfc_error ("Assignment to an allocatable polymorphic variable at %L "
   13301              :                      "requires %<-frealloc-lhs%>", &lhs->where);
   13302            1 :           return false;
   13303              :         }
   13304              :     }
   13305       283310 :   else if (lhs->ts.type == BT_CLASS)
   13306              :     {
   13307            9 :       gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
   13308              :                  "assignment at %L - check that there is a matching specific "
   13309              :                  "subroutine for %<=%> operator", &lhs->where);
   13310            9 :       return false;
   13311              :     }
   13312              : 
   13313       284285 :   bool lhs_coindexed = gfc_is_coindexed (lhs);
   13314              : 
   13315              :   /* F2008, Section 7.2.1.2.  */
   13316       284285 :   if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
   13317              :     {
   13318            1 :       gfc_error ("Coindexed variable must not have an allocatable ultimate "
   13319              :                  "component in assignment at %L", &lhs->where);
   13320            1 :       return false;
   13321              :     }
   13322              : 
   13323              :   /* Assign the 'data' of a class object to a derived type.  */
   13324       284284 :   if (lhs->ts.type == BT_DERIVED
   13325         7171 :       && rhs->ts.type == BT_CLASS
   13326          150 :       && (rhs->expr_type != EXPR_ARRAY
   13327          144 :           && rhs->expr_type != EXPR_OP))
   13328          138 :     gfc_add_data_component (rhs);
   13329              : 
   13330              :   /* Make sure there is a vtable and, in particular, a _copy for the
   13331              :      rhs type.  */
   13332       284284 :   if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
   13333          615 :     gfc_find_vtab (&rhs->ts);
   13334              : 
   13335       284284 :   gfc_check_assign (lhs, rhs, 1);
   13336              : 
   13337       284284 :   return false;
   13338              : }
   13339              : 
   13340              : 
   13341              : /* Add a component reference onto an expression.  */
   13342              : 
   13343              : static void
   13344          665 : add_comp_ref (gfc_expr *e, gfc_component *c)
   13345              : {
   13346          665 :   gfc_ref **ref;
   13347          665 :   ref = &(e->ref);
   13348          889 :   while (*ref)
   13349          224 :     ref = &((*ref)->next);
   13350          665 :   *ref = gfc_get_ref ();
   13351          665 :   (*ref)->type = REF_COMPONENT;
   13352          665 :   (*ref)->u.c.sym = e->ts.u.derived;
   13353          665 :   (*ref)->u.c.component = c;
   13354          665 :   e->ts = c->ts;
   13355              : 
   13356              :   /* Add a full array ref, as necessary.  */
   13357          665 :   if (c->as)
   13358              :     {
   13359           84 :       gfc_add_full_array_ref (e, c->as);
   13360           84 :       e->rank = c->as->rank;
   13361           84 :       e->corank = c->as->corank;
   13362              :     }
   13363          665 : }
   13364              : 
   13365              : 
   13366              : /* Build an assignment.  Keep the argument 'op' for future use, so that
   13367              :    pointer assignments can be made.  */
   13368              : 
   13369              : static gfc_code *
   13370          988 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
   13371              :                   gfc_component *comp1, gfc_component *comp2, locus loc)
   13372              : {
   13373          988 :   gfc_code *this_code;
   13374              : 
   13375          988 :   this_code = gfc_get_code (op);
   13376          988 :   this_code->next = NULL;
   13377          988 :   this_code->expr1 = gfc_copy_expr (expr1);
   13378          988 :   this_code->expr2 = gfc_copy_expr (expr2);
   13379          988 :   this_code->loc = loc;
   13380          988 :   if (comp1 && comp2)
   13381              :     {
   13382          288 :       add_comp_ref (this_code->expr1, comp1);
   13383          288 :       add_comp_ref (this_code->expr2, comp2);
   13384              :     }
   13385              : 
   13386          988 :   return this_code;
   13387              : }
   13388              : 
   13389              : 
   13390              : /* Makes a temporary variable expression based on the characteristics of
   13391              :    a given variable expression.  If allocatable is set, the temporary is
   13392              :    unconditionally allocatable*/
   13393              : 
   13394              : static gfc_expr*
   13395          482 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
   13396              :                     bool allocatable = false)
   13397              : {
   13398          482 :   static int serial = 0;
   13399          482 :   char name[GFC_MAX_SYMBOL_LEN];
   13400          482 :   gfc_symtree *tmp;
   13401          482 :   gfc_array_spec *as;
   13402          482 :   gfc_array_ref *aref;
   13403          482 :   gfc_ref *ref;
   13404              : 
   13405          482 :   sprintf (name, GFC_PREFIX("DA%d"), serial++);
   13406          482 :   gfc_get_sym_tree (name, ns, &tmp, false);
   13407          482 :   gfc_add_type (tmp->n.sym, &e->ts, NULL);
   13408              : 
   13409          482 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
   13410            0 :     tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
   13411              :                                                     NULL,
   13412            0 :                                                     e->value.character.length);
   13413              : 
   13414          482 :   as = NULL;
   13415          482 :   ref = NULL;
   13416          482 :   aref = NULL;
   13417              : 
   13418              :   /* Obtain the arrayspec for the temporary.  */
   13419          482 :    if (e->rank && e->expr_type != EXPR_ARRAY
   13420              :        && e->expr_type != EXPR_FUNCTION
   13421              :        && e->expr_type != EXPR_OP)
   13422              :     {
   13423           52 :       aref = gfc_find_array_ref (e);
   13424           52 :       if (e->expr_type == EXPR_VARIABLE
   13425           52 :           && e->symtree->n.sym->as == aref->as)
   13426              :         as = aref->as;
   13427              :       else
   13428              :         {
   13429            0 :           for (ref = e->ref; ref; ref = ref->next)
   13430            0 :             if (ref->type == REF_COMPONENT
   13431            0 :                 && ref->u.c.component->as == aref->as)
   13432              :               {
   13433              :                 as = aref->as;
   13434              :                 break;
   13435              :               }
   13436              :         }
   13437              :     }
   13438              : 
   13439              :   /* Add the attributes and the arrayspec to the temporary.  */
   13440          482 :   tmp->n.sym->attr = gfc_expr_attr (e);
   13441          482 :   tmp->n.sym->attr.function = 0;
   13442          482 :   tmp->n.sym->attr.proc_pointer = 0;
   13443          482 :   tmp->n.sym->attr.result = 0;
   13444          482 :   tmp->n.sym->attr.flavor = FL_VARIABLE;
   13445          482 :   tmp->n.sym->attr.dummy = 0;
   13446          482 :   tmp->n.sym->attr.use_assoc = 0;
   13447          482 :   tmp->n.sym->attr.intent = INTENT_UNKNOWN;
   13448              : 
   13449              : 
   13450          482 :   if (as && !allocatable)
   13451              :     {
   13452           52 :       tmp->n.sym->as = gfc_copy_array_spec (as);
   13453           52 :       if (!ref)
   13454           52 :         ref = e->ref;
   13455           52 :       if (as->type == AS_DEFERRED)
   13456           46 :         tmp->n.sym->attr.allocatable = 1;
   13457              :     }
   13458          430 :   else if ((e->rank || e->corank)
   13459          130 :            && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
   13460           24 :                || e->expr_type == EXPR_OP || allocatable))
   13461              :     {
   13462          130 :       tmp->n.sym->as = gfc_get_array_spec ();
   13463          130 :       tmp->n.sym->as->type = AS_DEFERRED;
   13464          130 :       tmp->n.sym->as->rank = e->rank;
   13465          130 :       tmp->n.sym->as->corank = e->corank;
   13466          130 :       tmp->n.sym->attr.allocatable = 1;
   13467          130 :       tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
   13468          260 :       tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
   13469              :     }
   13470              :   else
   13471          300 :     tmp->n.sym->attr.dimension = 0;
   13472              : 
   13473          482 :   gfc_set_sym_referenced (tmp->n.sym);
   13474          482 :   gfc_commit_symbol (tmp->n.sym);
   13475          482 :   e = gfc_lval_expr_from_sym (tmp->n.sym);
   13476              : 
   13477              :   /* Should the lhs be a section, use its array ref for the
   13478              :      temporary expression.  */
   13479          482 :   if (aref && aref->type != AR_FULL && !allocatable)
   13480              :     {
   13481            6 :       gfc_free_ref_list (e->ref);
   13482            6 :       e->ref = gfc_copy_ref (ref);
   13483              :     }
   13484          482 :   return e;
   13485              : }
   13486              : 
   13487              : 
   13488              : /* Helper function to take an argument in a subroutine call with a dependency
   13489              :    on another argument, copy it to an allocatable temporary and use the
   13490              :    temporary in the call expression. The new code is embedded in a block to
   13491              :    ensure local, automatic deallocation.  */
   13492              : 
   13493              : static void
   13494           36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
   13495              :                              gfc_expr **rhsptr)
   13496              : {
   13497           36 :   gfc_namespace *block_ns;
   13498           36 :   gfc_expr *tmp_var;
   13499              : 
   13500              :   /* Wrap the new code in a block so that the temporary is deallocated.  */
   13501           36 :   block_ns = gfc_build_block_ns (ns);
   13502              : 
   13503              :   /* As it stands, the block_ns does not not stand up to resolution because the
   13504              :      the assignment would be converted to a call and, in any case, the modified
   13505              :      call fails in gfc_check_conformance.  */
   13506           36 :   block_ns->resolved = 1;
   13507              : 
   13508              :   /* Assign the original expression to the temporary.  */
   13509           36 :   tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
   13510           72 :   block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
   13511           36 :                                      NULL, NULL, (*rhsptr)->where);
   13512              : 
   13513              :   /* Transfer the call to the block and terminate block code.  */
   13514           36 :   *rhsptr = gfc_copy_expr (tmp_var);
   13515           36 :   block_ns->code->next = gfc_get_code (EXEC_NOP);
   13516           36 :   *(block_ns->code->next) = *code;
   13517           36 :   block_ns->code->next->next = NULL;
   13518              : 
   13519              :   /* Convert the original code to execute the block.  */
   13520           36 :   code->op = EXEC_BLOCK;
   13521           36 :   code->ext.block.ns = block_ns;
   13522           36 :   code->ext.block.assoc = NULL;
   13523           36 :   code->expr1 = code->expr2 = NULL;
   13524           36 : }
   13525              : 
   13526              : 
   13527              : /* Add one line of code to the code chain, making sure that 'head' and
   13528              :    'tail' are appropriately updated.  */
   13529              : 
   13530              : static void
   13531          656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
   13532              : {
   13533          656 :   gcc_assert (this_code);
   13534          656 :   if (*head == NULL)
   13535          308 :     *head = *tail = *this_code;
   13536              :   else
   13537          348 :     *tail = gfc_append_code (*tail, *this_code);
   13538          656 :   *this_code = NULL;
   13539          656 : }
   13540              : 
   13541              : 
   13542              : /* Generate a final call from a variable expression  */
   13543              : 
   13544              : static void
   13545           81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
   13546              : {
   13547           81 :   gfc_code *this_code;
   13548           81 :   gfc_expr *final_expr = NULL;
   13549           81 :   gfc_expr *size_expr;
   13550           81 :   gfc_expr *fini_coarray;
   13551              : 
   13552           81 :   gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
   13553           81 :   if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
   13554           75 :     return;
   13555              : 
   13556              :   /* Now generate the finalizer call.  */
   13557            6 :   this_code = gfc_get_code (EXEC_CALL);
   13558            6 :   this_code->symtree = final_expr->symtree;
   13559            6 :   this_code->resolved_sym = final_expr->symtree->n.sym;
   13560              : 
   13561              :   //* Expression to be finalized  */
   13562            6 :   this_code->ext.actual = gfc_get_actual_arglist ();
   13563            6 :   this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
   13564              : 
   13565              :   /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE.  */
   13566            6 :   this_code->ext.actual->next = gfc_get_actual_arglist ();
   13567            6 :   size_expr = gfc_get_expr ();
   13568            6 :   size_expr->where = gfc_current_locus;
   13569            6 :   size_expr->expr_type = EXPR_OP;
   13570            6 :   size_expr->value.op.op = INTRINSIC_DIVIDE;
   13571            6 :   size_expr->value.op.op1
   13572           12 :         = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
   13573              :                                     "storage_size", gfc_current_locus, 2,
   13574            6 :                                     gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
   13575              :                                     gfc_get_int_expr (gfc_index_integer_kind,
   13576              :                                                       NULL, 0));
   13577            6 :   size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
   13578              :                                               gfc_character_storage_size);
   13579            6 :   size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
   13580            6 :   size_expr->ts = size_expr->value.op.op1->ts;
   13581            6 :   this_code->ext.actual->next->expr = size_expr;
   13582              : 
   13583              :   /* fini_coarray  */
   13584            6 :   this_code->ext.actual->next->next = gfc_get_actual_arglist ();
   13585            6 :   fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
   13586              :                                         &tmp_expr->where);
   13587            6 :   fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
   13588            6 :   this_code->ext.actual->next->next->expr = fini_coarray;
   13589              : 
   13590            6 :   add_code_to_chain (&this_code, head, tail);
   13591              : 
   13592              : }
   13593              : 
   13594              : /* Counts the potential number of part array references that would
   13595              :    result from resolution of typebound defined assignments.  */
   13596              : 
   13597              : 
   13598              : static int
   13599          243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
   13600              : {
   13601          243 :   gfc_component *c;
   13602          243 :   int c_depth = 0, t_depth;
   13603              : 
   13604          584 :   for (c= derived->components; c; c = c->next)
   13605              :     {
   13606          341 :       if ((!gfc_bt_struct (c->ts.type)
   13607          261 :             || c->attr.pointer
   13608          261 :             || c->attr.allocatable
   13609          260 :             || c->attr.proc_pointer_comp
   13610          260 :             || c->attr.class_pointer
   13611          260 :             || c->attr.proc_pointer)
   13612           81 :           && !c->attr.defined_assign_comp)
   13613           81 :         continue;
   13614              : 
   13615          260 :       if (c->as && c_depth == 0)
   13616          260 :         c_depth = 1;
   13617              : 
   13618          260 :       if (c->ts.u.derived->attr.defined_assign_comp)
   13619          110 :         t_depth = nonscalar_typebound_assign (c->ts.u.derived,
   13620              :                                               c->as ? 1 : 0);
   13621              :       else
   13622              :         t_depth = 0;
   13623              : 
   13624          260 :       c_depth = t_depth > c_depth ? t_depth : c_depth;
   13625              :     }
   13626          243 :   return depth + c_depth;
   13627              : }
   13628              : 
   13629              : 
   13630              : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
   13631              :    "An intrinsic assignment where the variable is of derived type is performed
   13632              :     as if each component of the variable were assigned from the corresponding
   13633              :     component of expr using pointer assignment (10.2.2) for each pointer
   13634              :     component, defined assignment for each nonpointer nonallocatable component
   13635              :     of a type that has a type-bound defined assignment consistent with the
   13636              :     component, intrinsic assignment for each other nonpointer nonallocatable
   13637              :     component, and intrinsic assignment for each allocated coarray component.
   13638              :     For unallocated coarray components, the corresponding component of the
   13639              :     variable shall be unallocated. For a noncoarray allocatable component the
   13640              :     following sequence of operations is applied.
   13641              :         (1) If the component of the variable is allocated, it is deallocated.
   13642              :         (2) If the component of the value of expr is allocated, the
   13643              :             corresponding component of the variable is allocated with the same
   13644              :             dynamic type and type parameters as the component of the value of
   13645              :             expr. If it is an array, it is allocated with the same bounds. The
   13646              :             value of the component of the value of expr is then assigned to the
   13647              :             corresponding component of the variable using defined assignment if
   13648              :             the declared type of the component has a type-bound defined
   13649              :             assignment consistent with the component, and intrinsic assignment
   13650              :             for the dynamic type of that component otherwise."
   13651              : 
   13652              :    The pointer assignments are taken care of by the intrinsic assignment of the
   13653              :    structure itself.  This function recursively adds defined assignments where
   13654              :    required.  The recursion is accomplished by calling gfc_resolve_code.
   13655              : 
   13656              :    When the lhs in a defined assignment has intent INOUT or is intent OUT
   13657              :    and the component of 'var' is finalizable, we need a temporary for the
   13658              :    lhs.  In pseudo-code for an assignment var = expr:
   13659              : 
   13660              :    ! Confine finalization of temporaries, as far as possible.
   13661              :      Enclose the code for the assignment in a block
   13662              :    ! Only call function 'expr' once.
   13663              :       #if ('expr is not a constant or an variable)
   13664              :         temp_expr = expr
   13665              :         expr = temp_x
   13666              :    ! Do the intrinsic assignment
   13667              :       #if typeof ('var') has a typebound final subroutine
   13668              :         finalize (var)
   13669              :       var = expr
   13670              :    ! Now do the component assignments
   13671              :       #do over derived type components [%cmp]
   13672              :         #if (cmp is a pointer of any kind)
   13673              :           continue
   13674              :         build the assignment
   13675              :         resolve the code
   13676              :         #if the code is a typebound assignment
   13677              :            #if (arg1 is INOUT or finalizable OUT && !t1)
   13678              :              t1 = var
   13679              :              arg1 = t1
   13680              :              deal with allocatation or not of var and this component
   13681              :         #elseif the code is an assignment by itself
   13682              :            #if this component does not need finalization
   13683              :              delete code and continue
   13684              :         #else
   13685              :            remove the leading assignment
   13686              :         #endif
   13687              :         commit the code
   13688              :         #if (t1 and (arg1 is INOUT or finalizable OUT))
   13689              :            var%cmp = t1%cmp
   13690              :       #enddo
   13691              :       put all code chunks involving t1 to the top of the generated code
   13692              :       insert the generated block in place of the original code
   13693              : */
   13694              : 
   13695              : static bool
   13696          381 : is_finalizable_type (gfc_typespec ts)
   13697              : {
   13698          381 :   gfc_component *c;
   13699              : 
   13700          381 :   if (ts.type != BT_DERIVED)
   13701              :     return false;
   13702              : 
   13703              :   /* (1) Check for FINAL subroutines.  */
   13704          381 :   if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
   13705              :     return true;
   13706              : 
   13707              :   /* (2) Check for components of finalizable type.  */
   13708          809 :   for (c = ts.u.derived->components; c; c = c->next)
   13709          470 :     if (c->ts.type == BT_DERIVED
   13710          243 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
   13711          242 :         && c->ts.u.derived->f2k_derived
   13712          242 :         && c->ts.u.derived->f2k_derived->finalizers)
   13713              :       return true;
   13714              : 
   13715              :   return false;
   13716              : }
   13717              : 
   13718              : /* The temporary assignments have to be put on top of the additional
   13719              :    code to avoid the result being changed by the intrinsic assignment.
   13720              :    */
   13721              : static int component_assignment_level = 0;
   13722              : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
   13723              : static bool finalizable_comp;
   13724              : 
   13725              : static void
   13726          188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
   13727              : {
   13728          188 :   gfc_component *comp1, *comp2;
   13729          188 :   gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
   13730          188 :   gfc_code *tmp_code = NULL;
   13731          188 :   gfc_expr *t1 = NULL;
   13732          188 :   gfc_expr *tmp_expr = NULL;
   13733          188 :   int error_count, depth;
   13734          188 :   bool finalizable_lhs;
   13735              : 
   13736          188 :   gfc_get_errors (NULL, &error_count);
   13737              : 
   13738              :   /* Filter out continuing processing after an error.  */
   13739          188 :   if (error_count
   13740          188 :       || (*code)->expr1->ts.type != BT_DERIVED
   13741          188 :       || (*code)->expr2->ts.type != BT_DERIVED)
   13742          140 :     return;
   13743              : 
   13744              :   /* TODO: Handle more than one part array reference in assignments.  */
   13745          188 :   depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
   13746          188 :                                       (*code)->expr1->rank ? 1 : 0);
   13747          188 :   if (depth > 1)
   13748              :     {
   13749            6 :       gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
   13750              :                    "done because multiple part array references would "
   13751              :                    "occur in intermediate expressions.", &(*code)->loc);
   13752            6 :       return;
   13753              :     }
   13754              : 
   13755          182 :   if (!component_assignment_level)
   13756          134 :     finalizable_comp = true;
   13757              : 
   13758              :   /* Build a block so that function result temporaries are finalized
   13759              :      locally on exiting the rather than enclosing scope.  */
   13760          182 :   if (!component_assignment_level)
   13761              :     {
   13762          134 :       ns = gfc_build_block_ns (ns);
   13763          134 :       tmp_code = gfc_get_code (EXEC_NOP);
   13764          134 :       *tmp_code = **code;
   13765          134 :       tmp_code->next = NULL;
   13766          134 :       (*code)->op = EXEC_BLOCK;
   13767          134 :       (*code)->ext.block.ns = ns;
   13768          134 :       (*code)->ext.block.assoc = NULL;
   13769          134 :       (*code)->expr1 = (*code)->expr2 = NULL;
   13770          134 :       ns->code = tmp_code;
   13771          134 :       code = &ns->code;
   13772              :     }
   13773              : 
   13774          182 :   component_assignment_level++;
   13775              : 
   13776          182 :   finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
   13777              : 
   13778              :   /* Create a temporary so that functions get called only once.  */
   13779          182 :   if ((*code)->expr2->expr_type != EXPR_VARIABLE
   13780          182 :       && (*code)->expr2->expr_type != EXPR_CONSTANT)
   13781              :     {
   13782              :       /* Assign the rhs to the temporary.  */
   13783           81 :       tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   13784           81 :       if (tmp_expr->symtree->n.sym->attr.pointer)
   13785              :         {
   13786              :           /* Use allocate on assignment for the sake of simplicity. The
   13787              :              temporary must not take on the optional attribute. Assume
   13788              :              that the assignment is guarded by a PRESENT condition if the
   13789              :              lhs is optional.  */
   13790           25 :           tmp_expr->symtree->n.sym->attr.pointer = 0;
   13791           25 :           tmp_expr->symtree->n.sym->attr.optional = 0;
   13792           25 :           tmp_expr->symtree->n.sym->attr.allocatable = 1;
   13793              :         }
   13794          162 :       this_code = build_assignment (EXEC_ASSIGN,
   13795              :                                     tmp_expr, (*code)->expr2,
   13796           81 :                                     NULL, NULL, (*code)->loc);
   13797           81 :       this_code->expr2->must_finalize = 1;
   13798              :       /* Add the code and substitute the rhs expression.  */
   13799           81 :       add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
   13800           81 :       gfc_free_expr ((*code)->expr2);
   13801           81 :       (*code)->expr2 = tmp_expr;
   13802              :     }
   13803              : 
   13804              :   /* Do the intrinsic assignment.  This is not needed if the lhs is one
   13805              :      of the temporaries generated here, since the intrinsic assignment
   13806              :      to the final result already does this.  */
   13807          182 :   if ((*code)->expr1->symtree->n.sym->name[2] != '.')
   13808              :     {
   13809          182 :       if (finalizable_lhs)
   13810           18 :         (*code)->expr1->must_finalize = 1;
   13811          182 :       this_code = build_assignment (EXEC_ASSIGN,
   13812              :                                     (*code)->expr1, (*code)->expr2,
   13813              :                                     NULL, NULL, (*code)->loc);
   13814          182 :       add_code_to_chain (&this_code, &head, &tail);
   13815              :     }
   13816              : 
   13817          182 :   comp1 = (*code)->expr1->ts.u.derived->components;
   13818          182 :   comp2 = (*code)->expr2->ts.u.derived->components;
   13819              : 
   13820          449 :   for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
   13821              :     {
   13822          267 :       bool inout = false;
   13823          267 :       bool finalizable_out = false;
   13824              : 
   13825              :       /* The intrinsic assignment does the right thing for pointers
   13826              :          of all kinds and allocatable components.  */
   13827          267 :       if (!gfc_bt_struct (comp1->ts.type)
   13828          200 :           || comp1->attr.pointer
   13829          200 :           || comp1->attr.allocatable
   13830          199 :           || comp1->attr.proc_pointer_comp
   13831          199 :           || comp1->attr.class_pointer
   13832          199 :           || comp1->attr.proc_pointer)
   13833           68 :         continue;
   13834              : 
   13835          398 :       finalizable_comp = is_finalizable_type (comp1->ts)
   13836          199 :                          && !finalizable_lhs;
   13837              : 
   13838              :       /* Make an assignment for this component.  */
   13839          398 :       this_code = build_assignment (EXEC_ASSIGN,
   13840              :                                     (*code)->expr1, (*code)->expr2,
   13841          199 :                                     comp1, comp2, (*code)->loc);
   13842              : 
   13843              :       /* Convert the assignment if there is a defined assignment for
   13844              :          this type.  Otherwise, using the call from gfc_resolve_code,
   13845              :          recurse into its components.  */
   13846          199 :       gfc_resolve_code (this_code, ns);
   13847              : 
   13848          199 :       if (this_code->op == EXEC_ASSIGN_CALL)
   13849              :         {
   13850          144 :           gfc_formal_arglist *dummy_args;
   13851          144 :           gfc_symbol *rsym;
   13852              :           /* Check that there is a typebound defined assignment.  If not,
   13853              :              then this must be a module defined assignment.  We cannot
   13854              :              use the defined_assign_comp attribute here because it must
   13855              :              be this derived type that has the defined assignment and not
   13856              :              a parent type.  */
   13857          144 :           if (!(comp1->ts.u.derived->f2k_derived
   13858              :                 && comp1->ts.u.derived->f2k_derived
   13859          144 :                                         ->tb_op[INTRINSIC_ASSIGN]))
   13860              :             {
   13861            1 :               gfc_free_statements (this_code);
   13862            1 :               this_code = NULL;
   13863            1 :               continue;
   13864              :             }
   13865              : 
   13866              :           /* If the first argument of the subroutine has intent INOUT
   13867              :              a temporary must be generated and used instead.  */
   13868          143 :           rsym = this_code->resolved_sym;
   13869          143 :           dummy_args = gfc_sym_get_dummy_args (rsym);
   13870          268 :           finalizable_out = gfc_may_be_finalized (comp1->ts)
   13871           18 :                             && dummy_args
   13872          161 :                             && dummy_args->sym->attr.intent == INTENT_OUT;
   13873          286 :           inout = dummy_args
   13874          268 :                   && dummy_args->sym->attr.intent == INTENT_INOUT;
   13875           72 :           if ((inout || finalizable_out)
   13876           89 :               && !comp1->attr.allocatable)
   13877              :             {
   13878           89 :               gfc_code *temp_code;
   13879           89 :               inout = true;
   13880              : 
   13881              :               /* Build the temporary required for the assignment and put
   13882              :                  it at the head of the generated code.  */
   13883           89 :               if (!t1)
   13884              :                 {
   13885           89 :                   gfc_namespace *tmp_ns = ns;
   13886           89 :                   if (ns->parent && gfc_may_be_finalized (comp1->ts))
   13887           18 :                     tmp_ns = (*code)->expr1->symtree->n.sym->ns;
   13888           89 :                   t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
   13889           89 :                   t1->symtree->n.sym->attr.artificial = 1;
   13890          178 :                   temp_code = build_assignment (EXEC_ASSIGN,
   13891              :                                                 t1, (*code)->expr1,
   13892           89 :                                 NULL, NULL, (*code)->loc);
   13893              : 
   13894              :                   /* For allocatable LHS, check whether it is allocated.  Note
   13895              :                      that allocatable components with defined assignment are
   13896              :                      not yet support.  See PR 57696.  */
   13897           89 :                   if ((*code)->expr1->symtree->n.sym->attr.allocatable)
   13898              :                     {
   13899           24 :                       gfc_code *block;
   13900           24 :                       gfc_expr *e =
   13901           24 :                         gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   13902           24 :                       block = gfc_get_code (EXEC_IF);
   13903           24 :                       block->block = gfc_get_code (EXEC_IF);
   13904           24 :                       block->block->expr1
   13905           48 :                           = gfc_build_intrinsic_call (ns,
   13906              :                                     GFC_ISYM_ALLOCATED, "allocated",
   13907           24 :                                     (*code)->loc, 1, e);
   13908           24 :                       block->block->next = temp_code;
   13909           24 :                       temp_code = block;
   13910              :                     }
   13911           89 :                   add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
   13912              :                 }
   13913              : 
   13914              :               /* Replace the first actual arg with the component of the
   13915              :                  temporary.  */
   13916           89 :               gfc_free_expr (this_code->ext.actual->expr);
   13917           89 :               this_code->ext.actual->expr = gfc_copy_expr (t1);
   13918           89 :               add_comp_ref (this_code->ext.actual->expr, comp1);
   13919              : 
   13920              :               /* If the LHS variable is allocatable and wasn't allocated and
   13921              :                  the temporary is allocatable, pointer assign the address of
   13922              :                  the freshly allocated LHS to the temporary.  */
   13923           89 :               if ((*code)->expr1->symtree->n.sym->attr.allocatable
   13924           89 :                   && gfc_expr_attr ((*code)->expr1).allocatable)
   13925              :                 {
   13926           18 :                   gfc_code *block;
   13927           18 :                   gfc_expr *cond;
   13928              : 
   13929           18 :                   cond = gfc_get_expr ();
   13930           18 :                   cond->ts.type = BT_LOGICAL;
   13931           18 :                   cond->ts.kind = gfc_default_logical_kind;
   13932           18 :                   cond->expr_type = EXPR_OP;
   13933           18 :                   cond->where = (*code)->loc;
   13934           18 :                   cond->value.op.op = INTRINSIC_NOT;
   13935           18 :                   cond->value.op.op1 = gfc_build_intrinsic_call (ns,
   13936              :                                           GFC_ISYM_ALLOCATED, "allocated",
   13937           18 :                                           (*code)->loc, 1, gfc_copy_expr (t1));
   13938           18 :                   block = gfc_get_code (EXEC_IF);
   13939           18 :                   block->block = gfc_get_code (EXEC_IF);
   13940           18 :                   block->block->expr1 = cond;
   13941           36 :                   block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   13942              :                                         t1, (*code)->expr1,
   13943           18 :                                         NULL, NULL, (*code)->loc);
   13944           18 :                   add_code_to_chain (&block, &head, &tail);
   13945              :                 }
   13946              :             }
   13947              :         }
   13948           55 :       else if (this_code->op == EXEC_ASSIGN && !this_code->next)
   13949              :         {
   13950              :           /* Don't add intrinsic assignments since they are already
   13951              :              effected by the intrinsic assignment of the structure, unless
   13952              :              finalization is required.  */
   13953            7 :           if (finalizable_comp)
   13954            0 :             this_code->expr1->must_finalize = 1;
   13955              :           else
   13956              :             {
   13957            7 :               gfc_free_statements (this_code);
   13958            7 :               this_code = NULL;
   13959            7 :               continue;
   13960              :             }
   13961              :         }
   13962              :       else
   13963              :         {
   13964              :           /* Resolution has expanded an assignment of a derived type with
   13965              :              defined assigned components.  Remove the redundant, leading
   13966              :              assignment.  */
   13967           48 :           gcc_assert (this_code->op == EXEC_ASSIGN);
   13968           48 :           gfc_code *tmp = this_code;
   13969           48 :           this_code = this_code->next;
   13970           48 :           tmp->next = NULL;
   13971           48 :           gfc_free_statements (tmp);
   13972              :         }
   13973              : 
   13974          191 :       add_code_to_chain (&this_code, &head, &tail);
   13975              : 
   13976          191 :       if (t1 && (inout || finalizable_out))
   13977              :         {
   13978              :           /* Transfer the value to the final result.  */
   13979          178 :           this_code = build_assignment (EXEC_ASSIGN,
   13980              :                                         (*code)->expr1, t1,
   13981           89 :                                         comp1, comp2, (*code)->loc);
   13982           89 :           this_code->expr1->must_finalize = 0;
   13983           89 :           add_code_to_chain (&this_code, &head, &tail);
   13984              :         }
   13985              :     }
   13986              : 
   13987              :   /* Put the temporary assignments at the top of the generated code.  */
   13988          182 :   if (tmp_head && component_assignment_level == 1)
   13989              :     {
   13990          126 :       gfc_append_code (tmp_head, head);
   13991          126 :       head = tmp_head;
   13992          126 :       tmp_head = tmp_tail = NULL;
   13993              :     }
   13994              : 
   13995              :   /* If we did a pointer assignment - thus, we need to ensure that the LHS is
   13996              :      not accidentally deallocated. Hence, nullify t1.  */
   13997           89 :   if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
   13998          271 :       && gfc_expr_attr ((*code)->expr1).allocatable)
   13999              :     {
   14000           18 :       gfc_code *block;
   14001           18 :       gfc_expr *cond;
   14002           18 :       gfc_expr *e;
   14003              : 
   14004           18 :       e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
   14005           18 :       cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
   14006           18 :                                        (*code)->loc, 2, gfc_copy_expr (t1), e);
   14007           18 :       block = gfc_get_code (EXEC_IF);
   14008           18 :       block->block = gfc_get_code (EXEC_IF);
   14009           18 :       block->block->expr1 = cond;
   14010           18 :       block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
   14011              :                                         t1, gfc_get_null_expr (&(*code)->loc),
   14012           18 :                                         NULL, NULL, (*code)->loc);
   14013           18 :       gfc_append_code (tail, block);
   14014           18 :       tail = block;
   14015              :     }
   14016              : 
   14017          182 :   component_assignment_level--;
   14018              : 
   14019              :   /* Make an explicit final call for the function result.  */
   14020          182 :   if (tmp_expr)
   14021           81 :     generate_final_call (tmp_expr, &head, &tail);
   14022              : 
   14023          182 :   if (tmp_code)
   14024              :     {
   14025          134 :       ns->code = head;
   14026          134 :       return;
   14027              :     }
   14028              : 
   14029              :   /* Now attach the remaining code chain to the input code.  Step on
   14030              :      to the end of the new code since resolution is complete.  */
   14031           48 :   gcc_assert ((*code)->op == EXEC_ASSIGN);
   14032           48 :   tail->next = (*code)->next;
   14033              :   /* Overwrite 'code' because this would place the intrinsic assignment
   14034              :      before the temporary for the lhs is created.  */
   14035           48 :   gfc_free_expr ((*code)->expr1);
   14036           48 :   gfc_free_expr ((*code)->expr2);
   14037           48 :   **code = *head;
   14038           48 :   if (head != tail)
   14039           48 :     free (head);
   14040           48 :   *code = tail;
   14041              : }
   14042              : 
   14043              : 
   14044              : /* F2008: Pointer function assignments are of the form:
   14045              :         ptr_fcn (args) = expr
   14046              :    This function breaks these assignments into two statements:
   14047              :         temporary_pointer => ptr_fcn(args)
   14048              :         temporary_pointer = expr  */
   14049              : 
   14050              : static bool
   14051       285494 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
   14052              : {
   14053       285494 :   gfc_expr *tmp_ptr_expr;
   14054       285494 :   gfc_code *this_code;
   14055       285494 :   gfc_component *comp;
   14056       285494 :   gfc_symbol *s;
   14057              : 
   14058       285494 :   if ((*code)->expr1->expr_type != EXPR_FUNCTION)
   14059              :     return false;
   14060              : 
   14061              :   /* Even if standard does not support this feature, continue to build
   14062              :      the two statements to avoid upsetting frontend_passes.c.  */
   14063          205 :   gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
   14064              :                   "%L", &(*code)->loc);
   14065              : 
   14066          205 :   comp = gfc_get_proc_ptr_comp ((*code)->expr1);
   14067              : 
   14068          205 :   if (comp)
   14069            6 :     s = comp->ts.interface;
   14070              :   else
   14071          199 :     s = (*code)->expr1->symtree->n.sym;
   14072              : 
   14073          205 :   if (s == NULL || !s->result->attr.pointer)
   14074              :     {
   14075            5 :       gfc_error ("The function result on the lhs of the assignment at "
   14076              :                  "%L must have the pointer attribute.",
   14077            5 :                  &(*code)->expr1->where);
   14078            5 :       (*code)->op = EXEC_NOP;
   14079            5 :       return false;
   14080              :     }
   14081              : 
   14082          200 :   tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
   14083              : 
   14084              :   /* get_temp_from_expression is set up for ordinary assignments. To that
   14085              :      end, where array bounds are not known, arrays are made allocatable.
   14086              :      Change the temporary to a pointer here.  */
   14087          200 :   tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
   14088          200 :   tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
   14089          200 :   tmp_ptr_expr->where = (*code)->loc;
   14090              : 
   14091              :   /* A new charlen is required to ensure that the variable string length
   14092              :      is different to that of the original lhs for deferred results.  */
   14093          200 :   if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
   14094              :     {
   14095           60 :       tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
   14096           60 :       tmp_ptr_expr->ts.deferred = 1;
   14097           60 :       tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
   14098           60 :       gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
   14099           60 :       tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
   14100              :     }
   14101              : 
   14102          400 :   this_code = build_assignment (EXEC_ASSIGN,
   14103              :                                 tmp_ptr_expr, (*code)->expr2,
   14104          200 :                                 NULL, NULL, (*code)->loc);
   14105          200 :   this_code->next = (*code)->next;
   14106          200 :   (*code)->next = this_code;
   14107          200 :   (*code)->op = EXEC_POINTER_ASSIGN;
   14108          200 :   (*code)->expr2 = (*code)->expr1;
   14109          200 :   (*code)->expr1 = tmp_ptr_expr;
   14110              : 
   14111          200 :   return true;
   14112              : }
   14113              : 
   14114              : 
   14115              : /* Deferred character length assignments from an operator expression
   14116              :    require a temporary because the character length of the lhs can
   14117              :    change in the course of the assignment.  */
   14118              : 
   14119              : static bool
   14120       284332 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
   14121              : {
   14122       284332 :   gfc_expr *tmp_expr;
   14123       284332 :   gfc_code *this_code;
   14124              : 
   14125       284332 :   if (!((*code)->expr1->ts.type == BT_CHARACTER
   14126        27120 :          && (*code)->expr1->ts.deferred && (*code)->expr1->rank
   14127          836 :          && (*code)->expr2->ts.type == BT_CHARACTER
   14128          835 :          && (*code)->expr2->expr_type == EXPR_OP))
   14129              :     return false;
   14130              : 
   14131           34 :   if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
   14132              :     return false;
   14133              : 
   14134           28 :   if (gfc_expr_attr ((*code)->expr1).pointer)
   14135              :     return false;
   14136              : 
   14137           22 :   tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
   14138           22 :   tmp_expr->where = (*code)->loc;
   14139              : 
   14140              :   /* A new charlen is required to ensure that the variable string
   14141              :      length is different to that of the original lhs.  */
   14142           22 :   tmp_expr->ts.u.cl = gfc_get_charlen();
   14143           22 :   tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
   14144           22 :   tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
   14145           22 :   (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
   14146              : 
   14147           22 :   tmp_expr->symtree->n.sym->ts.deferred = 1;
   14148              : 
   14149           22 :   this_code = build_assignment (EXEC_ASSIGN,
   14150           22 :                                 (*code)->expr1,
   14151              :                                 gfc_copy_expr (tmp_expr),
   14152              :                                 NULL, NULL, (*code)->loc);
   14153              : 
   14154           22 :   (*code)->expr1 = tmp_expr;
   14155              : 
   14156           22 :   this_code->next = (*code)->next;
   14157           22 :   (*code)->next = this_code;
   14158              : 
   14159           22 :   return true;
   14160              : }
   14161              : 
   14162              : 
   14163              : /* Given a block of code, recursively resolve everything pointed to by this
   14164              :    code block.  */
   14165              : 
   14166              : void
   14167       676100 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
   14168              : {
   14169       676100 :   int omp_workshare_save;
   14170       676100 :   int forall_save, do_concurrent_save;
   14171       676100 :   code_stack frame;
   14172       676100 :   bool t;
   14173              : 
   14174       676100 :   frame.prev = cs_base;
   14175       676100 :   frame.head = code;
   14176       676100 :   cs_base = &frame;
   14177              : 
   14178       676100 :   find_reachable_labels (code);
   14179              : 
   14180      1808291 :   for (; code; code = code->next)
   14181              :     {
   14182      1132192 :       frame.current = code;
   14183      1132192 :       forall_save = forall_flag;
   14184      1132192 :       do_concurrent_save = gfc_do_concurrent_flag;
   14185              : 
   14186      1132192 :       if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
   14187              :         {
   14188         2202 :           if (code->op == EXEC_FORALL)
   14189         1992 :             forall_flag = 1;
   14190          210 :           else if (code->op == EXEC_DO_CONCURRENT)
   14191          210 :             gfc_do_concurrent_flag = 1;
   14192         2202 :           gfc_resolve_forall (code, ns, forall_save);
   14193         2202 :           if (code->op == EXEC_FORALL)
   14194         1992 :             forall_flag = 2;
   14195          210 :           else if (code->op == EXEC_DO_CONCURRENT)
   14196          210 :             gfc_do_concurrent_flag = 2;
   14197              :         }
   14198      1129990 :       else if (code->op == EXEC_OMP_METADIRECTIVE)
   14199          138 :         for (gfc_omp_variant *variant
   14200              :                = code->ext.omp_variants;
   14201          448 :              variant; variant = variant->next)
   14202          310 :           gfc_resolve_code (variant->code, ns);
   14203      1129852 :       else if (code->block)
   14204              :         {
   14205       328658 :           omp_workshare_save = -1;
   14206       328658 :           switch (code->op)
   14207              :             {
   14208        10119 :             case EXEC_OACC_PARALLEL_LOOP:
   14209        10119 :             case EXEC_OACC_PARALLEL:
   14210        10119 :             case EXEC_OACC_KERNELS_LOOP:
   14211        10119 :             case EXEC_OACC_KERNELS:
   14212        10119 :             case EXEC_OACC_SERIAL_LOOP:
   14213        10119 :             case EXEC_OACC_SERIAL:
   14214        10119 :             case EXEC_OACC_DATA:
   14215        10119 :             case EXEC_OACC_HOST_DATA:
   14216        10119 :             case EXEC_OACC_LOOP:
   14217        10119 :               gfc_resolve_oacc_blocks (code, ns);
   14218        10119 :               break;
   14219           54 :             case EXEC_OMP_PARALLEL_WORKSHARE:
   14220           54 :               omp_workshare_save = omp_workshare_flag;
   14221           54 :               omp_workshare_flag = 1;
   14222           54 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14223           54 :               break;
   14224         5977 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14225         5977 :             case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14226         5977 :             case EXEC_OMP_MASKED_TASKLOOP:
   14227         5977 :             case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14228         5977 :             case EXEC_OMP_MASTER_TASKLOOP:
   14229         5977 :             case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14230         5977 :             case EXEC_OMP_PARALLEL:
   14231         5977 :             case EXEC_OMP_PARALLEL_DO:
   14232         5977 :             case EXEC_OMP_PARALLEL_DO_SIMD:
   14233         5977 :             case EXEC_OMP_PARALLEL_LOOP:
   14234         5977 :             case EXEC_OMP_PARALLEL_MASKED:
   14235         5977 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14236         5977 :             case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14237         5977 :             case EXEC_OMP_PARALLEL_MASTER:
   14238         5977 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14239         5977 :             case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14240         5977 :             case EXEC_OMP_PARALLEL_SECTIONS:
   14241         5977 :             case EXEC_OMP_TARGET_PARALLEL:
   14242         5977 :             case EXEC_OMP_TARGET_PARALLEL_DO:
   14243         5977 :             case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14244         5977 :             case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14245         5977 :             case EXEC_OMP_TARGET_TEAMS:
   14246         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14247         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14248         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14249         5977 :             case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14250         5977 :             case EXEC_OMP_TARGET_TEAMS_LOOP:
   14251         5977 :             case EXEC_OMP_TASK:
   14252         5977 :             case EXEC_OMP_TASKLOOP:
   14253         5977 :             case EXEC_OMP_TASKLOOP_SIMD:
   14254         5977 :             case EXEC_OMP_TEAMS:
   14255         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE:
   14256         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14257         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14258         5977 :             case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14259         5977 :             case EXEC_OMP_TEAMS_LOOP:
   14260         5977 :               omp_workshare_save = omp_workshare_flag;
   14261         5977 :               omp_workshare_flag = 0;
   14262         5977 :               gfc_resolve_omp_parallel_blocks (code, ns);
   14263         5977 :               break;
   14264         3063 :             case EXEC_OMP_DISTRIBUTE:
   14265         3063 :             case EXEC_OMP_DISTRIBUTE_SIMD:
   14266         3063 :             case EXEC_OMP_DO:
   14267         3063 :             case EXEC_OMP_DO_SIMD:
   14268         3063 :             case EXEC_OMP_LOOP:
   14269         3063 :             case EXEC_OMP_SIMD:
   14270         3063 :             case EXEC_OMP_TARGET_SIMD:
   14271         3063 :             case EXEC_OMP_TILE:
   14272         3063 :             case EXEC_OMP_UNROLL:
   14273         3063 :               gfc_resolve_omp_do_blocks (code, ns);
   14274         3063 :               break;
   14275              :             case EXEC_SELECT_TYPE:
   14276              :             case EXEC_SELECT_RANK:
   14277              :               /* Blocks are handled in resolve_select_type/rank because we
   14278              :                  have to transform the SELECT TYPE into ASSOCIATE first.  */
   14279              :               break;
   14280              :             case EXEC_DO_CONCURRENT:
   14281              :               gfc_do_concurrent_flag = 1;
   14282              :               gfc_resolve_blocks (code->block, ns);
   14283              :               gfc_do_concurrent_flag = 2;
   14284              :               break;
   14285           39 :             case EXEC_OMP_WORKSHARE:
   14286           39 :               omp_workshare_save = omp_workshare_flag;
   14287           39 :               omp_workshare_flag = 1;
   14288              :               /* FALL THROUGH */
   14289       305418 :             default:
   14290       305418 :               gfc_resolve_blocks (code->block, ns);
   14291       305418 :               break;
   14292              :             }
   14293              : 
   14294       324631 :           if (omp_workshare_save != -1)
   14295         6070 :             omp_workshare_flag = omp_workshare_save;
   14296              :         }
   14297       801194 : start:
   14298      1132397 :       t = true;
   14299      1132397 :       if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
   14300      1130992 :           t = gfc_resolve_expr (code->expr1);
   14301              : 
   14302      1132397 :       forall_flag = forall_save;
   14303      1132397 :       gfc_do_concurrent_flag = do_concurrent_save;
   14304              : 
   14305      1132397 :       if (!gfc_resolve_expr (code->expr2))
   14306          637 :         t = false;
   14307              : 
   14308      1132397 :       if (code->op == EXEC_ALLOCATE
   14309      1132397 :           && !gfc_resolve_expr (code->expr3))
   14310              :         t = false;
   14311              : 
   14312      1132397 :       switch (code->op)
   14313              :         {
   14314              :         case EXEC_NOP:
   14315              :         case EXEC_END_BLOCK:
   14316              :         case EXEC_END_NESTED_BLOCK:
   14317              :         case EXEC_CYCLE:
   14318              :         case EXEC_PAUSE:
   14319              :           break;
   14320              : 
   14321       217078 :         case EXEC_STOP:
   14322       217078 :         case EXEC_ERROR_STOP:
   14323       217078 :           if (code->expr2 != NULL
   14324           37 :               && (code->expr2->ts.type != BT_LOGICAL
   14325           37 :                   || code->expr2->rank != 0))
   14326            0 :             gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
   14327              :                        &code->expr2->where);
   14328              :           break;
   14329              : 
   14330              :         case EXEC_EXIT:
   14331              :         case EXEC_CONTINUE:
   14332              :         case EXEC_DT_END:
   14333              :         case EXEC_ASSIGN_CALL:
   14334              :           break;
   14335              : 
   14336           54 :         case EXEC_CRITICAL:
   14337           54 :           resolve_critical (code);
   14338           54 :           break;
   14339              : 
   14340         1316 :         case EXEC_SYNC_ALL:
   14341         1316 :         case EXEC_SYNC_IMAGES:
   14342         1316 :         case EXEC_SYNC_MEMORY:
   14343         1316 :           resolve_sync (code);
   14344         1316 :           break;
   14345              : 
   14346          197 :         case EXEC_LOCK:
   14347          197 :         case EXEC_UNLOCK:
   14348          197 :         case EXEC_EVENT_POST:
   14349          197 :         case EXEC_EVENT_WAIT:
   14350          197 :           resolve_lock_unlock_event (code);
   14351          197 :           break;
   14352              : 
   14353              :         case EXEC_FAIL_IMAGE:
   14354              :           break;
   14355              : 
   14356          130 :         case EXEC_FORM_TEAM:
   14357          130 :           resolve_form_team (code);
   14358          130 :           break;
   14359              : 
   14360           73 :         case EXEC_CHANGE_TEAM:
   14361           73 :           resolve_change_team (code);
   14362           73 :           break;
   14363              : 
   14364           71 :         case EXEC_END_TEAM:
   14365           71 :           resolve_end_team (code);
   14366           71 :           break;
   14367              : 
   14368           43 :         case EXEC_SYNC_TEAM:
   14369           43 :           resolve_sync_team (code);
   14370           43 :           break;
   14371              : 
   14372         1491 :         case EXEC_ENTRY:
   14373              :           /* Keep track of which entry we are up to.  */
   14374         1491 :           current_entry_id = code->ext.entry->id;
   14375         1491 :           break;
   14376              : 
   14377          453 :         case EXEC_WHERE:
   14378          453 :           resolve_where (code, NULL);
   14379          453 :           break;
   14380              : 
   14381         1250 :         case EXEC_GOTO:
   14382         1250 :           if (code->expr1 != NULL)
   14383              :             {
   14384           78 :               if (code->expr1->expr_type != EXPR_VARIABLE
   14385           76 :                   || code->expr1->ts.type != BT_INTEGER
   14386           76 :                   || (code->expr1->ref
   14387            1 :                       && code->expr1->ref->type == REF_ARRAY)
   14388           75 :                   || code->expr1->symtree == NULL
   14389           75 :                   || (code->expr1->symtree->n.sym
   14390           75 :                       && (code->expr1->symtree->n.sym->attr.flavor
   14391           75 :                           == FL_PARAMETER)))
   14392            4 :                 gfc_error ("ASSIGNED GOTO statement at %L requires a "
   14393              :                            "scalar INTEGER variable", &code->expr1->where);
   14394           74 :               else if (code->expr1->symtree->n.sym
   14395           74 :                        && code->expr1->symtree->n.sym->attr.assign != 1)
   14396            1 :                 gfc_error ("Variable %qs has not been assigned a target "
   14397              :                            "label at %L", code->expr1->symtree->n.sym->name,
   14398              :                            &code->expr1->where);
   14399              :             }
   14400              :           else
   14401         1172 :             resolve_branch (code->label1, code);
   14402              :           break;
   14403              : 
   14404         3224 :         case EXEC_RETURN:
   14405         3224 :           if (code->expr1 != NULL
   14406           53 :                 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
   14407            1 :             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
   14408              :                        "INTEGER return specifier", &code->expr1->where);
   14409              :           break;
   14410              : 
   14411              :         case EXEC_INIT_ASSIGN:
   14412              :         case EXEC_END_PROCEDURE:
   14413              :           break;
   14414              : 
   14415       286669 :         case EXEC_ASSIGN:
   14416       286669 :           if (!t)
   14417              :             break;
   14418              : 
   14419       285994 :           if (flag_coarray == GFC_FCOARRAY_LIB
   14420       285994 :               && gfc_is_coindexed (code->expr1))
   14421              :             {
   14422              :               /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
   14423              :                  coindexed variable.  */
   14424          500 :               code->op = EXEC_CALL;
   14425          500 :               gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
   14426              :                                 true);
   14427          500 :               code->resolved_sym = code->symtree->n.sym;
   14428          500 :               code->resolved_sym->attr.flavor = FL_PROCEDURE;
   14429          500 :               code->resolved_sym->attr.intrinsic = 1;
   14430          500 :               code->resolved_sym->attr.subroutine = 1;
   14431          500 :               code->resolved_isym
   14432          500 :                 = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
   14433          500 :               gfc_commit_symbol (code->resolved_sym);
   14434          500 :               code->ext.actual = gfc_get_actual_arglist ();
   14435          500 :               code->ext.actual->expr = code->expr1;
   14436          500 :               code->ext.actual->next = gfc_get_actual_arglist ();
   14437          500 :               if (code->expr2->expr_type != EXPR_VARIABLE
   14438          500 :                   && code->expr2->expr_type != EXPR_CONSTANT)
   14439              :                 {
   14440              :                   /* Convert assignments of expr1[...] = expr2 into
   14441              :                         tvar = expr2
   14442              :                         expr1[...] = tvar
   14443              :                      when expr2 is not trivial.  */
   14444           54 :                   gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
   14445           54 :                   gfc_code next_code = *code;
   14446           54 :                   gfc_code *rhs_code
   14447          108 :                     = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
   14448           54 :                                         NULL, code->expr2->where);
   14449           54 :                   *code = *rhs_code;
   14450           54 :                   code->next = rhs_code;
   14451           54 :                   *rhs_code = next_code;
   14452              : 
   14453           54 :                   rhs_code->ext.actual->next->expr = tvar;
   14454           54 :                   rhs_code->expr1 = NULL;
   14455           54 :                   rhs_code->expr2 = NULL;
   14456              :                 }
   14457              :               else
   14458              :                 {
   14459          446 :                   code->ext.actual->next->expr = code->expr2;
   14460              : 
   14461          446 :                   code->expr1 = NULL;
   14462          446 :                   code->expr2 = NULL;
   14463              :                 }
   14464              :               break;
   14465              :             }
   14466              : 
   14467       285494 :           if (code->expr1->ts.type == BT_CLASS)
   14468         1114 :             gfc_find_vtab (&code->expr2->ts);
   14469              : 
   14470              :           /* If this is a pointer function in an lvalue variable context,
   14471              :              the new code will have to be resolved afresh. This is also the
   14472              :              case with an error, where the code is transformed into NOP to
   14473              :              prevent ICEs downstream.  */
   14474       285494 :           if (resolve_ptr_fcn_assign (&code, ns)
   14475       285494 :               || code->op == EXEC_NOP)
   14476          205 :             goto start;
   14477              : 
   14478       285289 :           if (!gfc_check_vardef_context (code->expr1, false, false, false,
   14479       285289 :                                          _("assignment")))
   14480              :             break;
   14481              : 
   14482       285250 :           if (resolve_ordinary_assign (code, ns))
   14483              :             {
   14484          918 :               if (omp_workshare_flag)
   14485              :                 {
   14486            1 :                   gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
   14487            1 :                              "at %L", &code->loc);
   14488            1 :                   break;
   14489              :                 }
   14490          917 :               if (code->op == EXEC_COMPCALL)
   14491          449 :                 goto compcall;
   14492              :               else
   14493          468 :                 goto call;
   14494              :             }
   14495              : 
   14496              :           /* Check for dependencies in deferred character length array
   14497              :              assignments and generate a temporary, if necessary.  */
   14498       284332 :           if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
   14499              :             break;
   14500              : 
   14501              :           /* F03 7.4.1.3 for non-allocatable, non-pointer components.  */
   14502       284310 :           if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
   14503         7174 :               && code->expr1->ts.u.derived
   14504         7174 :               && code->expr1->ts.u.derived->attr.defined_assign_comp)
   14505          188 :             generate_component_assignments (&code, ns);
   14506       284122 :           else if (code->op == EXEC_ASSIGN)
   14507              :             {
   14508       284122 :               if (gfc_may_be_finalized (code->expr1->ts))
   14509         1253 :                 code->expr1->must_finalize = 1;
   14510       284122 :               if (code->expr2->expr_type == EXPR_ARRAY
   14511       284122 :                   && gfc_may_be_finalized (code->expr2->ts))
   14512           49 :                 code->expr2->must_finalize = 1;
   14513              :             }
   14514              : 
   14515              :           break;
   14516              : 
   14517          126 :         case EXEC_LABEL_ASSIGN:
   14518          126 :           if (code->label1->defined == ST_LABEL_UNKNOWN)
   14519            0 :             gfc_error ("Label %d referenced at %L is never defined",
   14520              :                        code->label1->value, &code->label1->where);
   14521          126 :           if (t
   14522          126 :               && (code->expr1->expr_type != EXPR_VARIABLE
   14523          126 :                   || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
   14524          126 :                   || code->expr1->symtree->n.sym->ts.kind
   14525          126 :                      != gfc_default_integer_kind
   14526          126 :                   || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
   14527          125 :                   || code->expr1->symtree->n.sym->as != NULL))
   14528            2 :             gfc_error ("ASSIGN statement at %L requires a scalar "
   14529              :                        "default INTEGER variable", &code->expr1->where);
   14530              :           break;
   14531              : 
   14532        10429 :         case EXEC_POINTER_ASSIGN:
   14533        10429 :           {
   14534        10429 :             gfc_expr* e;
   14535              : 
   14536        10429 :             if (!t)
   14537              :               break;
   14538              : 
   14539              :             /* This is both a variable definition and pointer assignment
   14540              :                context, so check both of them.  For rank remapping, a final
   14541              :                array ref may be present on the LHS and fool gfc_expr_attr
   14542              :                used in gfc_check_vardef_context.  Remove it.  */
   14543        10424 :             e = remove_last_array_ref (code->expr1);
   14544        20848 :             t = gfc_check_vardef_context (e, true, false, false,
   14545        10424 :                                           _("pointer assignment"));
   14546        10424 :             if (t)
   14547        10395 :               t = gfc_check_vardef_context (e, false, false, false,
   14548        10395 :                                             _("pointer assignment"));
   14549        10424 :             gfc_free_expr (e);
   14550              : 
   14551      1142473 :             t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
   14552              : 
   14553        10282 :             if (!t)
   14554              :               break;
   14555              : 
   14556              :             /* Assigning a class object always is a regular assign.  */
   14557        10282 :             if (code->expr2->ts.type == BT_CLASS
   14558          581 :                 && code->expr1->ts.type == BT_CLASS
   14559          490 :                 && CLASS_DATA (code->expr2)
   14560          489 :                 && !CLASS_DATA (code->expr2)->attr.dimension
   14561        10918 :                 && !(gfc_expr_attr (code->expr1).proc_pointer
   14562           55 :                      && code->expr2->expr_type == EXPR_VARIABLE
   14563           43 :                      && code->expr2->symtree->n.sym->attr.flavor
   14564           43 :                         == FL_PROCEDURE))
   14565          339 :               code->op = EXEC_ASSIGN;
   14566              :             break;
   14567              :           }
   14568              : 
   14569           72 :         case EXEC_ARITHMETIC_IF:
   14570           72 :           {
   14571           72 :             gfc_expr *e = code->expr1;
   14572              : 
   14573           72 :             gfc_resolve_expr (e);
   14574           72 :             if (e->expr_type == EXPR_NULL)
   14575            1 :               gfc_error ("Invalid NULL at %L", &e->where);
   14576              : 
   14577           72 :             if (t && (e->rank > 0
   14578           68 :                       || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
   14579            5 :               gfc_error ("Arithmetic IF statement at %L requires a scalar "
   14580              :                          "REAL or INTEGER expression", &e->where);
   14581              : 
   14582           72 :             resolve_branch (code->label1, code);
   14583           72 :             resolve_branch (code->label2, code);
   14584           72 :             resolve_branch (code->label3, code);
   14585              :           }
   14586           72 :           break;
   14587              : 
   14588       230543 :         case EXEC_IF:
   14589       230543 :           if (t && code->expr1 != NULL
   14590            0 :               && (code->expr1->ts.type != BT_LOGICAL
   14591            0 :                   || code->expr1->rank != 0))
   14592            0 :             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
   14593              :                        &code->expr1->where);
   14594              :           break;
   14595              : 
   14596        79564 :         case EXEC_CALL:
   14597        79564 :         call:
   14598        79564 :           resolve_call (code);
   14599        79564 :           break;
   14600              : 
   14601         1730 :         case EXEC_COMPCALL:
   14602         1730 :         compcall:
   14603         1730 :           resolve_typebound_subroutine (code);
   14604         1730 :           break;
   14605              : 
   14606          124 :         case EXEC_CALL_PPC:
   14607          124 :           resolve_ppc_call (code);
   14608          124 :           break;
   14609              : 
   14610          687 :         case EXEC_SELECT:
   14611              :           /* Select is complicated. Also, a SELECT construct could be
   14612              :              a transformed computed GOTO.  */
   14613          687 :           resolve_select (code, false);
   14614          687 :           break;
   14615              : 
   14616         3029 :         case EXEC_SELECT_TYPE:
   14617         3029 :           resolve_select_type (code, ns);
   14618         3029 :           break;
   14619              : 
   14620         1024 :         case EXEC_SELECT_RANK:
   14621         1024 :           resolve_select_rank (code, ns);
   14622         1024 :           break;
   14623              : 
   14624         7952 :         case EXEC_BLOCK:
   14625         7952 :           resolve_block_construct (code);
   14626         7952 :           break;
   14627              : 
   14628        32764 :         case EXEC_DO:
   14629        32764 :           if (code->ext.iterator != NULL)
   14630              :             {
   14631        32764 :               gfc_iterator *iter = code->ext.iterator;
   14632        32764 :               if (gfc_resolve_iterator (iter, true, false))
   14633        32750 :                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
   14634              :                                          true);
   14635              :             }
   14636              :           break;
   14637              : 
   14638          531 :         case EXEC_DO_WHILE:
   14639          531 :           if (code->expr1 == NULL)
   14640            0 :             gfc_internal_error ("gfc_resolve_code(): No expression on "
   14641              :                                 "DO WHILE");
   14642          531 :           if (t
   14643          531 :               && (code->expr1->rank != 0
   14644          531 :                   || code->expr1->ts.type != BT_LOGICAL))
   14645            0 :             gfc_error ("Exit condition of DO WHILE loop at %L must be "
   14646              :                        "a scalar LOGICAL expression", &code->expr1->where);
   14647              :           break;
   14648              : 
   14649        14283 :         case EXEC_ALLOCATE:
   14650        14283 :           if (t)
   14651        14281 :             resolve_allocate_deallocate (code, "ALLOCATE");
   14652              : 
   14653              :           break;
   14654              : 
   14655         6074 :         case EXEC_DEALLOCATE:
   14656         6074 :           if (t)
   14657         6074 :             resolve_allocate_deallocate (code, "DEALLOCATE");
   14658              : 
   14659              :           break;
   14660              : 
   14661         3906 :         case EXEC_OPEN:
   14662         3906 :           if (!gfc_resolve_open (code->ext.open, &code->loc))
   14663              :             break;
   14664              : 
   14665         3679 :           resolve_branch (code->ext.open->err, code);
   14666         3679 :           break;
   14667              : 
   14668         3094 :         case EXEC_CLOSE:
   14669         3094 :           if (!gfc_resolve_close (code->ext.close, &code->loc))
   14670              :             break;
   14671              : 
   14672         3060 :           resolve_branch (code->ext.close->err, code);
   14673         3060 :           break;
   14674              : 
   14675         2809 :         case EXEC_BACKSPACE:
   14676         2809 :         case EXEC_ENDFILE:
   14677         2809 :         case EXEC_REWIND:
   14678         2809 :         case EXEC_FLUSH:
   14679         2809 :           if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
   14680              :             break;
   14681              : 
   14682         2743 :           resolve_branch (code->ext.filepos->err, code);
   14683         2743 :           break;
   14684              : 
   14685          836 :         case EXEC_INQUIRE:
   14686          836 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14687              :               break;
   14688              : 
   14689          788 :           resolve_branch (code->ext.inquire->err, code);
   14690          788 :           break;
   14691              : 
   14692           92 :         case EXEC_IOLENGTH:
   14693           92 :           gcc_assert (code->ext.inquire != NULL);
   14694           92 :           if (!gfc_resolve_inquire (code->ext.inquire))
   14695              :             break;
   14696              : 
   14697           90 :           resolve_branch (code->ext.inquire->err, code);
   14698           90 :           break;
   14699              : 
   14700           89 :         case EXEC_WAIT:
   14701           89 :           if (!gfc_resolve_wait (code->ext.wait))
   14702              :             break;
   14703              : 
   14704           74 :           resolve_branch (code->ext.wait->err, code);
   14705           74 :           resolve_branch (code->ext.wait->end, code);
   14706           74 :           resolve_branch (code->ext.wait->eor, code);
   14707           74 :           break;
   14708              : 
   14709        32535 :         case EXEC_READ:
   14710        32535 :         case EXEC_WRITE:
   14711        32535 :           if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
   14712              :             break;
   14713              : 
   14714        32227 :           resolve_branch (code->ext.dt->err, code);
   14715        32227 :           resolve_branch (code->ext.dt->end, code);
   14716        32227 :           resolve_branch (code->ext.dt->eor, code);
   14717        32227 :           break;
   14718              : 
   14719        46546 :         case EXEC_TRANSFER:
   14720        46546 :           resolve_transfer (code);
   14721        46546 :           break;
   14722              : 
   14723         2202 :         case EXEC_DO_CONCURRENT:
   14724         2202 :         case EXEC_FORALL:
   14725         2202 :           resolve_forall_iterators (code->ext.concur.forall_iterator);
   14726              : 
   14727         2202 :           if (code->expr1 != NULL
   14728          732 :               && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
   14729            2 :             gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
   14730              :                        "expression", &code->expr1->where);
   14731              : 
   14732         2202 :     if (code->op == EXEC_DO_CONCURRENT)
   14733          210 :       resolve_locality_spec (code, ns);
   14734              :           break;
   14735              : 
   14736        13168 :         case EXEC_OACC_PARALLEL_LOOP:
   14737        13168 :         case EXEC_OACC_PARALLEL:
   14738        13168 :         case EXEC_OACC_KERNELS_LOOP:
   14739        13168 :         case EXEC_OACC_KERNELS:
   14740        13168 :         case EXEC_OACC_SERIAL_LOOP:
   14741        13168 :         case EXEC_OACC_SERIAL:
   14742        13168 :         case EXEC_OACC_DATA:
   14743        13168 :         case EXEC_OACC_HOST_DATA:
   14744        13168 :         case EXEC_OACC_LOOP:
   14745        13168 :         case EXEC_OACC_UPDATE:
   14746        13168 :         case EXEC_OACC_WAIT:
   14747        13168 :         case EXEC_OACC_CACHE:
   14748        13168 :         case EXEC_OACC_ENTER_DATA:
   14749        13168 :         case EXEC_OACC_EXIT_DATA:
   14750        13168 :         case EXEC_OACC_ATOMIC:
   14751        13168 :         case EXEC_OACC_DECLARE:
   14752        13168 :           gfc_resolve_oacc_directive (code, ns);
   14753        13168 :           break;
   14754              : 
   14755        16907 :         case EXEC_OMP_ALLOCATE:
   14756        16907 :         case EXEC_OMP_ALLOCATORS:
   14757        16907 :         case EXEC_OMP_ASSUME:
   14758        16907 :         case EXEC_OMP_ATOMIC:
   14759        16907 :         case EXEC_OMP_BARRIER:
   14760        16907 :         case EXEC_OMP_CANCEL:
   14761        16907 :         case EXEC_OMP_CANCELLATION_POINT:
   14762        16907 :         case EXEC_OMP_CRITICAL:
   14763        16907 :         case EXEC_OMP_FLUSH:
   14764        16907 :         case EXEC_OMP_DEPOBJ:
   14765        16907 :         case EXEC_OMP_DISPATCH:
   14766        16907 :         case EXEC_OMP_DISTRIBUTE:
   14767        16907 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
   14768        16907 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
   14769        16907 :         case EXEC_OMP_DISTRIBUTE_SIMD:
   14770        16907 :         case EXEC_OMP_DO:
   14771        16907 :         case EXEC_OMP_DO_SIMD:
   14772        16907 :         case EXEC_OMP_ERROR:
   14773        16907 :         case EXEC_OMP_INTEROP:
   14774        16907 :         case EXEC_OMP_LOOP:
   14775        16907 :         case EXEC_OMP_MASTER:
   14776        16907 :         case EXEC_OMP_MASTER_TASKLOOP:
   14777        16907 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
   14778        16907 :         case EXEC_OMP_MASKED:
   14779        16907 :         case EXEC_OMP_MASKED_TASKLOOP:
   14780        16907 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
   14781        16907 :         case EXEC_OMP_METADIRECTIVE:
   14782        16907 :         case EXEC_OMP_ORDERED:
   14783        16907 :         case EXEC_OMP_SCAN:
   14784        16907 :         case EXEC_OMP_SCOPE:
   14785        16907 :         case EXEC_OMP_SECTIONS:
   14786        16907 :         case EXEC_OMP_SIMD:
   14787        16907 :         case EXEC_OMP_SINGLE:
   14788        16907 :         case EXEC_OMP_TARGET:
   14789        16907 :         case EXEC_OMP_TARGET_DATA:
   14790        16907 :         case EXEC_OMP_TARGET_ENTER_DATA:
   14791        16907 :         case EXEC_OMP_TARGET_EXIT_DATA:
   14792        16907 :         case EXEC_OMP_TARGET_PARALLEL:
   14793        16907 :         case EXEC_OMP_TARGET_PARALLEL_DO:
   14794        16907 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
   14795        16907 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
   14796        16907 :         case EXEC_OMP_TARGET_SIMD:
   14797        16907 :         case EXEC_OMP_TARGET_TEAMS:
   14798        16907 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
   14799        16907 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14800        16907 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14801        16907 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
   14802        16907 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
   14803        16907 :         case EXEC_OMP_TARGET_UPDATE:
   14804        16907 :         case EXEC_OMP_TASK:
   14805        16907 :         case EXEC_OMP_TASKGROUP:
   14806        16907 :         case EXEC_OMP_TASKLOOP:
   14807        16907 :         case EXEC_OMP_TASKLOOP_SIMD:
   14808        16907 :         case EXEC_OMP_TASKWAIT:
   14809        16907 :         case EXEC_OMP_TASKYIELD:
   14810        16907 :         case EXEC_OMP_TEAMS:
   14811        16907 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
   14812        16907 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
   14813        16907 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
   14814        16907 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
   14815        16907 :         case EXEC_OMP_TEAMS_LOOP:
   14816        16907 :         case EXEC_OMP_TILE:
   14817        16907 :         case EXEC_OMP_UNROLL:
   14818        16907 :         case EXEC_OMP_WORKSHARE:
   14819        16907 :           gfc_resolve_omp_directive (code, ns);
   14820        16907 :           break;
   14821              : 
   14822         3888 :         case EXEC_OMP_PARALLEL:
   14823         3888 :         case EXEC_OMP_PARALLEL_DO:
   14824         3888 :         case EXEC_OMP_PARALLEL_DO_SIMD:
   14825         3888 :         case EXEC_OMP_PARALLEL_LOOP:
   14826         3888 :         case EXEC_OMP_PARALLEL_MASKED:
   14827         3888 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
   14828         3888 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
   14829         3888 :         case EXEC_OMP_PARALLEL_MASTER:
   14830         3888 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
   14831         3888 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
   14832         3888 :         case EXEC_OMP_PARALLEL_SECTIONS:
   14833         3888 :         case EXEC_OMP_PARALLEL_WORKSHARE:
   14834         3888 :           omp_workshare_save = omp_workshare_flag;
   14835         3888 :           omp_workshare_flag = 0;
   14836         3888 :           gfc_resolve_omp_directive (code, ns);
   14837         3888 :           omp_workshare_flag = omp_workshare_save;
   14838         3888 :           break;
   14839              : 
   14840            0 :         default:
   14841            0 :           gfc_internal_error ("gfc_resolve_code(): Bad statement code");
   14842              :         }
   14843              :     }
   14844              : 
   14845       676099 :   cs_base = frame.prev;
   14846       676099 : }
   14847              : 
   14848              : 
   14849              : /* Resolve initial values and make sure they are compatible with
   14850              :    the variable.  */
   14851              : 
   14852              : static void
   14853      1849142 : resolve_values (gfc_symbol *sym)
   14854              : {
   14855      1849142 :   bool t;
   14856              : 
   14857      1849142 :   if (sym->value == NULL)
   14858              :     return;
   14859              : 
   14860       416473 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
   14861           14 :     gfc_warning (OPT_Wdeprecated_declarations,
   14862              :                  "Using parameter %qs declared at %L is deprecated",
   14863              :                  sym->name, &sym->declared_at);
   14864              : 
   14865       416473 :   if (sym->value->expr_type == EXPR_STRUCTURE)
   14866        39738 :     t= resolve_structure_cons (sym->value, 1);
   14867              :   else
   14868       376735 :     t = gfc_resolve_expr (sym->value);
   14869              : 
   14870       416473 :   if (!t)
   14871              :     return;
   14872              : 
   14873       416471 :   gfc_check_assign_symbol (sym, NULL, sym->value);
   14874              : }
   14875              : 
   14876              : 
   14877              : /* Verify any BIND(C) derived types in the namespace so we can report errors
   14878              :    for them once, rather than for each variable declared of that type.  */
   14879              : 
   14880              : static void
   14881      1819859 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
   14882              : {
   14883      1819859 :   if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
   14884        83242 :       && derived_sym->attr.is_bind_c == 1)
   14885        27032 :     verify_bind_c_derived_type (derived_sym);
   14886              : 
   14887      1819859 :   return;
   14888              : }
   14889              : 
   14890              : 
   14891              : /* Check the interfaces of DTIO procedures associated with derived
   14892              :    type 'sym'.  These procedures can either have typebound bindings or
   14893              :    can appear in DTIO generic interfaces.  */
   14894              : 
   14895              : static void
   14896      1850112 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
   14897              : {
   14898      1850112 :   if (!sym || sym->attr.flavor != FL_DERIVED)
   14899              :     return;
   14900              : 
   14901        92542 :   gfc_check_dtio_interfaces (sym);
   14902              : 
   14903        92542 :   return;
   14904              : }
   14905              : 
   14906              : /* Verify that any binding labels used in a given namespace do not collide
   14907              :    with the names or binding labels of any global symbols.  Multiple INTERFACE
   14908              :    for the same procedure are permitted.  Abstract interfaces and dummy
   14909              :    arguments are not checked.  */
   14910              : 
   14911              : static void
   14912      1850112 : gfc_verify_binding_labels (gfc_symbol *sym)
   14913              : {
   14914      1850112 :   gfc_gsymbol *gsym;
   14915      1850112 :   const char *module;
   14916              : 
   14917      1850112 :   if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
   14918        62023 :       || sym->attr.flavor == FL_DERIVED || !sym->binding_label
   14919        34063 :       || sym->attr.abstract || sym->attr.dummy)
   14920              :     return;
   14921              : 
   14922        33927 :   gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
   14923              : 
   14924        33927 :   if (sym->module)
   14925              :     module = sym->module;
   14926        12085 :   else if (sym->ns && sym->ns->proc_name
   14927        12085 :            && sym->ns->proc_name->attr.flavor == FL_MODULE)
   14928         4511 :     module = sym->ns->proc_name->name;
   14929         7574 :   else if (sym->ns && sym->ns->parent
   14930          358 :            && sym->ns && sym->ns->parent->proc_name
   14931          358 :            && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   14932          272 :     module = sym->ns->parent->proc_name->name;
   14933              :   else
   14934              :     module = NULL;
   14935              : 
   14936        33927 :   if (!gsym
   14937        11459 :       || (!gsym->defined
   14938         8511 :           && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
   14939              :     {
   14940        22468 :       if (!gsym)
   14941        22468 :         gsym = gfc_get_gsymbol (sym->binding_label, true);
   14942        30979 :       gsym->where = sym->declared_at;
   14943        30979 :       gsym->sym_name = sym->name;
   14944        30979 :       gsym->binding_label = sym->binding_label;
   14945        30979 :       gsym->ns = sym->ns;
   14946        30979 :       gsym->mod_name = module;
   14947        30979 :       if (sym->attr.function)
   14948        20077 :         gsym->type = GSYM_FUNCTION;
   14949        10902 :       else if (sym->attr.subroutine)
   14950        10763 :         gsym->type = GSYM_SUBROUTINE;
   14951              :       /* Mark as variable/procedure as defined, unless its an INTERFACE.  */
   14952        30979 :       gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
   14953        30979 :       return;
   14954              :     }
   14955              : 
   14956         2948 :   if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
   14957              :     {
   14958            1 :       gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
   14959              :                  "identifier as entity at %L", sym->name,
   14960              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14961              :       /* Clear the binding label to prevent checking multiple times.  */
   14962            1 :       sym->binding_label = NULL;
   14963            1 :       return;
   14964              :     }
   14965              : 
   14966         2947 :   if (sym->attr.flavor == FL_VARIABLE && module
   14967           37 :       && (strcmp (module, gsym->mod_name) != 0
   14968           35 :           || strcmp (sym->name, gsym->sym_name) != 0))
   14969              :     {
   14970              :       /* This can only happen if the variable is defined in a module - if it
   14971              :          isn't the same module, reject it.  */
   14972            3 :       gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
   14973              :                  "uses the same global identifier as entity at %L from module %qs",
   14974              :                  sym->name, module, sym->binding_label,
   14975              :                  &sym->declared_at, &gsym->where, gsym->mod_name);
   14976            3 :       sym->binding_label = NULL;
   14977            3 :       return;
   14978              :     }
   14979              : 
   14980         2944 :   if ((sym->attr.function || sym->attr.subroutine)
   14981         2908 :       && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
   14982         2906 :            || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
   14983         2521 :       && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
   14984         2091 :       && (module != gsym->mod_name
   14985         2087 :           || strcmp (gsym->sym_name, sym->name) != 0
   14986         2087 :           || (module && strcmp (module, gsym->mod_name) != 0)))
   14987              :     {
   14988              :       /* Print an error if the procedure is defined multiple times; we have to
   14989              :          exclude references to the same procedure via module association or
   14990              :          multiple checks for the same procedure.  */
   14991            4 :       gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
   14992              :                  "global identifier as entity at %L", sym->name,
   14993              :                  sym->binding_label, &sym->declared_at, &gsym->where);
   14994            4 :       sym->binding_label = NULL;
   14995              :     }
   14996              : }
   14997              : 
   14998              : 
   14999              : /* Resolve an index expression.  */
   15000              : 
   15001              : static bool
   15002       265328 : resolve_index_expr (gfc_expr *e)
   15003              : {
   15004       265328 :   if (!gfc_resolve_expr (e))
   15005              :     return false;
   15006              : 
   15007       265318 :   if (!gfc_simplify_expr (e, 0))
   15008              :     return false;
   15009              : 
   15010       265316 :   if (!gfc_specification_expr (e))
   15011              :     return false;
   15012              : 
   15013              :   return true;
   15014              : }
   15015              : 
   15016              : 
   15017              : /* Resolve a charlen structure.  */
   15018              : 
   15019              : static bool
   15020       103530 : resolve_charlen (gfc_charlen *cl)
   15021              : {
   15022       103530 :   int k;
   15023       103530 :   bool saved_specification_expr;
   15024              : 
   15025       103530 :   if (cl->resolved)
   15026              :     return true;
   15027              : 
   15028        95049 :   cl->resolved = 1;
   15029        95049 :   saved_specification_expr = specification_expr;
   15030        95049 :   specification_expr = true;
   15031              : 
   15032        95049 :   if (cl->length_from_typespec)
   15033              :     {
   15034         2113 :       if (!gfc_resolve_expr (cl->length))
   15035              :         {
   15036            1 :           specification_expr = saved_specification_expr;
   15037            1 :           return false;
   15038              :         }
   15039              : 
   15040         2112 :       if (!gfc_simplify_expr (cl->length, 0))
   15041              :         {
   15042            0 :           specification_expr = saved_specification_expr;
   15043            0 :           return false;
   15044              :         }
   15045              : 
   15046              :       /* cl->length has been resolved.  It should have an integer type.  */
   15047         2112 :       if (cl->length
   15048         2111 :           && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
   15049              :         {
   15050            4 :           gfc_error ("Scalar INTEGER expression expected at %L",
   15051              :                      &cl->length->where);
   15052            4 :           return false;
   15053              :         }
   15054              :     }
   15055              :   else
   15056              :     {
   15057        92936 :       if (!resolve_index_expr (cl->length))
   15058              :         {
   15059           19 :           specification_expr = saved_specification_expr;
   15060           19 :           return false;
   15061              :         }
   15062              :     }
   15063              : 
   15064              :   /* F2008, 4.4.3.2:  If the character length parameter value evaluates to
   15065              :      a negative value, the length of character entities declared is zero.  */
   15066        95025 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15067        56564 :       && mpz_sgn (cl->length->value.integer) < 0)
   15068            0 :     gfc_replace_expr (cl->length,
   15069              :                       gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
   15070              : 
   15071              :   /* Check that the character length is not too large.  */
   15072        95025 :   k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
   15073        95025 :   if (cl->length && cl->length->expr_type == EXPR_CONSTANT
   15074        56564 :       && cl->length->ts.type == BT_INTEGER
   15075        56564 :       && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
   15076              :     {
   15077            4 :       gfc_error ("String length at %L is too large", &cl->length->where);
   15078            4 :       specification_expr = saved_specification_expr;
   15079            4 :       return false;
   15080              :     }
   15081              : 
   15082        95021 :   specification_expr = saved_specification_expr;
   15083        95021 :   return true;
   15084              : }
   15085              : 
   15086              : 
   15087              : /* Test for non-constant shape arrays.  */
   15088              : 
   15089              : static bool
   15090       117641 : is_non_constant_shape_array (gfc_symbol *sym)
   15091              : {
   15092       117641 :   gfc_expr *e;
   15093       117641 :   int i;
   15094       117641 :   bool not_constant;
   15095              : 
   15096       117641 :   not_constant = false;
   15097       117641 :   if (sym->as != NULL)
   15098              :     {
   15099              :       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
   15100              :          has not been simplified; parameter array references.  Do the
   15101              :          simplification now.  */
   15102       155182 :       for (i = 0; i < sym->as->rank + sym->as->corank; i++)
   15103              :         {
   15104        89611 :           if (i == GFC_MAX_DIMENSIONS)
   15105              :             break;
   15106              : 
   15107        89609 :           e = sym->as->lower[i];
   15108        89609 :           if (e && (!resolve_index_expr(e)
   15109        86792 :                     || !gfc_is_constant_expr (e)))
   15110              :             not_constant = true;
   15111        89609 :           e = sym->as->upper[i];
   15112        89609 :           if (e && (!resolve_index_expr(e)
   15113        85572 :                     || !gfc_is_constant_expr (e)))
   15114              :             not_constant = true;
   15115              :         }
   15116              :     }
   15117       117641 :   return not_constant;
   15118              : }
   15119              : 
   15120              : /* Given a symbol and an initialization expression, add code to initialize
   15121              :    the symbol to the function entry.  */
   15122              : static void
   15123         2099 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
   15124              : {
   15125         2099 :   gfc_expr *lval;
   15126         2099 :   gfc_code *init_st;
   15127         2099 :   gfc_namespace *ns = sym->ns;
   15128              : 
   15129         2099 :   if (sym->attr.function && sym->result == sym && IS_PDT (sym))
   15130              :     {
   15131           46 :       gfc_free_expr (init);
   15132           46 :       return;
   15133              :     }
   15134              : 
   15135              :   /* Search for the function namespace if this is a contained
   15136              :      function without an explicit result.  */
   15137         2053 :   if (sym->attr.function && sym == sym->result
   15138          299 :       && sym->name != sym->ns->proc_name->name)
   15139              :     {
   15140          298 :       ns = ns->contained;
   15141         1376 :       for (;ns; ns = ns->sibling)
   15142         1315 :         if (strcmp (ns->proc_name->name, sym->name) == 0)
   15143              :           break;
   15144              :     }
   15145              : 
   15146         2053 :   if (ns == NULL)
   15147              :     {
   15148           61 :       gfc_free_expr (init);
   15149           61 :       return;
   15150              :     }
   15151              : 
   15152              :   /* Build an l-value expression for the result.  */
   15153         1992 :   lval = gfc_lval_expr_from_sym (sym);
   15154              : 
   15155              :   /* Add the code at scope entry.  */
   15156         1992 :   init_st = gfc_get_code (EXEC_INIT_ASSIGN);
   15157         1992 :   init_st->next = ns->code;
   15158         1992 :   ns->code = init_st;
   15159              : 
   15160              :   /* Assign the default initializer to the l-value.  */
   15161         1992 :   init_st->loc = sym->declared_at;
   15162         1992 :   init_st->expr1 = lval;
   15163         1992 :   init_st->expr2 = init;
   15164              : }
   15165              : 
   15166              : 
   15167              : /* Whether or not we can generate a default initializer for a symbol.  */
   15168              : 
   15169              : static bool
   15170        30079 : can_generate_init (gfc_symbol *sym)
   15171              : {
   15172        30079 :   symbol_attribute *a;
   15173        30079 :   if (!sym)
   15174              :     return false;
   15175        30079 :   a = &sym->attr;
   15176              : 
   15177              :   /* These symbols should never have a default initialization.  */
   15178        49460 :   return !(
   15179        30079 :        a->allocatable
   15180        30079 :     || a->external
   15181        28920 :     || a->pointer
   15182        28920 :     || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
   15183         5704 :         && (CLASS_DATA (sym)->attr.class_pointer
   15184         3749 :             || CLASS_DATA (sym)->attr.proc_pointer))
   15185        26965 :     || a->in_equivalence
   15186        26844 :     || a->in_common
   15187        26797 :     || a->data
   15188        26619 :     || sym->module
   15189        22791 :     || a->cray_pointee
   15190        22729 :     || a->cray_pointer
   15191        22729 :     || sym->assoc
   15192        20051 :     || (!a->referenced && !a->result)
   15193        19381 :     || (a->dummy && (a->intent != INTENT_OUT
   15194         1081 :                      || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
   15195        19381 :     || (a->function && sym != sym->result)
   15196              :   );
   15197              : }
   15198              : 
   15199              : 
   15200              : /* Assign the default initializer to a derived type variable or result.  */
   15201              : 
   15202              : static void
   15203        11492 : apply_default_init (gfc_symbol *sym)
   15204              : {
   15205        11492 :   gfc_expr *init = NULL;
   15206              : 
   15207        11492 :   if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15208              :     return;
   15209              : 
   15210        11247 :   if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
   15211        10394 :     init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15212              : 
   15213        11247 :   if (init == NULL && sym->ts.type != BT_CLASS)
   15214              :     return;
   15215              : 
   15216         1717 :   build_init_assign (sym, init);
   15217         1717 :   sym->attr.referenced = 1;
   15218              : }
   15219              : 
   15220              : 
   15221              : /* Build an initializer for a local. Returns null if the symbol should not have
   15222              :    a default initialization.  */
   15223              : 
   15224              : static gfc_expr *
   15225       204311 : build_default_init_expr (gfc_symbol *sym)
   15226              : {
   15227              :   /* These symbols should never have a default initialization.  */
   15228       204311 :   if (sym->attr.allocatable
   15229       190597 :       || sym->attr.external
   15230       190597 :       || sym->attr.dummy
   15231       125242 :       || sym->attr.pointer
   15232       117131 :       || sym->attr.in_equivalence
   15233       114755 :       || sym->attr.in_common
   15234       111654 :       || sym->attr.data
   15235       109356 :       || sym->module
   15236       106827 :       || sym->attr.cray_pointee
   15237       106526 :       || sym->attr.cray_pointer
   15238       106224 :       || sym->assoc)
   15239              :     return NULL;
   15240              : 
   15241              :   /* Get the appropriate init expression.  */
   15242       101486 :   return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
   15243              : }
   15244              : 
   15245              : /* Add an initialization expression to a local variable.  */
   15246              : static void
   15247       204311 : apply_default_init_local (gfc_symbol *sym)
   15248              : {
   15249       204311 :   gfc_expr *init = NULL;
   15250              : 
   15251              :   /* The symbol should be a variable or a function return value.  */
   15252       204311 :   if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
   15253       204311 :       || (sym->attr.function && sym->result != sym))
   15254              :     return;
   15255              : 
   15256              :   /* Try to build the initializer expression.  If we can't initialize
   15257              :      this symbol, then init will be NULL.  */
   15258       204311 :   init = build_default_init_expr (sym);
   15259       204311 :   if (init == NULL)
   15260              :     return;
   15261              : 
   15262              :   /* For saved variables, we don't want to add an initializer at function
   15263              :      entry, so we just add a static initializer. Note that automatic variables
   15264              :      are stack allocated even with -fno-automatic; we have also to exclude
   15265              :      result variable, which are also nonstatic.  */
   15266          419 :   if (!sym->attr.automatic
   15267          419 :       && (sym->attr.save || sym->ns->save_all
   15268          377 :           || (flag_max_stack_var_size == 0 && !sym->attr.result
   15269           27 :               && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
   15270           14 :               && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
   15271              :     {
   15272              :       /* Don't clobber an existing initializer!  */
   15273           37 :       gcc_assert (sym->value == NULL);
   15274           37 :       sym->value = init;
   15275           37 :       return;
   15276              :     }
   15277              : 
   15278          382 :   build_init_assign (sym, init);
   15279              : }
   15280              : 
   15281              : 
   15282              : /* Resolution of common features of flavors variable and procedure.  */
   15283              : 
   15284              : static bool
   15285       966799 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
   15286              : {
   15287       966799 :   gfc_array_spec *as;
   15288              : 
   15289       966799 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15290        19351 :       && sym->ts.u.derived && CLASS_DATA (sym))
   15291        19345 :     as = CLASS_DATA (sym)->as;
   15292              :   else
   15293       947454 :     as = sym->as;
   15294              : 
   15295              :   /* Constraints on deferred shape variable.  */
   15296       966799 :   if (as == NULL || as->type != AS_DEFERRED)
   15297              :     {
   15298       942494 :       bool pointer, allocatable, dimension;
   15299              : 
   15300       942494 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15301        16138 :           && sym->ts.u.derived && CLASS_DATA (sym))
   15302              :         {
   15303        16132 :           pointer = CLASS_DATA (sym)->attr.class_pointer;
   15304        16132 :           allocatable = CLASS_DATA (sym)->attr.allocatable;
   15305        16132 :           dimension = CLASS_DATA (sym)->attr.dimension;
   15306              :         }
   15307              :       else
   15308              :         {
   15309       926362 :           pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
   15310       926362 :           allocatable = sym->attr.allocatable;
   15311       926362 :           dimension = sym->attr.dimension;
   15312              :         }
   15313              : 
   15314       942494 :       if (allocatable)
   15315              :         {
   15316         8024 :           if (dimension
   15317         8024 :               && as
   15318          524 :               && as->type != AS_ASSUMED_RANK
   15319            5 :               && !sym->attr.select_rank_temporary)
   15320              :             {
   15321            3 :               gfc_error ("Allocatable array %qs at %L must have a deferred "
   15322              :                          "shape or assumed rank", sym->name, &sym->declared_at);
   15323            3 :               return false;
   15324              :             }
   15325         8021 :           else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
   15326              :                                     "%qs at %L may not be ALLOCATABLE",
   15327              :                                     sym->name, &sym->declared_at))
   15328              :             return false;
   15329              :         }
   15330              : 
   15331       942490 :       if (pointer && dimension && as->type != AS_ASSUMED_RANK)
   15332              :         {
   15333            4 :           gfc_error ("Array pointer %qs at %L must have a deferred shape or "
   15334              :                      "assumed rank", sym->name, &sym->declared_at);
   15335            4 :           sym->error = 1;
   15336            4 :           return false;
   15337              :         }
   15338              :     }
   15339              :   else
   15340              :     {
   15341        24305 :       if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
   15342         4678 :           && sym->ts.type != BT_CLASS && !sym->assoc)
   15343              :         {
   15344            3 :           gfc_error ("Array %qs at %L cannot have a deferred shape",
   15345              :                      sym->name, &sym->declared_at);
   15346            3 :           return false;
   15347              :          }
   15348              :     }
   15349              : 
   15350              :   /* Constraints on polymorphic variables.  */
   15351       966788 :   if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
   15352              :     {
   15353              :       /* F03:C502.  */
   15354        18684 :       if (sym->attr.class_ok
   15355        18628 :           && sym->ts.u.derived
   15356        18623 :           && !sym->attr.select_type_temporary
   15357        17522 :           && !UNLIMITED_POLY (sym)
   15358        15012 :           && CLASS_DATA (sym)
   15359        15011 :           && CLASS_DATA (sym)->ts.u.derived
   15360        33694 :           && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
   15361              :         {
   15362            5 :           gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
   15363            5 :                      CLASS_DATA (sym)->ts.u.derived->name, sym->name,
   15364              :                      &sym->declared_at);
   15365            5 :           return false;
   15366              :         }
   15367              : 
   15368              :       /* F03:C509.  */
   15369              :       /* Assume that use associated symbols were checked in the module ns.
   15370              :          Class-variables that are associate-names are also something special
   15371              :          and excepted from the test.  */
   15372        18679 :       if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
   15373           54 :           && !sym->attr.select_type_temporary
   15374           54 :           && !sym->attr.select_rank_temporary)
   15375              :         {
   15376           54 :           gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
   15377              :                      "or pointer", sym->name, &sym->declared_at);
   15378           54 :           return false;
   15379              :         }
   15380              :     }
   15381              : 
   15382              :   return true;
   15383              : }
   15384              : 
   15385              : 
   15386              : /* Additional checks for symbols with flavor variable and derived
   15387              :    type.  To be called from resolve_fl_variable.  */
   15388              : 
   15389              : static bool
   15390        82224 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
   15391              : {
   15392        82224 :   gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
   15393              : 
   15394              :   /* Check to see if a derived type is blocked from being host
   15395              :      associated by the presence of another class I symbol in the same
   15396              :      namespace.  14.6.1.3 of the standard and the discussion on
   15397              :      comp.lang.fortran.  */
   15398        82224 :   if (sym->ts.u.derived
   15399        82219 :       && sym->ns != sym->ts.u.derived->ns
   15400        47189 :       && !sym->ts.u.derived->attr.use_assoc
   15401        17469 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
   15402              :     {
   15403        16499 :       gfc_symbol *s;
   15404        16499 :       gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
   15405        16499 :       if (s && s->attr.generic)
   15406            2 :         s = gfc_find_dt_in_generic (s);
   15407        16499 :       if (s && !gfc_fl_struct (s->attr.flavor))
   15408              :         {
   15409            2 :           gfc_error ("The type %qs cannot be host associated at %L "
   15410              :                      "because it is blocked by an incompatible object "
   15411              :                      "of the same name declared at %L",
   15412            2 :                      sym->ts.u.derived->name, &sym->declared_at,
   15413              :                      &s->declared_at);
   15414            2 :           return false;
   15415              :         }
   15416              :     }
   15417              : 
   15418              :   /* 4th constraint in section 11.3: "If an object of a type for which
   15419              :      component-initialization is specified (R429) appears in the
   15420              :      specification-part of a module and does not have the ALLOCATABLE
   15421              :      or POINTER attribute, the object shall have the SAVE attribute."
   15422              : 
   15423              :      The check for initializers is performed with
   15424              :      gfc_has_default_initializer because gfc_default_initializer generates
   15425              :      a hidden default for allocatable components.  */
   15426        81545 :   if (!(sym->value || no_init_flag) && sym->ns->proc_name
   15427        18435 :       && sym->ns->proc_name->attr.flavor == FL_MODULE
   15428          416 :       && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
   15429           21 :       && !sym->attr.pointer && !sym->attr.allocatable
   15430           21 :       && gfc_has_default_initializer (sym->ts.u.derived)
   15431        82231 :       && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
   15432              :                           "%qs at %L, needed due to the default "
   15433              :                           "initialization", sym->name, &sym->declared_at))
   15434              :     return false;
   15435              : 
   15436              :   /* Assign default initializer.  */
   15437        82220 :   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
   15438        75981 :       && (!no_init_flag
   15439        59347 :           || (sym->attr.intent == INTENT_OUT
   15440         3225 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
   15441        19685 :     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
   15442              : 
   15443              :   return true;
   15444              : }
   15445              : 
   15446              : 
   15447              : /* F2008, C402 (R401):  A colon shall not be used as a type-param-value
   15448              :    except in the declaration of an entity or component that has the POINTER
   15449              :    or ALLOCATABLE attribute.  */
   15450              : 
   15451              : static bool
   15452      1505573 : deferred_requirements (gfc_symbol *sym)
   15453              : {
   15454      1505573 :   if (sym->ts.deferred
   15455         7942 :       && !(sym->attr.pointer
   15456         2378 :            || sym->attr.allocatable
   15457           92 :            || sym->attr.associate_var
   15458            7 :            || sym->attr.omp_udr_artificial_var))
   15459              :     {
   15460              :       /* If a function has a result variable, only check the variable.  */
   15461            7 :       if (sym->result && sym->name != sym->result->name)
   15462              :         return true;
   15463              : 
   15464            6 :       gfc_error ("Entity %qs at %L has a deferred type parameter and "
   15465              :                  "requires either the POINTER or ALLOCATABLE attribute",
   15466              :                  sym->name, &sym->declared_at);
   15467            6 :       return false;
   15468              :     }
   15469              :   return true;
   15470              : }
   15471              : 
   15472              : 
   15473              : /* Resolve symbols with flavor variable.  */
   15474              : 
   15475              : static bool
   15476       648278 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
   15477              : {
   15478       648278 :   const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
   15479              :                                  "SAVE attribute");
   15480              : 
   15481       648278 :   if (!resolve_fl_var_and_proc (sym, mp_flag))
   15482              :     return false;
   15483              : 
   15484              :   /* Set this flag to check that variables are parameters of all entries.
   15485              :      This check is effected by the call to gfc_resolve_expr through
   15486              :      is_non_constant_shape_array.  */
   15487       648218 :   bool saved_specification_expr = specification_expr;
   15488       648218 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   15489       648218 :   specification_expr = true;
   15490       648218 :   specification_expr_symbol = sym;
   15491              : 
   15492       648218 :   if (sym->ns->proc_name
   15493       648123 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15494       643102 :           || sym->ns->proc_name->attr.is_main_program)
   15495        82543 :       && !sym->attr.use_assoc
   15496        79381 :       && !sym->attr.allocatable
   15497        73643 :       && !sym->attr.pointer
   15498       718227 :       && is_non_constant_shape_array (sym))
   15499              :     {
   15500              :       /* F08:C541. The shape of an array defined in a main program or module
   15501              :        * needs to be constant.  */
   15502            3 :       gfc_error ("The module or main program array %qs at %L must "
   15503              :                  "have constant shape", sym->name, &sym->declared_at);
   15504            3 :       specification_expr = saved_specification_expr;
   15505            3 :       specification_expr_symbol = saved_specification_expr_symbol;
   15506            3 :       return false;
   15507              :     }
   15508              : 
   15509              :   /* Constraints on deferred type parameter.  */
   15510       648215 :   if (!deferred_requirements (sym))
   15511              :     return false;
   15512              : 
   15513       648211 :   if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
   15514              :     {
   15515              :       /* Make sure that character string variables with assumed length are
   15516              :          dummy arguments.  */
   15517        35925 :       gfc_expr *e = NULL;
   15518              : 
   15519        35925 :       if (sym->ts.u.cl)
   15520        35925 :         e = sym->ts.u.cl->length;
   15521              :       else
   15522              :         return false;
   15523              : 
   15524        35925 :       if (e == NULL && !sym->attr.dummy && !sym->attr.result
   15525         2583 :           && !sym->ts.deferred && !sym->attr.select_type_temporary
   15526            2 :           && !sym->attr.omp_udr_artificial_var)
   15527              :         {
   15528            2 :           gfc_error ("Entity with assumed character length at %L must be a "
   15529              :                      "dummy argument or a PARAMETER", &sym->declared_at);
   15530            2 :           specification_expr = saved_specification_expr;
   15531            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15532            2 :           return false;
   15533              :         }
   15534              : 
   15535        20777 :       if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
   15536              :         {
   15537            1 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15538            1 :           specification_expr = saved_specification_expr;
   15539            1 :           specification_expr_symbol = saved_specification_expr_symbol;
   15540            1 :           return false;
   15541              :         }
   15542              : 
   15543        35922 :       if (!gfc_is_constant_expr (e)
   15544        35922 :           && !(e->expr_type == EXPR_VARIABLE
   15545         1388 :                && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
   15546              :         {
   15547         2184 :           if (!sym->attr.use_assoc && sym->ns->proc_name
   15548         1680 :               && (sym->ns->proc_name->attr.flavor == FL_MODULE
   15549         1679 :                   || sym->ns->proc_name->attr.is_main_program))
   15550              :             {
   15551            3 :               gfc_error ("%qs at %L must have constant character length "
   15552              :                         "in this context", sym->name, &sym->declared_at);
   15553            3 :               specification_expr = saved_specification_expr;
   15554            3 :               specification_expr_symbol = saved_specification_expr_symbol;
   15555            3 :               return false;
   15556              :             }
   15557         2181 :           if (sym->attr.in_common)
   15558              :             {
   15559            1 :               gfc_error ("COMMON variable %qs at %L must have constant "
   15560              :                          "character length", sym->name, &sym->declared_at);
   15561            1 :               specification_expr = saved_specification_expr;
   15562            1 :               specification_expr_symbol = saved_specification_expr_symbol;
   15563            1 :               return false;
   15564              :             }
   15565              :         }
   15566              :     }
   15567              : 
   15568       648204 :   if (sym->value == NULL && sym->attr.referenced
   15569       206226 :       && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
   15570       204311 :     apply_default_init_local (sym); /* Try to apply a default initialization.  */
   15571              : 
   15572              :   /* Determine if the symbol may not have an initializer.  */
   15573       648204 :   int no_init_flag = 0, automatic_flag = 0;
   15574       648204 :   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
   15575       170603 :       || sym->attr.intrinsic || sym->attr.result)
   15576              :     no_init_flag = 1;
   15577       138321 :   else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
   15578       172948 :            && is_non_constant_shape_array (sym))
   15579              :     {
   15580         1345 :       no_init_flag = automatic_flag = 1;
   15581              : 
   15582              :       /* Also, they must not have the SAVE attribute.
   15583              :          SAVE_IMPLICIT is checked below.  */
   15584         1345 :       if (sym->as && sym->attr.codimension)
   15585              :         {
   15586            7 :           int corank = sym->as->corank;
   15587            7 :           sym->as->corank = 0;
   15588            7 :           no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
   15589            7 :           sym->as->corank = corank;
   15590              :         }
   15591         1345 :       if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
   15592              :         {
   15593            2 :           gfc_error (auto_save_msg, sym->name, &sym->declared_at);
   15594            2 :           specification_expr = saved_specification_expr;
   15595            2 :           specification_expr_symbol = saved_specification_expr_symbol;
   15596            2 :           return false;
   15597              :         }
   15598              :     }
   15599              : 
   15600              :   /* Ensure that any initializer is simplified.  */
   15601       648202 :   if (sym->value)
   15602         8119 :     gfc_simplify_expr (sym->value, 1);
   15603              : 
   15604              :   /* Reject illegal initializers.  */
   15605       648202 :   if (!sym->mark && sym->value)
   15606              :     {
   15607         8119 :       if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
   15608           67 :                                     && CLASS_DATA (sym)->attr.allocatable))
   15609            1 :         gfc_error ("Allocatable %qs at %L cannot have an initializer",
   15610              :                    sym->name, &sym->declared_at);
   15611         8118 :       else if (sym->attr.external)
   15612            0 :         gfc_error ("External %qs at %L cannot have an initializer",
   15613              :                    sym->name, &sym->declared_at);
   15614         8118 :       else if (sym->attr.dummy)
   15615            3 :         gfc_error ("Dummy %qs at %L cannot have an initializer",
   15616              :                    sym->name, &sym->declared_at);
   15617         8115 :       else if (sym->attr.intrinsic)
   15618            0 :         gfc_error ("Intrinsic %qs at %L cannot have an initializer",
   15619              :                    sym->name, &sym->declared_at);
   15620         8115 :       else if (sym->attr.result)
   15621            1 :         gfc_error ("Function result %qs at %L cannot have an initializer",
   15622              :                    sym->name, &sym->declared_at);
   15623         8114 :       else if (automatic_flag)
   15624            5 :         gfc_error ("Automatic array %qs at %L cannot have an initializer",
   15625              :                    sym->name, &sym->declared_at);
   15626              :       else
   15627         8109 :         goto no_init_error;
   15628           10 :       specification_expr = saved_specification_expr;
   15629           10 :       specification_expr_symbol = saved_specification_expr_symbol;
   15630           10 :       return false;
   15631              :     }
   15632              : 
   15633       640083 : no_init_error:
   15634       648192 :   if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   15635              :     {
   15636        82224 :       bool res = resolve_fl_variable_derived (sym, no_init_flag);
   15637        82224 :       specification_expr = saved_specification_expr;
   15638        82224 :       specification_expr_symbol = saved_specification_expr_symbol;
   15639        82224 :       return res;
   15640              :     }
   15641              : 
   15642       565968 :   specification_expr = saved_specification_expr;
   15643       565968 :   specification_expr_symbol = saved_specification_expr_symbol;
   15644       565968 :   return true;
   15645              : }
   15646              : 
   15647              : 
   15648              : /* Compare the dummy characteristics of a module procedure interface
   15649              :    declaration with the corresponding declaration in a submodule.  */
   15650              : static gfc_formal_arglist *new_formal;
   15651              : static char errmsg[200];
   15652              : 
   15653              : static void
   15654         1324 : compare_fsyms (gfc_symbol *sym)
   15655              : {
   15656         1324 :   gfc_symbol *fsym;
   15657              : 
   15658         1324 :   if (sym == NULL || new_formal == NULL)
   15659              :     return;
   15660              : 
   15661         1324 :   fsym = new_formal->sym;
   15662              : 
   15663         1324 :   if (sym == fsym)
   15664              :     return;
   15665              : 
   15666         1300 :   if (strcmp (sym->name, fsym->name) == 0)
   15667              :     {
   15668          499 :       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
   15669            2 :         gfc_error ("%s at %L", errmsg, &fsym->declared_at);
   15670              :     }
   15671              : }
   15672              : 
   15673              : 
   15674              : /* Resolve a procedure.  */
   15675              : 
   15676              : static bool
   15677       475130 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
   15678              : {
   15679       475130 :   gfc_formal_arglist *arg;
   15680       475130 :   bool allocatable_or_pointer = false;
   15681              : 
   15682       475130 :   if (sym->attr.function
   15683       475130 :       && !resolve_fl_var_and_proc (sym, mp_flag))
   15684              :     return false;
   15685              : 
   15686              :   /* Constraints on deferred type parameter.  */
   15687       475120 :   if (!deferred_requirements (sym))
   15688              :     return false;
   15689              : 
   15690       475119 :   if (sym->ts.type == BT_CHARACTER)
   15691              :     {
   15692        11739 :       gfc_charlen *cl = sym->ts.u.cl;
   15693              : 
   15694         7627 :       if (cl && cl->length && gfc_is_constant_expr (cl->length)
   15695        13029 :              && !resolve_charlen (cl))
   15696              :         return false;
   15697              : 
   15698        11738 :       if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   15699        10449 :           && sym->attr.proc == PROC_ST_FUNCTION)
   15700              :         {
   15701            0 :           gfc_error ("Character-valued statement function %qs at %L must "
   15702              :                      "have constant length", sym->name, &sym->declared_at);
   15703            0 :           return false;
   15704              :         }
   15705              :     }
   15706              : 
   15707              :   /* Ensure that derived type for are not of a private type.  Internal
   15708              :      module procedures are excluded by 2.2.3.3 - i.e., they are not
   15709              :      externally accessible and can access all the objects accessible in
   15710              :      the host.  */
   15711       109280 :   if (!(sym->ns->parent && sym->ns->parent->proc_name
   15712       109280 :         && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
   15713       560023 :       && gfc_check_symbol_access (sym))
   15714              :     {
   15715       443731 :       gfc_interface *iface;
   15716              : 
   15717       935674 :       for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
   15718              :         {
   15719       491944 :           if (arg->sym
   15720       491803 :               && arg->sym->ts.type == BT_DERIVED
   15721        43002 :               && arg->sym->ts.u.derived
   15722        43002 :               && !arg->sym->ts.u.derived->attr.use_assoc
   15723         4420 :               && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15724       491953 :               && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
   15725              :                                   "and cannot be a dummy argument"
   15726              :                                   " of %qs, which is PUBLIC at %L",
   15727            9 :                                   arg->sym->name, sym->name,
   15728              :                                   &sym->declared_at))
   15729              :             {
   15730              :               /* Stop this message from recurring.  */
   15731            1 :               arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   15732            1 :               return false;
   15733              :             }
   15734              :         }
   15735              : 
   15736              :       /* PUBLIC interfaces may expose PRIVATE procedures that take types
   15737              :          PRIVATE to the containing module.  */
   15738       631269 :       for (iface = sym->generic; iface; iface = iface->next)
   15739              :         {
   15740       437591 :           for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
   15741              :             {
   15742       250052 :               if (arg->sym
   15743       250020 :                   && arg->sym->ts.type == BT_DERIVED
   15744         8030 :                   && !arg->sym->ts.u.derived->attr.use_assoc
   15745          244 :                   && !gfc_check_symbol_access (arg->sym->ts.u.derived)
   15746       250056 :                   && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
   15747              :                                       "PUBLIC interface %qs at %L "
   15748              :                                       "takes dummy arguments of %qs which "
   15749              :                                       "is PRIVATE", iface->sym->name,
   15750            4 :                                       sym->name, &iface->sym->declared_at,
   15751            4 :                                       gfc_typename(&arg->sym->ts)))
   15752              :                 {
   15753              :                   /* Stop this message from recurring.  */
   15754            1 :                   arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
   15755            1 :                   return false;
   15756              :                 }
   15757              :              }
   15758              :         }
   15759              :     }
   15760              : 
   15761       475116 :   if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
   15762           79 :       && !sym->attr.proc_pointer)
   15763              :     {
   15764            2 :       gfc_error ("Function %qs at %L cannot have an initializer",
   15765              :                  sym->name, &sym->declared_at);
   15766              : 
   15767              :       /* Make sure no second error is issued for this.  */
   15768            2 :       sym->value->error = 1;
   15769            2 :       return false;
   15770              :     }
   15771              : 
   15772              :   /* An external symbol may not have an initializer because it is taken to be
   15773              :      a procedure. Exception: Procedure Pointers.  */
   15774       475114 :   if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
   15775              :     {
   15776            0 :       gfc_error ("External object %qs at %L may not have an initializer",
   15777              :                  sym->name, &sym->declared_at);
   15778            0 :       return false;
   15779              :     }
   15780              : 
   15781              :   /* An elemental function is required to return a scalar 12.7.1  */
   15782       475114 :   if (sym->attr.elemental && sym->attr.function
   15783        86368 :       && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   15784            2 :                       && CLASS_DATA (sym)->as)))
   15785              :     {
   15786            3 :       gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
   15787              :                  "result", sym->name, &sym->declared_at);
   15788              :       /* Reset so that the error only occurs once.  */
   15789            3 :       sym->attr.elemental = 0;
   15790            3 :       return false;
   15791              :     }
   15792              : 
   15793       475111 :   if (sym->attr.proc == PROC_ST_FUNCTION
   15794          223 :       && (sym->attr.allocatable || sym->attr.pointer))
   15795              :     {
   15796            2 :       gfc_error ("Statement function %qs at %L may not have pointer or "
   15797              :                  "allocatable attribute", sym->name, &sym->declared_at);
   15798            2 :       return false;
   15799              :     }
   15800              : 
   15801              :   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
   15802              :      char-len-param shall not be array-valued, pointer-valued, recursive
   15803              :      or pure.  ....snip... A character value of * may only be used in the
   15804              :      following ways: (i) Dummy arg of procedure - dummy associates with
   15805              :      actual length; (ii) To declare a named constant; or (iii) External
   15806              :      function - but length must be declared in calling scoping unit.  */
   15807       475109 :   if (sym->attr.function
   15808       318502 :       && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
   15809         6694 :       && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
   15810              :     {
   15811          180 :       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
   15812          178 :           || (sym->attr.recursive) || (sym->attr.pure))
   15813              :         {
   15814            4 :           if (sym->as && sym->as->rank)
   15815            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15816              :                        "array-valued", sym->name, &sym->declared_at);
   15817              : 
   15818            4 :           if (sym->attr.pointer)
   15819            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15820              :                        "pointer-valued", sym->name, &sym->declared_at);
   15821              : 
   15822            4 :           if (sym->attr.pure)
   15823            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15824              :                        "pure", sym->name, &sym->declared_at);
   15825              : 
   15826            4 :           if (sym->attr.recursive)
   15827            1 :             gfc_error ("CHARACTER(*) function %qs at %L cannot be "
   15828              :                        "recursive", sym->name, &sym->declared_at);
   15829              : 
   15830            4 :           return false;
   15831              :         }
   15832              : 
   15833              :       /* Appendix B.2 of the standard.  Contained functions give an
   15834              :          error anyway.  Deferred character length is an F2003 feature.
   15835              :          Don't warn on intrinsic conversion functions, which start
   15836              :          with two underscores.  */
   15837          176 :       if (!sym->attr.contained && !sym->ts.deferred
   15838          172 :           && (sym->name[0] != '_' || sym->name[1] != '_'))
   15839          172 :         gfc_notify_std (GFC_STD_F95_OBS,
   15840              :                         "CHARACTER(*) function %qs at %L",
   15841              :                         sym->name, &sym->declared_at);
   15842              :     }
   15843              : 
   15844              :   /* F2008, C1218.  */
   15845       475105 :   if (sym->attr.elemental)
   15846              :     {
   15847        89634 :       if (sym->attr.proc_pointer)
   15848              :         {
   15849            7 :           const char* name = (sym->attr.result ? sym->ns->proc_name->name
   15850              :                                                : sym->name);
   15851            7 :           gfc_error ("Procedure pointer %qs at %L shall not be elemental",
   15852              :                      name, &sym->declared_at);
   15853            7 :           return false;
   15854              :         }
   15855        89627 :       if (sym->attr.dummy)
   15856              :         {
   15857            3 :           gfc_error ("Dummy procedure %qs at %L shall not be elemental",
   15858              :                      sym->name, &sym->declared_at);
   15859            3 :           return false;
   15860              :         }
   15861              :     }
   15862              : 
   15863              :   /* F2018, C15100: "The result of an elemental function shall be scalar,
   15864              :      and shall not have the POINTER or ALLOCATABLE attribute."  The scalar
   15865              :      pointer is tested and caught elsewhere.  */
   15866       475095 :   if (sym->result)
   15867       267426 :     allocatable_or_pointer = sym->result->ts.type == BT_CLASS
   15868       267426 :                              && CLASS_DATA (sym->result) ?
   15869         1663 :                              (CLASS_DATA (sym->result)->attr.allocatable
   15870         1663 :                               || CLASS_DATA (sym->result)->attr.pointer) :
   15871       265763 :                              (sym->result->attr.allocatable
   15872       265763 :                               || sym->result->attr.pointer);
   15873              : 
   15874       475095 :   if (sym->attr.elemental && sym->result
   15875        85993 :       && allocatable_or_pointer)
   15876              :     {
   15877            4 :       gfc_error ("Function result variable %qs at %L of elemental "
   15878              :                  "function %qs shall not have an ALLOCATABLE or POINTER "
   15879              :                  "attribute", sym->result->name,
   15880              :                  &sym->result->declared_at, sym->name);
   15881            4 :       return false;
   15882              :     }
   15883              : 
   15884              :   /* F2018:C1585: "The function result of a pure function shall not be both
   15885              :      polymorphic and allocatable, or have a polymorphic allocatable ultimate
   15886              :      component."  */
   15887       475091 :   if (sym->attr.pure && sym->result && sym->ts.u.derived)
   15888              :     {
   15889         2459 :       if (sym->ts.type == BT_CLASS
   15890            5 :           && sym->attr.class_ok
   15891            4 :           && CLASS_DATA (sym->result)
   15892            4 :           && CLASS_DATA (sym->result)->attr.allocatable)
   15893              :         {
   15894            4 :           gfc_error ("Result variable %qs of pure function at %L is "
   15895              :                      "polymorphic allocatable",
   15896              :                      sym->result->name, &sym->result->declared_at);
   15897            4 :           return false;
   15898              :         }
   15899              : 
   15900         2455 :       if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
   15901              :         {
   15902              :           gfc_component *c = sym->ts.u.derived->components;
   15903         4491 :           for (; c; c = c->next)
   15904         2345 :             if (c->ts.type == BT_CLASS
   15905            2 :                 && CLASS_DATA (c)
   15906            2 :                 && CLASS_DATA (c)->attr.allocatable)
   15907              :               {
   15908            2 :                 gfc_error ("Result variable %qs of pure function at %L has "
   15909              :                            "polymorphic allocatable component %qs",
   15910              :                            sym->result->name, &sym->result->declared_at,
   15911              :                            c->name);
   15912            2 :                 return false;
   15913              :               }
   15914              :         }
   15915              :     }
   15916              : 
   15917       475085 :   if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
   15918              :     {
   15919         6707 :       gfc_formal_arglist *curr_arg;
   15920         6707 :       int has_non_interop_arg = 0;
   15921              : 
   15922         6707 :       if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   15923         6707 :                               sym->common_block))
   15924              :         {
   15925              :           /* Clear these to prevent looking at them again if there was an
   15926              :              error.  */
   15927            2 :           sym->attr.is_bind_c = 0;
   15928            2 :           sym->attr.is_c_interop = 0;
   15929            2 :           sym->ts.is_c_interop = 0;
   15930              :         }
   15931              :       else
   15932              :         {
   15933              :           /* So far, no errors have been found.  */
   15934         6705 :           sym->attr.is_c_interop = 1;
   15935         6705 :           sym->ts.is_c_interop = 1;
   15936              :         }
   15937              : 
   15938         6707 :       curr_arg = gfc_sym_get_dummy_args (sym);
   15939        29880 :       while (curr_arg != NULL)
   15940              :         {
   15941              :           /* Skip implicitly typed dummy args here.  */
   15942        16466 :           if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
   15943        16409 :             if (!gfc_verify_c_interop_param (curr_arg->sym))
   15944              :               /* If something is found to fail, record the fact so we
   15945              :                  can mark the symbol for the procedure as not being
   15946              :                  BIND(C) to try and prevent multiple errors being
   15947              :                  reported.  */
   15948        16466 :               has_non_interop_arg = 1;
   15949              : 
   15950        16466 :           curr_arg = curr_arg->next;
   15951              :         }
   15952              : 
   15953              :       /* See if any of the arguments were not interoperable and if so, clear
   15954              :          the procedure symbol to prevent duplicate error messages.  */
   15955         6707 :       if (has_non_interop_arg != 0)
   15956              :         {
   15957          128 :           sym->attr.is_c_interop = 0;
   15958          128 :           sym->ts.is_c_interop = 0;
   15959          128 :           sym->attr.is_bind_c = 0;
   15960              :         }
   15961              :     }
   15962              : 
   15963       475085 :   if (!sym->attr.proc_pointer)
   15964              :     {
   15965       474004 :       if (sym->attr.save == SAVE_EXPLICIT)
   15966              :         {
   15967            5 :           gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
   15968              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15969            5 :           return false;
   15970              :         }
   15971       473999 :       if (sym->attr.intent)
   15972              :         {
   15973            1 :           gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
   15974              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15975            1 :           return false;
   15976              :         }
   15977       473998 :       if (sym->attr.subroutine && sym->attr.result)
   15978              :         {
   15979            2 :           gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
   15980            2 :                      "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
   15981            2 :           return false;
   15982              :         }
   15983       473996 :       if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
   15984       135110 :           && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
   15985       135107 :               || sym->attr.contained))
   15986              :         {
   15987            3 :           gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
   15988              :                      "in %qs at %L", sym->name, &sym->declared_at);
   15989            3 :           return false;
   15990              :         }
   15991       473993 :       if (strcmp ("ppr@", sym->name) == 0)
   15992              :         {
   15993            0 :           gfc_error ("Procedure pointer result %qs at %L "
   15994              :                      "is missing the pointer attribute",
   15995            0 :                      sym->ns->proc_name->name, &sym->declared_at);
   15996            0 :           return false;
   15997              :         }
   15998              :     }
   15999              : 
   16000              :   /* Assume that a procedure whose body is not known has references
   16001              :      to external arrays.  */
   16002       475074 :   if (sym->attr.if_source != IFSRC_DECL)
   16003       326739 :     sym->attr.array_outer_dependency = 1;
   16004              : 
   16005              :   /* Compare the characteristics of a module procedure with the
   16006              :      interface declaration. Ideally this would be done with
   16007              :      gfc_compare_interfaces but, at present, the formal interface
   16008              :      cannot be copied to the ts.interface.  */
   16009       475074 :   if (sym->attr.module_procedure
   16010         1517 :       && sym->attr.if_source == IFSRC_DECL)
   16011              :     {
   16012          629 :       gfc_symbol *iface;
   16013          629 :       char name[2*GFC_MAX_SYMBOL_LEN + 1];
   16014          629 :       char *module_name;
   16015          629 :       char *submodule_name;
   16016          629 :       strcpy (name, sym->ns->proc_name->name);
   16017          629 :       module_name = strtok (name, ".");
   16018          629 :       submodule_name = strtok (NULL, ".");
   16019              : 
   16020          629 :       iface = sym->tlink;
   16021          629 :       sym->tlink = NULL;
   16022              : 
   16023              :       /* Make sure that the result uses the correct charlen for deferred
   16024              :          length results.  */
   16025          629 :       if (iface && sym->result
   16026          189 :           && iface->ts.type == BT_CHARACTER
   16027           19 :           && iface->ts.deferred)
   16028            6 :         sym->result->ts.u.cl = iface->ts.u.cl;
   16029              : 
   16030            6 :       if (iface == NULL)
   16031          195 :         goto check_formal;
   16032              : 
   16033              :       /* Check the procedure characteristics.  */
   16034          434 :       if (sym->attr.elemental != iface->attr.elemental)
   16035              :         {
   16036            1 :           gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
   16037              :                      "PROCEDURE at %L and its interface in %s",
   16038              :                      &sym->declared_at, module_name);
   16039           10 :           return false;
   16040              :         }
   16041              : 
   16042          433 :       if (sym->attr.pure != iface->attr.pure)
   16043              :         {
   16044            2 :           gfc_error ("Mismatch in PURE attribute between MODULE "
   16045              :                      "PROCEDURE at %L and its interface in %s",
   16046              :                      &sym->declared_at, module_name);
   16047            2 :           return false;
   16048              :         }
   16049              : 
   16050          431 :       if (sym->attr.recursive != iface->attr.recursive)
   16051              :         {
   16052            2 :           gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
   16053              :                      "PROCEDURE at %L and its interface in %s",
   16054              :                      &sym->declared_at, module_name);
   16055            2 :           return false;
   16056              :         }
   16057              : 
   16058              :       /* Check the result characteristics.  */
   16059          429 :       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
   16060              :         {
   16061            5 :           gfc_error ("%s between the MODULE PROCEDURE declaration "
   16062              :                      "in MODULE %qs and the declaration at %L in "
   16063              :                      "(SUB)MODULE %qs",
   16064              :                      errmsg, module_name, &sym->declared_at,
   16065              :                      submodule_name ? submodule_name : module_name);
   16066            5 :           return false;
   16067              :         }
   16068              : 
   16069          424 : check_formal:
   16070              :       /* Check the characteristics of the formal arguments.  */
   16071          619 :       if (sym->formal && sym->formal_ns)
   16072              :         {
   16073         1212 :           for (arg = sym->formal; arg && arg->sym; arg = arg->next)
   16074              :             {
   16075          697 :               new_formal = arg;
   16076          697 :               gfc_traverse_ns (sym->formal_ns, compare_fsyms);
   16077              :             }
   16078              :         }
   16079              :     }
   16080              : 
   16081              :   /* F2018:15.4.2.2 requires an explicit interface for procedures with the
   16082              :      BIND(C) attribute.  */
   16083       475064 :   if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
   16084              :     {
   16085            1 :       gfc_error ("Interface of %qs at %L must be explicit",
   16086              :                  sym->name, &sym->declared_at);
   16087            1 :       return false;
   16088              :     }
   16089              : 
   16090              :   return true;
   16091              : }
   16092              : 
   16093              : 
   16094              : /* Resolve a list of finalizer procedures.  That is, after they have hopefully
   16095              :    been defined and we now know their defined arguments, check that they fulfill
   16096              :    the requirements of the standard for procedures used as finalizers.  */
   16097              : 
   16098              : static bool
   16099       111938 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
   16100              : {
   16101       111938 :   gfc_finalizer *list, *pdt_finalizers = NULL;
   16102       111938 :   gfc_finalizer** prev_link; /* For removing wrong entries from the list.  */
   16103       111938 :   bool result = true;
   16104       111938 :   bool seen_scalar = false;
   16105       111938 :   gfc_symbol *vtab;
   16106       111938 :   gfc_component *c;
   16107       111938 :   gfc_symbol *parent = gfc_get_derived_super_type (derived);
   16108              : 
   16109       111938 :   if (parent)
   16110        15589 :     gfc_resolve_finalizers (parent, finalizable);
   16111              : 
   16112              :   /* Ensure that derived-type components have a their finalizers resolved.  */
   16113       111938 :   bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
   16114       352402 :   for (c = derived->components; c; c = c->next)
   16115       240464 :     if (c->ts.type == BT_DERIVED
   16116        67575 :         && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
   16117              :       {
   16118         8294 :         bool has_final2 = false;
   16119         8294 :         if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
   16120            0 :           return false;  /* Error.  */
   16121         8294 :         has_final = has_final || has_final2;
   16122              :       }
   16123              :   /* Return early if not finalizable.  */
   16124       111938 :   if (!has_final)
   16125              :     {
   16126       109403 :       if (finalizable)
   16127         8208 :         *finalizable = false;
   16128       109403 :       return true;
   16129              :     }
   16130              : 
   16131              :   /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
   16132              :      the template. If the finalizers field has the same value, it needs to be
   16133              :      supplied with finalizers of the same pdt_type.  */
   16134         2535 :   if (derived->attr.pdt_type
   16135           30 :       && derived->template_sym
   16136           12 :       && derived->template_sym->f2k_derived
   16137           12 :       && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
   16138         2547 :       && derived->f2k_derived->finalizers == pdt_finalizers)
   16139              :     {
   16140           12 :       gfc_finalizer *tmp = NULL;
   16141           12 :       derived->f2k_derived->finalizers = NULL;
   16142           12 :       prev_link = &derived->f2k_derived->finalizers;
   16143           48 :       for (list = pdt_finalizers; list; list = list->next)
   16144              :         {
   16145           36 :           gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
   16146           36 :           if (args->sym
   16147           36 :               && args->sym->ts.type == BT_DERIVED
   16148           36 :               && args->sym->ts.u.derived
   16149           36 :               && !strcmp (args->sym->ts.u.derived->name, derived->name))
   16150              :             {
   16151           18 :               tmp = gfc_get_finalizer ();
   16152           18 :               *tmp = *list;
   16153           18 :               tmp->next = NULL;
   16154           18 :               if (*prev_link)
   16155              :                 {
   16156            6 :                   (*prev_link)->next = tmp;
   16157            6 :                   prev_link = &tmp;
   16158              :                 }
   16159              :               else
   16160           12 :                 *prev_link = tmp;
   16161           18 :               list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16162              :             }
   16163              :         }
   16164              :     }
   16165              : 
   16166              :   /* Walk over the list of finalizer-procedures, check them, and if any one
   16167              :      does not fit in with the standard's definition, print an error and remove
   16168              :      it from the list.  */
   16169         2535 :   prev_link = &derived->f2k_derived->finalizers;
   16170         5230 :   for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
   16171              :     {
   16172         2695 :       gfc_formal_arglist *dummy_args;
   16173         2695 :       gfc_symbol* arg;
   16174         2695 :       gfc_finalizer* i;
   16175         2695 :       int my_rank;
   16176              : 
   16177              :       /* Skip this finalizer if we already resolved it.  */
   16178         2695 :       if (list->proc_tree)
   16179              :         {
   16180         2162 :           if (list->proc_tree->n.sym->formal->sym->as == NULL
   16181          584 :               || list->proc_tree->n.sym->formal->sym->as->rank == 0)
   16182         1578 :             seen_scalar = true;
   16183         2162 :           prev_link = &(list->next);
   16184         2162 :           continue;
   16185              :         }
   16186              : 
   16187              :       /* Check this exists and is a SUBROUTINE.  */
   16188          533 :       if (!list->proc_sym->attr.subroutine)
   16189              :         {
   16190            3 :           gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
   16191              :                      list->proc_sym->name, &list->where);
   16192            3 :           goto error;
   16193              :         }
   16194              : 
   16195              :       /* We should have exactly one argument.  */
   16196          530 :       dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
   16197          530 :       if (!dummy_args || dummy_args->next)
   16198              :         {
   16199            2 :           gfc_error ("FINAL procedure at %L must have exactly one argument",
   16200              :                      &list->where);
   16201            2 :           goto error;
   16202              :         }
   16203          528 :       arg = dummy_args->sym;
   16204              : 
   16205          528 :       if (!arg)
   16206              :         {
   16207            1 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16208            1 :                      &list->proc_sym->declared_at, derived->name);
   16209            1 :           goto error;
   16210              :         }
   16211              : 
   16212          527 :       if (arg->as && arg->as->type == AS_ASSUMED_RANK
   16213            6 :           && ((list != derived->f2k_derived->finalizers) || list->next))
   16214              :         {
   16215            0 :           gfc_error ("FINAL procedure at %L with assumed rank argument must "
   16216              :                      "be the only finalizer with the same kind/type "
   16217              :                      "(F2018: C790)", &list->where);
   16218            0 :           goto error;
   16219              :         }
   16220              : 
   16221              :       /* This argument must be of our type.  */
   16222          527 :       if (!derived->attr.pdt_template
   16223          527 :           && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
   16224              :         {
   16225            2 :           gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
   16226              :                      &arg->declared_at, derived->name);
   16227            2 :           goto error;
   16228              :         }
   16229              : 
   16230              :       /* It must neither be a pointer nor allocatable nor optional.  */
   16231          525 :       if (arg->attr.pointer)
   16232              :         {
   16233            1 :           gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
   16234              :                      &arg->declared_at);
   16235            1 :           goto error;
   16236              :         }
   16237          524 :       if (arg->attr.allocatable)
   16238              :         {
   16239            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16240              :                      " ALLOCATABLE", &arg->declared_at);
   16241            1 :           goto error;
   16242              :         }
   16243          523 :       if (arg->attr.optional)
   16244              :         {
   16245            1 :           gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
   16246              :                      &arg->declared_at);
   16247            1 :           goto error;
   16248              :         }
   16249              : 
   16250              :       /* It must not be INTENT(OUT).  */
   16251          522 :       if (arg->attr.intent == INTENT_OUT)
   16252              :         {
   16253            1 :           gfc_error ("Argument of FINAL procedure at %L must not be"
   16254              :                      " INTENT(OUT)", &arg->declared_at);
   16255            1 :           goto error;
   16256              :         }
   16257              : 
   16258              :       /* Warn if the procedure is non-scalar and not assumed shape.  */
   16259          521 :       if (warn_surprising && arg->as && arg->as->rank != 0
   16260            3 :           && arg->as->type != AS_ASSUMED_SHAPE)
   16261            2 :         gfc_warning (OPT_Wsurprising,
   16262              :                      "Non-scalar FINAL procedure at %L should have assumed"
   16263              :                      " shape argument", &arg->declared_at);
   16264              : 
   16265              :       /* Check that it does not match in kind and rank with a FINAL procedure
   16266              :          defined earlier.  To really loop over the *earlier* declarations,
   16267              :          we need to walk the tail of the list as new ones were pushed at the
   16268              :          front.  */
   16269              :       /* TODO: Handle kind parameters once they are implemented.  */
   16270          521 :       my_rank = (arg->as ? arg->as->rank : 0);
   16271          616 :       for (i = list->next; i; i = i->next)
   16272              :         {
   16273           97 :           gfc_formal_arglist *dummy_args;
   16274              : 
   16275              :           /* Argument list might be empty; that is an error signalled earlier,
   16276              :              but we nevertheless continued resolving.  */
   16277           97 :           dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
   16278           97 :           if (dummy_args && !derived->attr.pdt_template)
   16279              :             {
   16280           95 :               gfc_symbol* i_arg = dummy_args->sym;
   16281           95 :               const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
   16282           95 :               if (i_rank == my_rank)
   16283              :                 {
   16284            2 :                   gfc_error ("FINAL procedure %qs declared at %L has the same"
   16285              :                              " rank (%d) as %qs",
   16286            2 :                              list->proc_sym->name, &list->where, my_rank,
   16287            2 :                              i->proc_sym->name);
   16288            2 :                   goto error;
   16289              :                 }
   16290              :             }
   16291              :         }
   16292              : 
   16293              :         /* Is this the/a scalar finalizer procedure?  */
   16294          519 :         if (my_rank == 0)
   16295          393 :           seen_scalar = true;
   16296              : 
   16297              :         /* Find the symtree for this procedure.  */
   16298          519 :         gcc_assert (!list->proc_tree);
   16299          519 :         list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
   16300              : 
   16301          519 :         prev_link = &list->next;
   16302          519 :         continue;
   16303              : 
   16304              :         /* Remove wrong nodes immediately from the list so we don't risk any
   16305              :            troubles in the future when they might fail later expectations.  */
   16306           14 : error:
   16307           14 :         i = list;
   16308           14 :         *prev_link = list->next;
   16309           14 :         gfc_free_finalizer (i);
   16310           14 :         result = false;
   16311          519 :     }
   16312              : 
   16313         2535 :   if (result == false)
   16314              :     return false;
   16315              : 
   16316              :   /* Warn if we haven't seen a scalar finalizer procedure (but we know there
   16317              :      were nodes in the list, must have been for arrays.  It is surely a good
   16318              :      idea to have a scalar version there if there's something to finalize.  */
   16319         2531 :   if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
   16320            1 :     gfc_warning (OPT_Wsurprising,
   16321              :                  "Only array FINAL procedures declared for derived type %qs"
   16322              :                  " defined at %L, suggest also scalar one unless an assumed"
   16323              :                  " rank finalizer has been declared",
   16324              :                  derived->name, &derived->declared_at);
   16325              : 
   16326         2531 :   if (!derived->attr.pdt_template)
   16327              :     {
   16328         2507 :       vtab = gfc_find_derived_vtab (derived);
   16329         2507 :       c = vtab->ts.u.derived->components->next->next->next->next->next;
   16330         2507 :       if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
   16331         2507 :         gfc_set_sym_referenced (c->initializer->symtree->n.sym);
   16332              :     }
   16333              : 
   16334         2531 :   if (finalizable)
   16335          640 :     *finalizable = true;
   16336              : 
   16337              :   return true;
   16338              : }
   16339              : 
   16340              : 
   16341              : static gfc_symbol * containing_dt;
   16342              : 
   16343              : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
   16344              :    arguments whose declared types are PDT instances only transmit the PASS arg
   16345              :    if they match the enclosing derived type.  */
   16346              : 
   16347              : static bool
   16348         1460 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
   16349              : {
   16350         1460 :   gfc_formal_arglist *dummy_args;
   16351         1460 :   if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
   16352              :     {
   16353          532 :       dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
   16354         1190 :       while (dummy_args && strcmp (pass, dummy_args->sym->name))
   16355          126 :         dummy_args = dummy_args->next;
   16356          532 :       gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
   16357          532 :       if (dummy_args->sym->ts.type == BT_CLASS
   16358          532 :           && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
   16359              :                      containing_dt->name))
   16360              :         return true;
   16361              :     }
   16362              :   return false;
   16363              : }
   16364              : 
   16365              : 
   16366              : /* Check if two GENERIC targets are ambiguous and emit an error is they are.  */
   16367              : 
   16368              : static bool
   16369          732 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
   16370              :                              const char* generic_name, locus where)
   16371              : {
   16372          732 :   gfc_symbol *sym1, *sym2;
   16373          732 :   const char *pass1, *pass2;
   16374          732 :   gfc_formal_arglist *dummy_args;
   16375              : 
   16376          732 :   gcc_assert (t1->specific && t2->specific);
   16377          732 :   gcc_assert (!t1->specific->is_generic);
   16378          732 :   gcc_assert (!t2->specific->is_generic);
   16379          732 :   gcc_assert (t1->is_operator == t2->is_operator);
   16380              : 
   16381          732 :   sym1 = t1->specific->u.specific->n.sym;
   16382          732 :   sym2 = t2->specific->u.specific->n.sym;
   16383              : 
   16384          732 :   if (sym1 == sym2)
   16385              :     return true;
   16386              : 
   16387              :   /* Both must be SUBROUTINEs or both must be FUNCTIONs.  */
   16388          732 :   if (sym1->attr.subroutine != sym2->attr.subroutine
   16389          730 :       || sym1->attr.function != sym2->attr.function)
   16390              :     {
   16391            2 :       gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
   16392              :                  " GENERIC %qs at %L",
   16393              :                  sym1->name, sym2->name, generic_name, &where);
   16394            2 :       return false;
   16395              :     }
   16396              : 
   16397              :   /* Determine PASS arguments.  */
   16398          730 :   if (t1->specific->nopass)
   16399              :     pass1 = NULL;
   16400          679 :   else if (t1->specific->pass_arg)
   16401              :     pass1 = t1->specific->pass_arg;
   16402              :   else
   16403              :     {
   16404          420 :       dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
   16405          420 :       if (dummy_args)
   16406          419 :         pass1 = dummy_args->sym->name;
   16407              :       else
   16408              :         pass1 = NULL;
   16409              :     }
   16410          730 :   if (t2->specific->nopass)
   16411              :     pass2 = NULL;
   16412          678 :   else if (t2->specific->pass_arg)
   16413              :     pass2 = t2->specific->pass_arg;
   16414              :   else
   16415              :     {
   16416          541 :       dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
   16417          541 :       if (dummy_args)
   16418          540 :         pass2 = dummy_args->sym->name;
   16419              :       else
   16420              :         pass2 = NULL;
   16421              :     }
   16422              : 
   16423              :   /* Care must be taken with pdt types and templates because the declared type
   16424              :      of the argument that is not 'no_pass' need not be the same as the
   16425              :      containing derived type.  If this is the case, subject the argument to
   16426              :      the full interface check, even though it cannot be used in the type
   16427              :      bound context.  */
   16428          730 :   pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
   16429          730 :   pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
   16430              : 
   16431          730 :   if (containing_dt != NULL && containing_dt->attr.pdt_template)
   16432          730 :     pass1 = pass2 = NULL;
   16433              : 
   16434              :   /* Compare the interfaces.  */
   16435          730 :   if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
   16436              :                               NULL, 0, pass1, pass2))
   16437              :     {
   16438            8 :       gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
   16439              :                  sym1->name, sym2->name, generic_name, &where);
   16440            8 :       return false;
   16441              :     }
   16442              : 
   16443              :   return true;
   16444              : }
   16445              : 
   16446              : 
   16447              : /* Worker function for resolving a generic procedure binding; this is used to
   16448              :    resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
   16449              : 
   16450              :    The difference between those cases is finding possible inherited bindings
   16451              :    that are overridden, as one has to look for them in tb_sym_root,
   16452              :    tb_uop_root or tb_op, respectively.  Thus the caller must already find
   16453              :    the super-type and set p->overridden correctly.  */
   16454              : 
   16455              : static bool
   16456         2310 : resolve_tb_generic_targets (gfc_symbol* super_type,
   16457              :                             gfc_typebound_proc* p, const char* name)
   16458              : {
   16459         2310 :   gfc_tbp_generic* target;
   16460         2310 :   gfc_symtree* first_target;
   16461         2310 :   gfc_symtree* inherited;
   16462              : 
   16463         2310 :   gcc_assert (p && p->is_generic);
   16464              : 
   16465              :   /* Try to find the specific bindings for the symtrees in our target-list.  */
   16466         2310 :   gcc_assert (p->u.generic);
   16467         5200 :   for (target = p->u.generic; target; target = target->next)
   16468         2907 :     if (!target->specific)
   16469              :       {
   16470         2528 :         gfc_typebound_proc* overridden_tbp;
   16471         2528 :         gfc_tbp_generic* g;
   16472         2528 :         const char* target_name;
   16473              : 
   16474         2528 :         target_name = target->specific_st->name;
   16475              : 
   16476              :         /* Defined for this type directly.  */
   16477         2528 :         if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
   16478              :           {
   16479         2519 :             target->specific = target->specific_st->n.tb;
   16480         2519 :             goto specific_found;
   16481              :           }
   16482              : 
   16483              :         /* Look for an inherited specific binding.  */
   16484            9 :         if (super_type)
   16485              :           {
   16486            5 :             inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
   16487              :                                                  true, NULL);
   16488              : 
   16489            5 :             if (inherited)
   16490              :               {
   16491            5 :                 gcc_assert (inherited->n.tb);
   16492            5 :                 target->specific = inherited->n.tb;
   16493            5 :                 goto specific_found;
   16494              :               }
   16495              :           }
   16496              : 
   16497            4 :         gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
   16498              :                    " at %L", target_name, name, &p->where);
   16499            4 :         return false;
   16500              : 
   16501              :         /* Once we've found the specific binding, check it is not ambiguous with
   16502              :            other specifics already found or inherited for the same GENERIC.  */
   16503         2524 : specific_found:
   16504         2524 :         gcc_assert (target->specific);
   16505              : 
   16506              :         /* This must really be a specific binding!  */
   16507         2524 :         if (target->specific->is_generic)
   16508              :           {
   16509            3 :             gfc_error ("GENERIC %qs at %L must target a specific binding,"
   16510              :                        " %qs is GENERIC, too", name, &p->where, target_name);
   16511            3 :             return false;
   16512              :           }
   16513              : 
   16514              :         /* Check those already resolved on this type directly.  */
   16515         6456 :         for (g = p->u.generic; g; g = g->next)
   16516         1428 :           if (g != target && g->specific
   16517         4656 :               && !check_generic_tbp_ambiguity (target, g, name, p->where))
   16518              :             return false;
   16519              : 
   16520              :         /* Check for ambiguity with inherited specific targets.  */
   16521         2530 :         for (overridden_tbp = p->overridden; overridden_tbp;
   16522           16 :              overridden_tbp = overridden_tbp->overridden)
   16523           19 :           if (overridden_tbp->is_generic)
   16524              :             {
   16525           33 :               for (g = overridden_tbp->u.generic; g; g = g->next)
   16526              :                 {
   16527           18 :                   gcc_assert (g->specific);
   16528           18 :                   if (!check_generic_tbp_ambiguity (target, g, name, p->where))
   16529              :                     return false;
   16530              :                 }
   16531              :             }
   16532              :       }
   16533              : 
   16534              :   /* If we attempt to "overwrite" a specific binding, this is an error.  */
   16535         2293 :   if (p->overridden && !p->overridden->is_generic)
   16536              :     {
   16537            1 :       gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
   16538              :                  " the same name", name, &p->where);
   16539            1 :       return false;
   16540              :     }
   16541              : 
   16542              :   /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
   16543              :      all must have the same attributes here.  */
   16544         2292 :   first_target = p->u.generic->specific->u.specific;
   16545         2292 :   gcc_assert (first_target);
   16546         2292 :   p->subroutine = first_target->n.sym->attr.subroutine;
   16547         2292 :   p->function = first_target->n.sym->attr.function;
   16548              : 
   16549         2292 :   return true;
   16550              : }
   16551              : 
   16552              : 
   16553              : /* Resolve a GENERIC procedure binding for a derived type.  */
   16554              : 
   16555              : static bool
   16556         1204 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
   16557              : {
   16558         1204 :   gfc_symbol* super_type;
   16559              : 
   16560              :   /* Find the overridden binding if any.  */
   16561         1204 :   st->n.tb->overridden = NULL;
   16562         1204 :   super_type = gfc_get_derived_super_type (derived);
   16563         1204 :   if (super_type)
   16564              :     {
   16565           40 :       gfc_symtree* overridden;
   16566           40 :       overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
   16567              :                                             true, NULL);
   16568              : 
   16569           40 :       if (overridden && overridden->n.tb)
   16570           21 :         st->n.tb->overridden = overridden->n.tb;
   16571              :     }
   16572              : 
   16573              :   /* Resolve using worker function.  */
   16574         1204 :   return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
   16575              : }
   16576              : 
   16577              : 
   16578              : /* Retrieve the target-procedure of an operator binding and do some checks in
   16579              :    common for intrinsic and user-defined type-bound operators.  */
   16580              : 
   16581              : static gfc_symbol*
   16582         1178 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
   16583              : {
   16584         1178 :   gfc_symbol* target_proc;
   16585              : 
   16586         1178 :   gcc_assert (target->specific && !target->specific->is_generic);
   16587         1178 :   target_proc = target->specific->u.specific->n.sym;
   16588         1178 :   gcc_assert (target_proc);
   16589              : 
   16590              :   /* F08:C468. All operator bindings must have a passed-object dummy argument.  */
   16591         1178 :   if (target->specific->nopass)
   16592              :     {
   16593            2 :       gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
   16594            2 :       return NULL;
   16595              :     }
   16596              : 
   16597              :   return target_proc;
   16598              : }
   16599              : 
   16600              : 
   16601              : /* Resolve a type-bound intrinsic operator.  */
   16602              : 
   16603              : static bool
   16604         1047 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
   16605              :                                 gfc_typebound_proc* p)
   16606              : {
   16607         1047 :   gfc_symbol* super_type;
   16608         1047 :   gfc_tbp_generic* target;
   16609              : 
   16610              :   /* If there's already an error here, do nothing (but don't fail again).  */
   16611         1047 :   if (p->error)
   16612              :     return true;
   16613              : 
   16614              :   /* Operators should always be GENERIC bindings.  */
   16615         1047 :   gcc_assert (p->is_generic);
   16616              : 
   16617              :   /* Look for an overridden binding.  */
   16618         1047 :   super_type = gfc_get_derived_super_type (derived);
   16619         1047 :   if (super_type && super_type->f2k_derived)
   16620            1 :     p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
   16621              :                                                      op, true, NULL);
   16622              :   else
   16623         1046 :     p->overridden = NULL;
   16624              : 
   16625              :   /* Resolve general GENERIC properties using worker function.  */
   16626         1047 :   if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
   16627            1 :     goto error;
   16628              : 
   16629              :   /* Check the targets to be procedures of correct interface.  */
   16630         2139 :   for (target = p->u.generic; target; target = target->next)
   16631              :     {
   16632         1118 :       gfc_symbol* target_proc;
   16633              : 
   16634         1118 :       target_proc = get_checked_tb_operator_target (target, p->where);
   16635         1118 :       if (!target_proc)
   16636            1 :         goto error;
   16637              : 
   16638         1117 :       if (!gfc_check_operator_interface (target_proc, op, p->where))
   16639            3 :         goto error;
   16640              : 
   16641              :       /* Add target to non-typebound operator list.  */
   16642         1114 :       if (!target->specific->deferred && !derived->attr.use_assoc
   16643          391 :           && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
   16644              :         {
   16645          389 :           gfc_interface *head, *intr;
   16646              : 
   16647              :           /* Preempt 'gfc_check_new_interface' for submodules, where the
   16648              :              mechanism for handling module procedures winds up resolving
   16649              :              operator interfaces twice and would otherwise cause an error.
   16650              :              Likewise, new instances of PDTs can cause the operator inter-
   16651              :              faces to be resolved multiple times.  */
   16652          461 :           for (intr = derived->ns->op[op]; intr; intr = intr->next)
   16653           91 :             if (intr->sym == target_proc
   16654           21 :                 && (target_proc->attr.used_in_submodule
   16655            4 :                     || derived->attr.pdt_type
   16656            2 :                     || derived->attr.pdt_template))
   16657              :               return true;
   16658              : 
   16659          370 :           if (!gfc_check_new_interface (derived->ns->op[op],
   16660              :                                         target_proc, p->where))
   16661              :             return false;
   16662          368 :           head = derived->ns->op[op];
   16663          368 :           intr = gfc_get_interface ();
   16664          368 :           intr->sym = target_proc;
   16665          368 :           intr->where = p->where;
   16666          368 :           intr->next = head;
   16667          368 :           derived->ns->op[op] = intr;
   16668              :         }
   16669              :     }
   16670              : 
   16671              :   return true;
   16672              : 
   16673            5 : error:
   16674            5 :   p->error = 1;
   16675            5 :   return false;
   16676              : }
   16677              : 
   16678              : 
   16679              : /* Resolve a type-bound user operator (tree-walker callback).  */
   16680              : 
   16681              : static gfc_symbol* resolve_bindings_derived;
   16682              : static bool resolve_bindings_result;
   16683              : 
   16684              : static bool check_uop_procedure (gfc_symbol* sym, locus where);
   16685              : 
   16686              : static void
   16687           59 : resolve_typebound_user_op (gfc_symtree* stree)
   16688              : {
   16689           59 :   gfc_symbol* super_type;
   16690           59 :   gfc_tbp_generic* target;
   16691              : 
   16692           59 :   gcc_assert (stree && stree->n.tb);
   16693              : 
   16694           59 :   if (stree->n.tb->error)
   16695              :     return;
   16696              : 
   16697              :   /* Operators should always be GENERIC bindings.  */
   16698           59 :   gcc_assert (stree->n.tb->is_generic);
   16699              : 
   16700              :   /* Find overridden procedure, if any.  */
   16701           59 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16702           59 :   if (super_type && super_type->f2k_derived)
   16703              :     {
   16704            0 :       gfc_symtree* overridden;
   16705            0 :       overridden = gfc_find_typebound_user_op (super_type, NULL,
   16706              :                                                stree->name, true, NULL);
   16707              : 
   16708            0 :       if (overridden && overridden->n.tb)
   16709            0 :         stree->n.tb->overridden = overridden->n.tb;
   16710              :     }
   16711              :   else
   16712           59 :     stree->n.tb->overridden = NULL;
   16713              : 
   16714              :   /* Resolve basically using worker function.  */
   16715           59 :   if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
   16716            0 :     goto error;
   16717              : 
   16718              :   /* Check the targets to be functions of correct interface.  */
   16719          116 :   for (target = stree->n.tb->u.generic; target; target = target->next)
   16720              :     {
   16721           60 :       gfc_symbol* target_proc;
   16722              : 
   16723           60 :       target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
   16724           60 :       if (!target_proc)
   16725            1 :         goto error;
   16726              : 
   16727           59 :       if (!check_uop_procedure (target_proc, stree->n.tb->where))
   16728            2 :         goto error;
   16729              :     }
   16730              : 
   16731              :   return;
   16732              : 
   16733            3 : error:
   16734            3 :   resolve_bindings_result = false;
   16735            3 :   stree->n.tb->error = 1;
   16736              : }
   16737              : 
   16738              : 
   16739              : /* Resolve the type-bound procedures for a derived type.  */
   16740              : 
   16741              : static void
   16742         9951 : resolve_typebound_procedure (gfc_symtree* stree)
   16743              : {
   16744         9951 :   gfc_symbol* proc;
   16745         9951 :   locus where;
   16746         9951 :   gfc_symbol* me_arg;
   16747         9951 :   gfc_symbol* super_type;
   16748         9951 :   gfc_component* comp;
   16749              : 
   16750         9951 :   gcc_assert (stree);
   16751              : 
   16752              :   /* Undefined specific symbol from GENERIC target definition.  */
   16753         9951 :   if (!stree->n.tb)
   16754         9869 :     return;
   16755              : 
   16756         9945 :   if (stree->n.tb->error)
   16757              :     return;
   16758              : 
   16759              :   /* If this is a GENERIC binding, use that routine.  */
   16760         9929 :   if (stree->n.tb->is_generic)
   16761              :     {
   16762         1204 :       if (!resolve_typebound_generic (resolve_bindings_derived, stree))
   16763           17 :         goto error;
   16764              :       return;
   16765              :     }
   16766              : 
   16767              :   /* Get the target-procedure to check it.  */
   16768         8725 :   gcc_assert (!stree->n.tb->is_generic);
   16769         8725 :   gcc_assert (stree->n.tb->u.specific);
   16770         8725 :   proc = stree->n.tb->u.specific->n.sym;
   16771         8725 :   where = stree->n.tb->where;
   16772              : 
   16773              :   /* Default access should already be resolved from the parser.  */
   16774         8725 :   gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
   16775              : 
   16776         8725 :   if (stree->n.tb->deferred)
   16777              :     {
   16778          676 :       if (!check_proc_interface (proc, &where))
   16779            5 :         goto error;
   16780              :     }
   16781              :   else
   16782              :     {
   16783              :       /* If proc has not been resolved at this point, proc->name may
   16784              :          actually be a USE associated entity. See PR fortran/89647. */
   16785         8049 :       if (!proc->resolve_symbol_called
   16786         5365 :           && proc->attr.function == 0 && proc->attr.subroutine == 0)
   16787              :         {
   16788           11 :           gfc_symbol *tmp;
   16789           11 :           gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
   16790           11 :           if (tmp && tmp->attr.use_assoc)
   16791              :             {
   16792            1 :               proc->module = tmp->module;
   16793            1 :               proc->attr.proc = tmp->attr.proc;
   16794            1 :               proc->attr.function = tmp->attr.function;
   16795            1 :               proc->attr.subroutine = tmp->attr.subroutine;
   16796            1 :               proc->attr.use_assoc = tmp->attr.use_assoc;
   16797            1 :               proc->ts = tmp->ts;
   16798            1 :               proc->result = tmp->result;
   16799              :             }
   16800              :         }
   16801              : 
   16802              :       /* Check for F08:C465.  */
   16803         8049 :       if ((!proc->attr.subroutine && !proc->attr.function)
   16804         8039 :           || (proc->attr.proc != PROC_MODULE
   16805           70 :               && proc->attr.if_source != IFSRC_IFBODY
   16806            7 :               && !proc->attr.module_procedure)
   16807         8038 :           || proc->attr.abstract)
   16808              :         {
   16809           12 :           gfc_error ("%qs must be a module procedure or an external "
   16810              :                      "procedure with an explicit interface at %L",
   16811              :                      proc->name, &where);
   16812           12 :           goto error;
   16813              :         }
   16814              :     }
   16815              : 
   16816         8708 :   stree->n.tb->subroutine = proc->attr.subroutine;
   16817         8708 :   stree->n.tb->function = proc->attr.function;
   16818              : 
   16819              :   /* Find the super-type of the current derived type.  We could do this once and
   16820              :      store in a global if speed is needed, but as long as not I believe this is
   16821              :      more readable and clearer.  */
   16822         8708 :   super_type = gfc_get_derived_super_type (resolve_bindings_derived);
   16823              : 
   16824              :   /* If PASS, resolve and check arguments if not already resolved / loaded
   16825              :      from a .mod file.  */
   16826         8708 :   if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
   16827              :     {
   16828         2774 :       gfc_formal_arglist *dummy_args;
   16829              : 
   16830         2774 :       dummy_args = gfc_sym_get_dummy_args (proc);
   16831         2774 :       if (stree->n.tb->pass_arg)
   16832              :         {
   16833          468 :           gfc_formal_arglist *i;
   16834              : 
   16835              :           /* If an explicit passing argument name is given, walk the arg-list
   16836              :              and look for it.  */
   16837              : 
   16838          468 :           me_arg = NULL;
   16839          468 :           stree->n.tb->pass_arg_num = 1;
   16840          601 :           for (i = dummy_args; i; i = i->next)
   16841              :             {
   16842          599 :               if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
   16843              :                 {
   16844              :                   me_arg = i->sym;
   16845              :                   break;
   16846              :                 }
   16847          133 :               ++stree->n.tb->pass_arg_num;
   16848              :             }
   16849              : 
   16850          468 :           if (!me_arg)
   16851              :             {
   16852            2 :               gfc_error ("Procedure %qs with PASS(%s) at %L has no"
   16853              :                          " argument %qs",
   16854              :                          proc->name, stree->n.tb->pass_arg, &where,
   16855              :                          stree->n.tb->pass_arg);
   16856            2 :               goto error;
   16857              :             }
   16858              :         }
   16859              :       else
   16860              :         {
   16861              :           /* Otherwise, take the first one; there should in fact be at least
   16862              :              one.  */
   16863         2306 :           stree->n.tb->pass_arg_num = 1;
   16864         2306 :           if (!dummy_args)
   16865              :             {
   16866            2 :               gfc_error ("Procedure %qs with PASS at %L must have at"
   16867              :                          " least one argument", proc->name, &where);
   16868            2 :               goto error;
   16869              :             }
   16870         2304 :           me_arg = dummy_args->sym;
   16871              :         }
   16872              : 
   16873              :       /* Now check that the argument-type matches and the passed-object
   16874              :          dummy argument is generally fine.  */
   16875              : 
   16876         2304 :       gcc_assert (me_arg);
   16877              : 
   16878         2770 :       if (me_arg->ts.type != BT_CLASS)
   16879              :         {
   16880            5 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   16881              :                      " at %L", proc->name, &where);
   16882            5 :           goto error;
   16883              :         }
   16884              : 
   16885              :       /* The derived type is not a PDT template or type.  Resolve as usual.  */
   16886         2765 :       if (!resolve_bindings_derived->attr.pdt_template
   16887         2756 :           && !(containing_dt && containing_dt->attr.pdt_type
   16888           60 :                && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
   16889         2736 :           && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
   16890              :         {
   16891            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   16892              :                      "the derived-type %qs", me_arg->name, proc->name,
   16893              :                      me_arg->name, &where, resolve_bindings_derived->name);
   16894            0 :           goto error;
   16895              :         }
   16896              : 
   16897         2765 :       if (resolve_bindings_derived->attr.pdt_template
   16898         2774 :           && !gfc_pdt_is_instance_of (resolve_bindings_derived,
   16899            9 :                                       CLASS_DATA (me_arg)->ts.u.derived))
   16900              :         {
   16901            0 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
   16902              :                      "the parametric derived-type %qs", me_arg->name,
   16903              :                      proc->name, me_arg->name, &where,
   16904              :                      resolve_bindings_derived->name);
   16905            0 :           goto error;
   16906              :         }
   16907              : 
   16908         2765 :       if (((resolve_bindings_derived->attr.pdt_template
   16909            9 :             && gfc_pdt_is_instance_of (resolve_bindings_derived,
   16910            9 :                                        CLASS_DATA (me_arg)->ts.u.derived))
   16911         2756 :            || resolve_bindings_derived->attr.pdt_type)
   16912           69 :           && (me_arg->param_list != NULL)
   16913         2834 :           && (gfc_spec_list_type (me_arg->param_list,
   16914           69 :                                   CLASS_DATA(me_arg)->ts.u.derived)
   16915              :                                   != SPEC_ASSUMED))
   16916              :         {
   16917              : 
   16918              :           /* Add a check to verify if there are any LEN parameters in the
   16919              :              first place.  If there are LEN parameters, throw this error.
   16920              :              If there are only KIND parameters, then don't trigger
   16921              :              this error.  */
   16922            6 :           gfc_component *c;
   16923            6 :           bool seen_len_param = false;
   16924            6 :           gfc_actual_arglist *me_arg_param = me_arg->param_list;
   16925              : 
   16926            6 :           for (; me_arg_param; me_arg_param = me_arg_param->next)
   16927              :             {
   16928            6 :               c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
   16929              :                                      me_arg_param->name, true, true, NULL);
   16930              : 
   16931            6 :               gcc_assert (c != NULL);
   16932              : 
   16933            6 :               if (c->attr.pdt_kind)
   16934            0 :                 continue;
   16935              : 
   16936              :               /* Getting here implies that there is a pdt_len parameter
   16937              :                  in the list.  */
   16938              :               seen_len_param = true;
   16939              :               break;
   16940              :             }
   16941              : 
   16942            6 :             if (seen_len_param)
   16943              :               {
   16944            6 :                 gfc_error ("All LEN type parameters of the passed dummy "
   16945              :                            "argument %qs of %qs at %L must be ASSUMED.",
   16946              :                            me_arg->name, proc->name, &where);
   16947            6 :                 goto error;
   16948              :               }
   16949              :         }
   16950              : 
   16951         2759 :       gcc_assert (me_arg->ts.type == BT_CLASS);
   16952         2759 :       if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
   16953              :         {
   16954            1 :           gfc_error ("Passed-object dummy argument of %qs at %L must be"
   16955              :                      " scalar", proc->name, &where);
   16956            1 :           goto error;
   16957              :         }
   16958         2758 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   16959              :         {
   16960            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   16961              :                      " be ALLOCATABLE", proc->name, &where);
   16962            2 :           goto error;
   16963              :         }
   16964         2756 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   16965              :         {
   16966            2 :           gfc_error ("Passed-object dummy argument of %qs at %L must not"
   16967              :                      " be POINTER", proc->name, &where);
   16968            2 :           goto error;
   16969              :         }
   16970              :     }
   16971              : 
   16972              :   /* If we are extending some type, check that we don't override a procedure
   16973              :      flagged NON_OVERRIDABLE.  */
   16974         8688 :   stree->n.tb->overridden = NULL;
   16975         8688 :   if (super_type)
   16976              :     {
   16977         1491 :       gfc_symtree* overridden;
   16978         1491 :       overridden = gfc_find_typebound_proc (super_type, NULL,
   16979              :                                             stree->name, true, NULL);
   16980              : 
   16981         1491 :       if (overridden)
   16982              :         {
   16983         1214 :           if (overridden->n.tb)
   16984         1214 :             stree->n.tb->overridden = overridden->n.tb;
   16985              : 
   16986         1214 :           if (!gfc_check_typebound_override (stree, overridden))
   16987           26 :             goto error;
   16988              :         }
   16989              :     }
   16990              : 
   16991              :   /* See if there's a name collision with a component directly in this type.  */
   16992        20866 :   for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
   16993        12205 :     if (!strcmp (comp->name, stree->name))
   16994              :       {
   16995            1 :         gfc_error ("Procedure %qs at %L has the same name as a component of"
   16996              :                    " %qs",
   16997              :                    stree->name, &where, resolve_bindings_derived->name);
   16998            1 :         goto error;
   16999              :       }
   17000              : 
   17001              :   /* Try to find a name collision with an inherited component.  */
   17002         8661 :   if (super_type && gfc_find_component (super_type, stree->name, true, true,
   17003              :                                         NULL))
   17004              :     {
   17005            1 :       gfc_error ("Procedure %qs at %L has the same name as an inherited"
   17006              :                  " component of %qs",
   17007              :                  stree->name, &where, resolve_bindings_derived->name);
   17008            1 :       goto error;
   17009              :     }
   17010              : 
   17011         8660 :   stree->n.tb->error = 0;
   17012         8660 :   return;
   17013              : 
   17014           82 : error:
   17015           82 :   resolve_bindings_result = false;
   17016           82 :   stree->n.tb->error = 1;
   17017              : }
   17018              : 
   17019              : 
   17020              : static bool
   17021        85963 : resolve_typebound_procedures (gfc_symbol* derived)
   17022              : {
   17023        85963 :   int op;
   17024        85963 :   gfc_symbol* super_type;
   17025              : 
   17026        85963 :   if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
   17027              :     return true;
   17028              : 
   17029         4770 :   super_type = gfc_get_derived_super_type (derived);
   17030         4770 :   if (super_type)
   17031          857 :     resolve_symbol (super_type);
   17032              : 
   17033         4770 :   resolve_bindings_derived = derived;
   17034         4770 :   resolve_bindings_result = true;
   17035              : 
   17036         4770 :   containing_dt = derived;  /* Needed for checks of PDTs.  */
   17037         4770 :   if (derived->f2k_derived->tb_sym_root)
   17038         4770 :     gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
   17039              :                           &resolve_typebound_procedure);
   17040              : 
   17041         4770 :   if (derived->f2k_derived->tb_uop_root)
   17042           55 :     gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
   17043              :                           &resolve_typebound_user_op);
   17044         4770 :   containing_dt = NULL;
   17045              : 
   17046       138330 :   for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
   17047              :     {
   17048       133560 :       gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
   17049       133560 :       if (p && !resolve_typebound_intrinsic_op (derived,
   17050              :                                                 (gfc_intrinsic_op)op, p))
   17051            7 :         resolve_bindings_result = false;
   17052              :     }
   17053              : 
   17054         4770 :   return resolve_bindings_result;
   17055              : }
   17056              : 
   17057              : 
   17058              : /* Add a derived type to the dt_list.  The dt_list is used in trans-types.cc
   17059              :    to give all identical derived types the same backend_decl.  */
   17060              : static void
   17061       176455 : add_dt_to_dt_list (gfc_symbol *derived)
   17062              : {
   17063       176455 :   if (!derived->dt_next)
   17064              :     {
   17065        82146 :       if (gfc_derived_types)
   17066              :         {
   17067        67344 :           derived->dt_next = gfc_derived_types->dt_next;
   17068        67344 :           gfc_derived_types->dt_next = derived;
   17069              :         }
   17070              :       else
   17071              :         {
   17072        14802 :           derived->dt_next = derived;
   17073              :         }
   17074        82146 :       gfc_derived_types = derived;
   17075              :     }
   17076       176455 : }
   17077              : 
   17078              : 
   17079              : /* Ensure that a derived-type is really not abstract, meaning that every
   17080              :    inherited DEFERRED binding is overridden by a non-DEFERRED one.  */
   17081              : 
   17082              : static bool
   17083         7086 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
   17084              : {
   17085         7086 :   if (!st)
   17086              :     return true;
   17087              : 
   17088         2772 :   if (!ensure_not_abstract_walker (sub, st->left))
   17089              :     return false;
   17090         2772 :   if (!ensure_not_abstract_walker (sub, st->right))
   17091              :     return false;
   17092              : 
   17093         2771 :   if (st->n.tb && st->n.tb->deferred)
   17094              :     {
   17095         2019 :       gfc_symtree* overriding;
   17096         2019 :       overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
   17097         2019 :       if (!overriding)
   17098              :         return false;
   17099         2018 :       gcc_assert (overriding->n.tb);
   17100         2018 :       if (overriding->n.tb->deferred)
   17101              :         {
   17102            5 :           gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
   17103              :                      " %qs is DEFERRED and not overridden",
   17104              :                      sub->name, &sub->declared_at, st->name);
   17105            5 :           return false;
   17106              :         }
   17107              :     }
   17108              : 
   17109              :   return true;
   17110              : }
   17111              : 
   17112              : static bool
   17113         1394 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
   17114              : {
   17115              :   /* The algorithm used here is to recursively travel up the ancestry of sub
   17116              :      and for each ancestor-type, check all bindings.  If any of them is
   17117              :      DEFERRED, look it up starting from sub and see if the found (overriding)
   17118              :      binding is not DEFERRED.
   17119              :      This is not the most efficient way to do this, but it should be ok and is
   17120              :      clearer than something sophisticated.  */
   17121              : 
   17122         1543 :   gcc_assert (ancestor && !sub->attr.abstract);
   17123              : 
   17124         1543 :   if (!ancestor->attr.abstract)
   17125              :     return true;
   17126              : 
   17127              :   /* Walk bindings of this ancestor.  */
   17128         1542 :   if (ancestor->f2k_derived)
   17129              :     {
   17130         1542 :       bool t;
   17131         1542 :       t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
   17132         1542 :       if (!t)
   17133              :         return false;
   17134              :     }
   17135              : 
   17136              :   /* Find next ancestor type and recurse on it.  */
   17137         1536 :   ancestor = gfc_get_derived_super_type (ancestor);
   17138         1536 :   if (ancestor)
   17139              :     return ensure_not_abstract (sub, ancestor);
   17140              : 
   17141              :   return true;
   17142              : }
   17143              : 
   17144              : 
   17145              : /* This check for typebound defined assignments is done recursively
   17146              :    since the order in which derived types are resolved is not always in
   17147              :    order of the declarations.  */
   17148              : 
   17149              : static void
   17150       180940 : check_defined_assignments (gfc_symbol *derived)
   17151              : {
   17152       180940 :   gfc_component *c;
   17153              : 
   17154       606556 :   for (c = derived->components; c; c = c->next)
   17155              :     {
   17156       427393 :       if (!gfc_bt_struct (c->ts.type)
   17157       103244 :           || c->attr.pointer
   17158        20446 :           || c->attr.proc_pointer_comp
   17159        20446 :           || c->attr.class_pointer
   17160        20440 :           || c->attr.proc_pointer)
   17161       407397 :         continue;
   17162              : 
   17163        19996 :       if (c->ts.u.derived->attr.defined_assign_comp
   17164        19761 :           || (c->ts.u.derived->f2k_derived
   17165        19191 :              && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
   17166              :         {
   17167         1753 :           derived->attr.defined_assign_comp = 1;
   17168         1753 :           return;
   17169              :         }
   17170              : 
   17171        18243 :       if (c->attr.allocatable)
   17172         6637 :         continue;
   17173              : 
   17174        11606 :       check_defined_assignments (c->ts.u.derived);
   17175        11606 :       if (c->ts.u.derived->attr.defined_assign_comp)
   17176              :         {
   17177           24 :           derived->attr.defined_assign_comp = 1;
   17178           24 :           return;
   17179              :         }
   17180              :     }
   17181              : }
   17182              : 
   17183              : 
   17184              : /* Resolve a single component of a derived type or structure.  */
   17185              : 
   17186              : static bool
   17187       407624 : resolve_component (gfc_component *c, gfc_symbol *sym)
   17188              : {
   17189       407624 :   gfc_symbol *super_type;
   17190       407624 :   symbol_attribute *attr;
   17191              : 
   17192       407624 :   if (c->attr.artificial)
   17193              :     return true;
   17194              : 
   17195              :   /* Do not allow vtype components to be resolved in nameless namespaces
   17196              :      such as block data because the procedure pointers will cause ICEs
   17197              :      and vtables are not needed in these contexts.  */
   17198       278322 :   if (sym->attr.vtype && sym->attr.use_assoc
   17199        48512 :       && sym->ns->proc_name == NULL)
   17200              :     return true;
   17201              : 
   17202              :   /* F2008, C442.  */
   17203       278313 :   if ((!sym->attr.is_class || c != sym->components)
   17204       278313 :       && c->attr.codimension
   17205          208 :       && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
   17206              :     {
   17207            4 :       gfc_error ("Coarray component %qs at %L must be allocatable with "
   17208              :                  "deferred shape", c->name, &c->loc);
   17209            4 :       return false;
   17210              :     }
   17211              : 
   17212              :   /* F2008, C443.  */
   17213       278309 :   if (c->attr.codimension && c->ts.type == BT_DERIVED
   17214           85 :       && c->ts.u.derived->ts.is_iso_c)
   17215              :     {
   17216            1 :       gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   17217              :                  "shall not be a coarray", c->name, &c->loc);
   17218            1 :       return false;
   17219              :     }
   17220              : 
   17221              :   /* F2008, C444.  */
   17222       278308 :   if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
   17223           28 :       && (c->attr.codimension || c->attr.pointer || c->attr.dimension
   17224           26 :           || c->attr.allocatable))
   17225              :     {
   17226            3 :       gfc_error ("Component %qs at %L with coarray component "
   17227              :                  "shall be a nonpointer, nonallocatable scalar",
   17228              :                  c->name, &c->loc);
   17229            3 :       return false;
   17230              :     }
   17231              : 
   17232              :   /* F2008, C448.  */
   17233       278305 :   if (c->ts.type == BT_CLASS)
   17234              :     {
   17235         6916 :       if (c->attr.class_ok && CLASS_DATA (c))
   17236              :         {
   17237         6908 :           attr = &(CLASS_DATA (c)->attr);
   17238              : 
   17239              :           /* Fix up contiguous attribute.  */
   17240         6908 :           if (c->attr.contiguous)
   17241           11 :             attr->contiguous = 1;
   17242              :         }
   17243              :       else
   17244              :         attr = NULL;
   17245              :     }
   17246              :   else
   17247       271389 :     attr = &c->attr;
   17248              : 
   17249       278308 :   if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
   17250              :     {
   17251            5 :       gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
   17252              :                  "is not an array pointer", c->name, &c->loc);
   17253            5 :       return false;
   17254              :     }
   17255              : 
   17256              :   /* F2003, 15.2.1 - length has to be one.  */
   17257        40610 :   if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
   17258       278319 :       && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
   17259           19 :           || !gfc_is_constant_expr (c->ts.u.cl->length)
   17260           19 :           || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
   17261              :     {
   17262            1 :       gfc_error ("Component %qs of BIND(C) type at %L must have length one",
   17263              :                  c->name, &c->loc);
   17264            1 :       return false;
   17265              :     }
   17266              : 
   17267        51687 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
   17268          307 :       && !sym->attr.pdt_type && !sym->attr.pdt_template
   17269       278307 :       && !(gfc_get_derived_super_type (sym)
   17270            0 :            && (gfc_get_derived_super_type (sym)->attr.pdt_type
   17271            0 :                ||  gfc_get_derived_super_type (sym)->attr.pdt_template)))
   17272              :     {
   17273            8 :       gfc_actual_arglist *type_spec_list;
   17274            8 :       if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
   17275              :                                 &type_spec_list)
   17276              :           != MATCH_YES)
   17277            0 :         return false;
   17278            8 :       gfc_free_actual_arglist (c->param_list);
   17279            8 :       c->param_list = type_spec_list;
   17280            8 :       if (!sym->attr.pdt_type)
   17281            8 :         sym->attr.pdt_comp = 1;
   17282              :     }
   17283       278291 :   else if (IS_PDT (c) && !sym->attr.pdt_type)
   17284           54 :     sym->attr.pdt_comp = 1;
   17285              : 
   17286       278299 :   if (c->attr.proc_pointer && c->ts.interface)
   17287              :     {
   17288        14615 :       gfc_symbol *ifc = c->ts.interface;
   17289              : 
   17290        14615 :       if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
   17291              :         {
   17292            6 :           c->tb->error = 1;
   17293            6 :           return false;
   17294              :         }
   17295              : 
   17296        14609 :       if (ifc->attr.if_source || ifc->attr.intrinsic)
   17297              :         {
   17298              :           /* Resolve interface and copy attributes.  */
   17299        14560 :           if (ifc->formal && !ifc->formal_ns)
   17300         2560 :             resolve_symbol (ifc);
   17301        14560 :           if (ifc->attr.intrinsic)
   17302            0 :             gfc_resolve_intrinsic (ifc, &ifc->declared_at);
   17303              : 
   17304        14560 :           if (ifc->result)
   17305              :             {
   17306         7601 :               c->ts = ifc->result->ts;
   17307         7601 :               c->attr.allocatable = ifc->result->attr.allocatable;
   17308         7601 :               c->attr.pointer = ifc->result->attr.pointer;
   17309         7601 :               c->attr.dimension = ifc->result->attr.dimension;
   17310         7601 :               c->as = gfc_copy_array_spec (ifc->result->as);
   17311         7601 :               c->attr.class_ok = ifc->result->attr.class_ok;
   17312              :             }
   17313              :           else
   17314              :             {
   17315         6959 :               c->ts = ifc->ts;
   17316         6959 :               c->attr.allocatable = ifc->attr.allocatable;
   17317         6959 :               c->attr.pointer = ifc->attr.pointer;
   17318         6959 :               c->attr.dimension = ifc->attr.dimension;
   17319         6959 :               c->as = gfc_copy_array_spec (ifc->as);
   17320         6959 :               c->attr.class_ok = ifc->attr.class_ok;
   17321              :             }
   17322        14560 :           c->ts.interface = ifc;
   17323        14560 :           c->attr.function = ifc->attr.function;
   17324        14560 :           c->attr.subroutine = ifc->attr.subroutine;
   17325              : 
   17326        14560 :           c->attr.pure = ifc->attr.pure;
   17327        14560 :           c->attr.elemental = ifc->attr.elemental;
   17328        14560 :           c->attr.recursive = ifc->attr.recursive;
   17329        14560 :           c->attr.always_explicit = ifc->attr.always_explicit;
   17330        14560 :           c->attr.ext_attr |= ifc->attr.ext_attr;
   17331              :           /* Copy char length.  */
   17332        14560 :           if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
   17333              :             {
   17334          491 :               gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
   17335          454 :               if (cl->length && !cl->resolved
   17336          601 :                   && !gfc_resolve_expr (cl->length))
   17337              :                 {
   17338            0 :                   c->tb->error = 1;
   17339            0 :                   return false;
   17340              :                 }
   17341          491 :               c->ts.u.cl = cl;
   17342              :             }
   17343              :         }
   17344              :     }
   17345       263684 :   else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
   17346              :     {
   17347              :       /* Since PPCs are not implicitly typed, a PPC without an explicit
   17348              :          interface must be a subroutine.  */
   17349          116 :       gfc_add_subroutine (&c->attr, c->name, &c->loc);
   17350              :     }
   17351              : 
   17352              :   /* Procedure pointer components: Check PASS arg.  */
   17353       278293 :   if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
   17354          805 :       && !sym->attr.vtype)
   17355              :     {
   17356           95 :       gfc_symbol* me_arg;
   17357              : 
   17358           95 :       if (c->tb->pass_arg)
   17359              :         {
   17360           20 :           gfc_formal_arglist* i;
   17361              : 
   17362              :           /* If an explicit passing argument name is given, walk the arg-list
   17363              :             and look for it.  */
   17364              : 
   17365           20 :           me_arg = NULL;
   17366           20 :           c->tb->pass_arg_num = 1;
   17367           34 :           for (i = c->ts.interface->formal; i; i = i->next)
   17368              :             {
   17369           33 :               if (!strcmp (i->sym->name, c->tb->pass_arg))
   17370              :                 {
   17371              :                   me_arg = i->sym;
   17372              :                   break;
   17373              :                 }
   17374           14 :               c->tb->pass_arg_num++;
   17375              :             }
   17376              : 
   17377           20 :           if (!me_arg)
   17378              :             {
   17379            1 :               gfc_error ("Procedure pointer component %qs with PASS(%s) "
   17380              :                          "at %L has no argument %qs", c->name,
   17381              :                          c->tb->pass_arg, &c->loc, c->tb->pass_arg);
   17382            1 :               c->tb->error = 1;
   17383            1 :               return false;
   17384              :             }
   17385              :         }
   17386              :       else
   17387              :         {
   17388              :           /* Otherwise, take the first one; there should in fact be at least
   17389              :             one.  */
   17390           75 :           c->tb->pass_arg_num = 1;
   17391           75 :           if (!c->ts.interface->formal)
   17392              :             {
   17393            3 :               gfc_error ("Procedure pointer component %qs with PASS at %L "
   17394              :                          "must have at least one argument",
   17395              :                          c->name, &c->loc);
   17396            3 :               c->tb->error = 1;
   17397            3 :               return false;
   17398              :             }
   17399           72 :           me_arg = c->ts.interface->formal->sym;
   17400              :         }
   17401              : 
   17402              :       /* Now check that the argument-type matches.  */
   17403           72 :       gcc_assert (me_arg);
   17404           91 :       if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
   17405           90 :           || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
   17406           90 :           || (me_arg->ts.type == BT_CLASS
   17407           82 :               && CLASS_DATA (me_arg)->ts.u.derived != sym))
   17408              :         {
   17409            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
   17410              :                      " the derived type %qs", me_arg->name, c->name,
   17411              :                      me_arg->name, &c->loc, sym->name);
   17412            1 :           c->tb->error = 1;
   17413            1 :           return false;
   17414              :         }
   17415              : 
   17416              :       /* Check for F03:C453.  */
   17417           90 :       if (CLASS_DATA (me_arg)->attr.dimension)
   17418              :         {
   17419            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17420              :                      "must be scalar", me_arg->name, c->name, me_arg->name,
   17421              :                      &c->loc);
   17422            1 :           c->tb->error = 1;
   17423            1 :           return false;
   17424              :         }
   17425              : 
   17426           89 :       if (CLASS_DATA (me_arg)->attr.class_pointer)
   17427              :         {
   17428            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17429              :                      "may not have the POINTER attribute", me_arg->name,
   17430              :                      c->name, me_arg->name, &c->loc);
   17431            1 :           c->tb->error = 1;
   17432            1 :           return false;
   17433              :         }
   17434              : 
   17435           88 :       if (CLASS_DATA (me_arg)->attr.allocatable)
   17436              :         {
   17437            1 :           gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
   17438              :                      "may not be ALLOCATABLE", me_arg->name, c->name,
   17439              :                      me_arg->name, &c->loc);
   17440            1 :           c->tb->error = 1;
   17441            1 :           return false;
   17442              :         }
   17443              : 
   17444           87 :       if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
   17445              :         {
   17446            2 :           gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
   17447              :                      " at %L", c->name, &c->loc);
   17448            2 :           return false;
   17449              :         }
   17450              : 
   17451              :     }
   17452              : 
   17453              :   /* Check type-spec if this is not the parent-type component.  */
   17454       278283 :   if (((sym->attr.is_class
   17455        12380 :         && (!sym->components->ts.u.derived->attr.extension
   17456         2385 :             || c != CLASS_DATA (sym->components)))
   17457       267239 :        || (!sym->attr.is_class
   17458       265903 :            && (!sym->attr.extension || c != sym->components)))
   17459       270190 :       && !sym->attr.vtype
   17460       440538 :       && !resolve_typespec_used (&c->ts, &c->loc, c->name))
   17461              :     return false;
   17462              : 
   17463       278282 :   super_type = gfc_get_derived_super_type (sym);
   17464              : 
   17465              :   /* If this type is an extension, set the accessibility of the parent
   17466              :      component.  */
   17467       278282 :   if (super_type
   17468        25532 :       && ((sym->attr.is_class
   17469        12380 :            && c == CLASS_DATA (sym->components))
   17470        16920 :           || (!sym->attr.is_class && c == sym->components))
   17471        15369 :       && strcmp (super_type->name, c->name) == 0)
   17472         6595 :     c->attr.access = super_type->attr.access;
   17473              : 
   17474              :   /* If this type is an extension, see if this component has the same name
   17475              :      as an inherited type-bound procedure.  */
   17476        25532 :   if (super_type && !sym->attr.is_class
   17477        13152 :       && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
   17478              :     {
   17479            1 :       gfc_error ("Component %qs of %qs at %L has the same name as an"
   17480              :                  " inherited type-bound procedure",
   17481              :                  c->name, sym->name, &c->loc);
   17482            1 :       return false;
   17483              :     }
   17484              : 
   17485       278281 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
   17486         9410 :       && !c->ts.deferred)
   17487              :     {
   17488         7184 :       if (sym->attr.pdt_template || c->attr.pdt_string)
   17489          258 :         gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
   17490              : 
   17491         7184 :       if (c->ts.u.cl->length == NULL
   17492         7178 :           || !resolve_charlen(c->ts.u.cl)
   17493        14361 :           || !gfc_is_constant_expr (c->ts.u.cl->length))
   17494              :         {
   17495            9 :           gfc_error ("Character length of component %qs needs to "
   17496              :                      "be a constant specification expression at %L",
   17497              :                      c->name,
   17498            9 :                      c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
   17499            9 :           return false;
   17500              :         }
   17501              : 
   17502         7175 :      if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
   17503              :         {
   17504            2 :          if (!c->ts.u.cl->length->error)
   17505              :            {
   17506            1 :              gfc_error ("Character length expression of component %qs at %L "
   17507              :                         "must be of INTEGER type, found %s",
   17508            1 :                         c->name, &c->ts.u.cl->length->where,
   17509              :                         gfc_basic_typename (c->ts.u.cl->length->ts.type));
   17510            1 :              c->ts.u.cl->length->error = 1;
   17511              :            }
   17512            2 :          return false;
   17513              :        }
   17514              :     }
   17515              : 
   17516       278270 :   if (c->ts.type == BT_CHARACTER && c->ts.deferred
   17517         2262 :       && !c->attr.pointer && !c->attr.allocatable)
   17518              :     {
   17519            1 :       gfc_error ("Character component %qs of %qs at %L with deferred "
   17520              :                  "length must be a POINTER or ALLOCATABLE",
   17521              :                  c->name, sym->name, &c->loc);
   17522            1 :       return false;
   17523              :     }
   17524              : 
   17525              :   /* Add the hidden deferred length field.  */
   17526       278269 :   if (c->ts.type == BT_CHARACTER
   17527         9910 :       && (c->ts.deferred || c->attr.pdt_string)
   17528         2438 :       && !c->attr.function
   17529         2402 :       && !sym->attr.is_class)
   17530              :     {
   17531         2255 :       char name[GFC_MAX_SYMBOL_LEN+9];
   17532         2255 :       gfc_component *strlen;
   17533         2255 :       sprintf (name, "_%s_length", c->name);
   17534         2255 :       strlen = gfc_find_component (sym, name, true, true, NULL);
   17535         2255 :       if (strlen == NULL)
   17536              :         {
   17537          479 :           if (!gfc_add_component (sym, name, &strlen))
   17538            0 :             return false;
   17539          479 :           strlen->ts.type = BT_INTEGER;
   17540          479 :           strlen->ts.kind = gfc_charlen_int_kind;
   17541          479 :           strlen->attr.access = ACCESS_PRIVATE;
   17542          479 :           strlen->attr.artificial = 1;
   17543              :         }
   17544              :     }
   17545              : 
   17546       278269 :   if (c->ts.type == BT_DERIVED
   17547        51867 :       && sym->component_access != ACCESS_PRIVATE
   17548        50847 :       && gfc_check_symbol_access (sym)
   17549        99658 :       && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
   17550        49777 :       && !c->ts.u.derived->attr.use_assoc
   17551        26705 :       && !gfc_check_symbol_access (c->ts.u.derived)
   17552       278465 :       && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
   17553              :                           "PRIVATE type and cannot be a component of "
   17554              :                           "%qs, which is PUBLIC at %L", c->name,
   17555              :                           sym->name, &sym->declared_at))
   17556              :     return false;
   17557              : 
   17558       278268 :   if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
   17559              :     {
   17560            2 :       gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
   17561              :                  "type %s", c->name, &c->loc, sym->name);
   17562            2 :       return false;
   17563              :     }
   17564              : 
   17565       278266 :   if (sym->attr.sequence)
   17566              :     {
   17567         2506 :       if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
   17568              :         {
   17569            0 :           gfc_error ("Component %s of SEQUENCE type declared at %L does "
   17570              :                      "not have the SEQUENCE attribute",
   17571              :                      c->ts.u.derived->name, &sym->declared_at);
   17572            0 :           return false;
   17573              :         }
   17574              :     }
   17575              : 
   17576       278266 :   if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
   17577            0 :     c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
   17578       278266 :   else if (c->ts.type == BT_CLASS && c->attr.class_ok
   17579         7248 :            && CLASS_DATA (c)->ts.u.derived->attr.generic)
   17580            0 :     CLASS_DATA (c)->ts.u.derived
   17581            0 :                 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
   17582              : 
   17583              :   /* If an allocatable component derived type is of the same type as
   17584              :      the enclosing derived type, we need a vtable generating so that
   17585              :      the __deallocate procedure is created.  */
   17586       278266 :   if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
   17587        59125 :        && c->ts.u.derived == sym && c->attr.allocatable == 1)
   17588          399 :     gfc_find_vtab (&c->ts);
   17589              : 
   17590              :   /* Ensure that all the derived type components are put on the
   17591              :      derived type list; even in formal namespaces, where derived type
   17592              :      pointer components might not have been declared.  */
   17593       278266 :   if (c->ts.type == BT_DERIVED
   17594        51866 :       && c->ts.u.derived
   17595        51866 :       && c->ts.u.derived->components
   17596        48602 :       && c->attr.pointer
   17597        33356 :       && sym != c->ts.u.derived)
   17598         4260 :     add_dt_to_dt_list (c->ts.u.derived);
   17599              : 
   17600       278266 :   if (c->as && c->as->type != AS_DEFERRED
   17601         6297 :       && (c->attr.pointer || c->attr.allocatable))
   17602              :     return false;
   17603              : 
   17604       278252 :   if (!gfc_resolve_array_spec (c->as,
   17605       278252 :                                !(c->attr.pointer || c->attr.proc_pointer
   17606       226502 :                                  || c->attr.allocatable)))
   17607              :     return false;
   17608              : 
   17609       104928 :   if (c->initializer && !sym->attr.vtype
   17610        32036 :       && !c->attr.pdt_kind && !c->attr.pdt_len
   17611       307202 :       && !gfc_check_assign_symbol (sym, c, c->initializer))
   17612              :     return false;
   17613              : 
   17614              :   return true;
   17615              : }
   17616              : 
   17617              : 
   17618              : /* Be nice about the locus for a structure expression - show the locus of the
   17619              :    first non-null sub-expression if we can.  */
   17620              : 
   17621              : static locus *
   17622            4 : cons_where (gfc_expr *struct_expr)
   17623              : {
   17624            4 :   gfc_constructor *cons;
   17625              : 
   17626            4 :   gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
   17627              : 
   17628            4 :   cons = gfc_constructor_first (struct_expr->value.constructor);
   17629           12 :   for (; cons; cons = gfc_constructor_next (cons))
   17630              :     {
   17631            8 :       if (cons->expr && cons->expr->expr_type != EXPR_NULL)
   17632            4 :         return &cons->expr->where;
   17633              :     }
   17634              : 
   17635            0 :   return &struct_expr->where;
   17636              : }
   17637              : 
   17638              : /* Resolve the components of a structure type. Much less work than derived
   17639              :    types.  */
   17640              : 
   17641              : static bool
   17642          913 : resolve_fl_struct (gfc_symbol *sym)
   17643              : {
   17644          913 :   gfc_component *c;
   17645          913 :   gfc_expr *init = NULL;
   17646          913 :   bool success;
   17647              : 
   17648              :   /* Make sure UNIONs do not have overlapping initializers.  */
   17649          913 :   if (sym->attr.flavor == FL_UNION)
   17650              :     {
   17651          498 :       for (c = sym->components; c; c = c->next)
   17652              :         {
   17653          331 :           if (init && c->initializer)
   17654              :             {
   17655            2 :               gfc_error ("Conflicting initializers in union at %L and %L",
   17656              :                          cons_where (init), cons_where (c->initializer));
   17657            2 :               gfc_free_expr (c->initializer);
   17658            2 :               c->initializer = NULL;
   17659              :             }
   17660          291 :           if (init == NULL)
   17661          291 :             init = c->initializer;
   17662              :         }
   17663              :     }
   17664              : 
   17665          913 :   success = true;
   17666         2830 :   for (c = sym->components; c; c = c->next)
   17667         1917 :     if (!resolve_component (c, sym))
   17668            0 :       success = false;
   17669              : 
   17670          913 :   if (!success)
   17671              :     return false;
   17672              : 
   17673          913 :   if (sym->components)
   17674          862 :     add_dt_to_dt_list (sym);
   17675              : 
   17676              :   return true;
   17677              : }
   17678              : 
   17679              : /* Figure if the derived type is using itself directly in one of its components
   17680              :    or through referencing other derived types.  The information is required to
   17681              :    generate the __deallocate and __final type bound procedures to ensure
   17682              :    freeing larger hierarchies of derived types with allocatable objects.  */
   17683              : 
   17684              : static void
   17685       137472 : resolve_cyclic_derived_type (gfc_symbol *derived)
   17686              : {
   17687       137472 :   hash_set<gfc_symbol *> seen, to_examin;
   17688       137472 :   gfc_component *c;
   17689       137472 :   seen.add (derived);
   17690       137472 :   to_examin.add (derived);
   17691       460991 :   while (!to_examin.is_empty ())
   17692              :     {
   17693       188239 :       gfc_symbol *cand = *to_examin.begin ();
   17694       188239 :       to_examin.remove (cand);
   17695       507380 :       for (c = cand->components; c; c = c->next)
   17696       321333 :         if (c->ts.type == BT_DERIVED)
   17697              :           {
   17698        70880 :             if (c->ts.u.derived == derived)
   17699              :               {
   17700         1168 :                 derived->attr.recursive = 1;
   17701         2192 :                 return;
   17702              :               }
   17703        69712 :             else if (!seen.contains (c->ts.u.derived))
   17704              :               {
   17705        46231 :                 seen.add (c->ts.u.derived);
   17706        46231 :                 to_examin.add (c->ts.u.derived);
   17707              :               }
   17708              :           }
   17709       250453 :         else if (c->ts.type == BT_CLASS)
   17710              :           {
   17711         9560 :             if (!c->attr.class_ok)
   17712            7 :               continue;
   17713         9553 :             if (CLASS_DATA (c)->ts.u.derived == derived)
   17714              :               {
   17715         1024 :                 derived->attr.recursive = 1;
   17716         1024 :                 return;
   17717              :               }
   17718         8529 :             else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
   17719              :               {
   17720         4762 :                 seen.add (CLASS_DATA (c)->ts.u.derived);
   17721         4762 :                 to_examin.add (CLASS_DATA (c)->ts.u.derived);
   17722              :               }
   17723              :           }
   17724              :     }
   17725       137472 : }
   17726              : 
   17727              : /* Resolve the components of a derived type. This does not have to wait until
   17728              :    resolution stage, but can be done as soon as the dt declaration has been
   17729              :    parsed.  */
   17730              : 
   17731              : static bool
   17732       169430 : resolve_fl_derived0 (gfc_symbol *sym)
   17733              : {
   17734       169430 :   gfc_symbol* super_type;
   17735       169430 :   gfc_component *c;
   17736       169430 :   gfc_formal_arglist *f;
   17737       169430 :   bool success;
   17738              : 
   17739       169430 :   if (sym->attr.unlimited_polymorphic)
   17740              :     return true;
   17741              : 
   17742       169430 :   super_type = gfc_get_derived_super_type (sym);
   17743              : 
   17744              :   /* F2008, C432.  */
   17745       169430 :   if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
   17746              :     {
   17747            2 :       gfc_error ("As extending type %qs at %L has a coarray component, "
   17748              :                  "parent type %qs shall also have one", sym->name,
   17749              :                  &sym->declared_at, super_type->name);
   17750            2 :       return false;
   17751              :     }
   17752              : 
   17753              :   /* Ensure the extended type gets resolved before we do.  */
   17754        17403 :   if (super_type && !resolve_fl_derived0 (super_type))
   17755              :     return false;
   17756              : 
   17757              :   /* An ABSTRACT type must be extensible.  */
   17758       169422 :   if (sym->attr.abstract && !gfc_type_is_extensible (sym))
   17759              :     {
   17760            2 :       gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
   17761              :                  sym->name, &sym->declared_at);
   17762            2 :       return false;
   17763              :     }
   17764              : 
   17765              :   /* Resolving components below, may create vtabs for which the cyclic type
   17766              :      information needs to be present.  */
   17767       169420 :   if (!sym->attr.vtype)
   17768       137472 :     resolve_cyclic_derived_type (sym);
   17769              : 
   17770       169420 :   c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
   17771              :                            : sym->components;
   17772              : 
   17773              :   success = true;
   17774       575127 :   for ( ; c != NULL; c = c->next)
   17775       405707 :     if (!resolve_component (c, sym))
   17776           96 :       success = false;
   17777              : 
   17778       169420 :   if (!success)
   17779              :     return false;
   17780              : 
   17781              :   /* Now add the caf token field, where needed.  */
   17782       169334 :   if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
   17783         1000 :       && !sym->attr.vtype)
   17784              :     {
   17785         2238 :       for (c = sym->components; c; c = c->next)
   17786         1441 :         if (!c->attr.dimension && !c->attr.codimension
   17787          795 :             && (c->attr.allocatable || c->attr.pointer))
   17788              :           {
   17789          146 :             char name[GFC_MAX_SYMBOL_LEN+9];
   17790          146 :             gfc_component *token;
   17791          146 :             sprintf (name, "_caf_%s", c->name);
   17792          146 :             token = gfc_find_component (sym, name, true, true, NULL);
   17793          146 :             if (token == NULL)
   17794              :               {
   17795           82 :                 if (!gfc_add_component (sym, name, &token))
   17796            0 :                   return false;
   17797           82 :                 token->ts.type = BT_VOID;
   17798           82 :                 token->ts.kind = gfc_default_integer_kind;
   17799           82 :                 token->attr.access = ACCESS_PRIVATE;
   17800           82 :                 token->attr.artificial = 1;
   17801           82 :                 token->attr.caf_token = 1;
   17802              :               }
   17803          146 :             c->caf_token = token;
   17804              :           }
   17805              :     }
   17806              : 
   17807       169334 :   check_defined_assignments (sym);
   17808              : 
   17809       169334 :   if (!sym->attr.defined_assign_comp && super_type)
   17810        16396 :     sym->attr.defined_assign_comp
   17811        16396 :                         = super_type->attr.defined_assign_comp;
   17812              : 
   17813              :   /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
   17814              :      all DEFERRED bindings are overridden.  */
   17815        17396 :   if (super_type && super_type->attr.abstract && !sym->attr.abstract
   17816         1397 :       && !sym->attr.is_class
   17817         3147 :       && !ensure_not_abstract (sym, super_type))
   17818              :     return false;
   17819              : 
   17820              :   /* Check that there is a component for every PDT parameter.  */
   17821       169328 :   if (sym->attr.pdt_template)
   17822              :     {
   17823         2340 :       for (f = sym->formal; f; f = f->next)
   17824              :         {
   17825         1362 :           if (!f->sym)
   17826            1 :             continue;
   17827         1361 :           c = gfc_find_component (sym, f->sym->name, true, true, NULL);
   17828         1361 :           if (c == NULL)
   17829              :             {
   17830            9 :               gfc_error ("Parameterized type %qs does not have a component "
   17831              :                          "corresponding to parameter %qs at %L", sym->name,
   17832            9 :                          f->sym->name, &sym->declared_at);
   17833            9 :               break;
   17834              :             }
   17835              :         }
   17836              :     }
   17837              : 
   17838              :   /* Add derived type to the derived type list.  */
   17839       169328 :   add_dt_to_dt_list (sym);
   17840              : 
   17841       169328 :   return true;
   17842              : }
   17843              : 
   17844              : /* The following procedure does the full resolution of a derived type,
   17845              :    including resolution of all type-bound procedures (if present). In contrast
   17846              :    to 'resolve_fl_derived0' this can only be done after the module has been
   17847              :    parsed completely.  */
   17848              : 
   17849              : static bool
   17850        88072 : resolve_fl_derived (gfc_symbol *sym)
   17851              : {
   17852        88072 :   gfc_symbol *gen_dt = NULL;
   17853              : 
   17854        88072 :   if (sym->attr.unlimited_polymorphic)
   17855              :     return true;
   17856              : 
   17857        88072 :   if (!sym->attr.is_class)
   17858        75422 :     gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
   17859        56406 :   if (gen_dt && gen_dt->generic && gen_dt->generic->next
   17860         2289 :       && (!gen_dt->generic->sym->attr.use_assoc
   17861         2146 :           || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
   17862        88248 :       && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
   17863              :                           "%qs at %L being the same name as derived "
   17864              :                           "type at %L", sym->name,
   17865              :                           gen_dt->generic->sym == sym
   17866           11 :                           ? gen_dt->generic->next->sym->name
   17867              :                           : gen_dt->generic->sym->name,
   17868              :                           gen_dt->generic->sym == sym
   17869           11 :                           ? &gen_dt->generic->next->sym->declared_at
   17870              :                           : &gen_dt->generic->sym->declared_at,
   17871              :                           &sym->declared_at))
   17872              :     return false;
   17873              : 
   17874        88068 :   if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
   17875              :     {
   17876           13 :       gfc_error ("Derived type %qs at %L has not been declared",
   17877              :                   sym->name, &sym->declared_at);
   17878           13 :       return false;
   17879              :     }
   17880              : 
   17881              :   /* Resolve the finalizer procedures.  */
   17882        88055 :   if (!gfc_resolve_finalizers (sym, NULL))
   17883              :     return false;
   17884              : 
   17885        88052 :   if (sym->attr.is_class && sym->ts.u.derived == NULL)
   17886              :     {
   17887              :       /* Fix up incomplete CLASS symbols.  */
   17888        12650 :       gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
   17889        12650 :       gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   17890              : 
   17891        12650 :       if (data->ts.u.derived->attr.pdt_template)
   17892              :         {
   17893            6 :           match m;
   17894            6 :           m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
   17895              :                                     &data->param_list);
   17896            6 :           if (m != MATCH_YES
   17897            6 :               || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
   17898              :             {
   17899            0 :               gfc_error ("Failed to build PDT class component at %L",
   17900              :                          &sym->declared_at);
   17901            0 :               return false;
   17902              :             }
   17903            6 :           data = gfc_find_component (sym, "_data", true, true, NULL);
   17904            6 :           vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
   17905              :         }
   17906              : 
   17907              :       /* Nothing more to do for unlimited polymorphic entities.  */
   17908        12650 :       if (data->ts.u.derived->attr.unlimited_polymorphic)
   17909              :         {
   17910         2005 :           add_dt_to_dt_list (sym);
   17911         2005 :           return true;
   17912              :         }
   17913        10645 :       else if (vptr->ts.u.derived == NULL)
   17914              :         {
   17915         6281 :           gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
   17916         6281 :           gcc_assert (vtab);
   17917         6281 :           vptr->ts.u.derived = vtab->ts.u.derived;
   17918         6281 :           if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
   17919              :             return false;
   17920              :         }
   17921              :     }
   17922              : 
   17923        86047 :   if (!resolve_fl_derived0 (sym))
   17924              :     return false;
   17925              : 
   17926              :   /* Resolve the type-bound procedures.  */
   17927        85963 :   if (!resolve_typebound_procedures (sym))
   17928              :     return false;
   17929              : 
   17930              :   /* Generate module vtables subject to their accessibility and their not
   17931              :      being vtables or pdt templates. If this is not done class declarations
   17932              :      in external procedures wind up with their own version and so SELECT TYPE
   17933              :      fails because the vptrs do not have the same address.  */
   17934        85922 :   if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
   17935        85861 :       && (sym->ns->proc_name->attr.flavor == FL_MODULE
   17936        64403 :           || (sym->attr.recursive && sym->attr.alloc_comp))
   17937        21612 :       && sym->attr.access != ACCESS_PRIVATE
   17938        21579 :       && !(sym->attr.vtype || sym->attr.pdt_template))
   17939              :     {
   17940        19415 :       gfc_symbol *vtab = gfc_find_derived_vtab (sym);
   17941        19415 :       gfc_set_sym_referenced (vtab);
   17942              :     }
   17943              : 
   17944              :   return true;
   17945              : }
   17946              : 
   17947              : 
   17948              : static bool
   17949          855 : resolve_fl_namelist (gfc_symbol *sym)
   17950              : {
   17951          855 :   gfc_namelist *nl;
   17952          855 :   gfc_symbol *nlsym;
   17953              : 
   17954         3024 :   for (nl = sym->namelist; nl; nl = nl->next)
   17955              :     {
   17956              :       /* Check again, the check in match only works if NAMELIST comes
   17957              :          after the decl.  */
   17958         2174 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
   17959              :         {
   17960            1 :           gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
   17961              :                      "allowed", nl->sym->name, sym->name, &sym->declared_at);
   17962            1 :           return false;
   17963              :         }
   17964              : 
   17965          672 :       if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
   17966         2181 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   17967              :                               "with assumed shape in namelist %qs at %L",
   17968              :                               nl->sym->name, sym->name, &sym->declared_at))
   17969              :         return false;
   17970              : 
   17971         2172 :       if (is_non_constant_shape_array (nl->sym)
   17972         2222 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
   17973              :                               "with nonconstant shape in namelist %qs at %L",
   17974           50 :                               nl->sym->name, sym->name, &sym->declared_at))
   17975              :         return false;
   17976              : 
   17977         2171 :       if (nl->sym->ts.type == BT_CHARACTER
   17978          593 :           && (nl->sym->ts.u.cl->length == NULL
   17979          554 :               || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
   17980         2253 :           && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
   17981              :                               "nonconstant character length in "
   17982           82 :                               "namelist %qs at %L", nl->sym->name,
   17983              :                               sym->name, &sym->declared_at))
   17984              :         return false;
   17985              : 
   17986              :     }
   17987              : 
   17988              :   /* Reject PRIVATE objects in a PUBLIC namelist.  */
   17989          850 :   if (gfc_check_symbol_access (sym))
   17990              :     {
   17991         3005 :       for (nl = sym->namelist; nl; nl = nl->next)
   17992              :         {
   17993         2168 :           if (!nl->sym->attr.use_assoc
   17994         4040 :               && !is_sym_host_assoc (nl->sym, sym->ns)
   17995         4166 :               && !gfc_check_symbol_access (nl->sym))
   17996              :             {
   17997            2 :               gfc_error ("NAMELIST object %qs was declared PRIVATE and "
   17998              :                          "cannot be member of PUBLIC namelist %qs at %L",
   17999            2 :                          nl->sym->name, sym->name, &sym->declared_at);
   18000            2 :               return false;
   18001              :             }
   18002              : 
   18003         2166 :           if (nl->sym->ts.type == BT_DERIVED
   18004          466 :              && (nl->sym->ts.u.derived->attr.alloc_comp
   18005          464 :                  || nl->sym->ts.u.derived->attr.pointer_comp))
   18006              :            {
   18007            5 :              if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
   18008              :                                   "namelist %qs at %L with ALLOCATABLE "
   18009              :                                   "or POINTER components", nl->sym->name,
   18010              :                                   sym->name, &sym->declared_at))
   18011              :                return false;
   18012              :              return true;
   18013              :            }
   18014              : 
   18015              :           /* Types with private components that came here by USE-association.  */
   18016         2161 :           if (nl->sym->ts.type == BT_DERIVED
   18017         2161 :               && derived_inaccessible (nl->sym->ts.u.derived))
   18018              :             {
   18019            6 :               gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
   18020              :                          "components and cannot be member of namelist %qs at %L",
   18021              :                          nl->sym->name, sym->name, &sym->declared_at);
   18022            6 :               return false;
   18023              :             }
   18024              : 
   18025              :           /* Types with private components that are defined in the same module.  */
   18026         2155 :           if (nl->sym->ts.type == BT_DERIVED
   18027          910 :               && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
   18028         2433 :               && nl->sym->ts.u.derived->attr.private_comp)
   18029              :             {
   18030            0 :               gfc_error ("NAMELIST object %qs has PRIVATE components and "
   18031              :                          "cannot be a member of PUBLIC namelist %qs at %L",
   18032              :                          nl->sym->name, sym->name, &sym->declared_at);
   18033            0 :               return false;
   18034              :             }
   18035              :         }
   18036              :     }
   18037              : 
   18038              : 
   18039              :   /* 14.1.2 A module or internal procedure represent local entities
   18040              :      of the same type as a namelist member and so are not allowed.  */
   18041         2989 :   for (nl = sym->namelist; nl; nl = nl->next)
   18042              :     {
   18043         2155 :       if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
   18044         1596 :         continue;
   18045              : 
   18046          559 :       if (nl->sym->attr.function && nl->sym == nl->sym->result)
   18047            7 :         if ((nl->sym == sym->ns->proc_name)
   18048            1 :                ||
   18049            1 :             (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
   18050            6 :           continue;
   18051              : 
   18052          553 :       nlsym = NULL;
   18053          553 :       if (nl->sym->name)
   18054          553 :         gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
   18055          553 :       if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
   18056              :         {
   18057            3 :           gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
   18058              :                      "attribute in %qs at %L", nlsym->name,
   18059              :                      &sym->declared_at);
   18060            3 :           return false;
   18061              :         }
   18062              :     }
   18063              : 
   18064              :   return true;
   18065              : }
   18066              : 
   18067              : 
   18068              : static bool
   18069       382255 : resolve_fl_parameter (gfc_symbol *sym)
   18070              : {
   18071              :   /* A parameter array's shape needs to be constant.  */
   18072       382255 :   if (sym->as != NULL
   18073       382255 :       && (sym->as->type == AS_DEFERRED
   18074         6252 :           || is_non_constant_shape_array (sym)))
   18075              :     {
   18076           17 :       gfc_error ("Parameter array %qs at %L cannot be automatic "
   18077              :                  "or of deferred shape", sym->name, &sym->declared_at);
   18078           17 :       return false;
   18079              :     }
   18080              : 
   18081              :   /* Constraints on deferred type parameter.  */
   18082       382238 :   if (!deferred_requirements (sym))
   18083              :     return false;
   18084              : 
   18085              :   /* Make sure a parameter that has been implicitly typed still
   18086              :      matches the implicit type, since PARAMETER statements can precede
   18087              :      IMPLICIT statements.  */
   18088       382237 :   if (sym->attr.implicit_type
   18089       382950 :       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
   18090          713 :                                                              sym->ns)))
   18091              :     {
   18092            0 :       gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
   18093              :                  "later IMPLICIT type", sym->name, &sym->declared_at);
   18094            0 :       return false;
   18095              :     }
   18096              : 
   18097              :   /* Make sure the types of derived parameters are consistent.  This
   18098              :      type checking is deferred until resolution because the type may
   18099              :      refer to a derived type from the host.  */
   18100       382237 :   if (sym->ts.type == BT_DERIVED
   18101       382237 :       && !gfc_compare_types (&sym->ts, &sym->value->ts))
   18102              :     {
   18103            0 :       gfc_error ("Incompatible derived type in PARAMETER at %L",
   18104            0 :                  &sym->value->where);
   18105            0 :       return false;
   18106              :     }
   18107              : 
   18108              :   /* F03:C509,C514.  */
   18109       382237 :   if (sym->ts.type == BT_CLASS)
   18110              :     {
   18111            0 :       gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
   18112              :                  sym->name, &sym->declared_at);
   18113            0 :       return false;
   18114              :     }
   18115              : 
   18116              :   /* Some programmers can have a typo when using an implied-do loop to
   18117              :      initialize an array constant.  For example,
   18118              :        INTEGER I,J
   18119              :        INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)]     ! OK
   18120              :        INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)]  ! Not OK, J undefined
   18121              :      This check catches the typo.  */
   18122       382237 :   if (sym->attr.dimension
   18123         6245 :       && sym->value && sym->value->expr_type == EXPR_ARRAY
   18124       388478 :       && !gfc_is_constant_expr (sym->value))
   18125              :     {
   18126              :       /* PR fortran/117070 argues a nonconstant proc pointer can appear in
   18127              :          the array constructor of a paramater.  This seems inconsistant with
   18128              :          the concept of a parameter. TODO: Needs an interpretation.  */
   18129           20 :       if (sym->value->ts.type == BT_DERIVED
   18130           18 :           && sym->value->ts.u.derived
   18131           18 :           && sym->value->ts.u.derived->attr.proc_pointer_comp)
   18132              :         return true;
   18133            2 :       gfc_error ("Expecting constant expression near %L", &sym->value->where);
   18134            2 :       return false;
   18135              :     }
   18136              : 
   18137              :   return true;
   18138              : }
   18139              : 
   18140              : 
   18141              : /* Called by resolve_symbol to check PDTs.  */
   18142              : 
   18143              : static void
   18144         1377 : resolve_pdt (gfc_symbol* sym)
   18145              : {
   18146         1377 :   gfc_symbol *derived = NULL;
   18147         1377 :   gfc_actual_arglist *param;
   18148         1377 :   gfc_component *c;
   18149         1377 :   bool const_len_exprs = true;
   18150         1377 :   bool assumed_len_exprs = false;
   18151         1377 :   symbol_attribute *attr;
   18152              : 
   18153         1377 :   if (sym->ts.type == BT_DERIVED)
   18154              :     {
   18155         1150 :       derived = sym->ts.u.derived;
   18156         1150 :       attr = &(sym->attr);
   18157              :     }
   18158          227 :   else if (sym->ts.type == BT_CLASS)
   18159              :     {
   18160          227 :       derived = CLASS_DATA (sym)->ts.u.derived;
   18161          227 :       attr = &(CLASS_DATA (sym)->attr);
   18162              :     }
   18163              :   else
   18164            0 :     gcc_unreachable ();
   18165              : 
   18166         1377 :   gcc_assert (derived->attr.pdt_type);
   18167              : 
   18168         3276 :   for (param = sym->param_list; param; param = param->next)
   18169              :     {
   18170         1899 :       c = gfc_find_component (derived, param->name, false, true, NULL);
   18171         1899 :       gcc_assert (c);
   18172         1899 :       if (c->attr.pdt_kind)
   18173         1016 :         continue;
   18174              : 
   18175          614 :       if (param->expr && !gfc_is_constant_expr (param->expr)
   18176          967 :           && c->attr.pdt_len)
   18177              :         const_len_exprs = false;
   18178          799 :       else if (param->spec_type == SPEC_ASSUMED)
   18179          291 :         assumed_len_exprs = true;
   18180              : 
   18181          883 :       if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
   18182           18 :           && ((sym->ts.type == BT_DERIVED && !attr->pointer)
   18183           16 :               || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
   18184            3 :         gfc_error ("Entity %qs at %L has a deferred LEN "
   18185              :                    "parameter %qs and requires either the POINTER "
   18186              :                    "or ALLOCATABLE attribute",
   18187              :                    sym->name, &sym->declared_at,
   18188              :                    param->name);
   18189              : 
   18190              :     }
   18191              : 
   18192         1377 :   if (!const_len_exprs
   18193           84 :       && (sym->ns->proc_name->attr.is_main_program
   18194           83 :           || sym->ns->proc_name->attr.flavor == FL_MODULE
   18195           82 :           || sym->attr.save != SAVE_NONE))
   18196            2 :     gfc_error ("The AUTOMATIC object %qs at %L must not have the "
   18197              :                "SAVE attribute or be a variable declared in the "
   18198              :                "main program, a module or a submodule(F08/C513)",
   18199              :                sym->name, &sym->declared_at);
   18200              : 
   18201         1377 :   if (assumed_len_exprs && !(sym->attr.dummy
   18202            1 :       || sym->attr.select_type_temporary || sym->attr.associate_var))
   18203            1 :     gfc_error ("The object %qs at %L with ASSUMED type parameters "
   18204              :                "must be a dummy or a SELECT TYPE selector(F08/4.2)",
   18205              :                sym->name, &sym->declared_at);
   18206         1377 : }
   18207              : 
   18208              : 
   18209              : /* Resolve the symbol's array spec.  */
   18210              : 
   18211              : static bool
   18212      1693030 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
   18213              : {
   18214      1693030 :   gfc_namespace *orig_current_ns = gfc_current_ns;
   18215      1693030 :   gfc_current_ns = gfc_get_spec_ns (sym);
   18216              : 
   18217      1693030 :   bool saved_specification_expr = specification_expr;
   18218      1693030 :   gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
   18219      1693030 :   specification_expr = true;
   18220      1693030 :   specification_expr_symbol = sym;
   18221              : 
   18222      1693030 :   bool result = gfc_resolve_array_spec (sym->as, check_constant);
   18223              : 
   18224      1693030 :   specification_expr = saved_specification_expr;
   18225      1693030 :   specification_expr_symbol = saved_specification_expr_symbol;
   18226      1693030 :   gfc_current_ns = orig_current_ns;
   18227              : 
   18228      1693030 :   return result;
   18229              : }
   18230              : 
   18231              : 
   18232              : /* Do anything necessary to resolve a symbol.  Right now, we just
   18233              :    assume that an otherwise unknown symbol is a variable.  This sort
   18234              :    of thing commonly happens for symbols in module.  */
   18235              : 
   18236              : static void
   18237      1834586 : resolve_symbol (gfc_symbol *sym)
   18238              : {
   18239      1834586 :   int check_constant, mp_flag;
   18240      1834586 :   gfc_symtree *symtree;
   18241      1834586 :   gfc_symtree *this_symtree;
   18242      1834586 :   gfc_namespace *ns;
   18243      1834586 :   gfc_component *c;
   18244      1834586 :   symbol_attribute class_attr;
   18245      1834586 :   gfc_array_spec *as;
   18246              : 
   18247      1834586 :   if (sym->resolve_symbol_called >= 1)
   18248       172871 :     return;
   18249      1760533 :   sym->resolve_symbol_called = 1;
   18250              : 
   18251              :   /* No symbol will ever have union type; only components can be unions.
   18252              :      Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
   18253              :      (just like derived type declaration symbols have flavor FL_DERIVED). */
   18254      1760533 :   gcc_assert (sym->ts.type != BT_UNION);
   18255              : 
   18256              :   /* Coarrayed polymorphic objects with allocatable or pointer components are
   18257              :      yet unsupported for -fcoarray=lib.  */
   18258      1760533 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
   18259          112 :       && sym->ts.u.derived && CLASS_DATA (sym)
   18260          112 :       && CLASS_DATA (sym)->attr.codimension
   18261           94 :       && CLASS_DATA (sym)->ts.u.derived
   18262           93 :       && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
   18263           90 :           || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
   18264              :     {
   18265            6 :       gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
   18266              :                  "type coarrays at %L are unsupported", &sym->declared_at);
   18267            6 :       return;
   18268              :     }
   18269              : 
   18270      1760527 :   if (sym->attr.artificial)
   18271              :     return;
   18272              : 
   18273      1664406 :   if (sym->attr.unlimited_polymorphic)
   18274              :     return;
   18275              : 
   18276      1662949 :   if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
   18277              :     {
   18278            4 :       gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
   18279              :                  "the OpenMP DEPEND clause", &sym->declared_at);
   18280            4 :       return;
   18281              :     }
   18282              : 
   18283      1662945 :   if (sym->attr.flavor == FL_UNKNOWN
   18284      1641719 :       || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
   18285       443126 :           && !sym->attr.generic && !sym->attr.external
   18286       180016 :           && sym->attr.if_source == IFSRC_UNKNOWN
   18287        81077 :           && sym->ts.type == BT_UNKNOWN))
   18288              :     {
   18289              :       /* A symbol in a common block might not have been resolved yet properly.
   18290              :          Do not try to find an interface with the same name.  */
   18291        93796 :       if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
   18292        21222 :           && !sym->attr.generic && !sym->attr.external
   18293        21171 :           && sym->attr.in_common)
   18294         2594 :         goto skip_interfaces;
   18295              : 
   18296              :     /* If we find that a flavorless symbol is an interface in one of the
   18297              :        parent namespaces, find its symtree in this namespace, free the
   18298              :        symbol and set the symtree to point to the interface symbol.  */
   18299       130136 :       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
   18300              :         {
   18301        39636 :           symtree = gfc_find_symtree (ns->sym_root, sym->name);
   18302        39636 :           if (symtree && (symtree->n.sym->generic ||
   18303          748 :                           (symtree->n.sym->attr.flavor == FL_PROCEDURE
   18304          658 :                            && sym->ns->construct_entities)))
   18305              :             {
   18306          710 :               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
   18307              :                                                sym->name);
   18308          710 :               if (this_symtree->n.sym == sym)
   18309              :                 {
   18310          702 :                   symtree->n.sym->refs++;
   18311          702 :                   gfc_release_symbol (sym);
   18312          702 :                   this_symtree->n.sym = symtree->n.sym;
   18313          702 :                   return;
   18314              :                 }
   18315              :             }
   18316              :         }
   18317              : 
   18318        90500 : skip_interfaces:
   18319              :       /* Otherwise give it a flavor according to such attributes as
   18320              :          it has.  */
   18321        93094 :       if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
   18322        21041 :           && sym->attr.intrinsic == 0)
   18323        21037 :         sym->attr.flavor = FL_VARIABLE;
   18324        72057 :       else if (sym->attr.flavor == FL_UNKNOWN)
   18325              :         {
   18326           55 :           sym->attr.flavor = FL_PROCEDURE;
   18327           55 :           if (sym->attr.dimension)
   18328            0 :             sym->attr.function = 1;
   18329              :         }
   18330              :     }
   18331              : 
   18332      1662243 :   if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
   18333         2304 :     gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
   18334              : 
   18335         1492 :   if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
   18336      1663735 :       && !resolve_procedure_interface (sym))
   18337              :     return;
   18338              : 
   18339      1662232 :   if (sym->attr.is_protected && !sym->attr.proc_pointer
   18340          130 :       && (sym->attr.procedure || sym->attr.external))
   18341              :     {
   18342            0 :       if (sym->attr.external)
   18343            0 :         gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
   18344              :                    "at %L", &sym->declared_at);
   18345              :       else
   18346            0 :         gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
   18347              :                    "at %L", &sym->declared_at);
   18348              : 
   18349            0 :       return;
   18350              :     }
   18351              : 
   18352              :   /* Ensure that variables of derived or class type having a finalizer are
   18353              :      marked used even when the variable is not used anything else in the scope.
   18354              :      This fixes PR118730.  */
   18355       648395 :   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
   18356       443386 :       && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
   18357      1711732 :       && gfc_may_be_finalized (sym->ts))
   18358         8471 :     gfc_set_sym_referenced (sym);
   18359              : 
   18360      1662232 :   if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
   18361              :     return;
   18362              : 
   18363      1661456 :   else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
   18364      1662219 :            && !resolve_fl_struct (sym))
   18365              :     return;
   18366              : 
   18367              :   /* Symbols that are module procedures with results (functions) have
   18368              :      the types and array specification copied for type checking in
   18369              :      procedures that call them, as well as for saving to a module
   18370              :      file.  These symbols can't stand the scrutiny that their results
   18371              :      can.  */
   18372      1662087 :   mp_flag = (sym->result != NULL && sym->result != sym);
   18373              : 
   18374              :   /* Make sure that the intrinsic is consistent with its internal
   18375              :      representation. This needs to be done before assigning a default
   18376              :      type to avoid spurious warnings.  */
   18377      1628049 :   if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
   18378      1694670 :       && !gfc_resolve_intrinsic (sym, &sym->declared_at))
   18379              :     return;
   18380              : 
   18381              :   /* Resolve associate names.  */
   18382      1662051 :   if (sym->assoc)
   18383         6760 :     resolve_assoc_var (sym, true);
   18384              : 
   18385              :   /* Assign default type to symbols that need one and don't have one.  */
   18386      1662051 :   if (sym->ts.type == BT_UNKNOWN)
   18387              :     {
   18388       400764 :       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
   18389              :         {
   18390        11753 :           gfc_set_default_type (sym, 1, NULL);
   18391              :         }
   18392              : 
   18393       258786 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
   18394        61251 :           && !sym->attr.function && !sym->attr.subroutine
   18395       402383 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
   18396          568 :         gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
   18397              : 
   18398       400764 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18399              :         {
   18400              :           /* The specific case of an external procedure should emit an error
   18401              :              in the case that there is no implicit type.  */
   18402       102179 :           if (!mp_flag)
   18403              :             {
   18404        96180 :               if (!sym->attr.mixed_entry_master)
   18405        96072 :                 gfc_set_default_type (sym, sym->attr.external, NULL);
   18406              :             }
   18407              :           else
   18408              :             {
   18409              :               /* Result may be in another namespace.  */
   18410         5999 :               resolve_symbol (sym->result);
   18411              : 
   18412         5999 :               if (!sym->result->attr.proc_pointer)
   18413              :                 {
   18414         5820 :                   sym->ts = sym->result->ts;
   18415         5820 :                   sym->as = gfc_copy_array_spec (sym->result->as);
   18416         5820 :                   sym->attr.dimension = sym->result->attr.dimension;
   18417         5820 :                   sym->attr.codimension = sym->result->attr.codimension;
   18418         5820 :                   sym->attr.pointer = sym->result->attr.pointer;
   18419         5820 :                   sym->attr.allocatable = sym->result->attr.allocatable;
   18420         5820 :                   sym->attr.contiguous = sym->result->attr.contiguous;
   18421              :                 }
   18422              :             }
   18423              :         }
   18424              :     }
   18425      1261287 :   else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
   18426        31312 :     resolve_symbol_array_spec (sym->result, false);
   18427              : 
   18428              :   /* For a CLASS-valued function with a result variable, affirm that it has
   18429              :      been resolved also when looking at the symbol 'sym'.  */
   18430       432076 :   if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
   18431          720 :     sym->attr.class_ok = sym->result->attr.class_ok;
   18432              : 
   18433      1662051 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
   18434        19354 :       && CLASS_DATA (sym))
   18435              :     {
   18436        19353 :       as = CLASS_DATA (sym)->as;
   18437        19353 :       class_attr = CLASS_DATA (sym)->attr;
   18438        19353 :       class_attr.pointer = class_attr.class_pointer;
   18439              :     }
   18440              :   else
   18441              :     {
   18442      1642698 :       class_attr = sym->attr;
   18443      1642698 :       as = sym->as;
   18444              :     }
   18445              : 
   18446              :   /* F2008, C530.  */
   18447      1662051 :   if (sym->attr.contiguous
   18448         7717 :       && !sym->attr.associate_var
   18449         7716 :       && (!class_attr.dimension
   18450         7713 :           || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
   18451          128 :               && !class_attr.pointer)))
   18452              :     {
   18453            7 :       gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
   18454              :                  "array pointer or an assumed-shape or assumed-rank array",
   18455              :                  sym->name, &sym->declared_at);
   18456            7 :       return;
   18457              :     }
   18458              : 
   18459              :   /* Assumed size arrays and assumed shape arrays must be dummy
   18460              :      arguments.  Array-spec's of implied-shape should have been resolved to
   18461              :      AS_EXPLICIT already.  */
   18462              : 
   18463      1654459 :   if (as)
   18464              :     {
   18465              :       /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
   18466              :          specification expression.  */
   18467       145700 :       if (as->type == AS_IMPLIED_SHAPE)
   18468              :         {
   18469              :           int i;
   18470            1 :           for (i=0; i<as->rank; i++)
   18471              :             {
   18472            1 :               if (as->lower[i] != NULL && as->upper[i] == NULL)
   18473              :                 {
   18474            1 :                   gfc_error ("Bad specification for assumed size array at %L",
   18475              :                              &as->lower[i]->where);
   18476            1 :                   return;
   18477              :                 }
   18478              :             }
   18479            0 :           gcc_unreachable();
   18480              :         }
   18481              : 
   18482       145699 :       if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
   18483       112678 :            || as->type == AS_ASSUMED_SHAPE)
   18484        44655 :           && !sym->attr.dummy && !sym->attr.select_type_temporary
   18485            8 :           && !sym->attr.associate_var)
   18486              :         {
   18487            7 :           if (as->type == AS_ASSUMED_SIZE)
   18488            7 :             gfc_error ("Assumed size array at %L must be a dummy argument",
   18489              :                        &sym->declared_at);
   18490              :           else
   18491            0 :             gfc_error ("Assumed shape array at %L must be a dummy argument",
   18492              :                        &sym->declared_at);
   18493            7 :           return;
   18494              :         }
   18495              :       /* TS 29113, C535a.  */
   18496       145692 :       if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
   18497           60 :           && !sym->attr.select_type_temporary
   18498           60 :           && !(cs_base && cs_base->current
   18499           45 :                && (cs_base->current->op == EXEC_SELECT_RANK
   18500            3 :                    || ((gfc_option.allow_std & GFC_STD_F202Y)
   18501            0 :                         && cs_base->current->op == EXEC_BLOCK))))
   18502              :         {
   18503           18 :           gfc_error ("Assumed-rank array at %L must be a dummy argument",
   18504              :                      &sym->declared_at);
   18505           18 :           return;
   18506              :         }
   18507       145674 :       if (as->type == AS_ASSUMED_RANK
   18508        26256 :           && (sym->attr.codimension || sym->attr.value))
   18509              :         {
   18510            2 :           gfc_error ("Assumed-rank array at %L may not have the VALUE or "
   18511              :                      "CODIMENSION attribute", &sym->declared_at);
   18512            2 :           return;
   18513              :         }
   18514              :     }
   18515              : 
   18516              :   /* Make sure symbols with known intent or optional are really dummy
   18517              :      variable.  Because of ENTRY statement, this has to be deferred
   18518              :      until resolution time.  */
   18519              : 
   18520      1662016 :   if (!sym->attr.dummy
   18521      1195369 :       && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
   18522              :     {
   18523            2 :       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
   18524            2 :       return;
   18525              :     }
   18526              : 
   18527      1662014 :   if (sym->attr.value && !sym->attr.dummy)
   18528              :     {
   18529            2 :       gfc_error ("%qs at %L cannot have the VALUE attribute because "
   18530              :                  "it is not a dummy argument", sym->name, &sym->declared_at);
   18531            2 :       return;
   18532              :     }
   18533              : 
   18534      1662012 :   if (sym->attr.value && sym->ts.type == BT_CHARACTER)
   18535              :     {
   18536          616 :       gfc_charlen *cl = sym->ts.u.cl;
   18537          616 :       if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
   18538              :         {
   18539            2 :           gfc_error ("Character dummy variable %qs at %L with VALUE "
   18540              :                      "attribute must have constant length",
   18541              :                      sym->name, &sym->declared_at);
   18542            2 :           return;
   18543              :         }
   18544              : 
   18545          614 :       if (sym->ts.is_c_interop
   18546          381 :           && mpz_cmp_si (cl->length->value.integer, 1) != 0)
   18547              :         {
   18548            1 :           gfc_error ("C interoperable character dummy variable %qs at %L "
   18549              :                      "with VALUE attribute must have length one",
   18550              :                      sym->name, &sym->declared_at);
   18551            1 :           return;
   18552              :         }
   18553              :     }
   18554              : 
   18555      1662009 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18556       123157 :       && sym->ts.u.derived->attr.generic)
   18557              :     {
   18558           20 :       sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
   18559           20 :       if (!sym->ts.u.derived)
   18560              :         {
   18561            0 :           gfc_error ("The derived type %qs at %L is of type %qs, "
   18562              :                      "which has not been defined", sym->name,
   18563              :                      &sym->declared_at, sym->ts.u.derived->name);
   18564            0 :           sym->ts.type = BT_UNKNOWN;
   18565            0 :           return;
   18566              :         }
   18567              :     }
   18568              : 
   18569              :     /* Use the same constraints as TYPE(*), except for the type check
   18570              :        and that only scalars and assumed-size arrays are permitted.  */
   18571      1662009 :     if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
   18572              :       {
   18573        12960 :         if (!sym->attr.dummy)
   18574              :           {
   18575            1 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18576              :                        "a dummy argument", sym->name, &sym->declared_at);
   18577            1 :             return;
   18578              :           }
   18579              : 
   18580        12959 :         if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
   18581            8 :             && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
   18582            0 :             && sym->ts.type != BT_COMPLEX)
   18583              :           {
   18584            0 :             gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
   18585              :                        "of type TYPE(*) or of an numeric intrinsic type",
   18586              :                        sym->name, &sym->declared_at);
   18587            0 :             return;
   18588              :           }
   18589              : 
   18590        12959 :       if (sym->attr.allocatable || sym->attr.codimension
   18591        12957 :           || sym->attr.pointer || sym->attr.value)
   18592              :         {
   18593            4 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18594              :                      "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
   18595              :                      "attribute", sym->name, &sym->declared_at);
   18596            4 :           return;
   18597              :         }
   18598              : 
   18599        12955 :       if (sym->attr.intent == INTENT_OUT)
   18600              :         {
   18601            0 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
   18602              :                      "have the INTENT(OUT) attribute",
   18603              :                      sym->name, &sym->declared_at);
   18604            0 :           return;
   18605              :         }
   18606        12955 :       if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
   18607              :         {
   18608            1 :           gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
   18609              :                      "either be a scalar or an assumed-size array",
   18610              :                      sym->name, &sym->declared_at);
   18611            1 :           return;
   18612              :         }
   18613              : 
   18614              :       /* Set the type to TYPE(*) and add a dimension(*) to ensure
   18615              :          NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
   18616              :          packing.  */
   18617        12954 :       sym->ts.type = BT_ASSUMED;
   18618        12954 :       sym->as = gfc_get_array_spec ();
   18619        12954 :       sym->as->type = AS_ASSUMED_SIZE;
   18620        12954 :       sym->as->rank = 1;
   18621        12954 :       sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
   18622              :     }
   18623      1649049 :   else if (sym->ts.type == BT_ASSUMED)
   18624              :     {
   18625              :       /* TS 29113, C407a.  */
   18626        11006 :       if (!sym->attr.dummy)
   18627              :         {
   18628            7 :           gfc_error ("Assumed type of variable %s at %L is only permitted "
   18629              :                      "for dummy variables", sym->name, &sym->declared_at);
   18630            7 :           return;
   18631              :         }
   18632        10999 :       if (sym->attr.allocatable || sym->attr.codimension
   18633        10995 :           || sym->attr.pointer || sym->attr.value)
   18634              :         {
   18635            8 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18636              :                      "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
   18637              :                      sym->name, &sym->declared_at);
   18638            8 :           return;
   18639              :         }
   18640        10991 :       if (sym->attr.intent == INTENT_OUT)
   18641              :         {
   18642            2 :           gfc_error ("Assumed-type variable %s at %L may not have the "
   18643              :                      "INTENT(OUT) attribute",
   18644              :                      sym->name, &sym->declared_at);
   18645            2 :           return;
   18646              :         }
   18647        10989 :       if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
   18648              :         {
   18649            3 :           gfc_error ("Assumed-type variable %s at %L shall not be an "
   18650              :                      "explicit-shape array", sym->name, &sym->declared_at);
   18651            3 :           return;
   18652              :         }
   18653              :     }
   18654              : 
   18655              :   /* If the symbol is marked as bind(c), that it is declared at module level
   18656              :      scope and verify its type and kind.  Do not do the latter for symbols
   18657              :      that are implicitly typed because that is handled in
   18658              :      gfc_set_default_type.  Handle dummy arguments and procedure definitions
   18659              :      separately.  Also, anything that is use associated is not handled here
   18660              :      but instead is handled in the module it is declared in.  Finally, derived
   18661              :      type definitions are allowed to be BIND(C) since that only implies that
   18662              :      they're interoperable, and they are checked fully for interoperability
   18663              :      when a variable is declared of that type.  */
   18664      1661983 :   if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
   18665         7282 :       && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
   18666          567 :       && sym->attr.flavor != FL_DERIVED)
   18667              :     {
   18668          167 :       bool t = true;
   18669              : 
   18670              :       /* First, make sure the variable is declared at the
   18671              :          module-level scope (J3/04-007, Section 15.3).  */
   18672          167 :       if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
   18673            7 :           && !sym->attr.in_common)
   18674              :         {
   18675            6 :           gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
   18676              :                      "is neither a COMMON block nor declared at the "
   18677              :                      "module level scope", sym->name, &(sym->declared_at));
   18678            6 :           t = false;
   18679              :         }
   18680          161 :       else if (sym->ts.type == BT_CHARACTER
   18681          161 :                && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
   18682            1 :                    || !gfc_is_constant_expr (sym->ts.u.cl->length)
   18683            1 :                    || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
   18684              :         {
   18685            1 :           gfc_error ("BIND(C) Variable %qs at %L must have length one",
   18686            1 :                      sym->name, &sym->declared_at);
   18687            1 :           t = false;
   18688              :         }
   18689          160 :       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
   18690              :         {
   18691            1 :           t = verify_com_block_vars_c_interop (sym->common_head);
   18692              :         }
   18693          159 :       else if (sym->attr.implicit_type == 0)
   18694              :         {
   18695              :           /* If type() declaration, we need to verify that the components
   18696              :              of the given type are all C interoperable, etc.  */
   18697          157 :           if (sym->ts.type == BT_DERIVED &&
   18698           24 :               sym->ts.u.derived->attr.is_c_interop != 1)
   18699              :             {
   18700              :               /* Make sure the user marked the derived type as BIND(C).  If
   18701              :                  not, call the verify routine.  This could print an error
   18702              :                  for the derived type more than once if multiple variables
   18703              :                  of that type are declared.  */
   18704           14 :               if (sym->ts.u.derived->attr.is_bind_c != 1)
   18705            1 :                 verify_bind_c_derived_type (sym->ts.u.derived);
   18706          157 :               t = false;
   18707              :             }
   18708              : 
   18709              :           /* Verify the variable itself as C interoperable if it
   18710              :              is BIND(C).  It is not possible for this to succeed if
   18711              :              the verify_bind_c_derived_type failed, so don't have to handle
   18712              :              any error returned by verify_bind_c_derived_type.  */
   18713          157 :           t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
   18714          157 :                                  sym->common_block);
   18715              :         }
   18716              : 
   18717          165 :       if (!t)
   18718              :         {
   18719              :           /* clear the is_bind_c flag to prevent reporting errors more than
   18720              :              once if something failed.  */
   18721           10 :           sym->attr.is_bind_c = 0;
   18722           10 :           return;
   18723              :         }
   18724              :     }
   18725              : 
   18726              :   /* If a derived type symbol has reached this point, without its
   18727              :      type being declared, we have an error.  Notice that most
   18728              :      conditions that produce undefined derived types have already
   18729              :      been dealt with.  However, the likes of:
   18730              :      implicit type(t) (t) ..... call foo (t) will get us here if
   18731              :      the type is not declared in the scope of the implicit
   18732              :      statement. Change the type to BT_UNKNOWN, both because it is so
   18733              :      and to prevent an ICE.  */
   18734      1661973 :   if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
   18735       123155 :       && sym->ts.u.derived->components == NULL
   18736         1138 :       && !sym->ts.u.derived->attr.zero_comp)
   18737              :     {
   18738            3 :       gfc_error ("The derived type %qs at %L is of type %qs, "
   18739              :                  "which has not been defined", sym->name,
   18740              :                   &sym->declared_at, sym->ts.u.derived->name);
   18741            3 :       sym->ts.type = BT_UNKNOWN;
   18742            3 :       return;
   18743              :     }
   18744              : 
   18745              :   /* Make sure that the derived type has been resolved and that the
   18746              :      derived type is visible in the symbol's namespace, if it is a
   18747              :      module function and is not PRIVATE.  */
   18748      1661970 :   if (sym->ts.type == BT_DERIVED
   18749       130104 :         && sym->ts.u.derived->attr.use_assoc
   18750       112706 :         && sym->ns->proc_name
   18751       112698 :         && sym->ns->proc_name->attr.flavor == FL_MODULE
   18752      1667849 :         && !resolve_fl_derived (sym->ts.u.derived))
   18753              :     return;
   18754              : 
   18755              :   /* Unless the derived-type declaration is use associated, Fortran 95
   18756              :      does not allow public entries of private derived types.
   18757              :      See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
   18758              :      161 in 95-006r3.  */
   18759      1661970 :   if (sym->ts.type == BT_DERIVED
   18760       130104 :       && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
   18761         7962 :       && !sym->ts.u.derived->attr.use_assoc
   18762         2083 :       && gfc_check_symbol_access (sym)
   18763         1870 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   18764      1661984 :       && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
   18765              :                           "derived type %qs",
   18766           14 :                           (sym->attr.flavor == FL_PARAMETER)
   18767              :                           ? "parameter" : "variable",
   18768              :                           sym->name, &sym->declared_at,
   18769           14 :                           sym->ts.u.derived->name))
   18770              :     return;
   18771              : 
   18772              :   /* F2008, C1302.  */
   18773      1661963 :   if (sym->ts.type == BT_DERIVED
   18774       130097 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   18775          154 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
   18776       130066 :           || sym->ts.u.derived->attr.lock_comp)
   18777           44 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   18778              :     {
   18779            4 :       gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
   18780              :                  "type LOCK_TYPE must be a coarray", sym->name,
   18781              :                  &sym->declared_at);
   18782            4 :       return;
   18783              :     }
   18784              : 
   18785              :   /* TS18508, C702/C703.  */
   18786      1661959 :   if (sym->ts.type == BT_DERIVED
   18787       130093 :       && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
   18788          153 :            && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
   18789       130076 :           || sym->ts.u.derived->attr.event_comp)
   18790           17 :       && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
   18791              :     {
   18792            1 :       gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
   18793              :                  "type EVENT_TYPE must be a coarray", sym->name,
   18794              :                  &sym->declared_at);
   18795            1 :       return;
   18796              :     }
   18797              : 
   18798              :   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
   18799              :      default initialization is defined (5.1.2.4.4).  */
   18800      1661958 :   if (sym->ts.type == BT_DERIVED
   18801       130092 :       && sym->attr.dummy
   18802        44866 :       && sym->attr.intent == INTENT_OUT
   18803         2356 :       && sym->as
   18804          381 :       && sym->as->type == AS_ASSUMED_SIZE)
   18805              :     {
   18806            1 :       for (c = sym->ts.u.derived->components; c; c = c->next)
   18807              :         {
   18808            1 :           if (c->initializer)
   18809              :             {
   18810            1 :               gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
   18811              :                          "ASSUMED SIZE and so cannot have a default initializer",
   18812              :                          sym->name, &sym->declared_at);
   18813            1 :               return;
   18814              :             }
   18815              :         }
   18816              :     }
   18817              : 
   18818              :   /* F2008, C542.  */
   18819      1661957 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   18820        44865 :       && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
   18821              :     {
   18822            0 :       gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
   18823              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   18824            0 :       return;
   18825              :     }
   18826              : 
   18827              :   /* TS18508.  */
   18828      1661957 :   if (sym->ts.type == BT_DERIVED && sym->attr.dummy
   18829        44865 :       && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
   18830              :     {
   18831            0 :       gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
   18832              :                  "INTENT(OUT)", sym->name, &sym->declared_at);
   18833            0 :       return;
   18834              :     }
   18835              : 
   18836              :   /* F2008, C525.  */
   18837      1661957 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18838      1661857 :          || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18839        19357 :              && sym->ts.u.derived && CLASS_DATA (sym)
   18840        19351 :              && CLASS_DATA (sym)->attr.coarray_comp))
   18841      1661857 :        || class_attr.codimension)
   18842         1795 :       && (sym->attr.result || sym->result == sym))
   18843              :     {
   18844            8 :       gfc_error ("Function result %qs at %L shall not be a coarray or have "
   18845              :                  "a coarray component", sym->name, &sym->declared_at);
   18846            8 :       return;
   18847              :     }
   18848              : 
   18849              :   /* F2008, C524.  */
   18850      1661949 :   if (sym->attr.codimension && sym->ts.type == BT_DERIVED
   18851          420 :       && sym->ts.u.derived->ts.is_iso_c)
   18852              :     {
   18853            3 :       gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
   18854              :                  "shall not be a coarray", sym->name, &sym->declared_at);
   18855            3 :       return;
   18856              :     }
   18857              : 
   18858              :   /* F2008, C525.  */
   18859      1661946 :   if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18860      1661849 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18861        19356 :             && sym->ts.u.derived && CLASS_DATA (sym)
   18862        19350 :             && CLASS_DATA (sym)->attr.coarray_comp))
   18863           97 :       && (class_attr.codimension || class_attr.pointer || class_attr.dimension
   18864           93 :           || class_attr.allocatable))
   18865              :     {
   18866            4 :       gfc_error ("Variable %qs at %L with coarray component shall be a "
   18867              :                  "nonpointer, nonallocatable scalar, which is not a coarray",
   18868              :                  sym->name, &sym->declared_at);
   18869            4 :       return;
   18870              :     }
   18871              : 
   18872              :   /* F2008, C526.  The function-result case was handled above.  */
   18873      1661942 :   if (class_attr.codimension
   18874         1687 :       && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
   18875          349 :            || sym->attr.select_type_temporary
   18876          273 :            || sym->attr.associate_var
   18877          255 :            || (sym->ns->save_all && !sym->attr.automatic)
   18878          255 :            || sym->ns->proc_name->attr.flavor == FL_MODULE
   18879          255 :            || sym->ns->proc_name->attr.is_main_program
   18880            5 :            || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
   18881              :     {
   18882            4 :       gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
   18883              :                  "nor a dummy argument", sym->name, &sym->declared_at);
   18884            4 :       return;
   18885              :     }
   18886              :   /* F2008, C528.  */
   18887      1661938 :   else if (class_attr.codimension && !sym->attr.select_type_temporary
   18888         1607 :            && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
   18889              :     {
   18890            6 :       gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
   18891              :                  "deferred shape without allocatable", sym->name,
   18892              :                  &sym->declared_at);
   18893            6 :       return;
   18894              :     }
   18895      1661932 :   else if (class_attr.codimension && class_attr.allocatable && as
   18896          614 :            && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
   18897              :     {
   18898            9 :       gfc_error ("Allocatable coarray variable %qs at %L must have "
   18899              :                  "deferred shape", sym->name, &sym->declared_at);
   18900            9 :       return;
   18901              :     }
   18902              : 
   18903              :   /* F2008, C541.  */
   18904      1661923 :   if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
   18905      1661830 :         || (sym->ts.type == BT_CLASS && sym->attr.class_ok
   18906        19351 :             && sym->ts.u.derived && CLASS_DATA (sym)
   18907        19345 :             && CLASS_DATA (sym)->attr.coarray_comp))
   18908      1661830 :        || (class_attr.codimension && class_attr.allocatable))
   18909          698 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
   18910              :     {
   18911            3 :       gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
   18912              :                  "allocatable coarray or have coarray components",
   18913              :                  sym->name, &sym->declared_at);
   18914            3 :       return;
   18915              :     }
   18916              : 
   18917      1661920 :   if (class_attr.codimension && sym->attr.dummy
   18918          469 :       && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
   18919              :     {
   18920            2 :       gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
   18921              :                  "procedure %qs", sym->name, &sym->declared_at,
   18922              :                  sym->ns->proc_name->name);
   18923            2 :       return;
   18924              :     }
   18925              : 
   18926      1661918 :   if (sym->ts.type == BT_LOGICAL
   18927       112177 :       && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
   18928       112174 :           || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
   18929        30990 :               && sym->ns->proc_name->attr.is_bind_c)))
   18930              :     {
   18931              :       int i;
   18932          200 :       for (i = 0; gfc_logical_kinds[i].kind; i++)
   18933          200 :         if (gfc_logical_kinds[i].kind == sym->ts.kind)
   18934              :           break;
   18935           16 :       if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
   18936          181 :           && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
   18937              :                               "%L with non-C_Bool kind in BIND(C) procedure "
   18938              :                               "%qs", sym->name, &sym->declared_at,
   18939           13 :                               sym->ns->proc_name->name))
   18940              :         return;
   18941          167 :       else if (!gfc_logical_kinds[i].c_bool
   18942          182 :                && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
   18943              :                                    "%qs at %L with non-C_Bool kind in "
   18944              :                                    "BIND(C) procedure %qs", sym->name,
   18945              :                                    &sym->declared_at,
   18946           15 :                                    sym->attr.function ? sym->name
   18947           13 :                                    : sym->ns->proc_name->name))
   18948              :         return;
   18949              :     }
   18950              : 
   18951      1661915 :   switch (sym->attr.flavor)
   18952              :     {
   18953       648278 :     case FL_VARIABLE:
   18954       648278 :       if (!resolve_fl_variable (sym, mp_flag))
   18955              :         return;
   18956              :       break;
   18957              : 
   18958       475131 :     case FL_PROCEDURE:
   18959       475131 :       if (sym->formal && !sym->formal_ns)
   18960              :         {
   18961              :           /* Check that none of the arguments are a namelist.  */
   18962              :           gfc_formal_arglist *formal = sym->formal;
   18963              : 
   18964       105260 :           for (; formal; formal = formal->next)
   18965        71459 :             if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
   18966              :               {
   18967            1 :                 gfc_error ("Namelist %qs cannot be an argument to "
   18968              :                            "subroutine or function at %L",
   18969              :                            formal->sym->name, &sym->declared_at);
   18970            1 :                 return;
   18971              :               }
   18972              :         }
   18973              : 
   18974       475130 :       if (!resolve_fl_procedure (sym, mp_flag))
   18975              :         return;
   18976              :       break;
   18977              : 
   18978          855 :     case FL_NAMELIST:
   18979          855 :       if (!resolve_fl_namelist (sym))
   18980              :         return;
   18981              :       break;
   18982              : 
   18983       382255 :     case FL_PARAMETER:
   18984       382255 :       if (!resolve_fl_parameter (sym))
   18985              :         return;
   18986              :       break;
   18987              : 
   18988              :     default:
   18989              :       break;
   18990              :     }
   18991              : 
   18992              :   /* Resolve array specifier. Check as well some constraints
   18993              :      on COMMON blocks.  */
   18994              : 
   18995      1661718 :   check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
   18996              : 
   18997      1661718 :   resolve_symbol_array_spec (sym, check_constant);
   18998              : 
   18999              :   /* Resolve formal namespaces.  */
   19000      1661718 :   if (sym->formal_ns && sym->formal_ns != gfc_current_ns
   19001       259978 :       && !sym->attr.contained && !sym->attr.intrinsic)
   19002       234716 :     gfc_resolve (sym->formal_ns);
   19003              : 
   19004              :   /* Make sure the formal namespace is present.  */
   19005      1661718 :   if (sym->formal && !sym->formal_ns)
   19006              :     {
   19007              :       gfc_formal_arglist *formal = sym->formal;
   19008        34245 :       while (formal && !formal->sym)
   19009           11 :         formal = formal->next;
   19010              : 
   19011        34234 :       if (formal)
   19012              :         {
   19013        34223 :           sym->formal_ns = formal->sym->ns;
   19014        34223 :           if (sym->formal_ns && sym->ns != formal->sym->ns)
   19015        25916 :             sym->formal_ns->refs++;
   19016              :         }
   19017              :     }
   19018              : 
   19019              :   /* Check threadprivate restrictions.  */
   19020      1661718 :   if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
   19021          384 :       && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
   19022           33 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19023           32 :       && sym->module == NULL
   19024           17 :       && (sym->ns->proc_name == NULL
   19025           17 :           || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19026            4 :               && !sym->ns->proc_name->attr.is_main_program)))
   19027              :     {
   19028            2 :       if (sym->attr.threadprivate)
   19029            1 :         gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
   19030              :       else
   19031            1 :         gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
   19032              :                    "attribute", sym->name, &sym->declared_at);
   19033              :     }
   19034              : 
   19035      1661718 :   if (sym->attr.omp_groupprivate && sym->value)
   19036            2 :     gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
   19037              :                "initializer", sym->name, &sym->declared_at);
   19038              : 
   19039              :   /* Check omp declare target restrictions.  */
   19040      1661718 :   if ((sym->attr.omp_declare_target
   19041      1660302 :        || sym->attr.omp_declare_target_link
   19042      1660254 :        || sym->attr.omp_declare_target_local)
   19043         1504 :       && !sym->attr.omp_groupprivate  /* already warned.  */
   19044         1457 :       && sym->attr.flavor == FL_VARIABLE
   19045          616 :       && !sym->attr.save
   19046          199 :       && !(sym->ns->save_all && !sym->attr.automatic)
   19047          199 :       && (!sym->attr.in_common
   19048          186 :           && sym->module == NULL
   19049           96 :           && (sym->ns->proc_name == NULL
   19050           96 :               || (sym->ns->proc_name->attr.flavor != FL_MODULE
   19051            6 :                   && !sym->ns->proc_name->attr.is_main_program))))
   19052            4 :     gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
   19053              :                sym->name, &sym->declared_at);
   19054              : 
   19055              :   /* If we have come this far we can apply default-initializers, as
   19056              :      described in 14.7.5, to those variables that have not already
   19057              :      been assigned one.  */
   19058      1661718 :   if (sym->ts.type == BT_DERIVED
   19059       130062 :       && !sym->value
   19060       105187 :       && !sym->attr.allocatable
   19061       102213 :       && !sym->attr.alloc_comp)
   19062              :     {
   19063       102155 :       symbol_attribute *a = &sym->attr;
   19064              : 
   19065       102155 :       if ((!a->save && !a->dummy && !a->pointer
   19066        55982 :            && !a->in_common && !a->use_assoc
   19067        10327 :            && a->referenced
   19068         8095 :            && !((a->function || a->result)
   19069         1608 :                 && (!a->dimension
   19070          160 :                     || sym->ts.u.derived->attr.alloc_comp
   19071           95 :                     || sym->ts.u.derived->attr.pointer_comp))
   19072         6568 :            && !(a->function && sym != sym->result))
   19073        95607 :           || (a->dummy && !a->pointer && a->intent == INTENT_OUT
   19074         1528 :               && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
   19075         7977 :         apply_default_init (sym);
   19076        94178 :       else if (a->function && !a->pointer && !a->allocatable
   19077        20463 :                && !a->use_assoc && !a->used_in_submodule && sym->result)
   19078              :         /* Default initialization for function results.  */
   19079         2662 :         apply_default_init (sym->result);
   19080        91516 :       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
   19081        11729 :                && (sym->ts.u.derived->attr.alloc_comp
   19082        11206 :                    || sym->ts.u.derived->attr.pointer_comp))
   19083              :         /* Mark the result symbol to be referenced, when it has allocatable
   19084              :            components.  */
   19085          582 :         sym->result->attr.referenced = 1;
   19086              :     }
   19087              : 
   19088      1661718 :   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
   19089        18852 :       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
   19090         1226 :       && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
   19091         1151 :       && !CLASS_DATA (sym)->attr.class_pointer
   19092         1125 :       && !CLASS_DATA (sym)->attr.allocatable)
   19093          853 :     apply_default_init (sym);
   19094              : 
   19095              :   /* If this symbol has a type-spec, check it.  */
   19096      1661718 :   if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
   19097       631295 :       || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
   19098      1348886 :     if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
   19099              :       return;
   19100              : 
   19101      1661715 :   if (sym->param_list)
   19102         1377 :     resolve_pdt (sym);
   19103              : }
   19104              : 
   19105              : 
   19106         3963 : void gfc_resolve_symbol (gfc_symbol *sym)
   19107              : {
   19108         3963 :   resolve_symbol (sym);
   19109         3963 :   return;
   19110              : }
   19111              : 
   19112              : 
   19113              : /************* Resolve DATA statements *************/
   19114              : 
   19115              : static struct
   19116              : {
   19117              :   gfc_data_value *vnode;
   19118              :   mpz_t left;
   19119              : }
   19120              : values;
   19121              : 
   19122              : 
   19123              : /* Advance the values structure to point to the next value in the data list.  */
   19124              : 
   19125              : static bool
   19126        10892 : next_data_value (void)
   19127              : {
   19128        16660 :   while (mpz_cmp_ui (values.left, 0) == 0)
   19129              :     {
   19130              : 
   19131         8198 :       if (values.vnode->next == NULL)
   19132              :         return false;
   19133              : 
   19134         5768 :       values.vnode = values.vnode->next;
   19135         5768 :       mpz_set (values.left, values.vnode->repeat);
   19136              :     }
   19137              : 
   19138              :   return true;
   19139              : }
   19140              : 
   19141              : 
   19142              : static bool
   19143         3557 : check_data_variable (gfc_data_variable *var, locus *where)
   19144              : {
   19145         3557 :   gfc_expr *e;
   19146         3557 :   mpz_t size;
   19147         3557 :   mpz_t offset;
   19148         3557 :   bool t;
   19149         3557 :   ar_type mark = AR_UNKNOWN;
   19150         3557 :   int i;
   19151         3557 :   mpz_t section_index[GFC_MAX_DIMENSIONS];
   19152         3557 :   int vector_offset[GFC_MAX_DIMENSIONS];
   19153         3557 :   gfc_ref *ref;
   19154         3557 :   gfc_array_ref *ar;
   19155         3557 :   gfc_symbol *sym;
   19156         3557 :   int has_pointer;
   19157              : 
   19158         3557 :   if (!gfc_resolve_expr (var->expr))
   19159              :     return false;
   19160              : 
   19161         3557 :   ar = NULL;
   19162         3557 :   e = var->expr;
   19163              : 
   19164         3557 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
   19165            0 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
   19166            0 :     e = e->value.function.actual->expr;
   19167              : 
   19168         3557 :   if (e->expr_type != EXPR_VARIABLE)
   19169              :     {
   19170            0 :       gfc_error ("Expecting definable entity near %L", where);
   19171            0 :       return false;
   19172              :     }
   19173              : 
   19174         3557 :   sym = e->symtree->n.sym;
   19175              : 
   19176         3557 :   if (sym->ns->is_block_data && !sym->attr.in_common)
   19177              :     {
   19178            2 :       gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
   19179              :                  sym->name, &sym->declared_at);
   19180            2 :       return false;
   19181              :     }
   19182              : 
   19183         3555 :   if (e->ref == NULL && sym->as)
   19184              :     {
   19185            1 :       gfc_error ("DATA array %qs at %L must be specified in a previous"
   19186              :                  " declaration", sym->name, where);
   19187            1 :       return false;
   19188              :     }
   19189              : 
   19190         3554 :   if (gfc_is_coindexed (e))
   19191              :     {
   19192            7 :       gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
   19193              :                  where);
   19194            7 :       return false;
   19195              :     }
   19196              : 
   19197         3547 :   has_pointer = sym->attr.pointer;
   19198              : 
   19199         5988 :   for (ref = e->ref; ref; ref = ref->next)
   19200              :     {
   19201         2445 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
   19202              :         has_pointer = 1;
   19203              : 
   19204         2419 :       if (has_pointer)
   19205              :         {
   19206           29 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
   19207              :             {
   19208            1 :               gfc_error ("DATA element %qs at %L is a pointer and so must "
   19209              :                          "be a full array", sym->name, where);
   19210            1 :               return false;
   19211              :             }
   19212              : 
   19213           28 :           if (values.vnode->expr->expr_type == EXPR_CONSTANT)
   19214              :             {
   19215            1 :               gfc_error ("DATA object near %L has the pointer attribute "
   19216              :                          "and the corresponding DATA value is not a valid "
   19217              :                          "initial-data-target", where);
   19218            1 :               return false;
   19219              :             }
   19220              :         }
   19221              : 
   19222         2443 :       if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
   19223              :         {
   19224            1 :           gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
   19225              :                      "attribute", ref->u.c.component->name, &e->where);
   19226            1 :           return false;
   19227              :         }
   19228              : 
   19229              :       /* Reject substrings of strings of non-constant length.  */
   19230         2442 :       if (ref->type == REF_SUBSTRING
   19231           73 :           && ref->u.ss.length
   19232           73 :           && ref->u.ss.length->length
   19233         2515 :           && !gfc_is_constant_expr (ref->u.ss.length->length))
   19234            1 :         goto bad_charlen;
   19235              :     }
   19236              : 
   19237              :   /* Reject strings with deferred length or non-constant length.  */
   19238         3543 :   if (e->ts.type == BT_CHARACTER
   19239         3543 :       && (e->ts.deferred
   19240          374 :           || (e->ts.u.cl->length
   19241          323 :               && !gfc_is_constant_expr (e->ts.u.cl->length))))
   19242            5 :     goto bad_charlen;
   19243              : 
   19244         3538 :   mpz_init_set_si (offset, 0);
   19245              : 
   19246         3538 :   if (e->rank == 0 || has_pointer)
   19247              :     {
   19248         2691 :       mpz_init_set_ui (size, 1);
   19249         2691 :       ref = NULL;
   19250              :     }
   19251              :   else
   19252              :     {
   19253          847 :       ref = e->ref;
   19254              : 
   19255              :       /* Find the array section reference.  */
   19256         1030 :       for (ref = e->ref; ref; ref = ref->next)
   19257              :         {
   19258         1030 :           if (ref->type != REF_ARRAY)
   19259           92 :             continue;
   19260          938 :           if (ref->u.ar.type == AR_ELEMENT)
   19261           91 :             continue;
   19262              :           break;
   19263              :         }
   19264          847 :       gcc_assert (ref);
   19265              : 
   19266              :       /* Set marks according to the reference pattern.  */
   19267          847 :       switch (ref->u.ar.type)
   19268              :         {
   19269              :         case AR_FULL:
   19270              :           mark = AR_FULL;
   19271              :           break;
   19272              : 
   19273          151 :         case AR_SECTION:
   19274          151 :           ar = &ref->u.ar;
   19275              :           /* Get the start position of array section.  */
   19276          151 :           gfc_get_section_index (ar, section_index, &offset, vector_offset);
   19277          151 :           mark = AR_SECTION;
   19278          151 :           break;
   19279              : 
   19280            0 :         default:
   19281            0 :           gcc_unreachable ();
   19282              :         }
   19283              : 
   19284          847 :       if (!gfc_array_size (e, &size))
   19285              :         {
   19286            1 :           gfc_error ("Nonconstant array section at %L in DATA statement",
   19287              :                      where);
   19288            1 :           mpz_clear (offset);
   19289            1 :           return false;
   19290              :         }
   19291              :     }
   19292              : 
   19293         3537 :   t = true;
   19294              : 
   19295        11937 :   while (mpz_cmp_ui (size, 0) > 0)
   19296              :     {
   19297         8463 :       if (!next_data_value ())
   19298              :         {
   19299            1 :           gfc_error ("DATA statement at %L has more variables than values",
   19300              :                      where);
   19301            1 :           t = false;
   19302            1 :           break;
   19303              :         }
   19304              : 
   19305         8462 :       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
   19306         8462 :       if (!t)
   19307              :         break;
   19308              : 
   19309              :       /* If we have more than one element left in the repeat count,
   19310              :          and we have more than one element left in the target variable,
   19311              :          then create a range assignment.  */
   19312              :       /* FIXME: Only done for full arrays for now, since array sections
   19313              :          seem tricky.  */
   19314         8443 :       if (mark == AR_FULL && ref && ref->next == NULL
   19315         5364 :           && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
   19316              :         {
   19317          137 :           mpz_t range;
   19318              : 
   19319          137 :           if (mpz_cmp (size, values.left) >= 0)
   19320              :             {
   19321          126 :               mpz_init_set (range, values.left);
   19322          126 :               mpz_sub (size, size, values.left);
   19323          126 :               mpz_set_ui (values.left, 0);
   19324              :             }
   19325              :           else
   19326              :             {
   19327           11 :               mpz_init_set (range, size);
   19328           11 :               mpz_sub (values.left, values.left, size);
   19329           11 :               mpz_set_ui (size, 0);
   19330              :             }
   19331              : 
   19332          137 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19333              :                                      offset, &range);
   19334              : 
   19335          137 :           mpz_add (offset, offset, range);
   19336          137 :           mpz_clear (range);
   19337              : 
   19338          137 :           if (!t)
   19339              :             break;
   19340          129 :         }
   19341              : 
   19342              :       /* Assign initial value to symbol.  */
   19343              :       else
   19344              :         {
   19345         8306 :           mpz_sub_ui (values.left, values.left, 1);
   19346         8306 :           mpz_sub_ui (size, size, 1);
   19347              : 
   19348         8306 :           t = gfc_assign_data_value (var->expr, values.vnode->expr,
   19349              :                                      offset, NULL);
   19350         8306 :           if (!t)
   19351              :             break;
   19352              : 
   19353         8271 :           if (mark == AR_FULL)
   19354         5259 :             mpz_add_ui (offset, offset, 1);
   19355              : 
   19356              :           /* Modify the array section indexes and recalculate the offset
   19357              :              for next element.  */
   19358         3012 :           else if (mark == AR_SECTION)
   19359          366 :             gfc_advance_section (section_index, ar, &offset, vector_offset);
   19360              :         }
   19361              :     }
   19362              : 
   19363         3537 :   if (mark == AR_SECTION)
   19364              :     {
   19365          344 :       for (i = 0; i < ar->dimen; i++)
   19366          194 :         mpz_clear (section_index[i]);
   19367              :     }
   19368              : 
   19369         3537 :   mpz_clear (size);
   19370         3537 :   mpz_clear (offset);
   19371              : 
   19372         3537 :   return t;
   19373              : 
   19374            6 : bad_charlen:
   19375            6 :   gfc_error ("Non-constant character length at %L in DATA statement",
   19376              :              &e->where);
   19377            6 :   return false;
   19378              : }
   19379              : 
   19380              : 
   19381              : static bool traverse_data_var (gfc_data_variable *, locus *);
   19382              : 
   19383              : /* Iterate over a list of elements in a DATA statement.  */
   19384              : 
   19385              : static bool
   19386          237 : traverse_data_list (gfc_data_variable *var, locus *where)
   19387              : {
   19388          237 :   mpz_t trip;
   19389          237 :   iterator_stack frame;
   19390          237 :   gfc_expr *e, *start, *end, *step;
   19391          237 :   bool retval = true;
   19392              : 
   19393          237 :   mpz_init (frame.value);
   19394          237 :   mpz_init (trip);
   19395              : 
   19396          237 :   start = gfc_copy_expr (var->iter.start);
   19397          237 :   end = gfc_copy_expr (var->iter.end);
   19398          237 :   step = gfc_copy_expr (var->iter.step);
   19399              : 
   19400          237 :   if (!gfc_simplify_expr (start, 1)
   19401          237 :       || start->expr_type != EXPR_CONSTANT)
   19402              :     {
   19403            0 :       gfc_error ("start of implied-do loop at %L could not be "
   19404              :                  "simplified to a constant value", &start->where);
   19405            0 :       retval = false;
   19406            0 :       goto cleanup;
   19407              :     }
   19408          237 :   if (!gfc_simplify_expr (end, 1)
   19409          237 :       || end->expr_type != EXPR_CONSTANT)
   19410              :     {
   19411            0 :       gfc_error ("end of implied-do loop at %L could not be "
   19412              :                  "simplified to a constant value", &end->where);
   19413            0 :       retval = false;
   19414            0 :       goto cleanup;
   19415              :     }
   19416          237 :   if (!gfc_simplify_expr (step, 1)
   19417          237 :       || step->expr_type != EXPR_CONSTANT)
   19418              :     {
   19419            0 :       gfc_error ("step of implied-do loop at %L could not be "
   19420              :                  "simplified to a constant value", &step->where);
   19421            0 :       retval = false;
   19422            0 :       goto cleanup;
   19423              :     }
   19424          237 :   if (mpz_cmp_si (step->value.integer, 0) == 0)
   19425              :     {
   19426            1 :       gfc_error ("step of implied-do loop at %L shall not be zero",
   19427              :                  &step->where);
   19428            1 :       retval = false;
   19429            1 :       goto cleanup;
   19430              :     }
   19431              : 
   19432          236 :   mpz_set (trip, end->value.integer);
   19433          236 :   mpz_sub (trip, trip, start->value.integer);
   19434          236 :   mpz_add (trip, trip, step->value.integer);
   19435              : 
   19436          236 :   mpz_div (trip, trip, step->value.integer);
   19437              : 
   19438          236 :   mpz_set (frame.value, start->value.integer);
   19439              : 
   19440          236 :   frame.prev = iter_stack;
   19441          236 :   frame.variable = var->iter.var->symtree;
   19442          236 :   iter_stack = &frame;
   19443              : 
   19444         1127 :   while (mpz_cmp_ui (trip, 0) > 0)
   19445              :     {
   19446          905 :       if (!traverse_data_var (var->list, where))
   19447              :         {
   19448           14 :           retval = false;
   19449           14 :           goto cleanup;
   19450              :         }
   19451              : 
   19452          891 :       e = gfc_copy_expr (var->expr);
   19453          891 :       if (!gfc_simplify_expr (e, 1))
   19454              :         {
   19455            0 :           gfc_free_expr (e);
   19456            0 :           retval = false;
   19457            0 :           goto cleanup;
   19458              :         }
   19459              : 
   19460          891 :       mpz_add (frame.value, frame.value, step->value.integer);
   19461              : 
   19462          891 :       mpz_sub_ui (trip, trip, 1);
   19463              :     }
   19464              : 
   19465          222 : cleanup:
   19466          237 :   mpz_clear (frame.value);
   19467          237 :   mpz_clear (trip);
   19468              : 
   19469          237 :   gfc_free_expr (start);
   19470          237 :   gfc_free_expr (end);
   19471          237 :   gfc_free_expr (step);
   19472              : 
   19473          237 :   iter_stack = frame.prev;
   19474          237 :   return retval;
   19475              : }
   19476              : 
   19477              : 
   19478              : /* Type resolve variables in the variable list of a DATA statement.  */
   19479              : 
   19480              : static bool
   19481         3418 : traverse_data_var (gfc_data_variable *var, locus *where)
   19482              : {
   19483         3418 :   bool t;
   19484              : 
   19485         7114 :   for (; var; var = var->next)
   19486              :     {
   19487         3794 :       if (var->expr == NULL)
   19488          237 :         t = traverse_data_list (var, where);
   19489              :       else
   19490         3557 :         t = check_data_variable (var, where);
   19491              : 
   19492         3794 :       if (!t)
   19493              :         return false;
   19494              :     }
   19495              : 
   19496              :   return true;
   19497              : }
   19498              : 
   19499              : 
   19500              : /* Resolve the expressions and iterators associated with a data statement.
   19501              :    This is separate from the assignment checking because data lists should
   19502              :    only be resolved once.  */
   19503              : 
   19504              : static bool
   19505         2668 : resolve_data_variables (gfc_data_variable *d)
   19506              : {
   19507         5707 :   for (; d; d = d->next)
   19508              :     {
   19509         3044 :       if (d->list == NULL)
   19510              :         {
   19511         2891 :           if (!gfc_resolve_expr (d->expr))
   19512              :             return false;
   19513              :         }
   19514              :       else
   19515              :         {
   19516          153 :           if (!gfc_resolve_iterator (&d->iter, false, true))
   19517              :             return false;
   19518              : 
   19519          150 :           if (!resolve_data_variables (d->list))
   19520              :             return false;
   19521              :         }
   19522              :     }
   19523              : 
   19524              :   return true;
   19525              : }
   19526              : 
   19527              : 
   19528              : /* Resolve a single DATA statement.  We implement this by storing a pointer to
   19529              :    the value list into static variables, and then recursively traversing the
   19530              :    variables list, expanding iterators and such.  */
   19531              : 
   19532              : static void
   19533         2518 : resolve_data (gfc_data *d)
   19534              : {
   19535              : 
   19536         2518 :   if (!resolve_data_variables (d->var))
   19537              :     return;
   19538              : 
   19539         2513 :   values.vnode = d->value;
   19540         2513 :   if (d->value == NULL)
   19541            0 :     mpz_set_ui (values.left, 0);
   19542              :   else
   19543         2513 :     mpz_set (values.left, d->value->repeat);
   19544              : 
   19545         2513 :   if (!traverse_data_var (d->var, &d->where))
   19546              :     return;
   19547              : 
   19548              :   /* At this point, we better not have any values left.  */
   19549              : 
   19550         2429 :   if (next_data_value ())
   19551            0 :     gfc_error ("DATA statement at %L has more values than variables",
   19552              :                &d->where);
   19553              : }
   19554              : 
   19555              : 
   19556              : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
   19557              :    accessed by host or use association, is a dummy argument to a pure function,
   19558              :    is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
   19559              :    is storage associated with any such variable, shall not be used in the
   19560              :    following contexts: (clients of this function).  */
   19561              : 
   19562              : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
   19563              :    procedure.  Returns zero if assignment is OK, nonzero if there is a
   19564              :    problem.  */
   19565              : bool
   19566        55562 : gfc_impure_variable (gfc_symbol *sym)
   19567              : {
   19568        55562 :   gfc_symbol *proc;
   19569        55562 :   gfc_namespace *ns;
   19570              : 
   19571        55562 :   if (sym->attr.use_assoc || sym->attr.in_common)
   19572              :     return 1;
   19573              : 
   19574              :   /* The namespace of a module procedure interface holds the arguments and
   19575              :      symbols, and so the symbol namespace can be different to that of the
   19576              :      procedure.  */
   19577        54945 :   if (sym->ns != gfc_current_ns
   19578         5860 :       && gfc_current_ns->proc_name->abr_modproc_decl
   19579           48 :       && sym->ns->proc_name->attr.function
   19580           12 :       && sym->attr.result
   19581           12 :       && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
   19582              :     return 0;
   19583              : 
   19584              :   /* Check if the symbol's ns is inside the pure procedure.  */
   19585        59598 :   for (ns = gfc_current_ns; ns; ns = ns->parent)
   19586              :     {
   19587        59307 :       if (ns == sym->ns)
   19588              :         break;
   19589         6166 :       if (ns->proc_name->attr.flavor == FL_PROCEDURE
   19590         5104 :           && !(sym->attr.function || sym->attr.result))
   19591              :         return 1;
   19592              :     }
   19593              : 
   19594        53432 :   proc = sym->ns->proc_name;
   19595        53432 :   if (sym->attr.dummy
   19596         5915 :       && !sym->attr.value
   19597         5793 :       && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
   19598         5590 :           || proc->attr.function))
   19599          691 :     return 1;
   19600              : 
   19601              :   /* TODO: Sort out what can be storage associated, if anything, and include
   19602              :      it here.  In principle equivalences should be scanned but it does not
   19603              :      seem to be possible to storage associate an impure variable this way.  */
   19604              :   return 0;
   19605              : }
   19606              : 
   19607              : 
   19608              : /* Test whether a symbol is pure or not.  For a NULL pointer, checks if the
   19609              :    current namespace is inside a pure procedure.  */
   19610              : 
   19611              : bool
   19612      2310530 : gfc_pure (gfc_symbol *sym)
   19613              : {
   19614      2310530 :   symbol_attribute attr;
   19615      2310530 :   gfc_namespace *ns;
   19616              : 
   19617      2310530 :   if (sym == NULL)
   19618              :     {
   19619              :       /* Check if the current namespace or one of its parents
   19620              :         belongs to a pure procedure.  */
   19621      3171898 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19622              :         {
   19623      1873448 :           sym = ns->proc_name;
   19624      1873448 :           if (sym == NULL)
   19625              :             return 0;
   19626      1872309 :           attr = sym->attr;
   19627      1872309 :           if (attr.flavor == FL_PROCEDURE && attr.pure)
   19628              :             return 1;
   19629              :         }
   19630              :       return 0;
   19631              :     }
   19632              : 
   19633      1003724 :   attr = sym->attr;
   19634              : 
   19635      1003724 :   return attr.flavor == FL_PROCEDURE && attr.pure;
   19636              : }
   19637              : 
   19638              : 
   19639              : /* Test whether a symbol is implicitly pure or not.  For a NULL pointer,
   19640              :    checks if the current namespace is implicitly pure.  Note that this
   19641              :    function returns false for a PURE procedure.  */
   19642              : 
   19643              : bool
   19644       722353 : gfc_implicit_pure (gfc_symbol *sym)
   19645              : {
   19646       722353 :   gfc_namespace *ns;
   19647              : 
   19648       722353 :   if (sym == NULL)
   19649              :     {
   19650              :       /* Check if the current procedure is implicit_pure.  Walk up
   19651              :          the procedure list until we find a procedure.  */
   19652       994911 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19653              :         {
   19654       710253 :           sym = ns->proc_name;
   19655       710253 :           if (sym == NULL)
   19656              :             return 0;
   19657              : 
   19658       710180 :           if (sym->attr.flavor == FL_PROCEDURE)
   19659              :             break;
   19660              :         }
   19661              :     }
   19662              : 
   19663       437619 :   return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
   19664       749370 :     && !sym->attr.pure;
   19665              : }
   19666              : 
   19667              : 
   19668              : void
   19669       423330 : gfc_unset_implicit_pure (gfc_symbol *sym)
   19670              : {
   19671       423330 :   gfc_namespace *ns;
   19672              : 
   19673       423330 :   if (sym == NULL)
   19674              :     {
   19675              :       /* Check if the current procedure is implicit_pure.  Walk up
   19676              :          the procedure list until we find a procedure.  */
   19677       691853 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
   19678              :         {
   19679       427773 :           sym = ns->proc_name;
   19680       427773 :           if (sym == NULL)
   19681              :             return;
   19682              : 
   19683       426942 :           if (sym->attr.flavor == FL_PROCEDURE)
   19684              :             break;
   19685              :         }
   19686              :     }
   19687              : 
   19688       422499 :   if (sym->attr.flavor == FL_PROCEDURE)
   19689       150147 :     sym->attr.implicit_pure = 0;
   19690              :   else
   19691       272352 :     sym->attr.pure = 0;
   19692              : }
   19693              : 
   19694              : 
   19695              : /* Test whether the current procedure is elemental or not.  */
   19696              : 
   19697              : bool
   19698      1345924 : gfc_elemental (gfc_symbol *sym)
   19699              : {
   19700      1345924 :   symbol_attribute attr;
   19701              : 
   19702      1345924 :   if (sym == NULL)
   19703            0 :     sym = gfc_current_ns->proc_name;
   19704            0 :   if (sym == NULL)
   19705              :     return 0;
   19706      1345924 :   attr = sym->attr;
   19707              : 
   19708      1345924 :   return attr.flavor == FL_PROCEDURE && attr.elemental;
   19709              : }
   19710              : 
   19711              : 
   19712              : /* Warn about unused labels.  */
   19713              : 
   19714              : static void
   19715         4656 : warn_unused_fortran_label (gfc_st_label *label)
   19716              : {
   19717         4682 :   if (label == NULL)
   19718              :     return;
   19719              : 
   19720           27 :   warn_unused_fortran_label (label->left);
   19721              : 
   19722           27 :   if (label->defined == ST_LABEL_UNKNOWN)
   19723              :     return;
   19724              : 
   19725           26 :   switch (label->referenced)
   19726              :     {
   19727            2 :     case ST_LABEL_UNKNOWN:
   19728            2 :       gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
   19729              :                    label->value, &label->where);
   19730            2 :       break;
   19731              : 
   19732            1 :     case ST_LABEL_BAD_TARGET:
   19733            1 :       gfc_warning (OPT_Wunused_label,
   19734              :                    "Label %d at %L defined but cannot be used",
   19735              :                    label->value, &label->where);
   19736            1 :       break;
   19737              : 
   19738              :     default:
   19739              :       break;
   19740              :     }
   19741              : 
   19742           26 :   warn_unused_fortran_label (label->right);
   19743              : }
   19744              : 
   19745              : 
   19746              : /* Returns the sequence type of a symbol or sequence.  */
   19747              : 
   19748              : static seq_type
   19749         1076 : sequence_type (gfc_typespec ts)
   19750              : {
   19751         1076 :   seq_type result;
   19752         1076 :   gfc_component *c;
   19753              : 
   19754         1076 :   switch (ts.type)
   19755              :   {
   19756           49 :     case BT_DERIVED:
   19757              : 
   19758           49 :       if (ts.u.derived->components == NULL)
   19759              :         return SEQ_NONDEFAULT;
   19760              : 
   19761           49 :       result = sequence_type (ts.u.derived->components->ts);
   19762          103 :       for (c = ts.u.derived->components->next; c; c = c->next)
   19763           67 :         if (sequence_type (c->ts) != result)
   19764              :           return SEQ_MIXED;
   19765              : 
   19766              :       return result;
   19767              : 
   19768          129 :     case BT_CHARACTER:
   19769          129 :       if (ts.kind != gfc_default_character_kind)
   19770            0 :           return SEQ_NONDEFAULT;
   19771              : 
   19772              :       return SEQ_CHARACTER;
   19773              : 
   19774          240 :     case BT_INTEGER:
   19775          240 :       if (ts.kind != gfc_default_integer_kind)
   19776           25 :           return SEQ_NONDEFAULT;
   19777              : 
   19778              :       return SEQ_NUMERIC;
   19779              : 
   19780          559 :     case BT_REAL:
   19781          559 :       if (!(ts.kind == gfc_default_real_kind
   19782          269 :             || ts.kind == gfc_default_double_kind))
   19783            0 :           return SEQ_NONDEFAULT;
   19784              : 
   19785              :       return SEQ_NUMERIC;
   19786              : 
   19787           81 :     case BT_COMPLEX:
   19788           81 :       if (ts.kind != gfc_default_complex_kind)
   19789           48 :           return SEQ_NONDEFAULT;
   19790              : 
   19791              :       return SEQ_NUMERIC;
   19792              : 
   19793           17 :     case BT_LOGICAL:
   19794           17 :       if (ts.kind != gfc_default_logical_kind)
   19795            0 :           return SEQ_NONDEFAULT;
   19796              : 
   19797              :       return SEQ_NUMERIC;
   19798              : 
   19799              :     default:
   19800              :       return SEQ_NONDEFAULT;
   19801              :   }
   19802              : }
   19803              : 
   19804              : 
   19805              : /* Resolve derived type EQUIVALENCE object.  */
   19806              : 
   19807              : static bool
   19808           80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
   19809              : {
   19810           80 :   gfc_component *c = derived->components;
   19811              : 
   19812           80 :   if (!derived)
   19813              :     return true;
   19814              : 
   19815              :   /* Shall not be an object of nonsequence derived type.  */
   19816           80 :   if (!derived->attr.sequence)
   19817              :     {
   19818            0 :       gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
   19819              :                  "attribute to be an EQUIVALENCE object", sym->name,
   19820              :                  &e->where);
   19821            0 :       return false;
   19822              :     }
   19823              : 
   19824              :   /* Shall not have allocatable components.  */
   19825           80 :   if (derived->attr.alloc_comp)
   19826              :     {
   19827            1 :       gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
   19828              :                  "components to be an EQUIVALENCE object",sym->name,
   19829              :                  &e->where);
   19830            1 :       return false;
   19831              :     }
   19832              : 
   19833           79 :   if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
   19834              :     {
   19835            1 :       gfc_error ("Derived type variable %qs at %L with default "
   19836              :                  "initialization cannot be in EQUIVALENCE with a variable "
   19837              :                  "in COMMON", sym->name, &e->where);
   19838            1 :       return false;
   19839              :     }
   19840              : 
   19841          245 :   for (; c ; c = c->next)
   19842              :     {
   19843          167 :       if (gfc_bt_struct (c->ts.type)
   19844          167 :           && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
   19845              :         return false;
   19846              : 
   19847              :       /* Shall not be an object of sequence derived type containing a pointer
   19848              :          in the structure.  */
   19849          167 :       if (c->attr.pointer)
   19850              :         {
   19851            0 :           gfc_error ("Derived type variable %qs at %L with pointer "
   19852              :                      "component(s) cannot be an EQUIVALENCE object",
   19853              :                      sym->name, &e->where);
   19854            0 :           return false;
   19855              :         }
   19856              :     }
   19857              :   return true;
   19858              : }
   19859              : 
   19860              : 
   19861              : /* Resolve equivalence object.
   19862              :    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
   19863              :    an allocatable array, an object of nonsequence derived type, an object of
   19864              :    sequence derived type containing a pointer at any level of component
   19865              :    selection, an automatic object, a function name, an entry name, a result
   19866              :    name, a named constant, a structure component, or a subobject of any of
   19867              :    the preceding objects.  A substring shall not have length zero.  A
   19868              :    derived type shall not have components with default initialization nor
   19869              :    shall two objects of an equivalence group be initialized.
   19870              :    Either all or none of the objects shall have an protected attribute.
   19871              :    The simple constraints are done in symbol.cc(check_conflict) and the rest
   19872              :    are implemented here.  */
   19873              : 
   19874              : static void
   19875         1565 : resolve_equivalence (gfc_equiv *eq)
   19876              : {
   19877         1565 :   gfc_symbol *sym;
   19878         1565 :   gfc_symbol *first_sym;
   19879         1565 :   gfc_expr *e;
   19880         1565 :   gfc_ref *r;
   19881         1565 :   locus *last_where = NULL;
   19882         1565 :   seq_type eq_type, last_eq_type;
   19883         1565 :   gfc_typespec *last_ts;
   19884         1565 :   int object, cnt_protected;
   19885         1565 :   const char *msg;
   19886              : 
   19887         1565 :   last_ts = &eq->expr->symtree->n.sym->ts;
   19888              : 
   19889         1565 :   first_sym = eq->expr->symtree->n.sym;
   19890              : 
   19891         1565 :   cnt_protected = 0;
   19892              : 
   19893         4727 :   for (object = 1; eq; eq = eq->eq, object++)
   19894              :     {
   19895         3171 :       e = eq->expr;
   19896              : 
   19897         3171 :       e->ts = e->symtree->n.sym->ts;
   19898              :       /* match_varspec might not know yet if it is seeing
   19899              :          array reference or substring reference, as it doesn't
   19900              :          know the types.  */
   19901         3171 :       if (e->ref && e->ref->type == REF_ARRAY)
   19902              :         {
   19903         2152 :           gfc_ref *ref = e->ref;
   19904         2152 :           sym = e->symtree->n.sym;
   19905              : 
   19906         2152 :           if (sym->attr.dimension)
   19907              :             {
   19908         1855 :               ref->u.ar.as = sym->as;
   19909         1855 :               ref = ref->next;
   19910              :             }
   19911              : 
   19912              :           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
   19913         2152 :           if (e->ts.type == BT_CHARACTER
   19914          592 :               && ref
   19915          371 :               && ref->type == REF_ARRAY
   19916          371 :               && ref->u.ar.dimen == 1
   19917          371 :               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
   19918          371 :               && ref->u.ar.stride[0] == NULL)
   19919              :             {
   19920          370 :               gfc_expr *start = ref->u.ar.start[0];
   19921          370 :               gfc_expr *end = ref->u.ar.end[0];
   19922          370 :               void *mem = NULL;
   19923              : 
   19924              :               /* Optimize away the (:) reference.  */
   19925          370 :               if (start == NULL && end == NULL)
   19926              :                 {
   19927            9 :                   if (e->ref == ref)
   19928            0 :                     e->ref = ref->next;
   19929              :                   else
   19930            9 :                     e->ref->next = ref->next;
   19931              :                   mem = ref;
   19932              :                 }
   19933              :               else
   19934              :                 {
   19935          361 :                   ref->type = REF_SUBSTRING;
   19936          361 :                   if (start == NULL)
   19937            9 :                     start = gfc_get_int_expr (gfc_charlen_int_kind,
   19938              :                                               NULL, 1);
   19939          361 :                   ref->u.ss.start = start;
   19940          361 :                   if (end == NULL && e->ts.u.cl)
   19941           27 :                     end = gfc_copy_expr (e->ts.u.cl->length);
   19942          361 :                   ref->u.ss.end = end;
   19943          361 :                   ref->u.ss.length = e->ts.u.cl;
   19944          361 :                   e->ts.u.cl = NULL;
   19945              :                 }
   19946          370 :               ref = ref->next;
   19947          370 :               free (mem);
   19948              :             }
   19949              : 
   19950              :           /* Any further ref is an error.  */
   19951         1930 :           if (ref)
   19952              :             {
   19953            1 :               gcc_assert (ref->type == REF_ARRAY);
   19954            1 :               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
   19955              :                          &ref->u.ar.where);
   19956            1 :               continue;
   19957              :             }
   19958              :         }
   19959              : 
   19960         3170 :       if (!gfc_resolve_expr (e))
   19961            2 :         continue;
   19962              : 
   19963         3168 :       sym = e->symtree->n.sym;
   19964              : 
   19965         3168 :       if (sym->attr.is_protected)
   19966            2 :         cnt_protected++;
   19967         3168 :       if (cnt_protected > 0 && cnt_protected != object)
   19968              :         {
   19969            2 :               gfc_error ("Either all or none of the objects in the "
   19970              :                          "EQUIVALENCE set at %L shall have the "
   19971              :                          "PROTECTED attribute",
   19972              :                          &e->where);
   19973            2 :               break;
   19974              :         }
   19975              : 
   19976              :       /* Shall not equivalence common block variables in a PURE procedure.  */
   19977         3166 :       if (sym->ns->proc_name
   19978         3150 :           && sym->ns->proc_name->attr.pure
   19979            7 :           && sym->attr.in_common)
   19980              :         {
   19981              :           /* Need to check for symbols that may have entered the pure
   19982              :              procedure via a USE statement.  */
   19983            7 :           bool saw_sym = false;
   19984            7 :           if (sym->ns->use_stmts)
   19985              :             {
   19986            6 :               gfc_use_rename *r;
   19987           10 :               for (r = sym->ns->use_stmts->rename; r; r = r->next)
   19988            4 :                 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
   19989              :             }
   19990              :           else
   19991              :             saw_sym = true;
   19992              : 
   19993            6 :           if (saw_sym)
   19994            3 :             gfc_error ("COMMON block member %qs at %L cannot be an "
   19995              :                        "EQUIVALENCE object in the pure procedure %qs",
   19996              :                        sym->name, &e->where, sym->ns->proc_name->name);
   19997              :           break;
   19998              :         }
   19999              : 
   20000              :       /* Shall not be a named constant.  */
   20001         3159 :       if (e->expr_type == EXPR_CONSTANT)
   20002              :         {
   20003            0 :           gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
   20004              :                      "object", sym->name, &e->where);
   20005            0 :           continue;
   20006              :         }
   20007              : 
   20008         3161 :       if (e->ts.type == BT_DERIVED
   20009         3159 :           && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
   20010            2 :         continue;
   20011              : 
   20012              :       /* Check that the types correspond correctly:
   20013              :          Note 5.28:
   20014              :          A numeric sequence structure may be equivalenced to another sequence
   20015              :          structure, an object of default integer type, default real type, double
   20016              :          precision real type, default logical type such that components of the
   20017              :          structure ultimately only become associated to objects of the same
   20018              :          kind. A character sequence structure may be equivalenced to an object
   20019              :          of default character kind or another character sequence structure.
   20020              :          Other objects may be equivalenced only to objects of the same type and
   20021              :          kind parameters.  */
   20022              : 
   20023              :       /* Identical types are unconditionally OK.  */
   20024         3157 :       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
   20025         2677 :         goto identical_types;
   20026              : 
   20027          480 :       last_eq_type = sequence_type (*last_ts);
   20028          480 :       eq_type = sequence_type (sym->ts);
   20029              : 
   20030              :       /* Since the pair of objects is not of the same type, mixed or
   20031              :          non-default sequences can be rejected.  */
   20032              : 
   20033          480 :       msg = G_("Sequence %s with mixed components in EQUIVALENCE "
   20034              :                "statement at %L with different type objects");
   20035          481 :       if ((object ==2
   20036          480 :            && last_eq_type == SEQ_MIXED
   20037            7 :            && last_where
   20038            7 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20039          486 :           || (eq_type == SEQ_MIXED
   20040            6 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20041            1 :         continue;
   20042              : 
   20043          479 :       msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
   20044              :                "statement at %L with objects of different type");
   20045          483 :       if ((object ==2
   20046          479 :            && last_eq_type == SEQ_NONDEFAULT
   20047           50 :            && last_where
   20048           49 :            && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
   20049          525 :           || (eq_type == SEQ_NONDEFAULT
   20050           24 :               && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
   20051            4 :         continue;
   20052              : 
   20053          475 :       msg = G_("Non-CHARACTER object %qs in default CHARACTER "
   20054              :                "EQUIVALENCE statement at %L");
   20055          479 :       if (last_eq_type == SEQ_CHARACTER
   20056          475 :           && eq_type != SEQ_CHARACTER
   20057          475 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20058            4 :                 continue;
   20059              : 
   20060          471 :       msg = G_("Non-NUMERIC object %qs in default NUMERIC "
   20061              :                "EQUIVALENCE statement at %L");
   20062          473 :       if (last_eq_type == SEQ_NUMERIC
   20063          471 :           && eq_type != SEQ_NUMERIC
   20064          471 :           && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
   20065            2 :                 continue;
   20066              : 
   20067         3146 : identical_types:
   20068              : 
   20069         3146 :       last_ts =&sym->ts;
   20070         3146 :       last_where = &e->where;
   20071              : 
   20072         3146 :       if (!e->ref)
   20073         1003 :         continue;
   20074              : 
   20075              :       /* Shall not be an automatic array.  */
   20076         2143 :       if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
   20077              :         {
   20078            3 :           gfc_error ("Array %qs at %L with non-constant bounds cannot be "
   20079              :                      "an EQUIVALENCE object", sym->name, &e->where);
   20080            3 :           continue;
   20081              :         }
   20082              : 
   20083         2140 :       r = e->ref;
   20084         4326 :       while (r)
   20085              :         {
   20086              :           /* Shall not be a structure component.  */
   20087         2187 :           if (r->type == REF_COMPONENT)
   20088              :             {
   20089            0 :               gfc_error ("Structure component %qs at %L cannot be an "
   20090              :                          "EQUIVALENCE object",
   20091            0 :                          r->u.c.component->name, &e->where);
   20092            0 :               break;
   20093              :             }
   20094              : 
   20095              :           /* A substring shall not have length zero.  */
   20096         2187 :           if (r->type == REF_SUBSTRING)
   20097              :             {
   20098          341 :               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
   20099              :                 {
   20100            1 :                   gfc_error ("Substring at %L has length zero",
   20101              :                              &r->u.ss.start->where);
   20102            1 :                   break;
   20103              :                 }
   20104              :             }
   20105         2186 :           r = r->next;
   20106              :         }
   20107              :     }
   20108         1565 : }
   20109              : 
   20110              : 
   20111              : /* Function called by resolve_fntype to flag other symbols used in the
   20112              :    length type parameter specification of function results.  */
   20113              : 
   20114              : static bool
   20115         4208 : flag_fn_result_spec (gfc_expr *expr,
   20116              :                      gfc_symbol *sym,
   20117              :                      int *f ATTRIBUTE_UNUSED)
   20118              : {
   20119         4208 :   gfc_namespace *ns;
   20120         4208 :   gfc_symbol *s;
   20121              : 
   20122         4208 :   if (expr->expr_type == EXPR_VARIABLE)
   20123              :     {
   20124         1378 :       s = expr->symtree->n.sym;
   20125         2153 :       for (ns = s->ns; ns; ns = ns->parent)
   20126         2153 :         if (!ns->parent)
   20127              :           break;
   20128              : 
   20129         1378 :       if (sym == s)
   20130              :         {
   20131            1 :           gfc_error ("Self reference in character length expression "
   20132              :                      "for %qs at %L", sym->name, &expr->where);
   20133            1 :           return true;
   20134              :         }
   20135              : 
   20136         1377 :       if (!s->fn_result_spec
   20137         1377 :           && s->attr.flavor == FL_PARAMETER)
   20138              :         {
   20139              :           /* Function contained in a module.... */
   20140           63 :           if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
   20141              :             {
   20142           32 :               gfc_symtree *st;
   20143           32 :               s->fn_result_spec = 1;
   20144              :               /* Make sure that this symbol is translated as a module
   20145              :                  variable.  */
   20146           32 :               st = gfc_get_unique_symtree (ns);
   20147           32 :               st->n.sym = s;
   20148           32 :               s->refs++;
   20149           32 :             }
   20150              :           /* ... which is use associated and called.  */
   20151           31 :           else if (s->attr.use_assoc || s->attr.used_in_submodule
   20152            0 :                         ||
   20153              :                   /* External function matched with an interface.  */
   20154            0 :                   (s->ns->proc_name
   20155            0 :                    && ((s->ns == ns
   20156            0 :                          && s->ns->proc_name->attr.if_source == IFSRC_DECL)
   20157            0 :                        || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20158            0 :                    && s->ns->proc_name->attr.function))
   20159           31 :             s->fn_result_spec = 1;
   20160              :         }
   20161              :     }
   20162              :   return false;
   20163              : }
   20164              : 
   20165              : 
   20166              : /* Resolve function and ENTRY types, issue diagnostics if needed.  */
   20167              : 
   20168              : static void
   20169       343587 : resolve_fntype (gfc_namespace *ns)
   20170              : {
   20171       343587 :   gfc_entry_list *el;
   20172       343587 :   gfc_symbol *sym;
   20173              : 
   20174       343587 :   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
   20175              :     return;
   20176              : 
   20177              :   /* If there are any entries, ns->proc_name is the entry master
   20178              :      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
   20179       178951 :   if (ns->entries)
   20180          596 :     sym = ns->entries->sym;
   20181              :   else
   20182              :     sym = ns->proc_name;
   20183       178951 :   if (sym->result == sym
   20184       143843 :       && sym->ts.type == BT_UNKNOWN
   20185            6 :       && !gfc_set_default_type (sym, 0, NULL)
   20186       178955 :       && !sym->attr.untyped)
   20187              :     {
   20188            3 :       gfc_error ("Function %qs at %L has no IMPLICIT type",
   20189              :                  sym->name, &sym->declared_at);
   20190            3 :       sym->attr.untyped = 1;
   20191              :     }
   20192              : 
   20193        13660 :   if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
   20194         1813 :       && !sym->attr.contained
   20195          299 :       && !gfc_check_symbol_access (sym->ts.u.derived)
   20196       178951 :       && gfc_check_symbol_access (sym))
   20197              :     {
   20198            0 :       gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
   20199              :                       "%L of PRIVATE type %qs", sym->name,
   20200            0 :                       &sym->declared_at, sym->ts.u.derived->name);
   20201              :     }
   20202              : 
   20203       178951 :     if (ns->entries)
   20204         1253 :     for (el = ns->entries->next; el; el = el->next)
   20205              :       {
   20206          657 :         if (el->sym->result == el->sym
   20207          445 :             && el->sym->ts.type == BT_UNKNOWN
   20208            2 :             && !gfc_set_default_type (el->sym, 0, NULL)
   20209          659 :             && !el->sym->attr.untyped)
   20210              :           {
   20211            2 :             gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
   20212              :                        el->sym->name, &el->sym->declared_at);
   20213            2 :             el->sym->attr.untyped = 1;
   20214              :           }
   20215              :       }
   20216              : 
   20217       178951 :   if (sym->ts.type == BT_CHARACTER
   20218         6985 :       && sym->ts.u.cl->length
   20219         1860 :       && sym->ts.u.cl->length->ts.type == BT_INTEGER)
   20220         1855 :     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
   20221              : }
   20222              : 
   20223              : 
   20224              : /* 12.3.2.1.1 Defined operators.  */
   20225              : 
   20226              : static bool
   20227          452 : check_uop_procedure (gfc_symbol *sym, locus where)
   20228              : {
   20229          452 :   gfc_formal_arglist *formal;
   20230              : 
   20231          452 :   if (!sym->attr.function)
   20232              :     {
   20233            4 :       gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
   20234              :                  sym->name, &where);
   20235            4 :       return false;
   20236              :     }
   20237              : 
   20238          448 :   if (sym->ts.type == BT_CHARACTER
   20239           15 :       && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
   20240            2 :       && !(sym->result && ((sym->result->ts.u.cl
   20241            2 :            && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
   20242              :     {
   20243            2 :       gfc_error ("User operator procedure %qs at %L cannot be assumed "
   20244              :                  "character length", sym->name, &where);
   20245            2 :       return false;
   20246              :     }
   20247              : 
   20248          446 :   formal = gfc_sym_get_dummy_args (sym);
   20249          446 :   if (!formal || !formal->sym)
   20250              :     {
   20251            1 :       gfc_error ("User operator procedure %qs at %L must have at least "
   20252              :                  "one argument", sym->name, &where);
   20253            1 :       return false;
   20254              :     }
   20255              : 
   20256          445 :   if (formal->sym->attr.intent != INTENT_IN)
   20257              :     {
   20258            0 :       gfc_error ("First argument of operator interface at %L must be "
   20259              :                  "INTENT(IN)", &where);
   20260            0 :       return false;
   20261              :     }
   20262              : 
   20263          445 :   if (formal->sym->attr.optional)
   20264              :     {
   20265            0 :       gfc_error ("First argument of operator interface at %L cannot be "
   20266              :                  "optional", &where);
   20267            0 :       return false;
   20268              :     }
   20269              : 
   20270          445 :   formal = formal->next;
   20271          445 :   if (!formal || !formal->sym)
   20272              :     return true;
   20273              : 
   20274          295 :   if (formal->sym->attr.intent != INTENT_IN)
   20275              :     {
   20276            0 :       gfc_error ("Second argument of operator interface at %L must be "
   20277              :                  "INTENT(IN)", &where);
   20278            0 :       return false;
   20279              :     }
   20280              : 
   20281          295 :   if (formal->sym->attr.optional)
   20282              :     {
   20283            1 :       gfc_error ("Second argument of operator interface at %L cannot be "
   20284              :                  "optional", &where);
   20285            1 :       return false;
   20286              :     }
   20287              : 
   20288          294 :   if (formal->next)
   20289              :     {
   20290            2 :       gfc_error ("Operator interface at %L must have, at most, two "
   20291              :                  "arguments", &where);
   20292            2 :       return false;
   20293              :     }
   20294              : 
   20295              :   return true;
   20296              : }
   20297              : 
   20298              : static void
   20299       344347 : gfc_resolve_uops (gfc_symtree *symtree)
   20300              : {
   20301       344347 :   gfc_interface *itr;
   20302              : 
   20303       344347 :   if (symtree == NULL)
   20304              :     return;
   20305              : 
   20306          380 :   gfc_resolve_uops (symtree->left);
   20307          380 :   gfc_resolve_uops (symtree->right);
   20308              : 
   20309          773 :   for (itr = symtree->n.uop->op; itr; itr = itr->next)
   20310          393 :     check_uop_procedure (itr->sym, itr->sym->declared_at);
   20311              : }
   20312              : 
   20313              : 
   20314              : /* Examine all of the expressions associated with a program unit,
   20315              :    assign types to all intermediate expressions, make sure that all
   20316              :    assignments are to compatible types and figure out which names
   20317              :    refer to which functions or subroutines.  It doesn't check code
   20318              :    block, which is handled by gfc_resolve_code.  */
   20319              : 
   20320              : static void
   20321       346093 : resolve_types (gfc_namespace *ns)
   20322              : {
   20323       346093 :   gfc_namespace *n;
   20324       346093 :   gfc_charlen *cl;
   20325       346093 :   gfc_data *d;
   20326       346093 :   gfc_equiv *eq;
   20327       346093 :   gfc_namespace* old_ns = gfc_current_ns;
   20328       346093 :   bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
   20329              : 
   20330       346093 :   if (ns->types_resolved)
   20331              :     return;
   20332              : 
   20333              :   /* Check that all IMPLICIT types are ok.  */
   20334       343588 :   if (!ns->seen_implicit_none)
   20335              :     {
   20336              :       unsigned letter;
   20337      8644267 :       for (letter = 0; letter != GFC_LETTERS; ++letter)
   20338      8324109 :         if (ns->set_flag[letter]
   20339      8324109 :             && !resolve_typespec_used (&ns->default_type[letter],
   20340              :                                        &ns->implicit_loc[letter], NULL))
   20341              :           return;
   20342              :     }
   20343              : 
   20344       343587 :   gfc_current_ns = ns;
   20345              : 
   20346       343587 :   resolve_entries (ns);
   20347              : 
   20348       343587 :   resolve_common_vars (&ns->blank_common, false);
   20349       343587 :   resolve_common_blocks (ns->common_root);
   20350              : 
   20351       343587 :   resolve_contained_functions (ns);
   20352              : 
   20353       343587 :   if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
   20354       293735 :       && ns->proc_name->attr.if_source == IFSRC_IFBODY)
   20355       192043 :     gfc_resolve_formal_arglist (ns->proc_name);
   20356              : 
   20357       343587 :   gfc_traverse_ns (ns, resolve_bind_c_derived_types);
   20358              : 
   20359       438649 :   for (cl = ns->cl_list; cl; cl = cl->next)
   20360        95062 :     resolve_charlen (cl);
   20361              : 
   20362       343587 :   gfc_traverse_ns (ns, resolve_symbol);
   20363              : 
   20364       343587 :   resolve_fntype (ns);
   20365              : 
   20366       391446 :   for (n = ns->contained; n; n = n->sibling)
   20367              :     {
   20368              :       /* Exclude final wrappers with the test for the artificial attribute.  */
   20369        47859 :       if (gfc_pure (ns->proc_name)
   20370            5 :           && !gfc_pure (n->proc_name)
   20371        47859 :           && !n->proc_name->attr.artificial)
   20372            0 :         gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
   20373              :                    "also be PURE", n->proc_name->name,
   20374              :                    &n->proc_name->declared_at);
   20375              : 
   20376        47859 :       resolve_types (n);
   20377              :     }
   20378              : 
   20379       343587 :   forall_flag = 0;
   20380       343587 :   gfc_do_concurrent_flag = 0;
   20381       343587 :   gfc_check_interfaces (ns);
   20382              : 
   20383       343587 :   gfc_traverse_ns (ns, resolve_values);
   20384              : 
   20385       343587 :   if (ns->save_all || (!flag_automatic && !recursive))
   20386          313 :     gfc_save_all (ns);
   20387              : 
   20388       343587 :   iter_stack = NULL;
   20389       346105 :   for (d = ns->data; d; d = d->next)
   20390         2518 :     resolve_data (d);
   20391              : 
   20392       343587 :   iter_stack = NULL;
   20393       343587 :   gfc_traverse_ns (ns, gfc_formalize_init_value);
   20394              : 
   20395       343587 :   gfc_traverse_ns (ns, gfc_verify_binding_labels);
   20396              : 
   20397       345152 :   for (eq = ns->equiv; eq; eq = eq->next)
   20398         1565 :     resolve_equivalence (eq);
   20399              : 
   20400              :   /* Warn about unused labels.  */
   20401       343587 :   if (warn_unused_label)
   20402         4629 :     warn_unused_fortran_label (ns->st_labels);
   20403              : 
   20404       343587 :   gfc_resolve_uops (ns->uop_root);
   20405              : 
   20406       343587 :   gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
   20407              : 
   20408       343587 :   gfc_resolve_omp_declare (ns);
   20409              : 
   20410       343587 :   gfc_resolve_omp_udrs (ns->omp_udr_root);
   20411              : 
   20412       343587 :   ns->types_resolved = 1;
   20413              : 
   20414       343587 :   gfc_current_ns = old_ns;
   20415              : }
   20416              : 
   20417              : 
   20418              : /* Call gfc_resolve_code recursively.  */
   20419              : 
   20420              : static void
   20421       346149 : resolve_codes (gfc_namespace *ns)
   20422              : {
   20423       346149 :   gfc_namespace *n;
   20424       346149 :   bitmap_obstack old_obstack;
   20425              : 
   20426       346149 :   if (ns->resolved == 1)
   20427        13990 :     return;
   20428              : 
   20429       380074 :   for (n = ns->contained; n; n = n->sibling)
   20430        47915 :     resolve_codes (n);
   20431              : 
   20432       332159 :   gfc_current_ns = ns;
   20433              : 
   20434              :   /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct.  */
   20435       332159 :   if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
   20436       320089 :     cs_base = NULL;
   20437              : 
   20438              :   /* Set to an out of range value.  */
   20439       332159 :   current_entry_id = -1;
   20440              : 
   20441       332159 :   old_obstack = labels_obstack;
   20442       332159 :   bitmap_obstack_initialize (&labels_obstack);
   20443              : 
   20444       332159 :   gfc_resolve_oacc_declare (ns);
   20445       332159 :   gfc_resolve_oacc_routines (ns);
   20446       332159 :   gfc_resolve_omp_local_vars (ns);
   20447       332159 :   if (ns->omp_allocate)
   20448           62 :     gfc_resolve_omp_allocate (ns, ns->omp_allocate);
   20449       332159 :   gfc_resolve_code (ns->code, ns);
   20450              : 
   20451       332158 :   bitmap_obstack_release (&labels_obstack);
   20452       332158 :   labels_obstack = old_obstack;
   20453              : }
   20454              : 
   20455              : 
   20456              : /* This function is called after a complete program unit has been compiled.
   20457              :    Its purpose is to examine all of the expressions associated with a program
   20458              :    unit, assign types to all intermediate expressions, make sure that all
   20459              :    assignments are to compatible types and figure out which names refer to
   20460              :    which functions or subroutines.  */
   20461              : 
   20462              : void
   20463       302988 : gfc_resolve (gfc_namespace *ns)
   20464              : {
   20465       302988 :   gfc_namespace *old_ns;
   20466       302988 :   code_stack *old_cs_base;
   20467       302988 :   struct gfc_omp_saved_state old_omp_state;
   20468              : 
   20469       302988 :   if (ns->resolved)
   20470         4754 :     return;
   20471              : 
   20472       298234 :   ns->resolved = -1;
   20473       298234 :   old_ns = gfc_current_ns;
   20474       298234 :   old_cs_base = cs_base;
   20475              : 
   20476              :   /* As gfc_resolve can be called during resolution of an OpenMP construct
   20477              :      body, we should clear any state associated to it, so that say NS's
   20478              :      DO loops are not interpreted as OpenMP loops.  */
   20479       298234 :   if (!ns->construct_entities)
   20480       286164 :     gfc_omp_save_and_clear_state (&old_omp_state);
   20481              : 
   20482       298234 :   resolve_types (ns);
   20483       298234 :   component_assignment_level = 0;
   20484       298234 :   resolve_codes (ns);
   20485              : 
   20486       298233 :   if (ns->omp_assumes)
   20487           13 :     gfc_resolve_omp_assumptions (ns->omp_assumes);
   20488              : 
   20489       298233 :   gfc_current_ns = old_ns;
   20490       298233 :   cs_base = old_cs_base;
   20491       298233 :   ns->resolved = 1;
   20492              : 
   20493       298233 :   gfc_run_passes (ns);
   20494              : 
   20495       298233 :   if (!ns->construct_entities)
   20496       286163 :     gfc_omp_restore_state (&old_omp_state);
   20497              : }
        

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.